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 MODULE_VERSION(linux64, 1);
84 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
85 #define LINUX_VDSOPAGE_LA48 (VM_MAXUSER_ADDRESS_LA48 - \
87 #define LINUX_SHAREDPAGE_LA48 (LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
89 * PAGE_SIZE - the size
90 * of the native SHAREDPAGE
92 #define LINUX_USRSTACK_LA48 LINUX_SHAREDPAGE_LA48
93 #define LINUX_PS_STRINGS_LA48 (LINUX_USRSTACK_LA48 - \
94 sizeof(struct ps_strings))
96 static int linux_szsigcode;
97 static vm_object_t linux_vdso_obj;
98 static char *linux_vdso_mapping;
99 extern char _binary_linux_vdso_so_o_start;
100 extern char _binary_linux_vdso_so_o_end;
101 static vm_offset_t linux_vdso_base;
103 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
104 extern const char *linux_syscallnames[];
106 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
108 static void linux_vdso_install(const void *param);
109 static void linux_vdso_deinstall(const void *param);
110 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
111 static void linux_set_syscall_retval(struct thread *td, int error);
112 static int linux_fetch_syscall_args(struct thread *td);
113 static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
115 static void linux_exec_sysvec_init(void *param);
116 static int linux_on_exec_vmspace(struct proc *p,
117 struct image_params *imgp);
118 static void linux_set_fork_retval(struct thread *td);
119 static int linux_vsyscall(struct thread *td);
121 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
122 LINUX_VDSO_SYM_CHAR(linux_platform);
123 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
124 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
125 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
128 linux_fetch_syscall_args(struct thread *td)
131 struct trapframe *frame;
132 struct syscall_args *sa;
135 frame = td->td_frame;
138 sa->args[0] = frame->tf_rdi;
139 sa->args[1] = frame->tf_rsi;
140 sa->args[2] = frame->tf_rdx;
141 sa->args[3] = frame->tf_rcx;
142 sa->args[4] = frame->tf_r8;
143 sa->args[5] = frame->tf_r9;
144 sa->code = frame->tf_rax;
145 sa->original_code = sa->code;
147 if (sa->code >= p->p_sysent->sv_size)
149 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
151 sa->callp = &p->p_sysent->sv_table[sa->code];
153 td->td_retval[0] = 0;
158 linux_set_syscall_retval(struct thread *td, int error)
160 struct trapframe *frame;
162 frame = td->td_frame;
166 frame->tf_rax = td->td_retval[0];
167 frame->tf_r10 = frame->tf_rcx;
172 * Reconstruct pc, we know that 'syscall' is 2 bytes,
173 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
174 * We saved this in tf_err.
177 frame->tf_rip -= frame->tf_err;
178 frame->tf_r10 = frame->tf_rcx;
185 frame->tf_rax = bsd_to_linux_errno(error);
186 frame->tf_r10 = frame->tf_rcx;
191 * Differently from FreeBSD native ABI, on Linux only %rcx
192 * and %r11 values are not preserved across the syscall.
193 * Require full context restore to get all registers except
194 * those two restored at return to usermode.
196 * XXX: Would be great to be able to avoid PCB_FULL_IRET
197 * for the error == 0 case.
199 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
203 linux_set_fork_retval(struct thread *td)
205 struct trapframe *frame = td->td_frame;
211 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
214 Elf_Auxinfo *argarray, *pos;
216 int error, issetugid;
219 args = (Elf64_Auxargs *)imgp->auxargs;
220 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
223 issetugid = p->p_flag & P_SUGID ? 1 : 0;
224 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
225 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
226 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
227 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
228 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
229 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
230 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
231 AUXARGS_ENTRY(pos, AT_BASE, args->base);
232 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
233 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
234 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
235 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
236 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
237 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
238 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
239 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
240 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
241 if (imgp->execpathp != 0)
242 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
243 if (args->execfd != -1)
244 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
245 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
246 AUXARGS_ENTRY(pos, AT_NULL, 0);
248 free(imgp->auxargs, M_TEMP);
249 imgp->auxargs = NULL;
250 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
252 error = copyout(argarray, (void *)base,
253 sizeof(*argarray) * LINUX_AT_COUNT);
254 free(argarray, M_TEMP);
259 * Reset registers to default values on exec.
262 linux_exec_setregs(struct thread *td, struct image_params *imgp,
265 struct trapframe *regs;
267 register_t saved_rflags;
272 if (td->td_proc->p_md.md_ldt != NULL)
277 clear_pcb_flags(pcb, PCB_32BIT);
278 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
279 set_pcb_flags(pcb, PCB_FULL_IRET);
281 saved_rflags = regs->tf_rflags & PSL_T;
282 bzero((char *)regs, sizeof(struct trapframe));
283 regs->tf_rip = imgp->entry_addr;
284 regs->tf_rsp = stack;
285 regs->tf_rflags = PSL_USER | saved_rflags;
286 regs->tf_ss = _udatasel;
287 regs->tf_cs = _ucodesel;
288 regs->tf_ds = _udatasel;
289 regs->tf_es = _udatasel;
290 regs->tf_fs = _ufssel;
291 regs->tf_gs = _ugssel;
292 regs->tf_flags = TF_HASSEGS;
294 x86_clear_dbregs(pcb);
297 * Drop the FP state if we hold it, so that the process gets a
298 * clean FP state if it uses the FPU again.
304 * Copied from amd64/amd64/machdep.c
307 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
310 struct l_rt_sigframe sf;
311 struct l_sigcontext *context;
312 struct trapframe *regs;
314 unsigned long rflags;
320 error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf));
325 context = &sf.sf_uc.uc_mcontext;
326 rflags = context->sc_rflags;
329 * Don't allow users to change privileged or reserved flags.
332 * XXX do allow users to change the privileged flag PSL_RF.
333 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers
334 * should sometimes set it there too. tf_rflags is kept in
335 * the signal context during signal handling and there is no
336 * other place to remember it, so the PSL_RF bit may be
337 * corrupted by the signal handler without us knowing.
338 * Corruption of the PSL_RF bit at worst causes one more or
339 * one less debugger trap, so allowing it is fairly harmless.
341 if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
342 uprintf("pid %d comm %s linux mangled rflags %#lx\n",
343 p->p_pid, p->p_comm, rflags);
348 * Don't allow users to load a valid privileged %cs. Let the
349 * hardware check for invalid selectors, excess privilege in
350 * other selectors, invalid %eip's and invalid %esp's.
352 if (!CS_SECURE(context->sc_cs)) {
353 uprintf("pid %d comm %s linux mangled cs %#x\n",
354 p->p_pid, p->p_comm, context->sc_cs);
355 ksiginfo_init_trap(&ksi);
356 ksi.ksi_signo = SIGBUS;
357 ksi.ksi_code = BUS_OBJERR;
358 ksi.ksi_trapno = T_PROTFLT;
359 ksi.ksi_addr = (void *)regs->tf_rip;
360 trapsignal(td, &ksi);
364 linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask);
365 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
367 regs->tf_rdi = context->sc_rdi;
368 regs->tf_rsi = context->sc_rsi;
369 regs->tf_rdx = context->sc_rdx;
370 regs->tf_rbp = context->sc_rbp;
371 regs->tf_rbx = context->sc_rbx;
372 regs->tf_rcx = context->sc_rcx;
373 regs->tf_rax = context->sc_rax;
374 regs->tf_rip = context->sc_rip;
375 regs->tf_rsp = context->sc_rsp;
376 regs->tf_r8 = context->sc_r8;
377 regs->tf_r9 = context->sc_r9;
378 regs->tf_r10 = context->sc_r10;
379 regs->tf_r11 = context->sc_r11;
380 regs->tf_r12 = context->sc_r12;
381 regs->tf_r13 = context->sc_r13;
382 regs->tf_r14 = context->sc_r14;
383 regs->tf_r15 = context->sc_r15;
384 regs->tf_cs = context->sc_cs;
385 regs->tf_err = context->sc_err;
386 regs->tf_rflags = rflags;
388 if (sf.sf_uc.uc_mcontext.sc_fpstate != NULL) {
389 struct savefpu *svfp = (struct savefpu *)mc.mc_fpstate;
391 bzero(&mc, sizeof(mc));
392 mc.mc_ownedfp = _MC_FPOWNED_FPU;
393 mc.mc_fpformat = _MC_FPFMT_XMM;
395 svfp->sv_env.en_cw = sf.sf_fs.cwd;
396 svfp->sv_env.en_sw = sf.sf_fs.swd;
397 svfp->sv_env.en_tw = sf.sf_fs.twd;
398 svfp->sv_env.en_opcode = sf.sf_fs.fop;
399 svfp->sv_env.en_rip = sf.sf_fs.rip;
400 svfp->sv_env.en_rdp = sf.sf_fs.rdp;
401 svfp->sv_env.en_mxcsr = sf.sf_fs.mxcsr;
402 svfp->sv_env.en_mxcsr_mask = sf.sf_fs.mxcsr_mask;
404 for (i = 0; i < nitems(svfp->sv_fp); ++i)
405 bcopy(&sf.sf_fs.st[i], svfp->sv_fp[i].fp_acc.fp_bytes,
406 sizeof(svfp->sv_fp[i].fp_acc.fp_bytes));
408 for (i = 0; i < nitems(svfp->sv_xmm); ++i)
409 bcopy(&sf.sf_fs.xmm[i], svfp->sv_xmm[i].xmm_bytes,
410 sizeof(svfp->sv_xmm[i].xmm_bytes));
411 error = set_fpcontext(td, &mc, NULL, 0);
413 uprintf("pid %d comm %s linux can't restore fpu state %d\n",
414 p->p_pid, p->p_comm, error);
419 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
420 return (EJUSTRETURN);
424 * copied from amd64/amd64/machdep.c
426 * Send an interrupt to process.
429 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
431 struct l_rt_sigframe sf, *sfp;
436 struct trapframe *regs;
437 struct savefpu *svfp;
440 int oonstack, issiginfo, i;
444 PROC_LOCK_ASSERT(p, MA_OWNED);
445 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
447 issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
448 code = ksi->ksi_code;
449 mtx_assert(&psp->ps_mtx, MA_OWNED);
451 oonstack = sigonstack(regs->tf_rsp);
453 LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
454 catcher, sig, mask, code);
456 bzero(&sf, sizeof(sf));
457 sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
458 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
459 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
460 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
462 /* Allocate space for the signal handler context. */
463 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
464 SIGISMEMBER(psp->ps_sigonstack, sig)) {
465 sp = (caddr_t)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
467 sp = (caddr_t)regs->tf_rsp - 128;
469 mtx_unlock(&psp->ps_mtx);
472 /* Make room, keeping the stack aligned. */
473 sp -= sizeof(struct l_rt_sigframe);
474 sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
476 /* Save user context. */
477 bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
478 sf.sf_uc.uc_mcontext.sc_mask = sf.sf_uc.uc_sigmask;
479 sf.sf_uc.uc_mcontext.sc_rdi = regs->tf_rdi;
480 sf.sf_uc.uc_mcontext.sc_rsi = regs->tf_rsi;
481 sf.sf_uc.uc_mcontext.sc_rdx = regs->tf_rdx;
482 sf.sf_uc.uc_mcontext.sc_rbp = regs->tf_rbp;
483 sf.sf_uc.uc_mcontext.sc_rbx = regs->tf_rbx;
484 sf.sf_uc.uc_mcontext.sc_rcx = regs->tf_rcx;
485 sf.sf_uc.uc_mcontext.sc_rax = regs->tf_rax;
486 sf.sf_uc.uc_mcontext.sc_rip = regs->tf_rip;
487 sf.sf_uc.uc_mcontext.sc_rsp = regs->tf_rsp;
488 sf.sf_uc.uc_mcontext.sc_r8 = regs->tf_r8;
489 sf.sf_uc.uc_mcontext.sc_r9 = regs->tf_r9;
490 sf.sf_uc.uc_mcontext.sc_r10 = regs->tf_r10;
491 sf.sf_uc.uc_mcontext.sc_r11 = regs->tf_r11;
492 sf.sf_uc.uc_mcontext.sc_r12 = regs->tf_r12;
493 sf.sf_uc.uc_mcontext.sc_r13 = regs->tf_r13;
494 sf.sf_uc.uc_mcontext.sc_r14 = regs->tf_r14;
495 sf.sf_uc.uc_mcontext.sc_r15 = regs->tf_r15;
496 sf.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
497 sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
498 sf.sf_uc.uc_mcontext.sc_err = regs->tf_err;
499 sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
500 sf.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
502 get_fpcontext(td, &mc, NULL, NULL);
503 KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present"));
504 svfp = (struct savefpu *)mc.mc_fpstate;
506 sf.sf_fs.cwd = svfp->sv_env.en_cw;
507 sf.sf_fs.swd = svfp->sv_env.en_sw;
508 sf.sf_fs.twd = svfp->sv_env.en_tw;
509 sf.sf_fs.fop = svfp->sv_env.en_opcode;
510 sf.sf_fs.rip = svfp->sv_env.en_rip;
511 sf.sf_fs.rdp = svfp->sv_env.en_rdp;
512 sf.sf_fs.mxcsr = svfp->sv_env.en_mxcsr;
513 sf.sf_fs.mxcsr_mask = svfp->sv_env.en_mxcsr_mask;
515 for (i = 0; i < nitems(svfp->sv_fp); ++i)
516 bcopy(svfp->sv_fp[i].fp_acc.fp_bytes, &sf.sf_fs.st[i],
517 sizeof(svfp->sv_fp[i].fp_acc.fp_bytes));
519 for (i = 0; i < nitems(svfp->sv_xmm); ++i)
520 bcopy(svfp->sv_xmm[i].xmm_bytes, &sf.sf_fs.xmm[i],
521 sizeof(svfp->sv_xmm[i].xmm_bytes));
522 sf.sf_uc.uc_mcontext.sc_fpstate = (struct l_fpstate *)((caddr_t)sfp +
523 offsetof(struct l_rt_sigframe, sf_fs));
525 /* Translate the signal. */
526 sig = bsd_to_linux_signal(sig);
527 /* Fill in POSIX parts. */
528 siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
530 /* Copy the sigframe out to the user's stack. */
531 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
532 uprintf("pid %d comm %s has trashed its stack, killing\n",
533 p->p_pid, p->p_comm);
539 /* Build the argument list for the signal handler. */
540 regs->tf_rdi = sig; /* arg 1 in %rdi */
543 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */
544 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */
549 regs->tf_rcx = (register_t)catcher;
550 regs->tf_rsp = (long)sfp;
551 regs->tf_rip = linux_rt_sigcode;
552 regs->tf_rflags &= ~(PSL_T | PSL_D);
553 regs->tf_cs = _ucodesel;
554 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
556 mtx_lock(&psp->ps_mtx);
559 #define LINUX_VSYSCALL_START (-10UL << 20)
560 #define LINUX_VSYSCALL_SZ 1024
562 const unsigned long linux_vsyscall_vector[] = {
563 LINUX_SYS_gettimeofday,
564 LINUX_SYS_linux_time,
565 LINUX_SYS_linux_getcpu,
569 linux_vsyscall(struct thread *td)
571 struct trapframe *frame;
576 frame = td->td_frame;
578 /* Check %rip for vsyscall area. */
579 if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
581 if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
583 code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
584 if (code >= nitems(linux_vsyscall_vector))
588 * vsyscall called as callq *(%rax), so we must
589 * use return address from %rsp and also fixup %rsp.
591 error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
595 frame->tf_rip = retqaddr;
596 frame->tf_rax = linux_vsyscall_vector[code];
599 traced = (frame->tf_flags & PSL_T);
601 amd64_syscall(td, traced);
606 struct sysentvec elf_linux_sysvec = {
607 .sv_size = LINUX_SYS_MAXSYSCALL,
608 .sv_table = linux_sysent,
609 .sv_fixup = __elfN(freebsd_fixup),
610 .sv_sendsig = linux_rt_sendsig,
611 .sv_sigcode = &_binary_linux_vdso_so_o_start,
612 .sv_szsigcode = &linux_szsigcode,
613 .sv_name = "Linux ELF64",
614 .sv_coredump = elf64_coredump,
615 .sv_elf_core_osabi = ELFOSABI_NONE,
616 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
617 .sv_elf_core_prepare_notes = linux64_prepare_notes,
618 .sv_imgact_try = linux_exec_imgact_try,
619 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
620 .sv_minuser = VM_MIN_ADDRESS,
621 .sv_maxuser = VM_MAXUSER_ADDRESS_LA48,
622 .sv_usrstack = LINUX_USRSTACK_LA48,
623 .sv_psstrings = LINUX_PS_STRINGS_LA48,
624 .sv_psstringssz = sizeof(struct ps_strings),
625 .sv_stackprot = VM_PROT_ALL,
626 .sv_copyout_auxargs = linux_copyout_auxargs,
627 .sv_copyout_strings = __linuxN(copyout_strings),
628 .sv_setregs = linux_exec_setregs,
631 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
632 SV_SIG_WAITNDQ | SV_TIMEKEEP,
633 .sv_set_syscall_retval = linux_set_syscall_retval,
634 .sv_fetch_syscall_args = linux_fetch_syscall_args,
635 .sv_syscallnames = linux_syscallnames,
636 .sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
637 .sv_shared_page_len = PAGE_SIZE,
638 .sv_schedtail = linux_schedtail,
639 .sv_thread_detach = linux_thread_detach,
640 .sv_trap = linux_vsyscall,
641 .sv_onexec = linux_on_exec_vmspace,
642 .sv_onexit = linux_on_exit,
643 .sv_ontdexit = linux_thread_dtor,
644 .sv_setid_allowed = &linux_setid_allowed_query,
645 .sv_set_fork_retval = linux_set_fork_retval,
649 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
653 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
654 LINUX_VDSOPAGE_SIZE, imgp);
656 linux_on_exec(p, imgp);
661 * linux_vdso_install() and linux_exec_sysvec_init() must be called
662 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
665 linux_exec_sysvec_init(void *param)
667 l_uintptr_t *ktimekeep_base, *ktsc_selector;
668 struct sysentvec *sv;
672 amd64_lower_shared_page(sv);
673 /* Fill timekeep_base */
674 exec_sysvec_init(sv);
676 tkoff = kern_timekeep_base - linux_vdso_base;
677 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
678 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
680 tkoff = kern_tsc_selector - linux_vdso_base;
681 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
682 *ktsc_selector = linux_vdso_tsc_selector_idx();
684 printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
686 tkoff = kern_cpu_selector - linux_vdso_base;
687 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
688 *ktsc_selector = linux_vdso_cpu_selector_idx();
690 printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
692 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
693 linux_exec_sysvec_init, &elf_linux_sysvec);
696 linux_vdso_install(const void *param)
698 char *vdso_start = &_binary_linux_vdso_so_o_start;
699 char *vdso_end = &_binary_linux_vdso_so_o_end;
701 linux_szsigcode = vdso_end - vdso_start;
702 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
704 linux_vdso_base = LINUX_VDSOPAGE_LA48;
705 if (hw_lower_amd64_sharedpage != 0)
706 linux_vdso_base -= PAGE_SIZE;
708 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
710 linux_vdso_obj = __elfN(linux_shared_page_init)
711 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
712 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
714 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
716 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
717 linux_vdso_install, NULL);
720 linux_vdso_deinstall(const void *param)
723 __elfN(linux_shared_page_fini)(linux_vdso_obj,
724 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
726 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
727 linux_vdso_deinstall, NULL);
730 linux_vdso_reloc(char *mapping, Elf_Addr offset)
732 const Elf_Ehdr *ehdr;
733 const Elf_Shdr *shdr;
734 Elf64_Addr *where, val;
735 Elf_Size rtype, symidx;
736 const Elf_Rela *rela;
737 Elf_Addr addr, addend;
744 ehdr = (const Elf_Ehdr *)mapping;
745 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
746 for (i = 0; i < ehdr->e_shnum; i++)
748 switch (shdr[i].sh_type) {
750 printf("Linux x86_64 vDSO: unexpected Rel section\n");
753 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
754 relacnt = shdr[i].sh_size / sizeof(*rela);
758 for (j = 0; j < relacnt; j++, rela++) {
759 where = (Elf_Addr *)(mapping + rela->r_offset);
760 addend = rela->r_addend;
761 rtype = ELF_R_TYPE(rela->r_info);
762 symidx = ELF_R_SYM(rela->r_info);
765 case R_X86_64_NONE: /* none */
768 case R_X86_64_RELATIVE: /* B + A */
769 addr = (Elf_Addr)(offset + addend);
774 case R_X86_64_IRELATIVE:
775 printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
776 "symbol index %ld\n", symidx);
779 printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
780 "symbol index %ld\n", rtype, symidx);
785 static Elf_Brandnote linux64_brandnote = {
786 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
789 .vendor = GNU_ABI_VENDOR,
790 .flags = BN_TRANSLATE_OSREL,
791 .trans_osrel = linux_trans_osrel
794 static Elf64_Brandinfo linux_glibc2brand = {
795 .brand = ELFOSABI_LINUX,
796 .machine = EM_X86_64,
797 .compat_3_brand = "Linux",
798 .emul_path = linux_emul_path,
799 .interp_path = "/lib64/ld-linux-x86-64.so.2",
800 .sysvec = &elf_linux_sysvec,
801 .interp_newpath = NULL,
802 .brand_note = &linux64_brandnote,
803 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
806 static Elf64_Brandinfo linux_glibc2brandshort = {
807 .brand = ELFOSABI_LINUX,
808 .machine = EM_X86_64,
809 .compat_3_brand = "Linux",
810 .emul_path = linux_emul_path,
811 .interp_path = "/lib64/ld-linux.so.2",
812 .sysvec = &elf_linux_sysvec,
813 .interp_newpath = NULL,
814 .brand_note = &linux64_brandnote,
815 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
818 static Elf64_Brandinfo linux_muslbrand = {
819 .brand = ELFOSABI_LINUX,
820 .machine = EM_X86_64,
821 .compat_3_brand = "Linux",
822 .emul_path = linux_emul_path,
823 .interp_path = "/lib/ld-musl-x86_64.so.1",
824 .sysvec = &elf_linux_sysvec,
825 .interp_newpath = NULL,
826 .brand_note = &linux64_brandnote,
827 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
828 LINUX_BI_FUTEX_REQUEUE
831 Elf64_Brandinfo *linux_brandlist[] = {
833 &linux_glibc2brandshort,
839 linux64_elf_modevent(module_t mod, int type, void *data)
841 Elf64_Brandinfo **brandinfo;
843 struct linux_ioctl_handler **lihp;
849 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
851 if (elf64_insert_brand_entry(*brandinfo) < 0)
854 SET_FOREACH(lihp, linux_ioctl_handler_set)
855 linux_ioctl_register_handler(*lihp);
856 stclohz = (stathz ? stathz : hz);
858 printf("Linux x86-64 ELF exec handler installed\n");
860 printf("cannot insert Linux x86-64 ELF brand handler\n");
863 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
865 if (elf64_brand_inuse(*brandinfo))
868 for (brandinfo = &linux_brandlist[0];
869 *brandinfo != NULL; ++brandinfo)
870 if (elf64_remove_brand_entry(*brandinfo) < 0)
874 SET_FOREACH(lihp, linux_ioctl_handler_set)
875 linux_ioctl_unregister_handler(*lihp);
877 printf("Linux x86_64 ELF exec handler removed\n");
879 printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
887 static moduledata_t linux64_elf_mod = {
889 linux64_elf_modevent,
893 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
894 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
895 FEATURE(linux64, "Linux 64bit support");