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/stddef.h>
60 #include <sys/signalvar.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/sysproto.h>
65 #include <sys/vnode.h>
66 #include <sys/eventhandler.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_param.h>
76 #include <machine/cpu.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
79 #include <machine/specialreg.h>
80 #include <machine/trap.h>
82 #include <x86/linux/linux_x86.h>
83 #include <amd64/linux32/linux.h>
84 #include <amd64/linux32/linux32_proto.h>
85 #include <compat/linux/linux_elf.h>
86 #include <compat/linux/linux_emul.h>
87 #include <compat/linux/linux_fork.h>
88 #include <compat/linux/linux_ioctl.h>
89 #include <compat/linux/linux_mib.h>
90 #include <compat/linux/linux_misc.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_util.h>
93 #include <compat/linux/linux_vdso.h>
95 #include <x86/linux/linux_x86_sigframe.h>
97 MODULE_VERSION(linux, 1);
99 #define LINUX32_MAXUSER ((1ul << 32) - PAGE_SIZE)
100 #define LINUX32_VDSOPAGE_SIZE PAGE_SIZE * 2
101 #define LINUX32_VDSOPAGE (LINUX32_MAXUSER - LINUX32_VDSOPAGE_SIZE)
102 #define LINUX32_SHAREDPAGE (LINUX32_VDSOPAGE - PAGE_SIZE)
104 * PAGE_SIZE - the size
105 * of the native SHAREDPAGE
107 #define LINUX32_USRSTACK LINUX32_SHAREDPAGE
109 static int linux_szsigcode;
110 static vm_object_t linux_vdso_obj;
111 static char *linux_vdso_mapping;
112 extern char _binary_linux32_vdso_so_o_start;
113 extern char _binary_linux32_vdso_so_o_end;
114 static vm_offset_t linux_vdso_base;
116 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
117 extern const char *linux32_syscallnames[];
119 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
121 static int linux_copyout_strings(struct image_params *imgp,
122 uintptr_t *stack_base);
123 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
124 static void linux_exec_setregs(struct thread *td,
125 struct image_params *imgp, uintptr_t stack);
126 static void linux_exec_sysvec_init(void *param);
127 static int linux_on_exec_vmspace(struct proc *p,
128 struct image_params *imgp);
129 static void linux32_fixlimit(struct rlimit *rl, int which);
130 static bool linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
131 static void linux_vdso_install(const void *param);
132 static void linux_vdso_deinstall(const void *param);
133 static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
134 static void linux32_set_fork_retval(struct thread *td);
135 static void linux32_set_syscall_retval(struct thread *td, int error);
137 struct linux32_ps_strings {
138 u_int32_t ps_argvstr; /* first of 0 or more argument strings */
139 u_int ps_nargvstr; /* the number of argument strings */
140 u_int32_t ps_envstr; /* first of 0 or more environment strings */
141 u_int ps_nenvstr; /* the number of environment strings */
143 #define LINUX32_PS_STRINGS (LINUX32_USRSTACK - \
144 sizeof(struct linux32_ps_strings))
146 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
147 LINUX_VDSO_SYM_INTPTR(linux32_vdso_sigcode);
148 LINUX_VDSO_SYM_INTPTR(linux32_vdso_rt_sigcode);
149 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
150 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
151 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
152 LINUX_VDSO_SYM_CHAR(linux_platform);
155 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
158 Elf32_Auxinfo *argarray, *pos;
159 int error, issetugid;
161 args = (Elf32_Auxargs *)imgp->auxargs;
162 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
165 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
166 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
167 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
168 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
169 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
172 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
173 * as it has appeared in the 2.4.0-rc7 first time.
174 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
175 * glibc falls back to the hard-coded CLK_TCK value when aux entry
177 * Also see linux_times() implementation.
179 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
180 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
181 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
182 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
183 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
184 AUXARGS_ENTRY(pos, AT_BASE, args->base);
185 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
186 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
187 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
188 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
189 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
190 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
191 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
192 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
193 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
194 if (imgp->execpathp != 0)
195 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
196 if (args->execfd != -1)
197 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
198 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
199 AUXARGS_ENTRY(pos, AT_NULL, 0);
201 free(imgp->auxargs, M_TEMP);
202 imgp->auxargs = NULL;
203 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
205 error = copyout(argarray, (void *)base,
206 sizeof(*argarray) * LINUX_AT_COUNT);
207 free(argarray, M_TEMP);
212 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
214 struct thread *td = curthread;
215 struct proc *p = td->td_proc;
217 struct trapframe *regs;
218 struct l_rt_sigframe *fp, frame;
223 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
224 code = ksi->ksi_code;
225 PROC_LOCK_ASSERT(p, MA_OWNED);
227 mtx_assert(&psp->ps_mtx, MA_OWNED);
229 oonstack = sigonstack(regs->tf_rsp);
231 /* Allocate space for the signal handler context. */
232 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
233 SIGISMEMBER(psp->ps_sigonstack, sig)) {
234 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
235 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
237 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
238 mtx_unlock(&psp->ps_mtx);
240 /* Build the argument list for the signal handler. */
241 sig = bsd_to_linux_signal(sig);
243 bzero(&frame, sizeof(frame));
246 frame.sf_siginfo = PTROUT(&fp->sf_si);
247 frame.sf_ucontext = PTROUT(&fp->sf_uc);
249 /* Fill in POSIX parts. */
250 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
253 * Build the signal context to be used by sigreturn and libgcc unwind.
255 frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
256 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
257 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
258 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
261 bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
263 frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask;
264 frame.sf_uc.uc_mcontext.sc_edi = regs->tf_rdi;
265 frame.sf_uc.uc_mcontext.sc_esi = regs->tf_rsi;
266 frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_rbp;
267 frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_rbx;
268 frame.sf_uc.uc_mcontext.sc_esp = regs->tf_rsp;
269 frame.sf_uc.uc_mcontext.sc_edx = regs->tf_rdx;
270 frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_rcx;
271 frame.sf_uc.uc_mcontext.sc_eax = regs->tf_rax;
272 frame.sf_uc.uc_mcontext.sc_eip = regs->tf_rip;
273 frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;
274 frame.sf_uc.uc_mcontext.sc_gs = regs->tf_gs;
275 frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs;
276 frame.sf_uc.uc_mcontext.sc_es = regs->tf_es;
277 frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds;
278 frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_rflags;
279 frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
280 frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss;
281 frame.sf_uc.uc_mcontext.sc_err = regs->tf_err;
282 frame.sf_uc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
283 frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
285 if (copyout(&frame, fp, sizeof(frame)) != 0) {
287 * Process has trashed its stack; give it an illegal
288 * instruction to halt it in its tracks.
294 /* Build context to run handler in. */
295 regs->tf_rsp = PTROUT(fp);
296 regs->tf_rip = linux32_vdso_rt_sigcode;
297 regs->tf_rdi = PTROUT(catcher);
298 regs->tf_rflags &= ~(PSL_T | PSL_D);
299 regs->tf_cs = _ucode32sel;
300 regs->tf_ss = _udatasel;
301 regs->tf_ds = _udatasel;
302 regs->tf_es = _udatasel;
303 regs->tf_fs = _ufssel;
304 regs->tf_gs = _ugssel;
305 regs->tf_flags = TF_HASSEGS;
306 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
308 mtx_lock(&psp->ps_mtx);
312 * Send an interrupt to process.
314 * Stack is set up to allow sigcode stored
315 * in u. to call routine, followed by kcall
316 * to sigreturn routine below. After sigreturn
317 * resets the signal mask, the stack, and the
318 * frame pointer, it returns to the user
322 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
324 struct thread *td = curthread;
325 struct proc *p = td->td_proc;
327 struct trapframe *regs;
328 struct l_sigframe *fp, frame;
333 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
334 code = ksi->ksi_code;
335 PROC_LOCK_ASSERT(p, MA_OWNED);
337 mtx_assert(&psp->ps_mtx, MA_OWNED);
338 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
339 /* Signal handler installed with SA_SIGINFO. */
340 linux_rt_sendsig(catcher, ksi, mask);
345 oonstack = sigonstack(regs->tf_rsp);
347 /* Allocate space for the signal handler context. */
348 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
349 SIGISMEMBER(psp->ps_sigonstack, sig)) {
350 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
351 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
353 fp = (struct l_sigframe *)regs->tf_rsp - 1;
354 mtx_unlock(&psp->ps_mtx);
357 /* Build the argument list for the signal handler. */
358 sig = bsd_to_linux_signal(sig);
360 bzero(&frame, sizeof(frame));
363 frame.sf_sigmask = *mask;
364 bsd_to_linux_sigset(mask, &lmask);
366 /* Build the signal context to be used by sigreturn. */
367 frame.sf_sc.sc_mask = lmask.__mask;
368 frame.sf_sc.sc_gs = regs->tf_gs;
369 frame.sf_sc.sc_fs = regs->tf_fs;
370 frame.sf_sc.sc_es = regs->tf_es;
371 frame.sf_sc.sc_ds = regs->tf_ds;
372 frame.sf_sc.sc_edi = regs->tf_rdi;
373 frame.sf_sc.sc_esi = regs->tf_rsi;
374 frame.sf_sc.sc_ebp = regs->tf_rbp;
375 frame.sf_sc.sc_ebx = regs->tf_rbx;
376 frame.sf_sc.sc_esp = regs->tf_rsp;
377 frame.sf_sc.sc_edx = regs->tf_rdx;
378 frame.sf_sc.sc_ecx = regs->tf_rcx;
379 frame.sf_sc.sc_eax = regs->tf_rax;
380 frame.sf_sc.sc_eip = regs->tf_rip;
381 frame.sf_sc.sc_cs = regs->tf_cs;
382 frame.sf_sc.sc_eflags = regs->tf_rflags;
383 frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
384 frame.sf_sc.sc_ss = regs->tf_ss;
385 frame.sf_sc.sc_err = regs->tf_err;
386 frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;
387 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
389 if (copyout(&frame, fp, sizeof(frame)) != 0) {
391 * Process has trashed its stack; give it an illegal
392 * instruction to halt it in its tracks.
398 /* Build context to run handler in. */
399 regs->tf_rsp = PTROUT(fp);
400 regs->tf_rip = linux32_vdso_sigcode;
401 regs->tf_rdi = PTROUT(catcher);
402 regs->tf_rflags &= ~(PSL_T | PSL_D);
403 regs->tf_cs = _ucode32sel;
404 regs->tf_ss = _udatasel;
405 regs->tf_ds = _udatasel;
406 regs->tf_es = _udatasel;
407 regs->tf_fs = _ufssel;
408 regs->tf_gs = _ugssel;
409 regs->tf_flags = TF_HASSEGS;
410 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
412 mtx_lock(&psp->ps_mtx);
416 * System call to cleanup state after a signal
417 * has been taken. Reset signal mask and
418 * stack state from context left by sendsig (above).
419 * Return to previous pc and psl as specified by
420 * context left by sendsig. Check carefully to
421 * make sure that the user has not modified the
422 * psl to gain improper privileges or to cause
426 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
428 struct l_sigframe frame;
429 struct trapframe *regs;
436 * The trampoline code hands us the sigframe.
437 * It is unsafe to keep track of it ourselves, in the event that a
438 * program jumps out of a signal handler.
440 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
443 /* Check for security violations. */
444 eflags = frame.sf_sc.sc_eflags;
445 if (!EFL_SECURE(eflags, regs->tf_rflags))
449 * Don't allow users to load a valid privileged %cs. Let the
450 * hardware check for invalid selectors, excess privilege in
451 * other selectors, invalid %eip's and invalid %esp's.
453 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
454 ksiginfo_init_trap(&ksi);
455 ksi.ksi_signo = SIGBUS;
456 ksi.ksi_code = BUS_OBJERR;
457 ksi.ksi_trapno = T_PROTFLT;
458 ksi.ksi_addr = (void *)regs->tf_rip;
459 trapsignal(td, &ksi);
463 kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
465 /* Restore signal context. */
466 regs->tf_rdi = frame.sf_sc.sc_edi;
467 regs->tf_rsi = frame.sf_sc.sc_esi;
468 regs->tf_rbp = frame.sf_sc.sc_ebp;
469 regs->tf_rbx = frame.sf_sc.sc_ebx;
470 regs->tf_rdx = frame.sf_sc.sc_edx;
471 regs->tf_rcx = frame.sf_sc.sc_ecx;
472 regs->tf_rax = frame.sf_sc.sc_eax;
473 regs->tf_rip = frame.sf_sc.sc_eip;
474 regs->tf_cs = frame.sf_sc.sc_cs;
475 regs->tf_ds = frame.sf_sc.sc_ds;
476 regs->tf_es = frame.sf_sc.sc_es;
477 regs->tf_fs = frame.sf_sc.sc_fs;
478 regs->tf_gs = frame.sf_sc.sc_gs;
479 regs->tf_rflags = eflags;
480 regs->tf_rsp = frame.sf_sc.sc_esp_at_signal;
481 regs->tf_ss = frame.sf_sc.sc_ss;
482 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
484 return (EJUSTRETURN);
488 * System call to cleanup state after a signal
489 * has been taken. Reset signal mask and
490 * stack state from context left by rt_sendsig (above).
491 * Return to previous pc and psl as specified by
492 * context left by sendsig. Check carefully to
493 * make sure that the user has not modified the
494 * psl to gain improper privileges or to cause
498 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
500 struct l_ucontext uc;
501 struct l_sigcontext *context;
505 struct trapframe *regs;
512 * The trampoline code hands us the ucontext.
513 * It is unsafe to keep track of it ourselves, in the event that a
514 * program jumps out of a signal handler.
516 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
519 context = &uc.uc_mcontext;
521 /* Check for security violations. */
522 eflags = context->sc_eflags;
523 if (!EFL_SECURE(eflags, regs->tf_rflags))
527 * Don't allow users to load a valid privileged %cs. Let the
528 * hardware check for invalid selectors, excess privilege in
529 * other selectors, invalid %eip's and invalid %esp's.
531 if (!CS_SECURE(context->sc_cs)) {
532 ksiginfo_init_trap(&ksi);
533 ksi.ksi_signo = SIGBUS;
534 ksi.ksi_code = BUS_OBJERR;
535 ksi.ksi_trapno = T_PROTFLT;
536 ksi.ksi_addr = (void *)regs->tf_rip;
537 trapsignal(td, &ksi);
541 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
542 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
545 * Restore signal context
547 regs->tf_gs = context->sc_gs;
548 regs->tf_fs = context->sc_fs;
549 regs->tf_es = context->sc_es;
550 regs->tf_ds = context->sc_ds;
551 regs->tf_rdi = context->sc_edi;
552 regs->tf_rsi = context->sc_esi;
553 regs->tf_rbp = context->sc_ebp;
554 regs->tf_rbx = context->sc_ebx;
555 regs->tf_rdx = context->sc_edx;
556 regs->tf_rcx = context->sc_ecx;
557 regs->tf_rax = context->sc_eax;
558 regs->tf_rip = context->sc_eip;
559 regs->tf_cs = context->sc_cs;
560 regs->tf_rflags = eflags;
561 regs->tf_rsp = context->sc_esp_at_signal;
562 regs->tf_ss = context->sc_ss;
563 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
566 * call sigaltstack & ignore results..
569 ss.ss_sp = PTRIN(lss->ss_sp);
570 ss.ss_size = lss->ss_size;
571 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
573 (void)kern_sigaltstack(td, &ss, NULL);
575 return (EJUSTRETURN);
579 linux32_fetch_syscall_args(struct thread *td)
582 struct trapframe *frame;
583 struct syscall_args *sa;
586 frame = td->td_frame;
589 sa->args[0] = frame->tf_rbx;
590 sa->args[1] = frame->tf_rcx;
591 sa->args[2] = frame->tf_rdx;
592 sa->args[3] = frame->tf_rsi;
593 sa->args[4] = frame->tf_rdi;
594 sa->args[5] = frame->tf_rbp; /* Unconfirmed */
595 sa->code = frame->tf_rax;
596 sa->original_code = sa->code;
598 if (sa->code >= p->p_sysent->sv_size)
600 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
602 sa->callp = &p->p_sysent->sv_table[sa->code];
604 td->td_retval[0] = 0;
605 td->td_retval[1] = frame->tf_rdx;
611 linux32_set_syscall_retval(struct thread *td, int error)
613 struct trapframe *frame = td->td_frame;
615 cpu_set_syscall_retval(td, error);
617 if (__predict_false(error != 0)) {
618 if (error != ERESTART && error != EJUSTRETURN)
619 frame->tf_rax = bsd_to_linux_errno(error);
624 linux32_set_fork_retval(struct thread *td)
626 struct trapframe *frame = td->td_frame;
632 * Clear registers on exec
633 * XXX copied from ia32_signal.c.
636 linux_exec_setregs(struct thread *td, struct image_params *imgp,
639 struct trapframe *regs = td->td_frame;
640 struct pcb *pcb = td->td_pcb;
641 register_t saved_rflags;
646 if (td->td_proc->p_md.md_ldt != NULL)
650 wrmsr(MSR_FSBASE, 0);
651 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */
655 pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
657 saved_rflags = regs->tf_rflags & PSL_T;
658 bzero((char *)regs, sizeof(struct trapframe));
659 regs->tf_rip = imgp->entry_addr;
660 regs->tf_rsp = stack;
661 regs->tf_rflags = PSL_USER | saved_rflags;
662 regs->tf_gs = _ugssel;
663 regs->tf_fs = _ufssel;
664 regs->tf_es = _udatasel;
665 regs->tf_ds = _udatasel;
666 regs->tf_ss = _udatasel;
667 regs->tf_flags = TF_HASSEGS;
668 regs->tf_cs = _ucode32sel;
669 regs->tf_rbx = (register_t)imgp->ps_strings;
671 x86_clear_dbregs(pcb);
675 /* Do full restore on return so that we can change to a different %cs */
676 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
680 * XXX copied from ia32_sysvec.c.
683 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
685 int argc, envc, error;
688 uintptr_t destp, ustringp;
689 struct linux32_ps_strings *arginfo;
690 char canary[LINUX_AT_RANDOM_LEN];
693 arginfo = (struct linux32_ps_strings *)PROC_PS_STRINGS(imgp->proc);
694 destp = (uintptr_t)arginfo;
696 if (imgp->execpath != NULL && imgp->auxargs != NULL) {
697 execpath_len = strlen(imgp->execpath) + 1;
698 destp -= execpath_len;
699 destp = rounddown2(destp, sizeof(uint32_t));
700 imgp->execpathp = (void *)destp;
701 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
706 /* Prepare the canary for SSP. */
707 arc4rand(canary, sizeof(canary), 0);
708 destp -= roundup(sizeof(canary), sizeof(uint32_t));
709 imgp->canary = (void *)destp;
710 error = copyout(canary, imgp->canary, sizeof(canary));
714 /* Allocate room for the argument and environment strings. */
715 destp -= ARG_MAX - imgp->args->stringspace;
716 destp = rounddown2(destp, sizeof(uint32_t));
721 * Allocate room on the stack for the ELF auxargs
722 * array. It has LINUX_AT_COUNT entries.
724 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
725 destp = rounddown2(destp, sizeof(uint32_t));
728 vectp = (uint32_t *)destp;
731 * Allocate room for the argv[] and env vectors including the
732 * terminating NULL pointers.
734 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
736 /* vectp also becomes our initial stack base. */
737 *stack_base = (uintptr_t)vectp;
739 stringp = imgp->args->begin_argv;
740 argc = imgp->args->argc;
741 envc = imgp->args->envc;
743 /* Copy out strings - arguments and environment. */
744 error = copyout(stringp, (void *)ustringp,
745 ARG_MAX - imgp->args->stringspace);
749 /* Fill in "ps_strings" struct for ps, w, etc. */
750 if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
751 suword32(&arginfo->ps_nargvstr, argc) != 0)
754 /* Fill in argument portion of vector table. */
755 for (; argc > 0; --argc) {
756 if (suword32(vectp++, ustringp) != 0)
758 while (*stringp++ != 0)
763 /* A null vector table pointer separates the argp's from the envp's. */
764 if (suword32(vectp++, 0) != 0)
767 if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
768 suword32(&arginfo->ps_nenvstr, envc) != 0)
771 /* Fill in environment portion of vector table. */
772 for (; envc > 0; --envc) {
773 if (suword32(vectp++, ustringp) != 0)
775 while (*stringp++ != 0)
780 /* The end of the vector table is a null pointer. */
781 if (suword32(vectp, 0) != 0)
786 error = imgp->sysent->sv_copyout_auxargs(imgp,
795 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
796 "32-bit Linux emulation");
798 static u_long linux32_maxdsiz = LINUX32_MAXDSIZ;
799 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
800 &linux32_maxdsiz, 0, "");
801 static u_long linux32_maxssiz = LINUX32_MAXSSIZ;
802 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
803 &linux32_maxssiz, 0, "");
804 static u_long linux32_maxvmem = LINUX32_MAXVMEM;
805 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
806 &linux32_maxvmem, 0, "");
807 bool linux32_emulate_i386 = false;
808 SYSCTL_BOOL(_compat_linux32, OID_AUTO, emulate_i386, CTLFLAG_RWTUN,
809 &linux32_emulate_i386, 0, "Emulate the real i386");
812 linux32_fixlimit(struct rlimit *rl, int which)
817 if (linux32_maxdsiz != 0) {
818 if (rl->rlim_cur > linux32_maxdsiz)
819 rl->rlim_cur = linux32_maxdsiz;
820 if (rl->rlim_max > linux32_maxdsiz)
821 rl->rlim_max = linux32_maxdsiz;
825 if (linux32_maxssiz != 0) {
826 if (rl->rlim_cur > linux32_maxssiz)
827 rl->rlim_cur = linux32_maxssiz;
828 if (rl->rlim_max > linux32_maxssiz)
829 rl->rlim_max = linux32_maxssiz;
833 if (linux32_maxvmem != 0) {
834 if (rl->rlim_cur > linux32_maxvmem)
835 rl->rlim_cur = linux32_maxvmem;
836 if (rl->rlim_max > linux32_maxvmem)
837 rl->rlim_max = linux32_maxvmem;
843 struct sysentvec elf_linux_sysvec = {
844 .sv_size = LINUX32_SYS_MAXSYSCALL,
845 .sv_table = linux32_sysent,
846 .sv_fixup = elf32_freebsd_fixup,
847 .sv_sendsig = linux_sendsig,
848 .sv_sigcode = &_binary_linux32_vdso_so_o_start,
849 .sv_szsigcode = &linux_szsigcode,
850 .sv_name = "Linux ELF32",
851 .sv_coredump = elf32_coredump,
852 .sv_elf_core_osabi = ELFOSABI_NONE,
853 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
854 .sv_elf_core_prepare_notes = linux32_prepare_notes,
855 .sv_imgact_try = linux_exec_imgact_try,
856 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
857 .sv_minuser = VM_MIN_ADDRESS,
858 .sv_maxuser = LINUX32_MAXUSER,
859 .sv_usrstack = LINUX32_USRSTACK,
860 .sv_psstrings = LINUX32_PS_STRINGS,
861 .sv_psstringssz = sizeof(struct linux32_ps_strings),
862 .sv_stackprot = VM_PROT_ALL,
863 .sv_copyout_auxargs = linux_copyout_auxargs,
864 .sv_copyout_strings = linux_copyout_strings,
865 .sv_setregs = linux_exec_setregs,
866 .sv_fixlimit = linux32_fixlimit,
867 .sv_maxssiz = &linux32_maxssiz,
868 .sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP |
869 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
870 .sv_set_syscall_retval = linux32_set_syscall_retval,
871 .sv_fetch_syscall_args = linux32_fetch_syscall_args,
872 .sv_syscallnames = linux32_syscallnames,
873 .sv_shared_page_base = LINUX32_SHAREDPAGE,
874 .sv_shared_page_len = PAGE_SIZE,
875 .sv_schedtail = linux_schedtail,
876 .sv_thread_detach = linux_thread_detach,
878 .sv_onexec = linux_on_exec_vmspace,
879 .sv_onexit = linux_on_exit,
880 .sv_ontdexit = linux_thread_dtor,
881 .sv_setid_allowed = &linux_setid_allowed_query,
882 .sv_set_fork_retval = linux32_set_fork_retval,
886 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
890 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
891 LINUX32_VDSOPAGE_SIZE, imgp);
893 linux_on_exec(p, imgp);
898 * linux_vdso_install() and linux_exec_sysvec_init() must be called
899 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
902 linux_exec_sysvec_init(void *param)
904 l_uintptr_t *ktimekeep_base, *ktsc_selector;
905 struct sysentvec *sv;
909 /* Fill timekeep_base */
910 exec_sysvec_init(sv);
912 tkoff = kern_timekeep_base - linux_vdso_base;
913 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
914 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
916 tkoff = kern_tsc_selector - linux_vdso_base;
917 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
918 *ktsc_selector = linux_vdso_tsc_selector_idx();
920 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
922 tkoff = kern_cpu_selector - linux_vdso_base;
923 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
924 *ktsc_selector = linux_vdso_cpu_selector_idx();
926 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
928 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
929 linux_exec_sysvec_init, &elf_linux_sysvec);
932 linux_vdso_install(const void *param)
934 char *vdso_start = &_binary_linux32_vdso_so_o_start;
935 char *vdso_end = &_binary_linux32_vdso_so_o_end;
937 linux_szsigcode = vdso_end - vdso_start;
938 MPASS(linux_szsigcode <= LINUX32_VDSOPAGE_SIZE);
940 linux_vdso_base = LINUX32_VDSOPAGE;
942 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
944 linux_vdso_obj = __elfN(linux_shared_page_init)
945 (&linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);
946 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
948 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
950 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
951 linux_vdso_install, NULL);
954 linux_vdso_deinstall(const void *param)
957 __elfN(linux_shared_page_fini)(linux_vdso_obj,
958 linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);
960 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
961 linux_vdso_deinstall, NULL);
964 linux_vdso_reloc(char *mapping, Elf_Addr offset)
966 const Elf_Shdr *shdr;
968 const Elf_Ehdr *ehdr;
970 Elf_Size rtype, symidx;
971 Elf32_Addr addr, addend;
977 ehdr = (const Elf_Ehdr *)mapping;
978 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
979 for (i = 0; i < ehdr->e_shnum; i++)
981 switch (shdr[i].sh_type) {
983 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
984 relcnt = shdr[i].sh_size / sizeof(*rel);
987 printf("Linux i386 vDSO: unexpected Rela section\n");
992 for (i = 0; i < relcnt; i++, rel++) {
993 where = (Elf32_Addr *)(mapping + rel->r_offset);
995 rtype = ELF_R_TYPE(rel->r_info);
996 symidx = ELF_R_SYM(rel->r_info);
999 case R_386_NONE: /* none */
1002 case R_386_RELATIVE: /* B + A */
1003 addr = (Elf32_Addr)PTROUT(offset + addend);
1008 case R_386_IRELATIVE:
1009 printf("Linux i386 vDSO: unexpected ifunc relocation, "
1010 "symbol index %ld\n", (intmax_t)symidx);
1013 printf("Linux i386 vDSO: unexpected relocation type %ld, "
1014 "symbol index %ld\n", (intmax_t)rtype, (intmax_t)symidx);
1019 static char GNU_ABI_VENDOR[] = "GNU";
1020 static int GNULINUX_ABI_DESC = 0;
1023 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
1025 const Elf32_Word *desc;
1028 p = (uintptr_t)(note + 1);
1029 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1031 desc = (const Elf32_Word *)p;
1032 if (desc[0] != GNULINUX_ABI_DESC)
1036 * For Linux we encode osrel using the Linux convention of
1037 * (version << 16) | (major << 8) | (minor)
1038 * See macro in linux_mib.h
1040 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
1045 static Elf_Brandnote linux32_brandnote = {
1046 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
1047 .hdr.n_descsz = 16, /* XXX at least 16 */
1049 .vendor = GNU_ABI_VENDOR,
1050 .flags = BN_TRANSLATE_OSREL,
1051 .trans_osrel = linux32_trans_osrel
1054 static Elf32_Brandinfo linux_brand = {
1055 .brand = ELFOSABI_LINUX,
1057 .compat_3_brand = "Linux",
1058 .emul_path = linux_emul_path,
1059 .interp_path = "/lib/ld-linux.so.1",
1060 .sysvec = &elf_linux_sysvec,
1061 .interp_newpath = NULL,
1062 .brand_note = &linux32_brandnote,
1063 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1066 static Elf32_Brandinfo linux_glibc2brand = {
1067 .brand = ELFOSABI_LINUX,
1069 .compat_3_brand = "Linux",
1070 .emul_path = linux_emul_path,
1071 .interp_path = "/lib/ld-linux.so.2",
1072 .sysvec = &elf_linux_sysvec,
1073 .interp_newpath = NULL,
1074 .brand_note = &linux32_brandnote,
1075 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1078 static Elf32_Brandinfo linux_muslbrand = {
1079 .brand = ELFOSABI_LINUX,
1081 .compat_3_brand = "Linux",
1082 .emul_path = linux_emul_path,
1083 .interp_path = "/lib/ld-musl-i386.so.1",
1084 .sysvec = &elf_linux_sysvec,
1085 .interp_newpath = NULL,
1086 .brand_note = &linux32_brandnote,
1087 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1088 LINUX_BI_FUTEX_REQUEUE
1091 Elf32_Brandinfo *linux_brandlist[] = {
1099 linux_elf_modevent(module_t mod, int type, void *data)
1101 Elf32_Brandinfo **brandinfo;
1103 struct linux_ioctl_handler **lihp;
1109 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1111 if (elf32_insert_brand_entry(*brandinfo) < 0)
1114 SET_FOREACH(lihp, linux_ioctl_handler_set)
1115 linux32_ioctl_register_handler(*lihp);
1116 stclohz = (stathz ? stathz : hz);
1118 printf("Linux i386 ELF exec handler installed\n");
1120 printf("cannot insert Linux i386 ELF brand handler\n");
1123 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1125 if (elf32_brand_inuse(*brandinfo))
1128 for (brandinfo = &linux_brandlist[0];
1129 *brandinfo != NULL; ++brandinfo)
1130 if (elf32_remove_brand_entry(*brandinfo) < 0)
1134 SET_FOREACH(lihp, linux_ioctl_handler_set)
1135 linux32_ioctl_unregister_handler(*lihp);
1137 printf("Linux i386 ELF exec handler removed\n");
1139 printf("Could not deinstall Linux i386 ELF interpreter entry\n");
1142 return (EOPNOTSUPP);
1147 static moduledata_t linux_elf_mod = {
1153 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1154 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1155 FEATURE(linux, "Linux 32bit support");