2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
45 #include <sys/signalvar.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
49 #include <sys/sysproto.h>
50 #include <sys/vnode.h>
51 #include <sys/eventhandler.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_param.h>
61 #include <machine/cpu.h>
62 #include <machine/cputypes.h>
63 #include <machine/md_var.h>
64 #include <machine/pcb.h>
65 #include <machine/trap.h>
67 #include <i386/linux/linux.h>
68 #include <i386/linux/linux_proto.h>
69 #include <compat/linux/linux_emul.h>
70 #include <compat/linux/linux_ioctl.h>
71 #include <compat/linux/linux_mib.h>
72 #include <compat/linux/linux_misc.h>
73 #include <compat/linux/linux_signal.h>
74 #include <compat/linux/linux_util.h>
75 #include <compat/linux/linux_vdso.h>
77 MODULE_VERSION(linux, 1);
79 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
81 static int linux_szsigcode;
82 static vm_object_t linux_shared_page_obj;
83 static char *linux_shared_page_mapping;
84 extern char _binary_linux_locore_o_start;
85 extern char _binary_linux_locore_o_end;
87 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
89 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
91 static int linux_fixup(uintptr_t *stack_base,
92 struct image_params *iparams);
93 static int linux_fixup_elf(uintptr_t *stack_base,
94 struct image_params *iparams);
95 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
96 static void linux_exec_setregs(struct thread *td,
97 struct image_params *imgp, uintptr_t stack);
98 static int linux_copyout_strings(struct image_params *imgp,
99 uintptr_t *stack_base);
100 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
101 static void linux_vdso_install(void *param);
102 static void linux_vdso_deinstall(void *param);
104 static int linux_szplatform;
105 const char *linux_kplatform;
107 static eventhandler_tag linux_exit_tag;
108 static eventhandler_tag linux_exec_tag;
109 static eventhandler_tag linux_thread_dtor_tag;
111 #define LINUX_T_UNKNOWN 255
112 static int _bsd_to_linux_trapcode[] = {
113 LINUX_T_UNKNOWN, /* 0 */
114 6, /* 1 T_PRIVINFLT */
115 LINUX_T_UNKNOWN, /* 2 */
117 LINUX_T_UNKNOWN, /* 4 */
118 LINUX_T_UNKNOWN, /* 5 */
119 16, /* 6 T_ARITHTRAP */
120 254, /* 7 T_ASTFLT */
121 LINUX_T_UNKNOWN, /* 8 */
122 13, /* 9 T_PROTFLT */
123 1, /* 10 T_TRCTRAP */
124 LINUX_T_UNKNOWN, /* 11 */
125 14, /* 12 T_PAGEFLT */
126 LINUX_T_UNKNOWN, /* 13 */
127 17, /* 14 T_ALIGNFLT */
128 LINUX_T_UNKNOWN, /* 15 */
129 LINUX_T_UNKNOWN, /* 16 */
130 LINUX_T_UNKNOWN, /* 17 */
136 8, /* 23 T_DOUBLEFLT */
137 9, /* 24 T_FPOPFLT */
138 10, /* 25 T_TSSFLT */
139 11, /* 26 T_SEGNPFLT */
140 12, /* 27 T_STKFLT */
142 19, /* 29 T_XMMFLT */
143 15 /* 30 T_RESERVED */
145 #define bsd_to_linux_trapcode(code) \
146 ((code)<nitems(_bsd_to_linux_trapcode)? \
147 _bsd_to_linux_trapcode[(code)]: \
150 LINUX_VDSO_SYM_INTPTR(linux_sigcode);
151 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
152 LINUX_VDSO_SYM_INTPTR(linux_vsyscall);
155 * If FreeBSD & Linux have a difference of opinion about what a trap
156 * means, deal with it here.
161 linux_translate_traps(int signal, int trap_code)
163 if (signal != SIGBUS)
177 linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
179 register_t *base, *argv, *envp;
181 base = (register_t *)*stack_base;
183 envp = base + (imgp->args->argc + 1);
185 suword(base, (intptr_t)envp);
187 suword(base, (intptr_t)argv);
189 suword(base, imgp->args->argc);
190 *stack_base = (uintptr_t)base;
195 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
199 Elf32_Auxinfo *argarray, *pos;
200 Elf32_Addr *uplatform;
201 struct ps_strings *arginfo;
202 int error, issetugid;
205 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
206 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
207 uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform);
208 args = (Elf32_Auxargs *)imgp->auxargs;
209 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
212 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
213 imgp->proc->p_sysent->sv_shared_page_base);
214 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall);
215 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
218 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
219 * as it has appeared in the 2.4.0-rc7 first time.
220 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
221 * glibc falls back to the hard-coded CLK_TCK value when aux entry
223 * Also see linux_times() implementation.
225 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
226 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
227 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
228 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
229 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
230 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
231 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
232 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
233 AUXARGS_ENTRY(pos, AT_BASE, args->base);
234 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
235 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
236 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
237 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
238 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
239 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform));
240 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
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, AT_NULL, 0);
247 free(imgp->auxargs, M_TEMP);
248 imgp->auxargs = NULL;
249 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
251 error = copyout(argarray, (void *)base,
252 sizeof(*argarray) * LINUX_AT_COUNT);
253 free(argarray, M_TEMP);
258 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
262 base = (register_t *)*stack_base;
264 if (suword(base, (register_t)imgp->args->argc) == -1)
266 *stack_base = (uintptr_t)base;
271 * Copied from kern/kern_exec.c
274 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
276 int argc, envc, error;
279 uintptr_t destp, ustringp;
280 struct ps_strings *arginfo;
281 char canary[LINUX_AT_RANDOM_LEN];
285 /* Calculate string base and vector table pointers. */
287 if (imgp->execpath != NULL && imgp->auxargs != NULL)
288 execpath_len = strlen(imgp->execpath) + 1;
291 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
292 destp = (uintptr_t)arginfo;
294 /* Install LINUX_PLATFORM. */
295 destp -= linux_szplatform;
296 destp = rounddown2(destp, sizeof(void *));
297 error = copyout(linux_kplatform, (void *)destp, linux_szplatform);
301 if (execpath_len != 0) {
302 destp -= execpath_len;
303 destp = rounddown2(destp, sizeof(void *));
304 imgp->execpathp = (void *)destp;
305 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
310 /* Prepare the canary for SSP. */
311 arc4rand(canary, sizeof(canary), 0);
312 destp -= roundup(sizeof(canary), sizeof(void *));
313 imgp->canary = (void *)destp;
314 error = copyout(canary, imgp->canary, sizeof(canary));
318 /* Allocate room for the argument and environment strings. */
319 destp -= ARG_MAX - imgp->args->stringspace;
320 destp = rounddown2(destp, sizeof(void *));
325 * Allocate room on the stack for the ELF auxargs
326 * array. It has LINUX_AT_COUNT entries.
328 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
329 destp = rounddown2(destp, sizeof(void *));
332 vectp = (char **)destp;
335 * Allocate room for the argv[] and env vectors including the
336 * terminating NULL pointers.
338 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
340 /* vectp also becomes our initial stack base. */
341 *stack_base = (uintptr_t)vectp;
343 stringp = imgp->args->begin_argv;
344 argc = imgp->args->argc;
345 envc = imgp->args->envc;
347 /* Copy out strings - arguments and environment. */
348 error = copyout(stringp, (void *)ustringp,
349 ARG_MAX - imgp->args->stringspace);
353 /* Fill in "ps_strings" struct for ps, w, etc. */
354 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
355 suword(&arginfo->ps_nargvstr, argc) != 0)
358 /* Fill in argument portion of vector table. */
359 for (; argc > 0; --argc) {
360 if (suword(vectp++, ustringp) != 0)
362 while (*stringp++ != 0)
367 /* A null vector table pointer separates the argp's from the envp's. */
368 if (suword(vectp++, 0) != 0)
371 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
372 suword(&arginfo->ps_nenvstr, envc) != 0)
375 /* Fill in environment portion of vector table. */
376 for (; envc > 0; --envc) {
377 if (suword(vectp++, ustringp) != 0)
379 while (*stringp++ != 0)
384 /* The end of the vector table is a null pointer. */
385 if (suword(vectp, 0) != 0)
390 error = imgp->sysent->sv_copyout_auxargs(imgp,
400 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
402 struct thread *td = curthread;
403 struct proc *p = td->td_proc;
405 struct trapframe *regs;
406 struct l_rt_sigframe *fp, frame;
410 sig = ksi->ksi_signo;
411 code = ksi->ksi_code;
412 PROC_LOCK_ASSERT(p, MA_OWNED);
414 mtx_assert(&psp->ps_mtx, MA_OWNED);
416 oonstack = sigonstack(regs->tf_esp);
418 /* Allocate space for the signal handler context. */
419 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
420 SIGISMEMBER(psp->ps_sigonstack, sig)) {
421 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
422 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
424 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
425 mtx_unlock(&psp->ps_mtx);
427 /* Build the argument list for the signal handler. */
428 sig = bsd_to_linux_signal(sig);
430 bzero(&frame, sizeof(frame));
432 frame.sf_handler = catcher;
434 frame.sf_siginfo = &fp->sf_si;
435 frame.sf_ucontext = &fp->sf_sc;
437 /* Fill in POSIX parts. */
438 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
440 /* Build the signal context to be used by sigreturn. */
441 frame.sf_sc.uc_flags = 0; /* XXX ??? */
442 frame.sf_sc.uc_link = NULL; /* XXX ??? */
444 frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp;
445 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
446 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
447 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
450 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
452 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
453 frame.sf_sc.uc_mcontext.sc_gs = rgs();
454 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
455 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
456 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
457 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi;
458 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi;
459 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp;
460 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx;
461 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_esp;
462 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx;
463 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx;
464 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax;
465 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip;
466 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
467 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
468 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
469 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
470 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
471 frame.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
472 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
474 if (copyout(&frame, fp, sizeof(frame)) != 0) {
476 * Process has trashed its stack; give it an illegal
477 * instruction to halt it in its tracks.
483 /* Build context to run handler in. */
484 regs->tf_esp = (int)fp;
485 regs->tf_eip = linux_rt_sigcode;
486 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
487 regs->tf_cs = _ucodesel;
488 regs->tf_ds = _udatasel;
489 regs->tf_es = _udatasel;
490 regs->tf_fs = _udatasel;
491 regs->tf_ss = _udatasel;
493 mtx_lock(&psp->ps_mtx);
498 * Send an interrupt to process.
500 * Stack is set up to allow sigcode stored
501 * in u. to call routine, followed by kcall
502 * to sigreturn routine below. After sigreturn
503 * resets the signal mask, the stack, and the
504 * frame pointer, it returns to the user
508 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
510 struct thread *td = curthread;
511 struct proc *p = td->td_proc;
513 struct trapframe *regs;
514 struct l_sigframe *fp, frame;
519 PROC_LOCK_ASSERT(p, MA_OWNED);
521 sig = ksi->ksi_signo;
522 code = ksi->ksi_code;
523 mtx_assert(&psp->ps_mtx, MA_OWNED);
524 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
525 /* Signal handler installed with SA_SIGINFO. */
526 linux_rt_sendsig(catcher, ksi, mask);
530 oonstack = sigonstack(regs->tf_esp);
532 /* Allocate space for the signal handler context. */
533 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
534 SIGISMEMBER(psp->ps_sigonstack, sig)) {
535 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
536 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
538 fp = (struct l_sigframe *)regs->tf_esp - 1;
539 mtx_unlock(&psp->ps_mtx);
542 /* Build the argument list for the signal handler. */
543 sig = bsd_to_linux_signal(sig);
545 bzero(&frame, sizeof(frame));
547 frame.sf_handler = catcher;
550 bsd_to_linux_sigset(mask, &lmask);
552 /* Build the signal context to be used by sigreturn. */
553 frame.sf_sc.sc_mask = lmask.__mask;
554 frame.sf_sc.sc_gs = rgs();
555 frame.sf_sc.sc_fs = regs->tf_fs;
556 frame.sf_sc.sc_es = regs->tf_es;
557 frame.sf_sc.sc_ds = regs->tf_ds;
558 frame.sf_sc.sc_edi = regs->tf_edi;
559 frame.sf_sc.sc_esi = regs->tf_esi;
560 frame.sf_sc.sc_ebp = regs->tf_ebp;
561 frame.sf_sc.sc_ebx = regs->tf_ebx;
562 frame.sf_sc.sc_esp = regs->tf_esp;
563 frame.sf_sc.sc_edx = regs->tf_edx;
564 frame.sf_sc.sc_ecx = regs->tf_ecx;
565 frame.sf_sc.sc_eax = regs->tf_eax;
566 frame.sf_sc.sc_eip = regs->tf_eip;
567 frame.sf_sc.sc_cs = regs->tf_cs;
568 frame.sf_sc.sc_eflags = regs->tf_eflags;
569 frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
570 frame.sf_sc.sc_ss = regs->tf_ss;
571 frame.sf_sc.sc_err = regs->tf_err;
572 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr;
573 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
575 frame.sf_extramask[0] = lmask.__mask;
577 if (copyout(&frame, fp, sizeof(frame)) != 0) {
579 * Process has trashed its stack; give it an illegal
580 * instruction to halt it in its tracks.
586 /* Build context to run handler in. */
587 regs->tf_esp = (int)fp;
588 regs->tf_eip = linux_sigcode;
589 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
590 regs->tf_cs = _ucodesel;
591 regs->tf_ds = _udatasel;
592 regs->tf_es = _udatasel;
593 regs->tf_fs = _udatasel;
594 regs->tf_ss = _udatasel;
596 mtx_lock(&psp->ps_mtx);
600 * System call to cleanup state after a signal
601 * has been taken. Reset signal mask and
602 * stack state from context left by sendsig (above).
603 * Return to previous pc and psl as specified by
604 * context left by sendsig. Check carefully to
605 * make sure that the user has not modified the
606 * psl to gain improper privileges or to cause
610 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
612 struct l_sigframe frame;
613 struct trapframe *regs;
622 * The trampoline code hands us the sigframe.
623 * It is unsafe to keep track of it ourselves, in the event that a
624 * program jumps out of a signal handler.
626 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
629 /* Check for security violations. */
630 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
631 eflags = frame.sf_sc.sc_eflags;
632 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
636 * Don't allow users to load a valid privileged %cs. Let the
637 * hardware check for invalid selectors, excess privilege in
638 * other selectors, invalid %eip's and invalid %esp's.
640 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
641 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
642 ksiginfo_init_trap(&ksi);
643 ksi.ksi_signo = SIGBUS;
644 ksi.ksi_code = BUS_OBJERR;
645 ksi.ksi_trapno = T_PROTFLT;
646 ksi.ksi_addr = (void *)regs->tf_eip;
647 trapsignal(td, &ksi);
651 lmask.__mask = frame.sf_sc.sc_mask;
652 linux_to_bsd_sigset(&lmask, &bmask);
653 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
655 /* Restore signal context. */
656 /* %gs was restored by the trampoline. */
657 regs->tf_fs = frame.sf_sc.sc_fs;
658 regs->tf_es = frame.sf_sc.sc_es;
659 regs->tf_ds = frame.sf_sc.sc_ds;
660 regs->tf_edi = frame.sf_sc.sc_edi;
661 regs->tf_esi = frame.sf_sc.sc_esi;
662 regs->tf_ebp = frame.sf_sc.sc_ebp;
663 regs->tf_ebx = frame.sf_sc.sc_ebx;
664 regs->tf_edx = frame.sf_sc.sc_edx;
665 regs->tf_ecx = frame.sf_sc.sc_ecx;
666 regs->tf_eax = frame.sf_sc.sc_eax;
667 regs->tf_eip = frame.sf_sc.sc_eip;
668 regs->tf_cs = frame.sf_sc.sc_cs;
669 regs->tf_eflags = eflags;
670 regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
671 regs->tf_ss = frame.sf_sc.sc_ss;
673 return (EJUSTRETURN);
677 * System call to cleanup state after a signal
678 * has been taken. Reset signal mask and
679 * stack state from context left by rt_sendsig (above).
680 * Return to previous pc and psl as specified by
681 * context left by sendsig. Check carefully to
682 * make sure that the user has not modified the
683 * psl to gain improper privileges or to cause
687 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
689 struct l_ucontext uc;
690 struct l_sigcontext *context;
694 struct trapframe *regs;
701 * The trampoline code hands us the ucontext.
702 * It is unsafe to keep track of it ourselves, in the event that a
703 * program jumps out of a signal handler.
705 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
708 context = &uc.uc_mcontext;
710 /* Check for security violations. */
711 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
712 eflags = context->sc_eflags;
713 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
717 * Don't allow users to load a valid privileged %cs. Let the
718 * hardware check for invalid selectors, excess privilege in
719 * other selectors, invalid %eip's and invalid %esp's.
721 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
722 if (!CS_SECURE(context->sc_cs)) {
723 ksiginfo_init_trap(&ksi);
724 ksi.ksi_signo = SIGBUS;
725 ksi.ksi_code = BUS_OBJERR;
726 ksi.ksi_trapno = T_PROTFLT;
727 ksi.ksi_addr = (void *)regs->tf_eip;
728 trapsignal(td, &ksi);
732 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
733 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
735 /* Restore signal context. */
736 /* %gs was restored by the trampoline. */
737 regs->tf_fs = context->sc_fs;
738 regs->tf_es = context->sc_es;
739 regs->tf_ds = context->sc_ds;
740 regs->tf_edi = context->sc_edi;
741 regs->tf_esi = context->sc_esi;
742 regs->tf_ebp = context->sc_ebp;
743 regs->tf_ebx = context->sc_ebx;
744 regs->tf_edx = context->sc_edx;
745 regs->tf_ecx = context->sc_ecx;
746 regs->tf_eax = context->sc_eax;
747 regs->tf_eip = context->sc_eip;
748 regs->tf_cs = context->sc_cs;
749 regs->tf_eflags = eflags;
750 regs->tf_esp = context->sc_esp_at_signal;
751 regs->tf_ss = context->sc_ss;
753 /* Call sigaltstack & ignore results. */
755 ss.ss_sp = lss->ss_sp;
756 ss.ss_size = lss->ss_size;
757 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
759 (void)kern_sigaltstack(td, &ss, NULL);
761 return (EJUSTRETURN);
765 linux_fetch_syscall_args(struct thread *td)
768 struct trapframe *frame;
769 struct syscall_args *sa;
772 frame = td->td_frame;
775 sa->code = frame->tf_eax;
776 sa->args[0] = frame->tf_ebx;
777 sa->args[1] = frame->tf_ecx;
778 sa->args[2] = frame->tf_edx;
779 sa->args[3] = frame->tf_esi;
780 sa->args[4] = frame->tf_edi;
781 sa->args[5] = frame->tf_ebp; /* Unconfirmed */
783 if (sa->code >= p->p_sysent->sv_size)
785 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
787 sa->callp = &p->p_sysent->sv_table[sa->code];
788 sa->narg = sa->callp->sy_narg;
790 td->td_retval[0] = 0;
791 td->td_retval[1] = frame->tf_edx;
797 * exec_setregs may initialize some registers differently than Linux
798 * does, thus potentially confusing Linux binaries. If necessary, we
799 * override the exec_setregs default(s) here.
802 linux_exec_setregs(struct thread *td, struct image_params *imgp,
805 struct pcb *pcb = td->td_pcb;
807 exec_setregs(td, imgp, stack);
809 /* Linux sets %gs to 0, we default to _udatasel. */
813 pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
817 linux_get_machine(const char **dst)
835 struct sysentvec linux_sysvec = {
836 .sv_size = LINUX_SYS_MAXSYSCALL,
837 .sv_table = linux_sysent,
838 .sv_errsize = ELAST + 1,
839 .sv_errtbl = linux_errtbl,
840 .sv_transtrap = linux_translate_traps,
841 .sv_fixup = linux_fixup,
842 .sv_sendsig = linux_sendsig,
843 .sv_sigcode = &_binary_linux_locore_o_start,
844 .sv_szsigcode = &linux_szsigcode,
845 .sv_name = "Linux a.out",
847 .sv_imgact_try = linux_exec_imgact_try,
848 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
849 .sv_minuser = VM_MIN_ADDRESS,
850 .sv_maxuser = VM_MAXUSER_ADDRESS,
851 .sv_usrstack = LINUX_USRSTACK,
852 .sv_psstrings = PS_STRINGS,
853 .sv_stackprot = VM_PROT_ALL,
854 .sv_copyout_strings = exec_copyout_strings,
855 .sv_setregs = linux_exec_setregs,
858 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32,
859 .sv_set_syscall_retval = cpu_set_syscall_retval,
860 .sv_fetch_syscall_args = linux_fetch_syscall_args,
861 .sv_syscallnames = NULL,
862 .sv_shared_page_base = LINUX_SHAREDPAGE,
863 .sv_shared_page_len = PAGE_SIZE,
864 .sv_schedtail = linux_schedtail,
865 .sv_thread_detach = linux_thread_detach,
868 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
870 struct sysentvec elf_linux_sysvec = {
871 .sv_size = LINUX_SYS_MAXSYSCALL,
872 .sv_table = linux_sysent,
873 .sv_errsize = ELAST + 1,
874 .sv_errtbl = linux_errtbl,
875 .sv_transtrap = linux_translate_traps,
876 .sv_fixup = linux_fixup_elf,
877 .sv_sendsig = linux_sendsig,
878 .sv_sigcode = &_binary_linux_locore_o_start,
879 .sv_szsigcode = &linux_szsigcode,
880 .sv_name = "Linux ELF",
881 .sv_coredump = elf32_coredump,
882 .sv_imgact_try = linux_exec_imgact_try,
883 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
884 .sv_minuser = VM_MIN_ADDRESS,
885 .sv_maxuser = VM_MAXUSER_ADDRESS,
886 .sv_usrstack = LINUX_USRSTACK,
887 .sv_psstrings = LINUX_PS_STRINGS,
888 .sv_stackprot = VM_PROT_ALL,
889 .sv_copyout_auxargs = linux_copyout_auxargs,
890 .sv_copyout_strings = linux_copyout_strings,
891 .sv_setregs = linux_exec_setregs,
894 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP,
895 .sv_set_syscall_retval = cpu_set_syscall_retval,
896 .sv_fetch_syscall_args = linux_fetch_syscall_args,
897 .sv_syscallnames = NULL,
898 .sv_shared_page_base = LINUX_SHAREDPAGE,
899 .sv_shared_page_len = PAGE_SIZE,
900 .sv_schedtail = linux_schedtail,
901 .sv_thread_detach = linux_thread_detach,
906 linux_vdso_install(void *param)
909 linux_szsigcode = (&_binary_linux_locore_o_end -
910 &_binary_linux_locore_o_start);
912 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
913 panic("Linux invalid vdso size\n");
915 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
917 linux_shared_page_obj = __elfN(linux_shared_page_init)
918 (&linux_shared_page_mapping);
920 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
922 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
924 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
926 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
927 linux_vdso_install, NULL);
930 linux_vdso_deinstall(void *param)
933 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
935 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
936 linux_vdso_deinstall, NULL);
938 static char GNU_ABI_VENDOR[] = "GNU";
939 static int GNULINUX_ABI_DESC = 0;
942 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
944 const Elf32_Word *desc;
947 p = (uintptr_t)(note + 1);
948 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
950 desc = (const Elf32_Word *)p;
951 if (desc[0] != GNULINUX_ABI_DESC)
955 * For Linux we encode osrel using the Linux convention of
956 * (version << 16) | (major << 8) | (minor)
957 * See macro in linux_mib.h
959 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
964 static Elf_Brandnote linux_brandnote = {
965 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
966 .hdr.n_descsz = 16, /* XXX at least 16 */
968 .vendor = GNU_ABI_VENDOR,
969 .flags = BN_TRANSLATE_OSREL,
970 .trans_osrel = linux_trans_osrel
973 static Elf32_Brandinfo linux_brand = {
974 .brand = ELFOSABI_LINUX,
976 .compat_3_brand = "Linux",
977 .emul_path = linux_emul_path,
978 .interp_path = "/lib/ld-linux.so.1",
979 .sysvec = &elf_linux_sysvec,
980 .interp_newpath = NULL,
981 .brand_note = &linux_brandnote,
982 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
985 static Elf32_Brandinfo linux_glibc2brand = {
986 .brand = ELFOSABI_LINUX,
988 .compat_3_brand = "Linux",
989 .emul_path = linux_emul_path,
990 .interp_path = "/lib/ld-linux.so.2",
991 .sysvec = &elf_linux_sysvec,
992 .interp_newpath = NULL,
993 .brand_note = &linux_brandnote,
994 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
997 static Elf32_Brandinfo linux_muslbrand = {
998 .brand = ELFOSABI_LINUX,
1000 .compat_3_brand = "Linux",
1001 .emul_path = linux_emul_path,
1002 .interp_path = "/lib/ld-musl-i386.so.1",
1003 .sysvec = &elf_linux_sysvec,
1004 .interp_newpath = NULL,
1005 .brand_note = &linux_brandnote,
1006 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1009 Elf32_Brandinfo *linux_brandlist[] = {
1017 linux_elf_modevent(module_t mod, int type, void *data)
1019 Elf32_Brandinfo **brandinfo;
1021 struct linux_ioctl_handler **lihp;
1027 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1029 if (elf32_insert_brand_entry(*brandinfo) < 0)
1032 SET_FOREACH(lihp, linux_ioctl_handler_set)
1033 linux_ioctl_register_handler(*lihp);
1034 LIST_INIT(&futex_list);
1035 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1036 linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit,
1038 linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec,
1040 linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
1041 linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
1042 linux_get_machine(&linux_kplatform);
1043 linux_szplatform = roundup(strlen(linux_kplatform) + 1,
1045 linux_dev_shm_create();
1046 linux_osd_jail_register();
1047 stclohz = (stathz ? stathz : hz);
1049 printf("Linux ELF exec handler installed\n");
1051 printf("cannot insert Linux ELF brand handler\n");
1054 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1056 if (elf32_brand_inuse(*brandinfo))
1059 for (brandinfo = &linux_brandlist[0];
1060 *brandinfo != NULL; ++brandinfo)
1061 if (elf32_remove_brand_entry(*brandinfo) < 0)
1065 SET_FOREACH(lihp, linux_ioctl_handler_set)
1066 linux_ioctl_unregister_handler(*lihp);
1067 mtx_destroy(&futex_mtx);
1068 EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag);
1069 EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag);
1070 EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag);
1071 linux_dev_shm_destroy();
1072 linux_osd_jail_deregister();
1074 printf("Linux ELF exec handler removed\n");
1076 printf("Could not deinstall ELF interpreter entry\n");
1079 return (EOPNOTSUPP);
1084 static moduledata_t linux_elf_mod = {
1090 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1091 FEATURE(linux, "Linux 32bit support");