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