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>
40 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/imgact.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
52 #include <sys/resourcevar.h>
53 #include <sys/stddef.h>
54 #include <sys/signalvar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/sysproto.h>
59 #include <sys/vnode.h>
60 #include <sys/eventhandler.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_param.h>
70 #include <machine/cpu.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/specialreg.h>
74 #include <machine/trap.h>
76 #include <x86/linux/linux_x86.h>
77 #include <amd64/linux/linux.h>
78 #include <amd64/linux/linux_proto.h>
79 #include <compat/linux/linux_emul.h>
80 #include <compat/linux/linux_fork.h>
81 #include <compat/linux/linux_ioctl.h>
82 #include <compat/linux/linux_mib.h>
83 #include <compat/linux/linux_misc.h>
84 #include <compat/linux/linux_signal.h>
85 #include <compat/linux/linux_sysproto.h>
86 #include <compat/linux/linux_util.h>
87 #include <compat/linux/linux_vdso.h>
89 #include <x86/linux/linux_x86_sigframe.h>
91 MODULE_VERSION(linux64, 1);
93 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
94 #define LINUX_VDSOPAGE_LA48 (VM_MAXUSER_ADDRESS_LA48 - \
96 #define LINUX_SHAREDPAGE_LA48 (LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
98 * PAGE_SIZE - the size
99 * of the native SHAREDPAGE
101 #define LINUX_USRSTACK_LA48 LINUX_SHAREDPAGE_LA48
102 #define LINUX_PS_STRINGS_LA48 (LINUX_USRSTACK_LA48 - \
103 sizeof(struct ps_strings))
105 static int linux_szsigcode;
106 static vm_object_t linux_vdso_obj;
107 static char *linux_vdso_mapping;
108 extern char _binary_linux_vdso_so_o_start;
109 extern char _binary_linux_vdso_so_o_end;
110 static vm_offset_t linux_vdso_base;
112 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
114 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
116 static int linux_copyout_strings(struct image_params *imgp,
117 uintptr_t *stack_base);
118 static int linux_fixup_elf(uintptr_t *stack_base,
119 struct image_params *iparams);
120 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
121 static void linux_vdso_install(const void *param);
122 static void linux_vdso_deinstall(const void *param);
123 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
124 static void linux_set_syscall_retval(struct thread *td, int error);
125 static int linux_fetch_syscall_args(struct thread *td);
126 static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
128 static void linux_exec_sysvec_init(void *param);
129 static int linux_on_exec_vmspace(struct proc *p,
130 struct image_params *imgp);
131 static void linux_set_fork_retval(struct thread *td);
132 static int linux_vsyscall(struct thread *td);
134 #define LINUX_T_UNKNOWN 255
135 static int _bsd_to_linux_trapcode[] = {
136 LINUX_T_UNKNOWN, /* 0 */
137 6, /* 1 T_PRIVINFLT */
138 LINUX_T_UNKNOWN, /* 2 */
140 LINUX_T_UNKNOWN, /* 4 */
141 LINUX_T_UNKNOWN, /* 5 */
142 16, /* 6 T_ARITHTRAP */
143 254, /* 7 T_ASTFLT */
144 LINUX_T_UNKNOWN, /* 8 */
145 13, /* 9 T_PROTFLT */
146 1, /* 10 T_TRCTRAP */
147 LINUX_T_UNKNOWN, /* 11 */
148 14, /* 12 T_PAGEFLT */
149 LINUX_T_UNKNOWN, /* 13 */
150 17, /* 14 T_ALIGNFLT */
151 LINUX_T_UNKNOWN, /* 15 */
152 LINUX_T_UNKNOWN, /* 16 */
153 LINUX_T_UNKNOWN, /* 17 */
159 8, /* 23 T_DOUBLEFLT */
160 9, /* 24 T_FPOPFLT */
161 10, /* 25 T_TSSFLT */
162 11, /* 26 T_SEGNPFLT */
163 12, /* 27 T_STKFLT */
165 19, /* 29 T_XMMFLT */
166 15 /* 30 T_RESERVED */
168 #define bsd_to_linux_trapcode(code) \
169 ((code)<nitems(_bsd_to_linux_trapcode)? \
170 _bsd_to_linux_trapcode[(code)]: \
173 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
174 LINUX_VDSO_SYM_CHAR(linux_platform);
175 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
176 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
177 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
180 * If FreeBSD & Linux have a difference of opinion about what a trap
181 * means, deal with it here.
186 linux_translate_traps(int signal, int trap_code)
189 if (signal != SIGBUS)
203 linux_fetch_syscall_args(struct thread *td)
206 struct trapframe *frame;
207 struct syscall_args *sa;
210 frame = td->td_frame;
213 sa->args[0] = frame->tf_rdi;
214 sa->args[1] = frame->tf_rsi;
215 sa->args[2] = frame->tf_rdx;
216 sa->args[3] = frame->tf_rcx;
217 sa->args[4] = frame->tf_r8;
218 sa->args[5] = frame->tf_r9;
219 sa->code = frame->tf_rax;
220 sa->original_code = sa->code;
222 if (sa->code >= p->p_sysent->sv_size)
224 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
226 sa->callp = &p->p_sysent->sv_table[sa->code];
228 td->td_retval[0] = 0;
233 linux_set_syscall_retval(struct thread *td, int error)
235 struct trapframe *frame;
237 frame = td->td_frame;
241 frame->tf_rax = td->td_retval[0];
242 frame->tf_r10 = frame->tf_rcx;
247 * Reconstruct pc, we know that 'syscall' is 2 bytes,
248 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
249 * We saved this in tf_err.
252 frame->tf_rip -= frame->tf_err;
253 frame->tf_r10 = frame->tf_rcx;
260 frame->tf_rax = bsd_to_linux_errno(error);
261 frame->tf_r10 = frame->tf_rcx;
266 * Differently from FreeBSD native ABI, on Linux only %rcx
267 * and %r11 values are not preserved across the syscall.
268 * Require full context restore to get all registers except
269 * those two restored at return to usermode.
271 * XXX: Would be great to be able to avoid PCB_FULL_IRET
272 * for the error == 0 case.
274 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
278 linux_set_fork_retval(struct thread *td)
280 struct trapframe *frame = td->td_frame;
286 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
289 Elf_Auxinfo *argarray, *pos;
291 int error, issetugid;
294 args = (Elf64_Auxargs *)imgp->auxargs;
295 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
298 issetugid = p->p_flag & P_SUGID ? 1 : 0;
299 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
300 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
301 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
302 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
303 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
304 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
305 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
306 AUXARGS_ENTRY(pos, AT_BASE, args->base);
307 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
308 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
309 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
310 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
311 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
312 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
313 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
314 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
315 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
316 if (imgp->execpathp != 0)
317 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
318 if (args->execfd != -1)
319 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
320 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
321 AUXARGS_ENTRY(pos, AT_NULL, 0);
323 free(imgp->auxargs, M_TEMP);
324 imgp->auxargs = NULL;
325 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
327 error = copyout(argarray, (void *)base,
328 sizeof(*argarray) * LINUX_AT_COUNT);
329 free(argarray, M_TEMP);
334 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
338 base = (Elf64_Addr *)*stack_base;
340 if (suword(base, (uint64_t)imgp->args->argc) == -1)
343 *stack_base = (uintptr_t)base;
348 * Copy strings out to the new process address space, constructing new arg
349 * and env vector tables. Return a pointer to the base so that it can be used
350 * as the initial stack pointer.
353 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
355 int argc, envc, error;
358 uintptr_t destp, ustringp;
359 struct ps_strings *arginfo;
360 char canary[LINUX_AT_RANDOM_LEN];
365 arginfo = (struct ps_strings *)PROC_PS_STRINGS(p);
366 destp = (uintptr_t)arginfo;
368 if (imgp->execpath != NULL && imgp->auxargs != NULL) {
369 execpath_len = strlen(imgp->execpath) + 1;
370 destp -= execpath_len;
371 destp = rounddown2(destp, sizeof(void *));
372 imgp->execpathp = (void *)destp;
373 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
378 /* Prepare the canary for SSP. */
379 arc4rand(canary, sizeof(canary), 0);
380 destp -= roundup(sizeof(canary), sizeof(void *));
381 imgp->canary = (void *)destp;
382 error = copyout(canary, imgp->canary, sizeof(canary));
386 /* Allocate room for the argument and environment strings. */
387 destp -= ARG_MAX - imgp->args->stringspace;
388 destp = rounddown2(destp, sizeof(void *));
393 * Allocate room on the stack for the ELF auxargs
394 * array. It has LINUX_AT_COUNT entries.
396 destp -= LINUX_AT_COUNT * sizeof(Elf64_Auxinfo);
397 destp = rounddown2(destp, sizeof(void *));
400 vectp = (char **)destp;
403 * Allocate room for the argv[] and env vectors including the
404 * terminating NULL pointers.
406 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
409 * Starting with 2.24, glibc depends on a 16-byte stack alignment.
410 * One "long argc" will be prepended later.
412 vectp = (char **)((((uintptr_t)vectp + 8) & ~0xF) - 8);
414 /* vectp also becomes our initial stack base. */
415 *stack_base = (uintptr_t)vectp;
417 stringp = imgp->args->begin_argv;
418 argc = imgp->args->argc;
419 envc = imgp->args->envc;
421 /* Copy out strings - arguments and environment. */
422 error = copyout(stringp, (void *)ustringp,
423 ARG_MAX - imgp->args->stringspace);
427 /* Fill in "ps_strings" struct for ps, w, etc. */
428 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
429 suword(&arginfo->ps_nargvstr, argc) != 0)
432 /* Fill in argument portion of vector table. */
433 for (; argc > 0; --argc) {
434 if (suword(vectp++, ustringp) != 0)
436 while (*stringp++ != 0)
441 /* A null vector table pointer separates the argp's from the envp's. */
442 if (suword(vectp++, 0) != 0)
445 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
446 suword(&arginfo->ps_nenvstr, envc) != 0)
449 /* Fill in environment portion of vector table. */
450 for (; envc > 0; --envc) {
451 if (suword(vectp++, ustringp) != 0)
453 while (*stringp++ != 0)
458 /* The end of the vector table is a null pointer. */
459 if (suword(vectp, 0) != 0)
464 error = imgp->sysent->sv_copyout_auxargs(imgp,
474 * Reset registers to default values on exec.
477 linux_exec_setregs(struct thread *td, struct image_params *imgp,
480 struct trapframe *regs;
482 register_t saved_rflags;
487 if (td->td_proc->p_md.md_ldt != NULL)
492 clear_pcb_flags(pcb, PCB_32BIT);
493 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
494 set_pcb_flags(pcb, PCB_FULL_IRET);
496 saved_rflags = regs->tf_rflags & PSL_T;
497 bzero((char *)regs, sizeof(struct trapframe));
498 regs->tf_rip = imgp->entry_addr;
499 regs->tf_rsp = stack;
500 regs->tf_rflags = PSL_USER | saved_rflags;
501 regs->tf_ss = _udatasel;
502 regs->tf_cs = _ucodesel;
503 regs->tf_ds = _udatasel;
504 regs->tf_es = _udatasel;
505 regs->tf_fs = _ufssel;
506 regs->tf_gs = _ugssel;
507 regs->tf_flags = TF_HASSEGS;
509 x86_clear_dbregs(pcb);
512 * Drop the FP state if we hold it, so that the process gets a
513 * clean FP state if it uses the FPU again.
519 * Copied from amd64/amd64/machdep.c
521 * XXX fpu state need? don't think so
524 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
527 struct l_ucontext uc;
528 struct l_sigcontext *context;
529 struct trapframe *regs;
530 unsigned long rflags;
535 error = copyin((void *)regs->tf_rbx, &uc, sizeof(uc));
540 context = &uc.uc_mcontext;
541 rflags = context->sc_rflags;
544 * Don't allow users to change privileged or reserved flags.
547 * XXX do allow users to change the privileged flag PSL_RF.
548 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers
549 * should sometimes set it there too. tf_rflags is kept in
550 * the signal context during signal handling and there is no
551 * other place to remember it, so the PSL_RF bit may be
552 * corrupted by the signal handler without us knowing.
553 * Corruption of the PSL_RF bit at worst causes one more or
554 * one less debugger trap, so allowing it is fairly harmless.
556 if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
557 uprintf("pid %d comm %s linux mangled rflags %#lx\n",
558 p->p_pid, p->p_comm, rflags);
563 * Don't allow users to load a valid privileged %cs. Let the
564 * hardware check for invalid selectors, excess privilege in
565 * other selectors, invalid %eip's and invalid %esp's.
567 if (!CS_SECURE(context->sc_cs)) {
568 uprintf("pid %d comm %s linux mangled cs %#x\n",
569 p->p_pid, p->p_comm, context->sc_cs);
570 ksiginfo_init_trap(&ksi);
571 ksi.ksi_signo = SIGBUS;
572 ksi.ksi_code = BUS_OBJERR;
573 ksi.ksi_trapno = T_PROTFLT;
574 ksi.ksi_addr = (void *)regs->tf_rip;
575 trapsignal(td, &ksi);
580 linux_to_bsd_sigset(&uc.uc_sigmask, &td->td_sigmask);
581 SIG_CANTMASK(td->td_sigmask);
585 regs->tf_rdi = context->sc_rdi;
586 regs->tf_rsi = context->sc_rsi;
587 regs->tf_rdx = context->sc_rdx;
588 regs->tf_rbp = context->sc_rbp;
589 regs->tf_rbx = context->sc_rbx;
590 regs->tf_rcx = context->sc_rcx;
591 regs->tf_rax = context->sc_rax;
592 regs->tf_rip = context->sc_rip;
593 regs->tf_rsp = context->sc_rsp;
594 regs->tf_r8 = context->sc_r8;
595 regs->tf_r9 = context->sc_r9;
596 regs->tf_r10 = context->sc_r10;
597 regs->tf_r11 = context->sc_r11;
598 regs->tf_r12 = context->sc_r12;
599 regs->tf_r13 = context->sc_r13;
600 regs->tf_r14 = context->sc_r14;
601 regs->tf_r15 = context->sc_r15;
602 regs->tf_cs = context->sc_cs;
603 regs->tf_err = context->sc_err;
604 regs->tf_rflags = rflags;
606 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
607 return (EJUSTRETURN);
611 * copied from amd64/amd64/machdep.c
613 * Send an interrupt to process.
616 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
618 struct l_rt_sigframe sf, *sfp;
623 struct trapframe *regs;
629 PROC_LOCK_ASSERT(p, MA_OWNED);
630 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
632 code = ksi->ksi_code;
633 mtx_assert(&psp->ps_mtx, MA_OWNED);
635 oonstack = sigonstack(regs->tf_rsp);
637 LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
638 catcher, sig, mask, code);
640 /* Save user context. */
641 bzero(&sf, sizeof(sf));
642 bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
643 bsd_to_linux_sigset(mask, &sf.sf_uc.uc_mcontext.sc_mask);
645 sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
646 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
647 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
648 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
650 sf.sf_uc.uc_mcontext.sc_rdi = regs->tf_rdi;
651 sf.sf_uc.uc_mcontext.sc_rsi = regs->tf_rsi;
652 sf.sf_uc.uc_mcontext.sc_rdx = regs->tf_rdx;
653 sf.sf_uc.uc_mcontext.sc_rbp = regs->tf_rbp;
654 sf.sf_uc.uc_mcontext.sc_rbx = regs->tf_rbx;
655 sf.sf_uc.uc_mcontext.sc_rcx = regs->tf_rcx;
656 sf.sf_uc.uc_mcontext.sc_rax = regs->tf_rax;
657 sf.sf_uc.uc_mcontext.sc_rip = regs->tf_rip;
658 sf.sf_uc.uc_mcontext.sc_rsp = regs->tf_rsp;
659 sf.sf_uc.uc_mcontext.sc_r8 = regs->tf_r8;
660 sf.sf_uc.uc_mcontext.sc_r9 = regs->tf_r9;
661 sf.sf_uc.uc_mcontext.sc_r10 = regs->tf_r10;
662 sf.sf_uc.uc_mcontext.sc_r11 = regs->tf_r11;
663 sf.sf_uc.uc_mcontext.sc_r12 = regs->tf_r12;
664 sf.sf_uc.uc_mcontext.sc_r13 = regs->tf_r13;
665 sf.sf_uc.uc_mcontext.sc_r14 = regs->tf_r14;
666 sf.sf_uc.uc_mcontext.sc_r15 = regs->tf_r15;
667 sf.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
668 sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
669 sf.sf_uc.uc_mcontext.sc_err = regs->tf_err;
670 sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
671 sf.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
673 /* Allocate space for the signal handler context. */
674 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
675 SIGISMEMBER(psp->ps_sigonstack, sig)) {
676 sp = (caddr_t)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
678 sp = (caddr_t)regs->tf_rsp - 128;
679 sp -= sizeof(struct l_rt_sigframe);
680 /* Align to 16 bytes. */
681 sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
683 /* Translate the signal. */
684 sig = bsd_to_linux_signal(sig);
686 /* Build the argument list for the signal handler. */
687 regs->tf_rdi = sig; /* arg 1 in %rdi */
689 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */
690 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */
691 regs->tf_rcx = (register_t)catcher;
693 /* Fill in POSIX parts. */
694 siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
696 mtx_unlock(&psp->ps_mtx);
699 /* Copy the sigframe out to the user's stack. */
700 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
701 uprintf("pid %d comm %s has trashed its stack, killing\n",
702 p->p_pid, p->p_comm);
707 regs->tf_rsp = (long)sfp;
708 regs->tf_rip = linux_rt_sigcode;
709 regs->tf_rflags &= ~(PSL_T | PSL_D);
710 regs->tf_cs = _ucodesel;
711 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
713 mtx_lock(&psp->ps_mtx);
716 #define LINUX_VSYSCALL_START (-10UL << 20)
717 #define LINUX_VSYSCALL_SZ 1024
719 const unsigned long linux_vsyscall_vector[] = {
720 LINUX_SYS_gettimeofday,
721 LINUX_SYS_linux_time,
722 LINUX_SYS_linux_getcpu,
726 linux_vsyscall(struct thread *td)
728 struct trapframe *frame;
733 frame = td->td_frame;
735 /* Check %rip for vsyscall area. */
736 if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
738 if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
740 code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
741 if (code >= nitems(linux_vsyscall_vector))
745 * vsyscall called as callq *(%rax), so we must
746 * use return address from %rsp and also fixup %rsp.
748 error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
752 frame->tf_rip = retqaddr;
753 frame->tf_rax = linux_vsyscall_vector[code];
756 traced = (frame->tf_flags & PSL_T);
758 amd64_syscall(td, traced);
763 struct sysentvec elf_linux_sysvec = {
764 .sv_size = LINUX_SYS_MAXSYSCALL,
765 .sv_table = linux_sysent,
766 .sv_fixup = linux_fixup_elf,
767 .sv_sendsig = linux_rt_sendsig,
768 .sv_sigcode = &_binary_linux_vdso_so_o_start,
769 .sv_szsigcode = &linux_szsigcode,
770 .sv_name = "Linux ELF64",
771 .sv_coredump = elf64_coredump,
772 .sv_elf_core_osabi = ELFOSABI_NONE,
773 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
774 .sv_elf_core_prepare_notes = linux64_prepare_notes,
775 .sv_imgact_try = linux_exec_imgact_try,
776 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
777 .sv_minuser = VM_MIN_ADDRESS,
778 .sv_maxuser = VM_MAXUSER_ADDRESS_LA48,
779 .sv_usrstack = LINUX_USRSTACK_LA48,
780 .sv_psstrings = LINUX_PS_STRINGS_LA48,
781 .sv_psstringssz = sizeof(struct ps_strings),
782 .sv_stackprot = VM_PROT_ALL,
783 .sv_copyout_auxargs = linux_copyout_auxargs,
784 .sv_copyout_strings = linux_copyout_strings,
785 .sv_setregs = linux_exec_setregs,
788 .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
789 SV_SIG_WAITNDQ | SV_TIMEKEEP,
790 .sv_set_syscall_retval = linux_set_syscall_retval,
791 .sv_fetch_syscall_args = linux_fetch_syscall_args,
792 .sv_syscallnames = NULL,
793 .sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
794 .sv_shared_page_len = PAGE_SIZE,
795 .sv_schedtail = linux_schedtail,
796 .sv_thread_detach = linux_thread_detach,
797 .sv_trap = linux_vsyscall,
798 .sv_onexec = linux_on_exec_vmspace,
799 .sv_onexit = linux_on_exit,
800 .sv_ontdexit = linux_thread_dtor,
801 .sv_setid_allowed = &linux_setid_allowed_query,
802 .sv_set_fork_retval = linux_set_fork_retval,
806 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
810 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
811 LINUX_VDSOPAGE_SIZE, imgp);
813 linux_on_exec(p, imgp);
818 * linux_vdso_install() and linux_exec_sysvec_init() must be called
819 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
822 linux_exec_sysvec_init(void *param)
824 l_uintptr_t *ktimekeep_base, *ktsc_selector;
825 struct sysentvec *sv;
829 amd64_lower_shared_page(sv);
830 /* Fill timekeep_base */
831 exec_sysvec_init(sv);
833 tkoff = kern_timekeep_base - linux_vdso_base;
834 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
835 *ktimekeep_base = sv->sv_timekeep_base;
837 tkoff = kern_tsc_selector - linux_vdso_base;
838 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
839 *ktsc_selector = linux_vdso_tsc_selector_idx();
841 printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
843 tkoff = kern_cpu_selector - linux_vdso_base;
844 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
845 *ktsc_selector = linux_vdso_cpu_selector_idx();
847 printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
849 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
850 linux_exec_sysvec_init, &elf_linux_sysvec);
853 linux_vdso_install(const void *param)
855 char *vdso_start = &_binary_linux_vdso_so_o_start;
856 char *vdso_end = &_binary_linux_vdso_so_o_end;
858 linux_szsigcode = vdso_end - vdso_start;
859 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
861 linux_vdso_base = LINUX_VDSOPAGE_LA48;
862 if (hw_lower_amd64_sharedpage != 0)
863 linux_vdso_base -= PAGE_SIZE;
865 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
867 linux_vdso_obj = __elfN(linux_shared_page_init)
868 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
869 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
871 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
873 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
874 linux_vdso_install, NULL);
877 linux_vdso_deinstall(const void *param)
880 __elfN(linux_shared_page_fini)(linux_vdso_obj,
881 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
883 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
884 linux_vdso_deinstall, NULL);
887 linux_vdso_reloc(char *mapping, Elf_Addr offset)
889 const Elf_Ehdr *ehdr;
890 const Elf_Shdr *shdr;
891 Elf64_Addr *where, val;
892 Elf_Size rtype, symidx;
893 const Elf_Rela *rela;
894 Elf_Addr addr, addend;
901 ehdr = (const Elf_Ehdr *)mapping;
902 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
903 for (i = 0; i < ehdr->e_shnum; i++)
905 switch (shdr[i].sh_type) {
907 printf("Linux x86_64 vDSO: unexpected Rel section\n");
910 rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
911 relacnt = shdr[i].sh_size / sizeof(*rela);
915 for (j = 0; j < relacnt; j++, rela++) {
916 where = (Elf_Addr *)(mapping + rela->r_offset);
917 addend = rela->r_addend;
918 rtype = ELF_R_TYPE(rela->r_info);
919 symidx = ELF_R_SYM(rela->r_info);
922 case R_X86_64_NONE: /* none */
925 case R_X86_64_RELATIVE: /* B + A */
926 addr = (Elf_Addr)(offset + addend);
931 case R_X86_64_IRELATIVE:
932 printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
933 "symbol index %ld\n", symidx);
936 printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
937 "symbol index %ld\n", rtype, symidx);
942 static char GNULINUX_ABI_VENDOR[] = "GNU";
943 static int GNULINUX_ABI_DESC = 0;
946 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
948 const Elf32_Word *desc;
951 p = (uintptr_t)(note + 1);
952 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
954 desc = (const Elf32_Word *)p;
955 if (desc[0] != GNULINUX_ABI_DESC)
959 * For Linux we encode osrel using the Linux convention of
960 * (version << 16) | (major << 8) | (minor)
961 * See macro in linux_mib.h
963 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
968 static Elf_Brandnote linux64_brandnote = {
969 .hdr.n_namesz = sizeof(GNULINUX_ABI_VENDOR),
972 .vendor = GNULINUX_ABI_VENDOR,
973 .flags = BN_TRANSLATE_OSREL,
974 .trans_osrel = linux_trans_osrel
977 static Elf64_Brandinfo linux_glibc2brand = {
978 .brand = ELFOSABI_LINUX,
979 .machine = EM_X86_64,
980 .compat_3_brand = "Linux",
981 .emul_path = linux_emul_path,
982 .interp_path = "/lib64/ld-linux-x86-64.so.2",
983 .sysvec = &elf_linux_sysvec,
984 .interp_newpath = NULL,
985 .brand_note = &linux64_brandnote,
986 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
989 static Elf64_Brandinfo linux_glibc2brandshort = {
990 .brand = ELFOSABI_LINUX,
991 .machine = EM_X86_64,
992 .compat_3_brand = "Linux",
993 .emul_path = linux_emul_path,
994 .interp_path = "/lib64/ld-linux.so.2",
995 .sysvec = &elf_linux_sysvec,
996 .interp_newpath = NULL,
997 .brand_note = &linux64_brandnote,
998 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1001 static Elf64_Brandinfo linux_muslbrand = {
1002 .brand = ELFOSABI_LINUX,
1003 .machine = EM_X86_64,
1004 .compat_3_brand = "Linux",
1005 .emul_path = linux_emul_path,
1006 .interp_path = "/lib/ld-musl-x86_64.so.1",
1007 .sysvec = &elf_linux_sysvec,
1008 .interp_newpath = NULL,
1009 .brand_note = &linux64_brandnote,
1010 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1011 LINUX_BI_FUTEX_REQUEUE
1014 Elf64_Brandinfo *linux_brandlist[] = {
1016 &linux_glibc2brandshort,
1022 linux64_elf_modevent(module_t mod, int type, void *data)
1024 Elf64_Brandinfo **brandinfo;
1026 struct linux_ioctl_handler **lihp;
1032 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1034 if (elf64_insert_brand_entry(*brandinfo) < 0)
1037 SET_FOREACH(lihp, linux_ioctl_handler_set)
1038 linux_ioctl_register_handler(*lihp);
1039 stclohz = (stathz ? stathz : hz);
1041 printf("Linux x86-64 ELF exec handler installed\n");
1043 printf("cannot insert Linux x86-64 ELF brand handler\n");
1046 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1048 if (elf64_brand_inuse(*brandinfo))
1051 for (brandinfo = &linux_brandlist[0];
1052 *brandinfo != NULL; ++brandinfo)
1053 if (elf64_remove_brand_entry(*brandinfo) < 0)
1057 SET_FOREACH(lihp, linux_ioctl_handler_set)
1058 linux_ioctl_unregister_handler(*lihp);
1060 printf("Linux x86_64 ELF exec handler removed\n");
1062 printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
1065 return (EOPNOTSUPP);
1070 static moduledata_t linux64_elf_mod = {
1072 linux64_elf_modevent,
1076 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1077 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
1078 FEATURE(linux64, "Linux 64bit support");