1 /*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2003 Peter Wemm 4 * Copyright (c) 2002 Doug Rabson 5 * Copyright (c) 1998-1999 Andrew Gallatin 6 * Copyright (c) 1994-1996 Søren Schmidt 7 * All rights reserved. 8 * Copyright (c) 2013, 2021 Dmitry Chagin <dchagin@FreeBSD.org> 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer 15 * in this position and unchanged. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #define __ELF_WORD_SIZE 64 36 37 #include <sys/param.h> 38 #include <sys/exec.h> 39 #include <sys/imgact.h> 40 #include <sys/imgact_elf.h> 41 #include <sys/kernel.h> 42 #include <sys/ktr.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/module.h> 46 #include <sys/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/stddef.h> 49 #include <sys/syscallsubr.h> 50 #include <sys/sysctl.h> 51 #include <sys/sysent.h> 52 53 #include <vm/pmap.h> 54 #include <vm/vm.h> 55 #include <vm/vm_map.h> 56 #include <vm/vm_page.h> 57 58 #include <machine/cpu.h> 59 #include <machine/md_var.h> 60 #include <machine/pcb.h> 61 #include <machine/specialreg.h> 62 #include <machine/trap.h> 63 64 #include <x86/linux/linux_x86.h> 65 #include <amd64/linux/linux.h> 66 #include <amd64/linux/linux_proto.h> 67 #include <compat/linux/linux_elf.h> 68 #include <compat/linux/linux_emul.h> 69 #include <compat/linux/linux_fork.h> 70 #include <compat/linux/linux_ioctl.h> 71 #include <compat/linux/linux_mib.h> 72 #include <compat/linux/linux_misc.h> 73 #include <compat/linux/linux_signal.h> 74 #include <compat/linux/linux_sysproto.h> 75 #include <compat/linux/linux_util.h> 76 #include <compat/linux/linux_vdso.h> 77 78 #include <x86/linux/linux_x86_sigframe.h> 79 80 _Static_assert(sizeof(struct l_fpstate) == 81 sizeof(__typeof(((mcontext_t *)0)->mc_fpstate)), 82 "fxsave area size incorrect"); 83 84 MODULE_VERSION(linux64, 1); 85 86 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 87 #define LINUX_VDSOPAGE_LA48 (VM_MAXUSER_ADDRESS_LA48 - \ 88 LINUX_VDSOPAGE_SIZE) 89 #define LINUX_SHAREDPAGE_LA48 (LINUX_VDSOPAGE_LA48 - PAGE_SIZE) 90 /* 91 * PAGE_SIZE - the size 92 * of the native SHAREDPAGE 93 */ 94 #define LINUX_USRSTACK_LA48 LINUX_SHAREDPAGE_LA48 95 #define LINUX_PS_STRINGS_LA48 (LINUX_USRSTACK_LA48 - \ 96 sizeof(struct ps_strings)) 97 98 static int linux_szsigcode; 99 static vm_object_t linux_vdso_obj; 100 static char *linux_vdso_mapping; 101 extern char _binary_linux_vdso_so_o_start; 102 extern char _binary_linux_vdso_so_o_end; 103 static vm_offset_t linux_vdso_base; 104 105 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 106 extern const char *linux_syscallnames[]; 107 108 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 109 110 static void linux_vdso_install(const void *param); 111 static void linux_vdso_deinstall(const void *param); 112 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 113 static void linux_set_syscall_retval(struct thread *td, int error); 114 static int linux_fetch_syscall_args(struct thread *td); 115 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 116 uintptr_t stack); 117 static void linux_exec_sysvec_init(void *param); 118 static int linux_on_exec_vmspace(struct proc *p, 119 struct image_params *imgp); 120 static void linux_set_fork_retval(struct thread *td); 121 static int linux_vsyscall(struct thread *td); 122 123 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode); 124 LINUX_VDSO_SYM_CHAR(linux_platform); 125 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 126 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector); 127 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector); 128 129 /* 130 * According to the Intel x86 ISA 64-bit syscall 131 * saves %rip to %rcx and rflags to %r11. Registers on syscall entry: 132 * %rax system call number 133 * %rcx return address 134 * %r11 saved rflags 135 * %rdi arg1 136 * %rsi arg2 137 * %rdx arg3 138 * %r10 arg4 139 * %r8 arg5 140 * %r9 arg6 141 * 142 * Then FreeBSD fast_syscall() move registers: 143 * %rcx -> trapframe.tf_rip 144 * %r10 -> trapframe.tf_rcx 145 */ 146 static int 147 linux_fetch_syscall_args(struct thread *td) 148 { 149 struct proc *p; 150 struct trapframe *frame; 151 struct syscall_args *sa; 152 153 p = td->td_proc; 154 frame = td->td_frame; 155 sa = &td->td_sa; 156 157 sa->args[0] = frame->tf_rdi; 158 sa->args[1] = frame->tf_rsi; 159 sa->args[2] = frame->tf_rdx; 160 sa->args[3] = frame->tf_rcx; 161 sa->args[4] = frame->tf_r8; 162 sa->args[5] = frame->tf_r9; 163 sa->code = frame->tf_rax; 164 sa->original_code = sa->code; 165 166 if (sa->code >= p->p_sysent->sv_size) 167 /* nosys */ 168 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 169 else 170 sa->callp = &p->p_sysent->sv_table[sa->code]; 171 172 /* Restore r10 earlier to avoid doing this multiply times. */ 173 frame->tf_r10 = frame->tf_rcx; 174 /* Restore %rcx for machine context. */ 175 frame->tf_rcx = frame->tf_rip; 176 177 td->td_retval[0] = 0; 178 return (0); 179 } 180 181 static void 182 linux_set_syscall_retval(struct thread *td, int error) 183 { 184 struct trapframe *frame; 185 186 frame = td->td_frame; 187 188 switch (error) { 189 case 0: 190 frame->tf_rax = td->td_retval[0]; 191 break; 192 193 case ERESTART: 194 /* 195 * Reconstruct pc, we know that 'syscall' is 2 bytes, 196 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes. 197 * We saved this in tf_err. 198 * 199 */ 200 frame->tf_rip -= frame->tf_err; 201 break; 202 203 case EJUSTRETURN: 204 break; 205 206 default: 207 frame->tf_rax = bsd_to_linux_errno(error); 208 break; 209 } 210 211 /* 212 * Differently from FreeBSD native ABI, on Linux only %rcx 213 * and %r11 values are not preserved across the syscall. 214 * Require full context restore to get all registers except 215 * those two restored at return to usermode. 216 */ 217 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 218 } 219 220 static void 221 linux_set_fork_retval(struct thread *td) 222 { 223 struct trapframe *frame = td->td_frame; 224 225 frame->tf_rax = 0; 226 } 227 228 void 229 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos) 230 { 231 232 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 233 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature); 234 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2()); 235 AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform)); 236 } 237 238 /* 239 * Reset registers to default values on exec. 240 */ 241 static void 242 linux_exec_setregs(struct thread *td, struct image_params *imgp, 243 uintptr_t stack) 244 { 245 struct trapframe *regs; 246 struct pcb *pcb; 247 register_t saved_rflags; 248 249 regs = td->td_frame; 250 pcb = td->td_pcb; 251 252 if (td->td_proc->p_md.md_ldt != NULL) 253 user_ldt_free(td); 254 255 pcb->pcb_fsbase = 0; 256 pcb->pcb_gsbase = 0; 257 clear_pcb_flags(pcb, PCB_32BIT); 258 pcb->pcb_initial_fpucw = __LINUX_NPXCW__; 259 set_pcb_flags(pcb, PCB_FULL_IRET); 260 261 saved_rflags = regs->tf_rflags & PSL_T; 262 bzero((char *)regs, sizeof(struct trapframe)); 263 regs->tf_rip = imgp->entry_addr; 264 regs->tf_rsp = stack; 265 regs->tf_rflags = PSL_USER | saved_rflags; 266 regs->tf_ss = _udatasel; 267 regs->tf_cs = _ucodesel; 268 regs->tf_ds = _udatasel; 269 regs->tf_es = _udatasel; 270 regs->tf_fs = _ufssel; 271 regs->tf_gs = _ugssel; 272 regs->tf_flags = TF_HASSEGS; 273 274 x86_clear_dbregs(pcb); 275 276 /* 277 * Drop the FP state if we hold it, so that the process gets a 278 * clean FP state if it uses the FPU again. 279 */ 280 fpstate_drop(td); 281 } 282 283 static int 284 linux_fxrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc) 285 { 286 struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0]; 287 int error; 288 289 error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate)); 290 if (error != 0) 291 return (error); 292 bzero(&fp->sv_pad[0], sizeof(fp->sv_pad)); 293 return (set_fpcontext(td, mcp, NULL, 0)); 294 } 295 296 static int 297 linux_xrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc) 298 { 299 struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0]; 300 char *xfpustate; 301 struct proc *p; 302 uint32_t magic2; 303 int error; 304 305 p = td->td_proc; 306 mcp->mc_xfpustate_len = cpu_max_ext_state_size - sizeof(struct savefpu); 307 308 /* Legacy region of an xsave area. */ 309 error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate)); 310 if (error != 0) 311 return (error); 312 bzero(&fp->sv_pad[0], sizeof(fp->sv_pad)); 313 314 /* Extended region of an xsave area. */ 315 sc->sc_fpstate += sizeof(mcp->mc_fpstate); 316 xfpustate = (char *)fpu_save_area_alloc(); 317 error = copyin(PTRIN(sc->sc_fpstate), xfpustate, mcp->mc_xfpustate_len); 318 if (error != 0) { 319 fpu_save_area_free((struct savefpu *)xfpustate); 320 uprintf("pid %d (%s): linux xrstor failed\n", p->p_pid, 321 td->td_name); 322 return (error); 323 } 324 325 /* Linux specific end of xsave area marker. */ 326 sc->sc_fpstate += mcp->mc_xfpustate_len; 327 error = copyin(PTRIN(sc->sc_fpstate), &magic2, LINUX_FP_XSTATE_MAGIC2_SIZE); 328 if (error != 0 || magic2 != LINUX_FP_XSTATE_MAGIC2) { 329 fpu_save_area_free((struct savefpu *)xfpustate); 330 uprintf("pid %d (%s): sigreturn magic2 0x%x error %d\n", 331 p->p_pid, td->td_name, magic2, error); 332 return (error); 333 } 334 335 error = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len); 336 fpu_save_area_free((struct savefpu *)xfpustate); 337 if (error != 0) { 338 uprintf("pid %d (%s): sigreturn set_fpcontext error %d\n", 339 p->p_pid, td->td_name, error); 340 } 341 return (error); 342 } 343 344 static int 345 linux_copyin_fpstate(struct thread *td, struct l_ucontext *uc) 346 { 347 mcontext_t mc; 348 349 bzero(&mc, sizeof(mc)); 350 mc.mc_ownedfp = _MC_FPOWNED_FPU; 351 mc.mc_fpformat = _MC_FPFMT_XMM; 352 353 if ((uc->uc_flags & LINUX_UC_FP_XSTATE) != 0) 354 return (linux_xrstor(td, &mc, &uc->uc_mcontext)); 355 else 356 return (linux_fxrstor(td, &mc, &uc->uc_mcontext)); 357 } 358 359 /* 360 * Copied from amd64/amd64/machdep.c 361 */ 362 int 363 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 364 { 365 struct proc *p; 366 struct l_rt_sigframe sf; 367 struct l_sigcontext *context; 368 struct trapframe *regs; 369 unsigned long rflags; 370 sigset_t bmask; 371 int error; 372 ksiginfo_t ksi; 373 374 regs = td->td_frame; 375 error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf)); 376 if (error != 0) 377 return (error); 378 379 p = td->td_proc; 380 context = &sf.sf_uc.uc_mcontext; 381 rflags = context->sc_rflags; 382 383 /* 384 * Don't allow users to change privileged or reserved flags. 385 */ 386 /* 387 * XXX do allow users to change the privileged flag PSL_RF. 388 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers 389 * should sometimes set it there too. tf_rflags is kept in 390 * the signal context during signal handling and there is no 391 * other place to remember it, so the PSL_RF bit may be 392 * corrupted by the signal handler without us knowing. 393 * Corruption of the PSL_RF bit at worst causes one more or 394 * one less debugger trap, so allowing it is fairly harmless. 395 */ 396 if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 397 uprintf("pid %d comm %s linux mangled rflags %#lx\n", 398 p->p_pid, p->p_comm, rflags); 399 return (EINVAL); 400 } 401 402 /* 403 * Don't allow users to load a valid privileged %cs. Let the 404 * hardware check for invalid selectors, excess privilege in 405 * other selectors, invalid %eip's and invalid %esp's. 406 */ 407 if (!CS_SECURE(context->sc_cs)) { 408 uprintf("pid %d comm %s linux mangled cs %#x\n", 409 p->p_pid, p->p_comm, context->sc_cs); 410 ksiginfo_init_trap(&ksi); 411 ksi.ksi_signo = SIGBUS; 412 ksi.ksi_code = BUS_OBJERR; 413 ksi.ksi_trapno = T_PROTFLT; 414 ksi.ksi_addr = (void *)regs->tf_rip; 415 trapsignal(td, &ksi); 416 return (EINVAL); 417 } 418 419 linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask); 420 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 421 422 regs->tf_rdi = context->sc_rdi; 423 regs->tf_rsi = context->sc_rsi; 424 regs->tf_rdx = context->sc_rdx; 425 regs->tf_rbp = context->sc_rbp; 426 regs->tf_rbx = context->sc_rbx; 427 regs->tf_rcx = context->sc_rcx; 428 regs->tf_rax = context->sc_rax; 429 regs->tf_rip = context->sc_rip; 430 regs->tf_rsp = context->sc_rsp; 431 regs->tf_r8 = context->sc_r8; 432 regs->tf_r9 = context->sc_r9; 433 regs->tf_r10 = context->sc_r10; 434 regs->tf_r11 = context->sc_r11; 435 regs->tf_r12 = context->sc_r12; 436 regs->tf_r13 = context->sc_r13; 437 regs->tf_r14 = context->sc_r14; 438 regs->tf_r15 = context->sc_r15; 439 regs->tf_cs = context->sc_cs; 440 regs->tf_err = context->sc_err; 441 regs->tf_rflags = rflags; 442 443 error = linux_copyin_fpstate(td, &sf.sf_uc); 444 if (error != 0) { 445 uprintf("pid %d comm %s linux can't restore fpu state %d\n", 446 p->p_pid, p->p_comm, error); 447 return (error); 448 } 449 450 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 451 return (EJUSTRETURN); 452 } 453 454 static int 455 linux_fxsave(mcontext_t *mcp, void *ufp) 456 { 457 struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0]; 458 459 bzero(&fx->reserved2[0], sizeof(fx->reserved2)); 460 return (copyout(fx, ufp, sizeof(*fx))); 461 } 462 463 static int 464 linux_xsave(mcontext_t *mcp, char *xfpusave, char *ufp) 465 { 466 struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0]; 467 uint32_t magic2; 468 int error; 469 470 /* Legacy region of an xsave area. */ 471 fx->sw_reserved.magic1 = LINUX_FP_XSTATE_MAGIC1; 472 fx->sw_reserved.xstate_size = mcp->mc_xfpustate_len + sizeof(*fx); 473 fx->sw_reserved.extended_size = fx->sw_reserved.xstate_size + 474 LINUX_FP_XSTATE_MAGIC2_SIZE; 475 fx->sw_reserved.xfeatures = xsave_mask; 476 477 error = copyout(fx, ufp, sizeof(*fx)); 478 if (error != 0) 479 return (error); 480 ufp += sizeof(*fx); 481 482 /* Extended region of an xsave area. */ 483 error = copyout(xfpusave, ufp, mcp->mc_xfpustate_len); 484 if (error != 0) 485 return (error); 486 487 /* Linux specific end of xsave area marker. */ 488 ufp += mcp->mc_xfpustate_len; 489 magic2 = LINUX_FP_XSTATE_MAGIC2; 490 return (copyout(&magic2, ufp, LINUX_FP_XSTATE_MAGIC2_SIZE)); 491 } 492 493 static int 494 linux_copyout_fpstate(struct thread *td, struct l_ucontext *uc, char **sp) 495 { 496 size_t xfpusave_len; 497 char *xfpusave; 498 mcontext_t mc; 499 char *ufp = *sp; 500 501 get_fpcontext(td, &mc, &xfpusave, &xfpusave_len); 502 KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present")); 503 504 /* Room for fxsave area. */ 505 ufp -= sizeof(struct l_fpstate); 506 if (xfpusave != NULL) { 507 /* Room for xsave area. */ 508 ufp -= (xfpusave_len + LINUX_FP_XSTATE_MAGIC2_SIZE); 509 uc->uc_flags |= LINUX_UC_FP_XSTATE; 510 } 511 *sp = ufp = (char *)((unsigned long)ufp & ~0x3Ful); 512 513 if (xfpusave != NULL) 514 return (linux_xsave(&mc, xfpusave, ufp)); 515 else 516 return (linux_fxsave(&mc, ufp)); 517 } 518 519 /* 520 * copied from amd64/amd64/machdep.c 521 * 522 * Send an interrupt to process. 523 */ 524 static void 525 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 526 { 527 struct l_rt_sigframe sf, *sfp; 528 struct proc *p; 529 struct thread *td; 530 struct sigacts *psp; 531 char *sp; 532 struct trapframe *regs; 533 int sig, code; 534 int oonstack, issiginfo; 535 536 td = curthread; 537 p = td->td_proc; 538 PROC_LOCK_ASSERT(p, MA_OWNED); 539 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 540 psp = p->p_sigacts; 541 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig); 542 code = ksi->ksi_code; 543 mtx_assert(&psp->ps_mtx, MA_OWNED); 544 regs = td->td_frame; 545 oonstack = sigonstack(regs->tf_rsp); 546 547 LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u", 548 catcher, sig, mask, code); 549 550 bzero(&sf, sizeof(sf)); 551 sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 552 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 553 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 554 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 555 556 /* Allocate space for the signal handler context. */ 557 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 558 SIGISMEMBER(psp->ps_sigonstack, sig)) { 559 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size; 560 } else 561 sp = (char *)regs->tf_rsp - 128; 562 563 mtx_unlock(&psp->ps_mtx); 564 PROC_UNLOCK(p); 565 566 if (linux_copyout_fpstate(td, &sf.sf_uc, &sp) != 0) { 567 uprintf("pid %d comm %s linux can't save fpu state, killing\n", 568 p->p_pid, p->p_comm); 569 PROC_LOCK(p); 570 sigexit(td, SIGILL); 571 } 572 sf.sf_uc.uc_mcontext.sc_fpstate = (register_t)sp; 573 574 /* Make room, keeping the stack aligned. */ 575 sp -= sizeof(struct l_rt_sigframe); 576 sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul); 577 578 /* Save user context. */ 579 bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask); 580 sf.sf_uc.uc_mcontext.sc_mask = sf.sf_uc.uc_sigmask; 581 sf.sf_uc.uc_mcontext.sc_rdi = regs->tf_rdi; 582 sf.sf_uc.uc_mcontext.sc_rsi = regs->tf_rsi; 583 sf.sf_uc.uc_mcontext.sc_rdx = regs->tf_rdx; 584 sf.sf_uc.uc_mcontext.sc_rbp = regs->tf_rbp; 585 sf.sf_uc.uc_mcontext.sc_rbx = regs->tf_rbx; 586 sf.sf_uc.uc_mcontext.sc_rcx = regs->tf_rcx; 587 sf.sf_uc.uc_mcontext.sc_rax = regs->tf_rax; 588 sf.sf_uc.uc_mcontext.sc_rip = regs->tf_rip; 589 sf.sf_uc.uc_mcontext.sc_rsp = regs->tf_rsp; 590 sf.sf_uc.uc_mcontext.sc_r8 = regs->tf_r8; 591 sf.sf_uc.uc_mcontext.sc_r9 = regs->tf_r9; 592 sf.sf_uc.uc_mcontext.sc_r10 = regs->tf_r10; 593 sf.sf_uc.uc_mcontext.sc_r11 = regs->tf_r11; 594 sf.sf_uc.uc_mcontext.sc_r12 = regs->tf_r12; 595 sf.sf_uc.uc_mcontext.sc_r13 = regs->tf_r13; 596 sf.sf_uc.uc_mcontext.sc_r14 = regs->tf_r14; 597 sf.sf_uc.uc_mcontext.sc_r15 = regs->tf_r15; 598 sf.sf_uc.uc_mcontext.sc_cs = regs->tf_cs; 599 sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags; 600 sf.sf_uc.uc_mcontext.sc_err = regs->tf_err; 601 sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 602 sf.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 603 604 /* Translate the signal. */ 605 sig = bsd_to_linux_signal(sig); 606 /* Fill in POSIX parts. */ 607 siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig); 608 609 /* Copy the sigframe out to the user's stack. */ 610 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 611 uprintf("pid %d comm %s has trashed its stack, killing\n", 612 p->p_pid, p->p_comm); 613 PROC_LOCK(p); 614 sigexit(td, SIGILL); 615 } 616 617 fpstate_drop(td); 618 /* Build the argument list for the signal handler. */ 619 regs->tf_rdi = sig; /* arg 1 in %rdi */ 620 regs->tf_rax = 0; 621 if (issiginfo) { 622 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */ 623 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */ 624 } else { 625 regs->tf_rsi = 0; 626 regs->tf_rdx = 0; 627 } 628 regs->tf_rcx = (register_t)catcher; 629 regs->tf_rsp = (long)sfp; 630 regs->tf_rip = linux_rt_sigcode; 631 regs->tf_rflags &= ~(PSL_T | PSL_D); 632 regs->tf_cs = _ucodesel; 633 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 634 PROC_LOCK(p); 635 mtx_lock(&psp->ps_mtx); 636 } 637 638 #define LINUX_VSYSCALL_START (-10UL << 20) 639 #define LINUX_VSYSCALL_SZ 1024 640 641 const unsigned long linux_vsyscall_vector[] = { 642 LINUX_SYS_gettimeofday, 643 LINUX_SYS_linux_time, 644 LINUX_SYS_linux_getcpu, 645 }; 646 647 static int 648 linux_vsyscall(struct thread *td) 649 { 650 struct trapframe *frame; 651 uint64_t retqaddr; 652 int code, traced; 653 int error; 654 655 frame = td->td_frame; 656 657 /* Check %rip for vsyscall area. */ 658 if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START)) 659 return (EINVAL); 660 if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0) 661 return (EINVAL); 662 code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ; 663 if (code >= nitems(linux_vsyscall_vector)) 664 return (EINVAL); 665 666 /* 667 * vsyscall called as callq *(%rax), so we must 668 * use return address from %rsp and also fixup %rsp. 669 */ 670 error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr)); 671 if (error) 672 return (error); 673 674 frame->tf_rip = retqaddr; 675 frame->tf_rax = linux_vsyscall_vector[code]; 676 frame->tf_rsp += 8; 677 678 traced = (frame->tf_flags & PSL_T); 679 680 amd64_syscall(td, traced); 681 682 return (0); 683 } 684 685 struct sysentvec elf_linux_sysvec = { 686 .sv_size = LINUX_SYS_MAXSYSCALL, 687 .sv_table = linux_sysent, 688 .sv_fixup = __elfN(freebsd_fixup), 689 .sv_sendsig = linux_rt_sendsig, 690 .sv_sigcode = &_binary_linux_vdso_so_o_start, 691 .sv_szsigcode = &linux_szsigcode, 692 .sv_name = "Linux ELF64", 693 .sv_coredump = elf64_coredump, 694 .sv_elf_core_osabi = ELFOSABI_NONE, 695 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 696 .sv_elf_core_prepare_notes = linux64_prepare_notes, 697 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 698 .sv_minuser = VM_MIN_ADDRESS, 699 .sv_maxuser = VM_MAXUSER_ADDRESS_LA48, 700 .sv_usrstack = LINUX_USRSTACK_LA48, 701 .sv_psstrings = LINUX_PS_STRINGS_LA48, 702 .sv_psstringssz = sizeof(struct ps_strings), 703 .sv_stackprot = VM_PROT_ALL, 704 .sv_copyout_auxargs = __linuxN(copyout_auxargs), 705 .sv_copyout_strings = __linuxN(copyout_strings), 706 .sv_setregs = linux_exec_setregs, 707 .sv_fixlimit = NULL, 708 .sv_maxssiz = NULL, 709 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 710 SV_SIG_WAITNDQ | SV_TIMEKEEP, 711 .sv_set_syscall_retval = linux_set_syscall_retval, 712 .sv_fetch_syscall_args = linux_fetch_syscall_args, 713 .sv_syscallnames = linux_syscallnames, 714 .sv_shared_page_base = LINUX_SHAREDPAGE_LA48, 715 .sv_shared_page_len = PAGE_SIZE, 716 .sv_schedtail = linux_schedtail, 717 .sv_thread_detach = linux_thread_detach, 718 .sv_trap = linux_vsyscall, 719 .sv_hwcap = NULL, 720 .sv_hwcap2 = NULL, 721 .sv_onexec = linux_on_exec_vmspace, 722 .sv_onexit = linux_on_exit, 723 .sv_ontdexit = linux_thread_dtor, 724 .sv_setid_allowed = &linux_setid_allowed_query, 725 .sv_set_fork_retval = linux_set_fork_retval, 726 }; 727 728 static int 729 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 730 { 731 int error; 732 733 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 734 LINUX_VDSOPAGE_SIZE, imgp); 735 if (error == 0) 736 error = linux_on_exec(p, imgp); 737 return (error); 738 } 739 740 /* 741 * linux_vdso_install() and linux_exec_sysvec_init() must be called 742 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 743 */ 744 static void 745 linux_exec_sysvec_init(void *param) 746 { 747 l_uintptr_t *ktimekeep_base, *ktsc_selector; 748 struct sysentvec *sv; 749 ptrdiff_t tkoff; 750 751 sv = param; 752 amd64_lower_shared_page(sv); 753 /* Fill timekeep_base */ 754 exec_sysvec_init(sv); 755 756 tkoff = kern_timekeep_base - linux_vdso_base; 757 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 758 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 759 760 tkoff = kern_tsc_selector - linux_vdso_base; 761 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 762 *ktsc_selector = linux_vdso_tsc_selector_idx(); 763 if (bootverbose) 764 printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector); 765 766 tkoff = kern_cpu_selector - linux_vdso_base; 767 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 768 *ktsc_selector = linux_vdso_cpu_selector_idx(); 769 if (bootverbose) 770 printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector); 771 } 772 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 773 linux_exec_sysvec_init, &elf_linux_sysvec); 774 775 static void 776 linux_vdso_install(const void *param) 777 { 778 char *vdso_start = &_binary_linux_vdso_so_o_start; 779 char *vdso_end = &_binary_linux_vdso_so_o_end; 780 781 linux_szsigcode = vdso_end - vdso_start; 782 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 783 784 linux_vdso_base = LINUX_VDSOPAGE_LA48; 785 if (hw_lower_amd64_sharedpage != 0) 786 linux_vdso_base -= PAGE_SIZE; 787 788 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 789 790 linux_vdso_obj = __elfN(linux_shared_page_init) 791 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 792 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 793 794 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 795 } 796 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 797 linux_vdso_install, NULL); 798 799 static void 800 linux_vdso_deinstall(const void *param) 801 { 802 803 __elfN(linux_shared_page_fini)(linux_vdso_obj, 804 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 805 } 806 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 807 linux_vdso_deinstall, NULL); 808 809 static void 810 linux_vdso_reloc(char *mapping, Elf_Addr offset) 811 { 812 const Elf_Ehdr *ehdr; 813 const Elf_Shdr *shdr; 814 Elf64_Addr *where, val; 815 Elf_Size rtype, symidx; 816 const Elf_Rela *rela; 817 Elf_Addr addr, addend; 818 int relacnt; 819 int i, j; 820 821 MPASS(offset != 0); 822 823 relacnt = 0; 824 ehdr = (const Elf_Ehdr *)mapping; 825 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 826 for (i = 0; i < ehdr->e_shnum; i++) 827 { 828 switch (shdr[i].sh_type) { 829 case SHT_REL: 830 printf("Linux x86_64 vDSO: unexpected Rel section\n"); 831 break; 832 case SHT_RELA: 833 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); 834 relacnt = shdr[i].sh_size / sizeof(*rela); 835 } 836 } 837 838 for (j = 0; j < relacnt; j++, rela++) { 839 where = (Elf_Addr *)(mapping + rela->r_offset); 840 addend = rela->r_addend; 841 rtype = ELF_R_TYPE(rela->r_info); 842 symidx = ELF_R_SYM(rela->r_info); 843 844 switch (rtype) { 845 case R_X86_64_NONE: /* none */ 846 break; 847 848 case R_X86_64_RELATIVE: /* B + A */ 849 addr = (Elf_Addr)(offset + addend); 850 val = addr; 851 if (*where != val) 852 *where = val; 853 break; 854 case R_X86_64_IRELATIVE: 855 printf("Linux x86_64 vDSO: unexpected ifunc relocation, " 856 "symbol index %ld\n", symidx); 857 break; 858 default: 859 printf("Linux x86_64 vDSO: unexpected relocation type %ld, " 860 "symbol index %ld\n", rtype, symidx); 861 } 862 } 863 } 864 865 static Elf_Brandnote linux64_brandnote = { 866 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 867 .hdr.n_descsz = 16, 868 .hdr.n_type = 1, 869 .vendor = GNU_ABI_VENDOR, 870 .flags = BN_TRANSLATE_OSREL, 871 .trans_osrel = linux_trans_osrel 872 }; 873 874 static Elf64_Brandinfo linux_glibc2brand = { 875 .brand = ELFOSABI_LINUX, 876 .machine = EM_X86_64, 877 .compat_3_brand = "Linux", 878 .interp_path = "/lib64/ld-linux-x86-64.so.2", 879 .sysvec = &elf_linux_sysvec, 880 .interp_newpath = NULL, 881 .brand_note = &linux64_brandnote, 882 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 883 }; 884 885 static Elf64_Brandinfo linux_glibc2brandshort = { 886 .brand = ELFOSABI_LINUX, 887 .machine = EM_X86_64, 888 .compat_3_brand = "Linux", 889 .interp_path = "/lib64/ld-linux.so.2", 890 .sysvec = &elf_linux_sysvec, 891 .interp_newpath = NULL, 892 .brand_note = &linux64_brandnote, 893 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 894 }; 895 896 static Elf64_Brandinfo linux_muslbrand = { 897 .brand = ELFOSABI_LINUX, 898 .machine = EM_X86_64, 899 .compat_3_brand = "Linux", 900 .interp_path = "/lib/ld-musl-x86_64.so.1", 901 .sysvec = &elf_linux_sysvec, 902 .interp_newpath = NULL, 903 .brand_note = &linux64_brandnote, 904 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE | 905 LINUX_BI_FUTEX_REQUEUE 906 }; 907 908 Elf64_Brandinfo *linux_brandlist[] = { 909 &linux_glibc2brand, 910 &linux_glibc2brandshort, 911 &linux_muslbrand, 912 NULL 913 }; 914 915 static int 916 linux64_elf_modevent(module_t mod, int type, void *data) 917 { 918 Elf64_Brandinfo **brandinfo; 919 int error; 920 struct linux_ioctl_handler **lihp; 921 922 error = 0; 923 924 switch(type) { 925 case MOD_LOAD: 926 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 927 ++brandinfo) 928 if (elf64_insert_brand_entry(*brandinfo) < 0) 929 error = EINVAL; 930 if (error == 0) { 931 SET_FOREACH(lihp, linux_ioctl_handler_set) 932 linux_ioctl_register_handler(*lihp); 933 stclohz = (stathz ? stathz : hz); 934 if (bootverbose) 935 printf("Linux x86-64 ELF exec handler installed\n"); 936 } else 937 printf("cannot insert Linux x86-64 ELF brand handler\n"); 938 break; 939 case MOD_UNLOAD: 940 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 941 ++brandinfo) 942 if (elf64_brand_inuse(*brandinfo)) 943 error = EBUSY; 944 if (error == 0) { 945 for (brandinfo = &linux_brandlist[0]; 946 *brandinfo != NULL; ++brandinfo) 947 if (elf64_remove_brand_entry(*brandinfo) < 0) 948 error = EINVAL; 949 } 950 if (error == 0) { 951 SET_FOREACH(lihp, linux_ioctl_handler_set) 952 linux_ioctl_unregister_handler(*lihp); 953 if (bootverbose) 954 printf("Linux x86_64 ELF exec handler removed\n"); 955 } else 956 printf("Could not deinstall Linux x86_64 ELF interpreter entry\n"); 957 break; 958 default: 959 return (EOPNOTSUPP); 960 } 961 return (error); 962 } 963 964 static moduledata_t linux64_elf_mod = { 965 "linux64elf", 966 linux64_elf_modevent, 967 0 968 }; 969 970 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 971 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 972 FEATURE(linux64, "Linux 64bit support"); 973