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
8 * Copyright (c) 2013, 2021 Dmitry Chagin <dchagin@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #define __ELF_WORD_SIZE 64
39 #include <sys/param.h>
41 #include <sys/imgact.h>
42 #include <sys/imgact_elf.h>
43 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
50 #include <sys/stddef.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysent.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
60 #include <machine/cpu.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/specialreg.h>
64 #include <machine/trap.h>
66 #include <x86/linux/linux_x86.h>
67 #include <amd64/linux/linux.h>
68 #include <amd64/linux/linux_proto.h>
69 #include <compat/linux/linux_elf.h>
70 #include <compat/linux/linux_emul.h>
71 #include <compat/linux/linux_fork.h>
72 #include <compat/linux/linux_ioctl.h>
73 #include <compat/linux/linux_mib.h>
74 #include <compat/linux/linux_misc.h>
75 #include <compat/linux/linux_signal.h>
76 #include <compat/linux/linux_sysproto.h>
77 #include <compat/linux/linux_util.h>
78 #include <compat/linux/linux_vdso.h>
80 #include <x86/linux/linux_x86_sigframe.h>
82 _Static_assert(sizeof(struct l_fpstate) ==
83 sizeof(__typeof(((mcontext_t *)0)->mc_fpstate)),
84 "fxsave area size incorrect");
86 MODULE_VERSION(linux64, 1);
88 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
89 #define LINUX_VDSOPAGE_LA48 (VM_MAXUSER_ADDRESS_LA48 - \
91 #define LINUX_SHAREDPAGE_LA48 (LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
93 * PAGE_SIZE - the size
94 * of the native SHAREDPAGE
96 #define LINUX_USRSTACK_LA48 LINUX_SHAREDPAGE_LA48
97 #define LINUX_PS_STRINGS_LA48 (LINUX_USRSTACK_LA48 - \
98 sizeof(struct ps_strings))
100 static int linux_szsigcode;
101 static vm_object_t linux_vdso_obj;
102 static char *linux_vdso_mapping;
103 extern char _binary_linux_vdso_so_o_start;
104 extern char _binary_linux_vdso_so_o_end;
105 static vm_offset_t linux_vdso_base;
107 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
108 extern const char *linux_syscallnames[];
110 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
112 static void linux_vdso_install(const void *param);
113 static void linux_vdso_deinstall(const void *param);
114 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
115 static void linux_set_syscall_retval(struct thread *td, int error);
116 static int linux_fetch_syscall_args(struct thread *td);
117 static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
119 static void linux_exec_sysvec_init(void *param);
120 static int linux_on_exec_vmspace(struct proc *p,
121 struct image_params *imgp);
122 static void linux_set_fork_retval(struct thread *td);
123 static int linux_vsyscall(struct thread *td);
125 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
126 LINUX_VDSO_SYM_CHAR(linux_platform);
127 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
128 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
129 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
132 * According to the Intel x86 ISA 64-bit syscall
133 * saves %rip to %rcx and rflags to %r11. Registers on syscall entry:
134 * %rax system call number
135 * %rcx return address
144 * Then FreeBSD fast_syscall() move registers:
145 * %rcx -> trapframe.tf_rip
146 * %r10 -> trapframe.tf_rcx
149 linux_fetch_syscall_args(struct thread *td)
152 struct trapframe *frame;
153 struct syscall_args *sa;
156 frame = td->td_frame;
159 sa->args[0] = frame->tf_rdi;
160 sa->args[1] = frame->tf_rsi;
161 sa->args[2] = frame->tf_rdx;
162 sa->args[3] = frame->tf_rcx;
163 sa->args[4] = frame->tf_r8;
164 sa->args[5] = frame->tf_r9;
165 sa->code = frame->tf_rax;
166 sa->original_code = sa->code;
168 if (sa->code >= p->p_sysent->sv_size)
170 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
172 sa->callp = &p->p_sysent->sv_table[sa->code];
174 /* Restore r10 earlier to avoid doing this multiply times. */
175 frame->tf_r10 = frame->tf_rcx;
176 /* Restore %rcx for machine context. */
177 frame->tf_rcx = frame->tf_rip;
179 td->td_retval[0] = 0;
184 linux_set_syscall_retval(struct thread *td, int error)
186 struct trapframe *frame;
188 frame = td->td_frame;
192 frame->tf_rax = td->td_retval[0];
197 * Reconstruct pc, we know that 'syscall' is 2 bytes,
198 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
199 * We saved this in tf_err.
202 frame->tf_rip -= frame->tf_err;
209 frame->tf_rax = bsd_to_linux_errno(error);
214 * Differently from FreeBSD native ABI, on Linux only %rcx
215 * and %r11 values are not preserved across the syscall.
216 * Require full context restore to get all registers except
217 * those two restored at return to usermode.
219 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
223 linux_set_fork_retval(struct thread *td)
225 struct trapframe *frame = td->td_frame;
231 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
234 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
235 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
236 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());
237 AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
241 * Reset registers to default values on exec.
244 linux_exec_setregs(struct thread *td, struct image_params *imgp,
247 struct trapframe *regs;
249 register_t saved_rflags;
254 if (td->td_proc->p_md.md_ldt != NULL)
259 clear_pcb_flags(pcb, PCB_32BIT);
260 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
261 set_pcb_flags(pcb, PCB_FULL_IRET);
263 saved_rflags = regs->tf_rflags & PSL_T;
264 bzero((char *)regs, sizeof(struct trapframe));
265 regs->tf_rip = imgp->entry_addr;
266 regs->tf_rsp = stack;
267 regs->tf_rflags = PSL_USER | saved_rflags;
268 regs->tf_ss = _udatasel;
269 regs->tf_cs = _ucodesel;
270 regs->tf_ds = _udatasel;
271 regs->tf_es = _udatasel;
272 regs->tf_fs = _ufssel;
273 regs->tf_gs = _ugssel;
274 regs->tf_flags = TF_HASSEGS;
276 x86_clear_dbregs(pcb);
279 * Drop the FP state if we hold it, so that the process gets a
280 * clean FP state if it uses the FPU again.
286 linux_fxrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
288 struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
291 error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
294 bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
295 return (set_fpcontext(td, mcp, NULL, 0));
299 linux_xrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
301 struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
308 mcp->mc_xfpustate_len = cpu_max_ext_state_size - sizeof(struct savefpu);
310 /* Legacy region of an xsave area. */
311 error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
314 bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
316 /* Extended region of an xsave area. */
317 sc->sc_fpstate += sizeof(mcp->mc_fpstate);
318 xfpustate = (char *)fpu_save_area_alloc();
319 error = copyin(PTRIN(sc->sc_fpstate), xfpustate, mcp->mc_xfpustate_len);
321 fpu_save_area_free((struct savefpu *)xfpustate);
322 uprintf("pid %d (%s): linux xrstor failed\n", p->p_pid,
327 /* Linux specific end of xsave area marker. */
328 sc->sc_fpstate += mcp->mc_xfpustate_len;
329 error = copyin(PTRIN(sc->sc_fpstate), &magic2, LINUX_FP_XSTATE_MAGIC2_SIZE);
330 if (error != 0 || magic2 != LINUX_FP_XSTATE_MAGIC2) {
331 fpu_save_area_free((struct savefpu *)xfpustate);
332 uprintf("pid %d (%s): sigreturn magic2 0x%x error %d\n",
333 p->p_pid, td->td_name, magic2, error);
337 error = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
338 fpu_save_area_free((struct savefpu *)xfpustate);
340 uprintf("pid %d (%s): sigreturn set_fpcontext error %d\n",
341 p->p_pid, td->td_name, error);
347 linux_copyin_fpstate(struct thread *td, struct l_ucontext *uc)
351 bzero(&mc, sizeof(mc));
352 mc.mc_ownedfp = _MC_FPOWNED_FPU;
353 mc.mc_fpformat = _MC_FPFMT_XMM;
355 if ((uc->uc_flags & LINUX_UC_FP_XSTATE) != 0)
356 return (linux_xrstor(td, &mc, &uc->uc_mcontext));
358 return (linux_fxrstor(td, &mc, &uc->uc_mcontext));
362 * Copied from amd64/amd64/machdep.c
365 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
368 struct l_rt_sigframe sf;
369 struct l_sigcontext *context;
370 struct trapframe *regs;
371 unsigned long rflags;
377 error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf));
382 context = &sf.sf_uc.uc_mcontext;
383 rflags = context->sc_rflags;
386 * Don't allow users to change privileged or reserved flags.
389 * XXX do allow users to change the privileged flag PSL_RF.
390 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers
391 * should sometimes set it there too. tf_rflags is kept in
392 * the signal context during signal handling and there is no
393 * other place to remember it, so the PSL_RF bit may be
394 * corrupted by the signal handler without us knowing.
395 * Corruption of the PSL_RF bit at worst causes one more or
396 * one less debugger trap, so allowing it is fairly harmless.
398 if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
399 uprintf("pid %d comm %s linux mangled rflags %#lx\n",
400 p->p_pid, p->p_comm, rflags);
405 * Don't allow users to load a valid privileged %cs. Let the
406 * hardware check for invalid selectors, excess privilege in
407 * other selectors, invalid %eip's and invalid %esp's.
409 if (!CS_SECURE(context->sc_cs)) {
410 uprintf("pid %d comm %s linux mangled cs %#x\n",
411 p->p_pid, p->p_comm, context->sc_cs);
412 ksiginfo_init_trap(&ksi);
413 ksi.ksi_signo = SIGBUS;
414 ksi.ksi_code = BUS_OBJERR;
415 ksi.ksi_trapno = T_PROTFLT;
416 ksi.ksi_addr = (void *)regs->tf_rip;
417 trapsignal(td, &ksi);
421 linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask);
422 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
424 regs->tf_rdi = context->sc_rdi;
425 regs->tf_rsi = context->sc_rsi;
426 regs->tf_rdx = context->sc_rdx;
427 regs->tf_rbp = context->sc_rbp;
428 regs->tf_rbx = context->sc_rbx;
429 regs->tf_rcx = context->sc_rcx;
430 regs->tf_rax = context->sc_rax;
431 regs->tf_rip = context->sc_rip;
432 regs->tf_rsp = context->sc_rsp;
433 regs->tf_r8 = context->sc_r8;
434 regs->tf_r9 = context->sc_r9;
435 regs->tf_r10 = context->sc_r10;
436 regs->tf_r11 = context->sc_r11;
437 regs->tf_r12 = context->sc_r12;
438 regs->tf_r13 = context->sc_r13;
439 regs->tf_r14 = context->sc_r14;
440 regs->tf_r15 = context->sc_r15;
441 regs->tf_cs = context->sc_cs;
442 regs->tf_err = context->sc_err;
443 regs->tf_rflags = rflags;
445 error = linux_copyin_fpstate(td, &sf.sf_uc);
447 uprintf("pid %d comm %s linux can't restore fpu state %d\n",
448 p->p_pid, p->p_comm, error);
452 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
453 return (EJUSTRETURN);
457 linux_fxsave(mcontext_t *mcp, void *ufp)
459 struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
461 bzero(&fx->reserved2[0], sizeof(fx->reserved2));
462 return (copyout(fx, ufp, sizeof(*fx)));
466 linux_xsave(mcontext_t *mcp, char *xfpusave, char *ufp)
468 struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
472 /* Legacy region of an xsave area. */
473 fx->sw_reserved.magic1 = LINUX_FP_XSTATE_MAGIC1;
474 fx->sw_reserved.xstate_size = mcp->mc_xfpustate_len + sizeof(*fx);
475 fx->sw_reserved.extended_size = fx->sw_reserved.xstate_size +
476 LINUX_FP_XSTATE_MAGIC2_SIZE;
477 fx->sw_reserved.xfeatures = xsave_mask;
479 error = copyout(fx, ufp, sizeof(*fx));
484 /* Extended region of an xsave area. */
485 error = copyout(xfpusave, ufp, mcp->mc_xfpustate_len);
489 /* Linux specific end of xsave area marker. */
490 ufp += mcp->mc_xfpustate_len;
491 magic2 = LINUX_FP_XSTATE_MAGIC2;
492 return (copyout(&magic2, ufp, LINUX_FP_XSTATE_MAGIC2_SIZE));
496 linux_copyout_fpstate(struct thread *td, struct l_ucontext *uc, char **sp)
503 get_fpcontext(td, &mc, &xfpusave, &xfpusave_len);
504 KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present"));
506 /* Room for fxsave area. */
507 ufp -= sizeof(struct l_fpstate);
508 if (xfpusave != NULL) {
509 /* Room for xsave area. */
510 ufp -= (xfpusave_len + LINUX_FP_XSTATE_MAGIC2_SIZE);
511 uc->uc_flags |= LINUX_UC_FP_XSTATE;
513 *sp = ufp = (char *)((unsigned long)ufp & ~0x3Ful);
515 if (xfpusave != NULL)
516 return (linux_xsave(&mc, xfpusave, ufp));
518 return (linux_fxsave(&mc, ufp));
522 * copied from amd64/amd64/machdep.c
524 * Send an interrupt to process.
527 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
529 struct l_rt_sigframe sf, *sfp;
534 struct trapframe *regs;
536 int oonstack, issiginfo;
540 PROC_LOCK_ASSERT(p, MA_OWNED);
541 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
543 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
544 code = ksi->ksi_code;
545 mtx_assert(&psp->ps_mtx, MA_OWNED);
547 oonstack = sigonstack(regs->tf_rsp);
549 LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
550 catcher, sig, mask, code);
552 bzero(&sf, sizeof(sf));
553 sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
554 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
555 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
556 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
558 /* Allocate space for the signal handler context. */
559 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
560 SIGISMEMBER(psp->ps_sigonstack, sig)) {
561 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
563 sp = (char *)regs->tf_rsp - 128;
565 mtx_unlock(&psp->ps_mtx);
568 if (linux_copyout_fpstate(td, &sf.sf_uc, &sp) != 0) {
569 uprintf("pid %d comm %s linux can't save fpu state, killing\n",
570 p->p_pid, p->p_comm);
574 sf.sf_uc.uc_mcontext.sc_fpstate = (register_t)sp;
576 /* Make room, keeping the stack aligned. */
577 sp -= sizeof(struct l_rt_sigframe);
578 sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
580 /* Save user context. */
581 bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
582 sf.sf_uc.uc_mcontext.sc_mask = sf.sf_uc.uc_sigmask;
583 sf.sf_uc.uc_mcontext.sc_rdi = regs->tf_rdi;
584 sf.sf_uc.uc_mcontext.sc_rsi = regs->tf_rsi;
585 sf.sf_uc.uc_mcontext.sc_rdx = regs->tf_rdx;
586 sf.sf_uc.uc_mcontext.sc_rbp = regs->tf_rbp;
587 sf.sf_uc.uc_mcontext.sc_rbx = regs->tf_rbx;
588 sf.sf_uc.uc_mcontext.sc_rcx = regs->tf_rcx;
589 sf.sf_uc.uc_mcontext.sc_rax = regs->tf_rax;
590 sf.sf_uc.uc_mcontext.sc_rip = regs->tf_rip;
591 sf.sf_uc.uc_mcontext.sc_rsp = regs->tf_rsp;
592 sf.sf_uc.uc_mcontext.sc_r8 = regs->tf_r8;
593 sf.sf_uc.uc_mcontext.sc_r9 = regs->tf_r9;
594 sf.sf_uc.uc_mcontext.sc_r10 = regs->tf_r10;
595 sf.sf_uc.uc_mcontext.sc_r11 = regs->tf_r11;
596 sf.sf_uc.uc_mcontext.sc_r12 = regs->tf_r12;
597 sf.sf_uc.uc_mcontext.sc_r13 = regs->tf_r13;
598 sf.sf_uc.uc_mcontext.sc_r14 = regs->tf_r14;
599 sf.sf_uc.uc_mcontext.sc_r15 = regs->tf_r15;
600 sf.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
601 sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
602 sf.sf_uc.uc_mcontext.sc_err = regs->tf_err;
603 sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
604 sf.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
606 /* Translate the signal. */
607 sig = bsd_to_linux_signal(sig);
608 /* Fill in POSIX parts. */
609 siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
611 /* Copy the sigframe out to the user's stack. */
612 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
613 uprintf("pid %d comm %s has trashed its stack, killing\n",
614 p->p_pid, p->p_comm);
620 /* Build the argument list for the signal handler. */
621 regs->tf_rdi = sig; /* arg 1 in %rdi */
624 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */
625 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */
630 regs->tf_rcx = (register_t)catcher;
631 regs->tf_rsp = (long)sfp;
632 regs->tf_rip = linux_rt_sigcode;
633 regs->tf_rflags &= ~(PSL_T | PSL_D);
634 regs->tf_cs = _ucodesel;
635 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
637 mtx_lock(&psp->ps_mtx);
640 #define LINUX_VSYSCALL_START (-10UL << 20)
641 #define LINUX_VSYSCALL_SZ 1024
643 const unsigned long linux_vsyscall_vector[] = {
644 LINUX_SYS_gettimeofday,
645 LINUX_SYS_linux_time,
646 LINUX_SYS_linux_getcpu,
650 linux_vsyscall(struct thread *td)
652 struct trapframe *frame;
657 frame = td->td_frame;
659 /* Check %rip for vsyscall area. */
660 if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
662 if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
664 code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
665 if (code >= nitems(linux_vsyscall_vector))
669 * vsyscall called as callq *(%rax), so we must
670 * use return address from %rsp and also fixup %rsp.
672 error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
676 frame->tf_rip = retqaddr;
677 frame->tf_rax = linux_vsyscall_vector[code];
680 traced = (frame->tf_flags & PSL_T);
682 amd64_syscall(td, traced);
687 struct sysentvec elf_linux_sysvec = {
688 .sv_size = LINUX_SYS_MAXSYSCALL,
689 .sv_table = linux_sysent,
690 .sv_fixup = __elfN(freebsd_fixup),
691 .sv_sendsig = linux_rt_sendsig,
692 .sv_sigcode = &_binary_linux_vdso_so_o_start,
693 .sv_szsigcode = &linux_szsigcode,
694 .sv_name = "Linux ELF64",
695 .sv_coredump = elf64_coredump,
696 .sv_elf_core_osabi = ELFOSABI_NONE,
697 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
698 .sv_elf_core_prepare_notes = linux64_prepare_notes,
699 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
700 .sv_minuser = VM_MIN_ADDRESS,
701 .sv_maxuser = VM_MAXUSER_ADDRESS_LA48,
702 .sv_usrstack = LINUX_USRSTACK_LA48,
703 .sv_psstrings = LINUX_PS_STRINGS_LA48,
704 .sv_psstringssz = sizeof(struct ps_strings),
705 .sv_stackprot = VM_PROT_ALL,
706 .sv_copyout_auxargs = __linuxN(copyout_auxargs),
707 .sv_copyout_strings = __linuxN(copyout_strings),
708 .sv_setregs = linux_exec_setregs,
711 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
712 SV_SIG_WAITNDQ | SV_TIMEKEEP,
713 .sv_set_syscall_retval = linux_set_syscall_retval,
714 .sv_fetch_syscall_args = linux_fetch_syscall_args,
715 .sv_syscallnames = linux_syscallnames,
716 .sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
717 .sv_shared_page_len = PAGE_SIZE,
718 .sv_schedtail = linux_schedtail,
719 .sv_thread_detach = linux_thread_detach,
720 .sv_trap = linux_vsyscall,
723 .sv_onexec = linux_on_exec_vmspace,
724 .sv_onexit = linux_on_exit,
725 .sv_ontdexit = linux_thread_dtor,
726 .sv_setid_allowed = &linux_setid_allowed_query,
727 .sv_set_fork_retval = linux_set_fork_retval,
731 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
735 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
736 LINUX_VDSOPAGE_SIZE, imgp);
738 error = linux_on_exec(p, imgp);
743 * linux_vdso_install() and linux_exec_sysvec_init() must be called
744 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
747 linux_exec_sysvec_init(void *param)
749 l_uintptr_t *ktimekeep_base, *ktsc_selector;
750 struct sysentvec *sv;
754 amd64_lower_shared_page(sv);
755 /* Fill timekeep_base */
756 exec_sysvec_init(sv);
758 tkoff = kern_timekeep_base - linux_vdso_base;
759 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
760 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
762 tkoff = kern_tsc_selector - linux_vdso_base;
763 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
764 *ktsc_selector = linux_vdso_tsc_selector_idx();
766 printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
768 tkoff = kern_cpu_selector - linux_vdso_base;
769 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
770 *ktsc_selector = linux_vdso_cpu_selector_idx();
772 printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
774 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
775 linux_exec_sysvec_init, &elf_linux_sysvec);
778 linux_vdso_install(const void *param)
780 char *vdso_start = &_binary_linux_vdso_so_o_start;
781 char *vdso_end = &_binary_linux_vdso_so_o_end;
783 linux_szsigcode = vdso_end - vdso_start;
784 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
786 linux_vdso_base = LINUX_VDSOPAGE_LA48;
787 if (hw_lower_amd64_sharedpage != 0)
788 linux_vdso_base -= PAGE_SIZE;
790 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
792 linux_vdso_obj = __elfN(linux_shared_page_init)
793 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
794 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
796 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
798 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
799 linux_vdso_install, NULL);
802 linux_vdso_deinstall(const void *param)
805 __elfN(linux_shared_page_fini)(linux_vdso_obj,
806 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
808 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
809 linux_vdso_deinstall, NULL);
812 linux_vdso_reloc(char *mapping, Elf_Addr offset)
814 const Elf_Ehdr *ehdr;
815 const Elf_Shdr *shdr;
816 Elf64_Addr *where, val;
817 Elf_Size rtype, symidx;
818 const Elf_Rela *rela;
819 Elf_Addr addr, addend;
826 ehdr = (const Elf_Ehdr *)mapping;
827 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
828 for (i = 0; i < ehdr->e_shnum; i++)
830 switch (shdr[i].sh_type) {
832 printf("Linux x86_64 vDSO: unexpected Rel section\n");
835 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
836 relacnt = shdr[i].sh_size / sizeof(*rela);
840 for (j = 0; j < relacnt; j++, rela++) {
841 where = (Elf_Addr *)(mapping + rela->r_offset);
842 addend = rela->r_addend;
843 rtype = ELF_R_TYPE(rela->r_info);
844 symidx = ELF_R_SYM(rela->r_info);
847 case R_X86_64_NONE: /* none */
850 case R_X86_64_RELATIVE: /* B + A */
851 addr = (Elf_Addr)(offset + addend);
856 case R_X86_64_IRELATIVE:
857 printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
858 "symbol index %ld\n", symidx);
861 printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
862 "symbol index %ld\n", rtype, symidx);
867 static Elf_Brandnote linux64_brandnote = {
868 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
871 .vendor = GNU_ABI_VENDOR,
872 .flags = BN_TRANSLATE_OSREL,
873 .trans_osrel = linux_trans_osrel
876 static Elf64_Brandinfo linux_glibc2brand = {
877 .brand = ELFOSABI_LINUX,
878 .machine = EM_X86_64,
879 .compat_3_brand = "Linux",
880 .interp_path = "/lib64/ld-linux-x86-64.so.2",
881 .sysvec = &elf_linux_sysvec,
882 .interp_newpath = NULL,
883 .brand_note = &linux64_brandnote,
884 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
887 static Elf64_Brandinfo linux_glibc2brandshort = {
888 .brand = ELFOSABI_LINUX,
889 .machine = EM_X86_64,
890 .compat_3_brand = "Linux",
891 .interp_path = "/lib64/ld-linux.so.2",
892 .sysvec = &elf_linux_sysvec,
893 .interp_newpath = NULL,
894 .brand_note = &linux64_brandnote,
895 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
898 static Elf64_Brandinfo linux_muslbrand = {
899 .brand = ELFOSABI_LINUX,
900 .machine = EM_X86_64,
901 .compat_3_brand = "Linux",
902 .interp_path = "/lib/ld-musl-x86_64.so.1",
903 .sysvec = &elf_linux_sysvec,
904 .interp_newpath = NULL,
905 .brand_note = &linux64_brandnote,
906 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
907 LINUX_BI_FUTEX_REQUEUE
910 Elf64_Brandinfo *linux_brandlist[] = {
912 &linux_glibc2brandshort,
918 linux64_elf_modevent(module_t mod, int type, void *data)
920 Elf64_Brandinfo **brandinfo;
922 struct linux_ioctl_handler **lihp;
928 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
930 if (elf64_insert_brand_entry(*brandinfo) < 0)
933 SET_FOREACH(lihp, linux_ioctl_handler_set)
934 linux_ioctl_register_handler(*lihp);
935 stclohz = (stathz ? stathz : hz);
937 printf("Linux x86-64 ELF exec handler installed\n");
939 printf("cannot insert Linux x86-64 ELF brand handler\n");
942 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
944 if (elf64_brand_inuse(*brandinfo))
947 for (brandinfo = &linux_brandlist[0];
948 *brandinfo != NULL; ++brandinfo)
949 if (elf64_remove_brand_entry(*brandinfo) < 0)
953 SET_FOREACH(lihp, linux_ioctl_handler_set)
954 linux_ioctl_unregister_handler(*lihp);
956 printf("Linux x86_64 ELF exec handler removed\n");
958 printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
966 static moduledata_t linux64_elf_mod = {
968 linux64_elf_modevent,
972 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
973 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
974 FEATURE(linux64, "Linux 64bit support");