1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1994-1996 Søren Schmidt 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #define __ELF_WORD_SIZE 32 30 31 #include <sys/param.h> 32 #include <sys/exec.h> 33 #include <sys/fcntl.h> 34 #include <sys/imgact.h> 35 #include <sys/imgact_aout.h> 36 #include <sys/imgact_elf.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/stddef.h> 44 #include <sys/syscallsubr.h> 45 #include <sys/sysctl.h> 46 #include <sys/sysent.h> 47 #include <sys/sysproto.h> 48 49 #include <vm/pmap.h> 50 #include <vm/vm.h> 51 #include <vm/vm_map.h> 52 #include <vm/vm_page.h> 53 54 #include <machine/cpu.h> 55 #include <machine/cputypes.h> 56 #include <machine/md_var.h> 57 #include <machine/pcb.h> 58 #include <machine/trap.h> 59 60 #include <x86/linux/linux_x86.h> 61 #include <i386/linux/linux.h> 62 #include <i386/linux/linux_proto.h> 63 #include <compat/linux/linux_elf.h> 64 #include <compat/linux/linux_emul.h> 65 #include <compat/linux/linux_fork.h> 66 #include <compat/linux/linux_ioctl.h> 67 #include <compat/linux/linux_mib.h> 68 #include <compat/linux/linux_misc.h> 69 #include <compat/linux/linux_signal.h> 70 #include <compat/linux/linux_util.h> 71 #include <compat/linux/linux_vdso.h> 72 73 #include <x86/linux/linux_x86_sigframe.h> 74 75 MODULE_VERSION(linux, 1); 76 77 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 78 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE) 79 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) 80 /* 81 * PAGE_SIZE - the size 82 * of the native SHAREDPAGE 83 */ 84 #define LINUX_USRSTACK LINUX_SHAREDPAGE 85 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings)) 86 87 static int linux_szsigcode; 88 static vm_object_t linux_vdso_obj; 89 static char *linux_vdso_mapping; 90 extern char _binary_linux_vdso_so_o_start; 91 extern char _binary_linux_vdso_so_o_end; 92 static vm_offset_t linux_vdso_base; 93 94 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 95 extern const char *linux_syscallnames[]; 96 97 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 98 99 static int linux_fixup(uintptr_t *stack_base, 100 struct image_params *iparams); 101 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 102 static void linux_exec_setregs(struct thread *td, 103 struct image_params *imgp, uintptr_t stack); 104 static void linux_exec_sysvec_init(void *param); 105 static int linux_on_exec_vmspace(struct proc *p, 106 struct image_params *imgp); 107 static void linux_set_fork_retval(struct thread *td); 108 static void linux_vdso_install(const void *param); 109 static void linux_vdso_deinstall(const void *param); 110 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 111 112 LINUX_VDSO_SYM_CHAR(linux_platform); 113 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall); 114 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode); 115 LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode); 116 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 117 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector); 118 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector); 119 120 static int 121 linux_fixup(uintptr_t *stack_base, struct image_params *imgp) 122 { 123 register_t *base, *argv, *envp; 124 125 base = (register_t *)*stack_base; 126 argv = base; 127 envp = base + (imgp->args->argc + 1); 128 base--; 129 suword(base, (intptr_t)envp); 130 base--; 131 suword(base, (intptr_t)argv); 132 base--; 133 suword(base, imgp->args->argc); 134 *stack_base = (uintptr_t)base; 135 return (0); 136 } 137 138 void 139 linux32_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos) 140 { 141 142 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 143 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO, __kernel_vsyscall); 144 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature); 145 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2()); 146 AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform)); 147 } 148 149 static void 150 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 151 { 152 struct thread *td = curthread; 153 struct proc *p = td->td_proc; 154 struct sigacts *psp; 155 struct trapframe *regs; 156 struct l_rt_sigframe *fp, frame; 157 int sig, code; 158 int oonstack; 159 160 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 161 code = ksi->ksi_code; 162 PROC_LOCK_ASSERT(p, MA_OWNED); 163 psp = p->p_sigacts; 164 mtx_assert(&psp->ps_mtx, MA_OWNED); 165 regs = td->td_frame; 166 oonstack = sigonstack(regs->tf_esp); 167 168 /* Allocate space for the signal handler context. */ 169 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 170 SIGISMEMBER(psp->ps_sigonstack, sig)) { 171 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 172 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 173 } else 174 fp = (struct l_rt_sigframe *)regs->tf_esp - 1; 175 mtx_unlock(&psp->ps_mtx); 176 177 /* Build the argument list for the signal handler. */ 178 sig = bsd_to_linux_signal(sig); 179 180 bzero(&frame, sizeof(frame)); 181 182 frame.sf_sig = sig; 183 frame.sf_siginfo = PTROUT(&fp->sf_si); 184 frame.sf_ucontext = PTROUT(&fp->sf_uc); 185 186 /* Fill in POSIX parts. */ 187 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig); 188 189 /* Build the signal context to be used by sigreturn. */ 190 frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 191 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 192 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 193 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 194 PROC_UNLOCK(p); 195 196 bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask); 197 198 frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask; 199 frame.sf_uc.uc_mcontext.sc_gs = rgs(); 200 frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs; 201 frame.sf_uc.uc_mcontext.sc_es = regs->tf_es; 202 frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds; 203 frame.sf_uc.uc_mcontext.sc_edi = regs->tf_edi; 204 frame.sf_uc.uc_mcontext.sc_esi = regs->tf_esi; 205 frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_ebp; 206 frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_ebx; 207 frame.sf_uc.uc_mcontext.sc_esp = regs->tf_esp; 208 frame.sf_uc.uc_mcontext.sc_edx = regs->tf_edx; 209 frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_ecx; 210 frame.sf_uc.uc_mcontext.sc_eax = regs->tf_eax; 211 frame.sf_uc.uc_mcontext.sc_eip = regs->tf_eip; 212 frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs; 213 frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags; 214 frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp; 215 frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss; 216 frame.sf_uc.uc_mcontext.sc_err = regs->tf_err; 217 frame.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 218 frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 219 220 if (copyout(&frame, fp, sizeof(frame)) != 0) { 221 /* 222 * Process has trashed its stack; give it an illegal 223 * instruction to halt it in its tracks. 224 */ 225 PROC_LOCK(p); 226 sigexit(td, SIGILL); 227 } 228 229 /* Build context to run handler in. */ 230 regs->tf_esp = PTROUT(fp); 231 regs->tf_eip = linux_vdso_rt_sigcode; 232 regs->tf_edi = PTROUT(catcher); 233 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 234 regs->tf_cs = _ucodesel; 235 regs->tf_ds = _udatasel; 236 regs->tf_es = _udatasel; 237 regs->tf_fs = _udatasel; 238 regs->tf_ss = _udatasel; 239 PROC_LOCK(p); 240 mtx_lock(&psp->ps_mtx); 241 } 242 243 /* 244 * Send an interrupt to process. 245 * 246 * Stack is set up to allow sigcode stored 247 * in u. to call routine, followed by kcall 248 * to sigreturn routine below. After sigreturn 249 * resets the signal mask, the stack, and the 250 * frame pointer, it returns to the user 251 * specified pc, psl. 252 */ 253 static void 254 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 255 { 256 struct thread *td = curthread; 257 struct proc *p = td->td_proc; 258 struct sigacts *psp; 259 struct trapframe *regs; 260 struct l_sigframe *fp, frame; 261 l_sigset_t lmask; 262 int sig; 263 int oonstack; 264 265 PROC_LOCK_ASSERT(p, MA_OWNED); 266 psp = p->p_sigacts; 267 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 268 mtx_assert(&psp->ps_mtx, MA_OWNED); 269 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 270 /* Signal handler installed with SA_SIGINFO. */ 271 linux_rt_sendsig(catcher, ksi, mask); 272 return; 273 } 274 regs = td->td_frame; 275 oonstack = sigonstack(regs->tf_esp); 276 277 /* Allocate space for the signal handler context. */ 278 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 279 SIGISMEMBER(psp->ps_sigonstack, sig)) { 280 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 281 td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 282 } else 283 fp = (struct l_sigframe *)regs->tf_esp - 1; 284 mtx_unlock(&psp->ps_mtx); 285 PROC_UNLOCK(p); 286 287 /* Build the argument list for the signal handler. */ 288 sig = bsd_to_linux_signal(sig); 289 290 bzero(&frame, sizeof(frame)); 291 292 frame.sf_sig = sig; 293 frame.sf_sigmask = *mask; 294 bsd_to_linux_sigset(mask, &lmask); 295 296 /* Build the signal context to be used by sigreturn. */ 297 frame.sf_sc.sc_mask = lmask.__mask; 298 frame.sf_sc.sc_gs = rgs(); 299 frame.sf_sc.sc_fs = regs->tf_fs; 300 frame.sf_sc.sc_es = regs->tf_es; 301 frame.sf_sc.sc_ds = regs->tf_ds; 302 frame.sf_sc.sc_edi = regs->tf_edi; 303 frame.sf_sc.sc_esi = regs->tf_esi; 304 frame.sf_sc.sc_ebp = regs->tf_ebp; 305 frame.sf_sc.sc_ebx = regs->tf_ebx; 306 frame.sf_sc.sc_esp = regs->tf_esp; 307 frame.sf_sc.sc_edx = regs->tf_edx; 308 frame.sf_sc.sc_ecx = regs->tf_ecx; 309 frame.sf_sc.sc_eax = regs->tf_eax; 310 frame.sf_sc.sc_eip = regs->tf_eip; 311 frame.sf_sc.sc_cs = regs->tf_cs; 312 frame.sf_sc.sc_eflags = regs->tf_eflags; 313 frame.sf_sc.sc_esp_at_signal = regs->tf_esp; 314 frame.sf_sc.sc_ss = regs->tf_ss; 315 frame.sf_sc.sc_err = regs->tf_err; 316 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr; 317 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno); 318 319 if (copyout(&frame, fp, sizeof(frame)) != 0) { 320 /* 321 * Process has trashed its stack; give it an illegal 322 * instruction to halt it in its tracks. 323 */ 324 PROC_LOCK(p); 325 sigexit(td, SIGILL); 326 } 327 328 /* Build context to run handler in. */ 329 regs->tf_esp = PTROUT(fp); 330 regs->tf_eip = linux_vdso_sigcode; 331 regs->tf_edi = PTROUT(catcher); 332 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 333 regs->tf_cs = _ucodesel; 334 regs->tf_ds = _udatasel; 335 regs->tf_es = _udatasel; 336 regs->tf_fs = _udatasel; 337 regs->tf_ss = _udatasel; 338 PROC_LOCK(p); 339 mtx_lock(&psp->ps_mtx); 340 } 341 342 /* 343 * System call to cleanup state after a signal 344 * has been taken. Reset signal mask and 345 * stack state from context left by sendsig (above). 346 * Return to previous pc and psl as specified by 347 * context left by sendsig. Check carefully to 348 * make sure that the user has not modified the 349 * psl to gain improper privileges or to cause 350 * a machine fault. 351 */ 352 int 353 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 354 { 355 struct l_sigframe frame; 356 struct trapframe *regs; 357 int eflags; 358 ksiginfo_t ksi; 359 360 regs = td->td_frame; 361 362 /* 363 * The trampoline code hands us the sigframe. 364 * It is unsafe to keep track of it ourselves, in the event that a 365 * program jumps out of a signal handler. 366 */ 367 if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 368 return (EFAULT); 369 370 /* Check for security violations. */ 371 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 372 eflags = frame.sf_sc.sc_eflags; 373 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 374 return (EINVAL); 375 376 /* 377 * Don't allow users to load a valid privileged %cs. Let the 378 * hardware check for invalid selectors, excess privilege in 379 * other selectors, invalid %eip's and invalid %esp's. 380 */ 381 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 382 if (!CS_SECURE(frame.sf_sc.sc_cs)) { 383 ksiginfo_init_trap(&ksi); 384 ksi.ksi_signo = SIGBUS; 385 ksi.ksi_code = BUS_OBJERR; 386 ksi.ksi_trapno = T_PROTFLT; 387 ksi.ksi_addr = (void *)regs->tf_eip; 388 trapsignal(td, &ksi); 389 return (EINVAL); 390 } 391 392 kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0); 393 394 /* Restore signal context. */ 395 /* %gs was restored by the trampoline. */ 396 regs->tf_fs = frame.sf_sc.sc_fs; 397 regs->tf_es = frame.sf_sc.sc_es; 398 regs->tf_ds = frame.sf_sc.sc_ds; 399 regs->tf_edi = frame.sf_sc.sc_edi; 400 regs->tf_esi = frame.sf_sc.sc_esi; 401 regs->tf_ebp = frame.sf_sc.sc_ebp; 402 regs->tf_ebx = frame.sf_sc.sc_ebx; 403 regs->tf_edx = frame.sf_sc.sc_edx; 404 regs->tf_ecx = frame.sf_sc.sc_ecx; 405 regs->tf_eax = frame.sf_sc.sc_eax; 406 regs->tf_eip = frame.sf_sc.sc_eip; 407 regs->tf_cs = frame.sf_sc.sc_cs; 408 regs->tf_eflags = eflags; 409 regs->tf_esp = frame.sf_sc.sc_esp_at_signal; 410 regs->tf_ss = frame.sf_sc.sc_ss; 411 412 return (EJUSTRETURN); 413 } 414 415 /* 416 * System call to cleanup state after a signal 417 * has been taken. Reset signal mask and 418 * stack state from context left by rt_sendsig (above). 419 * Return to previous pc and psl as specified by 420 * context left by sendsig. Check carefully to 421 * make sure that the user has not modified the 422 * psl to gain improper privileges or to cause 423 * a machine fault. 424 */ 425 int 426 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 427 { 428 struct l_ucontext uc; 429 struct l_sigcontext *context; 430 sigset_t bmask; 431 l_stack_t *lss; 432 stack_t ss; 433 struct trapframe *regs; 434 int eflags; 435 ksiginfo_t ksi; 436 437 regs = td->td_frame; 438 439 /* 440 * The trampoline code hands us the ucontext. 441 * It is unsafe to keep track of it ourselves, in the event that a 442 * program jumps out of a signal handler. 443 */ 444 if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 445 return (EFAULT); 446 447 context = &uc.uc_mcontext; 448 449 /* Check for security violations. */ 450 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 451 eflags = context->sc_eflags; 452 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 453 return (EINVAL); 454 455 /* 456 * Don't allow users to load a valid privileged %cs. Let the 457 * hardware check for invalid selectors, excess privilege in 458 * other selectors, invalid %eip's and invalid %esp's. 459 */ 460 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 461 if (!CS_SECURE(context->sc_cs)) { 462 ksiginfo_init_trap(&ksi); 463 ksi.ksi_signo = SIGBUS; 464 ksi.ksi_code = BUS_OBJERR; 465 ksi.ksi_trapno = T_PROTFLT; 466 ksi.ksi_addr = (void *)regs->tf_eip; 467 trapsignal(td, &ksi); 468 return (EINVAL); 469 } 470 471 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 472 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 473 474 /* Restore signal context. */ 475 /* %gs was restored by the trampoline. */ 476 regs->tf_fs = context->sc_fs; 477 regs->tf_es = context->sc_es; 478 regs->tf_ds = context->sc_ds; 479 regs->tf_edi = context->sc_edi; 480 regs->tf_esi = context->sc_esi; 481 regs->tf_ebp = context->sc_ebp; 482 regs->tf_ebx = context->sc_ebx; 483 regs->tf_edx = context->sc_edx; 484 regs->tf_ecx = context->sc_ecx; 485 regs->tf_eax = context->sc_eax; 486 regs->tf_eip = context->sc_eip; 487 regs->tf_cs = context->sc_cs; 488 regs->tf_eflags = eflags; 489 regs->tf_esp = context->sc_esp_at_signal; 490 regs->tf_ss = context->sc_ss; 491 492 /* Call sigaltstack & ignore results. */ 493 lss = &uc.uc_stack; 494 ss.ss_sp = PTRIN(lss->ss_sp); 495 ss.ss_size = lss->ss_size; 496 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 497 498 (void)kern_sigaltstack(td, &ss, NULL); 499 500 return (EJUSTRETURN); 501 } 502 503 static int 504 linux_fetch_syscall_args(struct thread *td) 505 { 506 struct proc *p; 507 struct trapframe *frame; 508 struct syscall_args *sa; 509 510 p = td->td_proc; 511 frame = td->td_frame; 512 sa = &td->td_sa; 513 514 sa->code = frame->tf_eax; 515 sa->original_code = sa->code; 516 sa->args[0] = frame->tf_ebx; 517 sa->args[1] = frame->tf_ecx; 518 sa->args[2] = frame->tf_edx; 519 sa->args[3] = frame->tf_esi; 520 sa->args[4] = frame->tf_edi; 521 sa->args[5] = frame->tf_ebp; /* Unconfirmed */ 522 523 if (sa->code >= p->p_sysent->sv_size) 524 /* nosys */ 525 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 526 else 527 sa->callp = &p->p_sysent->sv_table[sa->code]; 528 529 td->td_retval[0] = 0; 530 td->td_retval[1] = frame->tf_edx; 531 532 return (0); 533 } 534 535 static void 536 linux_set_syscall_retval(struct thread *td, int error) 537 { 538 struct trapframe *frame = td->td_frame; 539 540 cpu_set_syscall_retval(td, error); 541 542 if (__predict_false(error != 0)) { 543 if (error != ERESTART && error != EJUSTRETURN) 544 frame->tf_eax = bsd_to_linux_errno(error); 545 } 546 } 547 548 static void 549 linux_set_fork_retval(struct thread *td) 550 { 551 struct trapframe *frame = td->td_frame; 552 553 frame->tf_eax = 0; 554 } 555 556 /* 557 * exec_setregs may initialize some registers differently than Linux 558 * does, thus potentially confusing Linux binaries. If necessary, we 559 * override the exec_setregs default(s) here. 560 */ 561 static void 562 linux_exec_setregs(struct thread *td, struct image_params *imgp, 563 uintptr_t stack) 564 { 565 struct pcb *pcb = td->td_pcb; 566 567 exec_setregs(td, imgp, stack); 568 569 /* Linux sets %gs to 0, we default to _udatasel. */ 570 pcb->pcb_gs = 0; 571 load_gs(0); 572 573 pcb->pcb_initial_npxcw = __LINUX_NPXCW__; 574 } 575 576 struct sysentvec linux_sysvec = { 577 .sv_size = LINUX_SYS_MAXSYSCALL, 578 .sv_table = linux_sysent, 579 .sv_fixup = linux_fixup, 580 .sv_sendsig = linux_sendsig, 581 .sv_sigcode = &_binary_linux_vdso_so_o_start, 582 .sv_szsigcode = &linux_szsigcode, 583 .sv_name = "Linux a.out", 584 .sv_coredump = NULL, 585 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 586 .sv_minuser = VM_MIN_ADDRESS, 587 .sv_maxuser = VM_MAXUSER_ADDRESS, 588 .sv_usrstack = LINUX_USRSTACK, 589 .sv_psstrings = PS_STRINGS, 590 .sv_psstringssz = sizeof(struct ps_strings), 591 .sv_stackprot = VM_PROT_ALL, 592 .sv_copyout_strings = exec_copyout_strings, 593 .sv_setregs = linux_exec_setregs, 594 .sv_fixlimit = NULL, 595 .sv_maxssiz = NULL, 596 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 | 597 SV_SIG_DISCIGN | SV_SIG_WAITNDQ, 598 .sv_set_syscall_retval = linux_set_syscall_retval, 599 .sv_fetch_syscall_args = linux_fetch_syscall_args, 600 .sv_syscallnames = linux_syscallnames, 601 .sv_schedtail = linux_schedtail, 602 .sv_thread_detach = linux_thread_detach, 603 .sv_trap = NULL, 604 .sv_hwcap = NULL, 605 .sv_hwcap2 = NULL, 606 .sv_onexec = linux_on_exec_vmspace, 607 .sv_onexit = linux_on_exit, 608 .sv_ontdexit = linux_thread_dtor, 609 .sv_setid_allowed = &linux_setid_allowed_query, 610 .sv_set_fork_retval = linux_set_fork_retval, 611 }; 612 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec); 613 614 struct sysentvec elf_linux_sysvec = { 615 .sv_size = LINUX_SYS_MAXSYSCALL, 616 .sv_table = linux_sysent, 617 .sv_fixup = __elfN(freebsd_fixup), 618 .sv_sendsig = linux_sendsig, 619 .sv_sigcode = &_binary_linux_vdso_so_o_start, 620 .sv_szsigcode = &linux_szsigcode, 621 .sv_name = "Linux ELF32", 622 .sv_coredump = elf32_coredump, 623 .sv_elf_core_osabi = ELFOSABI_NONE, 624 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 625 .sv_elf_core_prepare_notes = __linuxN(prepare_notes), 626 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 627 .sv_minuser = VM_MIN_ADDRESS, 628 .sv_maxuser = VM_MAXUSER_ADDRESS, 629 .sv_usrstack = LINUX_USRSTACK, 630 .sv_psstrings = LINUX_PS_STRINGS, 631 .sv_psstringssz = sizeof(struct ps_strings), 632 .sv_stackprot = VM_PROT_ALL, 633 .sv_copyout_auxargs = __linuxN(copyout_auxargs), 634 .sv_copyout_strings = __linuxN(copyout_strings), 635 .sv_setregs = linux_exec_setregs, 636 .sv_fixlimit = NULL, 637 .sv_maxssiz = NULL, 638 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP | 639 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP, 640 .sv_set_syscall_retval = linux_set_syscall_retval, 641 .sv_fetch_syscall_args = linux_fetch_syscall_args, 642 .sv_syscallnames = NULL, 643 .sv_shared_page_base = LINUX_SHAREDPAGE, 644 .sv_shared_page_len = PAGE_SIZE, 645 .sv_schedtail = linux_schedtail, 646 .sv_thread_detach = linux_thread_detach, 647 .sv_trap = NULL, 648 .sv_hwcap = NULL, 649 .sv_hwcap2 = NULL, 650 .sv_onexec = linux_on_exec_vmspace, 651 .sv_onexit = linux_on_exit, 652 .sv_ontdexit = linux_thread_dtor, 653 .sv_setid_allowed = &linux_setid_allowed_query, 654 .sv_set_fork_retval = linux_set_fork_retval, 655 }; 656 657 static int 658 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 659 { 660 int error = 0; 661 662 if (SV_PROC_FLAG(p, SV_SHP) != 0) 663 error = linux_map_vdso(p, linux_vdso_obj, 664 linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp); 665 if (error == 0) 666 error = linux_on_exec(p, imgp); 667 return (error); 668 } 669 670 /* 671 * linux_vdso_install() and linux_exec_sysvec_init() must be called 672 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 673 */ 674 static void 675 linux_exec_sysvec_init(void *param) 676 { 677 l_uintptr_t *ktimekeep_base, *ktsc_selector; 678 struct sysentvec *sv; 679 ptrdiff_t tkoff; 680 681 sv = param; 682 /* Fill timekeep_base */ 683 exec_sysvec_init(sv); 684 685 tkoff = kern_timekeep_base - linux_vdso_base; 686 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 687 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 688 689 tkoff = kern_tsc_selector - linux_vdso_base; 690 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 691 *ktsc_selector = linux_vdso_tsc_selector_idx(); 692 if (bootverbose) 693 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector); 694 695 tkoff = kern_cpu_selector - linux_vdso_base; 696 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 697 *ktsc_selector = linux_vdso_cpu_selector_idx(); 698 if (bootverbose) 699 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector); 700 } 701 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 702 linux_exec_sysvec_init, &elf_linux_sysvec); 703 704 static void 705 linux_vdso_install(const void *param) 706 { 707 char *vdso_start = &_binary_linux_vdso_so_o_start; 708 char *vdso_end = &_binary_linux_vdso_so_o_end; 709 710 linux_szsigcode = vdso_end - vdso_start; 711 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 712 713 linux_vdso_base = LINUX_VDSOPAGE; 714 715 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 716 717 linux_vdso_obj = __elfN(linux_shared_page_init) 718 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 719 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 720 721 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 722 } 723 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 724 linux_vdso_install, NULL); 725 726 static void 727 linux_vdso_deinstall(const void *param) 728 { 729 730 __elfN(linux_shared_page_fini)(linux_vdso_obj, 731 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 732 } 733 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 734 linux_vdso_deinstall, NULL); 735 736 static void 737 linux_vdso_reloc(char *mapping, Elf_Addr offset) 738 { 739 const Elf_Shdr *shdr; 740 const Elf_Rel *rel; 741 const Elf_Ehdr *ehdr; 742 Elf_Addr *where; 743 Elf_Size rtype, symidx; 744 Elf_Addr addr, addend; 745 int i, relcnt; 746 747 MPASS(offset != 0); 748 749 relcnt = 0; 750 ehdr = (const Elf_Ehdr *)mapping; 751 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 752 for (i = 0; i < ehdr->e_shnum; i++) 753 { 754 switch (shdr[i].sh_type) { 755 case SHT_REL: 756 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset); 757 relcnt = shdr[i].sh_size / sizeof(*rel); 758 break; 759 case SHT_RELA: 760 printf("Linux i386 vDSO: unexpected Rela section\n"); 761 break; 762 } 763 } 764 765 for (i = 0; i < relcnt; i++, rel++) { 766 where = (Elf_Addr *)(mapping + rel->r_offset); 767 addend = *where; 768 rtype = ELF_R_TYPE(rel->r_info); 769 symidx = ELF_R_SYM(rel->r_info); 770 771 switch (rtype) { 772 case R_386_NONE: /* none */ 773 break; 774 775 case R_386_RELATIVE: /* B + A */ 776 addr = (Elf_Addr)PTROUT(offset + addend); 777 if (*where != addr) 778 *where = addr; 779 break; 780 781 case R_386_IRELATIVE: 782 printf("Linux i386 vDSO: unexpected ifunc relocation, " 783 "symbol index %d\n", symidx); 784 break; 785 default: 786 printf("Linux i386 vDSO: unexpected relocation type %d, " 787 "symbol index %d\n", rtype, symidx); 788 } 789 } 790 } 791 792 static Elf_Brandnote linux_brandnote = { 793 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 794 .hdr.n_descsz = 16, /* XXX at least 16 */ 795 .hdr.n_type = 1, 796 .vendor = GNU_ABI_VENDOR, 797 .flags = BN_TRANSLATE_OSREL, 798 .trans_osrel = linux_trans_osrel 799 }; 800 801 static Elf32_Brandinfo linux_brand = { 802 .brand = ELFOSABI_LINUX, 803 .machine = EM_386, 804 .compat_3_brand = "Linux", 805 .interp_path = "/lib/ld-linux.so.1", 806 .sysvec = &elf_linux_sysvec, 807 .interp_newpath = NULL, 808 .brand_note = &linux_brandnote, 809 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 810 }; 811 812 static Elf32_Brandinfo linux_glibc2brand = { 813 .brand = ELFOSABI_LINUX, 814 .machine = EM_386, 815 .compat_3_brand = "Linux", 816 .interp_path = "/lib/ld-linux.so.2", 817 .sysvec = &elf_linux_sysvec, 818 .interp_newpath = NULL, 819 .brand_note = &linux_brandnote, 820 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 821 }; 822 823 static Elf32_Brandinfo linux_muslbrand = { 824 .brand = ELFOSABI_LINUX, 825 .machine = EM_386, 826 .compat_3_brand = "Linux", 827 .interp_path = "/lib/ld-musl-i386.so.1", 828 .sysvec = &elf_linux_sysvec, 829 .interp_newpath = NULL, 830 .brand_note = &linux_brandnote, 831 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE | 832 LINUX_BI_FUTEX_REQUEUE 833 }; 834 835 Elf32_Brandinfo *linux_brandlist[] = { 836 &linux_brand, 837 &linux_glibc2brand, 838 &linux_muslbrand, 839 NULL 840 }; 841 842 static int 843 linux_elf_modevent(module_t mod, int type, void *data) 844 { 845 Elf32_Brandinfo **brandinfo; 846 int error; 847 struct linux_ioctl_handler **lihp; 848 849 error = 0; 850 851 switch(type) { 852 case MOD_LOAD: 853 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 854 ++brandinfo) 855 if (elf32_insert_brand_entry(*brandinfo) < 0) 856 error = EINVAL; 857 if (error == 0) { 858 SET_FOREACH(lihp, linux_ioctl_handler_set) 859 linux_ioctl_register_handler(*lihp); 860 linux_dev_shm_create(); 861 linux_osd_jail_register(); 862 linux_netlink_register(); 863 stclohz = (stathz ? stathz : hz); 864 if (bootverbose) 865 printf("Linux ELF exec handler installed\n"); 866 } else 867 printf("cannot insert Linux ELF brand handler\n"); 868 break; 869 case MOD_UNLOAD: 870 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 871 ++brandinfo) 872 if (elf32_brand_inuse(*brandinfo)) 873 error = EBUSY; 874 if (error == 0) { 875 for (brandinfo = &linux_brandlist[0]; 876 *brandinfo != NULL; ++brandinfo) 877 if (elf32_remove_brand_entry(*brandinfo) < 0) 878 error = EINVAL; 879 } 880 if (error == 0) { 881 SET_FOREACH(lihp, linux_ioctl_handler_set) 882 linux_ioctl_unregister_handler(*lihp); 883 linux_netlink_deregister(); 884 linux_dev_shm_destroy(); 885 linux_osd_jail_deregister(); 886 if (bootverbose) 887 printf("Linux ELF exec handler removed\n"); 888 } else 889 printf("Could not deinstall ELF interpreter entry\n"); 890 break; 891 default: 892 return (EOPNOTSUPP); 893 } 894 return (error); 895 } 896 897 static moduledata_t linux_elf_mod = { 898 "linuxelf", 899 linux_elf_modevent, 900 0 901 }; 902 903 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 904 MODULE_DEPEND(linuxelf, netlink, 1, 1, 1); 905 FEATURE(linux, "Linux 32bit support"); 906