2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2004 Tim J. Robbins
5 * Copyright (c) 2003 Peter Wemm
6 * Copyright (c) 2002 Doug Rabson
7 * Copyright (c) 1998-1999 Andrew Gallatin
8 * Copyright (c) 1994-1996 Søren Schmidt
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer
16 * in this position and unchanged.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #ifndef COMPAT_FREEBSD32
39 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
42 #define __ELF_WORD_SIZE 32
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/fcntl.h>
48 #include <sys/imgact.h>
49 #include <sys/imgact_elf.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/sysproto.h>
62 #include <sys/vnode.h>
63 #include <sys/eventhandler.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_param.h>
73 #include <machine/cpu.h>
74 #include <machine/md_var.h>
75 #include <machine/pcb.h>
76 #include <machine/specialreg.h>
77 #include <machine/trap.h>
79 #include <amd64/linux32/linux.h>
80 #include <amd64/linux32/linux32_proto.h>
81 #include <compat/linux/linux_emul.h>
82 #include <compat/linux/linux_futex.h>
83 #include <compat/linux/linux_ioctl.h>
84 #include <compat/linux/linux_mib.h>
85 #include <compat/linux/linux_misc.h>
86 #include <compat/linux/linux_signal.h>
87 #include <compat/linux/linux_util.h>
88 #include <compat/linux/linux_vdso.h>
90 MODULE_VERSION(linux, 1);
92 #define AUXARGS_ENTRY_32(pos, id, val) \
94 suword32(pos++, id); \
95 suword32(pos++, val); \
99 * Allow the sendsig functions to use the ldebug() facility even though they
100 * are not syscalls themselves. Map them to syscall 0. This is slightly less
101 * bogus than using ldebug(sigreturn).
103 #define LINUX32_SYS_linux_rt_sendsig 0
104 #define LINUX32_SYS_linux_sendsig 0
106 const char *linux_kplatform;
107 static int linux_szsigcode;
108 static vm_object_t linux_shared_page_obj;
109 static char *linux_shared_page_mapping;
110 extern char _binary_linux32_locore_o_start;
111 extern char _binary_linux32_locore_o_end;
113 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
115 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
117 static int linux_fixup_elf(register_t **stack_base,
118 struct image_params *iparams);
119 static register_t *linux_copyout_strings(struct image_params *imgp);
120 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
121 static void linux_exec_setregs(struct thread *td,
122 struct image_params *imgp, u_long stack);
123 static void linux32_fixlimit(struct rlimit *rl, int which);
124 static bool linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
125 static void linux_vdso_install(void *param);
126 static void linux_vdso_deinstall(void *param);
128 #define LINUX_T_UNKNOWN 255
129 static int _bsd_to_linux_trapcode[] = {
130 LINUX_T_UNKNOWN, /* 0 */
131 6, /* 1 T_PRIVINFLT */
132 LINUX_T_UNKNOWN, /* 2 */
134 LINUX_T_UNKNOWN, /* 4 */
135 LINUX_T_UNKNOWN, /* 5 */
136 16, /* 6 T_ARITHTRAP */
137 254, /* 7 T_ASTFLT */
138 LINUX_T_UNKNOWN, /* 8 */
139 13, /* 9 T_PROTFLT */
140 1, /* 10 T_TRCTRAP */
141 LINUX_T_UNKNOWN, /* 11 */
142 14, /* 12 T_PAGEFLT */
143 LINUX_T_UNKNOWN, /* 13 */
144 17, /* 14 T_ALIGNFLT */
145 LINUX_T_UNKNOWN, /* 15 */
146 LINUX_T_UNKNOWN, /* 16 */
147 LINUX_T_UNKNOWN, /* 17 */
153 8, /* 23 T_DOUBLEFLT */
154 9, /* 24 T_FPOPFLT */
155 10, /* 25 T_TSSFLT */
156 11, /* 26 T_SEGNPFLT */
157 12, /* 27 T_STKFLT */
159 19, /* 29 T_XMMFLT */
160 15 /* 30 T_RESERVED */
162 #define bsd_to_linux_trapcode(code) \
163 ((code)<nitems(_bsd_to_linux_trapcode)? \
164 _bsd_to_linux_trapcode[(code)]: \
167 struct linux32_ps_strings {
168 u_int32_t ps_argvstr; /* first of 0 or more argument strings */
169 u_int ps_nargvstr; /* the number of argument strings */
170 u_int32_t ps_envstr; /* first of 0 or more environment strings */
171 u_int ps_nenvstr; /* the number of environment strings */
174 LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
175 LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
176 LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
177 LINUX_VDSO_SYM_CHAR(linux_platform);
180 * If FreeBSD & Linux have a difference of opinion about what a trap
181 * means, deal with it here.
186 linux_translate_traps(int signal, int trap_code)
188 if (signal != SIGBUS)
202 linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
207 struct linux32_ps_strings *arginfo;
210 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
212 KASSERT(curthread->td_proc == imgp->proc,
213 ("unsafe linux_fixup_elf(), should be curproc"));
214 base = (Elf32_Addr *)*stack_base;
215 args = (Elf32_Auxargs *)imgp->auxargs;
216 pos = base + (imgp->args->argc + imgp->args->envc + 2);
218 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
219 AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO_EHDR,
220 imgp->proc->p_sysent->sv_shared_page_base);
221 AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
222 AUXARGS_ENTRY_32(pos, LINUX_AT_HWCAP, cpu_feature);
225 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
226 * as it has appeared in the 2.4.0-rc7 first time.
227 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
228 * glibc falls back to the hard-coded CLK_TCK value when aux entry
230 * Also see linux_times() implementation.
232 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
233 AUXARGS_ENTRY_32(pos, LINUX_AT_CLKTCK, stclohz);
234 AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr);
235 AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent);
236 AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum);
237 AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz);
238 AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags);
239 AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry);
240 AUXARGS_ENTRY_32(pos, AT_BASE, args->base);
241 AUXARGS_ENTRY_32(pos, LINUX_AT_SECURE, issetugid);
242 AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
243 AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
244 AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
245 AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
246 AUXARGS_ENTRY_32(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
247 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
248 if (imgp->execpathp != 0)
249 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
250 if (args->execfd != -1)
251 AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd);
252 AUXARGS_ENTRY_32(pos, AT_NULL, 0);
254 free(imgp->auxargs, M_TEMP);
255 imgp->auxargs = NULL;
258 suword32(base, (uint32_t)imgp->args->argc);
259 *stack_base = (register_t *)base;
264 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
266 struct thread *td = curthread;
267 struct proc *p = td->td_proc;
269 struct trapframe *regs;
270 struct l_rt_sigframe *fp, frame;
275 sig = ksi->ksi_signo;
276 code = ksi->ksi_code;
277 PROC_LOCK_ASSERT(p, MA_OWNED);
279 mtx_assert(&psp->ps_mtx, MA_OWNED);
281 oonstack = sigonstack(regs->tf_rsp);
284 if (ldebug(rt_sendsig))
285 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
286 catcher, sig, (void*)mask, code);
288 /* Allocate space for the signal handler context. */
289 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
290 SIGISMEMBER(psp->ps_sigonstack, sig)) {
291 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
292 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
294 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
295 mtx_unlock(&psp->ps_mtx);
297 /* Build the argument list for the signal handler. */
298 sig = bsd_to_linux_signal(sig);
300 bzero(&frame, sizeof(frame));
302 frame.sf_handler = PTROUT(catcher);
304 frame.sf_siginfo = PTROUT(&fp->sf_si);
305 frame.sf_ucontext = PTROUT(&fp->sf_sc);
307 /* Fill in POSIX parts. */
308 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
311 * Build the signal context to be used by sigreturn and libgcc unwind.
313 frame.sf_sc.uc_flags = 0; /* XXX ??? */
314 frame.sf_sc.uc_link = 0; /* XXX ??? */
316 frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
317 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
318 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
319 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
322 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
324 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
325 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_rdi;
326 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_rsi;
327 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_rbp;
328 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_rbx;
329 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_rsp;
330 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_rdx;
331 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_rcx;
332 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_rax;
333 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_rip;
334 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
335 frame.sf_sc.uc_mcontext.sc_gs = regs->tf_gs;
336 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
337 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
338 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
339 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
340 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
341 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
342 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
343 frame.sf_sc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
344 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
347 if (ldebug(rt_sendsig))
348 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
349 frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
350 td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
353 if (copyout(&frame, fp, sizeof(frame)) != 0) {
355 * Process has trashed its stack; give it an illegal
356 * instruction to halt it in its tracks.
359 if (ldebug(rt_sendsig))
360 printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
367 /* Build context to run handler in. */
368 regs->tf_rsp = PTROUT(fp);
369 regs->tf_rip = linux32_rt_sigcode;
370 regs->tf_rflags &= ~(PSL_T | PSL_D);
371 regs->tf_cs = _ucode32sel;
372 regs->tf_ss = _udatasel;
373 regs->tf_ds = _udatasel;
374 regs->tf_es = _udatasel;
375 regs->tf_fs = _ufssel;
376 regs->tf_gs = _ugssel;
377 regs->tf_flags = TF_HASSEGS;
378 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
380 mtx_lock(&psp->ps_mtx);
385 * Send an interrupt to process.
387 * Stack is set up to allow sigcode stored
388 * in u. to call routine, followed by kcall
389 * to sigreturn routine below. After sigreturn
390 * resets the signal mask, the stack, and the
391 * frame pointer, it returns to the user
395 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
397 struct thread *td = curthread;
398 struct proc *p = td->td_proc;
400 struct trapframe *regs;
401 struct l_sigframe *fp, frame;
406 sig = ksi->ksi_signo;
407 code = ksi->ksi_code;
408 PROC_LOCK_ASSERT(p, MA_OWNED);
410 mtx_assert(&psp->ps_mtx, MA_OWNED);
411 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
412 /* Signal handler installed with SA_SIGINFO. */
413 linux_rt_sendsig(catcher, ksi, mask);
418 oonstack = sigonstack(regs->tf_rsp);
422 printf(ARGS(sendsig, "%p, %d, %p, %u"),
423 catcher, sig, (void*)mask, code);
426 /* Allocate space for the signal handler context. */
427 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
428 SIGISMEMBER(psp->ps_sigonstack, sig)) {
429 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
430 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
432 fp = (struct l_sigframe *)regs->tf_rsp - 1;
433 mtx_unlock(&psp->ps_mtx);
436 /* Build the argument list for the signal handler. */
437 sig = bsd_to_linux_signal(sig);
439 bzero(&frame, sizeof(frame));
441 frame.sf_handler = PTROUT(catcher);
444 bsd_to_linux_sigset(mask, &lmask);
446 /* Build the signal context to be used by sigreturn. */
447 frame.sf_sc.sc_mask = lmask.__mask;
448 frame.sf_sc.sc_gs = regs->tf_gs;
449 frame.sf_sc.sc_fs = regs->tf_fs;
450 frame.sf_sc.sc_es = regs->tf_es;
451 frame.sf_sc.sc_ds = regs->tf_ds;
452 frame.sf_sc.sc_edi = regs->tf_rdi;
453 frame.sf_sc.sc_esi = regs->tf_rsi;
454 frame.sf_sc.sc_ebp = regs->tf_rbp;
455 frame.sf_sc.sc_ebx = regs->tf_rbx;
456 frame.sf_sc.sc_esp = regs->tf_rsp;
457 frame.sf_sc.sc_edx = regs->tf_rdx;
458 frame.sf_sc.sc_ecx = regs->tf_rcx;
459 frame.sf_sc.sc_eax = regs->tf_rax;
460 frame.sf_sc.sc_eip = regs->tf_rip;
461 frame.sf_sc.sc_cs = regs->tf_cs;
462 frame.sf_sc.sc_eflags = regs->tf_rflags;
463 frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
464 frame.sf_sc.sc_ss = regs->tf_ss;
465 frame.sf_sc.sc_err = regs->tf_err;
466 frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
467 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
469 frame.sf_extramask[0] = lmask.__mask;
471 if (copyout(&frame, fp, sizeof(frame)) != 0) {
473 * Process has trashed its stack; give it an illegal
474 * instruction to halt it in its tracks.
480 /* Build context to run handler in. */
481 regs->tf_rsp = PTROUT(fp);
482 regs->tf_rip = linux32_sigcode;
483 regs->tf_rflags &= ~(PSL_T | PSL_D);
484 regs->tf_cs = _ucode32sel;
485 regs->tf_ss = _udatasel;
486 regs->tf_ds = _udatasel;
487 regs->tf_es = _udatasel;
488 regs->tf_fs = _ufssel;
489 regs->tf_gs = _ugssel;
490 regs->tf_flags = TF_HASSEGS;
491 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
493 mtx_lock(&psp->ps_mtx);
497 * System call to cleanup state after a signal
498 * has been taken. Reset signal mask and
499 * stack state from context left by sendsig (above).
500 * Return to previous pc and psl as specified by
501 * context left by sendsig. Check carefully to
502 * make sure that the user has not modified the
503 * psl to gain improper privileges or to cause
507 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
509 struct l_sigframe frame;
510 struct trapframe *regs;
519 if (ldebug(sigreturn))
520 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
523 * The trampoline code hands us the sigframe.
524 * It is unsafe to keep track of it ourselves, in the event that a
525 * program jumps out of a signal handler.
527 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
530 /* Check for security violations. */
531 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
532 eflags = frame.sf_sc.sc_eflags;
533 if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
537 * Don't allow users to load a valid privileged %cs. Let the
538 * hardware check for invalid selectors, excess privilege in
539 * other selectors, invalid %eip's and invalid %esp's.
541 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
542 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
543 ksiginfo_init_trap(&ksi);
544 ksi.ksi_signo = SIGBUS;
545 ksi.ksi_code = BUS_OBJERR;
546 ksi.ksi_trapno = T_PROTFLT;
547 ksi.ksi_addr = (void *)regs->tf_rip;
548 trapsignal(td, &ksi);
552 lmask.__mask = frame.sf_sc.sc_mask;
553 lmask.__mask = frame.sf_extramask[0];
554 linux_to_bsd_sigset(&lmask, &bmask);
555 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
557 /* Restore signal context. */
558 regs->tf_rdi = frame.sf_sc.sc_edi;
559 regs->tf_rsi = frame.sf_sc.sc_esi;
560 regs->tf_rbp = frame.sf_sc.sc_ebp;
561 regs->tf_rbx = frame.sf_sc.sc_ebx;
562 regs->tf_rdx = frame.sf_sc.sc_edx;
563 regs->tf_rcx = frame.sf_sc.sc_ecx;
564 regs->tf_rax = frame.sf_sc.sc_eax;
565 regs->tf_rip = frame.sf_sc.sc_eip;
566 regs->tf_cs = frame.sf_sc.sc_cs;
567 regs->tf_ds = frame.sf_sc.sc_ds;
568 regs->tf_es = frame.sf_sc.sc_es;
569 regs->tf_fs = frame.sf_sc.sc_fs;
570 regs->tf_gs = frame.sf_sc.sc_gs;
571 regs->tf_rflags = eflags;
572 regs->tf_rsp = frame.sf_sc.sc_esp_at_signal;
573 regs->tf_ss = frame.sf_sc.sc_ss;
574 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
576 return (EJUSTRETURN);
580 * System call to cleanup state after a signal
581 * has been taken. Reset signal mask and
582 * stack state from context left by rt_sendsig (above).
583 * Return to previous pc and psl as specified by
584 * context left by sendsig. Check carefully to
585 * make sure that the user has not modified the
586 * psl to gain improper privileges or to cause
590 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
592 struct l_ucontext uc;
593 struct l_sigcontext *context;
597 struct trapframe *regs;
604 if (ldebug(rt_sigreturn))
605 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
608 * The trampoline code hands us the ucontext.
609 * It is unsafe to keep track of it ourselves, in the event that a
610 * program jumps out of a signal handler.
612 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
615 context = &uc.uc_mcontext;
617 /* Check for security violations. */
618 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
619 eflags = context->sc_eflags;
620 if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
624 * Don't allow users to load a valid privileged %cs. Let the
625 * hardware check for invalid selectors, excess privilege in
626 * other selectors, invalid %eip's and invalid %esp's.
628 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
629 if (!CS_SECURE(context->sc_cs)) {
630 ksiginfo_init_trap(&ksi);
631 ksi.ksi_signo = SIGBUS;
632 ksi.ksi_code = BUS_OBJERR;
633 ksi.ksi_trapno = T_PROTFLT;
634 ksi.ksi_addr = (void *)regs->tf_rip;
635 trapsignal(td, &ksi);
639 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
640 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
643 * Restore signal context
645 regs->tf_gs = context->sc_gs;
646 regs->tf_fs = context->sc_fs;
647 regs->tf_es = context->sc_es;
648 regs->tf_ds = context->sc_ds;
649 regs->tf_rdi = context->sc_edi;
650 regs->tf_rsi = context->sc_esi;
651 regs->tf_rbp = context->sc_ebp;
652 regs->tf_rbx = context->sc_ebx;
653 regs->tf_rdx = context->sc_edx;
654 regs->tf_rcx = context->sc_ecx;
655 regs->tf_rax = context->sc_eax;
656 regs->tf_rip = context->sc_eip;
657 regs->tf_cs = context->sc_cs;
658 regs->tf_rflags = eflags;
659 regs->tf_rsp = context->sc_esp_at_signal;
660 regs->tf_ss = context->sc_ss;
661 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
664 * call sigaltstack & ignore results..
667 ss.ss_sp = PTRIN(lss->ss_sp);
668 ss.ss_size = lss->ss_size;
669 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
672 if (ldebug(rt_sigreturn))
673 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
674 ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
676 (void)kern_sigaltstack(td, &ss, NULL);
678 return (EJUSTRETURN);
682 linux32_fetch_syscall_args(struct thread *td)
685 struct trapframe *frame;
686 struct syscall_args *sa;
689 frame = td->td_frame;
692 sa->args[0] = frame->tf_rbx;
693 sa->args[1] = frame->tf_rcx;
694 sa->args[2] = frame->tf_rdx;
695 sa->args[3] = frame->tf_rsi;
696 sa->args[4] = frame->tf_rdi;
697 sa->args[5] = frame->tf_rbp; /* Unconfirmed */
698 sa->code = frame->tf_rax;
700 if (sa->code >= p->p_sysent->sv_size)
702 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
704 sa->callp = &p->p_sysent->sv_table[sa->code];
705 sa->narg = sa->callp->sy_narg;
707 td->td_retval[0] = 0;
708 td->td_retval[1] = frame->tf_rdx;
714 * Clear registers on exec
715 * XXX copied from ia32_signal.c.
718 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
720 struct trapframe *regs = td->td_frame;
721 struct pcb *pcb = td->td_pcb;
722 register_t saved_rflags;
727 if (td->td_proc->p_md.md_ldt != NULL)
731 wrmsr(MSR_FSBASE, 0);
732 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */
736 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
738 saved_rflags = regs->tf_rflags & PSL_T;
739 bzero((char *)regs, sizeof(struct trapframe));
740 regs->tf_rip = imgp->entry_addr;
741 regs->tf_rsp = stack;
742 regs->tf_rflags = PSL_USER | saved_rflags;
743 regs->tf_gs = _ugssel;
744 regs->tf_fs = _ufssel;
745 regs->tf_es = _udatasel;
746 regs->tf_ds = _udatasel;
747 regs->tf_ss = _udatasel;
748 regs->tf_flags = TF_HASSEGS;
749 regs->tf_cs = _ucode32sel;
750 regs->tf_rbx = imgp->ps_strings;
754 /* Do full restore on return so that we can change to a different %cs */
755 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
759 * XXX copied from ia32_sysvec.c.
762 linux_copyout_strings(struct image_params *imgp)
766 char *stringp, *destp;
767 u_int32_t *stack_base;
768 struct linux32_ps_strings *arginfo;
769 char canary[LINUX_AT_RANDOM_LEN];
772 /* Calculate string base and vector table pointers. */
773 if (imgp->execpath != NULL && imgp->auxargs != NULL)
774 execpath_len = strlen(imgp->execpath) + 1;
778 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
779 destp = (caddr_t)arginfo - SPARE_USRSPACE -
780 roundup(sizeof(canary), sizeof(char *)) -
781 roundup(execpath_len, sizeof(char *)) -
782 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
784 if (execpath_len != 0) {
785 imgp->execpathp = (uintptr_t)arginfo - execpath_len;
786 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
789 /* Prepare the canary for SSP. */
790 arc4rand(canary, sizeof(canary), 0);
791 imgp->canary = (uintptr_t)arginfo -
792 roundup(execpath_len, sizeof(char *)) -
793 roundup(sizeof(canary), sizeof(char *));
794 copyout(canary, (void *)imgp->canary, sizeof(canary));
796 vectp = (uint32_t *)destp;
799 * Allocate room on the stack for the ELF auxargs
800 * array. It has LINUX_AT_COUNT entries.
802 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
807 * Allocate room for the argv[] and env vectors including the
808 * terminating NULL pointers.
810 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
812 /* vectp also becomes our initial stack base. */
815 stringp = imgp->args->begin_argv;
816 argc = imgp->args->argc;
817 envc = imgp->args->envc;
818 /* Copy out strings - arguments and environment. */
819 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
821 /* Fill in "ps_strings" struct for ps, w, etc. */
822 suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
823 suword32(&arginfo->ps_nargvstr, argc);
825 /* Fill in argument portion of vector table. */
826 for (; argc > 0; --argc) {
827 suword32(vectp++, (uint32_t)(intptr_t)destp);
828 while (*stringp++ != 0)
833 /* A null vector table pointer separates the argp's from the envp's. */
834 suword32(vectp++, 0);
836 suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
837 suword32(&arginfo->ps_nenvstr, envc);
839 /* Fill in environment portion of vector table. */
840 for (; envc > 0; --envc) {
841 suword32(vectp++, (uint32_t)(intptr_t)destp);
842 while (*stringp++ != 0)
847 /* The end of the vector table is a null pointer. */
850 return ((register_t *)stack_base);
853 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
854 "32-bit Linux emulation");
856 static u_long linux32_maxdsiz = LINUX32_MAXDSIZ;
857 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
858 &linux32_maxdsiz, 0, "");
859 static u_long linux32_maxssiz = LINUX32_MAXSSIZ;
860 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
861 &linux32_maxssiz, 0, "");
862 static u_long linux32_maxvmem = LINUX32_MAXVMEM;
863 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
864 &linux32_maxvmem, 0, "");
867 SYSCTL_PROC(_compat_linux32, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
868 linux_sysctl_debug, "A", "Linux debugging control");
872 linux32_fixlimit(struct rlimit *rl, int which)
877 if (linux32_maxdsiz != 0) {
878 if (rl->rlim_cur > linux32_maxdsiz)
879 rl->rlim_cur = linux32_maxdsiz;
880 if (rl->rlim_max > linux32_maxdsiz)
881 rl->rlim_max = linux32_maxdsiz;
885 if (linux32_maxssiz != 0) {
886 if (rl->rlim_cur > linux32_maxssiz)
887 rl->rlim_cur = linux32_maxssiz;
888 if (rl->rlim_max > linux32_maxssiz)
889 rl->rlim_max = linux32_maxssiz;
893 if (linux32_maxvmem != 0) {
894 if (rl->rlim_cur > linux32_maxvmem)
895 rl->rlim_cur = linux32_maxvmem;
896 if (rl->rlim_max > linux32_maxvmem)
897 rl->rlim_max = linux32_maxvmem;
903 struct sysentvec elf_linux_sysvec = {
904 .sv_size = LINUX32_SYS_MAXSYSCALL,
905 .sv_table = linux32_sysent,
907 .sv_errsize = ELAST + 1,
908 .sv_errtbl = linux_errtbl,
909 .sv_transtrap = linux_translate_traps,
910 .sv_fixup = linux_fixup_elf,
911 .sv_sendsig = linux_sendsig,
912 .sv_sigcode = &_binary_linux32_locore_o_start,
913 .sv_szsigcode = &linux_szsigcode,
914 .sv_name = "Linux ELF32",
915 .sv_coredump = elf32_coredump,
916 .sv_imgact_try = linux_exec_imgact_try,
917 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
918 .sv_pagesize = PAGE_SIZE,
919 .sv_minuser = VM_MIN_ADDRESS,
920 .sv_maxuser = LINUX32_MAXUSER,
921 .sv_usrstack = LINUX32_USRSTACK,
922 .sv_psstrings = LINUX32_PS_STRINGS,
923 .sv_stackprot = VM_PROT_ALL,
924 .sv_copyout_strings = linux_copyout_strings,
925 .sv_setregs = linux_exec_setregs,
926 .sv_fixlimit = linux32_fixlimit,
927 .sv_maxssiz = &linux32_maxssiz,
928 .sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
929 .sv_set_syscall_retval = cpu_set_syscall_retval,
930 .sv_fetch_syscall_args = linux32_fetch_syscall_args,
931 .sv_syscallnames = NULL,
932 .sv_shared_page_base = LINUX32_SHAREDPAGE,
933 .sv_shared_page_len = PAGE_SIZE,
934 .sv_schedtail = linux_schedtail,
935 .sv_thread_detach = linux_thread_detach,
940 linux_vdso_install(void *param)
943 linux_szsigcode = (&_binary_linux32_locore_o_end -
944 &_binary_linux32_locore_o_start);
946 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
947 panic("Linux invalid vdso size\n");
949 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
951 linux_shared_page_obj = __elfN(linux_shared_page_init)
952 (&linux_shared_page_mapping);
954 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
956 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
958 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
960 linux_kplatform = linux_shared_page_mapping +
961 (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
963 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
964 linux_vdso_install, NULL);
967 linux_vdso_deinstall(void *param)
970 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
972 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
973 linux_vdso_deinstall, NULL);
975 static char GNU_ABI_VENDOR[] = "GNU";
976 static int GNULINUX_ABI_DESC = 0;
979 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
981 const Elf32_Word *desc;
984 p = (uintptr_t)(note + 1);
985 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
987 desc = (const Elf32_Word *)p;
988 if (desc[0] != GNULINUX_ABI_DESC)
992 * For Linux we encode osrel as follows (see linux_mib.c):
993 * VVVMMMIII (version, major, minor), see linux_mib.c.
995 *osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
1000 static Elf_Brandnote linux32_brandnote = {
1001 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
1002 .hdr.n_descsz = 16, /* XXX at least 16 */
1004 .vendor = GNU_ABI_VENDOR,
1005 .flags = BN_TRANSLATE_OSREL,
1006 .trans_osrel = linux32_trans_osrel
1009 static Elf32_Brandinfo linux_brand = {
1010 .brand = ELFOSABI_LINUX,
1012 .compat_3_brand = "Linux",
1013 .emul_path = "/compat/linux",
1014 .interp_path = "/lib/ld-linux.so.1",
1015 .sysvec = &elf_linux_sysvec,
1016 .interp_newpath = NULL,
1017 .brand_note = &linux32_brandnote,
1018 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1021 static Elf32_Brandinfo linux_glibc2brand = {
1022 .brand = ELFOSABI_LINUX,
1024 .compat_3_brand = "Linux",
1025 .emul_path = "/compat/linux",
1026 .interp_path = "/lib/ld-linux.so.2",
1027 .sysvec = &elf_linux_sysvec,
1028 .interp_newpath = NULL,
1029 .brand_note = &linux32_brandnote,
1030 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1033 static Elf32_Brandinfo linux_muslbrand = {
1034 .brand = ELFOSABI_LINUX,
1036 .compat_3_brand = "Linux",
1037 .emul_path = "/compat/linux",
1038 .interp_path = "/lib/ld-musl-i386.so.1",
1039 .sysvec = &elf_linux_sysvec,
1040 .interp_newpath = NULL,
1041 .brand_note = &linux32_brandnote,
1042 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1045 Elf32_Brandinfo *linux_brandlist[] = {
1053 linux_elf_modevent(module_t mod, int type, void *data)
1055 Elf32_Brandinfo **brandinfo;
1057 struct linux_ioctl_handler **lihp;
1063 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1065 if (elf32_insert_brand_entry(*brandinfo) < 0)
1068 SET_FOREACH(lihp, linux_ioctl_handler_set)
1069 linux_ioctl_register_handler(*lihp);
1070 LIST_INIT(&futex_list);
1071 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1072 stclohz = (stathz ? stathz : hz);
1074 printf("Linux ELF exec handler installed\n");
1076 printf("cannot insert Linux ELF brand handler\n");
1079 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1081 if (elf32_brand_inuse(*brandinfo))
1084 for (brandinfo = &linux_brandlist[0];
1085 *brandinfo != NULL; ++brandinfo)
1086 if (elf32_remove_brand_entry(*brandinfo) < 0)
1090 SET_FOREACH(lihp, linux_ioctl_handler_set)
1091 linux_ioctl_unregister_handler(*lihp);
1092 mtx_destroy(&futex_mtx);
1094 printf("Linux ELF exec handler removed\n");
1096 printf("Could not deinstall ELF interpreter entry\n");
1099 return (EOPNOTSUPP);
1104 static moduledata_t linux_elf_mod = {
1110 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1111 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1112 FEATURE(linux, "Linux 32bit support");