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