1ea0fabbcSTim J. Robbins /*- 2c49761ddSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3c49761ddSPedro F. Giffuni * 4ea0fabbcSTim J. Robbins * Copyright (c) 2004 Tim J. Robbins 5ea0fabbcSTim J. Robbins * Copyright (c) 2003 Peter Wemm 6ea0fabbcSTim J. Robbins * Copyright (c) 2002 Doug Rabson 7ea0fabbcSTim J. Robbins * Copyright (c) 1998-1999 Andrew Gallatin 89a14aa01SUlrich Spörlein * Copyright (c) 1994-1996 Søren Schmidt 9ea0fabbcSTim J. Robbins * All rights reserved. 10ea0fabbcSTim J. Robbins * 11ea0fabbcSTim J. Robbins * Redistribution and use in source and binary forms, with or without 12ea0fabbcSTim J. Robbins * modification, are permitted provided that the following conditions 13ea0fabbcSTim J. Robbins * are met: 14ea0fabbcSTim J. Robbins * 1. Redistributions of source code must retain the above copyright 15ea0fabbcSTim J. Robbins * notice, this list of conditions and the following disclaimer 16ea0fabbcSTim J. Robbins * in this position and unchanged. 17ea0fabbcSTim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright 18ea0fabbcSTim J. Robbins * notice, this list of conditions and the following disclaimer in the 19ea0fabbcSTim J. Robbins * documentation and/or other materials provided with the distribution. 20ea0fabbcSTim J. Robbins * 3. The name of the author may not be used to endorse or promote products 21ea0fabbcSTim J. Robbins * derived from this software without specific prior written permission 22ea0fabbcSTim J. Robbins * 23ea0fabbcSTim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24ea0fabbcSTim J. Robbins * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25ea0fabbcSTim J. Robbins * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26ea0fabbcSTim J. Robbins * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27ea0fabbcSTim J. Robbins * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28ea0fabbcSTim J. Robbins * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29ea0fabbcSTim J. Robbins * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30ea0fabbcSTim J. Robbins * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31ea0fabbcSTim J. Robbins * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32ea0fabbcSTim J. Robbins * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33ea0fabbcSTim J. Robbins */ 34ea0fabbcSTim J. Robbins 3502bf7e5eSTijl Coosemans #include "opt_compat.h" 3602bf7e5eSTijl Coosemans 37ea0fabbcSTim J. Robbins #include <sys/cdefs.h> 38ea0fabbcSTim J. Robbins __FBSDID("$FreeBSD$"); 39ea0fabbcSTim J. Robbins 40841c0c7eSNathan Whitehorn #ifndef COMPAT_FREEBSD32 41841c0c7eSNathan Whitehorn #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!" 42ea0fabbcSTim J. Robbins #endif 43ea0fabbcSTim J. Robbins 44ea0fabbcSTim J. Robbins #define __ELF_WORD_SIZE 32 45ea0fabbcSTim J. Robbins 46ea0fabbcSTim J. Robbins #include <sys/param.h> 47ea0fabbcSTim J. Robbins #include <sys/systm.h> 48ea0fabbcSTim J. Robbins #include <sys/exec.h> 4948b05c3fSKonstantin Belousov #include <sys/fcntl.h> 50ea0fabbcSTim J. Robbins #include <sys/imgact.h> 51ea0fabbcSTim J. Robbins #include <sys/imgact_elf.h> 52ea0fabbcSTim J. Robbins #include <sys/kernel.h> 53ea0fabbcSTim J. Robbins #include <sys/lock.h> 54ea0fabbcSTim J. Robbins #include <sys/malloc.h> 55ea0fabbcSTim J. Robbins #include <sys/module.h> 56ea0fabbcSTim J. Robbins #include <sys/mutex.h> 57ea0fabbcSTim J. Robbins #include <sys/proc.h> 586004362eSDavid Schultz #include <sys/resourcevar.h> 59ea0fabbcSTim J. Robbins #include <sys/signalvar.h> 60ea0fabbcSTim J. Robbins #include <sys/syscallsubr.h> 61c0aa0e2cSEd Maste #include <sys/sysctl.h> 62ea0fabbcSTim J. Robbins #include <sys/sysent.h> 63ea0fabbcSTim J. Robbins #include <sys/sysproto.h> 64ea0fabbcSTim J. Robbins #include <sys/vnode.h> 657c09e6c0SAlexander Leidinger #include <sys/eventhandler.h> 66ea0fabbcSTim J. Robbins 67ea0fabbcSTim J. Robbins #include <vm/vm.h> 68ea0fabbcSTim J. Robbins #include <vm/pmap.h> 69ea0fabbcSTim J. Robbins #include <vm/vm_extern.h> 70ea0fabbcSTim J. Robbins #include <vm/vm_map.h> 71ea0fabbcSTim J. Robbins #include <vm/vm_object.h> 72ea0fabbcSTim J. Robbins #include <vm/vm_page.h> 73ea0fabbcSTim J. Robbins #include <vm/vm_param.h> 74ea0fabbcSTim J. Robbins 75ea0fabbcSTim J. Robbins #include <machine/cpu.h> 76ea0fabbcSTim J. Robbins #include <machine/md_var.h> 776004362eSDavid Schultz #include <machine/pcb.h> 78ea0fabbcSTim J. Robbins #include <machine/specialreg.h> 79d41e41f9SJohn Baldwin #include <machine/trap.h> 80ea0fabbcSTim J. Robbins 81ea0fabbcSTim J. Robbins #include <amd64/linux32/linux.h> 82ea0fabbcSTim J. Robbins #include <amd64/linux32/linux32_proto.h> 837c09e6c0SAlexander Leidinger #include <compat/linux/linux_emul.h> 84d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h> 85ea0fabbcSTim J. Robbins #include <compat/linux/linux_mib.h> 864d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h> 87ea0fabbcSTim J. Robbins #include <compat/linux/linux_signal.h> 88ea0fabbcSTim J. Robbins #include <compat/linux/linux_util.h> 89bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h> 90ea0fabbcSTim J. Robbins 91ea0fabbcSTim J. Robbins MODULE_VERSION(linux, 1); 92ea0fabbcSTim J. Robbins 930020bdf1SDmitry Chagin const char *linux_kplatform; 94bdc37934SDmitry Chagin static int linux_szsigcode; 95bdc37934SDmitry Chagin static vm_object_t linux_shared_page_obj; 96bdc37934SDmitry Chagin static char *linux_shared_page_mapping; 97bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_start; 98bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_end; 99ea0fabbcSTim J. Robbins 1002f99bcceSJohn Baldwin extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL]; 101ea0fabbcSTim J. Robbins 102ea0fabbcSTim J. Robbins SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 103ea0fabbcSTim J. Robbins 10431174518SJohn Baldwin static int linux_fixup_elf(uintptr_t *stack_base, 105ea0fabbcSTim J. Robbins struct image_params *iparams); 10603b0d68cSJohn Baldwin static int linux_copyout_strings(struct image_params *imgp, 10731174518SJohn Baldwin uintptr_t *stack_base); 1089104847fSDavid Xu static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 109dc858467SEd Maste static void linux_exec_setregs(struct thread *td, 11031174518SJohn Baldwin struct image_params *imgp, uintptr_t stack); 11119059a13SJohn Baldwin static void linux32_fixlimit(struct rlimit *rl, int which); 112a95659f7SEd Maste static bool linux32_trans_osrel(const Elf_Note *note, int32_t *osrel); 113bdc37934SDmitry Chagin static void linux_vdso_install(void *param); 114bdc37934SDmitry Chagin static void linux_vdso_deinstall(void *param); 115*c26391f4SEdward Tomasz Napierala static void linux32_set_syscall_retval(struct thread *td, int error); 116ea0fabbcSTim J. Robbins 117ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN 255 118ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = { 119ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 0 */ 120ea0fabbcSTim J. Robbins 6, /* 1 T_PRIVINFLT */ 121ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 2 */ 122ea0fabbcSTim J. Robbins 3, /* 3 T_BPTFLT */ 123ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 4 */ 124ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 5 */ 125ea0fabbcSTim J. Robbins 16, /* 6 T_ARITHTRAP */ 126ea0fabbcSTim J. Robbins 254, /* 7 T_ASTFLT */ 127ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 8 */ 128ea0fabbcSTim J. Robbins 13, /* 9 T_PROTFLT */ 129ea0fabbcSTim J. Robbins 1, /* 10 T_TRCTRAP */ 130ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 11 */ 131ea0fabbcSTim J. Robbins 14, /* 12 T_PAGEFLT */ 132ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 13 */ 133ea0fabbcSTim J. Robbins 17, /* 14 T_ALIGNFLT */ 134ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 15 */ 135ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 16 */ 136ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 17 */ 137ea0fabbcSTim J. Robbins 0, /* 18 T_DIVIDE */ 138ea0fabbcSTim J. Robbins 2, /* 19 T_NMI */ 139ea0fabbcSTim J. Robbins 4, /* 20 T_OFLOW */ 140ea0fabbcSTim J. Robbins 5, /* 21 T_BOUND */ 141ea0fabbcSTim J. Robbins 7, /* 22 T_DNA */ 142ea0fabbcSTim J. Robbins 8, /* 23 T_DOUBLEFLT */ 143ea0fabbcSTim J. Robbins 9, /* 24 T_FPOPFLT */ 144ea0fabbcSTim J. Robbins 10, /* 25 T_TSSFLT */ 145ea0fabbcSTim J. Robbins 11, /* 26 T_SEGNPFLT */ 146ea0fabbcSTim J. Robbins 12, /* 27 T_STKFLT */ 147ea0fabbcSTim J. Robbins 18, /* 28 T_MCHK */ 148ea0fabbcSTim J. Robbins 19, /* 29 T_XMMFLT */ 149ea0fabbcSTim J. Robbins 15 /* 30 T_RESERVED */ 150ea0fabbcSTim J. Robbins }; 151ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \ 152ea24b056SPedro F. Giffuni ((code)<nitems(_bsd_to_linux_trapcode)? \ 153ea0fabbcSTim J. Robbins _bsd_to_linux_trapcode[(code)]: \ 154ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN) 155ea0fabbcSTim J. Robbins 156ea0fabbcSTim J. Robbins struct linux32_ps_strings { 157ea0fabbcSTim J. Robbins u_int32_t ps_argvstr; /* first of 0 or more argument strings */ 158f2c7668eSDavid Schultz u_int ps_nargvstr; /* the number of argument strings */ 159ea0fabbcSTim J. Robbins u_int32_t ps_envstr; /* first of 0 or more environment strings */ 160f2c7668eSDavid Schultz u_int ps_nenvstr; /* the number of environment strings */ 161ea0fabbcSTim J. Robbins }; 162ea0fabbcSTim J. Robbins 163bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_sigcode); 164bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode); 165bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_vsyscall); 1660020bdf1SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform); 167bdc37934SDmitry Chagin 168ea0fabbcSTim J. Robbins /* 169ea0fabbcSTim J. Robbins * If FreeBSD & Linux have a difference of opinion about what a trap 170ea0fabbcSTim J. Robbins * means, deal with it here. 171ea0fabbcSTim J. Robbins * 172ea0fabbcSTim J. Robbins * MPSAFE 173ea0fabbcSTim J. Robbins */ 174ea0fabbcSTim J. Robbins static int 175dc858467SEd Maste linux_translate_traps(int signal, int trap_code) 176ea0fabbcSTim J. Robbins { 177ea0fabbcSTim J. Robbins if (signal != SIGBUS) 178ad448975SEd Maste return (signal); 179ea0fabbcSTim J. Robbins switch (trap_code) { 180ea0fabbcSTim J. Robbins case T_PROTFLT: 181ea0fabbcSTim J. Robbins case T_TSSFLT: 182ea0fabbcSTim J. Robbins case T_DOUBLEFLT: 183ea0fabbcSTim J. Robbins case T_PAGEFLT: 184ad448975SEd Maste return (SIGSEGV); 185ea0fabbcSTim J. Robbins default: 186ad448975SEd Maste return (signal); 187ea0fabbcSTim J. Robbins } 188ea0fabbcSTim J. Robbins } 189ea0fabbcSTim J. Robbins 19003b0d68cSJohn Baldwin static int 191d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 192ea0fabbcSTim J. Robbins { 193ea0fabbcSTim J. Robbins Elf32_Auxargs *args; 1945f77b8a8SBrooks Davis Elf32_Auxinfo *argarray, *pos; 19503b0d68cSJohn Baldwin int error, issetugid; 1964d7c2e8aSDmitry Chagin 197ea0fabbcSTim J. Robbins args = (Elf32_Auxargs *)imgp->auxargs; 1985f77b8a8SBrooks Davis argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 1995f77b8a8SBrooks Davis M_WAITOK | M_ZERO); 200ea0fabbcSTim J. Robbins 201669414e4SXin LI issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0; 2025f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 203bdc37934SDmitry Chagin imgp->proc->p_sysent->sv_shared_page_base); 2045f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux32_vsyscall); 2055f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature); 2068d30f381SDmitry Chagin 2078d30f381SDmitry Chagin /* 2088d30f381SDmitry Chagin * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0, 2098d30f381SDmitry Chagin * as it has appeared in the 2.4.0-rc7 first time. 2108d30f381SDmitry Chagin * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK), 2118d30f381SDmitry Chagin * glibc falls back to the hard-coded CLK_TCK value when aux entry 2128d30f381SDmitry Chagin * is not present. 2138d30f381SDmitry Chagin * Also see linux_times() implementation. 2148d30f381SDmitry Chagin */ 2158d30f381SDmitry Chagin if (linux_kernver(curthread) >= LINUX_KERNVER_2004000) 2165f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 2175f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 2185f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 2195f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 2205f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 2215f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 2225f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 2235f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_BASE, args->base); 2245f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 2255f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 2265f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 2275f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 2285f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 2295f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 2304048f59cSDmitry Chagin AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary)); 2314048f59cSDmitry Chagin if (imgp->execpathp != 0) 2324048f59cSDmitry Chagin AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp)); 2334d7c2e8aSDmitry Chagin if (args->execfd != -1) 2345f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 2355f77b8a8SBrooks Davis AUXARGS_ENTRY(pos, AT_NULL, 0); 236ea0fabbcSTim J. Robbins 237ea0fabbcSTim J. Robbins free(imgp->auxargs, M_TEMP); 238ea0fabbcSTim J. Robbins imgp->auxargs = NULL; 2396362b1a6SJung-uk Kim KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 2405f77b8a8SBrooks Davis 241d8010b11SJohn Baldwin error = copyout(argarray, (void *)base, 242d8010b11SJohn Baldwin sizeof(*argarray) * LINUX_AT_COUNT); 2435f77b8a8SBrooks Davis free(argarray, M_TEMP); 24403b0d68cSJohn Baldwin return (error); 2455caa67faSJohn Baldwin } 246ea0fabbcSTim J. Robbins 2475caa67faSJohn Baldwin static int 24831174518SJohn Baldwin linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp) 2495caa67faSJohn Baldwin { 2505caa67faSJohn Baldwin Elf32_Addr *base; 2515caa67faSJohn Baldwin 2525caa67faSJohn Baldwin base = (Elf32_Addr *)*stack_base; 253ea0fabbcSTim J. Robbins base--; 2545f77b8a8SBrooks Davis if (suword32(base, (uint32_t)imgp->args->argc) == -1) 2555f77b8a8SBrooks Davis return (EFAULT); 25631174518SJohn Baldwin *stack_base = (uintptr_t)base; 257af682d48SDmitry Chagin return (0); 258ea0fabbcSTim J. Robbins } 259ea0fabbcSTim J. Robbins 260ea0fabbcSTim J. Robbins static void 2619104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 262ea0fabbcSTim J. Robbins { 263ea0fabbcSTim J. Robbins struct thread *td = curthread; 264ea0fabbcSTim J. Robbins struct proc *p = td->td_proc; 265ea0fabbcSTim J. Robbins struct sigacts *psp; 266ea0fabbcSTim J. Robbins struct trapframe *regs; 267ea0fabbcSTim J. Robbins struct l_rt_sigframe *fp, frame; 268ea0fabbcSTim J. Robbins int oonstack; 2699104847fSDavid Xu int sig; 2709104847fSDavid Xu int code; 271ea0fabbcSTim J. Robbins 2729104847fSDavid Xu sig = ksi->ksi_signo; 2739104847fSDavid Xu code = ksi->ksi_code; 274ea0fabbcSTim J. Robbins PROC_LOCK_ASSERT(p, MA_OWNED); 275ea0fabbcSTim J. Robbins psp = p->p_sigacts; 276ea0fabbcSTim J. Robbins mtx_assert(&psp->ps_mtx, MA_OWNED); 277ea0fabbcSTim J. Robbins regs = td->td_frame; 278ea0fabbcSTim J. Robbins oonstack = sigonstack(regs->tf_rsp); 279ea0fabbcSTim J. Robbins 2804ba25759SEd Maste /* Allocate space for the signal handler context. */ 281ea0fabbcSTim J. Robbins if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 282ea0fabbcSTim J. Robbins SIGISMEMBER(psp->ps_sigonstack, sig)) { 283aa949be5SJohn Baldwin fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 284ea0fabbcSTim J. Robbins td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 285ea0fabbcSTim J. Robbins } else 286ea0fabbcSTim J. Robbins fp = (struct l_rt_sigframe *)regs->tf_rsp - 1; 287ea0fabbcSTim J. Robbins mtx_unlock(&psp->ps_mtx); 288ea0fabbcSTim J. Robbins 2894ba25759SEd Maste /* Build the argument list for the signal handler. */ 2904ab7403bSDmitry Chagin sig = bsd_to_linux_signal(sig); 291ea0fabbcSTim J. Robbins 292ea0fabbcSTim J. Robbins bzero(&frame, sizeof(frame)); 293ea0fabbcSTim J. Robbins 294ea0fabbcSTim J. Robbins frame.sf_handler = PTROUT(catcher); 295ea0fabbcSTim J. Robbins frame.sf_sig = sig; 296ea0fabbcSTim J. Robbins frame.sf_siginfo = PTROUT(&fp->sf_si); 297ea0fabbcSTim J. Robbins frame.sf_ucontext = PTROUT(&fp->sf_sc); 298ea0fabbcSTim J. Robbins 2994ba25759SEd Maste /* Fill in POSIX parts. */ 300aa8b2011SKonstantin Belousov ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig); 301ea0fabbcSTim J. Robbins 302ea0fabbcSTim J. Robbins /* 3034ba25759SEd Maste * Build the signal context to be used by sigreturn and libgcc unwind. 304ea0fabbcSTim J. Robbins */ 305ea0fabbcSTim J. Robbins frame.sf_sc.uc_flags = 0; /* XXX ??? */ 306ea0fabbcSTim J. Robbins frame.sf_sc.uc_link = 0; /* XXX ??? */ 307ea0fabbcSTim J. Robbins 308ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 309ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size; 310ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 311ea0fabbcSTim J. Robbins ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 312ea0fabbcSTim J. Robbins PROC_UNLOCK(p); 313ea0fabbcSTim J. Robbins 314ea0fabbcSTim J. Robbins bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask); 315ea0fabbcSTim J. Robbins 3164ab7403bSDmitry Chagin frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask; 317ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_edi = regs->tf_rdi; 318ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_esi = regs->tf_rsi; 319ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_rbp; 320ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_rbx; 321bdc37934SDmitry Chagin frame.sf_sc.uc_mcontext.sc_esp = regs->tf_rsp; 322ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_edx = regs->tf_rdx; 323ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_rcx; 324ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_eax = regs->tf_rax; 325ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_eip = regs->tf_rip; 326ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs; 3272c66cccaSKonstantin Belousov frame.sf_sc.uc_mcontext.sc_gs = regs->tf_gs; 3282c66cccaSKonstantin Belousov frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs; 3292c66cccaSKonstantin Belousov frame.sf_sc.uc_mcontext.sc_es = regs->tf_es; 3302c66cccaSKonstantin Belousov frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds; 331ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags; 332ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp; 333ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss; 334ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_err = regs->tf_err; 33596a2b635SKonstantin Belousov frame.sf_sc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr; 336ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 337ea0fabbcSTim J. Robbins 338ea0fabbcSTim J. Robbins if (copyout(&frame, fp, sizeof(frame)) != 0) { 339ea0fabbcSTim J. Robbins /* 340ea0fabbcSTim J. Robbins * Process has trashed its stack; give it an illegal 341ea0fabbcSTim J. Robbins * instruction to halt it in its tracks. 342ea0fabbcSTim J. Robbins */ 343ea0fabbcSTim J. Robbins PROC_LOCK(p); 344ea0fabbcSTim J. Robbins sigexit(td, SIGILL); 345ea0fabbcSTim J. Robbins } 346ea0fabbcSTim J. Robbins 3474ba25759SEd Maste /* Build context to run handler in. */ 348ea0fabbcSTim J. Robbins regs->tf_rsp = PTROUT(fp); 349bdc37934SDmitry Chagin regs->tf_rip = linux32_rt_sigcode; 35022eca0bfSKonstantin Belousov regs->tf_rflags &= ~(PSL_T | PSL_D); 351ea0fabbcSTim J. Robbins regs->tf_cs = _ucode32sel; 352ea0fabbcSTim J. Robbins regs->tf_ss = _udatasel; 3532c66cccaSKonstantin Belousov regs->tf_ds = _udatasel; 3542c66cccaSKonstantin Belousov regs->tf_es = _udatasel; 3552c66cccaSKonstantin Belousov regs->tf_fs = _ufssel; 3562c66cccaSKonstantin Belousov regs->tf_gs = _ugssel; 3572c66cccaSKonstantin Belousov regs->tf_flags = TF_HASSEGS; 358e6c006d9SJung-uk Kim set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 359ea0fabbcSTim J. Robbins PROC_LOCK(p); 360ea0fabbcSTim J. Robbins mtx_lock(&psp->ps_mtx); 361ea0fabbcSTim J. Robbins } 362ea0fabbcSTim J. Robbins 363ea0fabbcSTim J. Robbins /* 364ea0fabbcSTim J. Robbins * Send an interrupt to process. 365ea0fabbcSTim J. Robbins * 366ea0fabbcSTim J. Robbins * Stack is set up to allow sigcode stored 367ea0fabbcSTim J. Robbins * in u. to call routine, followed by kcall 368ea0fabbcSTim J. Robbins * to sigreturn routine below. After sigreturn 369ea0fabbcSTim J. Robbins * resets the signal mask, the stack, and the 370ea0fabbcSTim J. Robbins * frame pointer, it returns to the user 371ea0fabbcSTim J. Robbins * specified pc, psl. 372ea0fabbcSTim J. Robbins */ 373ea0fabbcSTim J. Robbins static void 3749104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 375ea0fabbcSTim J. Robbins { 376ea0fabbcSTim J. Robbins struct thread *td = curthread; 377ea0fabbcSTim J. Robbins struct proc *p = td->td_proc; 378ea0fabbcSTim J. Robbins struct sigacts *psp; 379ea0fabbcSTim J. Robbins struct trapframe *regs; 380ea0fabbcSTim J. Robbins struct l_sigframe *fp, frame; 381ea0fabbcSTim J. Robbins l_sigset_t lmask; 3824ab7403bSDmitry Chagin int oonstack; 3839104847fSDavid Xu int sig, code; 384ea0fabbcSTim J. Robbins 3859104847fSDavid Xu sig = ksi->ksi_signo; 3869104847fSDavid Xu code = ksi->ksi_code; 387ea0fabbcSTim J. Robbins PROC_LOCK_ASSERT(p, MA_OWNED); 388ea0fabbcSTim J. Robbins psp = p->p_sigacts; 389ea0fabbcSTim J. Robbins mtx_assert(&psp->ps_mtx, MA_OWNED); 390ea0fabbcSTim J. Robbins if (SIGISMEMBER(psp->ps_siginfo, sig)) { 391ea0fabbcSTim J. Robbins /* Signal handler installed with SA_SIGINFO. */ 3929104847fSDavid Xu linux_rt_sendsig(catcher, ksi, mask); 393ea0fabbcSTim J. Robbins return; 394ea0fabbcSTim J. Robbins } 395ea0fabbcSTim J. Robbins 396ea0fabbcSTim J. Robbins regs = td->td_frame; 397ea0fabbcSTim J. Robbins oonstack = sigonstack(regs->tf_rsp); 398ea0fabbcSTim J. Robbins 3994ba25759SEd Maste /* Allocate space for the signal handler context. */ 400ea0fabbcSTim J. Robbins if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 401ea0fabbcSTim J. Robbins SIGISMEMBER(psp->ps_sigonstack, sig)) { 402aa949be5SJohn Baldwin fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 403ea0fabbcSTim J. Robbins td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 404ea0fabbcSTim J. Robbins } else 405ea0fabbcSTim J. Robbins fp = (struct l_sigframe *)regs->tf_rsp - 1; 406ea0fabbcSTim J. Robbins mtx_unlock(&psp->ps_mtx); 407ea0fabbcSTim J. Robbins PROC_UNLOCK(p); 408ea0fabbcSTim J. Robbins 4094ba25759SEd Maste /* Build the argument list for the signal handler. */ 4104ab7403bSDmitry Chagin sig = bsd_to_linux_signal(sig); 411ea0fabbcSTim J. Robbins 412ea0fabbcSTim J. Robbins bzero(&frame, sizeof(frame)); 413ea0fabbcSTim J. Robbins 414ea0fabbcSTim J. Robbins frame.sf_handler = PTROUT(catcher); 415ea0fabbcSTim J. Robbins frame.sf_sig = sig; 416ea0fabbcSTim J. Robbins 417ea0fabbcSTim J. Robbins bsd_to_linux_sigset(mask, &lmask); 418ea0fabbcSTim J. Robbins 4194ba25759SEd Maste /* Build the signal context to be used by sigreturn. */ 4204ab7403bSDmitry Chagin frame.sf_sc.sc_mask = lmask.__mask; 4212c66cccaSKonstantin Belousov frame.sf_sc.sc_gs = regs->tf_gs; 4222c66cccaSKonstantin Belousov frame.sf_sc.sc_fs = regs->tf_fs; 4232c66cccaSKonstantin Belousov frame.sf_sc.sc_es = regs->tf_es; 4242c66cccaSKonstantin Belousov frame.sf_sc.sc_ds = regs->tf_ds; 425ea0fabbcSTim J. Robbins frame.sf_sc.sc_edi = regs->tf_rdi; 426ea0fabbcSTim J. Robbins frame.sf_sc.sc_esi = regs->tf_rsi; 427ea0fabbcSTim J. Robbins frame.sf_sc.sc_ebp = regs->tf_rbp; 428ea0fabbcSTim J. Robbins frame.sf_sc.sc_ebx = regs->tf_rbx; 429bdc37934SDmitry Chagin frame.sf_sc.sc_esp = regs->tf_rsp; 430ea0fabbcSTim J. Robbins frame.sf_sc.sc_edx = regs->tf_rdx; 431ea0fabbcSTim J. Robbins frame.sf_sc.sc_ecx = regs->tf_rcx; 432ea0fabbcSTim J. Robbins frame.sf_sc.sc_eax = regs->tf_rax; 433ea0fabbcSTim J. Robbins frame.sf_sc.sc_eip = regs->tf_rip; 434ea0fabbcSTim J. Robbins frame.sf_sc.sc_cs = regs->tf_cs; 435ea0fabbcSTim J. Robbins frame.sf_sc.sc_eflags = regs->tf_rflags; 436ea0fabbcSTim J. Robbins frame.sf_sc.sc_esp_at_signal = regs->tf_rsp; 437ea0fabbcSTim J. Robbins frame.sf_sc.sc_ss = regs->tf_ss; 438ea0fabbcSTim J. Robbins frame.sf_sc.sc_err = regs->tf_err; 43996a2b635SKonstantin Belousov frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr; 440ea0fabbcSTim J. Robbins frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code); 441ea0fabbcSTim J. Robbins 4424ab7403bSDmitry Chagin frame.sf_extramask[0] = lmask.__mask; 443ea0fabbcSTim J. Robbins 444ea0fabbcSTim J. Robbins if (copyout(&frame, fp, sizeof(frame)) != 0) { 445ea0fabbcSTim J. Robbins /* 446ea0fabbcSTim J. Robbins * Process has trashed its stack; give it an illegal 447ea0fabbcSTim J. Robbins * instruction to halt it in its tracks. 448ea0fabbcSTim J. Robbins */ 449ea0fabbcSTim J. Robbins PROC_LOCK(p); 450ea0fabbcSTim J. Robbins sigexit(td, SIGILL); 451ea0fabbcSTim J. Robbins } 452ea0fabbcSTim J. Robbins 4534ba25759SEd Maste /* Build context to run handler in. */ 454ea0fabbcSTim J. Robbins regs->tf_rsp = PTROUT(fp); 455bdc37934SDmitry Chagin regs->tf_rip = linux32_sigcode; 45622eca0bfSKonstantin Belousov regs->tf_rflags &= ~(PSL_T | PSL_D); 457ea0fabbcSTim J. Robbins regs->tf_cs = _ucode32sel; 458ea0fabbcSTim J. Robbins regs->tf_ss = _udatasel; 4592c66cccaSKonstantin Belousov regs->tf_ds = _udatasel; 4602c66cccaSKonstantin Belousov regs->tf_es = _udatasel; 4612c66cccaSKonstantin Belousov regs->tf_fs = _ufssel; 4622c66cccaSKonstantin Belousov regs->tf_gs = _ugssel; 4632c66cccaSKonstantin Belousov regs->tf_flags = TF_HASSEGS; 464e6c006d9SJung-uk Kim set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 465ea0fabbcSTim J. Robbins PROC_LOCK(p); 466ea0fabbcSTim J. Robbins mtx_lock(&psp->ps_mtx); 467ea0fabbcSTim J. Robbins } 468ea0fabbcSTim J. Robbins 469ea0fabbcSTim J. Robbins /* 470ea0fabbcSTim J. Robbins * System call to cleanup state after a signal 471ea0fabbcSTim J. Robbins * has been taken. Reset signal mask and 472ea0fabbcSTim J. Robbins * stack state from context left by sendsig (above). 473ea0fabbcSTim J. Robbins * Return to previous pc and psl as specified by 474ea0fabbcSTim J. Robbins * context left by sendsig. Check carefully to 475ea0fabbcSTim J. Robbins * make sure that the user has not modified the 476ea0fabbcSTim J. Robbins * psl to gain improper privileges or to cause 477ea0fabbcSTim J. Robbins * a machine fault. 478ea0fabbcSTim J. Robbins */ 479ea0fabbcSTim J. Robbins int 480ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 481ea0fabbcSTim J. Robbins { 482ea0fabbcSTim J. Robbins struct l_sigframe frame; 483ea0fabbcSTim J. Robbins struct trapframe *regs; 484d6e029adSKonstantin Belousov sigset_t bmask; 485ea0fabbcSTim J. Robbins l_sigset_t lmask; 4864ab7403bSDmitry Chagin int eflags; 4879104847fSDavid Xu ksiginfo_t ksi; 488ea0fabbcSTim J. Robbins 489ea0fabbcSTim J. Robbins regs = td->td_frame; 490ea0fabbcSTim J. Robbins 491ea0fabbcSTim J. Robbins /* 492ea0fabbcSTim J. Robbins * The trampoline code hands us the sigframe. 493ea0fabbcSTim J. Robbins * It is unsafe to keep track of it ourselves, in the event that a 494ea0fabbcSTim J. Robbins * program jumps out of a signal handler. 495ea0fabbcSTim J. Robbins */ 496ea0fabbcSTim J. Robbins if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 497ea0fabbcSTim J. Robbins return (EFAULT); 498ea0fabbcSTim J. Robbins 4994ba25759SEd Maste /* Check for security violations. */ 500ea0fabbcSTim J. Robbins #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 501ea0fabbcSTim J. Robbins eflags = frame.sf_sc.sc_eflags; 5023d271aaaSEd Maste if (!EFLAGS_SECURE(eflags, regs->tf_rflags)) 503ea0fabbcSTim J. Robbins return(EINVAL); 504ea0fabbcSTim J. Robbins 505ea0fabbcSTim J. Robbins /* 506ea0fabbcSTim J. Robbins * Don't allow users to load a valid privileged %cs. Let the 507ea0fabbcSTim J. Robbins * hardware check for invalid selectors, excess privilege in 508ea0fabbcSTim J. Robbins * other selectors, invalid %eip's and invalid %esp's. 509ea0fabbcSTim J. Robbins */ 510ea0fabbcSTim J. Robbins #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 511ea0fabbcSTim J. Robbins if (!CS_SECURE(frame.sf_sc.sc_cs)) { 5129104847fSDavid Xu ksiginfo_init_trap(&ksi); 5139104847fSDavid Xu ksi.ksi_signo = SIGBUS; 5149104847fSDavid Xu ksi.ksi_code = BUS_OBJERR; 5159104847fSDavid Xu ksi.ksi_trapno = T_PROTFLT; 5169104847fSDavid Xu ksi.ksi_addr = (void *)regs->tf_rip; 5179104847fSDavid Xu trapsignal(td, &ksi); 518ea0fabbcSTim J. Robbins return(EINVAL); 519ea0fabbcSTim J. Robbins } 520ea0fabbcSTim J. Robbins 5214ab7403bSDmitry Chagin lmask.__mask = frame.sf_sc.sc_mask; 5224ab7403bSDmitry Chagin lmask.__mask = frame.sf_extramask[0]; 523d6e029adSKonstantin Belousov linux_to_bsd_sigset(&lmask, &bmask); 524d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 525ea0fabbcSTim J. Robbins 5264ba25759SEd Maste /* Restore signal context. */ 527ea0fabbcSTim J. Robbins regs->tf_rdi = frame.sf_sc.sc_edi; 528ea0fabbcSTim J. Robbins regs->tf_rsi = frame.sf_sc.sc_esi; 529ea0fabbcSTim J. Robbins regs->tf_rbp = frame.sf_sc.sc_ebp; 530ea0fabbcSTim J. Robbins regs->tf_rbx = frame.sf_sc.sc_ebx; 531ea0fabbcSTim J. Robbins regs->tf_rdx = frame.sf_sc.sc_edx; 532ea0fabbcSTim J. Robbins regs->tf_rcx = frame.sf_sc.sc_ecx; 533ea0fabbcSTim J. Robbins regs->tf_rax = frame.sf_sc.sc_eax; 534ea0fabbcSTim J. Robbins regs->tf_rip = frame.sf_sc.sc_eip; 535ea0fabbcSTim J. Robbins regs->tf_cs = frame.sf_sc.sc_cs; 5362c66cccaSKonstantin Belousov regs->tf_ds = frame.sf_sc.sc_ds; 5372c66cccaSKonstantin Belousov regs->tf_es = frame.sf_sc.sc_es; 5382c66cccaSKonstantin Belousov regs->tf_fs = frame.sf_sc.sc_fs; 5392c66cccaSKonstantin Belousov regs->tf_gs = frame.sf_sc.sc_gs; 540ea0fabbcSTim J. Robbins regs->tf_rflags = eflags; 541ea0fabbcSTim J. Robbins regs->tf_rsp = frame.sf_sc.sc_esp_at_signal; 542ea0fabbcSTim J. Robbins regs->tf_ss = frame.sf_sc.sc_ss; 543e6c006d9SJung-uk Kim set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 544ea0fabbcSTim J. Robbins 545ea0fabbcSTim J. Robbins return (EJUSTRETURN); 546ea0fabbcSTim J. Robbins } 547ea0fabbcSTim J. Robbins 548ea0fabbcSTim J. Robbins /* 549ea0fabbcSTim J. Robbins * System call to cleanup state after a signal 550ea0fabbcSTim J. Robbins * has been taken. Reset signal mask and 551ea0fabbcSTim J. Robbins * stack state from context left by rt_sendsig (above). 552ea0fabbcSTim J. Robbins * Return to previous pc and psl as specified by 553ea0fabbcSTim J. Robbins * context left by sendsig. Check carefully to 554ea0fabbcSTim J. Robbins * make sure that the user has not modified the 555ea0fabbcSTim J. Robbins * psl to gain improper privileges or to cause 556ea0fabbcSTim J. Robbins * a machine fault. 557ea0fabbcSTim J. Robbins */ 558ea0fabbcSTim J. Robbins int 559ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 560ea0fabbcSTim J. Robbins { 561ea0fabbcSTim J. Robbins struct l_ucontext uc; 562ea0fabbcSTim J. Robbins struct l_sigcontext *context; 563d6e029adSKonstantin Belousov sigset_t bmask; 564ea0fabbcSTim J. Robbins l_stack_t *lss; 565ea0fabbcSTim J. Robbins stack_t ss; 566ea0fabbcSTim J. Robbins struct trapframe *regs; 567ea0fabbcSTim J. Robbins int eflags; 5689104847fSDavid Xu ksiginfo_t ksi; 569ea0fabbcSTim J. Robbins 570ea0fabbcSTim J. Robbins regs = td->td_frame; 571ea0fabbcSTim J. Robbins 572ea0fabbcSTim J. Robbins /* 573ea0fabbcSTim J. Robbins * The trampoline code hands us the ucontext. 574ea0fabbcSTim J. Robbins * It is unsafe to keep track of it ourselves, in the event that a 575ea0fabbcSTim J. Robbins * program jumps out of a signal handler. 576ea0fabbcSTim J. Robbins */ 577ea0fabbcSTim J. Robbins if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 578ea0fabbcSTim J. Robbins return (EFAULT); 579ea0fabbcSTim J. Robbins 580ea0fabbcSTim J. Robbins context = &uc.uc_mcontext; 581ea0fabbcSTim J. Robbins 5824ba25759SEd Maste /* Check for security violations. */ 583ea0fabbcSTim J. Robbins #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 584ea0fabbcSTim J. Robbins eflags = context->sc_eflags; 5853d271aaaSEd Maste if (!EFLAGS_SECURE(eflags, regs->tf_rflags)) 586ea0fabbcSTim J. Robbins return(EINVAL); 587ea0fabbcSTim J. Robbins 588ea0fabbcSTim J. Robbins /* 589ea0fabbcSTim J. Robbins * Don't allow users to load a valid privileged %cs. Let the 590ea0fabbcSTim J. Robbins * hardware check for invalid selectors, excess privilege in 591ea0fabbcSTim J. Robbins * other selectors, invalid %eip's and invalid %esp's. 592ea0fabbcSTim J. Robbins */ 593ea0fabbcSTim J. Robbins #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 594ea0fabbcSTim J. Robbins if (!CS_SECURE(context->sc_cs)) { 5959104847fSDavid Xu ksiginfo_init_trap(&ksi); 5969104847fSDavid Xu ksi.ksi_signo = SIGBUS; 5979104847fSDavid Xu ksi.ksi_code = BUS_OBJERR; 5989104847fSDavid Xu ksi.ksi_trapno = T_PROTFLT; 5999104847fSDavid Xu ksi.ksi_addr = (void *)regs->tf_rip; 6009104847fSDavid Xu trapsignal(td, &ksi); 601ea0fabbcSTim J. Robbins return(EINVAL); 602ea0fabbcSTim J. Robbins } 603ea0fabbcSTim J. Robbins 604d6e029adSKonstantin Belousov linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 605d6e029adSKonstantin Belousov kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 606ea0fabbcSTim J. Robbins 607ea0fabbcSTim J. Robbins /* 608ea0fabbcSTim J. Robbins * Restore signal context 609ea0fabbcSTim J. Robbins */ 6102c66cccaSKonstantin Belousov regs->tf_gs = context->sc_gs; 6112c66cccaSKonstantin Belousov regs->tf_fs = context->sc_fs; 6122c66cccaSKonstantin Belousov regs->tf_es = context->sc_es; 6132c66cccaSKonstantin Belousov regs->tf_ds = context->sc_ds; 614ea0fabbcSTim J. Robbins regs->tf_rdi = context->sc_edi; 615ea0fabbcSTim J. Robbins regs->tf_rsi = context->sc_esi; 616ea0fabbcSTim J. Robbins regs->tf_rbp = context->sc_ebp; 617ea0fabbcSTim J. Robbins regs->tf_rbx = context->sc_ebx; 618ea0fabbcSTim J. Robbins regs->tf_rdx = context->sc_edx; 619ea0fabbcSTim J. Robbins regs->tf_rcx = context->sc_ecx; 620ea0fabbcSTim J. Robbins regs->tf_rax = context->sc_eax; 621ea0fabbcSTim J. Robbins regs->tf_rip = context->sc_eip; 622ea0fabbcSTim J. Robbins regs->tf_cs = context->sc_cs; 623ea0fabbcSTim J. Robbins regs->tf_rflags = eflags; 624ea0fabbcSTim J. Robbins regs->tf_rsp = context->sc_esp_at_signal; 625ea0fabbcSTim J. Robbins regs->tf_ss = context->sc_ss; 626e6c006d9SJung-uk Kim set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 627ea0fabbcSTim J. Robbins 628ea0fabbcSTim J. Robbins /* 629ea0fabbcSTim J. Robbins * call sigaltstack & ignore results.. 630ea0fabbcSTim J. Robbins */ 631ea0fabbcSTim J. Robbins lss = &uc.uc_stack; 632ea0fabbcSTim J. Robbins ss.ss_sp = PTRIN(lss->ss_sp); 633ea0fabbcSTim J. Robbins ss.ss_size = lss->ss_size; 634ea0fabbcSTim J. Robbins ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 635ea0fabbcSTim J. Robbins 636ea0fabbcSTim J. Robbins (void)kern_sigaltstack(td, &ss, NULL); 637ea0fabbcSTim J. Robbins 638ea0fabbcSTim J. Robbins return (EJUSTRETURN); 639ea0fabbcSTim J. Robbins } 640ea0fabbcSTim J. Robbins 641afe1a688SKonstantin Belousov static int 6422d88da2fSKonstantin Belousov linux32_fetch_syscall_args(struct thread *td) 643ea0fabbcSTim J. Robbins { 644afe1a688SKonstantin Belousov struct proc *p; 645afe1a688SKonstantin Belousov struct trapframe *frame; 6462d88da2fSKonstantin Belousov struct syscall_args *sa; 647afe1a688SKonstantin Belousov 648afe1a688SKonstantin Belousov p = td->td_proc; 649afe1a688SKonstantin Belousov frame = td->td_frame; 6502d88da2fSKonstantin Belousov sa = &td->td_sa; 651afe1a688SKonstantin Belousov 652afe1a688SKonstantin Belousov sa->args[0] = frame->tf_rbx; 653afe1a688SKonstantin Belousov sa->args[1] = frame->tf_rcx; 654afe1a688SKonstantin Belousov sa->args[2] = frame->tf_rdx; 655afe1a688SKonstantin Belousov sa->args[3] = frame->tf_rsi; 656afe1a688SKonstantin Belousov sa->args[4] = frame->tf_rdi; 657afe1a688SKonstantin Belousov sa->args[5] = frame->tf_rbp; /* Unconfirmed */ 658afe1a688SKonstantin Belousov sa->code = frame->tf_rax; 659afe1a688SKonstantin Belousov 660afe1a688SKonstantin Belousov if (sa->code >= p->p_sysent->sv_size) 661fcdffc03SDmitry Chagin /* nosys */ 6625047105bSJohn Baldwin sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 663afe1a688SKonstantin Belousov else 664afe1a688SKonstantin Belousov sa->callp = &p->p_sysent->sv_table[sa->code]; 665afe1a688SKonstantin Belousov sa->narg = sa->callp->sy_narg; 666afe1a688SKonstantin Belousov 667afe1a688SKonstantin Belousov td->td_retval[0] = 0; 668afe1a688SKonstantin Belousov td->td_retval[1] = frame->tf_rdx; 669afe1a688SKonstantin Belousov 670afe1a688SKonstantin Belousov return (0); 671ea0fabbcSTim J. Robbins } 672ea0fabbcSTim J. Robbins 673*c26391f4SEdward Tomasz Napierala static void 674*c26391f4SEdward Tomasz Napierala linux32_set_syscall_retval(struct thread *td, int error) 675*c26391f4SEdward Tomasz Napierala { 676*c26391f4SEdward Tomasz Napierala struct trapframe *frame = td->td_frame; 677*c26391f4SEdward Tomasz Napierala 678*c26391f4SEdward Tomasz Napierala cpu_set_syscall_retval(td, error); 679*c26391f4SEdward Tomasz Napierala 680*c26391f4SEdward Tomasz Napierala if (__predict_false(error != 0)) { 681*c26391f4SEdward Tomasz Napierala if (error != ERESTART && error != EJUSTRETURN) 682*c26391f4SEdward Tomasz Napierala frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error); 683*c26391f4SEdward Tomasz Napierala } 684*c26391f4SEdward Tomasz Napierala } 685*c26391f4SEdward Tomasz Napierala 686ea0fabbcSTim J. Robbins /* 687ea0fabbcSTim J. Robbins * Clear registers on exec 688ea0fabbcSTim J. Robbins * XXX copied from ia32_signal.c. 689ea0fabbcSTim J. Robbins */ 690ea0fabbcSTim J. Robbins static void 69131174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp, 69231174518SJohn Baldwin uintptr_t stack) 693ea0fabbcSTim J. Robbins { 694ea0fabbcSTim J. Robbins struct trapframe *regs = td->td_frame; 695ea0fabbcSTim J. Robbins struct pcb *pcb = td->td_pcb; 6967c5d1690SKonstantin Belousov register_t saved_rflags; 6977c5d1690SKonstantin Belousov 6987c5d1690SKonstantin Belousov regs = td->td_frame; 6997c5d1690SKonstantin Belousov pcb = td->td_pcb; 700ea0fabbcSTim J. Robbins 7012c66cccaSKonstantin Belousov if (td->td_proc->p_md.md_ldt != NULL) 7022c66cccaSKonstantin Belousov user_ldt_free(td); 7032c66cccaSKonstantin Belousov 7049c5b213eSJung-uk Kim critical_enter(); 705ea0fabbcSTim J. Robbins wrmsr(MSR_FSBASE, 0); 706ea0fabbcSTim J. Robbins wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 707ea0fabbcSTim J. Robbins pcb->pcb_fsbase = 0; 708ea0fabbcSTim J. Robbins pcb->pcb_gsbase = 0; 7099c5b213eSJung-uk Kim critical_exit(); 7102ee8325fSJohn Baldwin pcb->pcb_initial_fpucw = __LINUX_NPXCW__; 711ea0fabbcSTim J. Robbins 7127c5d1690SKonstantin Belousov saved_rflags = regs->tf_rflags & PSL_T; 713ea0fabbcSTim J. Robbins bzero((char *)regs, sizeof(struct trapframe)); 714a107d8aaSNathan Whitehorn regs->tf_rip = imgp->entry_addr; 715ea0fabbcSTim J. Robbins regs->tf_rsp = stack; 7167c5d1690SKonstantin Belousov regs->tf_rflags = PSL_USER | saved_rflags; 7172c66cccaSKonstantin Belousov regs->tf_gs = _ugssel; 7182c66cccaSKonstantin Belousov regs->tf_fs = _ufssel; 7192c66cccaSKonstantin Belousov regs->tf_es = _udatasel; 7202c66cccaSKonstantin Belousov regs->tf_ds = _udatasel; 721ea0fabbcSTim J. Robbins regs->tf_ss = _udatasel; 7222c66cccaSKonstantin Belousov regs->tf_flags = TF_HASSEGS; 723ea0fabbcSTim J. Robbins regs->tf_cs = _ucode32sel; 724397df744SBrooks Davis regs->tf_rbx = (register_t)imgp->ps_strings; 725bdbf2db5SJung-uk Kim 7262a988f7cSStephan Uphoff fpstate_drop(td); 727ea0fabbcSTim J. Robbins 7281b3c3256SKonstantin Belousov /* Do full restore on return so that we can change to a different %cs */ 729e6c006d9SJung-uk Kim set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET); 730ea0fabbcSTim J. Robbins } 731ea0fabbcSTim J. Robbins 732ea0fabbcSTim J. Robbins /* 733ea0fabbcSTim J. Robbins * XXX copied from ia32_sysvec.c. 734ea0fabbcSTim J. Robbins */ 73503b0d68cSJohn Baldwin static int 73631174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 737ea0fabbcSTim J. Robbins { 73803b0d68cSJohn Baldwin int argc, envc, error; 739ea0fabbcSTim J. Robbins u_int32_t *vectp; 74031174518SJohn Baldwin char *stringp; 74131174518SJohn Baldwin uintptr_t destp, ustringp; 742ea0fabbcSTim J. Robbins struct linux32_ps_strings *arginfo; 7434048f59cSDmitry Chagin char canary[LINUX_AT_RANDOM_LEN]; 7444048f59cSDmitry Chagin size_t execpath_len; 745ea0fabbcSTim J. Robbins 7464ba25759SEd Maste /* Calculate string base and vector table pointers. */ 7474048f59cSDmitry Chagin if (imgp->execpath != NULL && imgp->auxargs != NULL) 7484048f59cSDmitry Chagin execpath_len = strlen(imgp->execpath) + 1; 7494048f59cSDmitry Chagin else 7504048f59cSDmitry Chagin execpath_len = 0; 7514048f59cSDmitry Chagin 752ea0fabbcSTim J. Robbins arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS; 75331174518SJohn Baldwin destp = (uintptr_t)arginfo; 754ea0fabbcSTim J. Robbins 7554048f59cSDmitry Chagin if (execpath_len != 0) { 75631174518SJohn Baldwin destp -= execpath_len; 75723a5b4edSJohn Baldwin destp = rounddown2(destp, sizeof(uint32_t)); 758b24e6ac8SBrooks Davis imgp->execpathp = (void *)destp; 759b24e6ac8SBrooks Davis error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 76003b0d68cSJohn Baldwin if (error != 0) 76103b0d68cSJohn Baldwin return (error); 7624048f59cSDmitry Chagin } 7634048f59cSDmitry Chagin 7644ba25759SEd Maste /* Prepare the canary for SSP. */ 7654048f59cSDmitry Chagin arc4rand(canary, sizeof(canary), 0); 76623a5b4edSJohn Baldwin destp -= roundup(sizeof(canary), sizeof(uint32_t)); 767b24e6ac8SBrooks Davis imgp->canary = (void *)destp; 768b24e6ac8SBrooks Davis error = copyout(canary, imgp->canary, sizeof(canary)); 76903b0d68cSJohn Baldwin if (error != 0) 77003b0d68cSJohn Baldwin return (error); 7714048f59cSDmitry Chagin 77231174518SJohn Baldwin /* Allocate room for the argument and environment strings. */ 77331174518SJohn Baldwin destp -= ARG_MAX - imgp->args->stringspace; 77423a5b4edSJohn Baldwin destp = rounddown2(destp, sizeof(uint32_t)); 77531174518SJohn Baldwin ustringp = destp; 77631174518SJohn Baldwin 77703b0d68cSJohn Baldwin if (imgp->auxargs) { 778d8010b11SJohn Baldwin /* 779d8010b11SJohn Baldwin * Allocate room on the stack for the ELF auxargs 780d8010b11SJohn Baldwin * array. It has LINUX_AT_COUNT entries. 781d8010b11SJohn Baldwin */ 782d8010b11SJohn Baldwin destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo); 78323a5b4edSJohn Baldwin destp = rounddown2(destp, sizeof(uint32_t)); 78403b0d68cSJohn Baldwin } 785ea0fabbcSTim J. Robbins 78631174518SJohn Baldwin vectp = (uint32_t *)destp; 78731174518SJohn Baldwin 78873c8686eSJohn Baldwin /* 78973c8686eSJohn Baldwin * Allocate room for the argv[] and env vectors including the 79073c8686eSJohn Baldwin * terminating NULL pointers. 79173c8686eSJohn Baldwin */ 79273c8686eSJohn Baldwin vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 79373c8686eSJohn Baldwin 7944ba25759SEd Maste /* vectp also becomes our initial stack base. */ 79531174518SJohn Baldwin *stack_base = (uintptr_t)vectp; 796ea0fabbcSTim J. Robbins 797610ecfe0SMaxim Sobolev stringp = imgp->args->begin_argv; 798610ecfe0SMaxim Sobolev argc = imgp->args->argc; 799610ecfe0SMaxim Sobolev envc = imgp->args->envc; 80031174518SJohn Baldwin 8014ba25759SEd Maste /* Copy out strings - arguments and environment. */ 80231174518SJohn Baldwin error = copyout(stringp, (void *)ustringp, 80331174518SJohn Baldwin ARG_MAX - imgp->args->stringspace); 80403b0d68cSJohn Baldwin if (error != 0) 80503b0d68cSJohn Baldwin return (error); 806ea0fabbcSTim J. Robbins 8074ba25759SEd Maste /* Fill in "ps_strings" struct for ps, w, etc. */ 80803b0d68cSJohn Baldwin if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 || 80903b0d68cSJohn Baldwin suword32(&arginfo->ps_nargvstr, argc) != 0) 81003b0d68cSJohn Baldwin return (EFAULT); 811ea0fabbcSTim J. Robbins 8124ba25759SEd Maste /* Fill in argument portion of vector table. */ 813ea0fabbcSTim J. Robbins for (; argc > 0; --argc) { 81431174518SJohn Baldwin if (suword32(vectp++, ustringp) != 0) 81503b0d68cSJohn Baldwin return (EFAULT); 816ea0fabbcSTim J. Robbins while (*stringp++ != 0) 81731174518SJohn Baldwin ustringp++; 81831174518SJohn Baldwin ustringp++; 819ea0fabbcSTim J. Robbins } 820ea0fabbcSTim J. Robbins 8214ba25759SEd Maste /* A null vector table pointer separates the argp's from the envp's. */ 82203b0d68cSJohn Baldwin if (suword32(vectp++, 0) != 0) 82303b0d68cSJohn Baldwin return (EFAULT); 824ea0fabbcSTim J. Robbins 82503b0d68cSJohn Baldwin if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 || 82603b0d68cSJohn Baldwin suword32(&arginfo->ps_nenvstr, envc) != 0) 82703b0d68cSJohn Baldwin return (EFAULT); 828ea0fabbcSTim J. Robbins 8294ba25759SEd Maste /* Fill in environment portion of vector table. */ 830ea0fabbcSTim J. Robbins for (; envc > 0; --envc) { 83131174518SJohn Baldwin if (suword32(vectp++, ustringp) != 0) 83203b0d68cSJohn Baldwin return (EFAULT); 833ea0fabbcSTim J. Robbins while (*stringp++ != 0) 83431174518SJohn Baldwin ustringp++; 83531174518SJohn Baldwin ustringp++; 836ea0fabbcSTim J. Robbins } 837ea0fabbcSTim J. Robbins 8384ba25759SEd Maste /* The end of the vector table is a null pointer. */ 83903b0d68cSJohn Baldwin if (suword32(vectp, 0) != 0) 84003b0d68cSJohn Baldwin return (EFAULT); 841ea0fabbcSTim J. Robbins 842d8010b11SJohn Baldwin if (imgp->auxargs) { 843d8010b11SJohn Baldwin vectp++; 844d8010b11SJohn Baldwin error = imgp->sysent->sv_copyout_auxargs(imgp, 845d8010b11SJohn Baldwin (uintptr_t)vectp); 846d8010b11SJohn Baldwin if (error != 0) 847d8010b11SJohn Baldwin return (error); 848d8010b11SJohn Baldwin } 849d8010b11SJohn Baldwin 85003b0d68cSJohn Baldwin return (0); 851ea0fabbcSTim J. Robbins } 852ea0fabbcSTim J. Robbins 8537029da5cSPawel Biernacki static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 854ea0fabbcSTim J. Robbins "32-bit Linux emulation"); 855ea0fabbcSTim J. Robbins 856ea0fabbcSTim J. Robbins static u_long linux32_maxdsiz = LINUX32_MAXDSIZ; 857ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW, 858ea0fabbcSTim J. Robbins &linux32_maxdsiz, 0, ""); 859ea0fabbcSTim J. Robbins static u_long linux32_maxssiz = LINUX32_MAXSSIZ; 860ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW, 861ea0fabbcSTim J. Robbins &linux32_maxssiz, 0, ""); 862ea0fabbcSTim J. Robbins static u_long linux32_maxvmem = LINUX32_MAXVMEM; 863ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW, 864ea0fabbcSTim J. Robbins &linux32_maxvmem, 0, ""); 865ea0fabbcSTim J. Robbins 866ea0fabbcSTim J. Robbins static void 86719059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which) 868ea0fabbcSTim J. Robbins { 869ea0fabbcSTim J. Robbins 87019059a13SJohn Baldwin switch (which) { 87119059a13SJohn Baldwin case RLIMIT_DATA: 872ea0fabbcSTim J. Robbins if (linux32_maxdsiz != 0) { 87319059a13SJohn Baldwin if (rl->rlim_cur > linux32_maxdsiz) 87419059a13SJohn Baldwin rl->rlim_cur = linux32_maxdsiz; 87519059a13SJohn Baldwin if (rl->rlim_max > linux32_maxdsiz) 87619059a13SJohn Baldwin rl->rlim_max = linux32_maxdsiz; 877ea0fabbcSTim J. Robbins } 87819059a13SJohn Baldwin break; 87919059a13SJohn Baldwin case RLIMIT_STACK: 880ea0fabbcSTim J. Robbins if (linux32_maxssiz != 0) { 88119059a13SJohn Baldwin if (rl->rlim_cur > linux32_maxssiz) 88219059a13SJohn Baldwin rl->rlim_cur = linux32_maxssiz; 88319059a13SJohn Baldwin if (rl->rlim_max > linux32_maxssiz) 88419059a13SJohn Baldwin rl->rlim_max = linux32_maxssiz; 885ea0fabbcSTim J. Robbins } 88619059a13SJohn Baldwin break; 88719059a13SJohn Baldwin case RLIMIT_VMEM: 888ea0fabbcSTim J. Robbins if (linux32_maxvmem != 0) { 88919059a13SJohn Baldwin if (rl->rlim_cur > linux32_maxvmem) 89019059a13SJohn Baldwin rl->rlim_cur = linux32_maxvmem; 89119059a13SJohn Baldwin if (rl->rlim_max > linux32_maxvmem) 89219059a13SJohn Baldwin rl->rlim_max = linux32_maxvmem; 893ea0fabbcSTim J. Robbins } 89419059a13SJohn Baldwin break; 89519059a13SJohn Baldwin } 896ea0fabbcSTim J. Robbins } 897ea0fabbcSTim J. Robbins 898ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = { 8992f99bcceSJohn Baldwin .sv_size = LINUX32_SYS_MAXSYSCALL, 9002f99bcceSJohn Baldwin .sv_table = linux32_sysent, 901a8d403e1SKonstantin Belousov .sv_errsize = ELAST + 1, 9021ac2776bSEd Maste .sv_errtbl = linux_errtbl, 903dc858467SEd Maste .sv_transtrap = linux_translate_traps, 904dc858467SEd Maste .sv_fixup = linux_fixup_elf, 905a8d403e1SKonstantin Belousov .sv_sendsig = linux_sendsig, 906bdc37934SDmitry Chagin .sv_sigcode = &_binary_linux32_locore_o_start, 907a8d403e1SKonstantin Belousov .sv_szsigcode = &linux_szsigcode, 908a8d403e1SKonstantin Belousov .sv_name = "Linux ELF32", 909a8d403e1SKonstantin Belousov .sv_coredump = elf32_coredump, 910dc858467SEd Maste .sv_imgact_try = linux_exec_imgact_try, 911a8d403e1SKonstantin Belousov .sv_minsigstksz = LINUX_MINSIGSTKSZ, 912a8d403e1SKonstantin Belousov .sv_minuser = VM_MIN_ADDRESS, 9138f1e49a6SDmitry Chagin .sv_maxuser = LINUX32_MAXUSER, 914a8d403e1SKonstantin Belousov .sv_usrstack = LINUX32_USRSTACK, 915a8d403e1SKonstantin Belousov .sv_psstrings = LINUX32_PS_STRINGS, 916a8d403e1SKonstantin Belousov .sv_stackprot = VM_PROT_ALL, 9175caa67faSJohn Baldwin .sv_copyout_auxargs = linux_copyout_auxargs, 918a8d403e1SKonstantin Belousov .sv_copyout_strings = linux_copyout_strings, 919dc858467SEd Maste .sv_setregs = linux_exec_setregs, 920a8d403e1SKonstantin Belousov .sv_fixlimit = linux32_fixlimit, 921a8d403e1SKonstantin Belousov .sv_maxssiz = &linux32_maxssiz, 9228f1e49a6SDmitry Chagin .sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP, 923*c26391f4SEdward Tomasz Napierala .sv_set_syscall_retval = linux32_set_syscall_retval, 924afe1a688SKonstantin Belousov .sv_fetch_syscall_args = linux32_fetch_syscall_args, 925afe1a688SKonstantin Belousov .sv_syscallnames = NULL, 9268f1e49a6SDmitry Chagin .sv_shared_page_base = LINUX32_SHAREDPAGE, 9278f1e49a6SDmitry Chagin .sv_shared_page_len = PAGE_SIZE, 928e5d81ef1SDmitry Chagin .sv_schedtail = linux_schedtail, 92981338031SDmitry Chagin .sv_thread_detach = linux_thread_detach, 930038c7205SDmitry Chagin .sv_trap = NULL, 931ea0fabbcSTim J. Robbins }; 932bdc37934SDmitry Chagin 933bdc37934SDmitry Chagin static void 934bdc37934SDmitry Chagin linux_vdso_install(void *param) 935bdc37934SDmitry Chagin { 936bdc37934SDmitry Chagin 937bdc37934SDmitry Chagin linux_szsigcode = (&_binary_linux32_locore_o_end - 938bdc37934SDmitry Chagin &_binary_linux32_locore_o_start); 939bdc37934SDmitry Chagin 940bdc37934SDmitry Chagin if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 941bdc37934SDmitry Chagin panic("Linux invalid vdso size\n"); 942bdc37934SDmitry Chagin 943bdc37934SDmitry Chagin __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 944bdc37934SDmitry Chagin 945bdc37934SDmitry Chagin linux_shared_page_obj = __elfN(linux_shared_page_init) 946bdc37934SDmitry Chagin (&linux_shared_page_mapping); 947bdc37934SDmitry Chagin 948c151945cSDmitry Chagin __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 949bdc37934SDmitry Chagin 950bdc37934SDmitry Chagin bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping, 951bdc37934SDmitry Chagin linux_szsigcode); 952bdc37934SDmitry Chagin elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 9530020bdf1SDmitry Chagin 9540020bdf1SDmitry Chagin linux_kplatform = linux_shared_page_mapping + 955c151945cSDmitry Chagin (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base); 956bdc37934SDmitry Chagin } 957bdc37934SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 958f8268d4dSEd Maste linux_vdso_install, NULL); 959bdc37934SDmitry Chagin 960bdc37934SDmitry Chagin static void 961bdc37934SDmitry Chagin linux_vdso_deinstall(void *param) 962bdc37934SDmitry Chagin { 963bdc37934SDmitry Chagin 964bdc37934SDmitry Chagin __elfN(linux_shared_page_fini)(linux_shared_page_obj); 9657b194b3dSEd Maste } 966bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 967f8268d4dSEd Maste linux_vdso_deinstall, NULL); 968ea0fabbcSTim J. Robbins 96989ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU"; 97089ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0; 97189ffc202SBjoern A. Zeeb 972a95659f7SEd Maste static bool 97389ffc202SBjoern A. Zeeb linux32_trans_osrel(const Elf_Note *note, int32_t *osrel) 97489ffc202SBjoern A. Zeeb { 97589ffc202SBjoern A. Zeeb const Elf32_Word *desc; 97689ffc202SBjoern A. Zeeb uintptr_t p; 97789ffc202SBjoern A. Zeeb 97889ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 97989ffc202SBjoern A. Zeeb p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 98089ffc202SBjoern A. Zeeb 98189ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 98289ffc202SBjoern A. Zeeb if (desc[0] != GNULINUX_ABI_DESC) 983a95659f7SEd Maste return (false); 98489ffc202SBjoern A. Zeeb 98589ffc202SBjoern A. Zeeb /* 98635755049SChuck Tuffli * For Linux we encode osrel using the Linux convention of 98735755049SChuck Tuffli * (version << 16) | (major << 8) | (minor) 98835755049SChuck Tuffli * See macro in linux_mib.h 98989ffc202SBjoern A. Zeeb */ 99035755049SChuck Tuffli *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 99189ffc202SBjoern A. Zeeb 992a95659f7SEd Maste return (true); 99389ffc202SBjoern A. Zeeb } 99432c01de2SDmitry Chagin 99532c01de2SDmitry Chagin static Elf_Brandnote linux32_brandnote = { 99689ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 99789ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 99832c01de2SDmitry Chagin .hdr.n_type = 1, 99989ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 100089ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 100189ffc202SBjoern A. Zeeb .trans_osrel = linux32_trans_osrel 100232c01de2SDmitry Chagin }; 100332c01de2SDmitry Chagin 1004ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = { 1005a8d403e1SKonstantin Belousov .brand = ELFOSABI_LINUX, 1006a8d403e1SKonstantin Belousov .machine = EM_386, 1007a8d403e1SKonstantin Belousov .compat_3_brand = "Linux", 1008b5f20658SEdward Tomasz Napierala .emul_path = linux_emul_path, 1009a8d403e1SKonstantin Belousov .interp_path = "/lib/ld-linux.so.1", 1010a8d403e1SKonstantin Belousov .sysvec = &elf_linux_sysvec, 1011a8d403e1SKonstantin Belousov .interp_newpath = NULL, 101232c01de2SDmitry Chagin .brand_note = &linux32_brandnote, 10132dedc128SDmitry Chagin .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1014ea0fabbcSTim J. Robbins }; 1015ea0fabbcSTim J. Robbins 1016ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = { 1017a8d403e1SKonstantin Belousov .brand = ELFOSABI_LINUX, 1018a8d403e1SKonstantin Belousov .machine = EM_386, 1019a8d403e1SKonstantin Belousov .compat_3_brand = "Linux", 1020b5f20658SEdward Tomasz Napierala .emul_path = linux_emul_path, 1021a8d403e1SKonstantin Belousov .interp_path = "/lib/ld-linux.so.2", 1022a8d403e1SKonstantin Belousov .sysvec = &elf_linux_sysvec, 1023a8d403e1SKonstantin Belousov .interp_newpath = NULL, 102432c01de2SDmitry Chagin .brand_note = &linux32_brandnote, 10252dedc128SDmitry Chagin .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1026ea0fabbcSTim J. Robbins }; 1027ea0fabbcSTim J. Robbins 1028a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = { 1029a0c59c7aSDmitry Chagin .brand = ELFOSABI_LINUX, 1030a0c59c7aSDmitry Chagin .machine = EM_386, 1031a0c59c7aSDmitry Chagin .compat_3_brand = "Linux", 1032b5f20658SEdward Tomasz Napierala .emul_path = linux_emul_path, 1033a0c59c7aSDmitry Chagin .interp_path = "/lib/ld-musl-i386.so.1", 1034a0c59c7aSDmitry Chagin .sysvec = &elf_linux_sysvec, 1035a0c59c7aSDmitry Chagin .interp_newpath = NULL, 1036a0c59c7aSDmitry Chagin .brand_note = &linux32_brandnote, 1037a0c59c7aSDmitry Chagin .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1038a0c59c7aSDmitry Chagin }; 1039a0c59c7aSDmitry Chagin 1040ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = { 1041ea0fabbcSTim J. Robbins &linux_brand, 1042ea0fabbcSTim J. Robbins &linux_glibc2brand, 1043a0c59c7aSDmitry Chagin &linux_muslbrand, 1044ea0fabbcSTim J. Robbins NULL 1045ea0fabbcSTim J. Robbins }; 1046ea0fabbcSTim J. Robbins 1047ea0fabbcSTim J. Robbins static int 1048ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data) 1049ea0fabbcSTim J. Robbins { 1050ea0fabbcSTim J. Robbins Elf32_Brandinfo **brandinfo; 1051ea0fabbcSTim J. Robbins int error; 1052ea0fabbcSTim J. Robbins struct linux_ioctl_handler **lihp; 1053ea0fabbcSTim J. Robbins 1054ea0fabbcSTim J. Robbins error = 0; 1055ea0fabbcSTim J. Robbins 1056ea0fabbcSTim J. Robbins switch(type) { 1057ea0fabbcSTim J. Robbins case MOD_LOAD: 1058ea0fabbcSTim J. Robbins for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1059ea0fabbcSTim J. Robbins ++brandinfo) 1060ea0fabbcSTim J. Robbins if (elf32_insert_brand_entry(*brandinfo) < 0) 1061ea0fabbcSTim J. Robbins error = EINVAL; 1062ea0fabbcSTim J. Robbins if (error == 0) { 1063ea0fabbcSTim J. Robbins SET_FOREACH(lihp, linux_ioctl_handler_set) 10648fc08087STijl Coosemans linux32_ioctl_register_handler(*lihp); 10651ca16454SDmitry Chagin stclohz = (stathz ? stathz : hz); 1066ea0fabbcSTim J. Robbins if (bootverbose) 1067ea0fabbcSTim J. Robbins printf("Linux ELF exec handler installed\n"); 1068ea0fabbcSTim J. Robbins } else 1069ea0fabbcSTim J. Robbins printf("cannot insert Linux ELF brand handler\n"); 1070ea0fabbcSTim J. Robbins break; 1071ea0fabbcSTim J. Robbins case MOD_UNLOAD: 1072ea0fabbcSTim J. Robbins for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1073ea0fabbcSTim J. Robbins ++brandinfo) 1074ea0fabbcSTim J. Robbins if (elf32_brand_inuse(*brandinfo)) 1075ea0fabbcSTim J. Robbins error = EBUSY; 1076ea0fabbcSTim J. Robbins if (error == 0) { 1077ea0fabbcSTim J. Robbins for (brandinfo = &linux_brandlist[0]; 1078ea0fabbcSTim J. Robbins *brandinfo != NULL; ++brandinfo) 1079ea0fabbcSTim J. Robbins if (elf32_remove_brand_entry(*brandinfo) < 0) 1080ea0fabbcSTim J. Robbins error = EINVAL; 1081ea0fabbcSTim J. Robbins } 1082ea0fabbcSTim J. Robbins if (error == 0) { 1083ea0fabbcSTim J. Robbins SET_FOREACH(lihp, linux_ioctl_handler_set) 10848fc08087STijl Coosemans linux32_ioctl_unregister_handler(*lihp); 1085ea0fabbcSTim J. Robbins if (bootverbose) 1086ea0fabbcSTim J. Robbins printf("Linux ELF exec handler removed\n"); 1087ea0fabbcSTim J. Robbins } else 1088ea0fabbcSTim J. Robbins printf("Could not deinstall ELF interpreter entry\n"); 1089ea0fabbcSTim J. Robbins break; 1090ea0fabbcSTim J. Robbins default: 1091af682d48SDmitry Chagin return (EOPNOTSUPP); 1092ea0fabbcSTim J. Robbins } 1093af682d48SDmitry Chagin return (error); 1094ea0fabbcSTim J. Robbins } 1095ea0fabbcSTim J. Robbins 1096ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = { 1097ea0fabbcSTim J. Robbins "linuxelf", 1098ea0fabbcSTim J. Robbins linux_elf_modevent, 10999823d527SKevin Lo 0 1100ea0fabbcSTim J. Robbins }; 1101ea0fabbcSTim J. Robbins 110278ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 110367d39748SDmitry Chagin MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1); 1104b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support"); 1105