2 * Copyright (c) 1994-1996 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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>
66 #include <i386/linux/linux.h>
67 #include <i386/linux/linux_proto.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_futex.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 #if BYTE_ORDER == LITTLE_ENDIAN
80 #define SHELLMAGIC 0x2123 /* #! */
82 #define SHELLMAGIC 0x2321
86 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
87 linux_sysctl_debug, "A", "Linux debugging control");
91 * Allow the sendsig functions to use the ldebug() facility even though they
92 * are not syscalls themselves. Map them to syscall 0. This is slightly less
93 * bogus than using ldebug(sigreturn).
95 #define LINUX_SYS_linux_rt_sendsig 0
96 #define LINUX_SYS_linux_sendsig 0
98 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
100 static int linux_szsigcode;
101 static vm_object_t linux_shared_page_obj;
102 static char *linux_shared_page_mapping;
103 extern char _binary_linux_locore_o_start;
104 extern char _binary_linux_locore_o_end;
106 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
108 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
110 static int linux_fixup(register_t **stack_base,
111 struct image_params *iparams);
112 static int linux_fixup_elf(register_t **stack_base,
113 struct image_params *iparams);
114 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
115 static int linux_exec_imgact_try(struct image_params *iparams);
116 static void linux_exec_setregs(struct thread *td,
117 struct image_params *imgp, u_long stack);
118 static register_t *linux_copyout_strings(struct image_params *imgp);
119 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
120 static void linux_vdso_install(void *param);
121 static void linux_vdso_deinstall(void *param);
123 static int linux_szplatform;
124 const char *linux_kplatform;
126 static eventhandler_tag linux_exit_tag;
127 static eventhandler_tag linux_exec_tag;
128 static eventhandler_tag linux_thread_dtor_tag;
130 #define LINUX_T_UNKNOWN 255
131 static int _bsd_to_linux_trapcode[] = {
132 LINUX_T_UNKNOWN, /* 0 */
133 6, /* 1 T_PRIVINFLT */
134 LINUX_T_UNKNOWN, /* 2 */
136 LINUX_T_UNKNOWN, /* 4 */
137 LINUX_T_UNKNOWN, /* 5 */
138 16, /* 6 T_ARITHTRAP */
139 254, /* 7 T_ASTFLT */
140 LINUX_T_UNKNOWN, /* 8 */
141 13, /* 9 T_PROTFLT */
142 1, /* 10 T_TRCTRAP */
143 LINUX_T_UNKNOWN, /* 11 */
144 14, /* 12 T_PAGEFLT */
145 LINUX_T_UNKNOWN, /* 13 */
146 17, /* 14 T_ALIGNFLT */
147 LINUX_T_UNKNOWN, /* 15 */
148 LINUX_T_UNKNOWN, /* 16 */
149 LINUX_T_UNKNOWN, /* 17 */
155 8, /* 23 T_DOUBLEFLT */
156 9, /* 24 T_FPOPFLT */
157 10, /* 25 T_TSSFLT */
158 11, /* 26 T_SEGNPFLT */
159 12, /* 27 T_STKFLT */
161 19, /* 29 T_XMMFLT */
162 15 /* 30 T_RESERVED */
164 #define bsd_to_linux_trapcode(code) \
165 ((code)<nitems(_bsd_to_linux_trapcode)? \
166 _bsd_to_linux_trapcode[(code)]: \
169 LINUX_VDSO_SYM_INTPTR(linux_sigcode);
170 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
171 LINUX_VDSO_SYM_INTPTR(linux_vsyscall);
174 * If FreeBSD & Linux have a difference of opinion about what a trap
175 * means, deal with it here.
180 linux_translate_traps(int signal, int trap_code)
182 if (signal != SIGBUS)
196 linux_fixup(register_t **stack_base, struct image_params *imgp)
198 register_t *argv, *envp;
201 envp = *stack_base + (imgp->args->argc + 1);
203 suword(*stack_base, (intptr_t)(void *)envp);
205 suword(*stack_base, (intptr_t)(void *)argv);
207 suword(*stack_base, imgp->args->argc);
212 linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
216 Elf32_Addr *uplatform;
217 struct ps_strings *arginfo;
221 KASSERT(curthread->td_proc == imgp->proc,
222 ("unsafe linux_fixup_elf(), should be curproc"));
225 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
226 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
227 uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform);
228 args = (Elf32_Auxargs *)imgp->auxargs;
229 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
231 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
232 imgp->proc->p_sysent->sv_shared_page_base);
233 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall);
234 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
237 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
238 * as it has appeared in the 2.4.0-rc7 first time.
239 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
240 * glibc falls back to the hard-coded CLK_TCK value when aux entry
242 * Also see linux_times() implementation.
244 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
245 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
246 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
247 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
248 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
249 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
250 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
251 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
252 AUXARGS_ENTRY(pos, AT_BASE, args->base);
253 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
254 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
255 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
256 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
257 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
258 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform));
259 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
260 if (imgp->execpathp != 0)
261 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
262 if (args->execfd != -1)
263 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
264 AUXARGS_ENTRY(pos, AT_NULL, 0);
266 free(imgp->auxargs, M_TEMP);
267 imgp->auxargs = NULL;
270 suword(*stack_base, (register_t)imgp->args->argc);
275 * Copied from kern/kern_exec.c
278 linux_copyout_strings(struct image_params *imgp)
282 char *stringp, *destp;
283 register_t *stack_base;
284 struct ps_strings *arginfo;
285 char canary[LINUX_AT_RANDOM_LEN];
289 /* Calculate string base and vector table pointers. */
291 if (imgp->execpath != NULL && imgp->auxargs != NULL)
292 execpath_len = strlen(imgp->execpath) + 1;
295 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
296 destp = (caddr_t)arginfo - SPARE_USRSPACE - linux_szplatform -
297 roundup(sizeof(canary), sizeof(char *)) -
298 roundup(execpath_len, sizeof(char *)) -
299 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
301 /* Install LINUX_PLATFORM. */
302 copyout(linux_kplatform, ((caddr_t)arginfo - linux_szplatform),
305 if (execpath_len != 0) {
306 imgp->execpathp = (uintptr_t)arginfo -
307 linux_szplatform - execpath_len;
308 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
311 /* Prepare the canary for SSP. */
312 arc4rand(canary, sizeof(canary), 0);
313 imgp->canary = (uintptr_t)arginfo - linux_szplatform -
314 roundup(execpath_len, sizeof(char *)) -
315 roundup(sizeof(canary), sizeof(char *));
316 copyout(canary, (void *)imgp->canary, sizeof(canary));
318 vectp = (char **)destp;
321 * Allocate room on the stack for the ELF auxargs
322 * array. It has LINUX_AT_COUNT entries.
324 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
329 * Allocate room for the argv[] and env vectors including the
330 * terminating NULL pointers.
332 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
334 /* vectp also becomes our initial stack base. */
335 stack_base = (register_t *)vectp;
337 stringp = imgp->args->begin_argv;
338 argc = imgp->args->argc;
339 envc = imgp->args->envc;
341 /* Copy out strings - arguments and environment. */
342 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
344 /* Fill in "ps_strings" struct for ps, w, etc. */
345 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
346 suword(&arginfo->ps_nargvstr, argc);
348 /* Fill in argument portion of vector table. */
349 for (; argc > 0; --argc) {
350 suword(vectp++, (long)(intptr_t)destp);
351 while (*stringp++ != 0)
356 /* A null vector table pointer separates the argp's from the envp's. */
359 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
360 suword(&arginfo->ps_nenvstr, envc);
362 /* Fill in environment portion of vector table. */
363 for (; envc > 0; --envc) {
364 suword(vectp++, (long)(intptr_t)destp);
365 while (*stringp++ != 0)
370 /* The end of the vector table is a null pointer. */
377 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
379 struct thread *td = curthread;
380 struct proc *p = td->td_proc;
382 struct trapframe *regs;
383 struct l_rt_sigframe *fp, frame;
387 sig = ksi->ksi_signo;
388 code = ksi->ksi_code;
389 PROC_LOCK_ASSERT(p, MA_OWNED);
391 mtx_assert(&psp->ps_mtx, MA_OWNED);
393 oonstack = sigonstack(regs->tf_esp);
396 if (ldebug(rt_sendsig))
397 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
398 catcher, sig, (void*)mask, code);
400 /* Allocate space for the signal handler context. */
401 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
402 SIGISMEMBER(psp->ps_sigonstack, sig)) {
403 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
404 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
406 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
407 mtx_unlock(&psp->ps_mtx);
409 /* Build the argument list for the signal handler. */
410 sig = bsd_to_linux_signal(sig);
412 bzero(&frame, sizeof(frame));
414 frame.sf_handler = catcher;
416 frame.sf_siginfo = &fp->sf_si;
417 frame.sf_ucontext = &fp->sf_sc;
419 /* Fill in POSIX parts. */
420 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
422 /* Build the signal context to be used by sigreturn. */
423 frame.sf_sc.uc_flags = 0; /* XXX ??? */
424 frame.sf_sc.uc_link = NULL; /* XXX ??? */
426 frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp;
427 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
428 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
429 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
432 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
434 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
435 frame.sf_sc.uc_mcontext.sc_gs = rgs();
436 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
437 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
438 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
439 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi;
440 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi;
441 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp;
442 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx;
443 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_esp;
444 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx;
445 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx;
446 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax;
447 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip;
448 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
449 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
450 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
451 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
452 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
453 frame.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
454 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
457 if (ldebug(rt_sendsig))
458 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
459 frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
460 td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
463 if (copyout(&frame, fp, sizeof(frame)) != 0) {
465 * Process has trashed its stack; give it an illegal
466 * instruction to halt it in its tracks.
469 if (ldebug(rt_sendsig))
470 printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
477 /* Build context to run handler in. */
478 regs->tf_esp = (int)fp;
479 regs->tf_eip = linux_rt_sigcode;
480 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
481 regs->tf_cs = _ucodesel;
482 regs->tf_ds = _udatasel;
483 regs->tf_es = _udatasel;
484 regs->tf_fs = _udatasel;
485 regs->tf_ss = _udatasel;
487 mtx_lock(&psp->ps_mtx);
492 * Send an interrupt to process.
494 * Stack is set up to allow sigcode stored
495 * in u. to call routine, followed by kcall
496 * to sigreturn routine below. After sigreturn
497 * resets the signal mask, the stack, and the
498 * frame pointer, it returns to the user
502 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
504 struct thread *td = curthread;
505 struct proc *p = td->td_proc;
507 struct trapframe *regs;
508 struct l_sigframe *fp, frame;
513 PROC_LOCK_ASSERT(p, MA_OWNED);
515 sig = ksi->ksi_signo;
516 code = ksi->ksi_code;
517 mtx_assert(&psp->ps_mtx, MA_OWNED);
518 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
519 /* Signal handler installed with SA_SIGINFO. */
520 linux_rt_sendsig(catcher, ksi, mask);
524 oonstack = sigonstack(regs->tf_esp);
528 printf(ARGS(sendsig, "%p, %d, %p, %u"),
529 catcher, sig, (void*)mask, code);
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 if (ldebug(sigreturn))
623 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
626 * The trampoline code hands us the sigframe.
627 * It is unsafe to keep track of it ourselves, in the event that a
628 * program jumps out of a signal handler.
630 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
633 /* Check for security violations. */
634 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
635 eflags = frame.sf_sc.sc_eflags;
636 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
640 * Don't allow users to load a valid privileged %cs. Let the
641 * hardware check for invalid selectors, excess privilege in
642 * other selectors, invalid %eip's and invalid %esp's.
644 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
645 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
646 ksiginfo_init_trap(&ksi);
647 ksi.ksi_signo = SIGBUS;
648 ksi.ksi_code = BUS_OBJERR;
649 ksi.ksi_trapno = T_PROTFLT;
650 ksi.ksi_addr = (void *)regs->tf_eip;
651 trapsignal(td, &ksi);
655 lmask.__mask = frame.sf_sc.sc_mask;
656 linux_to_bsd_sigset(&lmask, &bmask);
657 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
659 /* Restore signal context. */
660 /* %gs was restored by the trampoline. */
661 regs->tf_fs = frame.sf_sc.sc_fs;
662 regs->tf_es = frame.sf_sc.sc_es;
663 regs->tf_ds = frame.sf_sc.sc_ds;
664 regs->tf_edi = frame.sf_sc.sc_edi;
665 regs->tf_esi = frame.sf_sc.sc_esi;
666 regs->tf_ebp = frame.sf_sc.sc_ebp;
667 regs->tf_ebx = frame.sf_sc.sc_ebx;
668 regs->tf_edx = frame.sf_sc.sc_edx;
669 regs->tf_ecx = frame.sf_sc.sc_ecx;
670 regs->tf_eax = frame.sf_sc.sc_eax;
671 regs->tf_eip = frame.sf_sc.sc_eip;
672 regs->tf_cs = frame.sf_sc.sc_cs;
673 regs->tf_eflags = eflags;
674 regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
675 regs->tf_ss = frame.sf_sc.sc_ss;
677 return (EJUSTRETURN);
681 * System call to cleanup state after a signal
682 * has been taken. Reset signal mask and
683 * stack state from context left by rt_sendsig (above).
684 * Return to previous pc and psl as specified by
685 * context left by sendsig. Check carefully to
686 * make sure that the user has not modified the
687 * psl to gain improper privileges or to cause
691 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
693 struct l_ucontext uc;
694 struct l_sigcontext *context;
698 struct trapframe *regs;
705 if (ldebug(rt_sigreturn))
706 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
709 * The trampoline code hands us the ucontext.
710 * It is unsafe to keep track of it ourselves, in the event that a
711 * program jumps out of a signal handler.
713 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
716 context = &uc.uc_mcontext;
718 /* Check for security violations. */
719 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
720 eflags = context->sc_eflags;
721 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
725 * Don't allow users to load a valid privileged %cs. Let the
726 * hardware check for invalid selectors, excess privilege in
727 * other selectors, invalid %eip's and invalid %esp's.
729 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
730 if (!CS_SECURE(context->sc_cs)) {
731 ksiginfo_init_trap(&ksi);
732 ksi.ksi_signo = SIGBUS;
733 ksi.ksi_code = BUS_OBJERR;
734 ksi.ksi_trapno = T_PROTFLT;
735 ksi.ksi_addr = (void *)regs->tf_eip;
736 trapsignal(td, &ksi);
740 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
741 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
743 /* Restore signal context. */
744 /* %gs was restored by the trampoline. */
745 regs->tf_fs = context->sc_fs;
746 regs->tf_es = context->sc_es;
747 regs->tf_ds = context->sc_ds;
748 regs->tf_edi = context->sc_edi;
749 regs->tf_esi = context->sc_esi;
750 regs->tf_ebp = context->sc_ebp;
751 regs->tf_ebx = context->sc_ebx;
752 regs->tf_edx = context->sc_edx;
753 regs->tf_ecx = context->sc_ecx;
754 regs->tf_eax = context->sc_eax;
755 regs->tf_eip = context->sc_eip;
756 regs->tf_cs = context->sc_cs;
757 regs->tf_eflags = eflags;
758 regs->tf_esp = context->sc_esp_at_signal;
759 regs->tf_ss = context->sc_ss;
761 /* Call sigaltstack & ignore results. */
763 ss.ss_sp = lss->ss_sp;
764 ss.ss_size = lss->ss_size;
765 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
768 if (ldebug(rt_sigreturn))
769 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
770 ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
772 (void)kern_sigaltstack(td, &ss, NULL);
774 return (EJUSTRETURN);
778 linux_fetch_syscall_args(struct thread *td)
781 struct trapframe *frame;
782 struct syscall_args *sa;
785 frame = td->td_frame;
788 sa->code = frame->tf_eax;
789 sa->args[0] = frame->tf_ebx;
790 sa->args[1] = frame->tf_ecx;
791 sa->args[2] = frame->tf_edx;
792 sa->args[3] = frame->tf_esi;
793 sa->args[4] = frame->tf_edi;
794 sa->args[5] = frame->tf_ebp; /* Unconfirmed */
796 if (sa->code >= p->p_sysent->sv_size)
798 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
800 sa->callp = &p->p_sysent->sv_table[sa->code];
801 sa->narg = sa->callp->sy_narg;
803 td->td_retval[0] = 0;
804 td->td_retval[1] = frame->tf_edx;
810 * If a Linux binary is exec'ing something, try this image activator
811 * first. We override standard shell script execution in order to
812 * be able to modify the interpreter path. We only do this if a Linux
813 * binary is doing the exec, so we do not create an EXEC module for it.
816 linux_exec_imgact_try(struct image_params *imgp)
818 const char *head = (const char *)imgp->image_header;
823 * The interpreter for shell scripts run from a Linux binary needs
824 * to be located in /compat/linux if possible in order to recursively
825 * maintain Linux path emulation.
827 if (((const short *)head)[0] == SHELLMAGIC) {
829 * Run our normal shell image activator. If it succeeds then
830 * attempt to use the alternate path for the interpreter. If
831 * an alternate path is found, use our stringspace to store it.
833 if ((error = exec_shell_imgact(imgp)) == 0) {
834 linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
835 imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
838 imgp->args->fname_buf =
839 imgp->interpreter_name = rpath;
846 * exec_setregs may initialize some registers differently than Linux
847 * does, thus potentially confusing Linux binaries. If necessary, we
848 * override the exec_setregs default(s) here.
851 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
853 struct pcb *pcb = td->td_pcb;
855 exec_setregs(td, imgp, stack);
857 /* Linux sets %gs to 0, we default to _udatasel. */
861 pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
865 linux_get_machine(const char **dst)
883 struct sysentvec linux_sysvec = {
884 .sv_size = LINUX_SYS_MAXSYSCALL,
885 .sv_table = linux_sysent,
887 .sv_errsize = ELAST + 1,
888 .sv_errtbl = linux_errtbl,
889 .sv_transtrap = linux_translate_traps,
890 .sv_fixup = linux_fixup,
891 .sv_sendsig = linux_sendsig,
892 .sv_sigcode = &_binary_linux_locore_o_start,
893 .sv_szsigcode = &linux_szsigcode,
894 .sv_name = "Linux a.out",
896 .sv_imgact_try = linux_exec_imgact_try,
897 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
898 .sv_pagesize = PAGE_SIZE,
899 .sv_minuser = VM_MIN_ADDRESS,
900 .sv_maxuser = VM_MAXUSER_ADDRESS,
901 .sv_usrstack = LINUX_USRSTACK,
902 .sv_psstrings = PS_STRINGS,
903 .sv_stackprot = VM_PROT_ALL,
904 .sv_copyout_strings = exec_copyout_strings,
905 .sv_setregs = linux_exec_setregs,
908 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32,
909 .sv_set_syscall_retval = cpu_set_syscall_retval,
910 .sv_fetch_syscall_args = linux_fetch_syscall_args,
911 .sv_syscallnames = NULL,
912 .sv_shared_page_base = LINUX_SHAREDPAGE,
913 .sv_shared_page_len = PAGE_SIZE,
914 .sv_schedtail = linux_schedtail,
915 .sv_thread_detach = linux_thread_detach,
918 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
920 struct sysentvec elf_linux_sysvec = {
921 .sv_size = LINUX_SYS_MAXSYSCALL,
922 .sv_table = linux_sysent,
924 .sv_errsize = ELAST + 1,
925 .sv_errtbl = linux_errtbl,
926 .sv_transtrap = linux_translate_traps,
927 .sv_fixup = linux_fixup_elf,
928 .sv_sendsig = linux_sendsig,
929 .sv_sigcode = &_binary_linux_locore_o_start,
930 .sv_szsigcode = &linux_szsigcode,
931 .sv_name = "Linux ELF",
932 .sv_coredump = elf32_coredump,
933 .sv_imgact_try = linux_exec_imgact_try,
934 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
935 .sv_pagesize = PAGE_SIZE,
936 .sv_minuser = VM_MIN_ADDRESS,
937 .sv_maxuser = VM_MAXUSER_ADDRESS,
938 .sv_usrstack = LINUX_USRSTACK,
939 .sv_psstrings = LINUX_PS_STRINGS,
940 .sv_stackprot = VM_PROT_ALL,
941 .sv_copyout_strings = linux_copyout_strings,
942 .sv_setregs = linux_exec_setregs,
945 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP,
946 .sv_set_syscall_retval = cpu_set_syscall_retval,
947 .sv_fetch_syscall_args = linux_fetch_syscall_args,
948 .sv_syscallnames = NULL,
949 .sv_shared_page_base = LINUX_SHAREDPAGE,
950 .sv_shared_page_len = PAGE_SIZE,
951 .sv_schedtail = linux_schedtail,
952 .sv_thread_detach = linux_thread_detach,
957 linux_vdso_install(void *param)
960 linux_szsigcode = (&_binary_linux_locore_o_end -
961 &_binary_linux_locore_o_start);
963 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
964 panic("Linux invalid vdso size\n");
966 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
968 linux_shared_page_obj = __elfN(linux_shared_page_init)
969 (&linux_shared_page_mapping);
971 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
973 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
975 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
977 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
978 (sysinit_cfunc_t)linux_vdso_install, NULL);
981 linux_vdso_deinstall(void *param)
984 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
986 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
987 (sysinit_cfunc_t)linux_vdso_deinstall, NULL);
989 static char GNU_ABI_VENDOR[] = "GNU";
990 static int GNULINUX_ABI_DESC = 0;
993 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
995 const Elf32_Word *desc;
998 p = (uintptr_t)(note + 1);
999 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1001 desc = (const Elf32_Word *)p;
1002 if (desc[0] != GNULINUX_ABI_DESC)
1006 * For Linux we encode osrel as follows (see linux_mib.c):
1007 * VVVMMMIII (version, major, minor), see linux_mib.c.
1009 *osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
1014 static Elf_Brandnote linux_brandnote = {
1015 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
1016 .hdr.n_descsz = 16, /* XXX at least 16 */
1018 .vendor = GNU_ABI_VENDOR,
1019 .flags = BN_TRANSLATE_OSREL,
1020 .trans_osrel = linux_trans_osrel
1023 static Elf32_Brandinfo linux_brand = {
1024 .brand = ELFOSABI_LINUX,
1026 .compat_3_brand = "Linux",
1027 .emul_path = "/compat/linux",
1028 .interp_path = "/lib/ld-linux.so.1",
1029 .sysvec = &elf_linux_sysvec,
1030 .interp_newpath = NULL,
1031 .brand_note = &linux_brandnote,
1032 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1035 static Elf32_Brandinfo linux_glibc2brand = {
1036 .brand = ELFOSABI_LINUX,
1038 .compat_3_brand = "Linux",
1039 .emul_path = "/compat/linux",
1040 .interp_path = "/lib/ld-linux.so.2",
1041 .sysvec = &elf_linux_sysvec,
1042 .interp_newpath = NULL,
1043 .brand_note = &linux_brandnote,
1044 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1047 static Elf32_Brandinfo linux_muslbrand = {
1048 .brand = ELFOSABI_LINUX,
1050 .compat_3_brand = "Linux",
1051 .emul_path = "/compat/linux",
1052 .interp_path = "/lib/ld-musl-i386.so.1",
1053 .sysvec = &elf_linux_sysvec,
1054 .interp_newpath = NULL,
1055 .brand_note = &linux_brandnote,
1056 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1059 Elf32_Brandinfo *linux_brandlist[] = {
1067 linux_elf_modevent(module_t mod, int type, void *data)
1069 Elf32_Brandinfo **brandinfo;
1071 struct linux_ioctl_handler **lihp;
1077 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1079 if (elf32_insert_brand_entry(*brandinfo) < 0)
1082 SET_FOREACH(lihp, linux_ioctl_handler_set)
1083 linux_ioctl_register_handler(*lihp);
1084 LIST_INIT(&futex_list);
1085 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1086 linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit,
1088 linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec,
1090 linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
1091 linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
1092 linux_get_machine(&linux_kplatform);
1093 linux_szplatform = roundup(strlen(linux_kplatform) + 1,
1095 linux_osd_jail_register();
1096 stclohz = (stathz ? stathz : hz);
1098 printf("Linux ELF exec handler installed\n");
1100 printf("cannot insert Linux ELF brand handler\n");
1103 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1105 if (elf32_brand_inuse(*brandinfo))
1108 for (brandinfo = &linux_brandlist[0];
1109 *brandinfo != NULL; ++brandinfo)
1110 if (elf32_remove_brand_entry(*brandinfo) < 0)
1114 SET_FOREACH(lihp, linux_ioctl_handler_set)
1115 linux_ioctl_unregister_handler(*lihp);
1116 mtx_destroy(&futex_mtx);
1117 EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag);
1118 EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag);
1119 EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag);
1120 linux_osd_jail_deregister();
1122 printf("Linux ELF exec handler removed\n");
1124 printf("Could not deinstall ELF interpreter entry\n");
1127 return (EOPNOTSUPP);
1132 static moduledata_t linux_elf_mod = {
1138 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1139 FEATURE(linux, "Linux 32bit support");