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