| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /*- |
| 2 | * Copyright (c) 1994-1996 Søren Schmidt | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer | |
| 10 | * in this position and unchanged. | |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in the | |
| 13 | * documentation and/or other materials provided with the distribution. | |
| 14 | * 3. The name of the author may not be used to endorse or promote products | |
| 4d9022e3 | 15 | * derived from this software without specific prior written permission |
| 984263bc MD |
16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | * | |
| 28 | * $FreeBSD: src/sys/i386/linux/linux_sysvec.c,v 1.55.2.9 2002/01/12 11:03:30 bde Exp $ | |
| b276424c | 29 | * $DragonFly: src/sys/emulation/linux/i386/linux_sysvec.c,v 1.31 2008/04/21 15:47:53 dillon Exp $ |
| 984263bc MD |
30 | */ |
| 31 | ||
| 32 | /* XXX we use functions that might not exist. */ | |
| 33 | #include "opt_compat.h" | |
| 34 | ||
| 35 | #ifndef COMPAT_43 | |
| 36 | #error "Unable to compile Linux-emulator due to missing COMPAT_43 option!" | |
| 37 | #endif | |
| 38 | ||
| 39 | #include <sys/param.h> | |
| 40 | #include <sys/systm.h> | |
| 41 | #include <sys/imgact.h> | |
| 42 | #include <sys/imgact_aout.h> | |
| 43 | #include <sys/imgact_elf.h> | |
| 1058bc2a | 44 | #include <sys/kern_syscall.h> |
| 984263bc MD |
45 | #include <sys/lock.h> |
| 46 | #include <sys/malloc.h> | |
| 47 | #include <sys/proc.h> | |
| 48 | #include <sys/signalvar.h> | |
| 49 | #include <sys/sysent.h> | |
| 50 | #include <sys/sysproto.h> | |
| a1f82243 | 51 | #include <sys/eventhandler.h> |
| 984263bc MD |
52 | |
| 53 | #include <vm/vm.h> | |
| 54 | #include <vm/vm_param.h> | |
| 55 | #include <vm/vm_page.h> | |
| 56 | #include <vm/vm_extern.h> | |
| 57 | #include <sys/exec.h> | |
| 58 | #include <sys/kernel.h> | |
| 59 | #include <sys/module.h> | |
| 60 | #include <machine/cpu.h> | |
| 61 | ||
| 1f2de5d4 MD |
62 | #include "linux.h" |
| 63 | #include "linux_proto.h" | |
| 64 | #include "../linux_signal.h" | |
| 65 | #include "../linux_util.h" | |
| a1f82243 AH |
66 | #include "../linux_futex.h" |
| 67 | #include "../linux_emuldata.h" | |
| 984263bc | 68 | |
| 09c280ec SS |
69 | MODULE_VERSION(linux, 1); |
| 70 | ||
| 984263bc MD |
71 | MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); |
| 72 | ||
| 73 | #if BYTE_ORDER == LITTLE_ENDIAN | |
| 74 | #define SHELLMAGIC 0x2123 /* #! */ | |
| 75 | #else | |
| 76 | #define SHELLMAGIC 0x2321 | |
| 77 | #endif | |
| 78 | ||
| 79 | /* | |
| 80 | * Allow the sendsig functions to use the ldebug() facility | |
| 81 | * even though they are not syscalls themselves. Map them | |
| 82 | * to syscall 0. This is slightly less bogus than using | |
| 83 | * ldebug(sigreturn). | |
| 84 | */ | |
| 85 | #define LINUX_SYS_linux_rt_sendsig 0 | |
| 86 | #define LINUX_SYS_linux_sendsig 0 | |
| 87 | ||
| 88 | extern char linux_sigcode[]; | |
| 89 | extern int linux_szsigcode; | |
| 90 | ||
| 91 | extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; | |
| 92 | ||
| b1efe3b4 RG |
93 | static int linux_fixup (register_t **stack_base, |
| 94 | struct image_params *iparams); | |
| 95 | static int elf_linux_fixup (register_t **stack_base, | |
| 96 | struct image_params *iparams); | |
| 97 | static void linux_prepsyscall (struct trapframe *tf, int *args, | |
| 98 | u_int *code, caddr_t *params); | |
| 99 | static void linux_sendsig (sig_t catcher, int sig, sigset_t *mask, | |
| 100 | u_long code); | |
| 984263bc | 101 | |
| a1f82243 AH |
102 | static eventhandler_tag linux_exec_tag; |
| 103 | static eventhandler_tag linux_exit_tag; | |
| 104 | ||
| 984263bc MD |
105 | /* |
| 106 | * Linux syscalls return negative errno's, we do positive and map them | |
| 107 | */ | |
| 108 | static int bsd_to_linux_errno[ELAST + 1] = { | |
| 109 | -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, | |
| 110 | -10, -35, -12, -13, -14, -15, -16, -17, -18, -19, | |
| 111 | -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, | |
| 112 | -30, -31, -32, -33, -34, -11,-115,-114, -88, -89, | |
| 113 | -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, | |
| 114 | -100,-101,-102,-103,-104,-105,-106,-107,-108,-109, | |
| 115 | -110,-111, -40, -36,-112,-113, -39, -11, -87,-122, | |
| 116 | -116, -66, -6, -6, -6, -6, -6, -37, -38, -9, | |
| 117 | -6, -6, -43, -42, -75, -6, -84 | |
| 118 | }; | |
| 119 | ||
| 120 | int bsd_to_linux_signal[LINUX_SIGTBLSZ] = { | |
| 121 | LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT, LINUX_SIGILL, | |
| 122 | LINUX_SIGTRAP, LINUX_SIGABRT, 0, LINUX_SIGFPE, | |
| a1f82243 | 123 | LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV, LINUX_SIGSYS, |
| 984263bc MD |
124 | LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM, LINUX_SIGURG, |
| 125 | LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT, LINUX_SIGCHLD, | |
| 126 | LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO, LINUX_SIGXCPU, | |
| 127 | LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF, LINUX_SIGWINCH, | |
| 128 | 0, LINUX_SIGUSR1, LINUX_SIGUSR2 | |
| 129 | }; | |
| 130 | ||
| 131 | int linux_to_bsd_signal[LINUX_SIGTBLSZ] = { | |
| 132 | SIGHUP, SIGINT, SIGQUIT, SIGILL, | |
| 133 | SIGTRAP, SIGABRT, SIGBUS, SIGFPE, | |
| 134 | SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, | |
| 135 | SIGPIPE, SIGALRM, SIGTERM, SIGBUS, | |
| 136 | SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, | |
| 137 | SIGTTIN, SIGTTOU, SIGURG, SIGXCPU, | |
| 138 | SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, | |
| a1f82243 | 139 | SIGIO, SIGURG, SIGSYS |
| 984263bc MD |
140 | }; |
| 141 | ||
| 142 | #define LINUX_T_UNKNOWN 255 | |
| 143 | static int _bsd_to_linux_trapcode[] = { | |
| 144 | LINUX_T_UNKNOWN, /* 0 */ | |
| 145 | 6, /* 1 T_PRIVINFLT */ | |
| 146 | LINUX_T_UNKNOWN, /* 2 */ | |
| 147 | 3, /* 3 T_BPTFLT */ | |
| 148 | LINUX_T_UNKNOWN, /* 4 */ | |
| 149 | LINUX_T_UNKNOWN, /* 5 */ | |
| 150 | 16, /* 6 T_ARITHTRAP */ | |
| 151 | 254, /* 7 T_ASTFLT */ | |
| 152 | LINUX_T_UNKNOWN, /* 8 */ | |
| 153 | 13, /* 9 T_PROTFLT */ | |
| 154 | 1, /* 10 T_TRCTRAP */ | |
| 155 | LINUX_T_UNKNOWN, /* 11 */ | |
| 156 | 14, /* 12 T_PAGEFLT */ | |
| 157 | LINUX_T_UNKNOWN, /* 13 */ | |
| 158 | 17, /* 14 T_ALIGNFLT */ | |
| 159 | LINUX_T_UNKNOWN, /* 15 */ | |
| 160 | LINUX_T_UNKNOWN, /* 16 */ | |
| 161 | LINUX_T_UNKNOWN, /* 17 */ | |
| 162 | 0, /* 18 T_DIVIDE */ | |
| 163 | 2, /* 19 T_NMI */ | |
| 164 | 4, /* 20 T_OFLOW */ | |
| 165 | 5, /* 21 T_BOUND */ | |
| 166 | 7, /* 22 T_DNA */ | |
| 167 | 8, /* 23 T_DOUBLEFLT */ | |
| 168 | 9, /* 24 T_FPOPFLT */ | |
| 169 | 10, /* 25 T_TSSFLT */ | |
| 170 | 11, /* 26 T_SEGNPFLT */ | |
| 171 | 12, /* 27 T_STKFLT */ | |
| 172 | 18, /* 28 T_MCHK */ | |
| 173 | 19, /* 29 T_XMMFLT */ | |
| 174 | 15 /* 30 T_RESERVED */ | |
| 175 | }; | |
| 176 | #define bsd_to_linux_trapcode(code) \ | |
| 177 | ((code)<sizeof(_bsd_to_linux_trapcode)/sizeof(*_bsd_to_linux_trapcode)? \ | |
| 178 | _bsd_to_linux_trapcode[(code)]: \ | |
| 179 | LINUX_T_UNKNOWN) | |
| 180 | ||
| 181 | /* | |
| 182 | * If FreeBSD & Linux have a difference of opinion about what a trap | |
| 183 | * means, deal with it here. | |
| 184 | */ | |
| 185 | static int | |
| 186 | translate_traps(int signal, int trap_code) | |
| 187 | { | |
| 188 | if (signal != SIGBUS) | |
| 189 | return signal; | |
| 190 | switch (trap_code) { | |
| 191 | case T_PROTFLT: | |
| 192 | case T_TSSFLT: | |
| 193 | case T_DOUBLEFLT: | |
| 194 | case T_PAGEFLT: | |
| 195 | return SIGSEGV; | |
| 196 | default: | |
| 197 | return signal; | |
| 198 | } | |
| 199 | } | |
| 200 | ||
| 201 | static int | |
| 202 | linux_fixup(register_t **stack_base, struct image_params *imgp) | |
| 203 | { | |
| 204 | register_t *argv, *envp; | |
| 205 | ||
| 206 | argv = *stack_base; | |
| 2bd9d75c | 207 | envp = *stack_base + (imgp->args->argc + 1); |
| 984263bc MD |
208 | (*stack_base)--; |
| 209 | **stack_base = (intptr_t)(void *)envp; | |
| 210 | (*stack_base)--; | |
| 211 | **stack_base = (intptr_t)(void *)argv; | |
| 212 | (*stack_base)--; | |
| 2bd9d75c | 213 | **stack_base = imgp->args->argc; |
| 984263bc MD |
214 | return 0; |
| 215 | } | |
| 216 | ||
| 217 | static int | |
| 218 | elf_linux_fixup(register_t **stack_base, struct image_params *imgp) | |
| 219 | { | |
| 220 | Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs; | |
| 221 | register_t *pos; | |
| 222 | ||
| 2bd9d75c | 223 | pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2); |
| 984263bc MD |
224 | |
| 225 | if (args->trace) { | |
| 226 | AUXARGS_ENTRY(pos, AT_DEBUG, 1); | |
| 227 | } | |
| 228 | if (args->execfd != -1) { | |
| 229 | AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); | |
| 230 | } | |
| 231 | AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); | |
| 232 | AUXARGS_ENTRY(pos, AT_PHENT, args->phent); | |
| 233 | AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); | |
| 234 | AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); | |
| 235 | AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); | |
| 236 | AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); | |
| 237 | AUXARGS_ENTRY(pos, AT_BASE, args->base); | |
| 41c20dac MD |
238 | AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); |
| 239 | AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); | |
| 240 | AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); | |
| 241 | AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); | |
| 984263bc MD |
242 | AUXARGS_ENTRY(pos, AT_NULL, 0); |
| 243 | ||
| efda3bd0 | 244 | kfree(imgp->auxargs, M_TEMP); |
| 984263bc MD |
245 | imgp->auxargs = NULL; |
| 246 | ||
| 247 | (*stack_base)--; | |
| 2bd9d75c | 248 | **stack_base = (long)imgp->args->argc; |
| 984263bc MD |
249 | return 0; |
| 250 | } | |
| 251 | ||
| 252 | extern int _ucodesel, _udatasel; | |
| 253 | extern unsigned long linux_sznonrtsigcode; | |
| 254 | ||
| 255 | static void | |
| 256 | linux_rt_sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code) | |
| 257 | { | |
| 41c20dac | 258 | struct proc *p = curproc; |
| 08f2f1bb | 259 | struct lwp *lp = curthread->td_lwp; |
| 41c20dac | 260 | struct trapframe *regs; |
| 984263bc MD |
261 | struct l_rt_sigframe *fp, frame; |
| 262 | int oonstack; | |
| 263 | ||
| 08f2f1bb SS |
264 | regs = lp->lwp_md.md_regs; |
| 265 | oonstack = lp->lwp_sigstk.ss_flags & SS_ONSTACK; | |
| 984263bc MD |
266 | |
| 267 | #ifdef DEBUG | |
| 268 | if (ldebug(rt_sendsig)) | |
| 26be20a0 | 269 | kprintf(ARGS(rt_sendsig, "%p, %d, %p, %lu"), |
| 984263bc MD |
270 | catcher, sig, (void*)mask, code); |
| 271 | #endif | |
| 272 | /* | |
| 273 | * Allocate space for the signal handler context. | |
| 274 | */ | |
| 08f2f1bb | 275 | if ((lp->lwp_flag & LWP_ALTSTACK) && !oonstack && |
| 984263bc | 276 | SIGISMEMBER(p->p_sigacts->ps_sigonstack, sig)) { |
| 08f2f1bb SS |
277 | fp = (struct l_rt_sigframe *)(lp->lwp_sigstk.ss_sp + |
| 278 | lp->lwp_sigstk.ss_size - sizeof(struct l_rt_sigframe)); | |
| 279 | lp->lwp_sigstk.ss_flags |= SS_ONSTACK; | |
| 984263bc MD |
280 | } else |
| 281 | fp = (struct l_rt_sigframe *)regs->tf_esp - 1; | |
| 282 | ||
| 283 | /* | |
| 284 | * grow() will return FALSE if the fp will not fit inside the stack | |
| 285 | * and the stack can not be grown. useracc will return FALSE | |
| 286 | * if access is denied. | |
| 287 | */ | |
| 8d496bf9 | 288 | if ((vm_map_growstack(p, (vm_offset_t)fp) != KERN_SUCCESS) || |
| 984263bc MD |
289 | !useracc((caddr_t)fp, sizeof (struct l_rt_sigframe), |
| 290 | VM_PROT_WRITE)) { | |
| 291 | /* | |
| 292 | * Process has trashed its stack; give it an illegal | |
| 293 | * instruction to halt it in its tracks. | |
| 294 | */ | |
| 295 | SIGACTION(p, SIGILL) = SIG_DFL; | |
| 296 | SIGDELSET(p->p_sigignore, SIGILL); | |
| 297 | SIGDELSET(p->p_sigcatch, SIGILL); | |
| 08f2f1bb | 298 | SIGDELSET(lp->lwp_sigmask, SIGILL); |
| 984263bc MD |
299 | #ifdef DEBUG |
| 300 | if (ldebug(rt_sendsig)) | |
| 26be20a0 | 301 | kprintf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"), |
| 984263bc MD |
302 | fp, oonstack); |
| 303 | #endif | |
| 7278a846 | 304 | lwpsignal(p, lp, SIGILL); |
| 984263bc MD |
305 | return; |
| 306 | } | |
| 307 | ||
| 308 | /* | |
| 309 | * Build the argument list for the signal handler. | |
| 310 | */ | |
| 311 | if (p->p_sysent->sv_sigtbl) | |
| 312 | if (sig <= p->p_sysent->sv_sigsize) | |
| 313 | sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; | |
| 314 | ||
| 315 | frame.sf_handler = catcher; | |
| 316 | frame.sf_sig = sig; | |
| 317 | frame.sf_siginfo = &fp->sf_si; | |
| 318 | frame.sf_ucontext = &fp->sf_sc; | |
| 319 | ||
| 320 | /* Fill siginfo structure. */ | |
| 321 | frame.sf_si.lsi_signo = sig; | |
| 322 | frame.sf_si.lsi_code = code; | |
| 323 | frame.sf_si.lsi_addr = (void *)regs->tf_err; | |
| 324 | ||
| 325 | /* | |
| 326 | * Build the signal context to be used by sigreturn. | |
| 327 | */ | |
| 328 | frame.sf_sc.uc_flags = 0; /* XXX ??? */ | |
| 329 | frame.sf_sc.uc_link = NULL; /* XXX ??? */ | |
| 330 | ||
| 08f2f1bb SS |
331 | frame.sf_sc.uc_stack.ss_sp = lp->lwp_sigstk.ss_sp; |
| 332 | frame.sf_sc.uc_stack.ss_size = lp->lwp_sigstk.ss_size; | |
| 333 | frame.sf_sc.uc_stack.ss_flags = (lp->lwp_flag & LWP_ALTSTACK) | |
| 984263bc MD |
334 | ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; |
| 335 | ||
| 336 | bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask); | |
| 337 | ||
| 338 | frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__bits[0]; | |
| 4e7c41c5 | 339 | frame.sf_sc.uc_mcontext.sc_gs = regs->tf_gs; |
| 984263bc MD |
340 | frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs; |
| 341 | frame.sf_sc.uc_mcontext.sc_es = regs->tf_es; | |
| 342 | frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds; | |
| 343 | frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi; | |
| 344 | frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi; | |
| 345 | frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp; | |
| 346 | frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx; | |
| 347 | frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx; | |
| 348 | frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx; | |
| 349 | frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax; | |
| 350 | frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip; | |
| 351 | frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs; | |
| 352 | frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags; | |
| 353 | frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp; | |
| 354 | frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss; | |
| 355 | frame.sf_sc.uc_mcontext.sc_err = regs->tf_err; | |
| 356 | frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); | |
| 357 | ||
| 358 | #ifdef DEBUG | |
| 359 | if (ldebug(rt_sendsig)) | |
| 26be20a0 | 360 | kprintf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"), |
| 48cca561 | 361 | frame.sf_sc.uc_stack.ss_flags, lp->lwp_sigstk.ss_sp, |
| 08f2f1bb | 362 | lp->lwp_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask); |
| 984263bc MD |
363 | #endif |
| 364 | ||
| 365 | if (copyout(&frame, fp, sizeof(frame)) != 0) { | |
| 366 | /* | |
| 367 | * Process has trashed its stack; give it an illegal | |
| 368 | * instruction to halt it in its tracks. | |
| 369 | */ | |
| b276424c | 370 | sigexit(lp, SIGILL); |
| 984263bc MD |
371 | /* NOTREACHED */ |
| 372 | } | |
| 373 | ||
| 374 | /* | |
| 375 | * Build context to run handler in. | |
| 376 | */ | |
| 377 | regs->tf_esp = (int)fp; | |
| 378 | regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode) + | |
| 379 | linux_sznonrtsigcode; | |
| 8688c24a AE |
380 | |
| 381 | /* | |
| 382 | * i386 abi specifies that the direction flag must be cleared | |
| 383 | * on function entry | |
| 384 | */ | |
| 385 | regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); | |
| 386 | ||
| 984263bc MD |
387 | regs->tf_cs = _ucodesel; |
| 388 | regs->tf_ds = _udatasel; | |
| 389 | regs->tf_es = _udatasel; | |
| 4e7c41c5 MD |
390 | /* allow %fs and %gs to be inherited by the signal handler */ |
| 391 | /* | |
| 984263bc | 392 | regs->tf_fs = _udatasel; |
| 4e7c41c5 MD |
393 | regs->tf_gs = _udatasel; |
| 394 | */ | |
| 984263bc MD |
395 | regs->tf_ss = _udatasel; |
| 396 | } | |
| 397 | ||
| 398 | ||
| 399 | /* | |
| 400 | * Send an interrupt to process. | |
| 401 | * | |
| 402 | * Stack is set up to allow sigcode stored | |
| 403 | * in u. to call routine, followed by kcall | |
| 404 | * to sigreturn routine below. After sigreturn | |
| 405 | * resets the signal mask, the stack, and the | |
| 406 | * frame pointer, it returns to the user | |
| 407 | * specified pc, psl. | |
| 408 | */ | |
| 409 | ||
| 410 | static void | |
| 411 | linux_sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code) | |
| 412 | { | |
| 41c20dac | 413 | struct proc *p = curproc; |
| 08f2f1bb | 414 | struct lwp *lp = curthread->td_lwp; |
| 41c20dac | 415 | struct trapframe *regs; |
| 984263bc MD |
416 | struct l_sigframe *fp, frame; |
| 417 | l_sigset_t lmask; | |
| 418 | int oonstack, i; | |
| 419 | ||
| 420 | if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) { | |
| 421 | /* Signal handler installed with SA_SIGINFO. */ | |
| 422 | linux_rt_sendsig(catcher, sig, mask, code); | |
| 423 | return; | |
| 424 | } | |
| 425 | ||
| 08f2f1bb SS |
426 | regs = lp->lwp_md.md_regs; |
| 427 | oonstack = lp->lwp_sigstk.ss_flags & SS_ONSTACK; | |
| 984263bc MD |
428 | |
| 429 | #ifdef DEBUG | |
| 430 | if (ldebug(sendsig)) | |
| 26be20a0 | 431 | kprintf(ARGS(sendsig, "%p, %d, %p, %lu"), |
| 984263bc MD |
432 | catcher, sig, (void*)mask, code); |
| 433 | #endif | |
| 434 | ||
| 435 | /* | |
| 436 | * Allocate space for the signal handler context. | |
| 437 | */ | |
| 08f2f1bb | 438 | if ((lp->lwp_flag & LWP_ALTSTACK) && !oonstack && |
| 984263bc | 439 | SIGISMEMBER(p->p_sigacts->ps_sigonstack, sig)) { |
| 08f2f1bb SS |
440 | fp = (struct l_sigframe *)(lp->lwp_sigstk.ss_sp + |
| 441 | lp->lwp_sigstk.ss_size - sizeof(struct l_sigframe)); | |
| 442 | lp->lwp_sigstk.ss_flags |= SS_ONSTACK; | |
| 984263bc MD |
443 | } else |
| 444 | fp = (struct l_sigframe *)regs->tf_esp - 1; | |
| 445 | ||
| 446 | /* | |
| 447 | * grow() will return FALSE if the fp will not fit inside the stack | |
| 448 | * and the stack can not be grown. useracc will return FALSE | |
| 449 | * if access is denied. | |
| 450 | */ | |
| 8d496bf9 | 451 | if ((vm_map_growstack(p, (vm_offset_t)fp) != KERN_SUCCESS) || |
| 984263bc MD |
452 | !useracc((caddr_t)fp, sizeof (struct l_sigframe), |
| 453 | VM_PROT_WRITE)) { | |
| 454 | /* | |
| 455 | * Process has trashed its stack; give it an illegal | |
| 456 | * instruction to halt it in its tracks. | |
| 457 | */ | |
| 458 | SIGACTION(p, SIGILL) = SIG_DFL; | |
| 459 | SIGDELSET(p->p_sigignore, SIGILL); | |
| 460 | SIGDELSET(p->p_sigcatch, SIGILL); | |
| 08f2f1bb | 461 | SIGDELSET(lp->lwp_sigmask, SIGILL); |
| 7278a846 | 462 | lwpsignal(p, lp, SIGILL); |
| 984263bc MD |
463 | return; |
| 464 | } | |
| 465 | ||
| 466 | /* | |
| 467 | * Build the argument list for the signal handler. | |
| 468 | */ | |
| 469 | if (p->p_sysent->sv_sigtbl) | |
| 470 | if (sig <= p->p_sysent->sv_sigsize) | |
| 471 | sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; | |
| 472 | ||
| 473 | frame.sf_handler = catcher; | |
| 474 | frame.sf_sig = sig; | |
| 475 | ||
| 476 | bsd_to_linux_sigset(mask, &lmask); | |
| 477 | ||
| 478 | /* | |
| 479 | * Build the signal context to be used by sigreturn. | |
| 480 | */ | |
| 481 | frame.sf_sc.sc_mask = lmask.__bits[0]; | |
| 4e7c41c5 | 482 | frame.sf_sc.sc_gs = regs->tf_gs; |
| 984263bc MD |
483 | frame.sf_sc.sc_fs = regs->tf_fs; |
| 484 | frame.sf_sc.sc_es = regs->tf_es; | |
| 485 | frame.sf_sc.sc_ds = regs->tf_ds; | |
| 486 | frame.sf_sc.sc_edi = regs->tf_edi; | |
| 487 | frame.sf_sc.sc_esi = regs->tf_esi; | |
| 488 | frame.sf_sc.sc_ebp = regs->tf_ebp; | |
| 489 | frame.sf_sc.sc_ebx = regs->tf_ebx; | |
| 490 | frame.sf_sc.sc_edx = regs->tf_edx; | |
| 491 | frame.sf_sc.sc_ecx = regs->tf_ecx; | |
| 492 | frame.sf_sc.sc_eax = regs->tf_eax; | |
| 493 | frame.sf_sc.sc_eip = regs->tf_eip; | |
| 494 | frame.sf_sc.sc_cs = regs->tf_cs; | |
| 495 | frame.sf_sc.sc_eflags = regs->tf_eflags; | |
| 496 | frame.sf_sc.sc_esp_at_signal = regs->tf_esp; | |
| 497 | frame.sf_sc.sc_ss = regs->tf_ss; | |
| 498 | frame.sf_sc.sc_err = regs->tf_err; | |
| 499 | frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code); | |
| 500 | ||
| 501 | bzero(&frame.sf_fpstate, sizeof(struct l_fpstate)); | |
| 502 | ||
| 503 | for (i = 0; i < (LINUX_NSIG_WORDS-1); i++) | |
| 504 | frame.sf_extramask[i] = lmask.__bits[i+1]; | |
| 505 | ||
| 506 | if (copyout(&frame, fp, sizeof(frame)) != 0) { | |
| 507 | /* | |
| 508 | * Process has trashed its stack; give it an illegal | |
| 509 | * instruction to halt it in its tracks. | |
| 510 | */ | |
| b276424c | 511 | sigexit(lp, SIGILL); |
| 984263bc MD |
512 | /* NOTREACHED */ |
| 513 | } | |
| 514 | ||
| 515 | /* | |
| 516 | * Build context to run handler in. | |
| 517 | */ | |
| 518 | regs->tf_esp = (int)fp; | |
| 519 | regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode); | |
| 8688c24a AE |
520 | |
| 521 | /* | |
| 522 | * i386 abi specifies that the direction flag must be cleared | |
| 523 | * on function entry | |
| 524 | */ | |
| 525 | regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); | |
| 526 | ||
| 984263bc MD |
527 | regs->tf_cs = _ucodesel; |
| 528 | regs->tf_ds = _udatasel; | |
| 529 | regs->tf_es = _udatasel; | |
| 4e7c41c5 MD |
530 | /* Allow %fs and %gs to be inherited by the signal handler */ |
| 531 | /* | |
| 984263bc | 532 | regs->tf_fs = _udatasel; |
| 4e7c41c5 MD |
533 | regs->tf_gs = _udatasel; |
| 534 | */ | |
| 984263bc MD |
535 | regs->tf_ss = _udatasel; |
| 536 | } | |
| 537 | ||
| 538 | /* | |
| 539 | * System call to cleanup state after a signal | |
| 540 | * has been taken. Reset signal mask and | |
| 541 | * stack state from context left by sendsig (above). | |
| 542 | * Return to previous pc and psl as specified by | |
| 543 | * context left by sendsig. Check carefully to | |
| 544 | * make sure that the user has not modified the | |
| 545 | * psl to gain improper privileges or to cause | |
| 546 | * a machine fault. | |
| 3919ced0 MD |
547 | * |
| 548 | * MPSAFE | |
| 984263bc MD |
549 | */ |
| 550 | int | |
| 753fd850 | 551 | sys_linux_sigreturn(struct linux_sigreturn_args *args) |
| 984263bc | 552 | { |
| 08f2f1bb | 553 | struct lwp *lp = curthread->td_lwp; |
| 984263bc | 554 | struct l_sigframe frame; |
| c9faf524 | 555 | struct trapframe *regs; |
| 984263bc MD |
556 | l_sigset_t lmask; |
| 557 | int eflags, i; | |
| 558 | ||
| 08f2f1bb | 559 | regs = lp->lwp_md.md_regs; |
| 984263bc MD |
560 | |
| 561 | #ifdef DEBUG | |
| 562 | if (ldebug(sigreturn)) | |
| 26be20a0 | 563 | kprintf(ARGS(sigreturn, "%p"), (void *)args->sfp); |
| 984263bc MD |
564 | #endif |
| 565 | /* | |
| 566 | * The trampoline code hands us the sigframe. | |
| 567 | * It is unsafe to keep track of it ourselves, in the event that a | |
| 568 | * program jumps out of a signal handler. | |
| 569 | */ | |
| 570 | if (copyin((caddr_t)args->sfp, &frame, sizeof(frame)) != 0) | |
| 571 | return (EFAULT); | |
| 572 | ||
| 573 | /* | |
| 574 | * Check for security violations. | |
| 575 | */ | |
| 576 | #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) | |
| 577 | eflags = frame.sf_sc.sc_eflags; | |
| 578 | /* | |
| 579 | * XXX do allow users to change the privileged flag PSL_RF. The | |
| 580 | * cpu sets PSL_RF in tf_eflags for faults. Debuggers should | |
| 581 | * sometimes set it there too. tf_eflags is kept in the signal | |
| 582 | * context during signal handling and there is no other place | |
| 583 | * to remember it, so the PSL_RF bit may be corrupted by the | |
| 584 | * signal handler without us knowing. Corruption of the PSL_RF | |
| 585 | * bit at worst causes one more or one less debugger trap, so | |
| 586 | * allowing it is fairly harmless. | |
| 587 | */ | |
| 588 | if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { | |
| 589 | return(EINVAL); | |
| 590 | } | |
| 591 | ||
| 592 | /* | |
| 593 | * Don't allow users to load a valid privileged %cs. Let the | |
| 594 | * hardware check for invalid selectors, excess privilege in | |
| 595 | * other selectors, invalid %eip's and invalid %esp's. | |
| 596 | */ | |
| 597 | #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) | |
| 598 | if (!CS_SECURE(frame.sf_sc.sc_cs)) { | |
| 08f2f1bb | 599 | trapsignal(lp, SIGBUS, T_PROTFLT); |
| 984263bc MD |
600 | return(EINVAL); |
| 601 | } | |
| 602 | ||
| 08f2f1bb | 603 | lp->lwp_sigstk.ss_flags &= ~SS_ONSTACK; |
| 984263bc MD |
604 | lmask.__bits[0] = frame.sf_sc.sc_mask; |
| 605 | for (i = 0; i < (LINUX_NSIG_WORDS-1); i++) | |
| 606 | lmask.__bits[i+1] = frame.sf_extramask[i]; | |
| 08f2f1bb SS |
607 | linux_to_bsd_sigset(&lmask, &lp->lwp_sigmask); |
| 608 | SIG_CANTMASK(lp->lwp_sigmask); | |
| 984263bc MD |
609 | |
| 610 | /* | |
| 611 | * Restore signal context. | |
| 612 | */ | |
| 613 | /* %gs was restored by the trampoline. */ | |
| 614 | regs->tf_fs = frame.sf_sc.sc_fs; | |
| 615 | regs->tf_es = frame.sf_sc.sc_es; | |
| 616 | regs->tf_ds = frame.sf_sc.sc_ds; | |
| 617 | regs->tf_edi = frame.sf_sc.sc_edi; | |
| 618 | regs->tf_esi = frame.sf_sc.sc_esi; | |
| 619 | regs->tf_ebp = frame.sf_sc.sc_ebp; | |
| 620 | regs->tf_ebx = frame.sf_sc.sc_ebx; | |
| 621 | regs->tf_edx = frame.sf_sc.sc_edx; | |
| 622 | regs->tf_ecx = frame.sf_sc.sc_ecx; | |
| 623 | regs->tf_eax = frame.sf_sc.sc_eax; | |
| 624 | regs->tf_eip = frame.sf_sc.sc_eip; | |
| 625 | regs->tf_cs = frame.sf_sc.sc_cs; | |
| 626 | regs->tf_eflags = eflags; | |
| 627 | regs->tf_esp = frame.sf_sc.sc_esp_at_signal; | |
| 628 | regs->tf_ss = frame.sf_sc.sc_ss; | |
| 629 | ||
| 630 | return (EJUSTRETURN); | |
| 631 | } | |
| 632 | ||
| 633 | /* | |
| 634 | * System call to cleanup state after a signal | |
| 635 | * has been taken. Reset signal mask and | |
| 636 | * stack state from context left by rt_sendsig (above). | |
| 637 | * Return to previous pc and psl as specified by | |
| 638 | * context left by sendsig. Check carefully to | |
| 639 | * make sure that the user has not modified the | |
| 640 | * psl to gain improper privileges or to cause | |
| 641 | * a machine fault. | |
| 3919ced0 MD |
642 | * |
| 643 | * MPSAFE | |
| 984263bc MD |
644 | */ |
| 645 | int | |
| 753fd850 | 646 | sys_linux_rt_sigreturn(struct linux_rt_sigreturn_args *args) |
| 984263bc | 647 | { |
| 08f2f1bb | 648 | struct lwp *lp = curthread->td_lwp; |
| 984263bc MD |
649 | struct l_ucontext uc; |
| 650 | struct l_sigcontext *context; | |
| 651 | l_stack_t *lss; | |
| 1058bc2a | 652 | stack_t ss; |
| c9faf524 | 653 | struct trapframe *regs; |
| 984263bc | 654 | int eflags; |
| 984263bc | 655 | |
| 08f2f1bb | 656 | regs = lp->lwp_md.md_regs; |
| 984263bc MD |
657 | |
| 658 | #ifdef DEBUG | |
| 659 | if (ldebug(rt_sigreturn)) | |
| 26be20a0 | 660 | kprintf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp); |
| 984263bc MD |
661 | #endif |
| 662 | /* | |
| 663 | * The trampoline code hands us the ucontext. | |
| 664 | * It is unsafe to keep track of it ourselves, in the event that a | |
| 665 | * program jumps out of a signal handler. | |
| 666 | */ | |
| 667 | if (copyin((caddr_t)args->ucp, &uc, sizeof(uc)) != 0) | |
| 668 | return (EFAULT); | |
| 669 | ||
| 670 | context = &uc.uc_mcontext; | |
| 671 | ||
| 672 | /* | |
| 673 | * Check for security violations. | |
| 674 | */ | |
| 675 | #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) | |
| 676 | eflags = context->sc_eflags; | |
| 677 | /* | |
| 678 | * XXX do allow users to change the privileged flag PSL_RF. The | |
| 679 | * cpu sets PSL_RF in tf_eflags for faults. Debuggers should | |
| 680 | * sometimes set it there too. tf_eflags is kept in the signal | |
| 681 | * context during signal handling and there is no other place | |
| 682 | * to remember it, so the PSL_RF bit may be corrupted by the | |
| 683 | * signal handler without us knowing. Corruption of the PSL_RF | |
| 684 | * bit at worst causes one more or one less debugger trap, so | |
| 685 | * allowing it is fairly harmless. | |
| 686 | */ | |
| 687 | if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { | |
| 688 | return(EINVAL); | |
| 689 | } | |
| 690 | ||
| 691 | /* | |
| 692 | * Don't allow users to load a valid privileged %cs. Let the | |
| 693 | * hardware check for invalid selectors, excess privilege in | |
| 694 | * other selectors, invalid %eip's and invalid %esp's. | |
| 695 | */ | |
| 696 | #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) | |
| 697 | if (!CS_SECURE(context->sc_cs)) { | |
| 08f2f1bb | 698 | trapsignal(lp, SIGBUS, T_PROTFLT); |
| 984263bc MD |
699 | return(EINVAL); |
| 700 | } | |
| 701 | ||
| 08f2f1bb SS |
702 | lp->lwp_sigstk.ss_flags &= ~SS_ONSTACK; |
| 703 | linux_to_bsd_sigset(&uc.uc_sigmask, &lp->lwp_sigmask); | |
| 704 | SIG_CANTMASK(lp->lwp_sigmask); | |
| 984263bc MD |
705 | |
| 706 | /* | |
| 707 | * Restore signal context | |
| 708 | */ | |
| 709 | /* %gs was restored by the trampoline. */ | |
| 710 | regs->tf_fs = context->sc_fs; | |
| 711 | regs->tf_es = context->sc_es; | |
| 712 | regs->tf_ds = context->sc_ds; | |
| 713 | regs->tf_edi = context->sc_edi; | |
| 714 | regs->tf_esi = context->sc_esi; | |
| 715 | regs->tf_ebp = context->sc_ebp; | |
| 716 | regs->tf_ebx = context->sc_ebx; | |
| 717 | regs->tf_edx = context->sc_edx; | |
| 718 | regs->tf_ecx = context->sc_ecx; | |
| 719 | regs->tf_eax = context->sc_eax; | |
| 720 | regs->tf_eip = context->sc_eip; | |
| 721 | regs->tf_cs = context->sc_cs; | |
| 722 | regs->tf_eflags = eflags; | |
| 723 | regs->tf_esp = context->sc_esp_at_signal; | |
| 724 | regs->tf_ss = context->sc_ss; | |
| 725 | ||
| 726 | /* | |
| 727 | * call sigaltstack & ignore results.. | |
| 728 | */ | |
| 984263bc | 729 | lss = &uc.uc_stack; |
| 1058bc2a DRJ |
730 | ss.ss_sp = lss->ss_sp; |
| 731 | ss.ss_size = lss->ss_size; | |
| 732 | ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); | |
| 984263bc MD |
733 | |
| 734 | #ifdef DEBUG | |
| 735 | if (ldebug(rt_sigreturn)) | |
| 26be20a0 | 736 | kprintf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"), |
| 1058bc2a | 737 | ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask); |
| 984263bc | 738 | #endif |
| 1058bc2a | 739 | kern_sigaltstack(&ss, NULL); |
| 984263bc MD |
740 | |
| 741 | return (EJUSTRETURN); | |
| 742 | } | |
| 743 | ||
| 270ac911 MD |
744 | /* |
| 745 | * Prep arguments. | |
| 746 | * | |
| 747 | * MUST BE MPSAFE | |
| 748 | */ | |
| 984263bc MD |
749 | static void |
| 750 | linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params) | |
| 751 | { | |
| 752 | args[0] = tf->tf_ebx; | |
| 753 | args[1] = tf->tf_ecx; | |
| 754 | args[2] = tf->tf_edx; | |
| 755 | args[3] = tf->tf_esi; | |
| 756 | args[4] = tf->tf_edi; | |
| 22d86f66 | 757 | args[5] = tf->tf_ebp; |
| 984263bc MD |
758 | *params = NULL; /* no copyin */ |
| 759 | } | |
| 760 | ||
| 761 | /* | |
| 762 | * If a linux binary is exec'ing something, try this image activator | |
| 763 | * first. We override standard shell script execution in order to | |
| 764 | * be able to modify the interpreter path. We only do this if a linux | |
| 765 | * binary is doing the exec, so we do not create an EXEC module for it. | |
| 766 | */ | |
| b1efe3b4 | 767 | static int exec_linux_imgact_try (struct image_params *iparams); |
| 984263bc MD |
768 | |
| 769 | static int | |
| 2da2a8af | 770 | exec_linux_imgact_try(struct image_params *imgp) |
| 984263bc MD |
771 | { |
| 772 | const char *head = (const char *)imgp->image_header; | |
| 773 | int error = -1; | |
| 774 | ||
| 775 | /* | |
| 776 | * The interpreter for shell scripts run from a linux binary needs | |
| 777 | * to be located in /compat/linux if possible in order to recursively | |
| 778 | * maintain linux path emulation. | |
| 779 | */ | |
| 780 | if (((const short *)head)[0] == SHELLMAGIC) { | |
| 781 | /* | |
| 782 | * Run our normal shell image activator. If it succeeds attempt | |
| 783 | * to use the alternate path for the interpreter. If an alternate | |
| 784 | * path is found, use our stringspace to store it. | |
| 785 | */ | |
| 786 | if ((error = exec_shell_imgact(imgp)) == 0) { | |
| 136178b3 DRJ |
787 | linux_translate_path(imgp->interpreter_name, |
| 788 | MAXSHELLCMDLEN); | |
| 984263bc MD |
789 | } |
| 790 | } | |
| 791 | return(error); | |
| 792 | } | |
| 793 | ||
| 794 | struct sysentvec linux_sysvec = { | |
| 795 | LINUX_SYS_MAXSYSCALL, | |
| 796 | linux_sysent, | |
| a1f82243 | 797 | 0xffffffff, |
| 984263bc MD |
798 | LINUX_SIGTBLSZ, |
| 799 | bsd_to_linux_signal, | |
| 800 | ELAST + 1, | |
| 801 | bsd_to_linux_errno, | |
| 802 | translate_traps, | |
| 803 | linux_fixup, | |
| 804 | linux_sendsig, | |
| 805 | linux_sigcode, | |
| 806 | &linux_szsigcode, | |
| 807 | linux_prepsyscall, | |
| 808 | "Linux a.out", | |
| 1e2d97fb | 809 | NULL, |
| 984263bc MD |
810 | exec_linux_imgact_try, |
| 811 | LINUX_MINSIGSTKSZ | |
| 812 | }; | |
| 813 | ||
| 814 | struct sysentvec elf_linux_sysvec = { | |
| 815 | LINUX_SYS_MAXSYSCALL, | |
| 816 | linux_sysent, | |
| a1f82243 | 817 | 0xffffffff, |
| 984263bc MD |
818 | LINUX_SIGTBLSZ, |
| 819 | bsd_to_linux_signal, | |
| 820 | ELAST + 1, | |
| 821 | bsd_to_linux_errno, | |
| 822 | translate_traps, | |
| 823 | elf_linux_fixup, | |
| 824 | linux_sendsig, | |
| 825 | linux_sigcode, | |
| 826 | &linux_szsigcode, | |
| 827 | linux_prepsyscall, | |
| 1799f7b1 AHJ |
828 | #if defined(__x86_64__) |
| 829 | "Linux ELF64", | |
| 830 | #else | |
| 831 | "Linux ELF32", | |
| 832 | #endif | |
| 984263bc MD |
833 | elf_coredump, |
| 834 | exec_linux_imgact_try, | |
| 835 | LINUX_MINSIGSTKSZ | |
| 836 | }; | |
| 837 | ||
| 8d0415e1 | 838 | static int linux_match_abi_note(const Elf_Note *abi_note); |
| 8900652c | 839 | static int linux_match_suse_note(const Elf_Note *abi_note); |
| 8d0415e1 | 840 | |
| 984263bc MD |
841 | static Elf32_Brandinfo linux_brand = { |
| 842 | ELFOSABI_LINUX, | |
| 843 | "Linux", | |
| 8d0415e1 | 844 | linux_match_abi_note, |
| 984263bc MD |
845 | "/compat/linux", |
| 846 | "/lib/ld-linux.so.1", | |
| 847 | &elf_linux_sysvec | |
| 848 | }; | |
| 849 | ||
| 850 | static Elf32_Brandinfo linux_glibc2brand = { | |
| 851 | ELFOSABI_LINUX, | |
| 852 | "Linux", | |
| 8d0415e1 | 853 | linux_match_abi_note, |
| 984263bc MD |
854 | "/compat/linux", |
| 855 | "/lib/ld-linux.so.2", | |
| 856 | &elf_linux_sysvec | |
| 857 | }; | |
| 858 | ||
| 8900652c JS |
859 | static Elf32_Brandinfo linux_suse_brand = { |
| 860 | ELFOSABI_LINUX, | |
| 861 | "Linux", | |
| 862 | linux_match_suse_note, | |
| 863 | "/compat/linux", | |
| 864 | "/lib/ld-linux.so.2", | |
| 865 | &elf_linux_sysvec | |
| 866 | }; | |
| 867 | ||
| 984263bc MD |
868 | Elf32_Brandinfo *linux_brandlist[] = { |
| 869 | &linux_brand, | |
| 870 | &linux_glibc2brand, | |
| 8900652c | 871 | &linux_suse_brand, |
| 984263bc MD |
872 | NULL |
| 873 | }; | |
| 874 | ||
| 875 | static int | |
| 8d0415e1 JS |
876 | linux_match_abi_note(const Elf_Note *abi_note) |
| 877 | { | |
| 878 | const char *abi_name = (const char *) | |
| 879 | ((const uint8_t *)abi_note + sizeof(*abi_note)); | |
| 880 | const uint32_t *descr = (const uint32_t *) | |
| 881 | ((const uint8_t *)abi_name + abi_note->n_namesz); | |
| 882 | ||
| 883 | if (abi_note->n_namesz != sizeof("GNU")) | |
| 884 | return(FALSE); | |
| 885 | if (memcmp(abi_name, "GNU", sizeof("GNU"))) | |
| 886 | return(FALSE); | |
| 887 | if (abi_note->n_descsz < sizeof(uint32_t)) | |
| 888 | return(FALSE); | |
| 889 | ||
| 890 | if (*descr != 0) | |
| 891 | return(FALSE); | |
| 892 | return(TRUE); | |
| 893 | } | |
| 894 | ||
| 895 | static int | |
| 8900652c JS |
896 | linux_match_suse_note(const Elf_Note *abi_note) |
| 897 | { | |
| 898 | const char *abi_name = (const char *) | |
| 899 | ((const uint8_t *)abi_note + sizeof(*abi_note)); | |
| 900 | ||
| 901 | if (abi_note->n_namesz != sizeof("SuSE")) | |
| 902 | return(FALSE); | |
| 903 | if (memcmp(abi_name, "SuSE", sizeof("SuSE"))) | |
| 904 | return(FALSE); | |
| 905 | ||
| 906 | return(TRUE); | |
| 907 | } | |
| 908 | ||
| 909 | static int | |
| 984263bc MD |
910 | linux_elf_modevent(module_t mod, int type, void *data) |
| 911 | { | |
| 912 | Elf32_Brandinfo **brandinfo; | |
| 913 | int error; | |
| 914 | ||
| 915 | error = 0; | |
| 916 | ||
| 917 | switch(type) { | |
| 918 | case MOD_LOAD: | |
| 919 | for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; | |
| 920 | ++brandinfo) | |
| 921 | if (elf_insert_brand_entry(*brandinfo) < 0) | |
| 922 | error = EINVAL; | |
| 923 | if (error == 0) { | |
| 984263bc | 924 | if (bootverbose) |
| 26be20a0 | 925 | kprintf("Linux ELF exec handler installed\n"); |
| a1f82243 | 926 | } else { |
| 26be20a0 | 927 | kprintf("cannot insert Linux ELF brand handler\n"); |
| a1f82243 AH |
928 | } |
| 929 | EMUL_LOCKINIT(); | |
| 930 | lockinit(&futex_mtx, "linftxs", 0, LK_CANRECURSE); | |
| 931 | linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_transition, | |
| 932 | NULL, 1000); | |
| 933 | linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, emuldata_exit, | |
| 934 | NULL, 1000); | |
| 984263bc MD |
935 | break; |
| 936 | case MOD_UNLOAD: | |
| 937 | for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; | |
| 938 | ++brandinfo) | |
| 939 | if (elf_brand_inuse(*brandinfo)) | |
| 940 | error = EBUSY; | |
| 941 | if (error == 0) { | |
| 942 | for (brandinfo = &linux_brandlist[0]; | |
| 943 | *brandinfo != NULL; ++brandinfo) | |
| 944 | if (elf_remove_brand_entry(*brandinfo) < 0) | |
| 945 | error = EINVAL; | |
| 946 | } | |
| 947 | if (error == 0) { | |
| 984263bc | 948 | if (bootverbose) |
| 26be20a0 | 949 | kprintf("Linux ELF exec handler removed\n"); |
| a1f82243 | 950 | } else { |
| 26be20a0 | 951 | kprintf("Could not deinstall ELF interpreter entry\n"); |
| a1f82243 AH |
952 | } |
| 953 | EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag); | |
| 954 | EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag); | |
| 955 | lockuninit(&futex_mtx); | |
| 956 | EMUL_LOCKUNINIT(); | |
| 984263bc MD |
957 | break; |
| 958 | default: | |
| 959 | break; | |
| 960 | } | |
| 961 | return error; | |
| 962 | } | |
| 963 | ||
| 964 | static moduledata_t linux_elf_mod = { | |
| 965 | "linuxelf", | |
| 966 | linux_elf_modevent, | |
| 967 | 0 | |
| 968 | }; | |
| 969 | ||
| 970 | DECLARE_MODULE(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); |