1 /*- 2 * Copyright (c) 2013 Dmitry Chagin 3 * Copyright (c) 2004 Tim J. Robbins 4 * Copyright (c) 2003 Peter Wemm 5 * Copyright (c) 2002 Doug Rabson 6 * Copyright (c) 1998-1999 Andrew Gallatin 7 * Copyright (c) 1994-1996 Søren Schmidt 8 * All rights reserved. 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/signalvar.h> 54 #include <sys/syscallsubr.h> 55 #include <sys/sysctl.h> 56 #include <sys/sysent.h> 57 #include <sys/sysproto.h> 58 #include <sys/vnode.h> 59 #include <sys/eventhandler.h> 60 61 #include <vm/vm.h> 62 #include <vm/pmap.h> 63 #include <vm/vm_extern.h> 64 #include <vm/vm_map.h> 65 #include <vm/vm_object.h> 66 #include <vm/vm_page.h> 67 #include <vm/vm_param.h> 68 69 #include <machine/cpu.h> 70 #include <machine/md_var.h> 71 #include <machine/pcb.h> 72 #include <machine/specialreg.h> 73 #include <machine/trap.h> 74 75 #include <amd64/linux/linux.h> 76 #include <amd64/linux/linux_proto.h> 77 #include <compat/linux/linux_emul.h> 78 #include <compat/linux/linux_ioctl.h> 79 #include <compat/linux/linux_mib.h> 80 #include <compat/linux/linux_misc.h> 81 #include <compat/linux/linux_signal.h> 82 #include <compat/linux/linux_sysproto.h> 83 #include <compat/linux/linux_util.h> 84 #include <compat/linux/linux_vdso.h> 85 86 MODULE_VERSION(linux64, 1); 87 88 const char *linux_kplatform; 89 static int linux_szsigcode; 90 static vm_object_t linux_shared_page_obj; 91 static char *linux_shared_page_mapping; 92 extern char _binary_linux_locore_o_start; 93 extern char _binary_linux_locore_o_end; 94 95 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 96 97 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 98 99 static int linux_copyout_strings(struct image_params *imgp, 100 uintptr_t *stack_base); 101 static int linux_fixup_elf(uintptr_t *stack_base, 102 struct image_params *iparams); 103 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 104 static void linux_vdso_install(void *param); 105 static void linux_vdso_deinstall(void *param); 106 static void linux_set_syscall_retval(struct thread *td, int error); 107 static int linux_fetch_syscall_args(struct thread *td); 108 static void linux_exec_setregs(struct thread *td, struct image_params *imgp, 109 uintptr_t stack); 110 static int linux_vsyscall(struct thread *td); 111 112 #define LINUX_T_UNKNOWN 255 113 static int _bsd_to_linux_trapcode[] = { 114 LINUX_T_UNKNOWN, /* 0 */ 115 6, /* 1 T_PRIVINFLT */ 116 LINUX_T_UNKNOWN, /* 2 */ 117 3, /* 3 T_BPTFLT */ 118 LINUX_T_UNKNOWN, /* 4 */ 119 LINUX_T_UNKNOWN, /* 5 */ 120 16, /* 6 T_ARITHTRAP */ 121 254, /* 7 T_ASTFLT */ 122 LINUX_T_UNKNOWN, /* 8 */ 123 13, /* 9 T_PROTFLT */ 124 1, /* 10 T_TRCTRAP */ 125 LINUX_T_UNKNOWN, /* 11 */ 126 14, /* 12 T_PAGEFLT */ 127 LINUX_T_UNKNOWN, /* 13 */ 128 17, /* 14 T_ALIGNFLT */ 129 LINUX_T_UNKNOWN, /* 15 */ 130 LINUX_T_UNKNOWN, /* 16 */ 131 LINUX_T_UNKNOWN, /* 17 */ 132 0, /* 18 T_DIVIDE */ 133 2, /* 19 T_NMI */ 134 4, /* 20 T_OFLOW */ 135 5, /* 21 T_BOUND */ 136 7, /* 22 T_DNA */ 137 8, /* 23 T_DOUBLEFLT */ 138 9, /* 24 T_FPOPFLT */ 139 10, /* 25 T_TSSFLT */ 140 11, /* 26 T_SEGNPFLT */ 141 12, /* 27 T_STKFLT */ 142 18, /* 28 T_MCHK */ 143 19, /* 29 T_XMMFLT */ 144 15 /* 30 T_RESERVED */ 145 }; 146 #define bsd_to_linux_trapcode(code) \ 147 ((code)<nitems(_bsd_to_linux_trapcode)? \ 148 _bsd_to_linux_trapcode[(code)]: \ 149 LINUX_T_UNKNOWN) 150 151 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode); 152 LINUX_VDSO_SYM_CHAR(linux_platform); 153 154 /* 155 * If FreeBSD & Linux have a difference of opinion about what a trap 156 * means, deal with it here. 157 * 158 * MPSAFE 159 */ 160 static int 161 linux_translate_traps(int signal, int trap_code) 162 { 163 164 if (signal != SIGBUS) 165 return (signal); 166 switch (trap_code) { 167 case T_PROTFLT: 168 case T_TSSFLT: 169 case T_DOUBLEFLT: 170 case T_PAGEFLT: 171 return (SIGSEGV); 172 default: 173 return (signal); 174 } 175 } 176 177 static int 178 linux_fetch_syscall_args(struct thread *td) 179 { 180 struct proc *p; 181 struct trapframe *frame; 182 struct syscall_args *sa; 183 184 p = td->td_proc; 185 frame = td->td_frame; 186 sa = &td->td_sa; 187 188 sa->args[0] = frame->tf_rdi; 189 sa->args[1] = frame->tf_rsi; 190 sa->args[2] = frame->tf_rdx; 191 sa->args[3] = frame->tf_rcx; 192 sa->args[4] = frame->tf_r8; 193 sa->args[5] = frame->tf_r9; 194 sa->code = frame->tf_rax; 195 196 if (sa->code >= p->p_sysent->sv_size) 197 /* nosys */ 198 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 199 else 200 sa->callp = &p->p_sysent->sv_table[sa->code]; 201 202 td->td_retval[0] = 0; 203 return (0); 204 } 205 206 static void 207 linux_set_syscall_retval(struct thread *td, int error) 208 { 209 struct trapframe *frame = td->td_frame; 210 211 /* 212 * On Linux only %rcx and %r11 values are not preserved across 213 * the syscall. So, do not clobber %rdx and %r10. 214 */ 215 td->td_retval[1] = frame->tf_rdx; 216 if (error != EJUSTRETURN) 217 frame->tf_r10 = frame->tf_rcx; 218 219 cpu_set_syscall_retval(td, error); 220 221 if (__predict_false(error != 0)) { 222 if (error != ERESTART && error != EJUSTRETURN) 223 frame->tf_rax = linux_to_bsd_errno(error); 224 } 225 226 /* Restore all registers. */ 227 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 228 } 229 230 static int 231 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 232 { 233 Elf_Auxargs *args; 234 Elf_Auxinfo *argarray, *pos; 235 struct proc *p; 236 int error, issetugid; 237 238 p = imgp->proc; 239 args = (Elf64_Auxargs *)imgp->auxargs; 240 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 241 M_WAITOK | M_ZERO); 242 243 issetugid = p->p_flag & P_SUGID ? 1 : 0; 244 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 245 imgp->proc->p_sysent->sv_shared_page_base); 246 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature); 247 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 248 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 249 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 250 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 251 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 252 AUXARGS_ENTRY(pos, AT_BASE, args->base); 253 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 254 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 255 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 256 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 257 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 258 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 259 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 260 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 261 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 262 if (imgp->execpathp != 0) 263 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 264 if (args->execfd != -1) 265 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 266 AUXARGS_ENTRY(pos, AT_NULL, 0); 267 268 free(imgp->auxargs, M_TEMP); 269 imgp->auxargs = NULL; 270 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 271 272 error = copyout(argarray, (void *)base, 273 sizeof(*argarray) * LINUX_AT_COUNT); 274 free(argarray, M_TEMP); 275 return (error); 276 } 277 278 static int 279 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp) 280 { 281 Elf_Addr *base; 282 283 base = (Elf64_Addr *)*stack_base; 284 base--; 285 if (suword(base, (uint64_t)imgp->args->argc) == -1) 286 return (EFAULT); 287 288 *stack_base = (uintptr_t)base; 289 return (0); 290 } 291 292 /* 293 * Copy strings out to the new process address space, constructing new arg 294 * and env vector tables. Return a pointer to the base so that it can be used 295 * as the initial stack pointer. 296 */ 297 static int 298 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 299 { 300 int argc, envc, error; 301 char **vectp; 302 char *stringp; 303 uintptr_t destp, ustringp; 304 struct ps_strings *arginfo; 305 char canary[LINUX_AT_RANDOM_LEN]; 306 size_t execpath_len; 307 struct proc *p; 308 309 /* Calculate string base and vector table pointers. */ 310 if (imgp->execpath != NULL && imgp->auxargs != NULL) 311 execpath_len = strlen(imgp->execpath) + 1; 312 else 313 execpath_len = 0; 314 315 p = imgp->proc; 316 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 317 destp = (uintptr_t)arginfo; 318 319 if (execpath_len != 0) { 320 destp -= execpath_len; 321 destp = rounddown2(destp, sizeof(void *)); 322 imgp->execpathp = (void *)destp; 323 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 324 if (error != 0) 325 return (error); 326 } 327 328 /* Prepare the canary for SSP. */ 329 arc4rand(canary, sizeof(canary), 0); 330 destp -= roundup(sizeof(canary), sizeof(void *)); 331 imgp->canary = (void *)destp; 332 error = copyout(canary, imgp->canary, sizeof(canary)); 333 if (error != 0) 334 return (error); 335 336 /* Allocate room for the argument and environment strings. */ 337 destp -= ARG_MAX - imgp->args->stringspace; 338 destp = rounddown2(destp, sizeof(void *)); 339 ustringp = destp; 340 341 if (imgp->auxargs) { 342 /* 343 * Allocate room on the stack for the ELF auxargs 344 * array. It has LINUX_AT_COUNT entries. 345 */ 346 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo); 347 destp = rounddown2(destp, sizeof(void *)); 348 } 349 350 vectp = (char **)destp; 351 352 /* 353 * Allocate room for the argv[] and env vectors including the 354 * terminating NULL pointers. 355 */ 356 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 357 358 /* 359 * Starting with 2.24, glibc depends on a 16-byte stack alignment. 360 * One "long argc" will be prepended later. 361 */ 362 vectp = (char **)((((uintptr_t)vectp + 8) & ~0xF) - 8); 363 364 /* vectp also becomes our initial stack base. */ 365 *stack_base = (uintptr_t)vectp; 366 367 stringp = imgp->args->begin_argv; 368 argc = imgp->args->argc; 369 envc = imgp->args->envc; 370 371 /* Copy out strings - arguments and environment. */ 372 error = copyout(stringp, (void *)ustringp, 373 ARG_MAX - imgp->args->stringspace); 374 if (error != 0) 375 return (error); 376 377 /* Fill in "ps_strings" struct for ps, w, etc. */ 378 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 379 suword(&arginfo->ps_nargvstr, argc) != 0) 380 return (EFAULT); 381 382 /* Fill in argument portion of vector table. */ 383 for (; argc > 0; --argc) { 384 if (suword(vectp++, ustringp) != 0) 385 return (EFAULT); 386 while (*stringp++ != 0) 387 ustringp++; 388 ustringp++; 389 } 390 391 /* A null vector table pointer separates the argp's from the envp's. */ 392 if (suword(vectp++, 0) != 0) 393 return (EFAULT); 394 395 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 396 suword(&arginfo->ps_nenvstr, envc) != 0) 397 return (EFAULT); 398 399 /* Fill in environment portion of vector table. */ 400 for (; envc > 0; --envc) { 401 if (suword(vectp++, ustringp) != 0) 402 return (EFAULT); 403 while (*stringp++ != 0) 404 ustringp++; 405 ustringp++; 406 } 407 408 /* The end of the vector table is a null pointer. */ 409 if (suword(vectp, 0) != 0) 410 return (EFAULT); 411 412 if (imgp->auxargs) { 413 vectp++; 414 error = imgp->sysent->sv_copyout_auxargs(imgp, 415 (uintptr_t)vectp); 416 if (error != 0) 417 return (error); 418 } 419 420 return (0); 421 } 422 423 /* 424 * Reset registers to default values on exec. 425 */ 426 static void 427 linux_exec_setregs(struct thread *td, struct image_params *imgp, 428 uintptr_t stack) 429 { 430 struct trapframe *regs; 431 struct pcb *pcb; 432 register_t saved_rflags; 433 434 regs = td->td_frame; 435 pcb = td->td_pcb; 436 437 if (td->td_proc->p_md.md_ldt != NULL) 438 user_ldt_free(td); 439 440 pcb->pcb_fsbase = 0; 441 pcb->pcb_gsbase = 0; 442 clear_pcb_flags(pcb, PCB_32BIT); 443 pcb->pcb_initial_fpucw = __LINUX_NPXCW__; 444 set_pcb_flags(pcb, PCB_FULL_IRET); 445 446 saved_rflags = regs->tf_rflags & PSL_T; 447 bzero((char *)regs, sizeof(struct trapframe)); 448 regs->tf_rip = imgp->entry_addr; 449 regs->tf_rsp = stack; 450 regs->tf_rflags = PSL_USER | saved_rflags; 451 regs->tf_ss = _udatasel; 452 regs->tf_cs = _ucodesel; 453 regs->tf_ds = _udatasel; 454 regs->tf_es = _udatasel; 455 regs->tf_fs = _ufssel; 456 regs->tf_gs = _ugssel; 457 regs->tf_flags = TF_HASSEGS; 458 459 /* 460 * Reset the hardware debug registers if they were in use. 461 * They won't have any meaning for the newly exec'd process. 462 */ 463 if (pcb->pcb_flags & PCB_DBREGS) { 464 pcb->pcb_dr0 = 0; 465 pcb->pcb_dr1 = 0; 466 pcb->pcb_dr2 = 0; 467 pcb->pcb_dr3 = 0; 468 pcb->pcb_dr6 = 0; 469 pcb->pcb_dr7 = 0; 470 if (pcb == curpcb) { 471 /* 472 * Clear the debug registers on the running 473 * CPU, otherwise they will end up affecting 474 * the next process we switch to. 475 */ 476 reset_dbregs(); 477 } 478 clear_pcb_flags(pcb, PCB_DBREGS); 479 } 480 481 /* 482 * Drop the FP state if we hold it, so that the process gets a 483 * clean FP state if it uses the FPU again. 484 */ 485 fpstate_drop(td); 486 } 487 488 /* 489 * Copied from amd64/amd64/machdep.c 490 * 491 * XXX fpu state need? don't think so 492 */ 493 int 494 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 495 { 496 struct proc *p; 497 struct l_ucontext uc; 498 struct l_sigcontext *context; 499 struct trapframe *regs; 500 unsigned long rflags; 501 int error; 502 ksiginfo_t ksi; 503 504 regs = td->td_frame; 505 error = copyin((void *)regs->tf_rbx, &uc, sizeof(uc)); 506 if (error != 0) 507 return (error); 508 509 p = td->td_proc; 510 context = &uc.uc_mcontext; 511 rflags = context->sc_rflags; 512 513 /* 514 * Don't allow users to change privileged or reserved flags. 515 */ 516 /* 517 * XXX do allow users to change the privileged flag PSL_RF. 518 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers 519 * should sometimes set it there too. tf_rflags is kept in 520 * the signal context during signal handling and there is no 521 * other place to remember it, so the PSL_RF bit may be 522 * corrupted by the signal handler without us knowing. 523 * Corruption of the PSL_RF bit at worst causes one more or 524 * one less debugger trap, so allowing it is fairly harmless. 525 */ 526 527 #define RFLAG_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 528 if (!RFLAG_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 529 printf("linux_rt_sigreturn: rflags = 0x%lx\n", rflags); 530 return (EINVAL); 531 } 532 533 /* 534 * Don't allow users to load a valid privileged %cs. Let the 535 * hardware check for invalid selectors, excess privilege in 536 * other selectors, invalid %eip's and invalid %esp's. 537 */ 538 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 539 if (!CS_SECURE(context->sc_cs)) { 540 printf("linux_rt_sigreturn: cs = 0x%x\n", context->sc_cs); 541 ksiginfo_init_trap(&ksi); 542 ksi.ksi_signo = SIGBUS; 543 ksi.ksi_code = BUS_OBJERR; 544 ksi.ksi_trapno = T_PROTFLT; 545 ksi.ksi_addr = (void *)regs->tf_rip; 546 trapsignal(td, &ksi); 547 return (EINVAL); 548 } 549 550 PROC_LOCK(p); 551 linux_to_bsd_sigset(&uc.uc_sigmask, &td->td_sigmask); 552 SIG_CANTMASK(td->td_sigmask); 553 signotify(td); 554 PROC_UNLOCK(p); 555 556 regs->tf_rdi = context->sc_rdi; 557 regs->tf_rsi = context->sc_rsi; 558 regs->tf_rdx = context->sc_rdx; 559 regs->tf_rbp = context->sc_rbp; 560 regs->tf_rbx = context->sc_rbx; 561 regs->tf_rcx = context->sc_rcx; 562 regs->tf_rax = context->sc_rax; 563 regs->tf_rip = context->sc_rip; 564 regs->tf_rsp = context->sc_rsp; 565 regs->tf_r8 = context->sc_r8; 566 regs->tf_r9 = context->sc_r9; 567 regs->tf_r10 = context->sc_r10; 568 regs->tf_r11 = context->sc_r11; 569 regs->tf_r12 = context->sc_r12; 570 regs->tf_r13 = context->sc_r13; 571 regs->tf_r14 = context->sc_r14; 572 regs->tf_r15 = context->sc_r15; 573 regs->tf_cs = context->sc_cs; 574 regs->tf_err = context->sc_err; 575 regs->tf_rflags = rflags; 576 577 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 578 return (EJUSTRETURN); 579 } 580 581 /* 582 * copied from amd64/amd64/machdep.c 583 * 584 * Send an interrupt to process. 585 */ 586 static void 587 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 588 { 589 struct l_rt_sigframe sf, *sfp; 590 struct proc *p; 591 struct thread *td; 592 struct sigacts *psp; 593 caddr_t sp; 594 struct trapframe *regs; 595 int sig, code; 596 int oonstack; 597 598 td = curthread; 599 p = td->td_proc; 600 PROC_LOCK_ASSERT(p, MA_OWNED); 601 sig = ksi->ksi_signo; 602 psp = p->p_sigacts; 603 code = ksi->ksi_code; 604 mtx_assert(&psp->ps_mtx, MA_OWNED); 605 regs = td->td_frame; 606 oonstack = sigonstack(regs->tf_rsp); 607 608 LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u", 609 catcher, sig, mask, code); 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 sizeof(struct l_rt_sigframe); 616 } else 617 sp = (caddr_t)regs->tf_rsp - sizeof(struct l_rt_sigframe) - 128; 618 /* Align to 16 bytes. */ 619 sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul); 620 mtx_unlock(&psp->ps_mtx); 621 622 /* Translate the signal. */ 623 sig = bsd_to_linux_signal(sig); 624 625 /* Save user context. */ 626 bzero(&sf, sizeof(sf)); 627 bsd_to_linux_sigset(mask, &sf.sf_sc.uc_sigmask); 628 bsd_to_linux_sigset(mask, &sf.sf_sc.uc_mcontext.sc_mask); 629 630 sf.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 631 sf.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size; 632 sf.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 633 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 634 PROC_UNLOCK(p); 635 636 sf.sf_sc.uc_mcontext.sc_rdi = regs->tf_rdi; 637 sf.sf_sc.uc_mcontext.sc_rsi = regs->tf_rsi; 638 sf.sf_sc.uc_mcontext.sc_rdx = regs->tf_rdx; 639 sf.sf_sc.uc_mcontext.sc_rbp = regs->tf_rbp; 640 sf.sf_sc.uc_mcontext.sc_rbx = regs->tf_rbx; 641 sf.sf_sc.uc_mcontext.sc_rcx = regs->tf_rcx; 642 sf.sf_sc.uc_mcontext.sc_rax = regs->tf_rax; 643 sf.sf_sc.uc_mcontext.sc_rip = regs->tf_rip; 644 sf.sf_sc.uc_mcontext.sc_rsp = regs->tf_rsp; 645 sf.sf_sc.uc_mcontext.sc_r8 = regs->tf_r8; 646 sf.sf_sc.uc_mcontext.sc_r9 = regs->tf_r9; 647 sf.sf_sc.uc_mcontext.sc_r10 = regs->tf_r10; 648 sf.sf_sc.uc_mcontext.sc_r11 = regs->tf_r11; 649 sf.sf_sc.uc_mcontext.sc_r12 = regs->tf_r12; 650 sf.sf_sc.uc_mcontext.sc_r13 = regs->tf_r13; 651 sf.sf_sc.uc_mcontext.sc_r14 = regs->tf_r14; 652 sf.sf_sc.uc_mcontext.sc_r15 = regs->tf_r15; 653 sf.sf_sc.uc_mcontext.sc_cs = regs->tf_cs; 654 sf.sf_sc.uc_mcontext.sc_rflags = regs->tf_rflags; 655 sf.sf_sc.uc_mcontext.sc_err = regs->tf_err; 656 sf.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 657 sf.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 658 659 /* Build the argument list for the signal handler. */ 660 regs->tf_rdi = sig; /* arg 1 in %rdi */ 661 regs->tf_rax = 0; 662 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */ 663 regs->tf_rdx = (register_t)&sfp->sf_sc; /* arg 3 in %rdx */ 664 665 sf.sf_handler = catcher; 666 /* Fill in POSIX parts. */ 667 ksiginfo_to_lsiginfo(ksi, &sf.sf_si, sig); 668 669 /* Copy the sigframe out to the user's stack. */ 670 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 671 PROC_LOCK(p); 672 sigexit(td, SIGILL); 673 } 674 675 regs->tf_rsp = (long)sfp; 676 regs->tf_rip = linux_rt_sigcode; 677 regs->tf_rflags &= ~(PSL_T | PSL_D); 678 regs->tf_cs = _ucodesel; 679 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 680 PROC_LOCK(p); 681 mtx_lock(&psp->ps_mtx); 682 } 683 684 #define LINUX_VSYSCALL_START (-10UL << 20) 685 #define LINUX_VSYSCALL_SZ 1024 686 687 const unsigned long linux_vsyscall_vector[] = { 688 LINUX_SYS_gettimeofday, 689 LINUX_SYS_linux_time, 690 LINUX_SYS_linux_getcpu, 691 }; 692 693 static int 694 linux_vsyscall(struct thread *td) 695 { 696 struct trapframe *frame; 697 uint64_t retqaddr; 698 int code, traced; 699 int error; 700 701 frame = td->td_frame; 702 703 /* Check %rip for vsyscall area. */ 704 if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START)) 705 return (EINVAL); 706 if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0) 707 return (EINVAL); 708 code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ; 709 if (code >= nitems(linux_vsyscall_vector)) 710 return (EINVAL); 711 712 /* 713 * vsyscall called as callq *(%rax), so we must 714 * use return address from %rsp and also fixup %rsp. 715 */ 716 error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr)); 717 if (error) 718 return (error); 719 720 frame->tf_rip = retqaddr; 721 frame->tf_rax = linux_vsyscall_vector[code]; 722 frame->tf_rsp += 8; 723 724 traced = (frame->tf_flags & PSL_T); 725 726 amd64_syscall(td, traced); 727 728 return (0); 729 } 730 731 struct sysentvec elf_linux_sysvec = { 732 .sv_size = LINUX_SYS_MAXSYSCALL, 733 .sv_table = linux_sysent, 734 .sv_transtrap = linux_translate_traps, 735 .sv_fixup = linux_fixup_elf, 736 .sv_sendsig = linux_rt_sendsig, 737 .sv_sigcode = &_binary_linux_locore_o_start, 738 .sv_szsigcode = &linux_szsigcode, 739 .sv_name = "Linux ELF64", 740 .sv_coredump = elf64_coredump, 741 .sv_imgact_try = linux_exec_imgact_try, 742 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 743 .sv_minuser = VM_MIN_ADDRESS, 744 .sv_maxuser = VM_MAXUSER_ADDRESS_LA48, 745 .sv_usrstack = USRSTACK_LA48, 746 .sv_psstrings = PS_STRINGS_LA48, 747 .sv_stackprot = VM_PROT_ALL, 748 .sv_copyout_auxargs = linux_copyout_auxargs, 749 .sv_copyout_strings = linux_copyout_strings, 750 .sv_setregs = linux_exec_setregs, 751 .sv_fixlimit = NULL, 752 .sv_maxssiz = NULL, 753 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP, 754 .sv_set_syscall_retval = linux_set_syscall_retval, 755 .sv_fetch_syscall_args = linux_fetch_syscall_args, 756 .sv_syscallnames = NULL, 757 .sv_shared_page_base = SHAREDPAGE_LA48, 758 .sv_shared_page_len = PAGE_SIZE, 759 .sv_schedtail = linux_schedtail, 760 .sv_thread_detach = linux_thread_detach, 761 .sv_trap = linux_vsyscall, 762 }; 763 764 static void 765 linux_vdso_install(void *param) 766 { 767 768 amd64_lower_shared_page(&elf_linux_sysvec); 769 770 linux_szsigcode = (&_binary_linux_locore_o_end - 771 &_binary_linux_locore_o_start); 772 773 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 774 panic("Linux invalid vdso size\n"); 775 776 __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 777 778 linux_shared_page_obj = __elfN(linux_shared_page_init) 779 (&linux_shared_page_mapping); 780 781 __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 782 783 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping, 784 linux_szsigcode); 785 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 786 787 linux_kplatform = linux_shared_page_mapping + 788 (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base); 789 } 790 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 791 linux_vdso_install, NULL); 792 793 static void 794 linux_vdso_deinstall(void *param) 795 { 796 797 __elfN(linux_shared_page_fini)(linux_shared_page_obj); 798 } 799 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 800 linux_vdso_deinstall, NULL); 801 802 static char GNULINUX_ABI_VENDOR[] = "GNU"; 803 static int GNULINUX_ABI_DESC = 0; 804 805 static bool 806 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 807 { 808 const Elf32_Word *desc; 809 uintptr_t p; 810 811 p = (uintptr_t)(note + 1); 812 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 813 814 desc = (const Elf32_Word *)p; 815 if (desc[0] != GNULINUX_ABI_DESC) 816 return (false); 817 818 /* 819 * For Linux we encode osrel using the Linux convention of 820 * (version << 16) | (major << 8) | (minor) 821 * See macro in linux_mib.h 822 */ 823 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 824 825 return (true); 826 } 827 828 static Elf_Brandnote linux64_brandnote = { 829 .hdr.n_namesz = sizeof(GNULINUX_ABI_VENDOR), 830 .hdr.n_descsz = 16, 831 .hdr.n_type = 1, 832 .vendor = GNULINUX_ABI_VENDOR, 833 .flags = BN_TRANSLATE_OSREL, 834 .trans_osrel = linux_trans_osrel 835 }; 836 837 static Elf64_Brandinfo linux_glibc2brand = { 838 .brand = ELFOSABI_LINUX, 839 .machine = EM_X86_64, 840 .compat_3_brand = "Linux", 841 .emul_path = linux_emul_path, 842 .interp_path = "/lib64/ld-linux-x86-64.so.2", 843 .sysvec = &elf_linux_sysvec, 844 .interp_newpath = NULL, 845 .brand_note = &linux64_brandnote, 846 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 847 }; 848 849 static Elf64_Brandinfo linux_glibc2brandshort = { 850 .brand = ELFOSABI_LINUX, 851 .machine = EM_X86_64, 852 .compat_3_brand = "Linux", 853 .emul_path = linux_emul_path, 854 .interp_path = "/lib64/ld-linux.so.2", 855 .sysvec = &elf_linux_sysvec, 856 .interp_newpath = NULL, 857 .brand_note = &linux64_brandnote, 858 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 859 }; 860 861 static Elf64_Brandinfo linux_muslbrand = { 862 .brand = ELFOSABI_LINUX, 863 .machine = EM_X86_64, 864 .compat_3_brand = "Linux", 865 .emul_path = linux_emul_path, 866 .interp_path = "/lib/ld-musl-x86_64.so.1", 867 .sysvec = &elf_linux_sysvec, 868 .interp_newpath = NULL, 869 .brand_note = &linux64_brandnote, 870 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 871 }; 872 873 Elf64_Brandinfo *linux_brandlist[] = { 874 &linux_glibc2brand, 875 &linux_glibc2brandshort, 876 &linux_muslbrand, 877 NULL 878 }; 879 880 static int 881 linux64_elf_modevent(module_t mod, int type, void *data) 882 { 883 Elf64_Brandinfo **brandinfo; 884 int error; 885 struct linux_ioctl_handler **lihp; 886 887 error = 0; 888 889 switch(type) { 890 case MOD_LOAD: 891 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 892 ++brandinfo) 893 if (elf64_insert_brand_entry(*brandinfo) < 0) 894 error = EINVAL; 895 if (error == 0) { 896 SET_FOREACH(lihp, linux_ioctl_handler_set) 897 linux_ioctl_register_handler(*lihp); 898 stclohz = (stathz ? stathz : hz); 899 if (bootverbose) 900 printf("Linux x86-64 ELF exec handler installed\n"); 901 } else 902 printf("cannot insert Linux x86-64 ELF brand handler\n"); 903 break; 904 case MOD_UNLOAD: 905 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 906 ++brandinfo) 907 if (elf64_brand_inuse(*brandinfo)) 908 error = EBUSY; 909 if (error == 0) { 910 for (brandinfo = &linux_brandlist[0]; 911 *brandinfo != NULL; ++brandinfo) 912 if (elf64_remove_brand_entry(*brandinfo) < 0) 913 error = EINVAL; 914 } 915 if (error == 0) { 916 SET_FOREACH(lihp, linux_ioctl_handler_set) 917 linux_ioctl_unregister_handler(*lihp); 918 if (bootverbose) 919 printf("Linux ELF exec handler removed\n"); 920 } else 921 printf("Could not deinstall ELF interpreter entry\n"); 922 break; 923 default: 924 return (EOPNOTSUPP); 925 } 926 return (error); 927 } 928 929 static moduledata_t linux64_elf_mod = { 930 "linux64elf", 931 linux64_elf_modevent, 932 0 933 }; 934 935 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 936 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); 937 FEATURE(linux64, "Linux 64bit support"); 938