1ea0fabbcSTim J. Robbins /*- 2ea0fabbcSTim J. Robbins * Copyright (c) 2004 Tim J. Robbins 3ea0fabbcSTim J. Robbins * Copyright (c) 2003 Peter Wemm 4ea0fabbcSTim J. Robbins * Copyright (c) 2002 Doug Rabson 5ea0fabbcSTim J. Robbins * Copyright (c) 1998-1999 Andrew Gallatin 6ea0fabbcSTim J. Robbins * Copyright (c) 1994-1996 S�ren Schmidt 7ea0fabbcSTim J. Robbins * All rights reserved. 8ea0fabbcSTim J. Robbins * 9ea0fabbcSTim J. Robbins * Redistribution and use in source and binary forms, with or without 10ea0fabbcSTim J. Robbins * modification, are permitted provided that the following conditions 11ea0fabbcSTim J. Robbins * are met: 12ea0fabbcSTim J. Robbins * 1. Redistributions of source code must retain the above copyright 13ea0fabbcSTim J. Robbins * notice, this list of conditions and the following disclaimer 14ea0fabbcSTim J. Robbins * in this position and unchanged. 15ea0fabbcSTim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright 16ea0fabbcSTim J. Robbins * notice, this list of conditions and the following disclaimer in the 17ea0fabbcSTim J. Robbins * documentation and/or other materials provided with the distribution. 18ea0fabbcSTim J. Robbins * 3. The name of the author may not be used to endorse or promote products 19ea0fabbcSTim J. Robbins * derived from this software without specific prior written permission 20ea0fabbcSTim J. Robbins * 21ea0fabbcSTim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22ea0fabbcSTim J. Robbins * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23ea0fabbcSTim J. Robbins * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24ea0fabbcSTim J. Robbins * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25ea0fabbcSTim J. Robbins * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26ea0fabbcSTim J. Robbins * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27ea0fabbcSTim J. Robbins * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28ea0fabbcSTim J. Robbins * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29ea0fabbcSTim J. Robbins * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30ea0fabbcSTim J. Robbins * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31ea0fabbcSTim J. Robbins */ 32ea0fabbcSTim J. Robbins 33ea0fabbcSTim J. Robbins #include <sys/cdefs.h> 34ea0fabbcSTim J. Robbins __FBSDID("$FreeBSD$"); 35aefce619SRuslan Ermilov #include "opt_compat.h" 36ea0fabbcSTim J. Robbins 37c680f6b1SDavid E. O'Brien #ifndef COMPAT_IA32 38ce55a234SDavid E. O'Brien #error "Unable to compile Linux-emulator due to missing COMPAT_IA32 option!" 39ea0fabbcSTim J. Robbins #endif 40ea0fabbcSTim J. Robbins 41ea0fabbcSTim J. Robbins #define __ELF_WORD_SIZE 32 42ea0fabbcSTim J. Robbins 43ea0fabbcSTim J. Robbins #include <sys/param.h> 44ea0fabbcSTim J. Robbins #include <sys/systm.h> 45ea0fabbcSTim J. Robbins #include <sys/exec.h> 4648b05c3fSKonstantin Belousov #include <sys/fcntl.h> 47ea0fabbcSTim J. Robbins #include <sys/imgact.h> 48ea0fabbcSTim J. Robbins #include <sys/imgact_elf.h> 49ea0fabbcSTim J. Robbins #include <sys/kernel.h> 50ea0fabbcSTim J. Robbins #include <sys/lock.h> 51ea0fabbcSTim J. Robbins #include <sys/malloc.h> 52ea0fabbcSTim J. Robbins #include <sys/module.h> 53ea0fabbcSTim J. Robbins #include <sys/mutex.h> 54ea0fabbcSTim J. Robbins #include <sys/proc.h> 556004362eSDavid Schultz #include <sys/resourcevar.h> 56ea0fabbcSTim J. Robbins #include <sys/signalvar.h> 57ea0fabbcSTim J. Robbins #include <sys/sysctl.h> 58ea0fabbcSTim J. Robbins #include <sys/syscallsubr.h> 59ea0fabbcSTim J. Robbins #include <sys/sysent.h> 60ea0fabbcSTim J. Robbins #include <sys/sysproto.h> 61ea0fabbcSTim J. Robbins #include <sys/vnode.h> 627c09e6c0SAlexander Leidinger #include <sys/eventhandler.h> 63ea0fabbcSTim J. Robbins 64ea0fabbcSTim J. Robbins #include <vm/vm.h> 65ea0fabbcSTim J. Robbins #include <vm/pmap.h> 66ea0fabbcSTim J. Robbins #include <vm/vm_extern.h> 67ea0fabbcSTim J. Robbins #include <vm/vm_map.h> 68ea0fabbcSTim J. Robbins #include <vm/vm_object.h> 69ea0fabbcSTim J. Robbins #include <vm/vm_page.h> 70ea0fabbcSTim J. Robbins #include <vm/vm_param.h> 71ea0fabbcSTim J. Robbins 72ea0fabbcSTim J. Robbins #include <machine/cpu.h> 73ea0fabbcSTim J. Robbins #include <machine/md_var.h> 746004362eSDavid Schultz #include <machine/pcb.h> 75ea0fabbcSTim J. Robbins #include <machine/specialreg.h> 76ea0fabbcSTim J. Robbins 77ea0fabbcSTim J. Robbins #include <amd64/linux32/linux.h> 78ea0fabbcSTim J. Robbins #include <amd64/linux32/linux32_proto.h> 797c09e6c0SAlexander Leidinger #include <compat/linux/linux_emul.h> 80ea0fabbcSTim J. Robbins #include <compat/linux/linux_mib.h> 81ea0fabbcSTim J. Robbins #include <compat/linux/linux_signal.h> 82ea0fabbcSTim J. Robbins #include <compat/linux/linux_util.h> 83ea0fabbcSTim J. Robbins 84ea0fabbcSTim J. Robbins MODULE_VERSION(linux, 1); 85ea0fabbcSTim J. Robbins 86ea0fabbcSTim J. Robbins MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); 87ea0fabbcSTim J. Robbins 88ea0fabbcSTim J. Robbins #define AUXARGS_ENTRY_32(pos, id, val) \ 89ea0fabbcSTim J. Robbins do { \ 90ea0fabbcSTim J. Robbins suword32(pos++, id); \ 91ea0fabbcSTim J. Robbins suword32(pos++, val); \ 92ea0fabbcSTim J. Robbins } while (0) 93ea0fabbcSTim J. Robbins 94ea0fabbcSTim J. Robbins #if BYTE_ORDER == LITTLE_ENDIAN 95ea0fabbcSTim J. Robbins #define SHELLMAGIC 0x2123 /* #! */ 96ea0fabbcSTim J. Robbins #else 97ea0fabbcSTim J. Robbins #define SHELLMAGIC 0x2321 98ea0fabbcSTim J. Robbins #endif 99ea0fabbcSTim J. Robbins 100ea0fabbcSTim J. Robbins /* 101ea0fabbcSTim J. Robbins * Allow the sendsig functions to use the ldebug() facility 102ea0fabbcSTim J. Robbins * even though they are not syscalls themselves. Map them 103ea0fabbcSTim J. Robbins * to syscall 0. This is slightly less bogus than using 104ea0fabbcSTim J. Robbins * ldebug(sigreturn). 105ea0fabbcSTim J. Robbins */ 106ea0fabbcSTim J. Robbins #define LINUX_SYS_linux_rt_sendsig 0 107ea0fabbcSTim J. Robbins #define LINUX_SYS_linux_sendsig 0 108ea0fabbcSTim J. Robbins 109ea0fabbcSTim J. Robbins extern char linux_sigcode[]; 110ea0fabbcSTim J. Robbins extern int linux_szsigcode; 111ea0fabbcSTim J. Robbins 112ea0fabbcSTim J. Robbins extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 113ea0fabbcSTim J. Robbins 114ea0fabbcSTim J. Robbins SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 115387196bfSDoug Ambrisko SET_DECLARE(linux_device_handler_set, struct linux_device_handler); 116ea0fabbcSTim J. Robbins 117ea0fabbcSTim J. Robbins static int elf_linux_fixup(register_t **stack_base, 118ea0fabbcSTim J. Robbins struct image_params *iparams); 119ea0fabbcSTim J. Robbins static register_t *linux_copyout_strings(struct image_params *imgp); 120ea0fabbcSTim J. Robbins static void linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, 121ea0fabbcSTim J. Robbins caddr_t *params); 1229104847fSDavid Xu static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 123ea0fabbcSTim J. Robbins static void exec_linux_setregs(struct thread *td, u_long entry, 124ea0fabbcSTim J. Robbins u_long stack, u_long ps_strings); 12519059a13SJohn Baldwin static void linux32_fixlimit(struct rlimit *rl, int which); 126ea0fabbcSTim J. Robbins 1277c09e6c0SAlexander Leidinger extern LIST_HEAD(futex_list, futex) futex_list; 128bb59e63fSAlexander Leidinger extern struct sx futex_sx; 1297c09e6c0SAlexander Leidinger 1307c09e6c0SAlexander Leidinger static eventhandler_tag linux_exit_tag; 1317c09e6c0SAlexander Leidinger static eventhandler_tag linux_schedtail_tag; 1327c09e6c0SAlexander Leidinger static eventhandler_tag linux_exec_tag; 1337c09e6c0SAlexander Leidinger 134ea0fabbcSTim J. Robbins /* 135ea0fabbcSTim J. Robbins * Linux syscalls return negative errno's, we do positive and map them 13650e422f0SAlexander Leidinger * Reference: 13750e422f0SAlexander Leidinger * FreeBSD: src/sys/sys/errno.h 13850e422f0SAlexander Leidinger * Linux: linux-2.6.17.8/include/asm-generic/errno-base.h 13950e422f0SAlexander Leidinger * linux-2.6.17.8/include/asm-generic/errno.h 140ea0fabbcSTim J. Robbins */ 141ea0fabbcSTim J. Robbins static int bsd_to_linux_errno[ELAST + 1] = { 142ea0fabbcSTim J. Robbins -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 143ea0fabbcSTim J. Robbins -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, 144ea0fabbcSTim J. Robbins -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 145ea0fabbcSTim J. Robbins -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, 146ea0fabbcSTim J. Robbins -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, 147ea0fabbcSTim J. Robbins -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, 148ea0fabbcSTim J. Robbins -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, 149ea0fabbcSTim J. Robbins -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, 15050e422f0SAlexander Leidinger -6, -6, -43, -42, -75,-125, -84, -95, -16, -74, 15150e422f0SAlexander Leidinger -72, -67, -71 152ea0fabbcSTim J. Robbins }; 153ea0fabbcSTim J. Robbins 154ea0fabbcSTim J. Robbins int bsd_to_linux_signal[LINUX_SIGTBLSZ] = { 155ea0fabbcSTim J. Robbins LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT, LINUX_SIGILL, 156ea0fabbcSTim J. Robbins LINUX_SIGTRAP, LINUX_SIGABRT, 0, LINUX_SIGFPE, 157ea0fabbcSTim J. Robbins LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV, LINUX_SIGSYS, 158ea0fabbcSTim J. Robbins LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM, LINUX_SIGURG, 159ea0fabbcSTim J. Robbins LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT, LINUX_SIGCHLD, 160ea0fabbcSTim J. Robbins LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO, LINUX_SIGXCPU, 161ea0fabbcSTim J. Robbins LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF, LINUX_SIGWINCH, 162ea0fabbcSTim J. Robbins 0, LINUX_SIGUSR1, LINUX_SIGUSR2 163ea0fabbcSTim J. Robbins }; 164ea0fabbcSTim J. Robbins 165ea0fabbcSTim J. Robbins int linux_to_bsd_signal[LINUX_SIGTBLSZ] = { 166ea0fabbcSTim J. Robbins SIGHUP, SIGINT, SIGQUIT, SIGILL, 167ea0fabbcSTim J. Robbins SIGTRAP, SIGABRT, SIGBUS, SIGFPE, 168ea0fabbcSTim J. Robbins SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, 169ea0fabbcSTim J. Robbins SIGPIPE, SIGALRM, SIGTERM, SIGBUS, 170ea0fabbcSTim J. Robbins SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, 171ea0fabbcSTim J. Robbins SIGTTIN, SIGTTOU, SIGURG, SIGXCPU, 172ea0fabbcSTim J. Robbins SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, 173ea0fabbcSTim J. Robbins SIGIO, SIGURG, SIGSYS 174ea0fabbcSTim J. Robbins }; 175ea0fabbcSTim J. Robbins 176ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN 255 177ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = { 178ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 0 */ 179ea0fabbcSTim J. Robbins 6, /* 1 T_PRIVINFLT */ 180ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 2 */ 181ea0fabbcSTim J. Robbins 3, /* 3 T_BPTFLT */ 182ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 4 */ 183ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 5 */ 184ea0fabbcSTim J. Robbins 16, /* 6 T_ARITHTRAP */ 185ea0fabbcSTim J. Robbins 254, /* 7 T_ASTFLT */ 186ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 8 */ 187ea0fabbcSTim J. Robbins 13, /* 9 T_PROTFLT */ 188ea0fabbcSTim J. Robbins 1, /* 10 T_TRCTRAP */ 189ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 11 */ 190ea0fabbcSTim J. Robbins 14, /* 12 T_PAGEFLT */ 191ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 13 */ 192ea0fabbcSTim J. Robbins 17, /* 14 T_ALIGNFLT */ 193ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 15 */ 194ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 16 */ 195ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN, /* 17 */ 196ea0fabbcSTim J. Robbins 0, /* 18 T_DIVIDE */ 197ea0fabbcSTim J. Robbins 2, /* 19 T_NMI */ 198ea0fabbcSTim J. Robbins 4, /* 20 T_OFLOW */ 199ea0fabbcSTim J. Robbins 5, /* 21 T_BOUND */ 200ea0fabbcSTim J. Robbins 7, /* 22 T_DNA */ 201ea0fabbcSTim J. Robbins 8, /* 23 T_DOUBLEFLT */ 202ea0fabbcSTim J. Robbins 9, /* 24 T_FPOPFLT */ 203ea0fabbcSTim J. Robbins 10, /* 25 T_TSSFLT */ 204ea0fabbcSTim J. Robbins 11, /* 26 T_SEGNPFLT */ 205ea0fabbcSTim J. Robbins 12, /* 27 T_STKFLT */ 206ea0fabbcSTim J. Robbins 18, /* 28 T_MCHK */ 207ea0fabbcSTim J. Robbins 19, /* 29 T_XMMFLT */ 208ea0fabbcSTim J. Robbins 15 /* 30 T_RESERVED */ 209ea0fabbcSTim J. Robbins }; 210ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \ 211ea0fabbcSTim J. Robbins ((code)<sizeof(_bsd_to_linux_trapcode)/sizeof(*_bsd_to_linux_trapcode)? \ 212ea0fabbcSTim J. Robbins _bsd_to_linux_trapcode[(code)]: \ 213ea0fabbcSTim J. Robbins LINUX_T_UNKNOWN) 214ea0fabbcSTim J. Robbins 215ea0fabbcSTim J. Robbins struct linux32_ps_strings { 216ea0fabbcSTim J. Robbins u_int32_t ps_argvstr; /* first of 0 or more argument strings */ 217f2c7668eSDavid Schultz u_int ps_nargvstr; /* the number of argument strings */ 218ea0fabbcSTim J. Robbins u_int32_t ps_envstr; /* first of 0 or more environment strings */ 219f2c7668eSDavid Schultz u_int ps_nenvstr; /* the number of environment strings */ 220ea0fabbcSTim J. Robbins }; 221ea0fabbcSTim J. Robbins 222ea0fabbcSTim J. Robbins /* 223ea0fabbcSTim J. Robbins * If FreeBSD & Linux have a difference of opinion about what a trap 224ea0fabbcSTim J. Robbins * means, deal with it here. 225ea0fabbcSTim J. Robbins * 226ea0fabbcSTim J. Robbins * MPSAFE 227ea0fabbcSTim J. Robbins */ 228ea0fabbcSTim J. Robbins static int 229ea0fabbcSTim J. Robbins translate_traps(int signal, int trap_code) 230ea0fabbcSTim J. Robbins { 231ea0fabbcSTim J. Robbins if (signal != SIGBUS) 232ea0fabbcSTim J. Robbins return signal; 233ea0fabbcSTim J. Robbins switch (trap_code) { 234ea0fabbcSTim J. Robbins case T_PROTFLT: 235ea0fabbcSTim J. Robbins case T_TSSFLT: 236ea0fabbcSTim J. Robbins case T_DOUBLEFLT: 237ea0fabbcSTim J. Robbins case T_PAGEFLT: 238ea0fabbcSTim J. Robbins return SIGSEGV; 239ea0fabbcSTim J. Robbins default: 240ea0fabbcSTim J. Robbins return signal; 241ea0fabbcSTim J. Robbins } 242ea0fabbcSTim J. Robbins } 243ea0fabbcSTim J. Robbins 244ea0fabbcSTim J. Robbins static int 245ea0fabbcSTim J. Robbins elf_linux_fixup(register_t **stack_base, struct image_params *imgp) 246ea0fabbcSTim J. Robbins { 247ea0fabbcSTim J. Robbins Elf32_Auxargs *args; 248ea0fabbcSTim J. Robbins Elf32_Addr *base; 249ea0fabbcSTim J. Robbins Elf32_Addr *pos; 250ea0fabbcSTim J. Robbins 2516617724cSJeff Roberson KASSERT(curthread->td_proc == imgp->proc, 252ea0fabbcSTim J. Robbins ("unsafe elf_linux_fixup(), should be curproc")); 253ea0fabbcSTim J. Robbins base = (Elf32_Addr *)*stack_base; 254ea0fabbcSTim J. Robbins args = (Elf32_Auxargs *)imgp->auxargs; 255610ecfe0SMaxim Sobolev pos = base + (imgp->args->argc + imgp->args->envc + 2); 256ea0fabbcSTim J. Robbins 257ea0fabbcSTim J. Robbins if (args->trace) 258ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_DEBUG, 1); 259ea0fabbcSTim J. Robbins if (args->execfd != -1) 260ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd); 261ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr); 262ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent); 263ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum); 264ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz); 265ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags); 266ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry); 267ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_BASE, args->base); 268ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 269ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 270ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 271ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 272ea0fabbcSTim J. Robbins AUXARGS_ENTRY_32(pos, AT_NULL, 0); 273ea0fabbcSTim J. Robbins 274ea0fabbcSTim J. Robbins free(imgp->auxargs, M_TEMP); 275ea0fabbcSTim J. Robbins imgp->auxargs = NULL; 276ea0fabbcSTim J. Robbins 277ea0fabbcSTim J. Robbins base--; 278610ecfe0SMaxim Sobolev suword32(base, (uint32_t)imgp->args->argc); 279ea0fabbcSTim J. Robbins *stack_base = (register_t *)base; 280ea0fabbcSTim J. Robbins return 0; 281ea0fabbcSTim J. Robbins } 282ea0fabbcSTim J. Robbins 283ea0fabbcSTim J. Robbins extern int _ucodesel, _ucode32sel, _udatasel; 284ea0fabbcSTim J. Robbins extern unsigned long linux_sznonrtsigcode; 285ea0fabbcSTim J. Robbins 286ea0fabbcSTim J. Robbins static void 2879104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 288ea0fabbcSTim J. Robbins { 289ea0fabbcSTim J. Robbins struct thread *td = curthread; 290ea0fabbcSTim J. Robbins struct proc *p = td->td_proc; 291ea0fabbcSTim J. Robbins struct sigacts *psp; 292ea0fabbcSTim J. Robbins struct trapframe *regs; 293ea0fabbcSTim J. Robbins struct l_rt_sigframe *fp, frame; 294ea0fabbcSTim J. Robbins int oonstack; 2959104847fSDavid Xu int sig; 2969104847fSDavid Xu int code; 297ea0fabbcSTim J. Robbins 2989104847fSDavid Xu sig = ksi->ksi_signo; 2999104847fSDavid Xu code = ksi->ksi_code; 300ea0fabbcSTim J. Robbins PROC_LOCK_ASSERT(p, MA_OWNED); 301ea0fabbcSTim J. Robbins psp = p->p_sigacts; 302ea0fabbcSTim J. Robbins mtx_assert(&psp->ps_mtx, MA_OWNED); 303ea0fabbcSTim J. Robbins regs = td->td_frame; 304ea0fabbcSTim J. Robbins oonstack = sigonstack(regs->tf_rsp); 305ea0fabbcSTim J. Robbins 306ea0fabbcSTim J. Robbins #ifdef DEBUG 307ea0fabbcSTim J. Robbins if (ldebug(rt_sendsig)) 308728ef954SJohn Baldwin printf(ARGS(rt_sendsig, "%p, %d, %p, %u"), 309ea0fabbcSTim J. Robbins catcher, sig, (void*)mask, code); 310ea0fabbcSTim J. Robbins #endif 311ea0fabbcSTim J. Robbins /* 312ea0fabbcSTim J. Robbins * Allocate space for the signal handler context. 313ea0fabbcSTim J. Robbins */ 314ea0fabbcSTim J. Robbins if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 315ea0fabbcSTim J. Robbins SIGISMEMBER(psp->ps_sigonstack, sig)) { 316ea0fabbcSTim J. Robbins fp = (struct l_rt_sigframe *)(td->td_sigstk.ss_sp + 317ea0fabbcSTim J. Robbins td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 318ea0fabbcSTim J. Robbins } else 319ea0fabbcSTim J. Robbins fp = (struct l_rt_sigframe *)regs->tf_rsp - 1; 320ea0fabbcSTim J. Robbins mtx_unlock(&psp->ps_mtx); 321ea0fabbcSTim J. Robbins 322ea0fabbcSTim J. Robbins /* 323ea0fabbcSTim J. Robbins * Build the argument list for the signal handler. 324ea0fabbcSTim J. Robbins */ 325ea0fabbcSTim J. Robbins if (p->p_sysent->sv_sigtbl) 326ea0fabbcSTim J. Robbins if (sig <= p->p_sysent->sv_sigsize) 327ea0fabbcSTim J. Robbins sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 328ea0fabbcSTim J. Robbins 329ea0fabbcSTim J. Robbins bzero(&frame, sizeof(frame)); 330ea0fabbcSTim J. Robbins 331ea0fabbcSTim J. Robbins frame.sf_handler = PTROUT(catcher); 332ea0fabbcSTim J. Robbins frame.sf_sig = sig; 333ea0fabbcSTim J. Robbins frame.sf_siginfo = PTROUT(&fp->sf_si); 334ea0fabbcSTim J. Robbins frame.sf_ucontext = PTROUT(&fp->sf_sc); 335ea0fabbcSTim J. Robbins 336ea0fabbcSTim J. Robbins /* Fill in POSIX parts */ 337ea0fabbcSTim J. Robbins frame.sf_si.lsi_signo = sig; 338ea0fabbcSTim J. Robbins frame.sf_si.lsi_code = code; 3399104847fSDavid Xu frame.sf_si.lsi_addr = PTROUT(ksi->ksi_addr); 340ea0fabbcSTim J. Robbins 341ea0fabbcSTim J. Robbins /* 342ea0fabbcSTim J. Robbins * Build the signal context to be used by sigreturn. 343ea0fabbcSTim J. Robbins */ 344ea0fabbcSTim J. Robbins frame.sf_sc.uc_flags = 0; /* XXX ??? */ 345ea0fabbcSTim J. Robbins frame.sf_sc.uc_link = 0; /* XXX ??? */ 346ea0fabbcSTim J. Robbins 347ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 348ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size; 349ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 350ea0fabbcSTim J. Robbins ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 351ea0fabbcSTim J. Robbins PROC_UNLOCK(p); 352ea0fabbcSTim J. Robbins 353ea0fabbcSTim J. Robbins bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask); 354ea0fabbcSTim J. Robbins 355ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__bits[0]; 356ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_gs = rgs(); 357ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_fs = rfs(); 358ea0fabbcSTim J. Robbins __asm __volatile("movl %%es,%0" : 359ea0fabbcSTim J. Robbins "=rm" (frame.sf_sc.uc_mcontext.sc_es)); 360ea0fabbcSTim J. Robbins __asm __volatile("movl %%ds,%0" : 361ea0fabbcSTim J. Robbins "=rm" (frame.sf_sc.uc_mcontext.sc_ds)); 362ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_edi = regs->tf_rdi; 363ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_esi = regs->tf_rsi; 364ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_rbp; 365ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_rbx; 366ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_edx = regs->tf_rdx; 367ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_rcx; 368ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_eax = regs->tf_rax; 369ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_eip = regs->tf_rip; 370ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs; 371ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags; 372ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp; 373ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss; 374ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_err = regs->tf_err; 37596a2b635SKonstantin Belousov frame.sf_sc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr; 376ea0fabbcSTim J. Robbins frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 377ea0fabbcSTim J. Robbins 378ea0fabbcSTim J. Robbins #ifdef DEBUG 379ea0fabbcSTim J. Robbins if (ldebug(rt_sendsig)) 380c680f6b1SDavid E. O'Brien printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"), 381ea0fabbcSTim J. Robbins frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp, 382ea0fabbcSTim J. Robbins td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask); 383ea0fabbcSTim J. Robbins #endif 384ea0fabbcSTim J. Robbins 385ea0fabbcSTim J. Robbins if (copyout(&frame, fp, sizeof(frame)) != 0) { 386ea0fabbcSTim J. Robbins /* 387ea0fabbcSTim J. Robbins * Process has trashed its stack; give it an illegal 388ea0fabbcSTim J. Robbins * instruction to halt it in its tracks. 389ea0fabbcSTim J. Robbins */ 390ea0fabbcSTim J. Robbins #ifdef DEBUG 391ea0fabbcSTim J. Robbins if (ldebug(rt_sendsig)) 392ea0fabbcSTim J. Robbins printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"), 393ea0fabbcSTim J. Robbins fp, oonstack); 394ea0fabbcSTim J. Robbins #endif 395ea0fabbcSTim J. Robbins PROC_LOCK(p); 396ea0fabbcSTim J. Robbins sigexit(td, SIGILL); 397ea0fabbcSTim J. Robbins } 398ea0fabbcSTim J. Robbins 399ea0fabbcSTim J. Robbins /* 400ea0fabbcSTim J. Robbins * Build context to run handler in. 401ea0fabbcSTim J. Robbins */ 402ea0fabbcSTim J. Robbins regs->tf_rsp = PTROUT(fp); 403ea0fabbcSTim J. Robbins regs->tf_rip = LINUX32_PS_STRINGS - *(p->p_sysent->sv_szsigcode) + 404ea0fabbcSTim J. Robbins linux_sznonrtsigcode; 40522eca0bfSKonstantin Belousov regs->tf_rflags &= ~(PSL_T | PSL_D); 406ea0fabbcSTim J. Robbins regs->tf_cs = _ucode32sel; 407ea0fabbcSTim J. Robbins regs->tf_ss = _udatasel; 408ea0fabbcSTim J. Robbins load_ds(_udatasel); 409ea0fabbcSTim J. Robbins td->td_pcb->pcb_ds = _udatasel; 410ea0fabbcSTim J. Robbins load_es(_udatasel); 411ea0fabbcSTim J. Robbins td->td_pcb->pcb_es = _udatasel; 4129c5b213eSJung-uk Kim /* leave user %fs and %gs untouched */ 413ea0fabbcSTim J. Robbins PROC_LOCK(p); 414ea0fabbcSTim J. Robbins mtx_lock(&psp->ps_mtx); 415ea0fabbcSTim J. Robbins } 416ea0fabbcSTim J. Robbins 417ea0fabbcSTim J. Robbins 418ea0fabbcSTim J. Robbins /* 419ea0fabbcSTim J. Robbins * Send an interrupt to process. 420ea0fabbcSTim J. Robbins * 421ea0fabbcSTim J. Robbins * Stack is set up to allow sigcode stored 422ea0fabbcSTim J. Robbins * in u. to call routine, followed by kcall 423ea0fabbcSTim J. Robbins * to sigreturn routine below. After sigreturn 424ea0fabbcSTim J. Robbins * resets the signal mask, the stack, and the 425ea0fabbcSTim J. Robbins * frame pointer, it returns to the user 426ea0fabbcSTim J. Robbins * specified pc, psl. 427ea0fabbcSTim J. Robbins */ 428ea0fabbcSTim J. Robbins static void 4299104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 430ea0fabbcSTim J. Robbins { 431ea0fabbcSTim J. Robbins struct thread *td = curthread; 432ea0fabbcSTim J. Robbins struct proc *p = td->td_proc; 433ea0fabbcSTim J. Robbins struct sigacts *psp; 434ea0fabbcSTim J. Robbins struct trapframe *regs; 435ea0fabbcSTim J. Robbins struct l_sigframe *fp, frame; 436ea0fabbcSTim J. Robbins l_sigset_t lmask; 437ea0fabbcSTim J. Robbins int oonstack, i; 4389104847fSDavid Xu int sig, code; 439ea0fabbcSTim J. Robbins 4409104847fSDavid Xu sig = ksi->ksi_signo; 4419104847fSDavid Xu code = ksi->ksi_code; 442ea0fabbcSTim J. Robbins PROC_LOCK_ASSERT(p, MA_OWNED); 443ea0fabbcSTim J. Robbins psp = p->p_sigacts; 444ea0fabbcSTim J. Robbins mtx_assert(&psp->ps_mtx, MA_OWNED); 445ea0fabbcSTim J. Robbins if (SIGISMEMBER(psp->ps_siginfo, sig)) { 446ea0fabbcSTim J. Robbins /* Signal handler installed with SA_SIGINFO. */ 4479104847fSDavid Xu linux_rt_sendsig(catcher, ksi, mask); 448ea0fabbcSTim J. Robbins return; 449ea0fabbcSTim J. Robbins } 450ea0fabbcSTim J. Robbins 451ea0fabbcSTim J. Robbins regs = td->td_frame; 452ea0fabbcSTim J. Robbins oonstack = sigonstack(regs->tf_rsp); 453ea0fabbcSTim J. Robbins 454ea0fabbcSTim J. Robbins #ifdef DEBUG 455ea0fabbcSTim J. Robbins if (ldebug(sendsig)) 456728ef954SJohn Baldwin printf(ARGS(sendsig, "%p, %d, %p, %u"), 457ea0fabbcSTim J. Robbins catcher, sig, (void*)mask, code); 458ea0fabbcSTim J. Robbins #endif 459ea0fabbcSTim J. Robbins 460ea0fabbcSTim J. Robbins /* 461ea0fabbcSTim J. Robbins * Allocate space for the signal handler context. 462ea0fabbcSTim J. Robbins */ 463ea0fabbcSTim J. Robbins if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 464ea0fabbcSTim J. Robbins SIGISMEMBER(psp->ps_sigonstack, sig)) { 465ea0fabbcSTim J. Robbins fp = (struct l_sigframe *)(td->td_sigstk.ss_sp + 466ea0fabbcSTim J. Robbins td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 467ea0fabbcSTim J. Robbins } else 468ea0fabbcSTim J. Robbins fp = (struct l_sigframe *)regs->tf_rsp - 1; 469ea0fabbcSTim J. Robbins mtx_unlock(&psp->ps_mtx); 470ea0fabbcSTim J. Robbins PROC_UNLOCK(p); 471ea0fabbcSTim J. Robbins 472ea0fabbcSTim J. Robbins /* 473ea0fabbcSTim J. Robbins * Build the argument list for the signal handler. 474ea0fabbcSTim J. Robbins */ 475ea0fabbcSTim J. Robbins if (p->p_sysent->sv_sigtbl) 476ea0fabbcSTim J. Robbins if (sig <= p->p_sysent->sv_sigsize) 477ea0fabbcSTim J. Robbins sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 478ea0fabbcSTim J. Robbins 479ea0fabbcSTim J. Robbins bzero(&frame, sizeof(frame)); 480ea0fabbcSTim J. Robbins 481ea0fabbcSTim J. Robbins frame.sf_handler = PTROUT(catcher); 482ea0fabbcSTim J. Robbins frame.sf_sig = sig; 483ea0fabbcSTim J. Robbins 484ea0fabbcSTim J. Robbins bsd_to_linux_sigset(mask, &lmask); 485ea0fabbcSTim J. Robbins 486ea0fabbcSTim J. Robbins /* 487ea0fabbcSTim J. Robbins * Build the signal context to be used by sigreturn. 488ea0fabbcSTim J. Robbins */ 489ea0fabbcSTim J. Robbins frame.sf_sc.sc_mask = lmask.__bits[0]; 490ea0fabbcSTim J. Robbins frame.sf_sc.sc_gs = rgs(); 491ea0fabbcSTim J. Robbins frame.sf_sc.sc_fs = rfs(); 492ea0fabbcSTim J. Robbins __asm __volatile("movl %%es,%0" : "=rm" (frame.sf_sc.sc_es)); 493ea0fabbcSTim J. Robbins __asm __volatile("movl %%ds,%0" : "=rm" (frame.sf_sc.sc_ds)); 494ea0fabbcSTim J. Robbins frame.sf_sc.sc_edi = regs->tf_rdi; 495ea0fabbcSTim J. Robbins frame.sf_sc.sc_esi = regs->tf_rsi; 496ea0fabbcSTim J. Robbins frame.sf_sc.sc_ebp = regs->tf_rbp; 497ea0fabbcSTim J. Robbins frame.sf_sc.sc_ebx = regs->tf_rbx; 498ea0fabbcSTim J. Robbins frame.sf_sc.sc_edx = regs->tf_rdx; 499ea0fabbcSTim J. Robbins frame.sf_sc.sc_ecx = regs->tf_rcx; 500ea0fabbcSTim J. Robbins frame.sf_sc.sc_eax = regs->tf_rax; 501ea0fabbcSTim J. Robbins frame.sf_sc.sc_eip = regs->tf_rip; 502ea0fabbcSTim J. Robbins frame.sf_sc.sc_cs = regs->tf_cs; 503ea0fabbcSTim J. Robbins frame.sf_sc.sc_eflags = regs->tf_rflags; 504ea0fabbcSTim J. Robbins frame.sf_sc.sc_esp_at_signal = regs->tf_rsp; 505ea0fabbcSTim J. Robbins frame.sf_sc.sc_ss = regs->tf_ss; 506ea0fabbcSTim J. Robbins frame.sf_sc.sc_err = regs->tf_err; 50796a2b635SKonstantin Belousov frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr; 508ea0fabbcSTim J. Robbins frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code); 509ea0fabbcSTim J. Robbins 510ea0fabbcSTim J. Robbins for (i = 0; i < (LINUX_NSIG_WORDS-1); i++) 511ea0fabbcSTim J. Robbins frame.sf_extramask[i] = lmask.__bits[i+1]; 512ea0fabbcSTim J. Robbins 513ea0fabbcSTim J. Robbins if (copyout(&frame, fp, sizeof(frame)) != 0) { 514ea0fabbcSTim J. Robbins /* 515ea0fabbcSTim J. Robbins * Process has trashed its stack; give it an illegal 516ea0fabbcSTim J. Robbins * instruction to halt it in its tracks. 517ea0fabbcSTim J. Robbins */ 518ea0fabbcSTim J. Robbins PROC_LOCK(p); 519ea0fabbcSTim J. Robbins sigexit(td, SIGILL); 520ea0fabbcSTim J. Robbins } 521ea0fabbcSTim J. Robbins 522ea0fabbcSTim J. Robbins /* 523ea0fabbcSTim J. Robbins * Build context to run handler in. 524ea0fabbcSTim J. Robbins */ 525ea0fabbcSTim J. Robbins regs->tf_rsp = PTROUT(fp); 526ea0fabbcSTim J. Robbins regs->tf_rip = LINUX32_PS_STRINGS - *(p->p_sysent->sv_szsigcode); 52722eca0bfSKonstantin Belousov regs->tf_rflags &= ~(PSL_T | PSL_D); 528ea0fabbcSTim J. Robbins regs->tf_cs = _ucode32sel; 529ea0fabbcSTim J. Robbins regs->tf_ss = _udatasel; 530ea0fabbcSTim J. Robbins load_ds(_udatasel); 531ea0fabbcSTim J. Robbins td->td_pcb->pcb_ds = _udatasel; 532ea0fabbcSTim J. Robbins load_es(_udatasel); 533ea0fabbcSTim J. Robbins td->td_pcb->pcb_es = _udatasel; 5349c5b213eSJung-uk Kim /* leave user %fs and %gs untouched */ 535ea0fabbcSTim J. Robbins PROC_LOCK(p); 536ea0fabbcSTim J. Robbins mtx_lock(&psp->ps_mtx); 537ea0fabbcSTim J. Robbins } 538ea0fabbcSTim J. Robbins 539ea0fabbcSTim J. Robbins /* 540ea0fabbcSTim J. Robbins * System call to cleanup state after a signal 541ea0fabbcSTim J. Robbins * has been taken. Reset signal mask and 542ea0fabbcSTim J. Robbins * stack state from context left by sendsig (above). 543ea0fabbcSTim J. Robbins * Return to previous pc and psl as specified by 544ea0fabbcSTim J. Robbins * context left by sendsig. Check carefully to 545ea0fabbcSTim J. Robbins * make sure that the user has not modified the 546ea0fabbcSTim J. Robbins * psl to gain improper privileges or to cause 547ea0fabbcSTim J. Robbins * a machine fault. 548ea0fabbcSTim J. Robbins */ 549ea0fabbcSTim J. Robbins int 550ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 551ea0fabbcSTim J. Robbins { 552ea0fabbcSTim J. Robbins struct proc *p = td->td_proc; 553ea0fabbcSTim J. Robbins struct l_sigframe frame; 554ea0fabbcSTim J. Robbins struct trapframe *regs; 555ea0fabbcSTim J. Robbins l_sigset_t lmask; 556ea0fabbcSTim J. Robbins int eflags, i; 5579104847fSDavid Xu ksiginfo_t ksi; 558ea0fabbcSTim J. Robbins 559ea0fabbcSTim J. Robbins regs = td->td_frame; 560ea0fabbcSTim J. Robbins 561ea0fabbcSTim J. Robbins #ifdef DEBUG 562ea0fabbcSTim J. Robbins if (ldebug(sigreturn)) 563ea0fabbcSTim J. Robbins printf(ARGS(sigreturn, "%p"), (void *)args->sfp); 564ea0fabbcSTim J. Robbins #endif 565ea0fabbcSTim J. Robbins /* 566ea0fabbcSTim J. Robbins * The trampoline code hands us the sigframe. 567ea0fabbcSTim J. Robbins * It is unsafe to keep track of it ourselves, in the event that a 568ea0fabbcSTim J. Robbins * program jumps out of a signal handler. 569ea0fabbcSTim J. Robbins */ 570ea0fabbcSTim J. Robbins if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 571ea0fabbcSTim J. Robbins return (EFAULT); 572ea0fabbcSTim J. Robbins 573ea0fabbcSTim J. Robbins /* 574ea0fabbcSTim J. Robbins * Check for security violations. 575ea0fabbcSTim J. Robbins */ 576ea0fabbcSTim J. Robbins #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 577ea0fabbcSTim J. Robbins eflags = frame.sf_sc.sc_eflags; 578ea0fabbcSTim J. Robbins /* 579ea0fabbcSTim J. Robbins * XXX do allow users to change the privileged flag PSL_RF. The 580ea0fabbcSTim J. Robbins * cpu sets PSL_RF in tf_eflags for faults. Debuggers should 581ea0fabbcSTim J. Robbins * sometimes set it there too. tf_eflags is kept in the signal 582ea0fabbcSTim J. Robbins * context during signal handling and there is no other place 583ea0fabbcSTim J. Robbins * to remember it, so the PSL_RF bit may be corrupted by the 584ea0fabbcSTim J. Robbins * signal handler without us knowing. Corruption of the PSL_RF 585ea0fabbcSTim J. Robbins * bit at worst causes one more or one less debugger trap, so 586ea0fabbcSTim J. Robbins * allowing it is fairly harmless. 587ea0fabbcSTim J. Robbins */ 588ea0fabbcSTim J. Robbins if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) 589ea0fabbcSTim J. Robbins return(EINVAL); 590ea0fabbcSTim J. Robbins 591ea0fabbcSTim J. Robbins /* 592ea0fabbcSTim J. Robbins * Don't allow users to load a valid privileged %cs. Let the 593ea0fabbcSTim J. Robbins * hardware check for invalid selectors, excess privilege in 594ea0fabbcSTim J. Robbins * other selectors, invalid %eip's and invalid %esp's. 595ea0fabbcSTim J. Robbins */ 596ea0fabbcSTim J. Robbins #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 597ea0fabbcSTim J. Robbins if (!CS_SECURE(frame.sf_sc.sc_cs)) { 5989104847fSDavid Xu ksiginfo_init_trap(&ksi); 5999104847fSDavid Xu ksi.ksi_signo = SIGBUS; 6009104847fSDavid Xu ksi.ksi_code = BUS_OBJERR; 6019104847fSDavid Xu ksi.ksi_trapno = T_PROTFLT; 6029104847fSDavid Xu ksi.ksi_addr = (void *)regs->tf_rip; 6039104847fSDavid Xu trapsignal(td, &ksi); 604ea0fabbcSTim J. Robbins return(EINVAL); 605ea0fabbcSTim J. Robbins } 606ea0fabbcSTim J. Robbins 607ea0fabbcSTim J. Robbins lmask.__bits[0] = frame.sf_sc.sc_mask; 608ea0fabbcSTim J. Robbins for (i = 0; i < (LINUX_NSIG_WORDS-1); i++) 609ea0fabbcSTim J. Robbins lmask.__bits[i+1] = frame.sf_extramask[i]; 610ea0fabbcSTim J. Robbins PROC_LOCK(p); 611ea0fabbcSTim J. Robbins linux_to_bsd_sigset(&lmask, &td->td_sigmask); 612ea0fabbcSTim J. Robbins SIG_CANTMASK(td->td_sigmask); 613ea0fabbcSTim J. Robbins signotify(td); 614ea0fabbcSTim J. Robbins PROC_UNLOCK(p); 615ea0fabbcSTim J. Robbins 616ea0fabbcSTim J. Robbins /* 617ea0fabbcSTim J. Robbins * Restore signal context. 618ea0fabbcSTim J. Robbins */ 619ea0fabbcSTim J. Robbins /* Selectors were restored by the trampoline. */ 620ea0fabbcSTim J. Robbins regs->tf_rdi = frame.sf_sc.sc_edi; 621ea0fabbcSTim J. Robbins regs->tf_rsi = frame.sf_sc.sc_esi; 622ea0fabbcSTim J. Robbins regs->tf_rbp = frame.sf_sc.sc_ebp; 623ea0fabbcSTim J. Robbins regs->tf_rbx = frame.sf_sc.sc_ebx; 624ea0fabbcSTim J. Robbins regs->tf_rdx = frame.sf_sc.sc_edx; 625ea0fabbcSTim J. Robbins regs->tf_rcx = frame.sf_sc.sc_ecx; 626ea0fabbcSTim J. Robbins regs->tf_rax = frame.sf_sc.sc_eax; 627ea0fabbcSTim J. Robbins regs->tf_rip = frame.sf_sc.sc_eip; 628ea0fabbcSTim J. Robbins regs->tf_cs = frame.sf_sc.sc_cs; 629ea0fabbcSTim J. Robbins regs->tf_rflags = eflags; 630ea0fabbcSTim J. Robbins regs->tf_rsp = frame.sf_sc.sc_esp_at_signal; 631ea0fabbcSTim J. Robbins regs->tf_ss = frame.sf_sc.sc_ss; 632ea0fabbcSTim J. Robbins 633ea0fabbcSTim J. Robbins return (EJUSTRETURN); 634ea0fabbcSTim J. Robbins } 635ea0fabbcSTim J. Robbins 636ea0fabbcSTim J. Robbins /* 637ea0fabbcSTim J. Robbins * System call to cleanup state after a signal 638ea0fabbcSTim J. Robbins * has been taken. Reset signal mask and 639ea0fabbcSTim J. Robbins * stack state from context left by rt_sendsig (above). 640ea0fabbcSTim J. Robbins * Return to previous pc and psl as specified by 641ea0fabbcSTim J. Robbins * context left by sendsig. Check carefully to 642ea0fabbcSTim J. Robbins * make sure that the user has not modified the 643ea0fabbcSTim J. Robbins * psl to gain improper privileges or to cause 644ea0fabbcSTim J. Robbins * a machine fault. 645ea0fabbcSTim J. Robbins */ 646ea0fabbcSTim J. Robbins int 647ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 648ea0fabbcSTim J. Robbins { 649ea0fabbcSTim J. Robbins struct proc *p = td->td_proc; 650ea0fabbcSTim J. Robbins struct l_ucontext uc; 651ea0fabbcSTim J. Robbins struct l_sigcontext *context; 652ea0fabbcSTim J. Robbins l_stack_t *lss; 653ea0fabbcSTim J. Robbins stack_t ss; 654ea0fabbcSTim J. Robbins struct trapframe *regs; 655ea0fabbcSTim J. Robbins int eflags; 6569104847fSDavid Xu ksiginfo_t ksi; 657ea0fabbcSTim J. Robbins 658ea0fabbcSTim J. Robbins regs = td->td_frame; 659ea0fabbcSTim J. Robbins 660ea0fabbcSTim J. Robbins #ifdef DEBUG 661ea0fabbcSTim J. Robbins if (ldebug(rt_sigreturn)) 662ea0fabbcSTim J. Robbins printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp); 663ea0fabbcSTim J. Robbins #endif 664ea0fabbcSTim J. Robbins /* 665ea0fabbcSTim J. Robbins * The trampoline code hands us the ucontext. 666ea0fabbcSTim J. Robbins * It is unsafe to keep track of it ourselves, in the event that a 667ea0fabbcSTim J. Robbins * program jumps out of a signal handler. 668ea0fabbcSTim J. Robbins */ 669ea0fabbcSTim J. Robbins if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 670ea0fabbcSTim J. Robbins return (EFAULT); 671ea0fabbcSTim J. Robbins 672ea0fabbcSTim J. Robbins context = &uc.uc_mcontext; 673ea0fabbcSTim J. Robbins 674ea0fabbcSTim J. Robbins /* 675ea0fabbcSTim J. Robbins * Check for security violations. 676ea0fabbcSTim J. Robbins */ 677ea0fabbcSTim J. Robbins #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 678ea0fabbcSTim J. Robbins eflags = context->sc_eflags; 679ea0fabbcSTim J. Robbins /* 680ea0fabbcSTim J. Robbins * XXX do allow users to change the privileged flag PSL_RF. The 681ea0fabbcSTim J. Robbins * cpu sets PSL_RF in tf_eflags for faults. Debuggers should 682ea0fabbcSTim J. Robbins * sometimes set it there too. tf_eflags is kept in the signal 683ea0fabbcSTim J. Robbins * context during signal handling and there is no other place 684ea0fabbcSTim J. Robbins * to remember it, so the PSL_RF bit may be corrupted by the 685ea0fabbcSTim J. Robbins * signal handler without us knowing. Corruption of the PSL_RF 686ea0fabbcSTim J. Robbins * bit at worst causes one more or one less debugger trap, so 687ea0fabbcSTim J. Robbins * allowing it is fairly harmless. 688ea0fabbcSTim J. Robbins */ 689ea0fabbcSTim J. Robbins if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) 690ea0fabbcSTim J. Robbins return(EINVAL); 691ea0fabbcSTim J. Robbins 692ea0fabbcSTim J. Robbins /* 693ea0fabbcSTim J. Robbins * Don't allow users to load a valid privileged %cs. Let the 694ea0fabbcSTim J. Robbins * hardware check for invalid selectors, excess privilege in 695ea0fabbcSTim J. Robbins * other selectors, invalid %eip's and invalid %esp's. 696ea0fabbcSTim J. Robbins */ 697ea0fabbcSTim J. Robbins #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 698ea0fabbcSTim J. Robbins if (!CS_SECURE(context->sc_cs)) { 6999104847fSDavid Xu ksiginfo_init_trap(&ksi); 7009104847fSDavid Xu ksi.ksi_signo = SIGBUS; 7019104847fSDavid Xu ksi.ksi_code = BUS_OBJERR; 7029104847fSDavid Xu ksi.ksi_trapno = T_PROTFLT; 7039104847fSDavid Xu ksi.ksi_addr = (void *)regs->tf_rip; 7049104847fSDavid Xu trapsignal(td, &ksi); 705ea0fabbcSTim J. Robbins return(EINVAL); 706ea0fabbcSTim J. Robbins } 707ea0fabbcSTim J. Robbins 708ea0fabbcSTim J. Robbins PROC_LOCK(p); 709ea0fabbcSTim J. Robbins linux_to_bsd_sigset(&uc.uc_sigmask, &td->td_sigmask); 710ea0fabbcSTim J. Robbins SIG_CANTMASK(td->td_sigmask); 711ea0fabbcSTim J. Robbins signotify(td); 712ea0fabbcSTim J. Robbins PROC_UNLOCK(p); 713ea0fabbcSTim J. Robbins 714ea0fabbcSTim J. Robbins /* 715ea0fabbcSTim J. Robbins * Restore signal context 716ea0fabbcSTim J. Robbins */ 717ea0fabbcSTim J. Robbins /* Selectors were restored by the trampoline. */ 718ea0fabbcSTim J. Robbins regs->tf_rdi = context->sc_edi; 719ea0fabbcSTim J. Robbins regs->tf_rsi = context->sc_esi; 720ea0fabbcSTim J. Robbins regs->tf_rbp = context->sc_ebp; 721ea0fabbcSTim J. Robbins regs->tf_rbx = context->sc_ebx; 722ea0fabbcSTim J. Robbins regs->tf_rdx = context->sc_edx; 723ea0fabbcSTim J. Robbins regs->tf_rcx = context->sc_ecx; 724ea0fabbcSTim J. Robbins regs->tf_rax = context->sc_eax; 725ea0fabbcSTim J. Robbins regs->tf_rip = context->sc_eip; 726ea0fabbcSTim J. Robbins regs->tf_cs = context->sc_cs; 727ea0fabbcSTim J. Robbins regs->tf_rflags = eflags; 728ea0fabbcSTim J. Robbins regs->tf_rsp = context->sc_esp_at_signal; 729ea0fabbcSTim J. Robbins regs->tf_ss = context->sc_ss; 730ea0fabbcSTim J. Robbins 731ea0fabbcSTim J. Robbins /* 732ea0fabbcSTim J. Robbins * call sigaltstack & ignore results.. 733ea0fabbcSTim J. Robbins */ 734ea0fabbcSTim J. Robbins lss = &uc.uc_stack; 735ea0fabbcSTim J. Robbins ss.ss_sp = PTRIN(lss->ss_sp); 736ea0fabbcSTim J. Robbins ss.ss_size = lss->ss_size; 737ea0fabbcSTim J. Robbins ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 738ea0fabbcSTim J. Robbins 739ea0fabbcSTim J. Robbins #ifdef DEBUG 740ea0fabbcSTim J. Robbins if (ldebug(rt_sigreturn)) 741c680f6b1SDavid E. O'Brien printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"), 742ea0fabbcSTim J. Robbins ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask); 743ea0fabbcSTim J. Robbins #endif 744ea0fabbcSTim J. Robbins (void)kern_sigaltstack(td, &ss, NULL); 745ea0fabbcSTim J. Robbins 746ea0fabbcSTim J. Robbins return (EJUSTRETURN); 747ea0fabbcSTim J. Robbins } 748ea0fabbcSTim J. Robbins 749ea0fabbcSTim J. Robbins /* 750ea0fabbcSTim J. Robbins * MPSAFE 751ea0fabbcSTim J. Robbins */ 752ea0fabbcSTim J. Robbins static void 753ea0fabbcSTim J. Robbins linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params) 754ea0fabbcSTim J. Robbins { 755ea0fabbcSTim J. Robbins args[0] = tf->tf_rbx; 756ea0fabbcSTim J. Robbins args[1] = tf->tf_rcx; 757ea0fabbcSTim J. Robbins args[2] = tf->tf_rdx; 758ea0fabbcSTim J. Robbins args[3] = tf->tf_rsi; 759ea0fabbcSTim J. Robbins args[4] = tf->tf_rdi; 760ea0fabbcSTim J. Robbins args[5] = tf->tf_rbp; /* Unconfirmed */ 761ea0fabbcSTim J. Robbins *params = NULL; /* no copyin */ 762ea0fabbcSTim J. Robbins } 763ea0fabbcSTim J. Robbins 764ea0fabbcSTim J. Robbins /* 765ea0fabbcSTim J. Robbins * If a linux binary is exec'ing something, try this image activator 766ea0fabbcSTim J. Robbins * first. We override standard shell script execution in order to 767ea0fabbcSTim J. Robbins * be able to modify the interpreter path. We only do this if a linux 768ea0fabbcSTim J. Robbins * binary is doing the exec, so we do not create an EXEC module for it. 769ea0fabbcSTim J. Robbins */ 770ea0fabbcSTim J. Robbins static int exec_linux_imgact_try(struct image_params *iparams); 771ea0fabbcSTim J. Robbins 772ea0fabbcSTim J. Robbins static int 773ea0fabbcSTim J. Robbins exec_linux_imgact_try(struct image_params *imgp) 774ea0fabbcSTim J. Robbins { 775ea0fabbcSTim J. Robbins const char *head = (const char *)imgp->image_header; 7761d15fdd9SJohn Baldwin char *rpath; 7771d15fdd9SJohn Baldwin int error = -1, len; 778ea0fabbcSTim J. Robbins 779ea0fabbcSTim J. Robbins /* 780ea0fabbcSTim J. Robbins * The interpreter for shell scripts run from a linux binary needs 781ea0fabbcSTim J. Robbins * to be located in /compat/linux if possible in order to recursively 782ea0fabbcSTim J. Robbins * maintain linux path emulation. 783ea0fabbcSTim J. Robbins */ 784ea0fabbcSTim J. Robbins if (((const short *)head)[0] == SHELLMAGIC) { 785ea0fabbcSTim J. Robbins /* 786ea0fabbcSTim J. Robbins * Run our normal shell image activator. If it succeeds attempt 787ea0fabbcSTim J. Robbins * to use the alternate path for the interpreter. If an alternate 788ea0fabbcSTim J. Robbins * path is found, use our stringspace to store it. 789ea0fabbcSTim J. Robbins */ 790ea0fabbcSTim J. Robbins if ((error = exec_shell_imgact(imgp)) == 0) { 7911d15fdd9SJohn Baldwin linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc), 79248b05c3fSKonstantin Belousov imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0, AT_FDCWD); 7931d15fdd9SJohn Baldwin if (rpath != NULL) { 7941d15fdd9SJohn Baldwin len = strlen(rpath) + 1; 795ea0fabbcSTim J. Robbins 796ea0fabbcSTim J. Robbins if (len <= MAXSHELLCMDLEN) { 797ea0fabbcSTim J. Robbins memcpy(imgp->interpreter_name, rpath, len); 798ea0fabbcSTim J. Robbins } 799ea0fabbcSTim J. Robbins free(rpath, M_TEMP); 800ea0fabbcSTim J. Robbins } 801ea0fabbcSTim J. Robbins } 802ea0fabbcSTim J. Robbins } 803ea0fabbcSTim J. Robbins return(error); 804ea0fabbcSTim J. Robbins } 805ea0fabbcSTim J. Robbins 806ea0fabbcSTim J. Robbins /* 807ea0fabbcSTim J. Robbins * Clear registers on exec 808ea0fabbcSTim J. Robbins * XXX copied from ia32_signal.c. 809ea0fabbcSTim J. Robbins */ 810ea0fabbcSTim J. Robbins static void 811ea0fabbcSTim J. Robbins exec_linux_setregs(td, entry, stack, ps_strings) 812ea0fabbcSTim J. Robbins struct thread *td; 813ea0fabbcSTim J. Robbins u_long entry; 814ea0fabbcSTim J. Robbins u_long stack; 815ea0fabbcSTim J. Robbins u_long ps_strings; 816ea0fabbcSTim J. Robbins { 817ea0fabbcSTim J. Robbins struct trapframe *regs = td->td_frame; 818ea0fabbcSTim J. Robbins struct pcb *pcb = td->td_pcb; 819ea0fabbcSTim J. Robbins 8209c5b213eSJung-uk Kim critical_enter(); 821ea0fabbcSTim J. Robbins wrmsr(MSR_FSBASE, 0); 822ea0fabbcSTim J. Robbins wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 823ea0fabbcSTim J. Robbins pcb->pcb_fsbase = 0; 824ea0fabbcSTim J. Robbins pcb->pcb_gsbase = 0; 8259c5b213eSJung-uk Kim critical_exit(); 826ea0fabbcSTim J. Robbins load_ds(_udatasel); 827ea0fabbcSTim J. Robbins load_es(_udatasel); 828ea0fabbcSTim J. Robbins load_fs(_udatasel); 8299c5b213eSJung-uk Kim load_gs(_udatasel); 830ea0fabbcSTim J. Robbins pcb->pcb_ds = _udatasel; 831ea0fabbcSTim J. Robbins pcb->pcb_es = _udatasel; 832ea0fabbcSTim J. Robbins pcb->pcb_fs = _udatasel; 8339c5b213eSJung-uk Kim pcb->pcb_gs = _udatasel; 834ea0fabbcSTim J. Robbins 835ea0fabbcSTim J. Robbins bzero((char *)regs, sizeof(struct trapframe)); 836ea0fabbcSTim J. Robbins regs->tf_rip = entry; 837ea0fabbcSTim J. Robbins regs->tf_rsp = stack; 838ea0fabbcSTim J. Robbins regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T); 839ea0fabbcSTim J. Robbins regs->tf_ss = _udatasel; 840ea0fabbcSTim J. Robbins regs->tf_cs = _ucode32sel; 841ea0fabbcSTim J. Robbins regs->tf_rbx = ps_strings; 842ea0fabbcSTim J. Robbins load_cr0(rcr0() | CR0_MP | CR0_TS); 8432a988f7cSStephan Uphoff fpstate_drop(td); 844ea0fabbcSTim J. Robbins 845ea0fabbcSTim J. Robbins /* Return via doreti so that we can change to a different %cs */ 846ea0fabbcSTim J. Robbins pcb->pcb_flags |= PCB_FULLCTX; 847ea0fabbcSTim J. Robbins td->td_retval[1] = 0; 848ea0fabbcSTim J. Robbins } 849ea0fabbcSTim J. Robbins 850ea0fabbcSTim J. Robbins /* 851ea0fabbcSTim J. Robbins * XXX copied from ia32_sysvec.c. 852ea0fabbcSTim J. Robbins */ 853ea0fabbcSTim J. Robbins static register_t * 854ea0fabbcSTim J. Robbins linux_copyout_strings(struct image_params *imgp) 855ea0fabbcSTim J. Robbins { 856ea0fabbcSTim J. Robbins int argc, envc; 857ea0fabbcSTim J. Robbins u_int32_t *vectp; 858ea0fabbcSTim J. Robbins char *stringp, *destp; 859ea0fabbcSTim J. Robbins u_int32_t *stack_base; 860ea0fabbcSTim J. Robbins struct linux32_ps_strings *arginfo; 861ea0fabbcSTim J. Robbins int sigcodesz; 862ea0fabbcSTim J. Robbins 863ea0fabbcSTim J. Robbins /* 864ea0fabbcSTim J. Robbins * Calculate string base and vector table pointers. 865ea0fabbcSTim J. Robbins * Also deal with signal trampoline code for this exec type. 866ea0fabbcSTim J. Robbins */ 867ea0fabbcSTim J. Robbins arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS; 868ea0fabbcSTim J. Robbins sigcodesz = *(imgp->proc->p_sysent->sv_szsigcode); 869ea0fabbcSTim J. Robbins destp = (caddr_t)arginfo - sigcodesz - SPARE_USRSPACE - 870610ecfe0SMaxim Sobolev roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *)); 871ea0fabbcSTim J. Robbins 872ea0fabbcSTim J. Robbins /* 873ea0fabbcSTim J. Robbins * install sigcode 874ea0fabbcSTim J. Robbins */ 875ea0fabbcSTim J. Robbins if (sigcodesz) 876ea0fabbcSTim J. Robbins copyout(imgp->proc->p_sysent->sv_sigcode, 877d4d2a400SKonstantin Belousov ((caddr_t)arginfo - sigcodesz), sigcodesz); 878ea0fabbcSTim J. Robbins 879ea0fabbcSTim J. Robbins /* 880ea0fabbcSTim J. Robbins * If we have a valid auxargs ptr, prepare some room 881ea0fabbcSTim J. Robbins * on the stack. 882ea0fabbcSTim J. Robbins */ 883ea0fabbcSTim J. Robbins if (imgp->auxargs) { 884ea0fabbcSTim J. Robbins /* 885ea0fabbcSTim J. Robbins * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 886ea0fabbcSTim J. Robbins * lower compatibility. 887ea0fabbcSTim J. Robbins */ 888ea0fabbcSTim J. Robbins imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 889ea0fabbcSTim J. Robbins : (AT_COUNT * 2); 890ea0fabbcSTim J. Robbins /* 891ea0fabbcSTim J. Robbins * The '+ 2' is for the null pointers at the end of each of 892ea0fabbcSTim J. Robbins * the arg and env vector sets,and imgp->auxarg_size is room 893ea0fabbcSTim J. Robbins * for argument of Runtime loader. 894ea0fabbcSTim J. Robbins */ 895610ecfe0SMaxim Sobolev vectp = (u_int32_t *) (destp - (imgp->args->argc + imgp->args->envc + 2 + 896ea0fabbcSTim J. Robbins imgp->auxarg_size) * sizeof(u_int32_t)); 897ea0fabbcSTim J. Robbins 898ea0fabbcSTim J. Robbins } else 899ea0fabbcSTim J. Robbins /* 900ea0fabbcSTim J. Robbins * The '+ 2' is for the null pointers at the end of each of 901ea0fabbcSTim J. Robbins * the arg and env vector sets 902ea0fabbcSTim J. Robbins */ 903ea0fabbcSTim J. Robbins vectp = (u_int32_t *) 904610ecfe0SMaxim Sobolev (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t)); 905ea0fabbcSTim J. Robbins 906ea0fabbcSTim J. Robbins /* 907ea0fabbcSTim J. Robbins * vectp also becomes our initial stack base 908ea0fabbcSTim J. Robbins */ 909ea0fabbcSTim J. Robbins stack_base = vectp; 910ea0fabbcSTim J. Robbins 911610ecfe0SMaxim Sobolev stringp = imgp->args->begin_argv; 912610ecfe0SMaxim Sobolev argc = imgp->args->argc; 913610ecfe0SMaxim Sobolev envc = imgp->args->envc; 914ea0fabbcSTim J. Robbins /* 915ea0fabbcSTim J. Robbins * Copy out strings - arguments and environment. 916ea0fabbcSTim J. Robbins */ 917610ecfe0SMaxim Sobolev copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 918ea0fabbcSTim J. Robbins 919ea0fabbcSTim J. Robbins /* 920ea0fabbcSTim J. Robbins * Fill in "ps_strings" struct for ps, w, etc. 921ea0fabbcSTim J. Robbins */ 922ea0fabbcSTim J. Robbins suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp); 923ea0fabbcSTim J. Robbins suword32(&arginfo->ps_nargvstr, argc); 924ea0fabbcSTim J. Robbins 925ea0fabbcSTim J. Robbins /* 926ea0fabbcSTim J. Robbins * Fill in argument portion of vector table. 927ea0fabbcSTim J. Robbins */ 928ea0fabbcSTim J. Robbins for (; argc > 0; --argc) { 929ea0fabbcSTim J. Robbins suword32(vectp++, (u_int32_t)(intptr_t)destp); 930ea0fabbcSTim J. Robbins while (*stringp++ != 0) 931ea0fabbcSTim J. Robbins destp++; 932ea0fabbcSTim J. Robbins destp++; 933ea0fabbcSTim J. Robbins } 934ea0fabbcSTim J. Robbins 935ea0fabbcSTim J. Robbins /* a null vector table pointer separates the argp's from the envp's */ 936ea0fabbcSTim J. Robbins suword32(vectp++, 0); 937ea0fabbcSTim J. Robbins 938ea0fabbcSTim J. Robbins suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp); 939ea0fabbcSTim J. Robbins suword32(&arginfo->ps_nenvstr, envc); 940ea0fabbcSTim J. Robbins 941ea0fabbcSTim J. Robbins /* 942ea0fabbcSTim J. Robbins * Fill in environment portion of vector table. 943ea0fabbcSTim J. Robbins */ 944ea0fabbcSTim J. Robbins for (; envc > 0; --envc) { 945ea0fabbcSTim J. Robbins suword32(vectp++, (u_int32_t)(intptr_t)destp); 946ea0fabbcSTim J. Robbins while (*stringp++ != 0) 947ea0fabbcSTim J. Robbins destp++; 948ea0fabbcSTim J. Robbins destp++; 949ea0fabbcSTim J. Robbins } 950ea0fabbcSTim J. Robbins 951ea0fabbcSTim J. Robbins /* end of vector table is a null pointer */ 952ea0fabbcSTim J. Robbins suword32(vectp, 0); 953ea0fabbcSTim J. Robbins 954ea0fabbcSTim J. Robbins return ((register_t *)stack_base); 955ea0fabbcSTim J. Robbins } 956ea0fabbcSTim J. Robbins 957ea0fabbcSTim J. Robbins SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0, 958ea0fabbcSTim J. Robbins "32-bit Linux emulation"); 959ea0fabbcSTim J. Robbins 960ea0fabbcSTim J. Robbins static u_long linux32_maxdsiz = LINUX32_MAXDSIZ; 961ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW, 962ea0fabbcSTim J. Robbins &linux32_maxdsiz, 0, ""); 963ea0fabbcSTim J. Robbins static u_long linux32_maxssiz = LINUX32_MAXSSIZ; 964ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW, 965ea0fabbcSTim J. Robbins &linux32_maxssiz, 0, ""); 966ea0fabbcSTim J. Robbins static u_long linux32_maxvmem = LINUX32_MAXVMEM; 967ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW, 968ea0fabbcSTim J. Robbins &linux32_maxvmem, 0, ""); 969ea0fabbcSTim J. Robbins 970ea0fabbcSTim J. Robbins static void 97119059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which) 972ea0fabbcSTim J. Robbins { 973ea0fabbcSTim J. Robbins 97419059a13SJohn Baldwin switch (which) { 97519059a13SJohn Baldwin case RLIMIT_DATA: 976ea0fabbcSTim J. Robbins if (linux32_maxdsiz != 0) { 97719059a13SJohn Baldwin if (rl->rlim_cur > linux32_maxdsiz) 97819059a13SJohn Baldwin rl->rlim_cur = linux32_maxdsiz; 97919059a13SJohn Baldwin if (rl->rlim_max > linux32_maxdsiz) 98019059a13SJohn Baldwin rl->rlim_max = linux32_maxdsiz; 981ea0fabbcSTim J. Robbins } 98219059a13SJohn Baldwin break; 98319059a13SJohn Baldwin case RLIMIT_STACK: 984ea0fabbcSTim J. Robbins if (linux32_maxssiz != 0) { 98519059a13SJohn Baldwin if (rl->rlim_cur > linux32_maxssiz) 98619059a13SJohn Baldwin rl->rlim_cur = linux32_maxssiz; 98719059a13SJohn Baldwin if (rl->rlim_max > linux32_maxssiz) 98819059a13SJohn Baldwin rl->rlim_max = linux32_maxssiz; 989ea0fabbcSTim J. Robbins } 99019059a13SJohn Baldwin break; 99119059a13SJohn Baldwin case RLIMIT_VMEM: 992ea0fabbcSTim J. Robbins if (linux32_maxvmem != 0) { 99319059a13SJohn Baldwin if (rl->rlim_cur > linux32_maxvmem) 99419059a13SJohn Baldwin rl->rlim_cur = linux32_maxvmem; 99519059a13SJohn Baldwin if (rl->rlim_max > linux32_maxvmem) 99619059a13SJohn Baldwin rl->rlim_max = linux32_maxvmem; 997ea0fabbcSTim J. Robbins } 99819059a13SJohn Baldwin break; 99919059a13SJohn Baldwin } 1000ea0fabbcSTim J. Robbins } 1001ea0fabbcSTim J. Robbins 1002ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = { 1003a8d403e1SKonstantin Belousov .sv_size = LINUX_SYS_MAXSYSCALL, 1004a8d403e1SKonstantin Belousov .sv_table = linux_sysent, 1005a8d403e1SKonstantin Belousov .sv_mask = 0, 1006a8d403e1SKonstantin Belousov .sv_sigsize = LINUX_SIGTBLSZ, 1007a8d403e1SKonstantin Belousov .sv_sigtbl = bsd_to_linux_signal, 1008a8d403e1SKonstantin Belousov .sv_errsize = ELAST + 1, 1009a8d403e1SKonstantin Belousov .sv_errtbl = bsd_to_linux_errno, 1010a8d403e1SKonstantin Belousov .sv_transtrap = translate_traps, 1011a8d403e1SKonstantin Belousov .sv_fixup = elf_linux_fixup, 1012a8d403e1SKonstantin Belousov .sv_sendsig = linux_sendsig, 1013a8d403e1SKonstantin Belousov .sv_sigcode = linux_sigcode, 1014a8d403e1SKonstantin Belousov .sv_szsigcode = &linux_szsigcode, 1015a8d403e1SKonstantin Belousov .sv_prepsyscall = linux_prepsyscall, 1016a8d403e1SKonstantin Belousov .sv_name = "Linux ELF32", 1017a8d403e1SKonstantin Belousov .sv_coredump = elf32_coredump, 1018a8d403e1SKonstantin Belousov .sv_imgact_try = exec_linux_imgact_try, 1019a8d403e1SKonstantin Belousov .sv_minsigstksz = LINUX_MINSIGSTKSZ, 1020a8d403e1SKonstantin Belousov .sv_pagesize = PAGE_SIZE, 1021a8d403e1SKonstantin Belousov .sv_minuser = VM_MIN_ADDRESS, 1022a8d403e1SKonstantin Belousov .sv_maxuser = LINUX32_USRSTACK, 1023a8d403e1SKonstantin Belousov .sv_usrstack = LINUX32_USRSTACK, 1024a8d403e1SKonstantin Belousov .sv_psstrings = LINUX32_PS_STRINGS, 1025a8d403e1SKonstantin Belousov .sv_stackprot = VM_PROT_ALL, 1026a8d403e1SKonstantin Belousov .sv_copyout_strings = linux_copyout_strings, 1027a8d403e1SKonstantin Belousov .sv_setregs = exec_linux_setregs, 1028a8d403e1SKonstantin Belousov .sv_fixlimit = linux32_fixlimit, 1029a8d403e1SKonstantin Belousov .sv_maxssiz = &linux32_maxssiz, 1030ea0fabbcSTim J. Robbins }; 1031ea0fabbcSTim J. Robbins 1032ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = { 1033a8d403e1SKonstantin Belousov .brand = ELFOSABI_LINUX, 1034a8d403e1SKonstantin Belousov .machine = EM_386, 1035a8d403e1SKonstantin Belousov .compat_3_brand = "Linux", 1036a8d403e1SKonstantin Belousov .emul_path = "/compat/linux", 1037a8d403e1SKonstantin Belousov .interp_path = "/lib/ld-linux.so.1", 1038a8d403e1SKonstantin Belousov .sysvec = &elf_linux_sysvec, 1039a8d403e1SKonstantin Belousov .interp_newpath = NULL, 1040a8d403e1SKonstantin Belousov .flags = BI_CAN_EXEC_DYN, 1041ea0fabbcSTim J. Robbins }; 1042ea0fabbcSTim J. Robbins 1043ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = { 1044a8d403e1SKonstantin Belousov .brand = ELFOSABI_LINUX, 1045a8d403e1SKonstantin Belousov .machine = EM_386, 1046a8d403e1SKonstantin Belousov .compat_3_brand = "Linux", 1047a8d403e1SKonstantin Belousov .emul_path = "/compat/linux", 1048a8d403e1SKonstantin Belousov .interp_path = "/lib/ld-linux.so.2", 1049a8d403e1SKonstantin Belousov .sysvec = &elf_linux_sysvec, 1050a8d403e1SKonstantin Belousov .interp_newpath = NULL, 1051a8d403e1SKonstantin Belousov .flags = BI_CAN_EXEC_DYN, 1052ea0fabbcSTim J. Robbins }; 1053ea0fabbcSTim J. Robbins 1054ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = { 1055ea0fabbcSTim J. Robbins &linux_brand, 1056ea0fabbcSTim J. Robbins &linux_glibc2brand, 1057ea0fabbcSTim J. Robbins NULL 1058ea0fabbcSTim J. Robbins }; 1059ea0fabbcSTim J. Robbins 1060ea0fabbcSTim J. Robbins static int 1061ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data) 1062ea0fabbcSTim J. Robbins { 1063ea0fabbcSTim J. Robbins Elf32_Brandinfo **brandinfo; 1064ea0fabbcSTim J. Robbins int error; 1065ea0fabbcSTim J. Robbins struct linux_ioctl_handler **lihp; 1066387196bfSDoug Ambrisko struct linux_device_handler **ldhp; 1067ea0fabbcSTim J. Robbins 1068ea0fabbcSTim J. Robbins error = 0; 1069ea0fabbcSTim J. Robbins 1070ea0fabbcSTim J. Robbins switch(type) { 1071ea0fabbcSTim J. Robbins case MOD_LOAD: 1072ea0fabbcSTim J. Robbins for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1073ea0fabbcSTim J. Robbins ++brandinfo) 1074ea0fabbcSTim J. Robbins if (elf32_insert_brand_entry(*brandinfo) < 0) 1075ea0fabbcSTim J. Robbins error = EINVAL; 1076ea0fabbcSTim J. Robbins if (error == 0) { 1077ea0fabbcSTim J. Robbins SET_FOREACH(lihp, linux_ioctl_handler_set) 1078ea0fabbcSTim J. Robbins linux_ioctl_register_handler(*lihp); 1079387196bfSDoug Ambrisko SET_FOREACH(ldhp, linux_device_handler_set) 1080387196bfSDoug Ambrisko linux_device_register_handler(*ldhp); 1081357afa71SJung-uk Kim mtx_init(&emul_lock, "emuldata lock", NULL, MTX_DEF); 10827c09e6c0SAlexander Leidinger sx_init(&emul_shared_lock, "emuldata->shared lock"); 10837c09e6c0SAlexander Leidinger LIST_INIT(&futex_list); 1084bb59e63fSAlexander Leidinger sx_init(&futex_sx, "futex protection lock"); 10857c09e6c0SAlexander Leidinger linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit, 10867c09e6c0SAlexander Leidinger NULL, 1000); 10877c09e6c0SAlexander Leidinger linux_schedtail_tag = EVENTHANDLER_REGISTER(schedtail, linux_schedtail, 10887c09e6c0SAlexander Leidinger NULL, 1000); 10897c09e6c0SAlexander Leidinger linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec, 10907c09e6c0SAlexander Leidinger NULL, 1000); 1091ea0fabbcSTim J. Robbins if (bootverbose) 1092ea0fabbcSTim J. Robbins printf("Linux ELF exec handler installed\n"); 1093ea0fabbcSTim J. Robbins } else 1094ea0fabbcSTim J. Robbins printf("cannot insert Linux ELF brand handler\n"); 1095ea0fabbcSTim J. Robbins break; 1096ea0fabbcSTim J. Robbins case MOD_UNLOAD: 1097ea0fabbcSTim J. Robbins for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1098ea0fabbcSTim J. Robbins ++brandinfo) 1099ea0fabbcSTim J. Robbins if (elf32_brand_inuse(*brandinfo)) 1100ea0fabbcSTim J. Robbins error = EBUSY; 1101ea0fabbcSTim J. Robbins if (error == 0) { 1102ea0fabbcSTim J. Robbins for (brandinfo = &linux_brandlist[0]; 1103ea0fabbcSTim J. Robbins *brandinfo != NULL; ++brandinfo) 1104ea0fabbcSTim J. Robbins if (elf32_remove_brand_entry(*brandinfo) < 0) 1105ea0fabbcSTim J. Robbins error = EINVAL; 1106ea0fabbcSTim J. Robbins } 1107ea0fabbcSTim J. Robbins if (error == 0) { 1108ea0fabbcSTim J. Robbins SET_FOREACH(lihp, linux_ioctl_handler_set) 1109ea0fabbcSTim J. Robbins linux_ioctl_unregister_handler(*lihp); 1110387196bfSDoug Ambrisko SET_FOREACH(ldhp, linux_device_handler_set) 1111387196bfSDoug Ambrisko linux_device_unregister_handler(*ldhp); 1112357afa71SJung-uk Kim mtx_destroy(&emul_lock); 11137c09e6c0SAlexander Leidinger sx_destroy(&emul_shared_lock); 1114bb59e63fSAlexander Leidinger sx_destroy(&futex_sx); 11157c09e6c0SAlexander Leidinger EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag); 11167c09e6c0SAlexander Leidinger EVENTHANDLER_DEREGISTER(schedtail, linux_schedtail_tag); 11177c09e6c0SAlexander Leidinger EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag); 1118ea0fabbcSTim J. Robbins if (bootverbose) 1119ea0fabbcSTim J. Robbins printf("Linux ELF exec handler removed\n"); 1120ea0fabbcSTim J. Robbins } else 1121ea0fabbcSTim J. Robbins printf("Could not deinstall ELF interpreter entry\n"); 1122ea0fabbcSTim J. Robbins break; 1123ea0fabbcSTim J. Robbins default: 1124786e4fc4SAlexander Leidinger return EOPNOTSUPP; 1125ea0fabbcSTim J. Robbins } 1126ea0fabbcSTim J. Robbins return error; 1127ea0fabbcSTim J. Robbins } 1128ea0fabbcSTim J. Robbins 1129ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = { 1130ea0fabbcSTim J. Robbins "linuxelf", 1131ea0fabbcSTim J. Robbins linux_elf_modevent, 1132ea0fabbcSTim J. Robbins 0 1133ea0fabbcSTim J. Robbins }; 1134ea0fabbcSTim J. Robbins 1135ea0fabbcSTim J. Robbins DECLARE_MODULE(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 1136