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/ktr.h>
   45 #include <sys/malloc.h>
   46 #include <sys/proc.h>
   47 #include <sys/namei.h>
   48 #include <sys/fcntl.h>
   49 #include <sys/sysent.h>
   50 #include <sys/imgact_elf.h>
   51 #include <sys/syscall.h>
   52 #include <sys/syscallsubr.h>
   53 #include <sys/sysproto.h>
   54 #include <sys/signalvar.h>
   55 #include <sys/vnode.h>
   56 #include <sys/linker.h>
   57 
   58 #include <vm/vm.h>
   59 #include <vm/vm_param.h>
   60 
   61 #include <machine/cpuinfo.h>
   62 #include <machine/md_var.h>
   63 #include <machine/reg.h>
   64 #include <machine/sigframe.h>
   65 #include <machine/sysarch.h>
   66 #include <machine/tls.h>
   67 
   68 #include <compat/freebsd32/freebsd32_signal.h>
   69 #include <compat/freebsd32/freebsd32_util.h>
   70 #include <compat/freebsd32/freebsd32_proto.h>
   71 
   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_transtrap   = NULL,
   82         .sv_fixup       = __elfN(freebsd_fixup),
   83         .sv_sendsig     = freebsd32_sendsig,
   84         .sv_sigcode     = sigcode32,
   85         .sv_szsigcode   = &szsigcode32,
   86         .sv_name        = "FreeBSD ELF32",
   87         .sv_coredump    = __elfN(coredump),
   88         .sv_imgact_try  = NULL,
   89         .sv_minsigstksz = MINSIGSTKSZ,
   90         .sv_minuser     = VM_MIN_ADDRESS,
   91         .sv_maxuser     = ((vm_offset_t)0x80000000),
   92         .sv_usrstack    = FREEBSD32_USRSTACK,
   93         .sv_psstrings   = FREEBSD32_PS_STRINGS,
   94         .sv_stackprot   = VM_PROT_ALL,
   95         .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
   96         .sv_copyout_strings = freebsd32_copyout_strings,
   97         .sv_setregs     = exec_setregs,
   98         .sv_fixlimit    = NULL,
   99         .sv_maxssiz     = NULL,
  100         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32 | SV_RNG_SEED_VER,
  101         .sv_set_syscall_retval = cpu_set_syscall_retval,
  102         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
  103         .sv_syscallnames = freebsd32_syscallnames,
  104         .sv_schedtail   = NULL,
  105         .sv_thread_detach = NULL,
  106         .sv_trap        = NULL,
  107 };
  108 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
  109 
  110 static Elf32_Brandinfo freebsd_brand_info = {
  111         .brand          = ELFOSABI_FREEBSD,
  112         .machine        = EM_MIPS,
  113         .compat_3_brand = "FreeBSD",
  114         .emul_path      = NULL,
  115         .interp_path    = "/libexec/ld-elf.so.1",
  116         .sysvec         = &elf32_freebsd_sysvec,
  117         .interp_newpath = "/libexec/ld-elf32.so.1",
  118         .brand_note     = &elf32_freebsd_brandnote,
  119         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
  120 };
  121 
  122 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
  123     (sysinit_cfunc_t) elf32_insert_brand_entry,
  124     &freebsd_brand_info);
  125 
  126 int
  127 set_regs32(struct thread *td, struct reg32 *regs)
  128 {
  129         struct reg r;
  130         unsigned i;
  131 
  132         for (i = 0; i < NUMSAVEREGS; i++)
  133                 r.r_regs[i] = regs->r_regs[i];
  134 
  135         return (set_regs(td, &r));
  136 }
  137 
  138 int
  139 fill_regs32(struct thread *td, struct reg32 *regs)
  140 {
  141         struct reg r;
  142         unsigned i;
  143         int error;
  144 
  145         error = fill_regs(td, &r);
  146         if (error != 0)
  147                 return (error);
  148 
  149         for (i = 0; i < NUMSAVEREGS; i++)
  150                 regs->r_regs[i] = r.r_regs[i];
  151 
  152         return (0);
  153 }
  154 
  155 int
  156 set_fpregs32(struct thread *td, struct fpreg32 *fpregs)
  157 {
  158         struct fpreg fp;
  159         unsigned i;
  160 
  161         for (i = 0; i < NUMFPREGS; i++)
  162                 fp.r_regs[i] = fpregs->r_regs[i];
  163 
  164         return (set_fpregs(td, &fp));
  165 }
  166 
  167 int
  168 fill_fpregs32(struct thread *td, struct fpreg32 *fpregs)
  169 {
  170         struct fpreg fp;
  171         unsigned i;
  172         int error;
  173 
  174         error = fill_fpregs(td, &fp);
  175         if (error != 0)
  176                 return (error);
  177 
  178         for (i = 0; i < NUMFPREGS; i++)
  179                 fpregs->r_regs[i] = fp.r_regs[i];
  180 
  181         return (0);
  182 }
  183 
  184 static int
  185 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
  186 {
  187         mcontext_t mcp64;
  188         unsigned i;
  189         int error;
  190 
  191         error = get_mcontext(td, &mcp64, flags);
  192         if (error != 0)
  193                 return (error);
  194 
  195         mcp->mc_onstack = mcp64.mc_onstack;
  196         mcp->mc_pc = mcp64.mc_pc;
  197         for (i = 0; i < 32; i++)
  198                 mcp->mc_regs[i] = mcp64.mc_regs[i];
  199         mcp->sr = mcp64.sr;
  200         mcp->mullo = mcp64.mullo;
  201         mcp->mulhi = mcp64.mulhi;
  202         mcp->mc_fpused = mcp64.mc_fpused;
  203         for (i = 0; i < 33; i++)
  204                 mcp->mc_fpregs[i] = mcp64.mc_fpregs[i];
  205         mcp->mc_fpc_eir = mcp64.mc_fpc_eir;
  206         mcp->mc_tls = (int32_t)(intptr_t)mcp64.mc_tls;
  207 
  208         return (0);
  209 }
  210 
  211 static int
  212 set_mcontext32(struct thread *td, mcontext32_t *mcp)
  213 {
  214         mcontext_t mcp64;
  215         unsigned i;
  216 
  217         mcp64.mc_onstack = mcp->mc_onstack;
  218         mcp64.mc_pc = mcp->mc_pc;
  219         for (i = 0; i < 32; i++)
  220                 mcp64.mc_regs[i] = mcp->mc_regs[i];
  221         mcp64.sr = mcp->sr;
  222         mcp64.mullo = mcp->mullo;
  223         mcp64.mulhi = mcp->mulhi;
  224         mcp64.mc_fpused = mcp->mc_fpused;
  225         for (i = 0; i < 33; i++)
  226                 mcp64.mc_fpregs[i] = mcp->mc_fpregs[i];
  227         mcp64.mc_fpc_eir = mcp->mc_fpc_eir;
  228         mcp64.mc_tls = (void *)(intptr_t)mcp->mc_tls;
  229 
  230         return (set_mcontext(td, &mcp64));
  231 }
  232 
  233 int
  234 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
  235 {
  236         ucontext32_t uc;
  237         int error;
  238 
  239         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
  240 
  241         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
  242                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
  243                 return (EFAULT);
  244         }
  245 
  246         error = set_mcontext32(td, &uc.uc_mcontext);
  247         if (error != 0)
  248                 return (error);
  249 
  250         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
  251 
  252 #if 0
  253         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
  254              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
  255 #endif
  256 
  257         return (EJUSTRETURN);
  258 }
  259 
  260 /*
  261  * The first two fields of a ucontext_t are the signal mask and the machine
  262  * context.  The next field is uc_link; we want to avoid destroying the link
  263  * when copying out contexts.
  264  */
  265 #define UC32_COPY_SIZE  offsetof(ucontext32_t, uc_link)
  266 
  267 int
  268 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
  269 {
  270         ucontext32_t uc;
  271         int ret;
  272 
  273         if (uap->ucp == NULL)
  274                 ret = EINVAL;
  275         else {
  276                 bzero(&uc, sizeof(uc));
  277                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
  278                 PROC_LOCK(td->td_proc);
  279                 uc.uc_sigmask = td->td_sigmask;
  280                 PROC_UNLOCK(td->td_proc);
  281                 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
  282         }
  283         return (ret);
  284 }
  285 
  286 int
  287 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
  288 {
  289         ucontext32_t uc;
  290         int ret;        
  291 
  292         if (uap->ucp == NULL)
  293                 ret = EINVAL;
  294         else {
  295                 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
  296                 if (ret == 0) {
  297                         ret = set_mcontext32(td, &uc.uc_mcontext);
  298                         if (ret == 0) {
  299                                 kern_sigprocmask(td, SIG_SETMASK,
  300                                     &uc.uc_sigmask, NULL, 0);
  301                         }
  302                 }
  303         }
  304         return (ret == 0 ? EJUSTRETURN : ret);
  305 }
  306 
  307 int
  308 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
  309 {
  310         ucontext32_t uc;
  311         int ret;
  312 
  313         if (uap->oucp == NULL || uap->ucp == NULL)
  314                 ret = EINVAL;
  315         else {
  316                 bzero(&uc, sizeof(uc));
  317                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
  318                 PROC_LOCK(td->td_proc);
  319                 uc.uc_sigmask = td->td_sigmask;
  320                 PROC_UNLOCK(td->td_proc);
  321                 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
  322                 if (ret == 0) {
  323                         ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
  324                         if (ret == 0) {
  325                                 ret = set_mcontext32(td, &uc.uc_mcontext);
  326                                 if (ret == 0) {
  327                                         kern_sigprocmask(td, SIG_SETMASK,
  328                                             &uc.uc_sigmask, NULL, 0);
  329                                 }
  330                         }
  331                 }
  332         }
  333         return (ret == 0 ? EJUSTRETURN : ret);
  334 }
  335 
  336 #define UCONTEXT_MAGIC  0xACEDBADE
  337 
  338 /*
  339  * Send an interrupt to process.
  340  *
  341  * Stack is set up to allow sigcode stored
  342  * at top to call routine, followed by kcall
  343  * to sigreturn routine below.  After sigreturn
  344  * resets the signal mask, the stack, and the
  345  * frame pointer, it returns to the user
  346  * specified pc, psl.
  347  */
  348 static void
  349 freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
  350 {
  351         struct proc *p;
  352         struct thread *td;
  353         struct fpreg32 fpregs;
  354         struct reg32 regs;
  355         struct sigacts *psp;
  356         struct sigframe32 sf, *sfp;
  357         int sig;
  358         int oonstack;
  359         unsigned i;
  360 
  361         td = curthread;
  362         p = td->td_proc;
  363         PROC_LOCK_ASSERT(p, MA_OWNED);
  364         sig = ksi->ksi_signo;
  365         psp = p->p_sigacts;
  366         mtx_assert(&psp->ps_mtx, MA_OWNED);
  367 
  368         fill_regs32(td, &regs);
  369         oonstack = sigonstack(td->td_frame->sp);
  370 
  371         /* save user context */
  372         bzero(&sf, sizeof sf);
  373         sf.sf_uc.uc_sigmask = *mask;
  374         sf.sf_uc.uc_stack.ss_sp = (int32_t)(intptr_t)td->td_sigstk.ss_sp;
  375         sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
  376         sf.sf_uc.uc_stack.ss_flags = td->td_sigstk.ss_flags;
  377         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
  378         sf.sf_uc.uc_mcontext.mc_pc = regs.r_regs[PC];
  379         sf.sf_uc.uc_mcontext.mullo = regs.r_regs[MULLO];
  380         sf.sf_uc.uc_mcontext.mulhi = regs.r_regs[MULHI];
  381         sf.sf_uc.uc_mcontext.mc_tls = (int32_t)(intptr_t)td->td_md.md_tls;
  382         sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
  383         for (i = 1; i < 32; i++)
  384                 sf.sf_uc.uc_mcontext.mc_regs[i] = regs.r_regs[i];
  385         sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
  386         if (sf.sf_uc.uc_mcontext.mc_fpused) {
  387                 /* if FPU has current state, save it first */
  388                 if (td == PCPU_GET(fpcurthread))
  389                         MipsSaveCurFPState(td);
  390                 fill_fpregs32(td, &fpregs);
  391                 for (i = 0; i < 33; i++)
  392                         sf.sf_uc.uc_mcontext.mc_fpregs[i] = fpregs.r_regs[i];
  393         }
  394 
  395         /* Allocate and validate space for the signal handler context. */
  396         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
  397             SIGISMEMBER(psp->ps_sigonstack, sig)) {
  398                 sfp = (struct sigframe32 *)(((uintptr_t)td->td_sigstk.ss_sp +
  399                     td->td_sigstk.ss_size - sizeof(struct sigframe32))
  400                     & ~(sizeof(__int64_t) - 1));
  401         } else
  402                 sfp = (struct sigframe32 *)((vm_offset_t)(td->td_frame->sp - 
  403                     sizeof(struct sigframe32)) & ~(sizeof(__int64_t) - 1));
  404 
  405         /* Build the argument list for the signal handler. */
  406         td->td_frame->a0 = sig;
  407         td->td_frame->a2 = (register_t)(intptr_t)&sfp->sf_uc;
  408         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
  409                 /* Signal handler installed with SA_SIGINFO. */
  410                 td->td_frame->a1 = (register_t)(intptr_t)&sfp->sf_si;
  411                 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
  412 
  413                 /* fill siginfo structure */
  414                 sf.sf_si.si_signo = sig;
  415                 sf.sf_si.si_code = ksi->ksi_code;
  416                 sf.sf_si.si_addr = td->td_frame->badvaddr;
  417         } else {
  418                 /* Old FreeBSD-style arguments. */
  419                 td->td_frame->a1 = ksi->ksi_code;
  420                 td->td_frame->a3 = td->td_frame->badvaddr;
  421                 /* sf.sf_ahu.sf_handler = catcher; */
  422         }
  423 
  424         mtx_unlock(&psp->ps_mtx);
  425         PROC_UNLOCK(p);
  426 
  427         /*
  428          * Copy the sigframe out to the user's stack.
  429          */
  430         if (copyout(&sf, sfp, sizeof(struct sigframe32)) != 0) {
  431                 /*
  432                  * Something is wrong with the stack pointer.
  433                  * ...Kill the process.
  434                  */
  435                 PROC_LOCK(p);
  436                 sigexit(td, SIGILL);
  437         }
  438 
  439         td->td_frame->pc = (register_t)(intptr_t)catcher;
  440         td->td_frame->t9 = (register_t)(intptr_t)catcher;
  441         td->td_frame->sp = (register_t)(intptr_t)sfp;
  442         /*
  443          * Signal trampoline code is at base of user stack.
  444          */
  445         td->td_frame->ra = (register_t)(intptr_t)FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
  446         PROC_LOCK(p);
  447         mtx_lock(&psp->ps_mtx);
  448 }
  449 
  450 int
  451 freebsd32_sysarch(struct thread *td, struct freebsd32_sysarch_args *uap)
  452 {
  453         int error;
  454         int32_t tlsbase;
  455 
  456         switch (uap->op) {
  457         case MIPS_SET_TLS:
  458                 td->td_md.md_tls = (void *)(intptr_t)uap->parms;
  459 
  460                 /*
  461                  * If there is an user local register implementation (ULRI)
  462                  * update it as well.  Add the TLS and TCB offsets so the
  463                  * value in this register is adjusted like in the case of the
  464                  * rdhwr trap() instruction handler.
  465                  */
  466                 if (cpuinfo.userlocal_reg == true) {
  467                         mips_wr_userlocal((unsigned long)(uap->parms +
  468                             td->td_proc->p_md.md_tls_tcb_offset));
  469                 }
  470                 return (0);
  471         case MIPS_GET_TLS: 
  472                 tlsbase = (int32_t)(intptr_t)td->td_md.md_tls;
  473                 error = copyout(&tlsbase, uap->parms, sizeof(tlsbase));
  474                 return (error);
  475         default:
  476                 break;
  477         }
  478         return (EINVAL);
  479 }
  480 
  481 void
  482 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
  483     size_t *off __unused)
  484 {
  485 }

Cache object: 16726284f0f6060553f403ca68be2f5e


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