2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1994-1996 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #define __ELF_WORD_SIZE 32
34 #include <sys/param.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_aout.h>
39 #include <sys/imgact_elf.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
46 #include <sys/stddef.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_page.h>
57 #include <machine/cpu.h>
58 #include <machine/cputypes.h>
59 #include <machine/md_var.h>
60 #include <machine/pcb.h>
61 #include <machine/trap.h>
63 #include <x86/linux/linux_x86.h>
64 #include <i386/linux/linux.h>
65 #include <i386/linux/linux_proto.h>
66 #include <compat/linux/linux_elf.h>
67 #include <compat/linux/linux_emul.h>
68 #include <compat/linux/linux_fork.h>
69 #include <compat/linux/linux_ioctl.h>
70 #include <compat/linux/linux_mib.h>
71 #include <compat/linux/linux_misc.h>
72 #include <compat/linux/linux_signal.h>
73 #include <compat/linux/linux_util.h>
74 #include <compat/linux/linux_vdso.h>
76 #include <x86/linux/linux_x86_sigframe.h>
78 MODULE_VERSION(linux, 1);
80 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2
81 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
82 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE)
84 * PAGE_SIZE - the size
85 * of the native SHAREDPAGE
87 #define LINUX_USRSTACK LINUX_SHAREDPAGE
88 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
90 static int linux_szsigcode;
91 static vm_object_t linux_vdso_obj;
92 static char *linux_vdso_mapping;
93 extern char _binary_linux_vdso_so_o_start;
94 extern char _binary_linux_vdso_so_o_end;
95 static vm_offset_t linux_vdso_base;
97 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
98 extern const char *linux_syscallnames[];
100 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
102 static int linux_fixup(uintptr_t *stack_base,
103 struct image_params *iparams);
104 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
105 static void linux_exec_setregs(struct thread *td,
106 struct image_params *imgp, uintptr_t stack);
107 static void linux_exec_sysvec_init(void *param);
108 static int linux_on_exec_vmspace(struct proc *p,
109 struct image_params *imgp);
110 static void linux_set_fork_retval(struct thread *td);
111 static void linux_vdso_install(const void *param);
112 static void linux_vdso_deinstall(const void *param);
113 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
115 LINUX_VDSO_SYM_CHAR(linux_platform);
116 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
117 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
118 LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode);
119 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
120 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
121 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
124 linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
126 register_t *base, *argv, *envp;
128 base = (register_t *)*stack_base;
130 envp = base + (imgp->args->argc + 1);
132 suword(base, (intptr_t)envp);
134 suword(base, (intptr_t)argv);
136 suword(base, imgp->args->argc);
137 *stack_base = (uintptr_t)base;
142 linux32_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
145 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
146 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO, __kernel_vsyscall);
147 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
148 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());
149 AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
153 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
155 struct thread *td = curthread;
156 struct proc *p = td->td_proc;
158 struct trapframe *regs;
159 struct l_rt_sigframe *fp, frame;
163 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
164 code = ksi->ksi_code;
165 PROC_LOCK_ASSERT(p, MA_OWNED);
167 mtx_assert(&psp->ps_mtx, MA_OWNED);
169 oonstack = sigonstack(regs->tf_esp);
171 /* Allocate space for the signal handler context. */
172 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
173 SIGISMEMBER(psp->ps_sigonstack, sig)) {
174 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
175 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
177 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
178 mtx_unlock(&psp->ps_mtx);
180 /* Build the argument list for the signal handler. */
181 sig = bsd_to_linux_signal(sig);
183 bzero(&frame, sizeof(frame));
186 frame.sf_siginfo = PTROUT(&fp->sf_si);
187 frame.sf_ucontext = PTROUT(&fp->sf_uc);
189 /* Fill in POSIX parts. */
190 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
192 /* Build the signal context to be used by sigreturn. */
193 frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
194 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
195 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
196 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
199 bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
201 frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask;
202 frame.sf_uc.uc_mcontext.sc_gs = rgs();
203 frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs;
204 frame.sf_uc.uc_mcontext.sc_es = regs->tf_es;
205 frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds;
206 frame.sf_uc.uc_mcontext.sc_edi = regs->tf_edi;
207 frame.sf_uc.uc_mcontext.sc_esi = regs->tf_esi;
208 frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_ebp;
209 frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_ebx;
210 frame.sf_uc.uc_mcontext.sc_esp = regs->tf_esp;
211 frame.sf_uc.uc_mcontext.sc_edx = regs->tf_edx;
212 frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_ecx;
213 frame.sf_uc.uc_mcontext.sc_eax = regs->tf_eax;
214 frame.sf_uc.uc_mcontext.sc_eip = regs->tf_eip;
215 frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
216 frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags;
217 frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
218 frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss;
219 frame.sf_uc.uc_mcontext.sc_err = regs->tf_err;
220 frame.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
221 frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
223 if (copyout(&frame, fp, sizeof(frame)) != 0) {
225 * Process has trashed its stack; give it an illegal
226 * instruction to halt it in its tracks.
232 /* Build context to run handler in. */
233 regs->tf_esp = PTROUT(fp);
234 regs->tf_eip = linux_vdso_rt_sigcode;
235 regs->tf_edi = PTROUT(catcher);
236 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
237 regs->tf_cs = _ucodesel;
238 regs->tf_ds = _udatasel;
239 regs->tf_es = _udatasel;
240 regs->tf_fs = _udatasel;
241 regs->tf_ss = _udatasel;
243 mtx_lock(&psp->ps_mtx);
247 * Send an interrupt to process.
249 * Stack is set up to allow sigcode stored
250 * in u. to call routine, followed by kcall
251 * to sigreturn routine below. After sigreturn
252 * resets the signal mask, the stack, and the
253 * frame pointer, it returns to the user
257 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
259 struct thread *td = curthread;
260 struct proc *p = td->td_proc;
262 struct trapframe *regs;
263 struct l_sigframe *fp, frame;
268 PROC_LOCK_ASSERT(p, MA_OWNED);
270 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
271 mtx_assert(&psp->ps_mtx, MA_OWNED);
272 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
273 /* Signal handler installed with SA_SIGINFO. */
274 linux_rt_sendsig(catcher, ksi, mask);
278 oonstack = sigonstack(regs->tf_esp);
280 /* Allocate space for the signal handler context. */
281 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
282 SIGISMEMBER(psp->ps_sigonstack, sig)) {
283 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
284 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
286 fp = (struct l_sigframe *)regs->tf_esp - 1;
287 mtx_unlock(&psp->ps_mtx);
290 /* Build the argument list for the signal handler. */
291 sig = bsd_to_linux_signal(sig);
293 bzero(&frame, sizeof(frame));
296 frame.sf_sigmask = *mask;
297 bsd_to_linux_sigset(mask, &lmask);
299 /* Build the signal context to be used by sigreturn. */
300 frame.sf_sc.sc_mask = lmask.__mask;
301 frame.sf_sc.sc_gs = rgs();
302 frame.sf_sc.sc_fs = regs->tf_fs;
303 frame.sf_sc.sc_es = regs->tf_es;
304 frame.sf_sc.sc_ds = regs->tf_ds;
305 frame.sf_sc.sc_edi = regs->tf_edi;
306 frame.sf_sc.sc_esi = regs->tf_esi;
307 frame.sf_sc.sc_ebp = regs->tf_ebp;
308 frame.sf_sc.sc_ebx = regs->tf_ebx;
309 frame.sf_sc.sc_esp = regs->tf_esp;
310 frame.sf_sc.sc_edx = regs->tf_edx;
311 frame.sf_sc.sc_ecx = regs->tf_ecx;
312 frame.sf_sc.sc_eax = regs->tf_eax;
313 frame.sf_sc.sc_eip = regs->tf_eip;
314 frame.sf_sc.sc_cs = regs->tf_cs;
315 frame.sf_sc.sc_eflags = regs->tf_eflags;
316 frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
317 frame.sf_sc.sc_ss = regs->tf_ss;
318 frame.sf_sc.sc_err = regs->tf_err;
319 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr;
320 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
322 if (copyout(&frame, fp, sizeof(frame)) != 0) {
324 * Process has trashed its stack; give it an illegal
325 * instruction to halt it in its tracks.
331 /* Build context to run handler in. */
332 regs->tf_esp = PTROUT(fp);
333 regs->tf_eip = linux_vdso_sigcode;
334 regs->tf_edi = PTROUT(catcher);
335 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
336 regs->tf_cs = _ucodesel;
337 regs->tf_ds = _udatasel;
338 regs->tf_es = _udatasel;
339 regs->tf_fs = _udatasel;
340 regs->tf_ss = _udatasel;
342 mtx_lock(&psp->ps_mtx);
346 * System call to cleanup state after a signal
347 * has been taken. Reset signal mask and
348 * stack state from context left by sendsig (above).
349 * Return to previous pc and psl as specified by
350 * context left by sendsig. Check carefully to
351 * make sure that the user has not modified the
352 * psl to gain improper privileges or to cause
356 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
358 struct l_sigframe frame;
359 struct trapframe *regs;
366 * The trampoline code hands us the sigframe.
367 * It is unsafe to keep track of it ourselves, in the event that a
368 * program jumps out of a signal handler.
370 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
373 /* Check for security violations. */
374 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
375 eflags = frame.sf_sc.sc_eflags;
376 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
380 * Don't allow users to load a valid privileged %cs. Let the
381 * hardware check for invalid selectors, excess privilege in
382 * other selectors, invalid %eip's and invalid %esp's.
384 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
385 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
386 ksiginfo_init_trap(&ksi);
387 ksi.ksi_signo = SIGBUS;
388 ksi.ksi_code = BUS_OBJERR;
389 ksi.ksi_trapno = T_PROTFLT;
390 ksi.ksi_addr = (void *)regs->tf_eip;
391 trapsignal(td, &ksi);
395 kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
397 /* Restore signal context. */
398 /* %gs was restored by the trampoline. */
399 regs->tf_fs = frame.sf_sc.sc_fs;
400 regs->tf_es = frame.sf_sc.sc_es;
401 regs->tf_ds = frame.sf_sc.sc_ds;
402 regs->tf_edi = frame.sf_sc.sc_edi;
403 regs->tf_esi = frame.sf_sc.sc_esi;
404 regs->tf_ebp = frame.sf_sc.sc_ebp;
405 regs->tf_ebx = frame.sf_sc.sc_ebx;
406 regs->tf_edx = frame.sf_sc.sc_edx;
407 regs->tf_ecx = frame.sf_sc.sc_ecx;
408 regs->tf_eax = frame.sf_sc.sc_eax;
409 regs->tf_eip = frame.sf_sc.sc_eip;
410 regs->tf_cs = frame.sf_sc.sc_cs;
411 regs->tf_eflags = eflags;
412 regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
413 regs->tf_ss = frame.sf_sc.sc_ss;
415 return (EJUSTRETURN);
419 * System call to cleanup state after a signal
420 * has been taken. Reset signal mask and
421 * stack state from context left by rt_sendsig (above).
422 * Return to previous pc and psl as specified by
423 * context left by sendsig. Check carefully to
424 * make sure that the user has not modified the
425 * psl to gain improper privileges or to cause
429 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
431 struct l_ucontext uc;
432 struct l_sigcontext *context;
436 struct trapframe *regs;
443 * The trampoline code hands us the ucontext.
444 * It is unsafe to keep track of it ourselves, in the event that a
445 * program jumps out of a signal handler.
447 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
450 context = &uc.uc_mcontext;
452 /* Check for security violations. */
453 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
454 eflags = context->sc_eflags;
455 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
459 * Don't allow users to load a valid privileged %cs. Let the
460 * hardware check for invalid selectors, excess privilege in
461 * other selectors, invalid %eip's and invalid %esp's.
463 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
464 if (!CS_SECURE(context->sc_cs)) {
465 ksiginfo_init_trap(&ksi);
466 ksi.ksi_signo = SIGBUS;
467 ksi.ksi_code = BUS_OBJERR;
468 ksi.ksi_trapno = T_PROTFLT;
469 ksi.ksi_addr = (void *)regs->tf_eip;
470 trapsignal(td, &ksi);
474 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
475 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
477 /* Restore signal context. */
478 /* %gs was restored by the trampoline. */
479 regs->tf_fs = context->sc_fs;
480 regs->tf_es = context->sc_es;
481 regs->tf_ds = context->sc_ds;
482 regs->tf_edi = context->sc_edi;
483 regs->tf_esi = context->sc_esi;
484 regs->tf_ebp = context->sc_ebp;
485 regs->tf_ebx = context->sc_ebx;
486 regs->tf_edx = context->sc_edx;
487 regs->tf_ecx = context->sc_ecx;
488 regs->tf_eax = context->sc_eax;
489 regs->tf_eip = context->sc_eip;
490 regs->tf_cs = context->sc_cs;
491 regs->tf_eflags = eflags;
492 regs->tf_esp = context->sc_esp_at_signal;
493 regs->tf_ss = context->sc_ss;
495 /* Call sigaltstack & ignore results. */
497 ss.ss_sp = PTRIN(lss->ss_sp);
498 ss.ss_size = lss->ss_size;
499 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
501 (void)kern_sigaltstack(td, &ss, NULL);
503 return (EJUSTRETURN);
507 linux_fetch_syscall_args(struct thread *td)
510 struct trapframe *frame;
511 struct syscall_args *sa;
514 frame = td->td_frame;
517 sa->code = frame->tf_eax;
518 sa->original_code = sa->code;
519 sa->args[0] = frame->tf_ebx;
520 sa->args[1] = frame->tf_ecx;
521 sa->args[2] = frame->tf_edx;
522 sa->args[3] = frame->tf_esi;
523 sa->args[4] = frame->tf_edi;
524 sa->args[5] = frame->tf_ebp; /* Unconfirmed */
526 if (sa->code >= p->p_sysent->sv_size)
528 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
530 sa->callp = &p->p_sysent->sv_table[sa->code];
532 td->td_retval[0] = 0;
533 td->td_retval[1] = frame->tf_edx;
539 linux_set_syscall_retval(struct thread *td, int error)
541 struct trapframe *frame = td->td_frame;
543 cpu_set_syscall_retval(td, error);
545 if (__predict_false(error != 0)) {
546 if (error != ERESTART && error != EJUSTRETURN)
547 frame->tf_eax = bsd_to_linux_errno(error);
552 linux_set_fork_retval(struct thread *td)
554 struct trapframe *frame = td->td_frame;
560 * exec_setregs may initialize some registers differently than Linux
561 * does, thus potentially confusing Linux binaries. If necessary, we
562 * override the exec_setregs default(s) here.
565 linux_exec_setregs(struct thread *td, struct image_params *imgp,
568 struct pcb *pcb = td->td_pcb;
570 exec_setregs(td, imgp, stack);
572 /* Linux sets %gs to 0, we default to _udatasel. */
576 pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
579 struct sysentvec linux_sysvec = {
580 .sv_size = LINUX_SYS_MAXSYSCALL,
581 .sv_table = linux_sysent,
582 .sv_fixup = linux_fixup,
583 .sv_sendsig = linux_sendsig,
584 .sv_sigcode = &_binary_linux_vdso_so_o_start,
585 .sv_szsigcode = &linux_szsigcode,
586 .sv_name = "Linux a.out",
588 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
589 .sv_minuser = VM_MIN_ADDRESS,
590 .sv_maxuser = VM_MAXUSER_ADDRESS,
591 .sv_usrstack = LINUX_USRSTACK,
592 .sv_psstrings = PS_STRINGS,
593 .sv_psstringssz = sizeof(struct ps_strings),
594 .sv_stackprot = VM_PROT_ALL,
595 .sv_copyout_strings = exec_copyout_strings,
596 .sv_setregs = linux_exec_setregs,
599 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
600 SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
601 .sv_set_syscall_retval = linux_set_syscall_retval,
602 .sv_fetch_syscall_args = linux_fetch_syscall_args,
603 .sv_syscallnames = linux_syscallnames,
604 .sv_schedtail = linux_schedtail,
605 .sv_thread_detach = linux_thread_detach,
609 .sv_onexec = linux_on_exec_vmspace,
610 .sv_onexit = linux_on_exit,
611 .sv_ontdexit = linux_thread_dtor,
612 .sv_setid_allowed = &linux_setid_allowed_query,
613 .sv_set_fork_retval = linux_set_fork_retval,
615 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
617 struct sysentvec elf_linux_sysvec = {
618 .sv_size = LINUX_SYS_MAXSYSCALL,
619 .sv_table = linux_sysent,
620 .sv_fixup = __elfN(freebsd_fixup),
621 .sv_sendsig = linux_sendsig,
622 .sv_sigcode = &_binary_linux_vdso_so_o_start,
623 .sv_szsigcode = &linux_szsigcode,
624 .sv_name = "Linux ELF32",
625 .sv_coredump = elf32_coredump,
626 .sv_elf_core_osabi = ELFOSABI_NONE,
627 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
628 .sv_elf_core_prepare_notes = __linuxN(prepare_notes),
629 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
630 .sv_minuser = VM_MIN_ADDRESS,
631 .sv_maxuser = VM_MAXUSER_ADDRESS,
632 .sv_usrstack = LINUX_USRSTACK,
633 .sv_psstrings = LINUX_PS_STRINGS,
634 .sv_psstringssz = sizeof(struct ps_strings),
635 .sv_stackprot = VM_PROT_ALL,
636 .sv_copyout_auxargs = __linuxN(copyout_auxargs),
637 .sv_copyout_strings = __linuxN(copyout_strings),
638 .sv_setregs = linux_exec_setregs,
641 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
642 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
643 .sv_set_syscall_retval = linux_set_syscall_retval,
644 .sv_fetch_syscall_args = linux_fetch_syscall_args,
645 .sv_syscallnames = NULL,
646 .sv_shared_page_base = LINUX_SHAREDPAGE,
647 .sv_shared_page_len = PAGE_SIZE,
648 .sv_schedtail = linux_schedtail,
649 .sv_thread_detach = linux_thread_detach,
653 .sv_onexec = linux_on_exec_vmspace,
654 .sv_onexit = linux_on_exit,
655 .sv_ontdexit = linux_thread_dtor,
656 .sv_setid_allowed = &linux_setid_allowed_query,
657 .sv_set_fork_retval = linux_set_fork_retval,
661 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
665 if (SV_PROC_FLAG(p, SV_SHP) != 0)
666 error = linux_map_vdso(p, linux_vdso_obj,
667 linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
669 error = linux_on_exec(p, imgp);
674 * linux_vdso_install() and linux_exec_sysvec_init() must be called
675 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
678 linux_exec_sysvec_init(void *param)
680 l_uintptr_t *ktimekeep_base, *ktsc_selector;
681 struct sysentvec *sv;
685 /* Fill timekeep_base */
686 exec_sysvec_init(sv);
688 tkoff = kern_timekeep_base - linux_vdso_base;
689 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
690 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
692 tkoff = kern_tsc_selector - linux_vdso_base;
693 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
694 *ktsc_selector = linux_vdso_tsc_selector_idx();
696 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
698 tkoff = kern_cpu_selector - linux_vdso_base;
699 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
700 *ktsc_selector = linux_vdso_cpu_selector_idx();
702 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
704 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
705 linux_exec_sysvec_init, &elf_linux_sysvec);
708 linux_vdso_install(const void *param)
710 char *vdso_start = &_binary_linux_vdso_so_o_start;
711 char *vdso_end = &_binary_linux_vdso_so_o_end;
713 linux_szsigcode = vdso_end - vdso_start;
714 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
716 linux_vdso_base = LINUX_VDSOPAGE;
718 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
720 linux_vdso_obj = __elfN(linux_shared_page_init)
721 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
722 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
724 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
726 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
727 linux_vdso_install, NULL);
730 linux_vdso_deinstall(const void *param)
733 __elfN(linux_shared_page_fini)(linux_vdso_obj,
734 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
736 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
737 linux_vdso_deinstall, NULL);
740 linux_vdso_reloc(char *mapping, Elf_Addr offset)
742 const Elf_Shdr *shdr;
744 const Elf_Ehdr *ehdr;
746 Elf_Size rtype, symidx;
747 Elf_Addr addr, addend;
753 ehdr = (const Elf_Ehdr *)mapping;
754 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
755 for (i = 0; i < ehdr->e_shnum; i++)
757 switch (shdr[i].sh_type) {
759 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
760 relcnt = shdr[i].sh_size / sizeof(*rel);
763 printf("Linux i386 vDSO: unexpected Rela section\n");
768 for (i = 0; i < relcnt; i++, rel++) {
769 where = (Elf_Addr *)(mapping + rel->r_offset);
771 rtype = ELF_R_TYPE(rel->r_info);
772 symidx = ELF_R_SYM(rel->r_info);
775 case R_386_NONE: /* none */
778 case R_386_RELATIVE: /* B + A */
779 addr = (Elf_Addr)PTROUT(offset + addend);
784 case R_386_IRELATIVE:
785 printf("Linux i386 vDSO: unexpected ifunc relocation, "
786 "symbol index %d\n", symidx);
789 printf("Linux i386 vDSO: unexpected relocation type %d, "
790 "symbol index %d\n", rtype, symidx);
795 static Elf_Brandnote linux_brandnote = {
796 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
797 .hdr.n_descsz = 16, /* XXX at least 16 */
799 .vendor = GNU_ABI_VENDOR,
800 .flags = BN_TRANSLATE_OSREL,
801 .trans_osrel = linux_trans_osrel
804 static Elf32_Brandinfo linux_brand = {
805 .brand = ELFOSABI_LINUX,
807 .compat_3_brand = "Linux",
808 .interp_path = "/lib/ld-linux.so.1",
809 .sysvec = &elf_linux_sysvec,
810 .interp_newpath = NULL,
811 .brand_note = &linux_brandnote,
812 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
815 static Elf32_Brandinfo linux_glibc2brand = {
816 .brand = ELFOSABI_LINUX,
818 .compat_3_brand = "Linux",
819 .interp_path = "/lib/ld-linux.so.2",
820 .sysvec = &elf_linux_sysvec,
821 .interp_newpath = NULL,
822 .brand_note = &linux_brandnote,
823 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
826 static Elf32_Brandinfo linux_muslbrand = {
827 .brand = ELFOSABI_LINUX,
829 .compat_3_brand = "Linux",
830 .interp_path = "/lib/ld-musl-i386.so.1",
831 .sysvec = &elf_linux_sysvec,
832 .interp_newpath = NULL,
833 .brand_note = &linux_brandnote,
834 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
835 LINUX_BI_FUTEX_REQUEUE
838 Elf32_Brandinfo *linux_brandlist[] = {
846 linux_elf_modevent(module_t mod, int type, void *data)
848 Elf32_Brandinfo **brandinfo;
850 struct linux_ioctl_handler **lihp;
856 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
858 if (elf32_insert_brand_entry(*brandinfo) < 0)
861 SET_FOREACH(lihp, linux_ioctl_handler_set)
862 linux_ioctl_register_handler(*lihp);
863 linux_dev_shm_create();
864 linux_osd_jail_register();
865 linux_netlink_register();
866 stclohz = (stathz ? stathz : hz);
868 printf("Linux ELF exec handler installed\n");
870 printf("cannot insert Linux ELF brand handler\n");
873 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
875 if (elf32_brand_inuse(*brandinfo))
878 for (brandinfo = &linux_brandlist[0];
879 *brandinfo != NULL; ++brandinfo)
880 if (elf32_remove_brand_entry(*brandinfo) < 0)
884 SET_FOREACH(lihp, linux_ioctl_handler_set)
885 linux_ioctl_unregister_handler(*lihp);
886 linux_netlink_deregister();
887 linux_dev_shm_destroy();
888 linux_osd_jail_deregister();
890 printf("Linux ELF exec handler removed\n");
892 printf("Could not deinstall ELF interpreter entry\n");
900 static moduledata_t linux_elf_mod = {
906 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
907 MODULE_DEPEND(linuxelf, netlink, 1, 1, 1);
908 FEATURE(linux, "Linux 32bit support");