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