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 "opt_compat.h"
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #ifndef COMPAT_FREEBSD32
41 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
44 #define __ELF_WORD_SIZE 32
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/fcntl.h>
50 #include <sys/imgact.h>
51 #include <sys/imgact_elf.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
58 #include <sys/resourcevar.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/sysproto.h>
64 #include <sys/vnode.h>
65 #include <sys/eventhandler.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_param.h>
75 #include <machine/cpu.h>
76 #include <machine/md_var.h>
77 #include <machine/pcb.h>
78 #include <machine/specialreg.h>
79 #include <machine/trap.h>
81 #include <amd64/linux32/linux.h>
82 #include <amd64/linux32/linux32_proto.h>
83 #include <compat/linux/linux_emul.h>
84 #include <compat/linux/linux_futex.h>
85 #include <compat/linux/linux_ioctl.h>
86 #include <compat/linux/linux_mib.h>
87 #include <compat/linux/linux_misc.h>
88 #include <compat/linux/linux_signal.h>
89 #include <compat/linux/linux_util.h>
90 #include <compat/linux/linux_vdso.h>
92 MODULE_VERSION(linux, 1);
95 * Allow the sendsig functions to use the ldebug() facility even though they
96 * are not syscalls themselves. Map them to syscall 0. This is slightly less
97 * bogus than using ldebug(sigreturn).
99 #define LINUX32_SYS_linux_rt_sendsig 0
100 #define LINUX32_SYS_linux_sendsig 0
102 const char *linux_kplatform;
103 static int linux_szsigcode;
104 static vm_object_t linux_shared_page_obj;
105 static char *linux_shared_page_mapping;
106 extern char _binary_linux32_locore_o_start;
107 extern char _binary_linux32_locore_o_end;
109 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
111 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
113 static int linux_fixup_elf(register_t **stack_base,
114 struct image_params *iparams);
115 static register_t *linux_copyout_strings(struct image_params *imgp);
116 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
117 static void linux_exec_setregs(struct thread *td,
118 struct image_params *imgp, u_long stack);
119 static void linux32_fixlimit(struct rlimit *rl, int which);
120 static bool linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
121 static void linux_vdso_install(void *param);
122 static void linux_vdso_deinstall(void *param);
124 #define LINUX_T_UNKNOWN 255
125 static int _bsd_to_linux_trapcode[] = {
126 LINUX_T_UNKNOWN, /* 0 */
127 6, /* 1 T_PRIVINFLT */
128 LINUX_T_UNKNOWN, /* 2 */
130 LINUX_T_UNKNOWN, /* 4 */
131 LINUX_T_UNKNOWN, /* 5 */
132 16, /* 6 T_ARITHTRAP */
133 254, /* 7 T_ASTFLT */
134 LINUX_T_UNKNOWN, /* 8 */
135 13, /* 9 T_PROTFLT */
136 1, /* 10 T_TRCTRAP */
137 LINUX_T_UNKNOWN, /* 11 */
138 14, /* 12 T_PAGEFLT */
139 LINUX_T_UNKNOWN, /* 13 */
140 17, /* 14 T_ALIGNFLT */
141 LINUX_T_UNKNOWN, /* 15 */
142 LINUX_T_UNKNOWN, /* 16 */
143 LINUX_T_UNKNOWN, /* 17 */
149 8, /* 23 T_DOUBLEFLT */
150 9, /* 24 T_FPOPFLT */
151 10, /* 25 T_TSSFLT */
152 11, /* 26 T_SEGNPFLT */
153 12, /* 27 T_STKFLT */
155 19, /* 29 T_XMMFLT */
156 15 /* 30 T_RESERVED */
158 #define bsd_to_linux_trapcode(code) \
159 ((code)<nitems(_bsd_to_linux_trapcode)? \
160 _bsd_to_linux_trapcode[(code)]: \
163 struct linux32_ps_strings {
164 u_int32_t ps_argvstr; /* first of 0 or more argument strings */
165 u_int ps_nargvstr; /* the number of argument strings */
166 u_int32_t ps_envstr; /* first of 0 or more environment strings */
167 u_int ps_nenvstr; /* the number of environment strings */
170 LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
171 LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
172 LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
173 LINUX_VDSO_SYM_CHAR(linux_platform);
176 * If FreeBSD & Linux have a difference of opinion about what a trap
177 * means, deal with it here.
182 linux_translate_traps(int signal, int trap_code)
184 if (signal != SIGBUS)
198 linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
201 Elf32_Auxinfo *argarray, *pos;
202 Elf32_Addr *auxbase, *base;
203 struct linux32_ps_strings *arginfo;
204 int error, issetugid;
206 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
208 KASSERT(curthread->td_proc == imgp->proc,
209 ("unsafe linux_fixup_elf(), should be curproc"));
210 base = (Elf32_Addr *)*stack_base;
211 args = (Elf32_Auxargs *)imgp->auxargs;
212 auxbase = base + (imgp->args->argc + 1 + imgp->args->envc + 1);
213 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
216 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
217 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
218 imgp->proc->p_sysent->sv_shared_page_base);
219 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
220 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
223 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
224 * as it has appeared in the 2.4.0-rc7 first time.
225 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
226 * glibc falls back to the hard-coded CLK_TCK value when aux entry
228 * Also see linux_times() implementation.
230 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
231 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
232 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
233 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
234 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
235 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
236 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
237 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
238 AUXARGS_ENTRY(pos, AT_BASE, args->base);
239 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
240 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
241 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
242 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
243 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
244 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
245 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
246 if (imgp->execpathp != 0)
247 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
248 if (args->execfd != -1)
249 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
250 AUXARGS_ENTRY(pos, AT_NULL, 0);
252 free(imgp->auxargs, M_TEMP);
253 imgp->auxargs = NULL;
254 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
256 error = copyout(&argarray[0], auxbase,
257 sizeof(*argarray) * LINUX_AT_COUNT);
258 free(argarray, M_TEMP);
263 if (suword32(base, (uint32_t)imgp->args->argc) == -1)
265 *stack_base = (register_t *)base;
270 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
272 struct thread *td = curthread;
273 struct proc *p = td->td_proc;
275 struct trapframe *regs;
276 struct l_rt_sigframe *fp, frame;
281 sig = ksi->ksi_signo;
282 code = ksi->ksi_code;
283 PROC_LOCK_ASSERT(p, MA_OWNED);
285 mtx_assert(&psp->ps_mtx, MA_OWNED);
287 oonstack = sigonstack(regs->tf_rsp);
290 if (ldebug(rt_sendsig))
291 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
292 catcher, sig, (void*)mask, code);
294 /* Allocate space for the signal handler context. */
295 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
296 SIGISMEMBER(psp->ps_sigonstack, sig)) {
297 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
298 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
300 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
301 mtx_unlock(&psp->ps_mtx);
303 /* Build the argument list for the signal handler. */
304 sig = bsd_to_linux_signal(sig);
306 bzero(&frame, sizeof(frame));
308 frame.sf_handler = PTROUT(catcher);
310 frame.sf_siginfo = PTROUT(&fp->sf_si);
311 frame.sf_ucontext = PTROUT(&fp->sf_sc);
313 /* Fill in POSIX parts. */
314 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
317 * Build the signal context to be used by sigreturn and libgcc unwind.
319 frame.sf_sc.uc_flags = 0; /* XXX ??? */
320 frame.sf_sc.uc_link = 0; /* XXX ??? */
322 frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
323 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
324 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
325 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
328 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
330 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
331 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_rdi;
332 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_rsi;
333 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_rbp;
334 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_rbx;
335 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_rsp;
336 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_rdx;
337 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_rcx;
338 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_rax;
339 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_rip;
340 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
341 frame.sf_sc.uc_mcontext.sc_gs = regs->tf_gs;
342 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
343 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
344 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
345 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
346 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
347 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
348 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
349 frame.sf_sc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
350 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
353 if (ldebug(rt_sendsig))
354 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
355 frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
356 td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
359 if (copyout(&frame, fp, sizeof(frame)) != 0) {
361 * Process has trashed its stack; give it an illegal
362 * instruction to halt it in its tracks.
365 if (ldebug(rt_sendsig))
366 printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
373 /* Build context to run handler in. */
374 regs->tf_rsp = PTROUT(fp);
375 regs->tf_rip = linux32_rt_sigcode;
376 regs->tf_rflags &= ~(PSL_T | PSL_D);
377 regs->tf_cs = _ucode32sel;
378 regs->tf_ss = _udatasel;
379 regs->tf_ds = _udatasel;
380 regs->tf_es = _udatasel;
381 regs->tf_fs = _ufssel;
382 regs->tf_gs = _ugssel;
383 regs->tf_flags = TF_HASSEGS;
384 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
386 mtx_lock(&psp->ps_mtx);
391 * Send an interrupt to process.
393 * Stack is set up to allow sigcode stored
394 * in u. to call routine, followed by kcall
395 * to sigreturn routine below. After sigreturn
396 * resets the signal mask, the stack, and the
397 * frame pointer, it returns to the user
401 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
403 struct thread *td = curthread;
404 struct proc *p = td->td_proc;
406 struct trapframe *regs;
407 struct l_sigframe *fp, frame;
412 sig = ksi->ksi_signo;
413 code = ksi->ksi_code;
414 PROC_LOCK_ASSERT(p, MA_OWNED);
416 mtx_assert(&psp->ps_mtx, MA_OWNED);
417 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
418 /* Signal handler installed with SA_SIGINFO. */
419 linux_rt_sendsig(catcher, ksi, mask);
424 oonstack = sigonstack(regs->tf_rsp);
428 printf(ARGS(sendsig, "%p, %d, %p, %u"),
429 catcher, sig, (void*)mask, code);
432 /* Allocate space for the signal handler context. */
433 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
434 SIGISMEMBER(psp->ps_sigonstack, sig)) {
435 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
436 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
438 fp = (struct l_sigframe *)regs->tf_rsp - 1;
439 mtx_unlock(&psp->ps_mtx);
442 /* Build the argument list for the signal handler. */
443 sig = bsd_to_linux_signal(sig);
445 bzero(&frame, sizeof(frame));
447 frame.sf_handler = PTROUT(catcher);
450 bsd_to_linux_sigset(mask, &lmask);
452 /* Build the signal context to be used by sigreturn. */
453 frame.sf_sc.sc_mask = lmask.__mask;
454 frame.sf_sc.sc_gs = regs->tf_gs;
455 frame.sf_sc.sc_fs = regs->tf_fs;
456 frame.sf_sc.sc_es = regs->tf_es;
457 frame.sf_sc.sc_ds = regs->tf_ds;
458 frame.sf_sc.sc_edi = regs->tf_rdi;
459 frame.sf_sc.sc_esi = regs->tf_rsi;
460 frame.sf_sc.sc_ebp = regs->tf_rbp;
461 frame.sf_sc.sc_ebx = regs->tf_rbx;
462 frame.sf_sc.sc_esp = regs->tf_rsp;
463 frame.sf_sc.sc_edx = regs->tf_rdx;
464 frame.sf_sc.sc_ecx = regs->tf_rcx;
465 frame.sf_sc.sc_eax = regs->tf_rax;
466 frame.sf_sc.sc_eip = regs->tf_rip;
467 frame.sf_sc.sc_cs = regs->tf_cs;
468 frame.sf_sc.sc_eflags = regs->tf_rflags;
469 frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
470 frame.sf_sc.sc_ss = regs->tf_ss;
471 frame.sf_sc.sc_err = regs->tf_err;
472 frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
473 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
475 frame.sf_extramask[0] = lmask.__mask;
477 if (copyout(&frame, fp, sizeof(frame)) != 0) {
479 * Process has trashed its stack; give it an illegal
480 * instruction to halt it in its tracks.
486 /* Build context to run handler in. */
487 regs->tf_rsp = PTROUT(fp);
488 regs->tf_rip = linux32_sigcode;
489 regs->tf_rflags &= ~(PSL_T | PSL_D);
490 regs->tf_cs = _ucode32sel;
491 regs->tf_ss = _udatasel;
492 regs->tf_ds = _udatasel;
493 regs->tf_es = _udatasel;
494 regs->tf_fs = _ufssel;
495 regs->tf_gs = _ugssel;
496 regs->tf_flags = TF_HASSEGS;
497 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
499 mtx_lock(&psp->ps_mtx);
503 * System call to cleanup state after a signal
504 * has been taken. Reset signal mask and
505 * stack state from context left by sendsig (above).
506 * Return to previous pc and psl as specified by
507 * context left by sendsig. Check carefully to
508 * make sure that the user has not modified the
509 * psl to gain improper privileges or to cause
513 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
515 struct l_sigframe frame;
516 struct trapframe *regs;
525 if (ldebug(sigreturn))
526 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
529 * The trampoline code hands us the sigframe.
530 * It is unsafe to keep track of it ourselves, in the event that a
531 * program jumps out of a signal handler.
533 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
536 /* Check for security violations. */
537 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
538 eflags = frame.sf_sc.sc_eflags;
539 if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
543 * Don't allow users to load a valid privileged %cs. Let the
544 * hardware check for invalid selectors, excess privilege in
545 * other selectors, invalid %eip's and invalid %esp's.
547 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
548 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
549 ksiginfo_init_trap(&ksi);
550 ksi.ksi_signo = SIGBUS;
551 ksi.ksi_code = BUS_OBJERR;
552 ksi.ksi_trapno = T_PROTFLT;
553 ksi.ksi_addr = (void *)regs->tf_rip;
554 trapsignal(td, &ksi);
558 lmask.__mask = frame.sf_sc.sc_mask;
559 lmask.__mask = frame.sf_extramask[0];
560 linux_to_bsd_sigset(&lmask, &bmask);
561 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
563 /* Restore signal context. */
564 regs->tf_rdi = frame.sf_sc.sc_edi;
565 regs->tf_rsi = frame.sf_sc.sc_esi;
566 regs->tf_rbp = frame.sf_sc.sc_ebp;
567 regs->tf_rbx = frame.sf_sc.sc_ebx;
568 regs->tf_rdx = frame.sf_sc.sc_edx;
569 regs->tf_rcx = frame.sf_sc.sc_ecx;
570 regs->tf_rax = frame.sf_sc.sc_eax;
571 regs->tf_rip = frame.sf_sc.sc_eip;
572 regs->tf_cs = frame.sf_sc.sc_cs;
573 regs->tf_ds = frame.sf_sc.sc_ds;
574 regs->tf_es = frame.sf_sc.sc_es;
575 regs->tf_fs = frame.sf_sc.sc_fs;
576 regs->tf_gs = frame.sf_sc.sc_gs;
577 regs->tf_rflags = eflags;
578 regs->tf_rsp = frame.sf_sc.sc_esp_at_signal;
579 regs->tf_ss = frame.sf_sc.sc_ss;
580 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
582 return (EJUSTRETURN);
586 * System call to cleanup state after a signal
587 * has been taken. Reset signal mask and
588 * stack state from context left by rt_sendsig (above).
589 * Return to previous pc and psl as specified by
590 * context left by sendsig. Check carefully to
591 * make sure that the user has not modified the
592 * psl to gain improper privileges or to cause
596 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
598 struct l_ucontext uc;
599 struct l_sigcontext *context;
603 struct trapframe *regs;
610 if (ldebug(rt_sigreturn))
611 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
614 * The trampoline code hands us the ucontext.
615 * It is unsafe to keep track of it ourselves, in the event that a
616 * program jumps out of a signal handler.
618 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
621 context = &uc.uc_mcontext;
623 /* Check for security violations. */
624 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
625 eflags = context->sc_eflags;
626 if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
630 * Don't allow users to load a valid privileged %cs. Let the
631 * hardware check for invalid selectors, excess privilege in
632 * other selectors, invalid %eip's and invalid %esp's.
634 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
635 if (!CS_SECURE(context->sc_cs)) {
636 ksiginfo_init_trap(&ksi);
637 ksi.ksi_signo = SIGBUS;
638 ksi.ksi_code = BUS_OBJERR;
639 ksi.ksi_trapno = T_PROTFLT;
640 ksi.ksi_addr = (void *)regs->tf_rip;
641 trapsignal(td, &ksi);
645 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
646 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
649 * Restore signal context
651 regs->tf_gs = context->sc_gs;
652 regs->tf_fs = context->sc_fs;
653 regs->tf_es = context->sc_es;
654 regs->tf_ds = context->sc_ds;
655 regs->tf_rdi = context->sc_edi;
656 regs->tf_rsi = context->sc_esi;
657 regs->tf_rbp = context->sc_ebp;
658 regs->tf_rbx = context->sc_ebx;
659 regs->tf_rdx = context->sc_edx;
660 regs->tf_rcx = context->sc_ecx;
661 regs->tf_rax = context->sc_eax;
662 regs->tf_rip = context->sc_eip;
663 regs->tf_cs = context->sc_cs;
664 regs->tf_rflags = eflags;
665 regs->tf_rsp = context->sc_esp_at_signal;
666 regs->tf_ss = context->sc_ss;
667 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
670 * call sigaltstack & ignore results..
673 ss.ss_sp = PTRIN(lss->ss_sp);
674 ss.ss_size = lss->ss_size;
675 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
678 if (ldebug(rt_sigreturn))
679 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
680 ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
682 (void)kern_sigaltstack(td, &ss, NULL);
684 return (EJUSTRETURN);
688 linux32_fetch_syscall_args(struct thread *td)
691 struct trapframe *frame;
692 struct syscall_args *sa;
695 frame = td->td_frame;
698 sa->args[0] = frame->tf_rbx;
699 sa->args[1] = frame->tf_rcx;
700 sa->args[2] = frame->tf_rdx;
701 sa->args[3] = frame->tf_rsi;
702 sa->args[4] = frame->tf_rdi;
703 sa->args[5] = frame->tf_rbp; /* Unconfirmed */
704 sa->code = frame->tf_rax;
706 if (sa->code >= p->p_sysent->sv_size)
708 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
710 sa->callp = &p->p_sysent->sv_table[sa->code];
711 sa->narg = sa->callp->sy_narg;
713 td->td_retval[0] = 0;
714 td->td_retval[1] = frame->tf_rdx;
720 * Clear registers on exec
721 * XXX copied from ia32_signal.c.
724 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
726 struct trapframe *regs = td->td_frame;
727 struct pcb *pcb = td->td_pcb;
728 register_t saved_rflags;
733 if (td->td_proc->p_md.md_ldt != NULL)
737 wrmsr(MSR_FSBASE, 0);
738 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */
742 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
744 saved_rflags = regs->tf_rflags & PSL_T;
745 bzero((char *)regs, sizeof(struct trapframe));
746 regs->tf_rip = imgp->entry_addr;
747 regs->tf_rsp = stack;
748 regs->tf_rflags = PSL_USER | saved_rflags;
749 regs->tf_gs = _ugssel;
750 regs->tf_fs = _ufssel;
751 regs->tf_es = _udatasel;
752 regs->tf_ds = _udatasel;
753 regs->tf_ss = _udatasel;
754 regs->tf_flags = TF_HASSEGS;
755 regs->tf_cs = _ucode32sel;
756 regs->tf_rbx = imgp->ps_strings;
760 /* Do full restore on return so that we can change to a different %cs */
761 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
765 * XXX copied from ia32_sysvec.c.
768 linux_copyout_strings(struct image_params *imgp)
772 char *stringp, *destp;
773 u_int32_t *stack_base;
774 struct linux32_ps_strings *arginfo;
775 char canary[LINUX_AT_RANDOM_LEN];
778 /* Calculate string base and vector table pointers. */
779 if (imgp->execpath != NULL && imgp->auxargs != NULL)
780 execpath_len = strlen(imgp->execpath) + 1;
784 arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
785 destp = (caddr_t)arginfo - SPARE_USRSPACE -
786 roundup(sizeof(canary), sizeof(char *)) -
787 roundup(execpath_len, sizeof(char *)) -
788 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
790 if (execpath_len != 0) {
791 imgp->execpathp = (uintptr_t)arginfo - execpath_len;
792 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
795 /* Prepare the canary for SSP. */
796 arc4rand(canary, sizeof(canary), 0);
797 imgp->canary = (uintptr_t)arginfo -
798 roundup(execpath_len, sizeof(char *)) -
799 roundup(sizeof(canary), sizeof(char *));
800 copyout(canary, (void *)imgp->canary, sizeof(canary));
802 vectp = (uint32_t *)destp;
805 * Allocate room on the stack for the ELF auxargs
806 * array. It has LINUX_AT_COUNT entries.
808 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
813 * Allocate room for the argv[] and env vectors including the
814 * terminating NULL pointers.
816 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
818 /* vectp also becomes our initial stack base. */
821 stringp = imgp->args->begin_argv;
822 argc = imgp->args->argc;
823 envc = imgp->args->envc;
824 /* Copy out strings - arguments and environment. */
825 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
827 /* Fill in "ps_strings" struct for ps, w, etc. */
828 suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
829 suword32(&arginfo->ps_nargvstr, argc);
831 /* Fill in argument portion of vector table. */
832 for (; argc > 0; --argc) {
833 suword32(vectp++, (uint32_t)(intptr_t)destp);
834 while (*stringp++ != 0)
839 /* A null vector table pointer separates the argp's from the envp's. */
840 suword32(vectp++, 0);
842 suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
843 suword32(&arginfo->ps_nenvstr, envc);
845 /* Fill in environment portion of vector table. */
846 for (; envc > 0; --envc) {
847 suword32(vectp++, (uint32_t)(intptr_t)destp);
848 while (*stringp++ != 0)
853 /* The end of the vector table is a null pointer. */
856 return ((register_t *)stack_base);
859 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
860 "32-bit Linux emulation");
862 static u_long linux32_maxdsiz = LINUX32_MAXDSIZ;
863 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
864 &linux32_maxdsiz, 0, "");
865 static u_long linux32_maxssiz = LINUX32_MAXSSIZ;
866 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
867 &linux32_maxssiz, 0, "");
868 static u_long linux32_maxvmem = LINUX32_MAXVMEM;
869 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
870 &linux32_maxvmem, 0, "");
873 SYSCTL_PROC(_compat_linux32, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
874 linux_sysctl_debug, "A", "Linux debugging control");
878 linux32_fixlimit(struct rlimit *rl, int which)
883 if (linux32_maxdsiz != 0) {
884 if (rl->rlim_cur > linux32_maxdsiz)
885 rl->rlim_cur = linux32_maxdsiz;
886 if (rl->rlim_max > linux32_maxdsiz)
887 rl->rlim_max = linux32_maxdsiz;
891 if (linux32_maxssiz != 0) {
892 if (rl->rlim_cur > linux32_maxssiz)
893 rl->rlim_cur = linux32_maxssiz;
894 if (rl->rlim_max > linux32_maxssiz)
895 rl->rlim_max = linux32_maxssiz;
899 if (linux32_maxvmem != 0) {
900 if (rl->rlim_cur > linux32_maxvmem)
901 rl->rlim_cur = linux32_maxvmem;
902 if (rl->rlim_max > linux32_maxvmem)
903 rl->rlim_max = linux32_maxvmem;
909 struct sysentvec elf_linux_sysvec = {
910 .sv_size = LINUX32_SYS_MAXSYSCALL,
911 .sv_table = linux32_sysent,
912 .sv_errsize = ELAST + 1,
913 .sv_errtbl = linux_errtbl,
914 .sv_transtrap = linux_translate_traps,
915 .sv_fixup = linux_fixup_elf,
916 .sv_sendsig = linux_sendsig,
917 .sv_sigcode = &_binary_linux32_locore_o_start,
918 .sv_szsigcode = &linux_szsigcode,
919 .sv_name = "Linux ELF32",
920 .sv_coredump = elf32_coredump,
921 .sv_imgact_try = linux_exec_imgact_try,
922 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
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 linux32_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 linux32_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");