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