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