The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/mips/mips/freebsd32_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2012 Juli Mallett <jmallett@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD: releng/10.0/sys/mips/mips/freebsd32_machdep.c 232896 2012-03-12 21:25:32Z jmallett $
   27  */
   28 
   29 /*
   30  * Based on nwhitehorn's COMPAT_FREEBSD32 support code for PowerPC64.
   31  */
   32 
   33 #include "opt_compat.h"
   34 
   35 #define __ELF_WORD_SIZE 32
   36 
   37 #include <sys/types.h>
   38 #include <sys/param.h>
   39 #include <sys/kernel.h>
   40 #include <sys/systm.h>
   41 #include <sys/sysent.h>
   42 #include <sys/exec.h>
   43 #include <sys/imgact.h>
   44 #include <sys/malloc.h>
   45 #include <sys/proc.h>
   46 #include <sys/namei.h>
   47 #include <sys/fcntl.h>
   48 #include <sys/sysent.h>
   49 #include <sys/imgact_elf.h>
   50 #include <sys/syscall.h>
   51 #include <sys/syscallsubr.h>
   52 #include <sys/sysproto.h>
   53 #include <sys/signalvar.h>
   54 #include <sys/vnode.h>
   55 #include <sys/linker.h>
   56 
   57 #include <vm/vm.h>
   58 #include <vm/vm_param.h>
   59 
   60 #include <machine/md_var.h>
   61 #include <machine/reg.h>
   62 #include <machine/sigframe.h>
   63 #include <machine/sysarch.h>
   64 
   65 #include <compat/freebsd32/freebsd32_signal.h>
   66 #include <compat/freebsd32/freebsd32_util.h>
   67 #include <compat/freebsd32/freebsd32_proto.h>
   68 
   69 static void freebsd32_exec_setregs(struct thread *, struct image_params *, u_long);
   70 static int get_mcontext32(struct thread *, mcontext32_t *, int);
   71 static int set_mcontext32(struct thread *, const mcontext32_t *);
   72 static void freebsd32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
   73 
   74 extern const char *freebsd32_syscallnames[];
   75 
   76 struct sysentvec elf32_freebsd_sysvec = {
   77         .sv_size        = SYS_MAXSYSCALL,
   78         .sv_table       = freebsd32_sysent,
   79         .sv_mask        = 0,
   80         .sv_sigsize     = 0,
   81         .sv_sigtbl      = NULL,
   82         .sv_errsize     = 0,
   83         .sv_errtbl      = NULL,
   84         .sv_transtrap   = NULL,
   85         .sv_fixup       = __elfN(freebsd_fixup),
   86         .sv_sendsig     = freebsd32_sendsig,
   87         .sv_sigcode     = sigcode32,
   88         .sv_szsigcode   = &szsigcode32,
   89         .sv_prepsyscall = NULL,
   90         .sv_name        = "FreeBSD ELF32",
   91         .sv_coredump    = __elfN(coredump),
   92         .sv_imgact_try  = NULL,
   93         .sv_minsigstksz = MINSIGSTKSZ,
   94         .sv_pagesize    = PAGE_SIZE,
   95         .sv_minuser     = VM_MIN_ADDRESS,
   96         .sv_maxuser     = ((vm_offset_t)0x80000000),
   97         .sv_usrstack    = FREEBSD32_USRSTACK,
   98         .sv_psstrings   = FREEBSD32_PS_STRINGS,
   99         .sv_stackprot   = VM_PROT_ALL,
  100         .sv_copyout_strings = freebsd32_copyout_strings,
  101         .sv_setregs     = freebsd32_exec_setregs,
  102         .sv_fixlimit    = NULL,
  103         .sv_maxssiz     = NULL,
  104         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32,
  105         .sv_set_syscall_retval = cpu_set_syscall_retval,
  106         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
  107         .sv_syscallnames = freebsd32_syscallnames,
  108         .sv_schedtail   = NULL,
  109 };
  110 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
  111 
  112 static Elf32_Brandinfo freebsd_brand_info = {
  113         .brand          = ELFOSABI_FREEBSD,
  114         .machine        = EM_MIPS,
  115         .compat_3_brand = "FreeBSD",
  116         .emul_path      = NULL,
  117         .interp_path    = "/libexec/ld-elf.so.1",
  118         .sysvec         = &elf32_freebsd_sysvec,
  119         .interp_newpath = "/libexec/ld-elf32.so.1",
  120         .flags          = 0
  121 };
  122 
  123 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
  124     (sysinit_cfunc_t) elf32_insert_brand_entry,
  125     &freebsd_brand_info);
  126 
  127 static void
  128 freebsd32_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
  129 {
  130         exec_setregs(td, imgp, stack);
  131 
  132         /*
  133          * See comment in exec_setregs about running 32-bit binaries with 64-bit
  134          * registers.
  135          */
  136         td->td_frame->sp -= 65536;
  137 
  138         /*
  139          * Clear extended address space bit for userland.
  140          */
  141         td->td_frame->sr &= ~MIPS_SR_UX;
  142 }
  143 
  144 int
  145 set_regs32(struct thread *td, struct reg32 *regs)
  146 {
  147         struct reg r;
  148         unsigned i;
  149 
  150         for (i = 0; i < NUMSAVEREGS; i++)
  151                 r.r_regs[i] = regs->r_regs[i];
  152 
  153         return (set_regs(td, &r));
  154 }
  155 
  156 int
  157 fill_regs32(struct thread *td, struct reg32 *regs)
  158 {
  159         struct reg r;
  160         unsigned i;
  161         int error;
  162 
  163         error = fill_regs(td, &r);
  164         if (error != 0)
  165                 return (error);
  166 
  167         for (i = 0; i < NUMSAVEREGS; i++)
  168                 regs->r_regs[i] = r.r_regs[i];
  169 
  170         return (0);
  171 }
  172 
  173 int
  174 set_fpregs32(struct thread *td, struct fpreg32 *fpregs)
  175 {
  176         struct fpreg fp;
  177         unsigned i;
  178 
  179         for (i = 0; i < NUMFPREGS; i++)
  180                 fp.r_regs[i] = fpregs->r_regs[i];
  181 
  182         return (set_fpregs(td, &fp));
  183 }
  184 
  185 int
  186 fill_fpregs32(struct thread *td, struct fpreg32 *fpregs)
  187 {
  188         struct fpreg fp;
  189         unsigned i;
  190         int error;
  191 
  192         error = fill_fpregs(td, &fp);
  193         if (error != 0)
  194                 return (error);
  195 
  196         for (i = 0; i < NUMFPREGS; i++)
  197                 fpregs->r_regs[i] = fp.r_regs[i];
  198 
  199         return (0);
  200 }
  201 
  202 static int
  203 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
  204 {
  205         mcontext_t mcp64;
  206         unsigned i;
  207         int error;
  208 
  209         error = get_mcontext(td, &mcp64, flags);
  210         if (error != 0)
  211                 return (error);
  212 
  213         mcp->mc_onstack = mcp64.mc_onstack;
  214         mcp->mc_pc = mcp64.mc_pc;
  215         for (i = 0; i < 32; i++)
  216                 mcp->mc_regs[i] = mcp64.mc_regs[i];
  217         mcp->sr = mcp64.sr;
  218         mcp->mullo = mcp64.mullo;
  219         mcp->mulhi = mcp64.mulhi;
  220         mcp->mc_fpused = mcp64.mc_fpused;
  221         for (i = 0; i < 33; i++)
  222                 mcp->mc_fpregs[i] = mcp64.mc_fpregs[i];
  223         mcp->mc_fpc_eir = mcp64.mc_fpc_eir;
  224         mcp->mc_tls = (int32_t)(intptr_t)mcp64.mc_tls;
  225 
  226         return (0);
  227 }
  228 
  229 static int
  230 set_mcontext32(struct thread *td, const mcontext32_t *mcp)
  231 {
  232         mcontext_t mcp64;
  233         unsigned i;
  234 
  235         mcp64.mc_onstack = mcp->mc_onstack;
  236         mcp64.mc_pc = mcp->mc_pc;
  237         for (i = 0; i < 32; i++)
  238                 mcp64.mc_regs[i] = mcp->mc_regs[i];
  239         mcp64.sr = mcp->sr;
  240         mcp64.mullo = mcp->mullo;
  241         mcp64.mulhi = mcp->mulhi;
  242         mcp64.mc_fpused = mcp->mc_fpused;
  243         for (i = 0; i < 33; i++)
  244                 mcp64.mc_fpregs[i] = mcp->mc_fpregs[i];
  245         mcp64.mc_fpc_eir = mcp->mc_fpc_eir;
  246         mcp64.mc_tls = (void *)(intptr_t)mcp->mc_tls;
  247 
  248         return (set_mcontext(td, &mcp64));
  249 }
  250 
  251 int
  252 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
  253 {
  254         ucontext32_t uc;
  255         int error;
  256 
  257         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
  258 
  259         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
  260                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
  261                 return (EFAULT);
  262         }
  263 
  264         error = set_mcontext32(td, &uc.uc_mcontext);
  265         if (error != 0)
  266                 return (error);
  267 
  268         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
  269 
  270 #if 0
  271         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
  272              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
  273 #endif
  274 
  275         return (EJUSTRETURN);
  276 }
  277 
  278 /*
  279  * The first two fields of a ucontext_t are the signal mask and the machine
  280  * context.  The next field is uc_link; we want to avoid destroying the link
  281  * when copying out contexts.
  282  */
  283 #define UC32_COPY_SIZE  offsetof(ucontext32_t, uc_link)
  284 
  285 int
  286 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
  287 {
  288         ucontext32_t uc;
  289         int ret;
  290 
  291         if (uap->ucp == NULL)
  292                 ret = EINVAL;
  293         else {
  294                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
  295                 PROC_LOCK(td->td_proc);
  296                 uc.uc_sigmask = td->td_sigmask;
  297                 PROC_UNLOCK(td->td_proc);
  298                 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
  299         }
  300         return (ret);
  301 }
  302 
  303 int
  304 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
  305 {
  306         ucontext32_t uc;
  307         int ret;        
  308 
  309         if (uap->ucp == NULL)
  310                 ret = EINVAL;
  311         else {
  312                 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
  313                 if (ret == 0) {
  314                         ret = set_mcontext32(td, &uc.uc_mcontext);
  315                         if (ret == 0) {
  316                                 kern_sigprocmask(td, SIG_SETMASK,
  317                                     &uc.uc_sigmask, NULL, 0);
  318                         }
  319                 }
  320         }
  321         return (ret == 0 ? EJUSTRETURN : ret);
  322 }
  323 
  324 int
  325 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
  326 {
  327         ucontext32_t uc;
  328         int ret;
  329 
  330         if (uap->oucp == NULL || uap->ucp == NULL)
  331                 ret = EINVAL;
  332         else {
  333                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
  334                 PROC_LOCK(td->td_proc);
  335                 uc.uc_sigmask = td->td_sigmask;
  336                 PROC_UNLOCK(td->td_proc);
  337                 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
  338                 if (ret == 0) {
  339                         ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
  340                         if (ret == 0) {
  341                                 ret = set_mcontext32(td, &uc.uc_mcontext);
  342                                 if (ret == 0) {
  343                                         kern_sigprocmask(td, SIG_SETMASK,
  344                                             &uc.uc_sigmask, NULL, 0);
  345                                 }
  346                         }
  347                 }
  348         }
  349         return (ret == 0 ? EJUSTRETURN : ret);
  350 }
  351 
  352 #define UCONTEXT_MAGIC  0xACEDBADE
  353 
  354 /*
  355  * Send an interrupt to process.
  356  *
  357  * Stack is set up to allow sigcode stored
  358  * at top to call routine, followed by kcall
  359  * to sigreturn routine below.  After sigreturn
  360  * resets the signal mask, the stack, and the
  361  * frame pointer, it returns to the user
  362  * specified pc, psl.
  363  */
  364 static void
  365 freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
  366 {
  367         struct proc *p;
  368         struct thread *td;
  369         struct fpreg32 fpregs;
  370         struct reg32 regs;
  371         struct sigacts *psp;
  372         struct sigframe32 sf, *sfp;
  373         int sig;
  374         int oonstack;
  375         unsigned i;
  376 
  377         td = curthread;
  378         p = td->td_proc;
  379         PROC_LOCK_ASSERT(p, MA_OWNED);
  380         sig = ksi->ksi_signo;
  381         psp = p->p_sigacts;
  382         mtx_assert(&psp->ps_mtx, MA_OWNED);
  383 
  384         fill_regs32(td, &regs);
  385         oonstack = sigonstack(td->td_frame->sp);
  386 
  387         /* save user context */
  388         bzero(&sf, sizeof sf);
  389         sf.sf_uc.uc_sigmask = *mask;
  390         sf.sf_uc.uc_stack.ss_sp = (int32_t)(intptr_t)td->td_sigstk.ss_sp;
  391         sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
  392         sf.sf_uc.uc_stack.ss_flags = td->td_sigstk.ss_flags;
  393         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
  394         sf.sf_uc.uc_mcontext.mc_pc = regs.r_regs[PC];
  395         sf.sf_uc.uc_mcontext.mullo = regs.r_regs[MULLO];
  396         sf.sf_uc.uc_mcontext.mulhi = regs.r_regs[MULHI];
  397         sf.sf_uc.uc_mcontext.mc_tls = (int32_t)(intptr_t)td->td_md.md_tls;
  398         sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
  399         for (i = 1; i < 32; i++)
  400                 sf.sf_uc.uc_mcontext.mc_regs[i] = regs.r_regs[i];
  401         sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
  402         if (sf.sf_uc.uc_mcontext.mc_fpused) {
  403                 /* if FPU has current state, save it first */
  404                 if (td == PCPU_GET(fpcurthread))
  405                         MipsSaveCurFPState(td);
  406                 fill_fpregs32(td, &fpregs);
  407                 for (i = 0; i < 33; i++)
  408                         sf.sf_uc.uc_mcontext.mc_fpregs[i] = fpregs.r_regs[i];
  409         }
  410 
  411         /* Allocate and validate space for the signal handler context. */
  412         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
  413             SIGISMEMBER(psp->ps_sigonstack, sig)) {
  414                 sfp = (struct sigframe32 *)((vm_offset_t)(td->td_sigstk.ss_sp +
  415                     td->td_sigstk.ss_size - sizeof(struct sigframe32))
  416                     & ~(sizeof(__int64_t) - 1));
  417         } else
  418                 sfp = (struct sigframe32 *)((vm_offset_t)(td->td_frame->sp - 
  419                     sizeof(struct sigframe32)) & ~(sizeof(__int64_t) - 1));
  420 
  421         /* Translate the signal if appropriate */
  422         if (p->p_sysent->sv_sigtbl) {
  423                 if (sig <= p->p_sysent->sv_sigsize)
  424                         sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
  425         }
  426 
  427         /* Build the argument list for the signal handler. */
  428         td->td_frame->a0 = sig;
  429         td->td_frame->a2 = (register_t)(intptr_t)&sfp->sf_uc;
  430         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
  431                 /* Signal handler installed with SA_SIGINFO. */
  432                 td->td_frame->a1 = (register_t)(intptr_t)&sfp->sf_si;
  433                 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
  434 
  435                 /* fill siginfo structure */
  436                 sf.sf_si.si_signo = sig;
  437                 sf.sf_si.si_code = ksi->ksi_code;
  438                 sf.sf_si.si_addr = td->td_frame->badvaddr;
  439         } else {
  440                 /* Old FreeBSD-style arguments. */
  441                 td->td_frame->a1 = ksi->ksi_code;
  442                 td->td_frame->a3 = td->td_frame->badvaddr;
  443                 /* sf.sf_ahu.sf_handler = catcher; */
  444         }
  445 
  446         mtx_unlock(&psp->ps_mtx);
  447         PROC_UNLOCK(p);
  448 
  449         /*
  450          * Copy the sigframe out to the user's stack.
  451          */
  452         if (copyout(&sf, sfp, sizeof(struct sigframe32)) != 0) {
  453                 /*
  454                  * Something is wrong with the stack pointer.
  455                  * ...Kill the process.
  456                  */
  457                 PROC_LOCK(p);
  458                 sigexit(td, SIGILL);
  459         }
  460 
  461         td->td_frame->pc = (register_t)(intptr_t)catcher;
  462         td->td_frame->t9 = (register_t)(intptr_t)catcher;
  463         td->td_frame->sp = (register_t)(intptr_t)sfp;
  464         /*
  465          * Signal trampoline code is at base of user stack.
  466          */
  467         td->td_frame->ra = (register_t)(intptr_t)FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
  468         PROC_LOCK(p);
  469         mtx_lock(&psp->ps_mtx);
  470 }
  471 
  472 int
  473 freebsd32_sysarch(struct thread *td, struct freebsd32_sysarch_args *uap)
  474 {
  475         int error;
  476         int32_t tlsbase;
  477 
  478         switch (uap->op) {
  479         case MIPS_SET_TLS:
  480                 td->td_md.md_tls = (void *)(intptr_t)uap->parms;
  481                 return (0);
  482         case MIPS_GET_TLS: 
  483                 tlsbase = (int32_t)(intptr_t)td->td_md.md_tls;
  484                 error = copyout(&tlsbase, uap->parms, sizeof(tlsbase));
  485                 return (error);
  486         default:
  487                 break;
  488         }
  489         return (EINVAL);
  490 }
  491 
  492 void
  493 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
  494     size_t *off __unused)
  495 {
  496 }

Cache object: c5b1a2dea3f07735a843a628b141bdf3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.