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