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 __FBSDID("$FreeBSD$"); 36 37 #define __ELF_WORD_SIZE 64 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/exec.h> 42 #include <sys/fcntl.h> 43 #include <sys/imgact.h> 44 #include <sys/imgact_elf.h> 45 #include <sys/kernel.h> 46 #include <sys/ktr.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/module.h> 50 #include <sys/mutex.h> 51 #include <sys/proc.h> 52 #include <sys/resourcevar.h> 53 #include <sys/stddef.h> 54 #include <sys/signalvar.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysctl.h> 57 #include <sys/sysent.h> 58 #include <sys/sysproto.h> 59 #include <sys/vnode.h> 60 #include <sys/eventhandler.h> 61 62 #include <vm/vm.h> 63 #include <vm/pmap.h> 64 #include <vm/vm_extern.h> 65 #include <vm/vm_map.h> 66 #include <vm/vm_object.h> 67 #include <vm/vm_page.h> 68 #include <vm/vm_param.h> 69 70 #include <machine/cpu.h> 71 #include <machine/md_var.h> 72 #include <machine/pcb.h> 73 #include <machine/specialreg.h> 74 #include <machine/trap.h> 75 76 #include <x86/linux/linux_x86.h> 77 #include <amd64/linux/linux.h> 78 #include <amd64/linux/linux_proto.h> 79 #include <compat/linux/linux_emul.h> 80 #include <compat/linux/linux_fork.h> 81 #include <compat/linux/linux_ioctl.h> 82 #include <compat/linux/linux_mib.h> 83 #include <compat/linux/linux_misc.h> 84 #include <compat/linux/linux_signal.h> 85 #include <compat/linux/linux_sysproto.h> 86 #include <compat/linux/linux_util.h> 87 #include <compat/linux/linux_vdso.h> 88 89 #include <x86/linux/linux_x86_sigframe.h> 90 91 MODULE_VERSION(linux64, 1); 92 93 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 94 #define LINUX_VDSOPAGE_LA48 (VM_MAXUSER_ADDRESS_LA48 - \ 95 LINUX_VDSOPAGE_SIZE) 96 #define LINUX_SHAREDPAGE_LA48 (LINUX_VDSOPAGE_LA48 - PAGE_SIZE) 97 /* 98 * PAGE_SIZE - the size 99 * of the native SHAREDPAGE 100 */ 101 #define LINUX_USRSTACK_LA48 LINUX_SHAREDPAGE_LA48 102 #define LINUX_PS_STRINGS_LA48 (LINUX_USRSTACK_LA48 - \ 103 sizeof(struct ps_strings)) 104 105 static int linux_szsigcode; 106 static vm_object_t linux_vdso_obj; 107 static char *linux_vdso_mapping; 108 extern char _binary_linux_vdso_so_o_start; 109 extern char _binary_linux_vdso_so_o_end; 110 static vm_offset_t linux_vdso_base; 111 112 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 113 extern const char *linux_syscallnames[]; 114 115 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 116 117 static int linux_copyout_strings(struct image_params *imgp, 118 uintptr_t *stack_base); 119 static int linux_fixup_elf(uintptr_t *stack_base, 120 struct image_params *iparams); 121 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 122 static void linux_vdso_install(const void *param); 123 static void linux_vdso_deinstall(const void *param); 124 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 125 static void linux_set_syscall_retval(struct thread *td, int error); 126 static int linux_fetch_syscall_args(struct thread *td); 127 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 128 uintptr_t stack); 129 static void linux_exec_sysvec_init(void *param); 130 static int linux_on_exec_vmspace(struct proc *p, 131 struct image_params *imgp); 132 static void linux_set_fork_retval(struct thread *td); 133 static int linux_vsyscall(struct thread *td); 134 135 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode); 136 LINUX_VDSO_SYM_CHAR(linux_platform); 137 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 138 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector); 139 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector); 140 141 static int 142 linux_fetch_syscall_args(struct thread *td) 143 { 144 struct proc *p; 145 struct trapframe *frame; 146 struct syscall_args *sa; 147 148 p = td->td_proc; 149 frame = td->td_frame; 150 sa = &td->td_sa; 151 152 sa->args[0] = frame->tf_rdi; 153 sa->args[1] = frame->tf_rsi; 154 sa->args[2] = frame->tf_rdx; 155 sa->args[3] = frame->tf_rcx; 156 sa->args[4] = frame->tf_r8; 157 sa->args[5] = frame->tf_r9; 158 sa->code = frame->tf_rax; 159 sa->original_code = sa->code; 160 161 if (sa->code >= p->p_sysent->sv_size) 162 /* nosys */ 163 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 164 else 165 sa->callp = &p->p_sysent->sv_table[sa->code]; 166 167 td->td_retval[0] = 0; 168 return (0); 169 } 170 171 static void 172 linux_set_syscall_retval(struct thread *td, int error) 173 { 174 struct trapframe *frame; 175 176 frame = td->td_frame; 177 178 switch (error) { 179 case 0: 180 frame->tf_rax = td->td_retval[0]; 181 frame->tf_r10 = frame->tf_rcx; 182 break; 183 184 case ERESTART: 185 /* 186 * Reconstruct pc, we know that 'syscall' is 2 bytes, 187 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes. 188 * We saved this in tf_err. 189 * 190 */ 191 frame->tf_rip -= frame->tf_err; 192 frame->tf_r10 = frame->tf_rcx; 193 break; 194 195 case EJUSTRETURN: 196 break; 197 198 default: 199 frame->tf_rax = bsd_to_linux_errno(error); 200 frame->tf_r10 = frame->tf_rcx; 201 break; 202 } 203 204 /* 205 * Differently from FreeBSD native ABI, on Linux only %rcx 206 * and %r11 values are not preserved across the syscall. 207 * Require full context restore to get all registers except 208 * those two restored at return to usermode. 209 * 210 * XXX: Would be great to be able to avoid PCB_FULL_IRET 211 * for the error == 0 case. 212 */ 213 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 214 } 215 216 static void 217 linux_set_fork_retval(struct thread *td) 218 { 219 struct trapframe *frame = td->td_frame; 220 221 frame->tf_rax = 0; 222 } 223 224 static int 225 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 226 { 227 Elf_Auxargs *args; 228 Elf_Auxinfo *argarray, *pos; 229 struct proc *p; 230 int error, issetugid; 231 232 p = imgp->proc; 233 args = (Elf64_Auxargs *)imgp->auxargs; 234 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 235 M_WAITOK | M_ZERO); 236 237 issetugid = p->p_flag & P_SUGID ? 1 : 0; 238 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 239 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature); 240 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 241 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 242 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 243 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 244 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 245 AUXARGS_ENTRY(pos, AT_BASE, args->base); 246 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 247 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 248 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 249 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 250 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 251 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 252 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 253 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 254 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0); 255 if (imgp->execpathp != 0) 256 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 257 if (args->execfd != -1) 258 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 259 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 260 AUXARGS_ENTRY(pos, AT_NULL, 0); 261 262 free(imgp->auxargs, M_TEMP); 263 imgp->auxargs = NULL; 264 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 265 266 error = copyout(argarray, (void *)base, 267 sizeof(*argarray) * LINUX_AT_COUNT); 268 free(argarray, M_TEMP); 269 return (error); 270 } 271 272 static int 273 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp) 274 { 275 Elf_Addr *base; 276 277 base = (Elf64_Addr *)*stack_base; 278 base--; 279 if (suword(base, (uint64_t)imgp->args->argc) == -1) 280 return (EFAULT); 281 282 *stack_base = (uintptr_t)base; 283 return (0); 284 } 285 286 /* 287 * Copy strings out to the new process address space, constructing new arg 288 * and env vector tables. Return a pointer to the base so that it can be used 289 * as the initial stack pointer. 290 */ 291 static int 292 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 293 { 294 int argc, envc, error; 295 char **vectp; 296 char *stringp; 297 uintptr_t destp, ustringp; 298 struct ps_strings *arginfo; 299 char canary[LINUX_AT_RANDOM_LEN]; 300 size_t execpath_len; 301 struct proc *p; 302 303 p = imgp->proc; 304 arginfo = (struct ps_strings *)PROC_PS_STRINGS(p); 305 destp = (uintptr_t)arginfo; 306 307 if (imgp->execpath != NULL && imgp->auxargs != NULL) { 308 execpath_len = strlen(imgp->execpath) + 1; 309 destp -= execpath_len; 310 destp = rounddown2(destp, sizeof(void *)); 311 imgp->execpathp = (void *)destp; 312 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 313 if (error != 0) 314 return (error); 315 } 316 317 /* Prepare the canary for SSP. */ 318 arc4rand(canary, sizeof(canary), 0); 319 destp -= roundup(sizeof(canary), sizeof(void *)); 320 imgp->canary = (void *)destp; 321 error = copyout(canary, imgp->canary, sizeof(canary)); 322 if (error != 0) 323 return (error); 324 325 /* Allocate room for the argument and environment strings. */ 326 destp -= ARG_MAX - imgp->args->stringspace; 327 destp = rounddown2(destp, sizeof(void *)); 328 ustringp = destp; 329 330 if (imgp->auxargs) { 331 /* 332 * Allocate room on the stack for the ELF auxargs 333 * array. It has LINUX_AT_COUNT entries. 334 */ 335 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 336 destp = rounddown2(destp, sizeof(void *)); 337 } 338 339 vectp = (char **)destp; 340 341 /* 342 * Allocate room for the argv[] and env vectors including the 343 * terminating NULL pointers. 344 */ 345 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 346 347 /* 348 * Starting with 2.24, glibc depends on a 16-byte stack alignment. 349 * One "long argc" will be prepended later. 350 */ 351 vectp = (char **)((((uintptr_t)vectp + 8) & ~0xF) - 8); 352 353 /* vectp also becomes our initial stack base. */ 354 *stack_base = (uintptr_t)vectp; 355 356 stringp = imgp->args->begin_argv; 357 argc = imgp->args->argc; 358 envc = imgp->args->envc; 359 360 /* Copy out strings - arguments and environment. */ 361 error = copyout(stringp, (void *)ustringp, 362 ARG_MAX - imgp->args->stringspace); 363 if (error != 0) 364 return (error); 365 366 /* Fill in "ps_strings" struct for ps, w, etc. */ 367 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 368 suword(&arginfo->ps_nargvstr, argc) != 0) 369 return (EFAULT); 370 371 /* Fill in argument portion of vector table. */ 372 for (; argc > 0; --argc) { 373 if (suword(vectp++, ustringp) != 0) 374 return (EFAULT); 375 while (*stringp++ != 0) 376 ustringp++; 377 ustringp++; 378 } 379 380 /* A null vector table pointer separates the argp's from the envp's. */ 381 if (suword(vectp++, 0) != 0) 382 return (EFAULT); 383 384 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 385 suword(&arginfo->ps_nenvstr, envc) != 0) 386 return (EFAULT); 387 388 /* Fill in environment portion of vector table. */ 389 for (; envc > 0; --envc) { 390 if (suword(vectp++, ustringp) != 0) 391 return (EFAULT); 392 while (*stringp++ != 0) 393 ustringp++; 394 ustringp++; 395 } 396 397 /* The end of the vector table is a null pointer. */ 398 if (suword(vectp, 0) != 0) 399 return (EFAULT); 400 401 if (imgp->auxargs) { 402 vectp++; 403 error = imgp->sysent->sv_copyout_auxargs(imgp, 404 (uintptr_t)vectp); 405 if (error != 0) 406 return (error); 407 } 408 409 return (0); 410 } 411 412 /* 413 * Reset registers to default values on exec. 414 */ 415 static void 416 linux_exec_setregs(struct thread *td, struct image_params *imgp, 417 uintptr_t stack) 418 { 419 struct trapframe *regs; 420 struct pcb *pcb; 421 register_t saved_rflags; 422 423 regs = td->td_frame; 424 pcb = td->td_pcb; 425 426 if (td->td_proc->p_md.md_ldt != NULL) 427 user_ldt_free(td); 428 429 pcb->pcb_fsbase = 0; 430 pcb->pcb_gsbase = 0; 431 clear_pcb_flags(pcb, PCB_32BIT); 432 pcb->pcb_initial_fpucw = __LINUX_NPXCW__; 433 set_pcb_flags(pcb, PCB_FULL_IRET); 434 435 saved_rflags = regs->tf_rflags & PSL_T; 436 bzero((char *)regs, sizeof(struct trapframe)); 437 regs->tf_rip = imgp->entry_addr; 438 regs->tf_rsp = stack; 439 regs->tf_rflags = PSL_USER | saved_rflags; 440 regs->tf_ss = _udatasel; 441 regs->tf_cs = _ucodesel; 442 regs->tf_ds = _udatasel; 443 regs->tf_es = _udatasel; 444 regs->tf_fs = _ufssel; 445 regs->tf_gs = _ugssel; 446 regs->tf_flags = TF_HASSEGS; 447 448 x86_clear_dbregs(pcb); 449 450 /* 451 * Drop the FP state if we hold it, so that the process gets a 452 * clean FP state if it uses the FPU again. 453 */ 454 fpstate_drop(td); 455 } 456 457 /* 458 * Copied from amd64/amd64/machdep.c 459 * 460 * XXX fpu state need? don't think so 461 */ 462 int 463 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 464 { 465 struct proc *p; 466 struct l_ucontext uc; 467 struct l_sigcontext *context; 468 struct trapframe *regs; 469 unsigned long rflags; 470 sigset_t bmask; 471 int error; 472 ksiginfo_t ksi; 473 474 regs = td->td_frame; 475 error = copyin((void *)regs->tf_rbx, &uc, sizeof(uc)); 476 if (error != 0) 477 return (error); 478 479 p = td->td_proc; 480 context = &uc.uc_mcontext; 481 rflags = context->sc_rflags; 482 483 /* 484 * Don't allow users to change privileged or reserved flags. 485 */ 486 /* 487 * XXX do allow users to change the privileged flag PSL_RF. 488 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers 489 * should sometimes set it there too. tf_rflags is kept in 490 * the signal context during signal handling and there is no 491 * other place to remember it, so the PSL_RF bit may be 492 * corrupted by the signal handler without us knowing. 493 * Corruption of the PSL_RF bit at worst causes one more or 494 * one less debugger trap, so allowing it is fairly harmless. 495 */ 496 if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 497 uprintf("pid %d comm %s linux mangled rflags %#lx\n", 498 p->p_pid, p->p_comm, rflags); 499 return (EINVAL); 500 } 501 502 /* 503 * Don't allow users to load a valid privileged %cs. Let the 504 * hardware check for invalid selectors, excess privilege in 505 * other selectors, invalid %eip's and invalid %esp's. 506 */ 507 if (!CS_SECURE(context->sc_cs)) { 508 uprintf("pid %d comm %s linux mangled cs %#x\n", 509 p->p_pid, p->p_comm, context->sc_cs); 510 ksiginfo_init_trap(&ksi); 511 ksi.ksi_signo = SIGBUS; 512 ksi.ksi_code = BUS_OBJERR; 513 ksi.ksi_trapno = T_PROTFLT; 514 ksi.ksi_addr = (void *)regs->tf_rip; 515 trapsignal(td, &ksi); 516 return (EINVAL); 517 } 518 519 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 520 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 521 522 regs->tf_rdi = context->sc_rdi; 523 regs->tf_rsi = context->sc_rsi; 524 regs->tf_rdx = context->sc_rdx; 525 regs->tf_rbp = context->sc_rbp; 526 regs->tf_rbx = context->sc_rbx; 527 regs->tf_rcx = context->sc_rcx; 528 regs->tf_rax = context->sc_rax; 529 regs->tf_rip = context->sc_rip; 530 regs->tf_rsp = context->sc_rsp; 531 regs->tf_r8 = context->sc_r8; 532 regs->tf_r9 = context->sc_r9; 533 regs->tf_r10 = context->sc_r10; 534 regs->tf_r11 = context->sc_r11; 535 regs->tf_r12 = context->sc_r12; 536 regs->tf_r13 = context->sc_r13; 537 regs->tf_r14 = context->sc_r14; 538 regs->tf_r15 = context->sc_r15; 539 regs->tf_cs = context->sc_cs; 540 regs->tf_err = context->sc_err; 541 regs->tf_rflags = rflags; 542 543 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 544 return (EJUSTRETURN); 545 } 546 547 /* 548 * copied from amd64/amd64/machdep.c 549 * 550 * Send an interrupt to process. 551 */ 552 static void 553 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 554 { 555 struct l_rt_sigframe sf, *sfp; 556 struct proc *p; 557 struct thread *td; 558 struct sigacts *psp; 559 caddr_t sp; 560 struct trapframe *regs; 561 int sig, code; 562 int oonstack, issiginfo; 563 564 td = curthread; 565 p = td->td_proc; 566 PROC_LOCK_ASSERT(p, MA_OWNED); 567 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 568 psp = p->p_sigacts; 569 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig); 570 code = ksi->ksi_code; 571 mtx_assert(&psp->ps_mtx, MA_OWNED); 572 regs = td->td_frame; 573 oonstack = sigonstack(regs->tf_rsp); 574 575 LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u", 576 catcher, sig, mask, code); 577 578 /* Save user context. */ 579 bzero(&sf, sizeof(sf)); 580 bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask); 581 sf.sf_uc.uc_mcontext.sc_mask = sf.sf_uc.uc_sigmask; 582 583 sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 584 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 585 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 586 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 587 588 sf.sf_uc.uc_mcontext.sc_rdi = regs->tf_rdi; 589 sf.sf_uc.uc_mcontext.sc_rsi = regs->tf_rsi; 590 sf.sf_uc.uc_mcontext.sc_rdx = regs->tf_rdx; 591 sf.sf_uc.uc_mcontext.sc_rbp = regs->tf_rbp; 592 sf.sf_uc.uc_mcontext.sc_rbx = regs->tf_rbx; 593 sf.sf_uc.uc_mcontext.sc_rcx = regs->tf_rcx; 594 sf.sf_uc.uc_mcontext.sc_rax = regs->tf_rax; 595 sf.sf_uc.uc_mcontext.sc_rip = regs->tf_rip; 596 sf.sf_uc.uc_mcontext.sc_rsp = regs->tf_rsp; 597 sf.sf_uc.uc_mcontext.sc_r8 = regs->tf_r8; 598 sf.sf_uc.uc_mcontext.sc_r9 = regs->tf_r9; 599 sf.sf_uc.uc_mcontext.sc_r10 = regs->tf_r10; 600 sf.sf_uc.uc_mcontext.sc_r11 = regs->tf_r11; 601 sf.sf_uc.uc_mcontext.sc_r12 = regs->tf_r12; 602 sf.sf_uc.uc_mcontext.sc_r13 = regs->tf_r13; 603 sf.sf_uc.uc_mcontext.sc_r14 = regs->tf_r14; 604 sf.sf_uc.uc_mcontext.sc_r15 = regs->tf_r15; 605 sf.sf_uc.uc_mcontext.sc_cs = regs->tf_cs; 606 sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags; 607 sf.sf_uc.uc_mcontext.sc_err = regs->tf_err; 608 sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 609 sf.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 610 611 /* Allocate space for the signal handler context. */ 612 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 613 SIGISMEMBER(psp->ps_sigonstack, sig)) { 614 sp = (caddr_t)td->td_sigstk.ss_sp + td->td_sigstk.ss_size; 615 } else 616 sp = (caddr_t)regs->tf_rsp - 128; 617 sp -= sizeof(struct l_rt_sigframe); 618 /* Align to 16 bytes. */ 619 sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul); 620 621 mtx_unlock(&psp->ps_mtx); 622 PROC_UNLOCK(p); 623 624 /* Translate the signal. */ 625 sig = bsd_to_linux_signal(sig); 626 /* Fill in POSIX parts. */ 627 siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig); 628 629 /* Copy the sigframe out to the user's stack. */ 630 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 631 uprintf("pid %d comm %s has trashed its stack, killing\n", 632 p->p_pid, p->p_comm); 633 PROC_LOCK(p); 634 sigexit(td, SIGILL); 635 } 636 637 /* Build the argument list for the signal handler. */ 638 regs->tf_rdi = sig; /* arg 1 in %rdi */ 639 regs->tf_rax = 0; 640 if (issiginfo) { 641 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */ 642 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */ 643 } else { 644 regs->tf_rsi = 0; 645 regs->tf_rdx = 0; 646 } 647 regs->tf_rcx = (register_t)catcher; 648 regs->tf_rsp = (long)sfp; 649 regs->tf_rip = linux_rt_sigcode; 650 regs->tf_rflags &= ~(PSL_T | PSL_D); 651 regs->tf_cs = _ucodesel; 652 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 653 PROC_LOCK(p); 654 mtx_lock(&psp->ps_mtx); 655 } 656 657 #define LINUX_VSYSCALL_START (-10UL << 20) 658 #define LINUX_VSYSCALL_SZ 1024 659 660 const unsigned long linux_vsyscall_vector[] = { 661 LINUX_SYS_gettimeofday, 662 LINUX_SYS_linux_time, 663 LINUX_SYS_linux_getcpu, 664 }; 665 666 static int 667 linux_vsyscall(struct thread *td) 668 { 669 struct trapframe *frame; 670 uint64_t retqaddr; 671 int code, traced; 672 int error; 673 674 frame = td->td_frame; 675 676 /* Check %rip for vsyscall area. */ 677 if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START)) 678 return (EINVAL); 679 if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0) 680 return (EINVAL); 681 code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ; 682 if (code >= nitems(linux_vsyscall_vector)) 683 return (EINVAL); 684 685 /* 686 * vsyscall called as callq *(%rax), so we must 687 * use return address from %rsp and also fixup %rsp. 688 */ 689 error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr)); 690 if (error) 691 return (error); 692 693 frame->tf_rip = retqaddr; 694 frame->tf_rax = linux_vsyscall_vector[code]; 695 frame->tf_rsp += 8; 696 697 traced = (frame->tf_flags & PSL_T); 698 699 amd64_syscall(td, traced); 700 701 return (0); 702 } 703 704 struct sysentvec elf_linux_sysvec = { 705 .sv_size = LINUX_SYS_MAXSYSCALL, 706 .sv_table = linux_sysent, 707 .sv_fixup = linux_fixup_elf, 708 .sv_sendsig = linux_rt_sendsig, 709 .sv_sigcode = &_binary_linux_vdso_so_o_start, 710 .sv_szsigcode = &linux_szsigcode, 711 .sv_name = "Linux ELF64", 712 .sv_coredump = elf64_coredump, 713 .sv_elf_core_osabi = ELFOSABI_NONE, 714 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 715 .sv_elf_core_prepare_notes = linux64_prepare_notes, 716 .sv_imgact_try = linux_exec_imgact_try, 717 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 718 .sv_minuser = VM_MIN_ADDRESS, 719 .sv_maxuser = VM_MAXUSER_ADDRESS_LA48, 720 .sv_usrstack = LINUX_USRSTACK_LA48, 721 .sv_psstrings = LINUX_PS_STRINGS_LA48, 722 .sv_psstringssz = sizeof(struct ps_strings), 723 .sv_stackprot = VM_PROT_ALL, 724 .sv_copyout_auxargs = linux_copyout_auxargs, 725 .sv_copyout_strings = linux_copyout_strings, 726 .sv_setregs = linux_exec_setregs, 727 .sv_fixlimit = NULL, 728 .sv_maxssiz = NULL, 729 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | 730 SV_SIG_WAITNDQ | SV_TIMEKEEP, 731 .sv_set_syscall_retval = linux_set_syscall_retval, 732 .sv_fetch_syscall_args = linux_fetch_syscall_args, 733 .sv_syscallnames = linux_syscallnames, 734 .sv_shared_page_base = LINUX_SHAREDPAGE_LA48, 735 .sv_shared_page_len = PAGE_SIZE, 736 .sv_schedtail = linux_schedtail, 737 .sv_thread_detach = linux_thread_detach, 738 .sv_trap = linux_vsyscall, 739 .sv_onexec = linux_on_exec_vmspace, 740 .sv_onexit = linux_on_exit, 741 .sv_ontdexit = linux_thread_dtor, 742 .sv_setid_allowed = &linux_setid_allowed_query, 743 .sv_set_fork_retval = linux_set_fork_retval, 744 }; 745 746 static int 747 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 748 { 749 int error; 750 751 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 752 LINUX_VDSOPAGE_SIZE, imgp); 753 if (error == 0) 754 linux_on_exec(p, imgp); 755 return (error); 756 } 757 758 /* 759 * linux_vdso_install() and linux_exec_sysvec_init() must be called 760 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 761 */ 762 static void 763 linux_exec_sysvec_init(void *param) 764 { 765 l_uintptr_t *ktimekeep_base, *ktsc_selector; 766 struct sysentvec *sv; 767 ptrdiff_t tkoff; 768 769 sv = param; 770 amd64_lower_shared_page(sv); 771 /* Fill timekeep_base */ 772 exec_sysvec_init(sv); 773 774 tkoff = kern_timekeep_base - linux_vdso_base; 775 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 776 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 777 778 tkoff = kern_tsc_selector - linux_vdso_base; 779 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 780 *ktsc_selector = linux_vdso_tsc_selector_idx(); 781 if (bootverbose) 782 printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector); 783 784 tkoff = kern_cpu_selector - linux_vdso_base; 785 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 786 *ktsc_selector = linux_vdso_cpu_selector_idx(); 787 if (bootverbose) 788 printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector); 789 } 790 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 791 linux_exec_sysvec_init, &elf_linux_sysvec); 792 793 static void 794 linux_vdso_install(const void *param) 795 { 796 char *vdso_start = &_binary_linux_vdso_so_o_start; 797 char *vdso_end = &_binary_linux_vdso_so_o_end; 798 799 linux_szsigcode = vdso_end - vdso_start; 800 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 801 802 linux_vdso_base = LINUX_VDSOPAGE_LA48; 803 if (hw_lower_amd64_sharedpage != 0) 804 linux_vdso_base -= PAGE_SIZE; 805 806 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 807 808 linux_vdso_obj = __elfN(linux_shared_page_init) 809 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 810 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 811 812 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 813 } 814 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 815 linux_vdso_install, NULL); 816 817 static void 818 linux_vdso_deinstall(const void *param) 819 { 820 821 __elfN(linux_shared_page_fini)(linux_vdso_obj, 822 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 823 } 824 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 825 linux_vdso_deinstall, NULL); 826 827 static void 828 linux_vdso_reloc(char *mapping, Elf_Addr offset) 829 { 830 const Elf_Ehdr *ehdr; 831 const Elf_Shdr *shdr; 832 Elf64_Addr *where, val; 833 Elf_Size rtype, symidx; 834 const Elf_Rela *rela; 835 Elf_Addr addr, addend; 836 int relacnt; 837 int i, j; 838 839 MPASS(offset != 0); 840 841 relacnt = 0; 842 ehdr = (const Elf_Ehdr *)mapping; 843 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 844 for (i = 0; i < ehdr->e_shnum; i++) 845 { 846 switch (shdr[i].sh_type) { 847 case SHT_REL: 848 printf("Linux x86_64 vDSO: unexpected Rel section\n"); 849 break; 850 case SHT_RELA: 851 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); 852 relacnt = shdr[i].sh_size / sizeof(*rela); 853 } 854 } 855 856 for (j = 0; j < relacnt; j++, rela++) { 857 where = (Elf_Addr *)(mapping + rela->r_offset); 858 addend = rela->r_addend; 859 rtype = ELF_R_TYPE(rela->r_info); 860 symidx = ELF_R_SYM(rela->r_info); 861 862 switch (rtype) { 863 case R_X86_64_NONE: /* none */ 864 break; 865 866 case R_X86_64_RELATIVE: /* B + A */ 867 addr = (Elf_Addr)(offset + addend); 868 val = addr; 869 if (*where != val) 870 *where = val; 871 break; 872 case R_X86_64_IRELATIVE: 873 printf("Linux x86_64 vDSO: unexpected ifunc relocation, " 874 "symbol index %ld\n", symidx); 875 break; 876 default: 877 printf("Linux x86_64 vDSO: unexpected relocation type %ld, " 878 "symbol index %ld\n", rtype, symidx); 879 } 880 } 881 } 882 883 static char GNULINUX_ABI_VENDOR[] = "GNU"; 884 static int GNULINUX_ABI_DESC = 0; 885 886 static bool 887 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 888 { 889 const Elf32_Word *desc; 890 uintptr_t p; 891 892 p = (uintptr_t)(note + 1); 893 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 894 895 desc = (const Elf32_Word *)p; 896 if (desc[0] != GNULINUX_ABI_DESC) 897 return (false); 898 899 /* 900 * For Linux we encode osrel using the Linux convention of 901 * (version << 16) | (major << 8) | (minor) 902 * See macro in linux_mib.h 903 */ 904 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 905 906 return (true); 907 } 908 909 static Elf_Brandnote linux64_brandnote = { 910 .hdr.n_namesz = sizeof(GNULINUX_ABI_VENDOR), 911 .hdr.n_descsz = 16, 912 .hdr.n_type = 1, 913 .vendor = GNULINUX_ABI_VENDOR, 914 .flags = BN_TRANSLATE_OSREL, 915 .trans_osrel = linux_trans_osrel 916 }; 917 918 static Elf64_Brandinfo linux_glibc2brand = { 919 .brand = ELFOSABI_LINUX, 920 .machine = EM_X86_64, 921 .compat_3_brand = "Linux", 922 .emul_path = linux_emul_path, 923 .interp_path = "/lib64/ld-linux-x86-64.so.2", 924 .sysvec = &elf_linux_sysvec, 925 .interp_newpath = NULL, 926 .brand_note = &linux64_brandnote, 927 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 928 }; 929 930 static Elf64_Brandinfo linux_glibc2brandshort = { 931 .brand = ELFOSABI_LINUX, 932 .machine = EM_X86_64, 933 .compat_3_brand = "Linux", 934 .emul_path = linux_emul_path, 935 .interp_path = "/lib64/ld-linux.so.2", 936 .sysvec = &elf_linux_sysvec, 937 .interp_newpath = NULL, 938 .brand_note = &linux64_brandnote, 939 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 940 }; 941 942 static Elf64_Brandinfo linux_muslbrand = { 943 .brand = ELFOSABI_LINUX, 944 .machine = EM_X86_64, 945 .compat_3_brand = "Linux", 946 .emul_path = linux_emul_path, 947 .interp_path = "/lib/ld-musl-x86_64.so.1", 948 .sysvec = &elf_linux_sysvec, 949 .interp_newpath = NULL, 950 .brand_note = &linux64_brandnote, 951 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE | 952 LINUX_BI_FUTEX_REQUEUE 953 }; 954 955 Elf64_Brandinfo *linux_brandlist[] = { 956 &linux_glibc2brand, 957 &linux_glibc2brandshort, 958 &linux_muslbrand, 959 NULL 960 }; 961 962 static int 963 linux64_elf_modevent(module_t mod, int type, void *data) 964 { 965 Elf64_Brandinfo **brandinfo; 966 int error; 967 struct linux_ioctl_handler **lihp; 968 969 error = 0; 970 971 switch(type) { 972 case MOD_LOAD: 973 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 974 ++brandinfo) 975 if (elf64_insert_brand_entry(*brandinfo) < 0) 976 error = EINVAL; 977 if (error == 0) { 978 SET_FOREACH(lihp, linux_ioctl_handler_set) 979 linux_ioctl_register_handler(*lihp); 980 stclohz = (stathz ? stathz : hz); 981 if (bootverbose) 982 printf("Linux x86-64 ELF exec handler installed\n"); 983 } else 984 printf("cannot insert Linux x86-64 ELF brand handler\n"); 985 break; 986 case MOD_UNLOAD: 987 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 988 ++brandinfo) 989 if (elf64_brand_inuse(*brandinfo)) 990 error = EBUSY; 991 if (error == 0) { 992 for (brandinfo = &linux_brandlist[0]; 993 *brandinfo != NULL; ++brandinfo) 994 if (elf64_remove_brand_entry(*brandinfo) < 0) 995 error = EINVAL; 996 } 997 if (error == 0) { 998 SET_FOREACH(lihp, linux_ioctl_handler_set) 999 linux_ioctl_unregister_handler(*lihp); 1000 if (bootverbose) 1001 printf("Linux x86_64 ELF exec handler removed\n"); 1002 } else 1003 printf("Could not deinstall Linux x86_64 ELF interpreter entry\n"); 1004 break; 1005 default: 1006 return (EOPNOTSUPP); 1007 } 1008 return (error); 1009 } 1010 1011 static moduledata_t linux64_elf_mod = { 1012 "linux64elf", 1013 linux64_elf_modevent, 1014 0 1015 }; 1016 1017 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 1018 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 1019 FEATURE(linux64, "Linux 64bit support"); 1020