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/amd64/linux32/linux32_sysvec.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-3-Clause
    3  *
    4  * Copyright (c) 2004 Tim J. Robbins
    5  * Copyright (c) 2003 Peter Wemm
    6  * Copyright (c) 2002 Doug Rabson
    7  * Copyright (c) 1998-1999 Andrew Gallatin
    8  * Copyright (c) 1994-1996 Søren Schmidt
    9  * All rights reserved.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer
   16  *    in this position and unchanged.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. The name of the author may not be used to endorse or promote products
   21  *    derived from this software without specific prior written permission
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 
   35 #include "opt_compat.h"
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD$");
   39 
   40 #ifndef COMPAT_FREEBSD32
   41 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
   42 #endif
   43 
   44 #define __ELF_WORD_SIZE 32
   45 
   46 #include <sys/param.h>
   47 #include <sys/systm.h>
   48 #include <sys/exec.h>
   49 #include <sys/fcntl.h>
   50 #include <sys/imgact.h>
   51 #include <sys/imgact_elf.h>
   52 #include <sys/kernel.h>
   53 #include <sys/lock.h>
   54 #include <sys/malloc.h>
   55 #include <sys/module.h>
   56 #include <sys/mutex.h>
   57 #include <sys/proc.h>
   58 #include <sys/resourcevar.h>
   59 #include <sys/stddef.h>
   60 #include <sys/signalvar.h>
   61 #include <sys/syscallsubr.h>
   62 #include <sys/sysctl.h>
   63 #include <sys/sysent.h>
   64 #include <sys/sysproto.h>
   65 #include <sys/vnode.h>
   66 #include <sys/eventhandler.h>
   67 
   68 #include <vm/vm.h>
   69 #include <vm/pmap.h>
   70 #include <vm/vm_extern.h>
   71 #include <vm/vm_map.h>
   72 #include <vm/vm_object.h>
   73 #include <vm/vm_page.h>
   74 #include <vm/vm_param.h>
   75 
   76 #include <machine/cpu.h>
   77 #include <machine/md_var.h>
   78 #include <machine/pcb.h>
   79 #include <machine/specialreg.h>
   80 #include <machine/trap.h>
   81 
   82 #include <x86/linux/linux_x86.h>
   83 #include <amd64/linux32/linux.h>
   84 #include <amd64/linux32/linux32_proto.h>
   85 #include <compat/linux/linux_emul.h>
   86 #include <compat/linux/linux_fork.h>
   87 #include <compat/linux/linux_ioctl.h>
   88 #include <compat/linux/linux_mib.h>
   89 #include <compat/linux/linux_misc.h>
   90 #include <compat/linux/linux_signal.h>
   91 #include <compat/linux/linux_util.h>
   92 #include <compat/linux/linux_vdso.h>
   93 
   94 #include <x86/linux/linux_x86_sigframe.h>
   95 
   96 MODULE_VERSION(linux, 1);
   97 
   98 #define LINUX32_MAXUSER         ((1ul << 32) - PAGE_SIZE)
   99 #define LINUX32_VDSOPAGE_SIZE   PAGE_SIZE * 2
  100 #define LINUX32_VDSOPAGE        (LINUX32_MAXUSER - LINUX32_VDSOPAGE_SIZE)
  101 #define LINUX32_SHAREDPAGE      (LINUX32_VDSOPAGE - PAGE_SIZE)
  102                                 /*
  103                                  * PAGE_SIZE - the size
  104                                  * of the native SHAREDPAGE
  105                                  */
  106 #define LINUX32_USRSTACK        LINUX32_SHAREDPAGE
  107 
  108 static int linux_szsigcode;
  109 static vm_object_t linux_vdso_obj;
  110 static char *linux_vdso_mapping;
  111 extern char _binary_linux32_vdso_so_o_start;
  112 extern char _binary_linux32_vdso_so_o_end;
  113 static vm_offset_t linux_vdso_base;
  114 
  115 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
  116 extern const char *linux32_syscallnames[];
  117 
  118 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
  119 
  120 static int      linux_fixup_elf(uintptr_t *stack_base,
  121                     struct image_params *iparams);
  122 static int      linux_copyout_strings(struct image_params *imgp,
  123                     uintptr_t *stack_base);
  124 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
  125 static void     linux_exec_setregs(struct thread *td,
  126                                    struct image_params *imgp, uintptr_t stack);
  127 static void     linux_exec_sysvec_init(void *param);
  128 static int      linux_on_exec_vmspace(struct proc *p,
  129                     struct image_params *imgp);
  130 static void     linux32_fixlimit(struct rlimit *rl, int which);
  131 static bool     linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
  132 static void     linux_vdso_install(const void *param);
  133 static void     linux_vdso_deinstall(const void *param);
  134 static void     linux_vdso_reloc(char *mapping, Elf_Addr offset);
  135 static void     linux32_set_fork_retval(struct thread *td);
  136 static void     linux32_set_syscall_retval(struct thread *td, int error);
  137 
  138 struct linux32_ps_strings {
  139         u_int32_t ps_argvstr;   /* first of 0 or more argument strings */
  140         u_int ps_nargvstr;      /* the number of argument strings */
  141         u_int32_t ps_envstr;    /* first of 0 or more environment strings */
  142         u_int ps_nenvstr;       /* the number of environment strings */
  143 };
  144 #define LINUX32_PS_STRINGS      (LINUX32_USRSTACK - \
  145                                     sizeof(struct linux32_ps_strings))
  146 
  147 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
  148 LINUX_VDSO_SYM_INTPTR(linux32_vdso_sigcode);
  149 LINUX_VDSO_SYM_INTPTR(linux32_vdso_rt_sigcode);
  150 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
  151 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
  152 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
  153 LINUX_VDSO_SYM_CHAR(linux_platform);
  154 
  155 static int
  156 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
  157 {
  158         Elf32_Auxargs *args;
  159         Elf32_Auxinfo *argarray, *pos;
  160         int error, issetugid;
  161 
  162         args = (Elf32_Auxargs *)imgp->auxargs;
  163         argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
  164             M_WAITOK | M_ZERO);
  165 
  166         issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
  167         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
  168         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
  169         AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
  170         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
  171 
  172         /*
  173          * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
  174          * as it has appeared in the 2.4.0-rc7 first time.
  175          * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
  176          * glibc falls back to the hard-coded CLK_TCK value when aux entry
  177          * is not present.
  178          * Also see linux_times() implementation.
  179          */
  180         if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
  181                 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
  182         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
  183         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
  184         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
  185         AUXARGS_ENTRY(pos, AT_BASE, args->base);
  186         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
  187         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
  188         AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
  189         AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
  190         AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
  191         AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
  192         AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
  193         AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
  194         AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
  195         if (imgp->execpathp != 0)
  196                 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
  197         if (args->execfd != -1)
  198                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
  199         AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
  200         AUXARGS_ENTRY(pos, AT_NULL, 0);
  201 
  202         free(imgp->auxargs, M_TEMP);
  203         imgp->auxargs = NULL;
  204         KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
  205 
  206         error = copyout(argarray, (void *)base,
  207             sizeof(*argarray) * LINUX_AT_COUNT);
  208         free(argarray, M_TEMP);
  209         return (error);
  210 }
  211 
  212 static int
  213 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
  214 {
  215         Elf32_Addr *base;
  216 
  217         base = (Elf32_Addr *)*stack_base;
  218         base--;
  219         if (suword32(base, (uint32_t)imgp->args->argc) == -1)
  220                 return (EFAULT);
  221         *stack_base = (uintptr_t)base;
  222         return (0);
  223 }
  224 
  225 static void
  226 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
  227 {
  228         struct thread *td = curthread;
  229         struct proc *p = td->td_proc;
  230         struct sigacts *psp;
  231         struct trapframe *regs;
  232         struct l_rt_sigframe *fp, frame;
  233         int oonstack;
  234         int sig;
  235         int code;
  236 
  237         sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
  238         code = ksi->ksi_code;
  239         PROC_LOCK_ASSERT(p, MA_OWNED);
  240         psp = p->p_sigacts;
  241         mtx_assert(&psp->ps_mtx, MA_OWNED);
  242         regs = td->td_frame;
  243         oonstack = sigonstack(regs->tf_rsp);
  244 
  245         /* Allocate space for the signal handler context. */
  246         if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
  247             SIGISMEMBER(psp->ps_sigonstack, sig)) {
  248                 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
  249                     td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
  250         } else
  251                 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
  252         mtx_unlock(&psp->ps_mtx);
  253 
  254         /* Build the argument list for the signal handler. */
  255         sig = bsd_to_linux_signal(sig);
  256 
  257         bzero(&frame, sizeof(frame));
  258 
  259         frame.sf_sig = sig;
  260         frame.sf_siginfo = PTROUT(&fp->sf_si);
  261         frame.sf_ucontext = PTROUT(&fp->sf_uc);
  262 
  263         /* Fill in POSIX parts. */
  264         siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
  265 
  266         /*
  267          * Build the signal context to be used by sigreturn and libgcc unwind.
  268          */
  269         frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
  270         frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
  271         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
  272             ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
  273         PROC_UNLOCK(p);
  274 
  275         bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
  276 
  277         frame.sf_uc.uc_mcontext.sc_mask   = frame.sf_uc.uc_sigmask.__mask;
  278         frame.sf_uc.uc_mcontext.sc_edi    = regs->tf_rdi;
  279         frame.sf_uc.uc_mcontext.sc_esi    = regs->tf_rsi;
  280         frame.sf_uc.uc_mcontext.sc_ebp    = regs->tf_rbp;
  281         frame.sf_uc.uc_mcontext.sc_ebx    = regs->tf_rbx;
  282         frame.sf_uc.uc_mcontext.sc_esp    = regs->tf_rsp;
  283         frame.sf_uc.uc_mcontext.sc_edx    = regs->tf_rdx;
  284         frame.sf_uc.uc_mcontext.sc_ecx    = regs->tf_rcx;
  285         frame.sf_uc.uc_mcontext.sc_eax    = regs->tf_rax;
  286         frame.sf_uc.uc_mcontext.sc_eip    = regs->tf_rip;
  287         frame.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
  288         frame.sf_uc.uc_mcontext.sc_gs     = regs->tf_gs;
  289         frame.sf_uc.uc_mcontext.sc_fs     = regs->tf_fs;
  290         frame.sf_uc.uc_mcontext.sc_es     = regs->tf_es;
  291         frame.sf_uc.uc_mcontext.sc_ds     = regs->tf_ds;
  292         frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_rflags;
  293         frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
  294         frame.sf_uc.uc_mcontext.sc_ss     = regs->tf_ss;
  295         frame.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
  296         frame.sf_uc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
  297         frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
  298 
  299         if (copyout(&frame, fp, sizeof(frame)) != 0) {
  300                 /*
  301                  * Process has trashed its stack; give it an illegal
  302                  * instruction to halt it in its tracks.
  303                  */
  304                 PROC_LOCK(p);
  305                 sigexit(td, SIGILL);
  306         }
  307 
  308         /* Build context to run handler in. */
  309         regs->tf_rsp = PTROUT(fp);
  310         regs->tf_rip = linux32_vdso_rt_sigcode;
  311         regs->tf_rdi = PTROUT(catcher);
  312         regs->tf_rflags &= ~(PSL_T | PSL_D);
  313         regs->tf_cs = _ucode32sel;
  314         regs->tf_ss = _udatasel;
  315         regs->tf_ds = _udatasel;
  316         regs->tf_es = _udatasel;
  317         regs->tf_fs = _ufssel;
  318         regs->tf_gs = _ugssel;
  319         regs->tf_flags = TF_HASSEGS;
  320         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  321         PROC_LOCK(p);
  322         mtx_lock(&psp->ps_mtx);
  323 }
  324 
  325 /*
  326  * Send an interrupt to process.
  327  *
  328  * Stack is set up to allow sigcode stored
  329  * in u. to call routine, followed by kcall
  330  * to sigreturn routine below.  After sigreturn
  331  * resets the signal mask, the stack, and the
  332  * frame pointer, it returns to the user
  333  * specified pc, psl.
  334  */
  335 static void
  336 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
  337 {
  338         struct thread *td = curthread;
  339         struct proc *p = td->td_proc;
  340         struct sigacts *psp;
  341         struct trapframe *regs;
  342         struct l_sigframe *fp, frame;
  343         l_sigset_t lmask;
  344         int oonstack;
  345         int sig, code;
  346 
  347         sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
  348         code = ksi->ksi_code;
  349         PROC_LOCK_ASSERT(p, MA_OWNED);
  350         psp = p->p_sigacts;
  351         mtx_assert(&psp->ps_mtx, MA_OWNED);
  352         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
  353                 /* Signal handler installed with SA_SIGINFO. */
  354                 linux_rt_sendsig(catcher, ksi, mask);
  355                 return;
  356         }
  357 
  358         regs = td->td_frame;
  359         oonstack = sigonstack(regs->tf_rsp);
  360 
  361         /* Allocate space for the signal handler context. */
  362         if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
  363             SIGISMEMBER(psp->ps_sigonstack, sig)) {
  364                 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
  365                     td->td_sigstk.ss_size - sizeof(struct l_sigframe));
  366         } else
  367                 fp = (struct l_sigframe *)regs->tf_rsp - 1;
  368         mtx_unlock(&psp->ps_mtx);
  369         PROC_UNLOCK(p);
  370 
  371         /* Build the argument list for the signal handler. */
  372         sig = bsd_to_linux_signal(sig);
  373 
  374         bzero(&frame, sizeof(frame));
  375 
  376         frame.sf_sig = sig;
  377         frame.sf_sigmask = *mask;
  378         bsd_to_linux_sigset(mask, &lmask);
  379 
  380         /* Build the signal context to be used by sigreturn. */
  381         frame.sf_sc.sc_mask   = lmask.__mask;
  382         frame.sf_sc.sc_gs     = regs->tf_gs;
  383         frame.sf_sc.sc_fs     = regs->tf_fs;
  384         frame.sf_sc.sc_es     = regs->tf_es;
  385         frame.sf_sc.sc_ds     = regs->tf_ds;
  386         frame.sf_sc.sc_edi    = regs->tf_rdi;
  387         frame.sf_sc.sc_esi    = regs->tf_rsi;
  388         frame.sf_sc.sc_ebp    = regs->tf_rbp;
  389         frame.sf_sc.sc_ebx    = regs->tf_rbx;
  390         frame.sf_sc.sc_esp    = regs->tf_rsp;
  391         frame.sf_sc.sc_edx    = regs->tf_rdx;
  392         frame.sf_sc.sc_ecx    = regs->tf_rcx;
  393         frame.sf_sc.sc_eax    = regs->tf_rax;
  394         frame.sf_sc.sc_eip    = regs->tf_rip;
  395         frame.sf_sc.sc_cs     = regs->tf_cs;
  396         frame.sf_sc.sc_eflags = regs->tf_rflags;
  397         frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
  398         frame.sf_sc.sc_ss     = regs->tf_ss;
  399         frame.sf_sc.sc_err    = regs->tf_err;
  400         frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
  401         frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
  402 
  403         if (copyout(&frame, fp, sizeof(frame)) != 0) {
  404                 /*
  405                  * Process has trashed its stack; give it an illegal
  406                  * instruction to halt it in its tracks.
  407                  */
  408                 PROC_LOCK(p);
  409                 sigexit(td, SIGILL);
  410         }
  411 
  412         /* Build context to run handler in. */
  413         regs->tf_rsp = PTROUT(fp);
  414         regs->tf_rip = linux32_vdso_sigcode;
  415         regs->tf_rdi = PTROUT(catcher);
  416         regs->tf_rflags &= ~(PSL_T | PSL_D);
  417         regs->tf_cs = _ucode32sel;
  418         regs->tf_ss = _udatasel;
  419         regs->tf_ds = _udatasel;
  420         regs->tf_es = _udatasel;
  421         regs->tf_fs = _ufssel;
  422         regs->tf_gs = _ugssel;
  423         regs->tf_flags = TF_HASSEGS;
  424         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  425         PROC_LOCK(p);
  426         mtx_lock(&psp->ps_mtx);
  427 }
  428 
  429 /*
  430  * System call to cleanup state after a signal
  431  * has been taken.  Reset signal mask and
  432  * stack state from context left by sendsig (above).
  433  * Return to previous pc and psl as specified by
  434  * context left by sendsig. Check carefully to
  435  * make sure that the user has not modified the
  436  * psl to gain improper privileges or to cause
  437  * a machine fault.
  438  */
  439 int
  440 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
  441 {
  442         struct l_sigframe frame;
  443         struct trapframe *regs;
  444         int eflags;
  445         ksiginfo_t ksi;
  446 
  447         regs = td->td_frame;
  448 
  449         /*
  450          * The trampoline code hands us the sigframe.
  451          * It is unsafe to keep track of it ourselves, in the event that a
  452          * program jumps out of a signal handler.
  453          */
  454         if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
  455                 return (EFAULT);
  456 
  457         /* Check for security violations. */
  458         eflags = frame.sf_sc.sc_eflags;
  459         if (!EFL_SECURE(eflags, regs->tf_rflags))
  460                 return(EINVAL);
  461 
  462         /*
  463          * Don't allow users to load a valid privileged %cs.  Let the
  464          * hardware check for invalid selectors, excess privilege in
  465          * other selectors, invalid %eip's and invalid %esp's.
  466          */
  467         if (!CS_SECURE(frame.sf_sc.sc_cs)) {
  468                 ksiginfo_init_trap(&ksi);
  469                 ksi.ksi_signo = SIGBUS;
  470                 ksi.ksi_code = BUS_OBJERR;
  471                 ksi.ksi_trapno = T_PROTFLT;
  472                 ksi.ksi_addr = (void *)regs->tf_rip;
  473                 trapsignal(td, &ksi);
  474                 return(EINVAL);
  475         }
  476 
  477         kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
  478 
  479         /* Restore signal context. */
  480         regs->tf_rdi    = frame.sf_sc.sc_edi;
  481         regs->tf_rsi    = frame.sf_sc.sc_esi;
  482         regs->tf_rbp    = frame.sf_sc.sc_ebp;
  483         regs->tf_rbx    = frame.sf_sc.sc_ebx;
  484         regs->tf_rdx    = frame.sf_sc.sc_edx;
  485         regs->tf_rcx    = frame.sf_sc.sc_ecx;
  486         regs->tf_rax    = frame.sf_sc.sc_eax;
  487         regs->tf_rip    = frame.sf_sc.sc_eip;
  488         regs->tf_cs     = frame.sf_sc.sc_cs;
  489         regs->tf_ds     = frame.sf_sc.sc_ds;
  490         regs->tf_es     = frame.sf_sc.sc_es;
  491         regs->tf_fs     = frame.sf_sc.sc_fs;
  492         regs->tf_gs     = frame.sf_sc.sc_gs;
  493         regs->tf_rflags = eflags;
  494         regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
  495         regs->tf_ss     = frame.sf_sc.sc_ss;
  496         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  497 
  498         return (EJUSTRETURN);
  499 }
  500 
  501 /*
  502  * System call to cleanup state after a signal
  503  * has been taken.  Reset signal mask and
  504  * stack state from context left by rt_sendsig (above).
  505  * Return to previous pc and psl as specified by
  506  * context left by sendsig. Check carefully to
  507  * make sure that the user has not modified the
  508  * psl to gain improper privileges or to cause
  509  * a machine fault.
  510  */
  511 int
  512 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
  513 {
  514         struct l_ucontext uc;
  515         struct l_sigcontext *context;
  516         sigset_t bmask;
  517         l_stack_t *lss;
  518         stack_t ss;
  519         struct trapframe *regs;
  520         int eflags;
  521         ksiginfo_t ksi;
  522 
  523         regs = td->td_frame;
  524 
  525         /*
  526          * The trampoline code hands us the ucontext.
  527          * It is unsafe to keep track of it ourselves, in the event that a
  528          * program jumps out of a signal handler.
  529          */
  530         if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
  531                 return (EFAULT);
  532 
  533         context = &uc.uc_mcontext;
  534 
  535         /* Check for security violations. */
  536         eflags = context->sc_eflags;
  537         if (!EFL_SECURE(eflags, regs->tf_rflags))
  538                 return(EINVAL);
  539 
  540         /*
  541          * Don't allow users to load a valid privileged %cs.  Let the
  542          * hardware check for invalid selectors, excess privilege in
  543          * other selectors, invalid %eip's and invalid %esp's.
  544          */
  545         if (!CS_SECURE(context->sc_cs)) {
  546                 ksiginfo_init_trap(&ksi);
  547                 ksi.ksi_signo = SIGBUS;
  548                 ksi.ksi_code = BUS_OBJERR;
  549                 ksi.ksi_trapno = T_PROTFLT;
  550                 ksi.ksi_addr = (void *)regs->tf_rip;
  551                 trapsignal(td, &ksi);
  552                 return(EINVAL);
  553         }
  554 
  555         linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
  556         kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
  557 
  558         /*
  559          * Restore signal context
  560          */
  561         regs->tf_gs     = context->sc_gs;
  562         regs->tf_fs     = context->sc_fs;
  563         regs->tf_es     = context->sc_es;
  564         regs->tf_ds     = context->sc_ds;
  565         regs->tf_rdi    = context->sc_edi;
  566         regs->tf_rsi    = context->sc_esi;
  567         regs->tf_rbp    = context->sc_ebp;
  568         regs->tf_rbx    = context->sc_ebx;
  569         regs->tf_rdx    = context->sc_edx;
  570         regs->tf_rcx    = context->sc_ecx;
  571         regs->tf_rax    = context->sc_eax;
  572         regs->tf_rip    = context->sc_eip;
  573         regs->tf_cs     = context->sc_cs;
  574         regs->tf_rflags = eflags;
  575         regs->tf_rsp    = context->sc_esp_at_signal;
  576         regs->tf_ss     = context->sc_ss;
  577         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  578 
  579         /*
  580          * call sigaltstack & ignore results..
  581          */
  582         lss = &uc.uc_stack;
  583         ss.ss_sp = PTRIN(lss->ss_sp);
  584         ss.ss_size = lss->ss_size;
  585         ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
  586 
  587         (void)kern_sigaltstack(td, &ss, NULL);
  588 
  589         return (EJUSTRETURN);
  590 }
  591 
  592 static int
  593 linux32_fetch_syscall_args(struct thread *td)
  594 {
  595         struct proc *p;
  596         struct trapframe *frame;
  597         struct syscall_args *sa;
  598 
  599         p = td->td_proc;
  600         frame = td->td_frame;
  601         sa = &td->td_sa;
  602 
  603         sa->args[0] = frame->tf_rbx;
  604         sa->args[1] = frame->tf_rcx;
  605         sa->args[2] = frame->tf_rdx;
  606         sa->args[3] = frame->tf_rsi;
  607         sa->args[4] = frame->tf_rdi;
  608         sa->args[5] = frame->tf_rbp;    /* Unconfirmed */
  609         sa->code = frame->tf_rax;
  610         sa->original_code = sa->code;
  611 
  612         if (sa->code >= p->p_sysent->sv_size)
  613                 /* nosys */
  614                 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
  615         else
  616                 sa->callp = &p->p_sysent->sv_table[sa->code];
  617 
  618         td->td_retval[0] = 0;
  619         td->td_retval[1] = frame->tf_rdx;
  620 
  621         return (0);
  622 }
  623 
  624 static void
  625 linux32_set_syscall_retval(struct thread *td, int error)
  626 {
  627         struct trapframe *frame = td->td_frame;
  628 
  629         cpu_set_syscall_retval(td, error);
  630 
  631         if (__predict_false(error != 0)) {
  632                 if (error != ERESTART && error != EJUSTRETURN)
  633                         frame->tf_rax = bsd_to_linux_errno(error);
  634         }
  635 }
  636 
  637 static void
  638 linux32_set_fork_retval(struct thread *td)
  639 {
  640         struct trapframe *frame = td->td_frame;
  641 
  642         frame->tf_rax = 0;
  643 }
  644 
  645 /*
  646  * Clear registers on exec
  647  * XXX copied from ia32_signal.c.
  648  */
  649 static void
  650 linux_exec_setregs(struct thread *td, struct image_params *imgp,
  651     uintptr_t stack)
  652 {
  653         struct trapframe *regs = td->td_frame;
  654         struct pcb *pcb = td->td_pcb;
  655         register_t saved_rflags;
  656 
  657         regs = td->td_frame;
  658         pcb = td->td_pcb;
  659 
  660         if (td->td_proc->p_md.md_ldt != NULL)
  661                 user_ldt_free(td);
  662 
  663         critical_enter();
  664         wrmsr(MSR_FSBASE, 0);
  665         wrmsr(MSR_KGSBASE, 0);  /* User value while we're in the kernel */
  666         pcb->pcb_fsbase = 0;
  667         pcb->pcb_gsbase = 0;
  668         critical_exit();
  669         pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
  670 
  671         saved_rflags = regs->tf_rflags & PSL_T;
  672         bzero((char *)regs, sizeof(struct trapframe));
  673         regs->tf_rip = imgp->entry_addr;
  674         regs->tf_rsp = stack;
  675         regs->tf_rflags = PSL_USER | saved_rflags;
  676         regs->tf_gs = _ugssel;
  677         regs->tf_fs = _ufssel;
  678         regs->tf_es = _udatasel;
  679         regs->tf_ds = _udatasel;
  680         regs->tf_ss = _udatasel;
  681         regs->tf_flags = TF_HASSEGS;
  682         regs->tf_cs = _ucode32sel;
  683         regs->tf_rbx = (register_t)imgp->ps_strings;
  684 
  685         x86_clear_dbregs(pcb);
  686 
  687         fpstate_drop(td);
  688 
  689         /* Do full restore on return so that we can change to a different %cs */
  690         set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
  691 }
  692 
  693 /*
  694  * XXX copied from ia32_sysvec.c.
  695  */
  696 static int
  697 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
  698 {
  699         int argc, envc, error;
  700         u_int32_t *vectp;
  701         char *stringp;
  702         uintptr_t destp, ustringp;
  703         struct linux32_ps_strings *arginfo;
  704         char canary[LINUX_AT_RANDOM_LEN];
  705         size_t execpath_len;
  706 
  707         arginfo = (struct linux32_ps_strings *)PROC_PS_STRINGS(imgp->proc);
  708         destp = (uintptr_t)arginfo;
  709 
  710         if (imgp->execpath != NULL && imgp->auxargs != NULL) {
  711                 execpath_len = strlen(imgp->execpath) + 1;
  712                 destp -= execpath_len;
  713                 destp = rounddown2(destp, sizeof(uint32_t));
  714                 imgp->execpathp = (void *)destp;
  715                 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
  716                 if (error != 0)
  717                         return (error);
  718         }
  719 
  720         /* Prepare the canary for SSP. */
  721         arc4rand(canary, sizeof(canary), 0);
  722         destp -= roundup(sizeof(canary), sizeof(uint32_t));
  723         imgp->canary = (void *)destp;
  724         error = copyout(canary, imgp->canary, sizeof(canary));
  725         if (error != 0)
  726                 return (error);
  727 
  728         /* Allocate room for the argument and environment strings. */
  729         destp -= ARG_MAX - imgp->args->stringspace;
  730         destp = rounddown2(destp, sizeof(uint32_t));
  731         ustringp = destp;
  732 
  733         if (imgp->auxargs) {
  734                 /*
  735                  * Allocate room on the stack for the ELF auxargs
  736                  * array.  It has LINUX_AT_COUNT entries.
  737                  */
  738                 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
  739                 destp = rounddown2(destp, sizeof(uint32_t));
  740         }
  741 
  742         vectp = (uint32_t *)destp;
  743 
  744         /*
  745          * Allocate room for the argv[] and env vectors including the
  746          * terminating NULL pointers.
  747          */
  748         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
  749 
  750         /* vectp also becomes our initial stack base. */
  751         *stack_base = (uintptr_t)vectp;
  752 
  753         stringp = imgp->args->begin_argv;
  754         argc = imgp->args->argc;
  755         envc = imgp->args->envc;
  756 
  757         /* Copy out strings - arguments and environment. */
  758         error = copyout(stringp, (void *)ustringp,
  759             ARG_MAX - imgp->args->stringspace);
  760         if (error != 0)
  761                 return (error);
  762 
  763         /* Fill in "ps_strings" struct for ps, w, etc. */
  764         if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
  765             suword32(&arginfo->ps_nargvstr, argc) != 0)
  766                 return (EFAULT);
  767 
  768         /* Fill in argument portion of vector table. */
  769         for (; argc > 0; --argc) {
  770                 if (suword32(vectp++, ustringp) != 0)
  771                         return (EFAULT);
  772                 while (*stringp++ != 0)
  773                         ustringp++;
  774                 ustringp++;
  775         }
  776 
  777         /* A null vector table pointer separates the argp's from the envp's. */
  778         if (suword32(vectp++, 0) != 0)
  779                 return (EFAULT);
  780 
  781         if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
  782             suword32(&arginfo->ps_nenvstr, envc) != 0)
  783                 return (EFAULT);
  784 
  785         /* Fill in environment portion of vector table. */
  786         for (; envc > 0; --envc) {
  787                 if (suword32(vectp++, ustringp) != 0)
  788                         return (EFAULT);
  789                 while (*stringp++ != 0)
  790                         ustringp++;
  791                 ustringp++;
  792         }
  793 
  794         /* The end of the vector table is a null pointer. */
  795         if (suword32(vectp, 0) != 0)
  796                 return (EFAULT);
  797 
  798         if (imgp->auxargs) {
  799                 vectp++;
  800                 error = imgp->sysent->sv_copyout_auxargs(imgp,
  801                     (uintptr_t)vectp);
  802                 if (error != 0)
  803                         return (error);
  804         }
  805 
  806         return (0);
  807 }
  808 
  809 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  810     "32-bit Linux emulation");
  811 
  812 static u_long   linux32_maxdsiz = LINUX32_MAXDSIZ;
  813 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
  814     &linux32_maxdsiz, 0, "");
  815 static u_long   linux32_maxssiz = LINUX32_MAXSSIZ;
  816 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
  817     &linux32_maxssiz, 0, "");
  818 static u_long   linux32_maxvmem = LINUX32_MAXVMEM;
  819 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
  820     &linux32_maxvmem, 0, "");
  821 bool linux32_emulate_i386 = false;
  822 SYSCTL_BOOL(_compat_linux32, OID_AUTO, emulate_i386, CTLFLAG_RWTUN,
  823     &linux32_emulate_i386, 0, "Emulate the real i386");
  824 
  825 static void
  826 linux32_fixlimit(struct rlimit *rl, int which)
  827 {
  828 
  829         switch (which) {
  830         case RLIMIT_DATA:
  831                 if (linux32_maxdsiz != 0) {
  832                         if (rl->rlim_cur > linux32_maxdsiz)
  833                                 rl->rlim_cur = linux32_maxdsiz;
  834                         if (rl->rlim_max > linux32_maxdsiz)
  835                                 rl->rlim_max = linux32_maxdsiz;
  836                 }
  837                 break;
  838         case RLIMIT_STACK:
  839                 if (linux32_maxssiz != 0) {
  840                         if (rl->rlim_cur > linux32_maxssiz)
  841                                 rl->rlim_cur = linux32_maxssiz;
  842                         if (rl->rlim_max > linux32_maxssiz)
  843                                 rl->rlim_max = linux32_maxssiz;
  844                 }
  845                 break;
  846         case RLIMIT_VMEM:
  847                 if (linux32_maxvmem != 0) {
  848                         if (rl->rlim_cur > linux32_maxvmem)
  849                                 rl->rlim_cur = linux32_maxvmem;
  850                         if (rl->rlim_max > linux32_maxvmem)
  851                                 rl->rlim_max = linux32_maxvmem;
  852                 }
  853                 break;
  854         }
  855 }
  856 
  857 struct sysentvec elf_linux_sysvec = {
  858         .sv_size        = LINUX32_SYS_MAXSYSCALL,
  859         .sv_table       = linux32_sysent,
  860         .sv_fixup       = linux_fixup_elf,
  861         .sv_sendsig     = linux_sendsig,
  862         .sv_sigcode     = &_binary_linux32_vdso_so_o_start,
  863         .sv_szsigcode   = &linux_szsigcode,
  864         .sv_name        = "Linux ELF32",
  865         .sv_coredump    = elf32_coredump,
  866         .sv_elf_core_osabi = ELFOSABI_NONE,
  867         .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
  868         .sv_elf_core_prepare_notes = linux32_prepare_notes,
  869         .sv_imgact_try  = linux_exec_imgact_try,
  870         .sv_minsigstksz = LINUX_MINSIGSTKSZ,
  871         .sv_minuser     = VM_MIN_ADDRESS,
  872         .sv_maxuser     = LINUX32_MAXUSER,
  873         .sv_usrstack    = LINUX32_USRSTACK,
  874         .sv_psstrings   = LINUX32_PS_STRINGS,
  875         .sv_psstringssz = sizeof(struct linux32_ps_strings),
  876         .sv_stackprot   = VM_PROT_ALL,
  877         .sv_copyout_auxargs = linux_copyout_auxargs,
  878         .sv_copyout_strings = linux_copyout_strings,
  879         .sv_setregs     = linux_exec_setregs,
  880         .sv_fixlimit    = linux32_fixlimit,
  881         .sv_maxssiz     = &linux32_maxssiz,
  882         .sv_flags       = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP |
  883             SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
  884         .sv_set_syscall_retval = linux32_set_syscall_retval,
  885         .sv_fetch_syscall_args = linux32_fetch_syscall_args,
  886         .sv_syscallnames = linux32_syscallnames,
  887         .sv_shared_page_base = LINUX32_SHAREDPAGE,
  888         .sv_shared_page_len = PAGE_SIZE,
  889         .sv_schedtail   = linux_schedtail,
  890         .sv_thread_detach = linux_thread_detach,
  891         .sv_trap        = NULL,
  892         .sv_onexec      = linux_on_exec_vmspace,
  893         .sv_onexit      = linux_on_exit,
  894         .sv_ontdexit    = linux_thread_dtor,
  895         .sv_setid_allowed = &linux_setid_allowed_query,
  896         .sv_set_fork_retval = linux32_set_fork_retval,
  897 };
  898 
  899 static int
  900 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
  901 {
  902         int error;
  903 
  904         error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
  905             LINUX32_VDSOPAGE_SIZE, imgp);
  906         if (error == 0)
  907                 linux_on_exec(p, imgp);
  908         return (error);
  909 }
  910 
  911 /*
  912  * linux_vdso_install() and linux_exec_sysvec_init() must be called
  913  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
  914  */
  915 static void
  916 linux_exec_sysvec_init(void *param)
  917 {
  918         l_uintptr_t *ktimekeep_base, *ktsc_selector;
  919         struct sysentvec *sv;
  920         ptrdiff_t tkoff;
  921 
  922         sv = param;
  923         /* Fill timekeep_base */
  924         exec_sysvec_init(sv);
  925 
  926         tkoff = kern_timekeep_base - linux_vdso_base;
  927         ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
  928         *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
  929 
  930         tkoff = kern_tsc_selector - linux_vdso_base;
  931         ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
  932         *ktsc_selector = linux_vdso_tsc_selector_idx();
  933         if (bootverbose)
  934                 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
  935 
  936         tkoff = kern_cpu_selector - linux_vdso_base;
  937         ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
  938         *ktsc_selector = linux_vdso_cpu_selector_idx();
  939         if (bootverbose)
  940                 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
  941 }
  942 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
  943     linux_exec_sysvec_init, &elf_linux_sysvec);
  944 
  945 static void
  946 linux_vdso_install(const void *param)
  947 {
  948         char *vdso_start = &_binary_linux32_vdso_so_o_start;
  949         char *vdso_end = &_binary_linux32_vdso_so_o_end;
  950 
  951         linux_szsigcode = vdso_end - vdso_start;
  952         MPASS(linux_szsigcode <= LINUX32_VDSOPAGE_SIZE);
  953 
  954         linux_vdso_base = LINUX32_VDSOPAGE;
  955 
  956         __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
  957 
  958         linux_vdso_obj = __elfN(linux_shared_page_init)
  959             (&linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);
  960         bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
  961 
  962         linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
  963 }
  964 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
  965     linux_vdso_install, NULL);
  966 
  967 static void
  968 linux_vdso_deinstall(const void *param)
  969 {
  970 
  971         __elfN(linux_shared_page_fini)(linux_vdso_obj,
  972             linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);
  973 }
  974 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
  975     linux_vdso_deinstall, NULL);
  976 
  977 static void
  978 linux_vdso_reloc(char *mapping, Elf_Addr offset)
  979 {
  980         const Elf_Shdr *shdr;
  981         const Elf_Rel *rel;
  982         const Elf_Ehdr *ehdr;
  983         Elf32_Addr *where;
  984         Elf_Size rtype, symidx;
  985         Elf32_Addr addr, addend;
  986         int i, relcnt;
  987 
  988         MPASS(offset != 0);
  989 
  990         relcnt = 0;
  991         ehdr = (const Elf_Ehdr *)mapping;
  992         shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
  993         for (i = 0; i < ehdr->e_shnum; i++)
  994         {
  995                 switch (shdr[i].sh_type) {
  996                 case SHT_REL:
  997                         rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
  998                         relcnt = shdr[i].sh_size / sizeof(*rel);
  999                         break;
 1000                 case SHT_RELA:
 1001                         printf("Linux i386 vDSO: unexpected Rela section\n");
 1002                         break;
 1003                 }
 1004         }
 1005 
 1006         for (i = 0; i < relcnt; i++, rel++) {
 1007                 where = (Elf32_Addr *)(mapping + rel->r_offset);
 1008                 addend = *where;
 1009                 rtype = ELF_R_TYPE(rel->r_info);
 1010                 symidx = ELF_R_SYM(rel->r_info);
 1011 
 1012                 switch (rtype) {
 1013                 case R_386_NONE:        /* none */
 1014                         break;
 1015 
 1016                 case R_386_RELATIVE:    /* B + A */
 1017                         addr = (Elf32_Addr)PTROUT(offset + addend);
 1018                         if (*where != addr)
 1019                                 *where = addr;
 1020                         break;
 1021 
 1022                 case R_386_IRELATIVE:
 1023                         printf("Linux i386 vDSO: unexpected ifunc relocation, "
 1024                             "symbol index %ld\n", (intmax_t)symidx);
 1025                         break;
 1026                 default:
 1027                         printf("Linux i386 vDSO: unexpected relocation type %ld, "
 1028                             "symbol index %ld\n", (intmax_t)rtype, (intmax_t)symidx);
 1029                 }
 1030         }
 1031 }
 1032 
 1033 static char GNU_ABI_VENDOR[] = "GNU";
 1034 static int GNULINUX_ABI_DESC = 0;
 1035 
 1036 static bool
 1037 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
 1038 {
 1039         const Elf32_Word *desc;
 1040         uintptr_t p;
 1041 
 1042         p = (uintptr_t)(note + 1);
 1043         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
 1044 
 1045         desc = (const Elf32_Word *)p;
 1046         if (desc[0] != GNULINUX_ABI_DESC)
 1047                 return (false);
 1048 
 1049         /*
 1050          * For Linux we encode osrel using the Linux convention of
 1051          *      (version << 16) | (major << 8) | (minor)
 1052          * See macro in linux_mib.h
 1053          */
 1054         *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
 1055 
 1056         return (true);
 1057 }
 1058 
 1059 static Elf_Brandnote linux32_brandnote = {
 1060         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
 1061         .hdr.n_descsz   = 16,   /* XXX at least 16 */
 1062         .hdr.n_type     = 1,
 1063         .vendor         = GNU_ABI_VENDOR,
 1064         .flags          = BN_TRANSLATE_OSREL,
 1065         .trans_osrel    = linux32_trans_osrel
 1066 };
 1067 
 1068 static Elf32_Brandinfo linux_brand = {
 1069         .brand          = ELFOSABI_LINUX,
 1070         .machine        = EM_386,
 1071         .compat_3_brand = "Linux",
 1072         .emul_path      = linux_emul_path,
 1073         .interp_path    = "/lib/ld-linux.so.1",
 1074         .sysvec         = &elf_linux_sysvec,
 1075         .interp_newpath = NULL,
 1076         .brand_note     = &linux32_brandnote,
 1077         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
 1078 };
 1079 
 1080 static Elf32_Brandinfo linux_glibc2brand = {
 1081         .brand          = ELFOSABI_LINUX,
 1082         .machine        = EM_386,
 1083         .compat_3_brand = "Linux",
 1084         .emul_path      = linux_emul_path,
 1085         .interp_path    = "/lib/ld-linux.so.2",
 1086         .sysvec         = &elf_linux_sysvec,
 1087         .interp_newpath = NULL,
 1088         .brand_note     = &linux32_brandnote,
 1089         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
 1090 };
 1091 
 1092 static Elf32_Brandinfo linux_muslbrand = {
 1093         .brand          = ELFOSABI_LINUX,
 1094         .machine        = EM_386,
 1095         .compat_3_brand = "Linux",
 1096         .emul_path      = linux_emul_path,
 1097         .interp_path    = "/lib/ld-musl-i386.so.1",
 1098         .sysvec         = &elf_linux_sysvec,
 1099         .interp_newpath = NULL,
 1100         .brand_note     = &linux32_brandnote,
 1101         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
 1102                             LINUX_BI_FUTEX_REQUEUE
 1103 };
 1104 
 1105 Elf32_Brandinfo *linux_brandlist[] = {
 1106         &linux_brand,
 1107         &linux_glibc2brand,
 1108         &linux_muslbrand,
 1109         NULL
 1110 };
 1111 
 1112 static int
 1113 linux_elf_modevent(module_t mod, int type, void *data)
 1114 {
 1115         Elf32_Brandinfo **brandinfo;
 1116         int error;
 1117         struct linux_ioctl_handler **lihp;
 1118 
 1119         error = 0;
 1120 
 1121         switch(type) {
 1122         case MOD_LOAD:
 1123                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
 1124                      ++brandinfo)
 1125                         if (elf32_insert_brand_entry(*brandinfo) < 0)
 1126                                 error = EINVAL;
 1127                 if (error == 0) {
 1128                         SET_FOREACH(lihp, linux_ioctl_handler_set)
 1129                                 linux32_ioctl_register_handler(*lihp);
 1130                         stclohz = (stathz ? stathz : hz);
 1131                         if (bootverbose)
 1132                                 printf("Linux i386 ELF exec handler installed\n");
 1133                 } else
 1134                         printf("cannot insert Linux i386 ELF brand handler\n");
 1135                 break;
 1136         case MOD_UNLOAD:
 1137                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
 1138                      ++brandinfo)
 1139                         if (elf32_brand_inuse(*brandinfo))
 1140                                 error = EBUSY;
 1141                 if (error == 0) {
 1142                         for (brandinfo = &linux_brandlist[0];
 1143                              *brandinfo != NULL; ++brandinfo)
 1144                                 if (elf32_remove_brand_entry(*brandinfo) < 0)
 1145                                         error = EINVAL;
 1146                 }
 1147                 if (error == 0) {
 1148                         SET_FOREACH(lihp, linux_ioctl_handler_set)
 1149                                 linux32_ioctl_unregister_handler(*lihp);
 1150                         if (bootverbose)
 1151                                 printf("Linux i386 ELF exec handler removed\n");
 1152                 } else
 1153                         printf("Could not deinstall Linux i386 ELF interpreter entry\n");
 1154                 break;
 1155         default:
 1156                 return (EOPNOTSUPP);
 1157         }
 1158         return (error);
 1159 }
 1160 
 1161 static moduledata_t linux_elf_mod = {
 1162         "linuxelf",
 1163         linux_elf_modevent,
 1164         0
 1165 };
 1166 
 1167 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
 1168 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
 1169 FEATURE(linux, "Linux 32bit support");

Cache object: b86cc3e6a6942b1e5e7f272cea45698e


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