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_futex.h>
71 #include <compat/linux/linux_ioctl.h>
72 #include <compat/linux/linux_mib.h>
73 #include <compat/linux/linux_misc.h>
74 #include <compat/linux/linux_signal.h>
75 #include <compat/linux/linux_util.h>
76 #include <compat/linux/linux_vdso.h>
78 MODULE_VERSION(linux, 1);
81 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
82 linux_sysctl_debug, "A", "Linux debugging control");
86 * Allow the sendsig functions to use the ldebug() facility even though they
87 * are not syscalls themselves. Map them to syscall 0. This is slightly less
88 * bogus than using ldebug(sigreturn).
90 #define LINUX_SYS_linux_rt_sendsig 0
91 #define LINUX_SYS_linux_sendsig 0
93 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
95 static int linux_szsigcode;
96 static vm_object_t linux_shared_page_obj;
97 static char *linux_shared_page_mapping;
98 extern char _binary_linux_locore_o_start;
99 extern char _binary_linux_locore_o_end;
101 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
103 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
105 static int linux_fixup(register_t **stack_base,
106 struct image_params *iparams);
107 static int linux_fixup_elf(register_t **stack_base,
108 struct image_params *iparams);
109 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
110 static void linux_exec_setregs(struct thread *td,
111 struct image_params *imgp, u_long stack);
112 static register_t *linux_copyout_strings(struct image_params *imgp);
113 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
114 static void linux_vdso_install(void *param);
115 static void linux_vdso_deinstall(void *param);
117 static int linux_szplatform;
118 const char *linux_kplatform;
120 static eventhandler_tag linux_exit_tag;
121 static eventhandler_tag linux_exec_tag;
122 static eventhandler_tag linux_thread_dtor_tag;
124 #define LINUX_T_UNKNOWN 255
125 static int _bsd_to_linux_trapcode[] = {
126 LINUX_T_UNKNOWN, /* 0 */
127 6, /* 1 T_PRIVINFLT */
128 LINUX_T_UNKNOWN, /* 2 */
130 LINUX_T_UNKNOWN, /* 4 */
131 LINUX_T_UNKNOWN, /* 5 */
132 16, /* 6 T_ARITHTRAP */
133 254, /* 7 T_ASTFLT */
134 LINUX_T_UNKNOWN, /* 8 */
135 13, /* 9 T_PROTFLT */
136 1, /* 10 T_TRCTRAP */
137 LINUX_T_UNKNOWN, /* 11 */
138 14, /* 12 T_PAGEFLT */
139 LINUX_T_UNKNOWN, /* 13 */
140 17, /* 14 T_ALIGNFLT */
141 LINUX_T_UNKNOWN, /* 15 */
142 LINUX_T_UNKNOWN, /* 16 */
143 LINUX_T_UNKNOWN, /* 17 */
149 8, /* 23 T_DOUBLEFLT */
150 9, /* 24 T_FPOPFLT */
151 10, /* 25 T_TSSFLT */
152 11, /* 26 T_SEGNPFLT */
153 12, /* 27 T_STKFLT */
155 19, /* 29 T_XMMFLT */
156 15 /* 30 T_RESERVED */
158 #define bsd_to_linux_trapcode(code) \
159 ((code)<nitems(_bsd_to_linux_trapcode)? \
160 _bsd_to_linux_trapcode[(code)]: \
163 LINUX_VDSO_SYM_INTPTR(linux_sigcode);
164 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
165 LINUX_VDSO_SYM_INTPTR(linux_vsyscall);
168 * If FreeBSD & Linux have a difference of opinion about what a trap
169 * means, deal with it here.
174 linux_translate_traps(int signal, int trap_code)
176 if (signal != SIGBUS)
190 linux_fixup(register_t **stack_base, struct image_params *imgp)
192 register_t *argv, *envp;
195 envp = *stack_base + (imgp->args->argc + 1);
197 suword(*stack_base, (intptr_t)(void *)envp);
199 suword(*stack_base, (intptr_t)(void *)argv);
201 suword(*stack_base, imgp->args->argc);
206 linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
210 Elf32_Auxinfo *argarray, *pos;
211 Elf32_Addr *auxbase, *uplatform;
212 struct ps_strings *arginfo;
213 int error, issetugid;
215 KASSERT(curthread->td_proc == imgp->proc,
216 ("unsafe linux_fixup_elf(), should be curproc"));
219 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
220 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
221 uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform);
222 args = (Elf32_Auxargs *)imgp->auxargs;
223 auxbase = *stack_base + imgp->args->argc + 1 + imgp->args->envc + 1;
224 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
227 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
228 imgp->proc->p_sysent->sv_shared_page_base);
229 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall);
230 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
233 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
234 * as it has appeared in the 2.4.0-rc7 first time.
235 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
236 * glibc falls back to the hard-coded CLK_TCK value when aux entry
238 * Also see linux_times() implementation.
240 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
241 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
242 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
243 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
244 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
245 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
246 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
247 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
248 AUXARGS_ENTRY(pos, AT_BASE, args->base);
249 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
250 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
251 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
252 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
253 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
254 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform));
255 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
256 if (imgp->execpathp != 0)
257 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
258 if (args->execfd != -1)
259 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
260 AUXARGS_ENTRY(pos, AT_NULL, 0);
262 free(imgp->auxargs, M_TEMP);
263 imgp->auxargs = NULL;
264 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
266 error = copyout(argarray, auxbase, sizeof(*argarray) * LINUX_AT_COUNT);
267 free(argarray, M_TEMP);
272 if (suword(*stack_base, (register_t)imgp->args->argc) == -1)
278 * Copied from kern/kern_exec.c
281 linux_copyout_strings(struct image_params *imgp)
285 char *stringp, *destp;
286 register_t *stack_base;
287 struct ps_strings *arginfo;
288 char canary[LINUX_AT_RANDOM_LEN];
292 /* Calculate string base and vector table pointers. */
294 if (imgp->execpath != NULL && imgp->auxargs != NULL)
295 execpath_len = strlen(imgp->execpath) + 1;
298 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
299 destp = (caddr_t)arginfo - SPARE_USRSPACE - linux_szplatform -
300 roundup(sizeof(canary), sizeof(char *)) -
301 roundup(execpath_len, sizeof(char *)) -
302 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
304 /* Install LINUX_PLATFORM. */
305 copyout(linux_kplatform, ((caddr_t)arginfo - linux_szplatform),
308 if (execpath_len != 0) {
309 imgp->execpathp = (uintptr_t)arginfo -
310 linux_szplatform - execpath_len;
311 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
314 /* Prepare the canary for SSP. */
315 arc4rand(canary, sizeof(canary), 0);
316 imgp->canary = (uintptr_t)arginfo - linux_szplatform -
317 roundup(execpath_len, sizeof(char *)) -
318 roundup(sizeof(canary), sizeof(char *));
319 copyout(canary, (void *)imgp->canary, sizeof(canary));
321 vectp = (char **)destp;
324 * Allocate room on the stack for the ELF auxargs
325 * array. It has LINUX_AT_COUNT entries.
327 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
332 * Allocate room for the argv[] and env vectors including the
333 * terminating NULL pointers.
335 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
337 /* vectp also becomes our initial stack base. */
338 stack_base = (register_t *)vectp;
340 stringp = imgp->args->begin_argv;
341 argc = imgp->args->argc;
342 envc = imgp->args->envc;
344 /* Copy out strings - arguments and environment. */
345 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
347 /* Fill in "ps_strings" struct for ps, w, etc. */
348 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
349 suword(&arginfo->ps_nargvstr, argc);
351 /* Fill in argument portion of vector table. */
352 for (; argc > 0; --argc) {
353 suword(vectp++, (long)(intptr_t)destp);
354 while (*stringp++ != 0)
359 /* A null vector table pointer separates the argp's from the envp's. */
362 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
363 suword(&arginfo->ps_nenvstr, envc);
365 /* Fill in environment portion of vector table. */
366 for (; envc > 0; --envc) {
367 suword(vectp++, (long)(intptr_t)destp);
368 while (*stringp++ != 0)
373 /* The end of the vector table is a null pointer. */
380 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
382 struct thread *td = curthread;
383 struct proc *p = td->td_proc;
385 struct trapframe *regs;
386 struct l_rt_sigframe *fp, frame;
390 sig = ksi->ksi_signo;
391 code = ksi->ksi_code;
392 PROC_LOCK_ASSERT(p, MA_OWNED);
394 mtx_assert(&psp->ps_mtx, MA_OWNED);
396 oonstack = sigonstack(regs->tf_esp);
399 if (ldebug(rt_sendsig))
400 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
401 catcher, sig, (void*)mask, code);
403 /* Allocate space for the signal handler context. */
404 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
405 SIGISMEMBER(psp->ps_sigonstack, sig)) {
406 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
407 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
409 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
410 mtx_unlock(&psp->ps_mtx);
412 /* Build the argument list for the signal handler. */
413 sig = bsd_to_linux_signal(sig);
415 bzero(&frame, sizeof(frame));
417 frame.sf_handler = catcher;
419 frame.sf_siginfo = &fp->sf_si;
420 frame.sf_ucontext = &fp->sf_sc;
422 /* Fill in POSIX parts. */
423 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
425 /* Build the signal context to be used by sigreturn. */
426 frame.sf_sc.uc_flags = 0; /* XXX ??? */
427 frame.sf_sc.uc_link = NULL; /* XXX ??? */
429 frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp;
430 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
431 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
432 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
435 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
437 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
438 frame.sf_sc.uc_mcontext.sc_gs = rgs();
439 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
440 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
441 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
442 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi;
443 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi;
444 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp;
445 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx;
446 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_esp;
447 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx;
448 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx;
449 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax;
450 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip;
451 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
452 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
453 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
454 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
455 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
456 frame.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
457 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
460 if (ldebug(rt_sendsig))
461 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
462 frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
463 td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
466 if (copyout(&frame, fp, sizeof(frame)) != 0) {
468 * Process has trashed its stack; give it an illegal
469 * instruction to halt it in its tracks.
472 if (ldebug(rt_sendsig))
473 printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
480 /* Build context to run handler in. */
481 regs->tf_esp = (int)fp;
482 regs->tf_eip = linux_rt_sigcode;
483 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
484 regs->tf_cs = _ucodesel;
485 regs->tf_ds = _udatasel;
486 regs->tf_es = _udatasel;
487 regs->tf_fs = _udatasel;
488 regs->tf_ss = _udatasel;
490 mtx_lock(&psp->ps_mtx);
495 * Send an interrupt to process.
497 * Stack is set up to allow sigcode stored
498 * in u. to call routine, followed by kcall
499 * to sigreturn routine below. After sigreturn
500 * resets the signal mask, the stack, and the
501 * frame pointer, it returns to the user
505 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
507 struct thread *td = curthread;
508 struct proc *p = td->td_proc;
510 struct trapframe *regs;
511 struct l_sigframe *fp, frame;
516 PROC_LOCK_ASSERT(p, MA_OWNED);
518 sig = ksi->ksi_signo;
519 code = ksi->ksi_code;
520 mtx_assert(&psp->ps_mtx, MA_OWNED);
521 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
522 /* Signal handler installed with SA_SIGINFO. */
523 linux_rt_sendsig(catcher, ksi, mask);
527 oonstack = sigonstack(regs->tf_esp);
531 printf(ARGS(sendsig, "%p, %d, %p, %u"),
532 catcher, sig, (void*)mask, code);
535 /* Allocate space for the signal handler context. */
536 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
537 SIGISMEMBER(psp->ps_sigonstack, sig)) {
538 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
539 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
541 fp = (struct l_sigframe *)regs->tf_esp - 1;
542 mtx_unlock(&psp->ps_mtx);
545 /* Build the argument list for the signal handler. */
546 sig = bsd_to_linux_signal(sig);
548 bzero(&frame, sizeof(frame));
550 frame.sf_handler = catcher;
553 bsd_to_linux_sigset(mask, &lmask);
555 /* Build the signal context to be used by sigreturn. */
556 frame.sf_sc.sc_mask = lmask.__mask;
557 frame.sf_sc.sc_gs = rgs();
558 frame.sf_sc.sc_fs = regs->tf_fs;
559 frame.sf_sc.sc_es = regs->tf_es;
560 frame.sf_sc.sc_ds = regs->tf_ds;
561 frame.sf_sc.sc_edi = regs->tf_edi;
562 frame.sf_sc.sc_esi = regs->tf_esi;
563 frame.sf_sc.sc_ebp = regs->tf_ebp;
564 frame.sf_sc.sc_ebx = regs->tf_ebx;
565 frame.sf_sc.sc_esp = regs->tf_esp;
566 frame.sf_sc.sc_edx = regs->tf_edx;
567 frame.sf_sc.sc_ecx = regs->tf_ecx;
568 frame.sf_sc.sc_eax = regs->tf_eax;
569 frame.sf_sc.sc_eip = regs->tf_eip;
570 frame.sf_sc.sc_cs = regs->tf_cs;
571 frame.sf_sc.sc_eflags = regs->tf_eflags;
572 frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
573 frame.sf_sc.sc_ss = regs->tf_ss;
574 frame.sf_sc.sc_err = regs->tf_err;
575 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr;
576 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
578 frame.sf_extramask[0] = lmask.__mask;
580 if (copyout(&frame, fp, sizeof(frame)) != 0) {
582 * Process has trashed its stack; give it an illegal
583 * instruction to halt it in its tracks.
589 /* Build context to run handler in. */
590 regs->tf_esp = (int)fp;
591 regs->tf_eip = linux_sigcode;
592 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
593 regs->tf_cs = _ucodesel;
594 regs->tf_ds = _udatasel;
595 regs->tf_es = _udatasel;
596 regs->tf_fs = _udatasel;
597 regs->tf_ss = _udatasel;
599 mtx_lock(&psp->ps_mtx);
603 * System call to cleanup state after a signal
604 * has been taken. Reset signal mask and
605 * stack state from context left by sendsig (above).
606 * Return to previous pc and psl as specified by
607 * context left by sendsig. Check carefully to
608 * make sure that the user has not modified the
609 * psl to gain improper privileges or to cause
613 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
615 struct l_sigframe frame;
616 struct trapframe *regs;
625 if (ldebug(sigreturn))
626 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
629 * The trampoline code hands us the sigframe.
630 * It is unsafe to keep track of it ourselves, in the event that a
631 * program jumps out of a signal handler.
633 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
636 /* Check for security violations. */
637 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
638 eflags = frame.sf_sc.sc_eflags;
639 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
643 * Don't allow users to load a valid privileged %cs. Let the
644 * hardware check for invalid selectors, excess privilege in
645 * other selectors, invalid %eip's and invalid %esp's.
647 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
648 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
649 ksiginfo_init_trap(&ksi);
650 ksi.ksi_signo = SIGBUS;
651 ksi.ksi_code = BUS_OBJERR;
652 ksi.ksi_trapno = T_PROTFLT;
653 ksi.ksi_addr = (void *)regs->tf_eip;
654 trapsignal(td, &ksi);
658 lmask.__mask = frame.sf_sc.sc_mask;
659 linux_to_bsd_sigset(&lmask, &bmask);
660 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
662 /* Restore signal context. */
663 /* %gs was restored by the trampoline. */
664 regs->tf_fs = frame.sf_sc.sc_fs;
665 regs->tf_es = frame.sf_sc.sc_es;
666 regs->tf_ds = frame.sf_sc.sc_ds;
667 regs->tf_edi = frame.sf_sc.sc_edi;
668 regs->tf_esi = frame.sf_sc.sc_esi;
669 regs->tf_ebp = frame.sf_sc.sc_ebp;
670 regs->tf_ebx = frame.sf_sc.sc_ebx;
671 regs->tf_edx = frame.sf_sc.sc_edx;
672 regs->tf_ecx = frame.sf_sc.sc_ecx;
673 regs->tf_eax = frame.sf_sc.sc_eax;
674 regs->tf_eip = frame.sf_sc.sc_eip;
675 regs->tf_cs = frame.sf_sc.sc_cs;
676 regs->tf_eflags = eflags;
677 regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
678 regs->tf_ss = frame.sf_sc.sc_ss;
680 return (EJUSTRETURN);
684 * System call to cleanup state after a signal
685 * has been taken. Reset signal mask and
686 * stack state from context left by rt_sendsig (above).
687 * Return to previous pc and psl as specified by
688 * context left by sendsig. Check carefully to
689 * make sure that the user has not modified the
690 * psl to gain improper privileges or to cause
694 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
696 struct l_ucontext uc;
697 struct l_sigcontext *context;
701 struct trapframe *regs;
708 if (ldebug(rt_sigreturn))
709 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
712 * The trampoline code hands us the ucontext.
713 * It is unsafe to keep track of it ourselves, in the event that a
714 * program jumps out of a signal handler.
716 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
719 context = &uc.uc_mcontext;
721 /* Check for security violations. */
722 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
723 eflags = context->sc_eflags;
724 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
728 * Don't allow users to load a valid privileged %cs. Let the
729 * hardware check for invalid selectors, excess privilege in
730 * other selectors, invalid %eip's and invalid %esp's.
732 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
733 if (!CS_SECURE(context->sc_cs)) {
734 ksiginfo_init_trap(&ksi);
735 ksi.ksi_signo = SIGBUS;
736 ksi.ksi_code = BUS_OBJERR;
737 ksi.ksi_trapno = T_PROTFLT;
738 ksi.ksi_addr = (void *)regs->tf_eip;
739 trapsignal(td, &ksi);
743 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
744 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
746 /* Restore signal context. */
747 /* %gs was restored by the trampoline. */
748 regs->tf_fs = context->sc_fs;
749 regs->tf_es = context->sc_es;
750 regs->tf_ds = context->sc_ds;
751 regs->tf_edi = context->sc_edi;
752 regs->tf_esi = context->sc_esi;
753 regs->tf_ebp = context->sc_ebp;
754 regs->tf_ebx = context->sc_ebx;
755 regs->tf_edx = context->sc_edx;
756 regs->tf_ecx = context->sc_ecx;
757 regs->tf_eax = context->sc_eax;
758 regs->tf_eip = context->sc_eip;
759 regs->tf_cs = context->sc_cs;
760 regs->tf_eflags = eflags;
761 regs->tf_esp = context->sc_esp_at_signal;
762 regs->tf_ss = context->sc_ss;
764 /* Call sigaltstack & ignore results. */
766 ss.ss_sp = lss->ss_sp;
767 ss.ss_size = lss->ss_size;
768 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
771 if (ldebug(rt_sigreturn))
772 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
773 ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
775 (void)kern_sigaltstack(td, &ss, NULL);
777 return (EJUSTRETURN);
781 linux_fetch_syscall_args(struct thread *td)
784 struct trapframe *frame;
785 struct syscall_args *sa;
788 frame = td->td_frame;
791 sa->code = frame->tf_eax;
792 sa->args[0] = frame->tf_ebx;
793 sa->args[1] = frame->tf_ecx;
794 sa->args[2] = frame->tf_edx;
795 sa->args[3] = frame->tf_esi;
796 sa->args[4] = frame->tf_edi;
797 sa->args[5] = frame->tf_ebp; /* Unconfirmed */
799 if (sa->code >= p->p_sysent->sv_size)
801 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
803 sa->callp = &p->p_sysent->sv_table[sa->code];
804 sa->narg = sa->callp->sy_narg;
806 td->td_retval[0] = 0;
807 td->td_retval[1] = frame->tf_edx;
813 * exec_setregs may initialize some registers differently than Linux
814 * does, thus potentially confusing Linux binaries. If necessary, we
815 * override the exec_setregs default(s) here.
818 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
820 struct pcb *pcb = td->td_pcb;
822 exec_setregs(td, imgp, stack);
824 /* Linux sets %gs to 0, we default to _udatasel. */
828 pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
832 linux_get_machine(const char **dst)
850 struct sysentvec linux_sysvec = {
851 .sv_size = LINUX_SYS_MAXSYSCALL,
852 .sv_table = linux_sysent,
854 .sv_errsize = ELAST + 1,
855 .sv_errtbl = linux_errtbl,
856 .sv_transtrap = linux_translate_traps,
857 .sv_fixup = linux_fixup,
858 .sv_sendsig = linux_sendsig,
859 .sv_sigcode = &_binary_linux_locore_o_start,
860 .sv_szsigcode = &linux_szsigcode,
861 .sv_name = "Linux a.out",
863 .sv_imgact_try = linux_exec_imgact_try,
864 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
865 .sv_pagesize = PAGE_SIZE,
866 .sv_minuser = VM_MIN_ADDRESS,
867 .sv_maxuser = VM_MAXUSER_ADDRESS,
868 .sv_usrstack = LINUX_USRSTACK,
869 .sv_psstrings = PS_STRINGS,
870 .sv_stackprot = VM_PROT_ALL,
871 .sv_copyout_strings = exec_copyout_strings,
872 .sv_setregs = linux_exec_setregs,
875 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32,
876 .sv_set_syscall_retval = cpu_set_syscall_retval,
877 .sv_fetch_syscall_args = linux_fetch_syscall_args,
878 .sv_syscallnames = NULL,
879 .sv_shared_page_base = LINUX_SHAREDPAGE,
880 .sv_shared_page_len = PAGE_SIZE,
881 .sv_schedtail = linux_schedtail,
882 .sv_thread_detach = linux_thread_detach,
885 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
887 struct sysentvec elf_linux_sysvec = {
888 .sv_size = LINUX_SYS_MAXSYSCALL,
889 .sv_table = linux_sysent,
891 .sv_errsize = ELAST + 1,
892 .sv_errtbl = linux_errtbl,
893 .sv_transtrap = linux_translate_traps,
894 .sv_fixup = linux_fixup_elf,
895 .sv_sendsig = linux_sendsig,
896 .sv_sigcode = &_binary_linux_locore_o_start,
897 .sv_szsigcode = &linux_szsigcode,
898 .sv_name = "Linux ELF",
899 .sv_coredump = elf32_coredump,
900 .sv_imgact_try = linux_exec_imgact_try,
901 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
902 .sv_pagesize = PAGE_SIZE,
903 .sv_minuser = VM_MIN_ADDRESS,
904 .sv_maxuser = VM_MAXUSER_ADDRESS,
905 .sv_usrstack = LINUX_USRSTACK,
906 .sv_psstrings = LINUX_PS_STRINGS,
907 .sv_stackprot = VM_PROT_ALL,
908 .sv_copyout_strings = linux_copyout_strings,
909 .sv_setregs = linux_exec_setregs,
912 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP,
913 .sv_set_syscall_retval = cpu_set_syscall_retval,
914 .sv_fetch_syscall_args = linux_fetch_syscall_args,
915 .sv_syscallnames = NULL,
916 .sv_shared_page_base = LINUX_SHAREDPAGE,
917 .sv_shared_page_len = PAGE_SIZE,
918 .sv_schedtail = linux_schedtail,
919 .sv_thread_detach = linux_thread_detach,
924 linux_vdso_install(void *param)
927 linux_szsigcode = (&_binary_linux_locore_o_end -
928 &_binary_linux_locore_o_start);
930 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
931 panic("Linux invalid vdso size\n");
933 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
935 linux_shared_page_obj = __elfN(linux_shared_page_init)
936 (&linux_shared_page_mapping);
938 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
940 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
942 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
944 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
945 linux_vdso_install, NULL);
948 linux_vdso_deinstall(void *param)
951 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
953 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
954 linux_vdso_deinstall, NULL);
956 static char GNU_ABI_VENDOR[] = "GNU";
957 static int GNULINUX_ABI_DESC = 0;
960 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
962 const Elf32_Word *desc;
965 p = (uintptr_t)(note + 1);
966 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
968 desc = (const Elf32_Word *)p;
969 if (desc[0] != GNULINUX_ABI_DESC)
973 * For Linux we encode osrel using the Linux convention of
974 * (version << 16) | (major << 8) | (minor)
975 * See macro in linux_mib.h
977 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
982 static Elf_Brandnote linux_brandnote = {
983 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
984 .hdr.n_descsz = 16, /* XXX at least 16 */
986 .vendor = GNU_ABI_VENDOR,
987 .flags = BN_TRANSLATE_OSREL,
988 .trans_osrel = linux_trans_osrel
991 static Elf32_Brandinfo linux_brand = {
992 .brand = ELFOSABI_LINUX,
994 .compat_3_brand = "Linux",
995 .emul_path = "/compat/linux",
996 .interp_path = "/lib/ld-linux.so.1",
997 .sysvec = &elf_linux_sysvec,
998 .interp_newpath = NULL,
999 .brand_note = &linux_brandnote,
1000 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1003 static Elf32_Brandinfo linux_glibc2brand = {
1004 .brand = ELFOSABI_LINUX,
1006 .compat_3_brand = "Linux",
1007 .emul_path = "/compat/linux",
1008 .interp_path = "/lib/ld-linux.so.2",
1009 .sysvec = &elf_linux_sysvec,
1010 .interp_newpath = NULL,
1011 .brand_note = &linux_brandnote,
1012 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1015 static Elf32_Brandinfo linux_muslbrand = {
1016 .brand = ELFOSABI_LINUX,
1018 .compat_3_brand = "Linux",
1019 .emul_path = "/compat/linux",
1020 .interp_path = "/lib/ld-musl-i386.so.1",
1021 .sysvec = &elf_linux_sysvec,
1022 .interp_newpath = NULL,
1023 .brand_note = &linux_brandnote,
1024 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1027 Elf32_Brandinfo *linux_brandlist[] = {
1035 linux_elf_modevent(module_t mod, int type, void *data)
1037 Elf32_Brandinfo **brandinfo;
1039 struct linux_ioctl_handler **lihp;
1045 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1047 if (elf32_insert_brand_entry(*brandinfo) < 0)
1050 SET_FOREACH(lihp, linux_ioctl_handler_set)
1051 linux_ioctl_register_handler(*lihp);
1052 LIST_INIT(&futex_list);
1053 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1054 linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit,
1056 linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec,
1058 linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
1059 linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
1060 linux_get_machine(&linux_kplatform);
1061 linux_szplatform = roundup(strlen(linux_kplatform) + 1,
1063 linux_osd_jail_register();
1064 stclohz = (stathz ? stathz : hz);
1066 printf("Linux ELF exec handler installed\n");
1068 printf("cannot insert Linux ELF brand handler\n");
1071 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1073 if (elf32_brand_inuse(*brandinfo))
1076 for (brandinfo = &linux_brandlist[0];
1077 *brandinfo != NULL; ++brandinfo)
1078 if (elf32_remove_brand_entry(*brandinfo) < 0)
1082 SET_FOREACH(lihp, linux_ioctl_handler_set)
1083 linux_ioctl_unregister_handler(*lihp);
1084 mtx_destroy(&futex_mtx);
1085 EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag);
1086 EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag);
1087 EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag);
1088 linux_osd_jail_deregister();
1090 printf("Linux ELF exec handler removed\n");
1092 printf("Could not deinstall ELF interpreter entry\n");
1095 return (EOPNOTSUPP);
1100 static moduledata_t linux_elf_mod = {
1106 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1107 FEATURE(linux, "Linux 32bit support");