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);
93 * Allow the sendsig functions to use the ldebug() facility even though they
94 * are not syscalls themselves. Map them to syscall 0. This is slightly less
95 * bogus than using ldebug(sigreturn).
97 #define LINUX32_SYS_linux_rt_sendsig 0
98 #define LINUX32_SYS_linux_sendsig 0
100 const char *linux_kplatform;
101 static int linux_szsigcode;
102 static vm_object_t linux_shared_page_obj;
103 static char *linux_shared_page_mapping;
104 extern char _binary_linux32_locore_o_start;
105 extern char _binary_linux32_locore_o_end;
107 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
109 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
111 static int linux_fixup_elf(register_t **stack_base,
112 struct image_params *iparams);
113 static register_t *linux_copyout_strings(struct image_params *imgp);
114 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
115 static void linux_exec_setregs(struct thread *td,
116 struct image_params *imgp, u_long stack);
117 static void linux32_fixlimit(struct rlimit *rl, int which);
118 static bool linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
119 static void linux_vdso_install(void *param);
120 static void linux_vdso_deinstall(void *param);
122 #define LINUX_T_UNKNOWN 255
123 static int _bsd_to_linux_trapcode[] = {
124 LINUX_T_UNKNOWN, /* 0 */
125 6, /* 1 T_PRIVINFLT */
126 LINUX_T_UNKNOWN, /* 2 */
128 LINUX_T_UNKNOWN, /* 4 */
129 LINUX_T_UNKNOWN, /* 5 */
130 16, /* 6 T_ARITHTRAP */
131 254, /* 7 T_ASTFLT */
132 LINUX_T_UNKNOWN, /* 8 */
133 13, /* 9 T_PROTFLT */
134 1, /* 10 T_TRCTRAP */
135 LINUX_T_UNKNOWN, /* 11 */
136 14, /* 12 T_PAGEFLT */
137 LINUX_T_UNKNOWN, /* 13 */
138 17, /* 14 T_ALIGNFLT */
139 LINUX_T_UNKNOWN, /* 15 */
140 LINUX_T_UNKNOWN, /* 16 */
141 LINUX_T_UNKNOWN, /* 17 */
147 8, /* 23 T_DOUBLEFLT */
148 9, /* 24 T_FPOPFLT */
149 10, /* 25 T_TSSFLT */
150 11, /* 26 T_SEGNPFLT */
151 12, /* 27 T_STKFLT */
153 19, /* 29 T_XMMFLT */
154 15 /* 30 T_RESERVED */
156 #define bsd_to_linux_trapcode(code) \
157 ((code)<nitems(_bsd_to_linux_trapcode)? \
158 _bsd_to_linux_trapcode[(code)]: \
161 struct linux32_ps_strings {
162 u_int32_t ps_argvstr; /* first of 0 or more argument strings */
163 u_int ps_nargvstr; /* the number of argument strings */
164 u_int32_t ps_envstr; /* first of 0 or more environment strings */
165 u_int ps_nenvstr; /* the number of environment strings */
168 LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
169 LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
170 LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
171 LINUX_VDSO_SYM_CHAR(linux_platform);
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_elf(register_t **stack_base, struct image_params *imgp)
199 Elf32_Auxinfo *argarray, *pos;
200 Elf32_Addr *auxbase, *base;
201 struct linux32_ps_strings *arginfo;
202 int error, issetugid;
204 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
206 KASSERT(curthread->td_proc == imgp->proc,
207 ("unsafe linux_fixup_elf(), should be curproc"));
208 base = (Elf32_Addr *)*stack_base;
209 args = (Elf32_Auxargs *)imgp->auxargs;
210 auxbase = base + (imgp->args->argc + 1 + imgp->args->envc + 1);
211 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
214 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
215 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
216 imgp->proc->p_sysent->sv_shared_page_base);
217 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
218 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
221 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
222 * as it has appeared in the 2.4.0-rc7 first time.
223 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
224 * glibc falls back to the hard-coded CLK_TCK value when aux entry
226 * Also see linux_times() implementation.
228 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
229 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
230 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
231 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
232 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
233 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
234 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
235 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
236 AUXARGS_ENTRY(pos, AT_BASE, args->base);
237 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
238 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
239 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
240 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
241 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
242 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
243 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
244 if (imgp->execpathp != 0)
245 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
246 if (args->execfd != -1)
247 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
248 AUXARGS_ENTRY(pos, AT_NULL, 0);
250 free(imgp->auxargs, M_TEMP);
251 imgp->auxargs = NULL;
252 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
254 error = copyout(&argarray[0], auxbase,
255 sizeof(*argarray) * LINUX_AT_COUNT);
256 free(argarray, M_TEMP);
261 if (suword32(base, (uint32_t)imgp->args->argc) == -1)
263 *stack_base = (register_t *)base;
268 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
270 struct thread *td = curthread;
271 struct proc *p = td->td_proc;
273 struct trapframe *regs;
274 struct l_rt_sigframe *fp, frame;
279 sig = ksi->ksi_signo;
280 code = ksi->ksi_code;
281 PROC_LOCK_ASSERT(p, MA_OWNED);
283 mtx_assert(&psp->ps_mtx, MA_OWNED);
285 oonstack = sigonstack(regs->tf_rsp);
288 if (ldebug(rt_sendsig))
289 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
290 catcher, sig, (void*)mask, code);
292 /* Allocate space for the signal handler context. */
293 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
294 SIGISMEMBER(psp->ps_sigonstack, sig)) {
295 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
296 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
298 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
299 mtx_unlock(&psp->ps_mtx);
301 /* Build the argument list for the signal handler. */
302 sig = bsd_to_linux_signal(sig);
304 bzero(&frame, sizeof(frame));
306 frame.sf_handler = PTROUT(catcher);
308 frame.sf_siginfo = PTROUT(&fp->sf_si);
309 frame.sf_ucontext = PTROUT(&fp->sf_sc);
311 /* Fill in POSIX parts. */
312 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
315 * Build the signal context to be used by sigreturn and libgcc unwind.
317 frame.sf_sc.uc_flags = 0; /* XXX ??? */
318 frame.sf_sc.uc_link = 0; /* XXX ??? */
320 frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
321 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
322 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
323 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
326 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
328 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
329 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_rdi;
330 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_rsi;
331 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_rbp;
332 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_rbx;
333 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_rsp;
334 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_rdx;
335 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_rcx;
336 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_rax;
337 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_rip;
338 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
339 frame.sf_sc.uc_mcontext.sc_gs = regs->tf_gs;
340 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
341 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
342 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
343 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
344 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
345 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
346 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
347 frame.sf_sc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
348 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
351 if (ldebug(rt_sendsig))
352 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
353 frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
354 td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
357 if (copyout(&frame, fp, sizeof(frame)) != 0) {
359 * Process has trashed its stack; give it an illegal
360 * instruction to halt it in its tracks.
363 if (ldebug(rt_sendsig))
364 printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
371 /* Build context to run handler in. */
372 regs->tf_rsp = PTROUT(fp);
373 regs->tf_rip = linux32_rt_sigcode;
374 regs->tf_rflags &= ~(PSL_T | PSL_D);
375 regs->tf_cs = _ucode32sel;
376 regs->tf_ss = _udatasel;
377 regs->tf_ds = _udatasel;
378 regs->tf_es = _udatasel;
379 regs->tf_fs = _ufssel;
380 regs->tf_gs = _ugssel;
381 regs->tf_flags = TF_HASSEGS;
382 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
384 mtx_lock(&psp->ps_mtx);
389 * Send an interrupt to process.
391 * Stack is set up to allow sigcode stored
392 * in u. to call routine, followed by kcall
393 * to sigreturn routine below. After sigreturn
394 * resets the signal mask, the stack, and the
395 * frame pointer, it returns to the user
399 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
401 struct thread *td = curthread;
402 struct proc *p = td->td_proc;
404 struct trapframe *regs;
405 struct l_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);
415 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
416 /* Signal handler installed with SA_SIGINFO. */
417 linux_rt_sendsig(catcher, ksi, mask);
422 oonstack = sigonstack(regs->tf_rsp);
426 printf(ARGS(sendsig, "%p, %d, %p, %u"),
427 catcher, sig, (void*)mask, code);
430 /* Allocate space for the signal handler context. */
431 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
432 SIGISMEMBER(psp->ps_sigonstack, sig)) {
433 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
434 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
436 fp = (struct l_sigframe *)regs->tf_rsp - 1;
437 mtx_unlock(&psp->ps_mtx);
440 /* Build the argument list for the signal handler. */
441 sig = bsd_to_linux_signal(sig);
443 bzero(&frame, sizeof(frame));
445 frame.sf_handler = PTROUT(catcher);
448 bsd_to_linux_sigset(mask, &lmask);
450 /* Build the signal context to be used by sigreturn. */
451 frame.sf_sc.sc_mask = lmask.__mask;
452 frame.sf_sc.sc_gs = regs->tf_gs;
453 frame.sf_sc.sc_fs = regs->tf_fs;
454 frame.sf_sc.sc_es = regs->tf_es;
455 frame.sf_sc.sc_ds = regs->tf_ds;
456 frame.sf_sc.sc_edi = regs->tf_rdi;
457 frame.sf_sc.sc_esi = regs->tf_rsi;
458 frame.sf_sc.sc_ebp = regs->tf_rbp;
459 frame.sf_sc.sc_ebx = regs->tf_rbx;
460 frame.sf_sc.sc_esp = regs->tf_rsp;
461 frame.sf_sc.sc_edx = regs->tf_rdx;
462 frame.sf_sc.sc_ecx = regs->tf_rcx;
463 frame.sf_sc.sc_eax = regs->tf_rax;
464 frame.sf_sc.sc_eip = regs->tf_rip;
465 frame.sf_sc.sc_cs = regs->tf_cs;
466 frame.sf_sc.sc_eflags = regs->tf_rflags;
467 frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
468 frame.sf_sc.sc_ss = regs->tf_ss;
469 frame.sf_sc.sc_err = regs->tf_err;
470 frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
471 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
473 frame.sf_extramask[0] = lmask.__mask;
475 if (copyout(&frame, fp, sizeof(frame)) != 0) {
477 * Process has trashed its stack; give it an illegal
478 * instruction to halt it in its tracks.
484 /* Build context to run handler in. */
485 regs->tf_rsp = PTROUT(fp);
486 regs->tf_rip = linux32_sigcode;
487 regs->tf_rflags &= ~(PSL_T | PSL_D);
488 regs->tf_cs = _ucode32sel;
489 regs->tf_ss = _udatasel;
490 regs->tf_ds = _udatasel;
491 regs->tf_es = _udatasel;
492 regs->tf_fs = _ufssel;
493 regs->tf_gs = _ugssel;
494 regs->tf_flags = TF_HASSEGS;
495 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
497 mtx_lock(&psp->ps_mtx);
501 * System call to cleanup state after a signal
502 * has been taken. Reset signal mask and
503 * stack state from context left by sendsig (above).
504 * Return to previous pc and psl as specified by
505 * context left by sendsig. Check carefully to
506 * make sure that the user has not modified the
507 * psl to gain improper privileges or to cause
511 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
513 struct l_sigframe frame;
514 struct trapframe *regs;
523 if (ldebug(sigreturn))
524 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
527 * The trampoline code hands us the sigframe.
528 * It is unsafe to keep track of it ourselves, in the event that a
529 * program jumps out of a signal handler.
531 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
534 /* Check for security violations. */
535 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
536 eflags = frame.sf_sc.sc_eflags;
537 if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
541 * Don't allow users to load a valid privileged %cs. Let the
542 * hardware check for invalid selectors, excess privilege in
543 * other selectors, invalid %eip's and invalid %esp's.
545 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
546 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
547 ksiginfo_init_trap(&ksi);
548 ksi.ksi_signo = SIGBUS;
549 ksi.ksi_code = BUS_OBJERR;
550 ksi.ksi_trapno = T_PROTFLT;
551 ksi.ksi_addr = (void *)regs->tf_rip;
552 trapsignal(td, &ksi);
556 lmask.__mask = frame.sf_sc.sc_mask;
557 lmask.__mask = frame.sf_extramask[0];
558 linux_to_bsd_sigset(&lmask, &bmask);
559 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
561 /* Restore signal context. */
562 regs->tf_rdi = frame.sf_sc.sc_edi;
563 regs->tf_rsi = frame.sf_sc.sc_esi;
564 regs->tf_rbp = frame.sf_sc.sc_ebp;
565 regs->tf_rbx = frame.sf_sc.sc_ebx;
566 regs->tf_rdx = frame.sf_sc.sc_edx;
567 regs->tf_rcx = frame.sf_sc.sc_ecx;
568 regs->tf_rax = frame.sf_sc.sc_eax;
569 regs->tf_rip = frame.sf_sc.sc_eip;
570 regs->tf_cs = frame.sf_sc.sc_cs;
571 regs->tf_ds = frame.sf_sc.sc_ds;
572 regs->tf_es = frame.sf_sc.sc_es;
573 regs->tf_fs = frame.sf_sc.sc_fs;
574 regs->tf_gs = frame.sf_sc.sc_gs;
575 regs->tf_rflags = eflags;
576 regs->tf_rsp = frame.sf_sc.sc_esp_at_signal;
577 regs->tf_ss = frame.sf_sc.sc_ss;
578 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
580 return (EJUSTRETURN);
584 * System call to cleanup state after a signal
585 * has been taken. Reset signal mask and
586 * stack state from context left by rt_sendsig (above).
587 * Return to previous pc and psl as specified by
588 * context left by sendsig. Check carefully to
589 * make sure that the user has not modified the
590 * psl to gain improper privileges or to cause
594 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
596 struct l_ucontext uc;
597 struct l_sigcontext *context;
601 struct trapframe *regs;
608 if (ldebug(rt_sigreturn))
609 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
612 * The trampoline code hands us the ucontext.
613 * It is unsafe to keep track of it ourselves, in the event that a
614 * program jumps out of a signal handler.
616 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
619 context = &uc.uc_mcontext;
621 /* Check for security violations. */
622 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
623 eflags = context->sc_eflags;
624 if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
628 * Don't allow users to load a valid privileged %cs. Let the
629 * hardware check for invalid selectors, excess privilege in
630 * other selectors, invalid %eip's and invalid %esp's.
632 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
633 if (!CS_SECURE(context->sc_cs)) {
634 ksiginfo_init_trap(&ksi);
635 ksi.ksi_signo = SIGBUS;
636 ksi.ksi_code = BUS_OBJERR;
637 ksi.ksi_trapno = T_PROTFLT;
638 ksi.ksi_addr = (void *)regs->tf_rip;
639 trapsignal(td, &ksi);
643 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
644 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
647 * Restore signal context
649 regs->tf_gs = context->sc_gs;
650 regs->tf_fs = context->sc_fs;
651 regs->tf_es = context->sc_es;
652 regs->tf_ds = context->sc_ds;
653 regs->tf_rdi = context->sc_edi;
654 regs->tf_rsi = context->sc_esi;
655 regs->tf_rbp = context->sc_ebp;
656 regs->tf_rbx = context->sc_ebx;
657 regs->tf_rdx = context->sc_edx;
658 regs->tf_rcx = context->sc_ecx;
659 regs->tf_rax = context->sc_eax;
660 regs->tf_rip = context->sc_eip;
661 regs->tf_cs = context->sc_cs;
662 regs->tf_rflags = eflags;
663 regs->tf_rsp = context->sc_esp_at_signal;
664 regs->tf_ss = context->sc_ss;
665 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
668 * call sigaltstack & ignore results..
671 ss.ss_sp = PTRIN(lss->ss_sp);
672 ss.ss_size = lss->ss_size;
673 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
676 if (ldebug(rt_sigreturn))
677 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
678 ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
680 (void)kern_sigaltstack(td, &ss, NULL);
682 return (EJUSTRETURN);
686 linux32_fetch_syscall_args(struct thread *td)
689 struct trapframe *frame;
690 struct syscall_args *sa;
693 frame = td->td_frame;
696 sa->args[0] = frame->tf_rbx;
697 sa->args[1] = frame->tf_rcx;
698 sa->args[2] = frame->tf_rdx;
699 sa->args[3] = frame->tf_rsi;
700 sa->args[4] = frame->tf_rdi;
701 sa->args[5] = frame->tf_rbp; /* Unconfirmed */
702 sa->code = frame->tf_rax;
704 if (sa->code >= p->p_sysent->sv_size)
706 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
708 sa->callp = &p->p_sysent->sv_table[sa->code];
709 sa->narg = sa->callp->sy_narg;
711 td->td_retval[0] = 0;
712 td->td_retval[1] = frame->tf_rdx;
718 * Clear registers on exec
719 * XXX copied from ia32_signal.c.
722 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
724 struct trapframe *regs = td->td_frame;
725 struct pcb *pcb = td->td_pcb;
726 register_t saved_rflags;
731 if (td->td_proc->p_md.md_ldt != NULL)
735 wrmsr(MSR_FSBASE, 0);
736 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */
740 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
742 saved_rflags = regs->tf_rflags & PSL_T;
743 bzero((char *)regs, sizeof(struct trapframe));
744 regs->tf_rip = imgp->entry_addr;
745 regs->tf_rsp = stack;
746 regs->tf_rflags = PSL_USER | saved_rflags;
747 regs->tf_gs = _ugssel;
748 regs->tf_fs = _ufssel;
749 regs->tf_es = _udatasel;
750 regs->tf_ds = _udatasel;
751 regs->tf_ss = _udatasel;
752 regs->tf_flags = TF_HASSEGS;
753 regs->tf_cs = _ucode32sel;
754 regs->tf_rbx = imgp->ps_strings;
758 /* Do full restore on return so that we can change to a different %cs */
759 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
763 * XXX copied from ia32_sysvec.c.
766 linux_copyout_strings(struct image_params *imgp)
770 char *stringp, *destp;
771 u_int32_t *stack_base;
772 struct linux32_ps_strings *arginfo;
773 char canary[LINUX_AT_RANDOM_LEN];
776 /* Calculate string base and vector table pointers. */
777 if (imgp->execpath != NULL && imgp->auxargs != NULL)
778 execpath_len = strlen(imgp->execpath) + 1;
782 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
783 destp = (caddr_t)arginfo - SPARE_USRSPACE -
784 roundup(sizeof(canary), sizeof(char *)) -
785 roundup(execpath_len, sizeof(char *)) -
786 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
788 if (execpath_len != 0) {
789 imgp->execpathp = (uintptr_t)arginfo - execpath_len;
790 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
793 /* Prepare the canary for SSP. */
794 arc4rand(canary, sizeof(canary), 0);
795 imgp->canary = (uintptr_t)arginfo -
796 roundup(execpath_len, sizeof(char *)) -
797 roundup(sizeof(canary), sizeof(char *));
798 copyout(canary, (void *)imgp->canary, sizeof(canary));
800 vectp = (uint32_t *)destp;
803 * Allocate room on the stack for the ELF auxargs
804 * array. It has LINUX_AT_COUNT entries.
806 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
811 * Allocate room for the argv[] and env vectors including the
812 * terminating NULL pointers.
814 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
816 /* vectp also becomes our initial stack base. */
819 stringp = imgp->args->begin_argv;
820 argc = imgp->args->argc;
821 envc = imgp->args->envc;
822 /* Copy out strings - arguments and environment. */
823 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
825 /* Fill in "ps_strings" struct for ps, w, etc. */
826 suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
827 suword32(&arginfo->ps_nargvstr, argc);
829 /* Fill in argument portion of vector table. */
830 for (; argc > 0; --argc) {
831 suword32(vectp++, (uint32_t)(intptr_t)destp);
832 while (*stringp++ != 0)
837 /* A null vector table pointer separates the argp's from the envp's. */
838 suword32(vectp++, 0);
840 suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
841 suword32(&arginfo->ps_nenvstr, envc);
843 /* Fill in environment portion of vector table. */
844 for (; envc > 0; --envc) {
845 suword32(vectp++, (uint32_t)(intptr_t)destp);
846 while (*stringp++ != 0)
851 /* The end of the vector table is a null pointer. */
854 return ((register_t *)stack_base);
857 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
858 "32-bit Linux emulation");
860 static u_long linux32_maxdsiz = LINUX32_MAXDSIZ;
861 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
862 &linux32_maxdsiz, 0, "");
863 static u_long linux32_maxssiz = LINUX32_MAXSSIZ;
864 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
865 &linux32_maxssiz, 0, "");
866 static u_long linux32_maxvmem = LINUX32_MAXVMEM;
867 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
868 &linux32_maxvmem, 0, "");
871 SYSCTL_PROC(_compat_linux32, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
872 linux_sysctl_debug, "A", "Linux debugging control");
876 linux32_fixlimit(struct rlimit *rl, int which)
881 if (linux32_maxdsiz != 0) {
882 if (rl->rlim_cur > linux32_maxdsiz)
883 rl->rlim_cur = linux32_maxdsiz;
884 if (rl->rlim_max > linux32_maxdsiz)
885 rl->rlim_max = linux32_maxdsiz;
889 if (linux32_maxssiz != 0) {
890 if (rl->rlim_cur > linux32_maxssiz)
891 rl->rlim_cur = linux32_maxssiz;
892 if (rl->rlim_max > linux32_maxssiz)
893 rl->rlim_max = linux32_maxssiz;
897 if (linux32_maxvmem != 0) {
898 if (rl->rlim_cur > linux32_maxvmem)
899 rl->rlim_cur = linux32_maxvmem;
900 if (rl->rlim_max > linux32_maxvmem)
901 rl->rlim_max = linux32_maxvmem;
907 struct sysentvec elf_linux_sysvec = {
908 .sv_size = LINUX32_SYS_MAXSYSCALL,
909 .sv_table = linux32_sysent,
911 .sv_errsize = ELAST + 1,
912 .sv_errtbl = linux_errtbl,
913 .sv_transtrap = linux_translate_traps,
914 .sv_fixup = linux_fixup_elf,
915 .sv_sendsig = linux_sendsig,
916 .sv_sigcode = &_binary_linux32_locore_o_start,
917 .sv_szsigcode = &linux_szsigcode,
918 .sv_name = "Linux ELF32",
919 .sv_coredump = elf32_coredump,
920 .sv_imgact_try = linux_exec_imgact_try,
921 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
922 .sv_pagesize = PAGE_SIZE,
923 .sv_minuser = VM_MIN_ADDRESS,
924 .sv_maxuser = LINUX32_MAXUSER,
925 .sv_usrstack = LINUX32_USRSTACK,
926 .sv_psstrings = LINUX32_PS_STRINGS,
927 .sv_stackprot = VM_PROT_ALL,
928 .sv_copyout_strings = linux_copyout_strings,
929 .sv_setregs = linux_exec_setregs,
930 .sv_fixlimit = linux32_fixlimit,
931 .sv_maxssiz = &linux32_maxssiz,
932 .sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
933 .sv_set_syscall_retval = cpu_set_syscall_retval,
934 .sv_fetch_syscall_args = linux32_fetch_syscall_args,
935 .sv_syscallnames = NULL,
936 .sv_shared_page_base = LINUX32_SHAREDPAGE,
937 .sv_shared_page_len = PAGE_SIZE,
938 .sv_schedtail = linux_schedtail,
939 .sv_thread_detach = linux_thread_detach,
944 linux_vdso_install(void *param)
947 linux_szsigcode = (&_binary_linux32_locore_o_end -
948 &_binary_linux32_locore_o_start);
950 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
951 panic("Linux invalid vdso size\n");
953 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
955 linux_shared_page_obj = __elfN(linux_shared_page_init)
956 (&linux_shared_page_mapping);
958 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
960 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
962 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
964 linux_kplatform = linux_shared_page_mapping +
965 (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
967 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
968 linux_vdso_install, NULL);
971 linux_vdso_deinstall(void *param)
974 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
976 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
977 linux_vdso_deinstall, NULL);
979 static char GNU_ABI_VENDOR[] = "GNU";
980 static int GNULINUX_ABI_DESC = 0;
983 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
985 const Elf32_Word *desc;
988 p = (uintptr_t)(note + 1);
989 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
991 desc = (const Elf32_Word *)p;
992 if (desc[0] != GNULINUX_ABI_DESC)
996 * For Linux we encode osrel using the Linux convention of
997 * (version << 16) | (major << 8) | (minor)
998 * See macro in linux_mib.h
1000 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
1005 static Elf_Brandnote linux32_brandnote = {
1006 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
1007 .hdr.n_descsz = 16, /* XXX at least 16 */
1009 .vendor = GNU_ABI_VENDOR,
1010 .flags = BN_TRANSLATE_OSREL,
1011 .trans_osrel = linux32_trans_osrel
1014 static Elf32_Brandinfo linux_brand = {
1015 .brand = ELFOSABI_LINUX,
1017 .compat_3_brand = "Linux",
1018 .emul_path = "/compat/linux",
1019 .interp_path = "/lib/ld-linux.so.1",
1020 .sysvec = &elf_linux_sysvec,
1021 .interp_newpath = NULL,
1022 .brand_note = &linux32_brandnote,
1023 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1026 static Elf32_Brandinfo linux_glibc2brand = {
1027 .brand = ELFOSABI_LINUX,
1029 .compat_3_brand = "Linux",
1030 .emul_path = "/compat/linux",
1031 .interp_path = "/lib/ld-linux.so.2",
1032 .sysvec = &elf_linux_sysvec,
1033 .interp_newpath = NULL,
1034 .brand_note = &linux32_brandnote,
1035 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1038 static Elf32_Brandinfo linux_muslbrand = {
1039 .brand = ELFOSABI_LINUX,
1041 .compat_3_brand = "Linux",
1042 .emul_path = "/compat/linux",
1043 .interp_path = "/lib/ld-musl-i386.so.1",
1044 .sysvec = &elf_linux_sysvec,
1045 .interp_newpath = NULL,
1046 .brand_note = &linux32_brandnote,
1047 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1050 Elf32_Brandinfo *linux_brandlist[] = {
1058 linux_elf_modevent(module_t mod, int type, void *data)
1060 Elf32_Brandinfo **brandinfo;
1062 struct linux_ioctl_handler **lihp;
1068 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1070 if (elf32_insert_brand_entry(*brandinfo) < 0)
1073 SET_FOREACH(lihp, linux_ioctl_handler_set)
1074 linux_ioctl_register_handler(*lihp);
1075 LIST_INIT(&futex_list);
1076 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1077 stclohz = (stathz ? stathz : hz);
1079 printf("Linux ELF exec handler installed\n");
1081 printf("cannot insert Linux ELF brand handler\n");
1084 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1086 if (elf32_brand_inuse(*brandinfo))
1089 for (brandinfo = &linux_brandlist[0];
1090 *brandinfo != NULL; ++brandinfo)
1091 if (elf32_remove_brand_entry(*brandinfo) < 0)
1095 SET_FOREACH(lihp, linux_ioctl_handler_set)
1096 linux_ioctl_unregister_handler(*lihp);
1097 mtx_destroy(&futex_mtx);
1099 printf("Linux ELF exec handler removed\n");
1101 printf("Could not deinstall ELF interpreter entry\n");
1104 return (EOPNOTSUPP);
1109 static moduledata_t linux_elf_mod = {
1115 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1116 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1117 FEATURE(linux, "Linux 32bit support");