xref: /freebsd/sys/i386/linux/linux_sysvec.c (revision 3fde275167ce1f1455a03586d29840546d06d97a)
1d66a5066SPeter Wemm /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
383ef78beSPedro F. Giffuni  *
49a14aa01SUlrich Spörlein  * Copyright (c) 1994-1996 Søren Schmidt
5d66a5066SPeter Wemm  * All rights reserved.
6d66a5066SPeter Wemm  *
7d66a5066SPeter Wemm  * Redistribution and use in source and binary forms, with or without
8d66a5066SPeter Wemm  * modification, are permitted provided that the following conditions
9d66a5066SPeter Wemm  * are met:
10d66a5066SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
110ba1b365SEd Maste  *    notice, this list of conditions and the following disclaimer.
12d66a5066SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
13d66a5066SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
14d66a5066SPeter Wemm  *    documentation and/or other materials provided with the distribution.
15d66a5066SPeter Wemm  *
160ba1b365SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170ba1b365SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180ba1b365SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190ba1b365SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200ba1b365SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210ba1b365SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220ba1b365SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230ba1b365SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240ba1b365SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250ba1b365SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260ba1b365SEd Maste  * SUCH DAMAGE.
27d66a5066SPeter Wemm  */
28d66a5066SPeter Wemm 
29cc1b0f7dSDmitry Chagin #define __ELF_WORD_SIZE	32
30cc1b0f7dSDmitry Chagin 
31d66a5066SPeter Wemm #include <sys/param.h>
32ff22c670SBruce Evans #include <sys/exec.h>
3357b4252eSKonstantin Belousov #include <sys/fcntl.h>
34d66a5066SPeter Wemm #include <sys/imgact.h>
3522d4b0fbSJohn Polstra #include <sys/imgact_aout.h>
36e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
37ff22c670SBruce Evans #include <sys/kernel.h>
38f4a512a5SDmitry Chagin #include <sys/lock.h>
39e1743d02SSøren Schmidt #include <sys/malloc.h>
40ff22c670SBruce Evans #include <sys/module.h>
41f4a512a5SDmitry Chagin #include <sys/mutex.h>
42fb919e4dSMark Murray #include <sys/proc.h>
439931033bSDmitry Chagin #include <sys/stddef.h>
44206a5d3aSIan Dowse #include <sys/syscallsubr.h>
4567d39748SDmitry Chagin #include <sys/sysctl.h>
46fb919e4dSMark Murray #include <sys/sysent.h>
47fb919e4dSMark Murray #include <sys/sysproto.h>
48fb919e4dSMark Murray 
49a9148ab1SPeter Wemm #include <vm/pmap.h>
50f4a512a5SDmitry Chagin #include <vm/vm.h>
51a9148ab1SPeter Wemm #include <vm/vm_map.h>
52ff22c670SBruce Evans #include <vm/vm_page.h>
53ff22c670SBruce Evans 
54ff22c670SBruce Evans #include <machine/cpu.h>
554d7c2e8aSDmitry Chagin #include <machine/cputypes.h>
56ff22c670SBruce Evans #include <machine/md_var.h>
57d3adf769SDavid Schultz #include <machine/pcb.h>
58d41e41f9SJohn Baldwin #include <machine/trap.h>
59a9148ab1SPeter Wemm 
609931033bSDmitry Chagin #include <x86/linux/linux_x86.h>
61d66a5066SPeter Wemm #include <i386/linux/linux.h>
62ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
63cc1b0f7dSDmitry Chagin #include <compat/linux/linux_elf.h>
6494cb2ecfSAlexander Leidinger #include <compat/linux/linux_emul.h>
650a4b664aSDmitry Chagin #include <compat/linux/linux_fork.h>
66d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
670f9d6538SJohn Baldwin #include <compat/linux/linux_mib.h>
684d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
69b595ab37SAndrew Gallatin #include <compat/linux/linux_signal.h>
70322bfdc3SMarcel Moolenaar #include <compat/linux/linux_util.h>
71bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
72e1743d02SSøren Schmidt 
7321f24617SDmitry Chagin #include <x86/linux/linux_x86_sigframe.h>
7421f24617SDmitry Chagin 
751d91482dSPeter Wemm MODULE_VERSION(linux, 1);
761d91482dSPeter Wemm 
779931033bSDmitry Chagin #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
789931033bSDmitry Chagin #define	LINUX_VDSOPAGE		(VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
799931033bSDmitry Chagin #define	LINUX_SHAREDPAGE	(LINUX_VDSOPAGE - PAGE_SIZE)
809931033bSDmitry Chagin 				/*
819931033bSDmitry Chagin 				 * PAGE_SIZE - the size
829931033bSDmitry Chagin 				 * of the native SHAREDPAGE
839931033bSDmitry Chagin 				 */
849931033bSDmitry Chagin #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
858f1e49a6SDmitry Chagin #define	LINUX_PS_STRINGS	(LINUX_USRSTACK - sizeof(struct ps_strings))
868f1e49a6SDmitry Chagin 
87bdc37934SDmitry Chagin static int linux_szsigcode;
889931033bSDmitry Chagin static vm_object_t linux_vdso_obj;
899931033bSDmitry Chagin static char *linux_vdso_mapping;
909931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start;
919931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end;
929931033bSDmitry Chagin static vm_offset_t linux_vdso_base;
9343bef515SMarcel Moolenaar 
9443bef515SMarcel Moolenaar extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
951da65dcbSMitchell Horne extern const char *linux_syscallnames[];
9643bef515SMarcel Moolenaar 
97f41325dbSPeter Wemm SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
9843bef515SMarcel Moolenaar 
9931174518SJohn Baldwin static int	linux_fixup(uintptr_t *stack_base,
10089c9a483SAlfred Perlstein 		    struct image_params *iparams);
1019104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
102dc858467SEd Maste static void	linux_exec_setregs(struct thread *td,
10331174518SJohn Baldwin 		    struct image_params *imgp, uintptr_t stack);
1049931033bSDmitry Chagin static void	linux_exec_sysvec_init(void *param);
1055fd9cd53SDmitry Chagin static int	linux_on_exec_vmspace(struct proc *p,
1065fd9cd53SDmitry Chagin 		    struct image_params *imgp);
107de8374dfSDmitry Chagin static void	linux_set_fork_retval(struct thread *td);
108a543556cSDmitry Chagin static void	linux_vdso_install(const void *param);
109a543556cSDmitry Chagin static void	linux_vdso_deinstall(const void *param);
1109931033bSDmitry Chagin static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
1114d7c2e8aSDmitry Chagin 
112c1da89feSDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
1139931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
1148f9635dcSDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
1158f9635dcSDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode);
1169931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
1179931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
1185a6a4fb2SDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
119bdc37934SDmitry Chagin 
120303b270bSEivind Eklund static int
linux_fixup(uintptr_t * stack_base,struct image_params * imgp)12131174518SJohn Baldwin linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
122d66a5066SPeter Wemm {
12331174518SJohn Baldwin 	register_t *base, *argv, *envp;
124d66a5066SPeter Wemm 
12531174518SJohn Baldwin 	base = (register_t *)*stack_base;
12631174518SJohn Baldwin 	argv = base;
12731174518SJohn Baldwin 	envp = base + (imgp->args->argc + 1);
12831174518SJohn Baldwin 	base--;
129*3fde2751SMark Johnston 	if (suword(base, (intptr_t)envp) != 0)
130*3fde2751SMark Johnston 		return (EFAULT);
13131174518SJohn Baldwin 	base--;
132*3fde2751SMark Johnston 	if (suword(base, (intptr_t)argv) != 0)
133*3fde2751SMark Johnston 		return (EFAULT);
13431174518SJohn Baldwin 	base--;
135*3fde2751SMark Johnston 	if (suword(base, imgp->args->argc) != 0)
136*3fde2751SMark Johnston 		return (EFAULT);
13731174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
1384d7c2e8aSDmitry Chagin 	return (0);
139d66a5066SPeter Wemm }
140d66a5066SPeter Wemm 
1417d8c9839SDmitry Chagin void
linux32_arch_copyout_auxargs(struct image_params * imgp,Elf_Auxinfo ** pos)1427d8c9839SDmitry Chagin linux32_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
143e1743d02SSøren Schmidt {
1444d7c2e8aSDmitry Chagin 
1457d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
1467d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO, __kernel_vsyscall);
1477d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
1484281dab8SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());
1497d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
1505caa67faSJohn Baldwin }
1515caa67faSJohn Baldwin 
15279363394SAndrew Gallatin static void
linux_rt_sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)1539104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
15479363394SAndrew Gallatin {
1551d062e2bSDag-Erling Smørgrav 	struct thread *td = curthread;
1561d062e2bSDag-Erling Smørgrav 	struct proc *p = td->td_proc;
15790af4afaSJohn Baldwin 	struct sigacts *psp;
1581d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
1595002a60fSMarcel Moolenaar 	struct l_rt_sigframe *fp, frame;
1609104847fSDavid Xu 	int sig, code;
16179363394SAndrew Gallatin 	int oonstack;
16279363394SAndrew Gallatin 
163eca368ecSDmitry Chagin 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
1649104847fSDavid Xu 	code = ksi->ksi_code;
165df53e91cSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
16690af4afaSJohn Baldwin 	psp = p->p_sigacts;
16790af4afaSJohn Baldwin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
168b40ce416SJulian Elischer 	regs = td->td_frame;
169d034d459SMarcel Moolenaar 	oonstack = sigonstack(regs->tf_esp);
17079363394SAndrew Gallatin 
1714ba25759SEd Maste 	/* Allocate space for the signal handler context. */
172a30ec4b9SDavid Xu 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
17390af4afaSJohn Baldwin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
174aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
175a30ec4b9SDavid Xu 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
176d034d459SMarcel Moolenaar 	} else
1775002a60fSMarcel Moolenaar 		fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
17890af4afaSJohn Baldwin 	mtx_unlock(&psp->ps_mtx);
17979363394SAndrew Gallatin 
1804ba25759SEd Maste 	/* Build the argument list for the signal handler. */
1814ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
18279363394SAndrew Gallatin 
18399d45c5fSMarcel Moolenaar 	bzero(&frame, sizeof(frame));
18499d45c5fSMarcel Moolenaar 
18579363394SAndrew Gallatin 	frame.sf_sig = sig;
18621f24617SDmitry Chagin 	frame.sf_siginfo = PTROUT(&fp->sf_si);
1876e826d27SDmitry Chagin 	frame.sf_ucontext = PTROUT(&fp->sf_uc);
188cc6ca9b3SMarcel Moolenaar 
1894ba25759SEd Maste 	/* Fill in POSIX parts. */
190f4e80108SDmitry Chagin 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
191cc6ca9b3SMarcel Moolenaar 
1924ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
1936e826d27SDmitry Chagin 	frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
1946e826d27SDmitry Chagin 	frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
1956e826d27SDmitry Chagin 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
196d034d459SMarcel Moolenaar 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
197611d9407SJohn Baldwin 	PROC_UNLOCK(p);
198cc6ca9b3SMarcel Moolenaar 
1996e826d27SDmitry Chagin 	bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
200cc6ca9b3SMarcel Moolenaar 
2016e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_mask   = frame.sf_uc.uc_sigmask.__mask;
2026e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_gs     = rgs();
2036e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_fs     = regs->tf_fs;
2046e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_es     = regs->tf_es;
2056e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ds     = regs->tf_ds;
2066e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_edi    = regs->tf_edi;
2076e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_esi    = regs->tf_esi;
2086e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ebp    = regs->tf_ebp;
2096e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ebx    = regs->tf_ebx;
2106e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_esp    = regs->tf_esp;
2116e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_edx    = regs->tf_edx;
2126e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ecx    = regs->tf_ecx;
2136e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_eax    = regs->tf_eax;
2146e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_eip    = regs->tf_eip;
2156e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
2166e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags;
2176e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
2186e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ss     = regs->tf_ss;
2196e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
2206e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
2216e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
22279363394SAndrew Gallatin 
22379363394SAndrew Gallatin 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
22479363394SAndrew Gallatin 		/*
22579363394SAndrew Gallatin 		 * Process has trashed its stack; give it an illegal
22679363394SAndrew Gallatin 		 * instruction to halt it in its tracks.
22779363394SAndrew Gallatin 		 */
22819eb87d2SJohn Baldwin 		PROC_LOCK(p);
229b40ce416SJulian Elischer 		sigexit(td, SIGILL);
23079363394SAndrew Gallatin 	}
23179363394SAndrew Gallatin 
2324ba25759SEd Maste 	/* Build context to run handler in. */
23321f24617SDmitry Chagin 	regs->tf_esp = PTROUT(fp);
2348f9635dcSDmitry Chagin 	regs->tf_eip = linux_vdso_rt_sigcode;
23521f24617SDmitry Chagin 	regs->tf_edi = PTROUT(catcher);
23622eca0bfSKonstantin Belousov 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
23779363394SAndrew Gallatin 	regs->tf_cs = _ucodesel;
23879363394SAndrew Gallatin 	regs->tf_ds = _udatasel;
23979363394SAndrew Gallatin 	regs->tf_es = _udatasel;
24079363394SAndrew Gallatin 	regs->tf_fs = _udatasel;
24179363394SAndrew Gallatin 	regs->tf_ss = _udatasel;
242df53e91cSJohn Baldwin 	PROC_LOCK(p);
24390af4afaSJohn Baldwin 	mtx_lock(&psp->ps_mtx);
24479363394SAndrew Gallatin }
24579363394SAndrew Gallatin 
246d66a5066SPeter Wemm /*
247d66a5066SPeter Wemm  * Send an interrupt to process.
248d66a5066SPeter Wemm  *
249d66a5066SPeter Wemm  * Stack is set up to allow sigcode stored
250d66a5066SPeter Wemm  * in u. to call routine, followed by kcall
251d66a5066SPeter Wemm  * to sigreturn routine below.  After sigreturn
252d66a5066SPeter Wemm  * resets the signal mask, the stack, and the
253d66a5066SPeter Wemm  * frame pointer, it returns to the user
254d66a5066SPeter Wemm  * specified pc, psl.
255d66a5066SPeter Wemm  */
256303b270bSEivind Eklund static void
linux_sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)2579104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
258d66a5066SPeter Wemm {
2591d062e2bSDag-Erling Smørgrav 	struct thread *td = curthread;
2601d062e2bSDag-Erling Smørgrav 	struct proc *p = td->td_proc;
26190af4afaSJohn Baldwin 	struct sigacts *psp;
2621d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
2635002a60fSMarcel Moolenaar 	struct l_sigframe *fp, frame;
2645002a60fSMarcel Moolenaar 	l_sigset_t lmask;
2659e701630SJohn Baldwin 	int sig;
2664ab7403bSDmitry Chagin 	int oonstack;
267d66a5066SPeter Wemm 
2682509e6c2SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
26990af4afaSJohn Baldwin 	psp = p->p_sigacts;
270eca368ecSDmitry Chagin 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
27190af4afaSJohn Baldwin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
27290af4afaSJohn Baldwin 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
273cc6ca9b3SMarcel Moolenaar 		/* Signal handler installed with SA_SIGINFO. */
2749104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
275cc6ca9b3SMarcel Moolenaar 		return;
276cc6ca9b3SMarcel Moolenaar 	}
277b40ce416SJulian Elischer 	regs = td->td_frame;
278d034d459SMarcel Moolenaar 	oonstack = sigonstack(regs->tf_esp);
279d66a5066SPeter Wemm 
2804ba25759SEd Maste 	/* Allocate space for the signal handler context. */
281a30ec4b9SDavid Xu 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
28290af4afaSJohn Baldwin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
283aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
284a30ec4b9SDavid Xu 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
285d034d459SMarcel Moolenaar 	} else
2865002a60fSMarcel Moolenaar 		fp = (struct l_sigframe *)regs->tf_esp - 1;
28790af4afaSJohn Baldwin 	mtx_unlock(&psp->ps_mtx);
288611d9407SJohn Baldwin 	PROC_UNLOCK(p);
289d66a5066SPeter Wemm 
2904ba25759SEd Maste 	/* Build the argument list for the signal handler. */
2914ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
292d66a5066SPeter Wemm 
29399d45c5fSMarcel Moolenaar 	bzero(&frame, sizeof(frame));
29499d45c5fSMarcel Moolenaar 
295d66a5066SPeter Wemm 	frame.sf_sig = sig;
2964a6c2d07SDmitry Chagin 	frame.sf_sigmask = *mask;
297cc6ca9b3SMarcel Moolenaar 	bsd_to_linux_sigset(mask, &lmask);
298cc6ca9b3SMarcel Moolenaar 
2994ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
3004ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
3015206bca1SLuoqi Chen 	frame.sf_sc.sc_gs     = rgs();
3025206bca1SLuoqi Chen 	frame.sf_sc.sc_fs     = regs->tf_fs;
303213fdd80SPeter Wemm 	frame.sf_sc.sc_es     = regs->tf_es;
304213fdd80SPeter Wemm 	frame.sf_sc.sc_ds     = regs->tf_ds;
305213fdd80SPeter Wemm 	frame.sf_sc.sc_edi    = regs->tf_edi;
306213fdd80SPeter Wemm 	frame.sf_sc.sc_esi    = regs->tf_esi;
307213fdd80SPeter Wemm 	frame.sf_sc.sc_ebp    = regs->tf_ebp;
308213fdd80SPeter Wemm 	frame.sf_sc.sc_ebx    = regs->tf_ebx;
309bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_esp;
310213fdd80SPeter Wemm 	frame.sf_sc.sc_edx    = regs->tf_edx;
311213fdd80SPeter Wemm 	frame.sf_sc.sc_ecx    = regs->tf_ecx;
312213fdd80SPeter Wemm 	frame.sf_sc.sc_eax    = regs->tf_eax;
313213fdd80SPeter Wemm 	frame.sf_sc.sc_eip    = regs->tf_eip;
314213fdd80SPeter Wemm 	frame.sf_sc.sc_cs     = regs->tf_cs;
315213fdd80SPeter Wemm 	frame.sf_sc.sc_eflags = regs->tf_eflags;
316213fdd80SPeter Wemm 	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
317213fdd80SPeter Wemm 	frame.sf_sc.sc_ss     = regs->tf_ss;
318213fdd80SPeter Wemm 	frame.sf_sc.sc_err    = regs->tf_err;
31996a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
3209104847fSDavid Xu 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
321cc6ca9b3SMarcel Moolenaar 
322d66a5066SPeter Wemm 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
323d66a5066SPeter Wemm 		/*
324d66a5066SPeter Wemm 		 * Process has trashed its stack; give it an illegal
325d66a5066SPeter Wemm 		 * instruction to halt it in its tracks.
326d66a5066SPeter Wemm 		 */
32719eb87d2SJohn Baldwin 		PROC_LOCK(p);
328b40ce416SJulian Elischer 		sigexit(td, SIGILL);
329d66a5066SPeter Wemm 	}
330d66a5066SPeter Wemm 
3314ba25759SEd Maste 	/* Build context to run handler in. */
33221f24617SDmitry Chagin 	regs->tf_esp = PTROUT(fp);
3338f9635dcSDmitry Chagin 	regs->tf_eip = linux_vdso_sigcode;
33421f24617SDmitry Chagin 	regs->tf_edi = PTROUT(catcher);
33522eca0bfSKonstantin Belousov 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
336213fdd80SPeter Wemm 	regs->tf_cs = _ucodesel;
337213fdd80SPeter Wemm 	regs->tf_ds = _udatasel;
338213fdd80SPeter Wemm 	regs->tf_es = _udatasel;
3395206bca1SLuoqi Chen 	regs->tf_fs = _udatasel;
340213fdd80SPeter Wemm 	regs->tf_ss = _udatasel;
3415002a60fSMarcel Moolenaar 	PROC_LOCK(p);
34290af4afaSJohn Baldwin 	mtx_lock(&psp->ps_mtx);
343d66a5066SPeter Wemm }
344d66a5066SPeter Wemm 
345d66a5066SPeter Wemm /*
346d66a5066SPeter Wemm  * System call to cleanup state after a signal
347d66a5066SPeter Wemm  * has been taken.  Reset signal mask and
348d66a5066SPeter Wemm  * stack state from context left by sendsig (above).
349d66a5066SPeter Wemm  * Return to previous pc and psl as specified by
350d66a5066SPeter Wemm  * context left by sendsig. Check carefully to
351d66a5066SPeter Wemm  * make sure that the user has not modified the
352d66a5066SPeter Wemm  * psl to gain improper privileges or to cause
353d66a5066SPeter Wemm  * a machine fault.
354d66a5066SPeter Wemm  */
355d66a5066SPeter Wemm int
linux_sigreturn(struct thread * td,struct linux_sigreturn_args * args)356b07cd97eSMark Murray linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
357d66a5066SPeter Wemm {
3585002a60fSMarcel Moolenaar 	struct l_sigframe frame;
3591d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
3604ab7403bSDmitry Chagin 	int eflags;
3619104847fSDavid Xu 	ksiginfo_t ksi;
362d66a5066SPeter Wemm 
363b40ce416SJulian Elischer 	regs = td->td_frame;
364d66a5066SPeter Wemm 
365d66a5066SPeter Wemm 	/*
366cc6ca9b3SMarcel Moolenaar 	 * The trampoline code hands us the sigframe.
367d66a5066SPeter Wemm 	 * It is unsafe to keep track of it ourselves, in the event that a
368d66a5066SPeter Wemm 	 * program jumps out of a signal handler.
369d66a5066SPeter Wemm 	 */
3704b7ef73dSDag-Erling Smørgrav 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
371d66a5066SPeter Wemm 		return (EFAULT);
372d66a5066SPeter Wemm 
3734ba25759SEd Maste 	/* Check for security violations. */
374d66a5066SPeter Wemm #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
375cc6ca9b3SMarcel Moolenaar 	eflags = frame.sf_sc.sc_eflags;
3763d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
377d66a5066SPeter Wemm 		return (EINVAL);
378d66a5066SPeter Wemm 
379d66a5066SPeter Wemm 	/*
380d66a5066SPeter Wemm 	 * Don't allow users to load a valid privileged %cs.  Let the
381d66a5066SPeter Wemm 	 * hardware check for invalid selectors, excess privilege in
382d66a5066SPeter Wemm 	 * other selectors, invalid %eip's and invalid %esp's.
383d66a5066SPeter Wemm 	 */
38440d50994SPhilippe Charnier #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
385cc6ca9b3SMarcel Moolenaar 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
3869104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
3879104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
3889104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
3899104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
3909104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_eip;
3919104847fSDavid Xu 		trapsignal(td, &ksi);
392d66a5066SPeter Wemm 		return (EINVAL);
393d66a5066SPeter Wemm 	}
394d66a5066SPeter Wemm 
3954a6c2d07SDmitry Chagin 	kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
396956d3333SMarcel Moolenaar 
3974ba25759SEd Maste 	/* Restore signal context. */
3985206bca1SLuoqi Chen 	/* %gs was restored by the trampoline. */
399cc6ca9b3SMarcel Moolenaar 	regs->tf_fs     = frame.sf_sc.sc_fs;
400cc6ca9b3SMarcel Moolenaar 	regs->tf_es     = frame.sf_sc.sc_es;
401cc6ca9b3SMarcel Moolenaar 	regs->tf_ds     = frame.sf_sc.sc_ds;
402cc6ca9b3SMarcel Moolenaar 	regs->tf_edi    = frame.sf_sc.sc_edi;
403cc6ca9b3SMarcel Moolenaar 	regs->tf_esi    = frame.sf_sc.sc_esi;
404cc6ca9b3SMarcel Moolenaar 	regs->tf_ebp    = frame.sf_sc.sc_ebp;
405cc6ca9b3SMarcel Moolenaar 	regs->tf_ebx    = frame.sf_sc.sc_ebx;
406cc6ca9b3SMarcel Moolenaar 	regs->tf_edx    = frame.sf_sc.sc_edx;
407cc6ca9b3SMarcel Moolenaar 	regs->tf_ecx    = frame.sf_sc.sc_ecx;
408cc6ca9b3SMarcel Moolenaar 	regs->tf_eax    = frame.sf_sc.sc_eax;
409cc6ca9b3SMarcel Moolenaar 	regs->tf_eip    = frame.sf_sc.sc_eip;
410cc6ca9b3SMarcel Moolenaar 	regs->tf_cs     = frame.sf_sc.sc_cs;
411213fdd80SPeter Wemm 	regs->tf_eflags = eflags;
412cc6ca9b3SMarcel Moolenaar 	regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
413cc6ca9b3SMarcel Moolenaar 	regs->tf_ss     = frame.sf_sc.sc_ss;
414d66a5066SPeter Wemm 
415d66a5066SPeter Wemm 	return (EJUSTRETURN);
416d66a5066SPeter Wemm }
417d66a5066SPeter Wemm 
41879363394SAndrew Gallatin /*
41979363394SAndrew Gallatin  * System call to cleanup state after a signal
42079363394SAndrew Gallatin  * has been taken.  Reset signal mask and
42179363394SAndrew Gallatin  * stack state from context left by rt_sendsig (above).
42279363394SAndrew Gallatin  * Return to previous pc and psl as specified by
42379363394SAndrew Gallatin  * context left by sendsig. Check carefully to
42479363394SAndrew Gallatin  * make sure that the user has not modified the
42579363394SAndrew Gallatin  * psl to gain improper privileges or to cause
42679363394SAndrew Gallatin  * a machine fault.
42779363394SAndrew Gallatin  */
42879363394SAndrew Gallatin int
linux_rt_sigreturn(struct thread * td,struct linux_rt_sigreturn_args * args)429b07cd97eSMark Murray linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
43079363394SAndrew Gallatin {
4315002a60fSMarcel Moolenaar 	struct l_ucontext uc;
4325002a60fSMarcel Moolenaar 	struct l_sigcontext *context;
433d6e029adSKonstantin Belousov 	sigset_t bmask;
4345002a60fSMarcel Moolenaar 	l_stack_t *lss;
435206a5d3aSIan Dowse 	stack_t ss;
4361d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
43779363394SAndrew Gallatin 	int eflags;
4389104847fSDavid Xu 	ksiginfo_t ksi;
43979363394SAndrew Gallatin 
440b40ce416SJulian Elischer 	regs = td->td_frame;
44179363394SAndrew Gallatin 
44279363394SAndrew Gallatin 	/*
443cc6ca9b3SMarcel Moolenaar 	 * The trampoline code hands us the ucontext.
44479363394SAndrew Gallatin 	 * It is unsafe to keep track of it ourselves, in the event that a
44579363394SAndrew Gallatin 	 * program jumps out of a signal handler.
44679363394SAndrew Gallatin 	 */
4474b7ef73dSDag-Erling Smørgrav 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
44879363394SAndrew Gallatin 		return (EFAULT);
44979363394SAndrew Gallatin 
45079363394SAndrew Gallatin 	context = &uc.uc_mcontext;
45179363394SAndrew Gallatin 
4524ba25759SEd Maste 	/* Check for security violations. */
45379363394SAndrew Gallatin #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
45479363394SAndrew Gallatin 	eflags = context->sc_eflags;
4553d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
45679363394SAndrew Gallatin 		return (EINVAL);
45779363394SAndrew Gallatin 
45879363394SAndrew Gallatin 	/*
45979363394SAndrew Gallatin 	 * Don't allow users to load a valid privileged %cs.  Let the
46079363394SAndrew Gallatin 	 * hardware check for invalid selectors, excess privilege in
46179363394SAndrew Gallatin 	 * other selectors, invalid %eip's and invalid %esp's.
46279363394SAndrew Gallatin 	 */
46379363394SAndrew Gallatin #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
46479363394SAndrew Gallatin 	if (!CS_SECURE(context->sc_cs)) {
4659104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
4669104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
4679104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
4689104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
4699104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_eip;
4709104847fSDavid Xu 		trapsignal(td, &ksi);
47179363394SAndrew Gallatin 		return (EINVAL);
47279363394SAndrew Gallatin 	}
47379363394SAndrew Gallatin 
474d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
475d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
47679363394SAndrew Gallatin 
4774ba25759SEd Maste 	/* Restore signal context. */
47879363394SAndrew Gallatin 	/* %gs was restored by the trampoline. */
47979363394SAndrew Gallatin 	regs->tf_fs     = context->sc_fs;
48079363394SAndrew Gallatin 	regs->tf_es     = context->sc_es;
48179363394SAndrew Gallatin 	regs->tf_ds     = context->sc_ds;
48279363394SAndrew Gallatin 	regs->tf_edi    = context->sc_edi;
48379363394SAndrew Gallatin 	regs->tf_esi    = context->sc_esi;
48479363394SAndrew Gallatin 	regs->tf_ebp    = context->sc_ebp;
48579363394SAndrew Gallatin 	regs->tf_ebx    = context->sc_ebx;
48679363394SAndrew Gallatin 	regs->tf_edx    = context->sc_edx;
48779363394SAndrew Gallatin 	regs->tf_ecx    = context->sc_ecx;
48879363394SAndrew Gallatin 	regs->tf_eax    = context->sc_eax;
48979363394SAndrew Gallatin 	regs->tf_eip    = context->sc_eip;
49079363394SAndrew Gallatin 	regs->tf_cs     = context->sc_cs;
49179363394SAndrew Gallatin 	regs->tf_eflags = eflags;
49279363394SAndrew Gallatin 	regs->tf_esp    = context->sc_esp_at_signal;
49379363394SAndrew Gallatin 	regs->tf_ss     = context->sc_ss;
49479363394SAndrew Gallatin 
4954ba25759SEd Maste 	/* Call sigaltstack & ignore results. */
49679363394SAndrew Gallatin 	lss = &uc.uc_stack;
49730c6d982SEdward Tomasz Napierala 	ss.ss_sp = PTRIN(lss->ss_sp);
498206a5d3aSIan Dowse 	ss.ss_size = lss->ss_size;
499206a5d3aSIan Dowse 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
50079363394SAndrew Gallatin 
501206a5d3aSIan Dowse 	(void)kern_sigaltstack(td, &ss, NULL);
50279363394SAndrew Gallatin 
50379363394SAndrew Gallatin 	return (EJUSTRETURN);
50479363394SAndrew Gallatin }
50579363394SAndrew Gallatin 
506afe1a688SKonstantin Belousov static int
linux_fetch_syscall_args(struct thread * td)5072d88da2fSKonstantin Belousov linux_fetch_syscall_args(struct thread *td)
508d66a5066SPeter Wemm {
509afe1a688SKonstantin Belousov 	struct proc *p;
510afe1a688SKonstantin Belousov 	struct trapframe *frame;
5112d88da2fSKonstantin Belousov 	struct syscall_args *sa;
512afe1a688SKonstantin Belousov 
513afe1a688SKonstantin Belousov 	p = td->td_proc;
514afe1a688SKonstantin Belousov 	frame = td->td_frame;
5152d88da2fSKonstantin Belousov 	sa = &td->td_sa;
516afe1a688SKonstantin Belousov 
517afe1a688SKonstantin Belousov 	sa->code = frame->tf_eax;
518cf98bc28SDavid Chisnall 	sa->original_code = sa->code;
519afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_ebx;
520afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_ecx;
521afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_edx;
522afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_esi;
523afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_edi;
5245bdd74ccSDmitry Chagin 	sa->args[5] = frame->tf_ebp;
525afe1a688SKonstantin Belousov 
526afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
527fcdffc03SDmitry Chagin 		/* nosys */
52839024a89SKonstantin Belousov 		sa->callp = &nosys_sysent;
529afe1a688SKonstantin Belousov 	else
530afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
531afe1a688SKonstantin Belousov 
532afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
533afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_edx;
534afe1a688SKonstantin Belousov 
535afe1a688SKonstantin Belousov 	return (0);
536d66a5066SPeter Wemm }
537d66a5066SPeter Wemm 
538c26391f4SEdward Tomasz Napierala static void
linux_set_syscall_retval(struct thread * td,int error)539c26391f4SEdward Tomasz Napierala linux_set_syscall_retval(struct thread *td, int error)
540c26391f4SEdward Tomasz Napierala {
541c26391f4SEdward Tomasz Napierala 	struct trapframe *frame = td->td_frame;
542c26391f4SEdward Tomasz Napierala 
543c26391f4SEdward Tomasz Napierala 	cpu_set_syscall_retval(td, error);
544c26391f4SEdward Tomasz Napierala 
545c26391f4SEdward Tomasz Napierala 	if (__predict_false(error != 0)) {
546c26391f4SEdward Tomasz Napierala 		if (error != ERESTART && error != EJUSTRETURN)
547866b1f51SEdward Tomasz Napierala 			frame->tf_eax = bsd_to_linux_errno(error);
548c26391f4SEdward Tomasz Napierala 	}
549c26391f4SEdward Tomasz Napierala }
550c26391f4SEdward Tomasz Napierala 
551de8374dfSDmitry Chagin static void
linux_set_fork_retval(struct thread * td)552de8374dfSDmitry Chagin linux_set_fork_retval(struct thread *td)
553de8374dfSDmitry Chagin {
554de8374dfSDmitry Chagin 	struct trapframe *frame = td->td_frame;
555de8374dfSDmitry Chagin 
556de8374dfSDmitry Chagin 	frame->tf_eax = 0;
557de8374dfSDmitry Chagin }
558de8374dfSDmitry Chagin 
559d323ddf3SMatthew Dillon /*
560598d45beSMatthew N. Dodd  * exec_setregs may initialize some registers differently than Linux
561598d45beSMatthew N. Dodd  * does, thus potentially confusing Linux binaries. If necessary, we
562598d45beSMatthew N. Dodd  * override the exec_setregs default(s) here.
563598d45beSMatthew N. Dodd  */
564598d45beSMatthew N. Dodd static void
linux_exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)56531174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
56631174518SJohn Baldwin     uintptr_t stack)
567598d45beSMatthew N. Dodd {
568598d45beSMatthew N. Dodd 	struct pcb *pcb = td->td_pcb;
569598d45beSMatthew N. Dodd 
570a107d8aaSNathan Whitehorn 	exec_setregs(td, imgp, stack);
571598d45beSMatthew N. Dodd 
5724ba25759SEd Maste 	/* Linux sets %gs to 0, we default to _udatasel. */
5732ee8325fSJohn Baldwin 	pcb->pcb_gs = 0;
5742ee8325fSJohn Baldwin 	load_gs(0);
5752a51b9b0SDavid Schultz 
5762ee8325fSJohn Baldwin 	pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
577598d45beSMatthew N. Dodd }
578598d45beSMatthew N. Dodd 
579d66a5066SPeter Wemm struct sysentvec linux_sysvec = {
580a8d403e1SKonstantin Belousov 	.sv_size	= LINUX_SYS_MAXSYSCALL,
581a8d403e1SKonstantin Belousov 	.sv_table	= linux_sysent,
582a8d403e1SKonstantin Belousov 	.sv_fixup	= linux_fixup,
583a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
5849931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
585a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
586a8d403e1SKonstantin Belousov 	.sv_name	= "Linux a.out",
587a8d403e1SKonstantin Belousov 	.sv_coredump	= NULL,
588a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
589a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
590a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
5918f1e49a6SDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK,
592a8d403e1SKonstantin Belousov 	.sv_psstrings	= PS_STRINGS,
5933fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
594a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
595a8d403e1SKonstantin Belousov 	.sv_copyout_strings = exec_copyout_strings,
596dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
597a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
598b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
599870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
600870e197dSKonstantin Belousov 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
601c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux_set_syscall_retval,
602afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
6031da65dcbSMitchell Horne 	.sv_syscallnames = linux_syscallnames,
604e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
60581338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
606038c7205SDmitry Chagin 	.sv_trap	= NULL,
6077d8c9839SDmitry Chagin 	.sv_hwcap	= NULL,
6087d8c9839SDmitry Chagin 	.sv_hwcap2	= NULL,
6095fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
6104815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
6114815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
612598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
613de8374dfSDmitry Chagin 	.sv_set_fork_retval = linux_set_fork_retval,
614d66a5066SPeter Wemm };
6158f1e49a6SDmitry Chagin INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
616e1743d02SSøren Schmidt 
617e1743d02SSøren Schmidt struct sysentvec elf_linux_sysvec = {
618a8d403e1SKonstantin Belousov 	.sv_size	= LINUX_SYS_MAXSYSCALL,
619a8d403e1SKonstantin Belousov 	.sv_table	= linux_sysent,
6209e550625SDmitry Chagin 	.sv_fixup	= __elfN(freebsd_fixup),
621a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
6229931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
623a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
6245faeda90SJessica Clarke 	.sv_name	= "Linux ELF32",
625a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
626cc1b0f7dSDmitry Chagin 	.sv_elf_core_osabi = ELFOSABI_NONE,
627cc1b0f7dSDmitry Chagin 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
628cc1b0f7dSDmitry Chagin 	.sv_elf_core_prepare_notes = __linuxN(prepare_notes),
629a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
630a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
631a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
6328f1e49a6SDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK,
6338f1e49a6SDmitry Chagin 	.sv_psstrings	= LINUX_PS_STRINGS,
6343fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
635a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
6367d8c9839SDmitry Chagin 	.sv_copyout_auxargs = __linuxN(copyout_auxargs),
6376039e966SDmitry Chagin 	.sv_copyout_strings = __linuxN(copyout_strings),
638dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
639a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
640b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
641870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
6429931033bSDmitry Chagin 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
643c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux_set_syscall_retval,
644afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
645afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
6468f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX_SHAREDPAGE,
6478f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
648e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
64981338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
650038c7205SDmitry Chagin 	.sv_trap	= NULL,
6517d8c9839SDmitry Chagin 	.sv_hwcap	= NULL,
6527d8c9839SDmitry Chagin 	.sv_hwcap2	= NULL,
6535fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
6544815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
6554815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
656598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
657de8374dfSDmitry Chagin 	.sv_set_fork_retval = linux_set_fork_retval,
658e1743d02SSøren Schmidt };
659bdc37934SDmitry Chagin 
6605fd9cd53SDmitry Chagin static int
linux_on_exec_vmspace(struct proc * p,struct image_params * imgp)6615fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
6625fd9cd53SDmitry Chagin {
6639931033bSDmitry Chagin 	int error = 0;
6645fd9cd53SDmitry Chagin 
6659931033bSDmitry Chagin 	if (SV_PROC_FLAG(p, SV_SHP) != 0)
6669931033bSDmitry Chagin 		error = linux_map_vdso(p, linux_vdso_obj,
6679931033bSDmitry Chagin 		    linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
6689931033bSDmitry Chagin 	if (error == 0)
669fd745e1dSDmitry Chagin 		error = linux_on_exec(p, imgp);
6709931033bSDmitry Chagin 	return (error);
6715fd9cd53SDmitry Chagin }
6725fd9cd53SDmitry Chagin 
67309cffde9SDmitry Chagin /*
67409cffde9SDmitry Chagin  * linux_vdso_install() and linux_exec_sysvec_init() must be called
67509cffde9SDmitry Chagin  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
67609cffde9SDmitry Chagin  */
677bdc37934SDmitry Chagin static void
linux_exec_sysvec_init(void * param)6789931033bSDmitry Chagin linux_exec_sysvec_init(void *param)
6799931033bSDmitry Chagin {
6809931033bSDmitry Chagin 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
6819931033bSDmitry Chagin 	struct sysentvec *sv;
6829931033bSDmitry Chagin 	ptrdiff_t tkoff;
6839931033bSDmitry Chagin 
6849931033bSDmitry Chagin 	sv = param;
6859931033bSDmitry Chagin 	/* Fill timekeep_base */
6869931033bSDmitry Chagin 	exec_sysvec_init(sv);
6879931033bSDmitry Chagin 
6889931033bSDmitry Chagin 	tkoff = kern_timekeep_base - linux_vdso_base;
6899931033bSDmitry Chagin 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
690361971fbSKornel Dulęba 	*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
6919931033bSDmitry Chagin 
6929931033bSDmitry Chagin 	tkoff = kern_tsc_selector - linux_vdso_base;
6939931033bSDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
6949931033bSDmitry Chagin 	*ktsc_selector = linux_vdso_tsc_selector_idx();
6959931033bSDmitry Chagin 	if (bootverbose)
6969931033bSDmitry Chagin 		printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
6975a6a4fb2SDmitry Chagin 
6985a6a4fb2SDmitry Chagin 	tkoff = kern_cpu_selector - linux_vdso_base;
6995a6a4fb2SDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
7005a6a4fb2SDmitry Chagin 	*ktsc_selector = linux_vdso_cpu_selector_idx();
7015a6a4fb2SDmitry Chagin 	if (bootverbose)
7025a6a4fb2SDmitry Chagin 		printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
7039931033bSDmitry Chagin }
70409cffde9SDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
7059931033bSDmitry Chagin     linux_exec_sysvec_init, &elf_linux_sysvec);
7069931033bSDmitry Chagin 
7079931033bSDmitry Chagin static void
linux_vdso_install(const void * param)708a543556cSDmitry Chagin linux_vdso_install(const void *param)
709bdc37934SDmitry Chagin {
7109931033bSDmitry Chagin 	char *vdso_start = &_binary_linux_vdso_so_o_start;
7119931033bSDmitry Chagin 	char *vdso_end = &_binary_linux_vdso_so_o_end;
712bdc37934SDmitry Chagin 
7139931033bSDmitry Chagin 	linux_szsigcode = vdso_end - vdso_start;
7149931033bSDmitry Chagin 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
715bdc37934SDmitry Chagin 
7169931033bSDmitry Chagin 	linux_vdso_base = LINUX_VDSOPAGE;
717bdc37934SDmitry Chagin 
7189931033bSDmitry Chagin 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
719bdc37934SDmitry Chagin 
7209931033bSDmitry Chagin 	linux_vdso_obj = __elfN(linux_shared_page_init)
7219931033bSDmitry Chagin 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
7229931033bSDmitry Chagin 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
723bdc37934SDmitry Chagin 
7249931033bSDmitry Chagin 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
725bdc37934SDmitry Chagin }
72609cffde9SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
727f8268d4dSEd Maste     linux_vdso_install, NULL);
728bdc37934SDmitry Chagin 
729bdc37934SDmitry Chagin static void
linux_vdso_deinstall(const void * param)730a543556cSDmitry Chagin linux_vdso_deinstall(const void *param)
731bdc37934SDmitry Chagin {
732bdc37934SDmitry Chagin 
7339931033bSDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
7349931033bSDmitry Chagin 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
7357b194b3dSEd Maste }
736bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
737f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
738e1743d02SSøren Schmidt 
7399931033bSDmitry Chagin static void
linux_vdso_reloc(char * mapping,Elf_Addr offset)7409931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset)
7419931033bSDmitry Chagin {
7429931033bSDmitry Chagin 	const Elf_Shdr *shdr;
7439931033bSDmitry Chagin 	const Elf_Rel *rel;
7449931033bSDmitry Chagin 	const Elf_Ehdr *ehdr;
7459931033bSDmitry Chagin 	Elf_Addr *where;
7469931033bSDmitry Chagin 	Elf_Size rtype, symidx;
7479931033bSDmitry Chagin 	Elf_Addr addr, addend;
7489931033bSDmitry Chagin 	int i, relcnt;
7499931033bSDmitry Chagin 
7509931033bSDmitry Chagin 	MPASS(offset != 0);
7519931033bSDmitry Chagin 
7529931033bSDmitry Chagin 	relcnt = 0;
7539931033bSDmitry Chagin 	ehdr = (const Elf_Ehdr *)mapping;
7549931033bSDmitry Chagin 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
7559931033bSDmitry Chagin 	for (i = 0; i < ehdr->e_shnum; i++)
7569931033bSDmitry Chagin 	{
7579931033bSDmitry Chagin 		switch (shdr[i].sh_type) {
7589931033bSDmitry Chagin 		case SHT_REL:
7599931033bSDmitry Chagin 			rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
7609931033bSDmitry Chagin 			relcnt = shdr[i].sh_size / sizeof(*rel);
7619931033bSDmitry Chagin 			break;
7629931033bSDmitry Chagin 		case SHT_RELA:
7639931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected Rela section\n");
7649931033bSDmitry Chagin 			break;
7659931033bSDmitry Chagin 		}
7669931033bSDmitry Chagin 	}
7679931033bSDmitry Chagin 
7689931033bSDmitry Chagin 	for (i = 0; i < relcnt; i++, rel++) {
7699931033bSDmitry Chagin 		where = (Elf_Addr *)(mapping + rel->r_offset);
7709931033bSDmitry Chagin 		addend = *where;
7719931033bSDmitry Chagin 		rtype = ELF_R_TYPE(rel->r_info);
7729931033bSDmitry Chagin 		symidx = ELF_R_SYM(rel->r_info);
7739931033bSDmitry Chagin 
7749931033bSDmitry Chagin 		switch (rtype) {
7759931033bSDmitry Chagin 		case R_386_NONE:	/* none */
7769931033bSDmitry Chagin 			break;
7779931033bSDmitry Chagin 
7789931033bSDmitry Chagin 		case R_386_RELATIVE:	/* B + A */
7799931033bSDmitry Chagin 			addr = (Elf_Addr)PTROUT(offset + addend);
7809931033bSDmitry Chagin 			if (*where != addr)
7819931033bSDmitry Chagin 				*where = addr;
7829931033bSDmitry Chagin 			break;
7839931033bSDmitry Chagin 
7849931033bSDmitry Chagin 		case R_386_IRELATIVE:
7859931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected ifunc relocation, "
7869931033bSDmitry Chagin 			    "symbol index %d\n", symidx);
7879931033bSDmitry Chagin 			break;
7889931033bSDmitry Chagin 		default:
7899931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected relocation type %d, "
7909931033bSDmitry Chagin 			    "symbol index %d\n", rtype, symidx);
7919931033bSDmitry Chagin 		}
7929931033bSDmitry Chagin 	}
7939931033bSDmitry Chagin }
7949931033bSDmitry Chagin 
79532c01de2SDmitry Chagin static Elf_Brandnote linux_brandnote = {
79689ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
79789ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
79832c01de2SDmitry Chagin 	.hdr.n_type	= 1,
79989ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
80089ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
80189ffc202SBjoern A. Zeeb 	.trans_osrel	= linux_trans_osrel
80232c01de2SDmitry Chagin };
80332c01de2SDmitry Chagin 
804514058dcSAlexander Langer static Elf32_Brandinfo linux_brand = {
805a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
806a8d403e1SKonstantin Belousov 	.machine	= EM_386,
807a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
808a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
809a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
810a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
81132c01de2SDmitry Chagin 	.brand_note	= &linux_brandnote,
8122dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
8135cf588ebSPeter Wemm };
8145cf588ebSPeter Wemm 
815514058dcSAlexander Langer static Elf32_Brandinfo linux_glibc2brand = {
816a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
817a8d403e1SKonstantin Belousov 	.machine	= EM_386,
818a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
819a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
820a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
821a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
82232c01de2SDmitry Chagin 	.brand_note	= &linux_brandnote,
8232dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
8244e138a28SMike Smith };
8254e138a28SMike Smith 
826a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
827a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
828a0c59c7aSDmitry Chagin 	.machine	= EM_386,
829a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
830a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
831a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
832a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
833a0c59c7aSDmitry Chagin 	.brand_note	= &linux_brandnote,
834cf8d74e3SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
835cf8d74e3SDmitry Chagin 			    LINUX_BI_FUTEX_REQUEUE
836a0c59c7aSDmitry Chagin };
837a0c59c7aSDmitry Chagin 
838514058dcSAlexander Langer Elf32_Brandinfo *linux_brandlist[] = {
839514058dcSAlexander Langer 	&linux_brand,
840514058dcSAlexander Langer 	&linux_glibc2brand,
841a0c59c7aSDmitry Chagin 	&linux_muslbrand,
842514058dcSAlexander Langer 	NULL
843514058dcSAlexander Langer };
844514058dcSAlexander Langer 
845aa855a59SPeter Wemm static int
linux_elf_modevent(module_t mod,int type,void * data)846c25ded31SBruce Evans linux_elf_modevent(module_t mod, int type, void *data)
847d30ea4f5SPeter Wemm {
848514058dcSAlexander Langer 	Elf32_Brandinfo **brandinfo;
849514058dcSAlexander Langer 	int error;
850f41325dbSPeter Wemm 	struct linux_ioctl_handler **lihp;
851514058dcSAlexander Langer 
852514058dcSAlexander Langer 	error = 0;
853514058dcSAlexander Langer 
854aa855a59SPeter Wemm 	switch(type) {
855aa855a59SPeter Wemm 	case MOD_LOAD:
856aa855a59SPeter Wemm 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
857aa855a59SPeter Wemm 		     ++brandinfo)
8583ebc1248SPeter Wemm 			if (elf32_insert_brand_entry(*brandinfo) < 0)
859aa855a59SPeter Wemm 				error = EINVAL;
860466b14d7SMarcel Moolenaar 		if (error == 0) {
861f41325dbSPeter Wemm 			SET_FOREACH(lihp, linux_ioctl_handler_set)
862f41325dbSPeter Wemm 				linux_ioctl_register_handler(*lihp);
863044ab55eSEdward Tomasz Napierala 			linux_dev_shm_create();
8647ae27ff4SJamie Gritton 			linux_osd_jail_register();
86507db1f36SDmitry Chagin 			linux_netlink_register();
8661ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
86743bef515SMarcel Moolenaar 			if (bootverbose)
868466b14d7SMarcel Moolenaar 				printf("Linux ELF exec handler installed\n");
869466b14d7SMarcel Moolenaar 		} else
870466b14d7SMarcel Moolenaar 			printf("cannot insert Linux ELF brand handler\n");
871aa855a59SPeter Wemm 		break;
872aa855a59SPeter Wemm 	case MOD_UNLOAD:
873aa855a59SPeter Wemm 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
874aa855a59SPeter Wemm 		     ++brandinfo)
8753ebc1248SPeter Wemm 			if (elf32_brand_inuse(*brandinfo))
876d2758342SMark Newton 				error = EBUSY;
877d2758342SMark Newton 		if (error == 0) {
878d2758342SMark Newton 			for (brandinfo = &linux_brandlist[0];
879d2758342SMark Newton 			     *brandinfo != NULL; ++brandinfo)
8803ebc1248SPeter Wemm 				if (elf32_remove_brand_entry(*brandinfo) < 0)
881aa855a59SPeter Wemm 					error = EINVAL;
882d2758342SMark Newton 		}
883466b14d7SMarcel Moolenaar 		if (error == 0) {
884f41325dbSPeter Wemm 			SET_FOREACH(lihp, linux_ioctl_handler_set)
885f41325dbSPeter Wemm 				linux_ioctl_unregister_handler(*lihp);
88607db1f36SDmitry Chagin 			linux_netlink_deregister();
887044ab55eSEdward Tomasz Napierala 			linux_dev_shm_destroy();
8887ae27ff4SJamie Gritton 			linux_osd_jail_deregister();
889466b14d7SMarcel Moolenaar 			if (bootverbose)
890466b14d7SMarcel Moolenaar 				printf("Linux ELF exec handler removed\n");
891466b14d7SMarcel Moolenaar 		} else
892aa855a59SPeter Wemm 			printf("Could not deinstall ELF interpreter entry\n");
893aa855a59SPeter Wemm 		break;
894aa855a59SPeter Wemm 	default:
895af682d48SDmitry Chagin 		return (EOPNOTSUPP);
896d30ea4f5SPeter Wemm 	}
897af682d48SDmitry Chagin 	return (error);
898aa855a59SPeter Wemm }
899466b14d7SMarcel Moolenaar 
900aa855a59SPeter Wemm static moduledata_t linux_elf_mod = {
901aa855a59SPeter Wemm 	"linuxelf",
902aa855a59SPeter Wemm 	linux_elf_modevent,
9039823d527SKevin Lo 	0
904aa855a59SPeter Wemm };
905466b14d7SMarcel Moolenaar 
90678ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
90707db1f36SDmitry Chagin MODULE_DEPEND(linuxelf, netlink, 1, 1, 1);
908b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
909