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

Cache object: ee3eed7b869c55c3f5db142c496d4b7f


[ 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.