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