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