xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision 598f6fb49c9ca688029b79de0a44227ab79c608c)
1ea0fabbcSTim J. Robbins /*-
2c49761ddSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3c49761ddSPedro F. Giffuni  *
4ea0fabbcSTim J. Robbins  * Copyright (c) 2004 Tim J. Robbins
5ea0fabbcSTim J. Robbins  * Copyright (c) 2003 Peter Wemm
6ea0fabbcSTim J. Robbins  * Copyright (c) 2002 Doug Rabson
7ea0fabbcSTim J. Robbins  * Copyright (c) 1998-1999 Andrew Gallatin
89a14aa01SUlrich Spörlein  * Copyright (c) 1994-1996 Søren Schmidt
9ea0fabbcSTim J. Robbins  * All rights reserved.
10ea0fabbcSTim J. Robbins  *
11ea0fabbcSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
12ea0fabbcSTim J. Robbins  * modification, are permitted provided that the following conditions
13ea0fabbcSTim J. Robbins  * are met:
14ea0fabbcSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
15ea0fabbcSTim J. Robbins  *    notice, this list of conditions and the following disclaimer
16ea0fabbcSTim J. Robbins  *    in this position and unchanged.
17ea0fabbcSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
18ea0fabbcSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
19ea0fabbcSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
20ea0fabbcSTim J. Robbins  * 3. The name of the author may not be used to endorse or promote products
21ea0fabbcSTim J. Robbins  *    derived from this software without specific prior written permission
22ea0fabbcSTim J. Robbins  *
23ea0fabbcSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24ea0fabbcSTim J. Robbins  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25ea0fabbcSTim J. Robbins  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26ea0fabbcSTim J. Robbins  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27ea0fabbcSTim J. Robbins  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28ea0fabbcSTim J. Robbins  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29ea0fabbcSTim J. Robbins  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30ea0fabbcSTim J. Robbins  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31ea0fabbcSTim J. Robbins  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32ea0fabbcSTim J. Robbins  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33ea0fabbcSTim J. Robbins  */
34ea0fabbcSTim J. Robbins 
3502bf7e5eSTijl Coosemans #include "opt_compat.h"
3602bf7e5eSTijl Coosemans 
37ea0fabbcSTim J. Robbins #include <sys/cdefs.h>
38ea0fabbcSTim J. Robbins __FBSDID("$FreeBSD$");
39ea0fabbcSTim J. Robbins 
40841c0c7eSNathan Whitehorn #ifndef COMPAT_FREEBSD32
41841c0c7eSNathan Whitehorn #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
42ea0fabbcSTim J. Robbins #endif
43ea0fabbcSTim J. Robbins 
44ea0fabbcSTim J. Robbins #define	__ELF_WORD_SIZE	32
45ea0fabbcSTim J. Robbins 
46ea0fabbcSTim J. Robbins #include <sys/param.h>
47ea0fabbcSTim J. Robbins #include <sys/systm.h>
48ea0fabbcSTim J. Robbins #include <sys/exec.h>
4948b05c3fSKonstantin Belousov #include <sys/fcntl.h>
50ea0fabbcSTim J. Robbins #include <sys/imgact.h>
51ea0fabbcSTim J. Robbins #include <sys/imgact_elf.h>
52ea0fabbcSTim J. Robbins #include <sys/kernel.h>
53ea0fabbcSTim J. Robbins #include <sys/lock.h>
54ea0fabbcSTim J. Robbins #include <sys/malloc.h>
55ea0fabbcSTim J. Robbins #include <sys/module.h>
56ea0fabbcSTim J. Robbins #include <sys/mutex.h>
57ea0fabbcSTim J. Robbins #include <sys/proc.h>
586004362eSDavid Schultz #include <sys/resourcevar.h>
59ea0fabbcSTim J. Robbins #include <sys/signalvar.h>
60ea0fabbcSTim J. Robbins #include <sys/syscallsubr.h>
61c0aa0e2cSEd Maste #include <sys/sysctl.h>
62ea0fabbcSTim J. Robbins #include <sys/sysent.h>
63ea0fabbcSTim J. Robbins #include <sys/sysproto.h>
64ea0fabbcSTim J. Robbins #include <sys/vnode.h>
657c09e6c0SAlexander Leidinger #include <sys/eventhandler.h>
66ea0fabbcSTim J. Robbins 
67ea0fabbcSTim J. Robbins #include <vm/vm.h>
68ea0fabbcSTim J. Robbins #include <vm/pmap.h>
69ea0fabbcSTim J. Robbins #include <vm/vm_extern.h>
70ea0fabbcSTim J. Robbins #include <vm/vm_map.h>
71ea0fabbcSTim J. Robbins #include <vm/vm_object.h>
72ea0fabbcSTim J. Robbins #include <vm/vm_page.h>
73ea0fabbcSTim J. Robbins #include <vm/vm_param.h>
74ea0fabbcSTim J. Robbins 
75ea0fabbcSTim J. Robbins #include <machine/cpu.h>
76ea0fabbcSTim J. Robbins #include <machine/md_var.h>
776004362eSDavid Schultz #include <machine/pcb.h>
78ea0fabbcSTim J. Robbins #include <machine/specialreg.h>
79d41e41f9SJohn Baldwin #include <machine/trap.h>
80ea0fabbcSTim J. Robbins 
81ea0fabbcSTim J. Robbins #include <amd64/linux32/linux.h>
82ea0fabbcSTim J. Robbins #include <amd64/linux32/linux32_proto.h>
837c09e6c0SAlexander Leidinger #include <compat/linux/linux_emul.h>
84d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
85ea0fabbcSTim J. Robbins #include <compat/linux/linux_mib.h>
864d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
87ea0fabbcSTim J. Robbins #include <compat/linux/linux_signal.h>
88ea0fabbcSTim J. Robbins #include <compat/linux/linux_util.h>
89bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
90ea0fabbcSTim J. Robbins 
91ea0fabbcSTim J. Robbins MODULE_VERSION(linux, 1);
92ea0fabbcSTim J. Robbins 
930020bdf1SDmitry Chagin const char *linux_kplatform;
94bdc37934SDmitry Chagin static int linux_szsigcode;
95bdc37934SDmitry Chagin static vm_object_t linux_shared_page_obj;
96bdc37934SDmitry Chagin static char *linux_shared_page_mapping;
97bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_start;
98bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_end;
99ea0fabbcSTim J. Robbins 
1002f99bcceSJohn Baldwin extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
101ea0fabbcSTim J. Robbins 
102ea0fabbcSTim J. Robbins SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
103ea0fabbcSTim J. Robbins 
10431174518SJohn Baldwin static int	linux_fixup_elf(uintptr_t *stack_base,
105ea0fabbcSTim J. Robbins 		    struct image_params *iparams);
10603b0d68cSJohn Baldwin static int	linux_copyout_strings(struct image_params *imgp,
10731174518SJohn Baldwin 		    uintptr_t *stack_base);
1089104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
109dc858467SEd Maste static void	linux_exec_setregs(struct thread *td,
11031174518SJohn Baldwin 				   struct image_params *imgp, uintptr_t stack);
11119059a13SJohn Baldwin static void	linux32_fixlimit(struct rlimit *rl, int which);
112a95659f7SEd Maste static bool	linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
113bdc37934SDmitry Chagin static void	linux_vdso_install(void *param);
114bdc37934SDmitry Chagin static void	linux_vdso_deinstall(void *param);
115c26391f4SEdward Tomasz Napierala static void	linux32_set_syscall_retval(struct thread *td, int error);
116ea0fabbcSTim J. Robbins 
117ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN  255
118ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = {
119ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 0 */
120ea0fabbcSTim J. Robbins 	6,			/* 1  T_PRIVINFLT */
121ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 2 */
122ea0fabbcSTim J. Robbins 	3,			/* 3  T_BPTFLT */
123ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 4 */
124ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 5 */
125ea0fabbcSTim J. Robbins 	16,			/* 6  T_ARITHTRAP */
126ea0fabbcSTim J. Robbins 	254,			/* 7  T_ASTFLT */
127ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 8 */
128ea0fabbcSTim J. Robbins 	13,			/* 9  T_PROTFLT */
129ea0fabbcSTim J. Robbins 	1,			/* 10 T_TRCTRAP */
130ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 11 */
131ea0fabbcSTim J. Robbins 	14,			/* 12 T_PAGEFLT */
132ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 13 */
133ea0fabbcSTim J. Robbins 	17,			/* 14 T_ALIGNFLT */
134ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 15 */
135ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 16 */
136ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 17 */
137ea0fabbcSTim J. Robbins 	0,			/* 18 T_DIVIDE */
138ea0fabbcSTim J. Robbins 	2,			/* 19 T_NMI */
139ea0fabbcSTim J. Robbins 	4,			/* 20 T_OFLOW */
140ea0fabbcSTim J. Robbins 	5,			/* 21 T_BOUND */
141ea0fabbcSTim J. Robbins 	7,			/* 22 T_DNA */
142ea0fabbcSTim J. Robbins 	8,			/* 23 T_DOUBLEFLT */
143ea0fabbcSTim J. Robbins 	9,			/* 24 T_FPOPFLT */
144ea0fabbcSTim J. Robbins 	10,			/* 25 T_TSSFLT */
145ea0fabbcSTim J. Robbins 	11,			/* 26 T_SEGNPFLT */
146ea0fabbcSTim J. Robbins 	12,			/* 27 T_STKFLT */
147ea0fabbcSTim J. Robbins 	18,			/* 28 T_MCHK */
148ea0fabbcSTim J. Robbins 	19,			/* 29 T_XMMFLT */
149ea0fabbcSTim J. Robbins 	15			/* 30 T_RESERVED */
150ea0fabbcSTim J. Robbins };
151ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \
152ea24b056SPedro F. Giffuni     ((code)<nitems(_bsd_to_linux_trapcode)? \
153ea0fabbcSTim J. Robbins      _bsd_to_linux_trapcode[(code)]: \
154ea0fabbcSTim J. Robbins      LINUX_T_UNKNOWN)
155ea0fabbcSTim J. Robbins 
156ea0fabbcSTim J. Robbins struct linux32_ps_strings {
157ea0fabbcSTim J. Robbins 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
158f2c7668eSDavid Schultz 	u_int ps_nargvstr;	/* the number of argument strings */
159ea0fabbcSTim J. Robbins 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
160f2c7668eSDavid Schultz 	u_int ps_nenvstr;	/* the number of environment strings */
161ea0fabbcSTim J. Robbins };
162ea0fabbcSTim J. Robbins 
163bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
164bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
165bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
1660020bdf1SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
167bdc37934SDmitry Chagin 
168ea0fabbcSTim J. Robbins /*
169ea0fabbcSTim J. Robbins  * If FreeBSD & Linux have a difference of opinion about what a trap
170ea0fabbcSTim J. Robbins  * means, deal with it here.
171ea0fabbcSTim J. Robbins  *
172ea0fabbcSTim J. Robbins  * MPSAFE
173ea0fabbcSTim J. Robbins  */
174ea0fabbcSTim J. Robbins static int
175dc858467SEd Maste linux_translate_traps(int signal, int trap_code)
176ea0fabbcSTim J. Robbins {
177ea0fabbcSTim J. Robbins 	if (signal != SIGBUS)
178ad448975SEd Maste 		return (signal);
179ea0fabbcSTim J. Robbins 	switch (trap_code) {
180ea0fabbcSTim J. Robbins 	case T_PROTFLT:
181ea0fabbcSTim J. Robbins 	case T_TSSFLT:
182ea0fabbcSTim J. Robbins 	case T_DOUBLEFLT:
183ea0fabbcSTim J. Robbins 	case T_PAGEFLT:
184ad448975SEd Maste 		return (SIGSEGV);
185ea0fabbcSTim J. Robbins 	default:
186ad448975SEd Maste 		return (signal);
187ea0fabbcSTim J. Robbins 	}
188ea0fabbcSTim J. Robbins }
189ea0fabbcSTim J. Robbins 
19003b0d68cSJohn Baldwin static int
191d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
192ea0fabbcSTim J. Robbins {
193ea0fabbcSTim J. Robbins 	Elf32_Auxargs *args;
1945f77b8a8SBrooks Davis 	Elf32_Auxinfo *argarray, *pos;
19503b0d68cSJohn Baldwin 	int error, issetugid;
1964d7c2e8aSDmitry Chagin 
197ea0fabbcSTim J. Robbins 	args = (Elf32_Auxargs *)imgp->auxargs;
1985f77b8a8SBrooks Davis 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
1995f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
200ea0fabbcSTim J. Robbins 
201669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
202ca6e1fa3SEdward Tomasz Napierala 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
2035f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
204bdc37934SDmitry Chagin 	    imgp->proc->p_sysent->sv_shared_page_base);
2055f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
206ca6e1fa3SEdward Tomasz Napierala 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
2078d30f381SDmitry Chagin 
2088d30f381SDmitry Chagin 	/*
2098d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
2108d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
2118d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
2128d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
2138d30f381SDmitry Chagin 	 * is not present.
2148d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
2158d30f381SDmitry Chagin 	 */
2168d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
2175f77b8a8SBrooks Davis 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
2185f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
2195f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
2205f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
221ca6e1fa3SEdward Tomasz Napierala 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
2225f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
2235f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
2245f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
2255f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
2265f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
2275f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
228ca6e1fa3SEdward Tomasz Napierala 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
2294048f59cSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
230ca6e1fa3SEdward Tomasz Napierala 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
2314048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
2324048f59cSDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
2334d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
2345f77b8a8SBrooks Davis 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
235ca6e1fa3SEdward Tomasz Napierala 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
2365f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_NULL, 0);
237ea0fabbcSTim J. Robbins 
238ea0fabbcSTim J. Robbins 	free(imgp->auxargs, M_TEMP);
239ea0fabbcSTim J. Robbins 	imgp->auxargs = NULL;
2406362b1a6SJung-uk Kim 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
2415f77b8a8SBrooks Davis 
242d8010b11SJohn Baldwin 	error = copyout(argarray, (void *)base,
243d8010b11SJohn Baldwin 	    sizeof(*argarray) * LINUX_AT_COUNT);
2445f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
24503b0d68cSJohn Baldwin 	return (error);
2465caa67faSJohn Baldwin }
247ea0fabbcSTim J. Robbins 
2485caa67faSJohn Baldwin static int
24931174518SJohn Baldwin linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
2505caa67faSJohn Baldwin {
2515caa67faSJohn Baldwin 	Elf32_Addr *base;
2525caa67faSJohn Baldwin 
2535caa67faSJohn Baldwin 	base = (Elf32_Addr *)*stack_base;
254ea0fabbcSTim J. Robbins 	base--;
2555f77b8a8SBrooks Davis 	if (suword32(base, (uint32_t)imgp->args->argc) == -1)
2565f77b8a8SBrooks Davis 		return (EFAULT);
25731174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
258af682d48SDmitry Chagin 	return (0);
259ea0fabbcSTim J. Robbins }
260ea0fabbcSTim J. Robbins 
261ea0fabbcSTim J. Robbins static void
2629104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
263ea0fabbcSTim J. Robbins {
264ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
265ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
266ea0fabbcSTim J. Robbins 	struct sigacts *psp;
267ea0fabbcSTim J. Robbins 	struct trapframe *regs;
268ea0fabbcSTim J. Robbins 	struct l_rt_sigframe *fp, frame;
269ea0fabbcSTim J. Robbins 	int oonstack;
2709104847fSDavid Xu 	int sig;
2719104847fSDavid Xu 	int code;
272ea0fabbcSTim J. Robbins 
2739104847fSDavid Xu 	sig = ksi->ksi_signo;
2749104847fSDavid Xu 	code = ksi->ksi_code;
275ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
276ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
277ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
278ea0fabbcSTim J. Robbins 	regs = td->td_frame;
279ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
280ea0fabbcSTim J. Robbins 
2814ba25759SEd Maste 	/* Allocate space for the signal handler context. */
282ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
283ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
284aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
285ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
286ea0fabbcSTim J. Robbins 	} else
287ea0fabbcSTim J. Robbins 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
288ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
289ea0fabbcSTim J. Robbins 
2904ba25759SEd Maste 	/* Build the argument list for the signal handler. */
2914ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
292ea0fabbcSTim J. Robbins 
293ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
294ea0fabbcSTim J. Robbins 
295ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
296ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
297ea0fabbcSTim J. Robbins 	frame.sf_siginfo = PTROUT(&fp->sf_si);
298ea0fabbcSTim J. Robbins 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
299ea0fabbcSTim J. Robbins 
3004ba25759SEd Maste 	/* Fill in POSIX parts. */
301f4e80108SDmitry Chagin 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
302ea0fabbcSTim J. Robbins 
303ea0fabbcSTim J. Robbins 	/*
3044ba25759SEd Maste 	 * Build the signal context to be used by sigreturn and libgcc unwind.
305ea0fabbcSTim J. Robbins 	 */
306ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
307ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
308ea0fabbcSTim J. Robbins 
309ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
310ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
311ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
312ea0fabbcSTim J. Robbins 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
313ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
314ea0fabbcSTim J. Robbins 
315ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
316ea0fabbcSTim J. Robbins 
3174ab7403bSDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
318ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
319ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
320ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
321ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
322bdc37934SDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
323ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
324ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
325ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
326ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
327ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
3282c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
3292c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
3302c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
3312c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
332ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
333ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
334ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
335ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
33696a2b635SKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
337ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
338ea0fabbcSTim J. Robbins 
339ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
340ea0fabbcSTim J. Robbins 		/*
341ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
342ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
343ea0fabbcSTim J. Robbins 		 */
344ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
345ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
346ea0fabbcSTim J. Robbins 	}
347ea0fabbcSTim J. Robbins 
3484ba25759SEd Maste 	/* Build context to run handler in. */
349ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
350bdc37934SDmitry Chagin 	regs->tf_rip = linux32_rt_sigcode;
35122eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
352ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
353ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
3542c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
3552c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
3562c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
3572c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
3582c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
359e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
360ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
361ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
362ea0fabbcSTim J. Robbins }
363ea0fabbcSTim J. Robbins 
364ea0fabbcSTim J. Robbins /*
365ea0fabbcSTim J. Robbins  * Send an interrupt to process.
366ea0fabbcSTim J. Robbins  *
367ea0fabbcSTim J. Robbins  * Stack is set up to allow sigcode stored
368ea0fabbcSTim J. Robbins  * in u. to call routine, followed by kcall
369ea0fabbcSTim J. Robbins  * to sigreturn routine below.  After sigreturn
370ea0fabbcSTim J. Robbins  * resets the signal mask, the stack, and the
371ea0fabbcSTim J. Robbins  * frame pointer, it returns to the user
372ea0fabbcSTim J. Robbins  * specified pc, psl.
373ea0fabbcSTim J. Robbins  */
374ea0fabbcSTim J. Robbins static void
3759104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
376ea0fabbcSTim J. Robbins {
377ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
378ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
379ea0fabbcSTim J. Robbins 	struct sigacts *psp;
380ea0fabbcSTim J. Robbins 	struct trapframe *regs;
381ea0fabbcSTim J. Robbins 	struct l_sigframe *fp, frame;
382ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
3834ab7403bSDmitry Chagin 	int oonstack;
3849104847fSDavid Xu 	int sig, code;
385ea0fabbcSTim J. Robbins 
3869104847fSDavid Xu 	sig = ksi->ksi_signo;
3879104847fSDavid Xu 	code = ksi->ksi_code;
388ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
389ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
390ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
391ea0fabbcSTim J. Robbins 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
392ea0fabbcSTim J. Robbins 		/* Signal handler installed with SA_SIGINFO. */
3939104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
394ea0fabbcSTim J. Robbins 		return;
395ea0fabbcSTim J. Robbins 	}
396ea0fabbcSTim J. Robbins 
397ea0fabbcSTim J. Robbins 	regs = td->td_frame;
398ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
399ea0fabbcSTim J. Robbins 
4004ba25759SEd Maste 	/* Allocate space for the signal handler context. */
401ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
402ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
403aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
404ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
405ea0fabbcSTim J. Robbins 	} else
406ea0fabbcSTim J. Robbins 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
407ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
408ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
409ea0fabbcSTim J. Robbins 
4104ba25759SEd Maste 	/* Build the argument list for the signal handler. */
4114ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
412ea0fabbcSTim J. Robbins 
413ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
414ea0fabbcSTim J. Robbins 
415ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
416ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
417ea0fabbcSTim J. Robbins 
418ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &lmask);
419ea0fabbcSTim J. Robbins 
4204ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
4214ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
4222c66cccaSKonstantin Belousov 	frame.sf_sc.sc_gs     = regs->tf_gs;
4232c66cccaSKonstantin Belousov 	frame.sf_sc.sc_fs     = regs->tf_fs;
4242c66cccaSKonstantin Belousov 	frame.sf_sc.sc_es     = regs->tf_es;
4252c66cccaSKonstantin Belousov 	frame.sf_sc.sc_ds     = regs->tf_ds;
426ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edi    = regs->tf_rdi;
427ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esi    = regs->tf_rsi;
428ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
429ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
430bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_rsp;
431ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edx    = regs->tf_rdx;
432ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
433ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eax    = regs->tf_rax;
434ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eip    = regs->tf_rip;
435ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_cs     = regs->tf_cs;
436ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eflags = regs->tf_rflags;
437ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
438ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ss     = regs->tf_ss;
439ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_err    = regs->tf_err;
44096a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
441ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
442ea0fabbcSTim J. Robbins 
4434ab7403bSDmitry Chagin 	frame.sf_extramask[0] = lmask.__mask;
444ea0fabbcSTim J. Robbins 
445ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
446ea0fabbcSTim J. Robbins 		/*
447ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
448ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
449ea0fabbcSTim J. Robbins 		 */
450ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
451ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
452ea0fabbcSTim J. Robbins 	}
453ea0fabbcSTim J. Robbins 
4544ba25759SEd Maste 	/* Build context to run handler in. */
455ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
456bdc37934SDmitry Chagin 	regs->tf_rip = linux32_sigcode;
45722eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
458ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
459ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
4602c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
4612c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
4622c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
4632c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
4642c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
465e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
466ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
467ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
468ea0fabbcSTim J. Robbins }
469ea0fabbcSTim J. Robbins 
470ea0fabbcSTim J. Robbins /*
471ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
472ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
473ea0fabbcSTim J. Robbins  * stack state from context left by sendsig (above).
474ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
475ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
476ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
477ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
478ea0fabbcSTim J. Robbins  * a machine fault.
479ea0fabbcSTim J. Robbins  */
480ea0fabbcSTim J. Robbins int
481ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
482ea0fabbcSTim J. Robbins {
483ea0fabbcSTim J. Robbins 	struct l_sigframe frame;
484ea0fabbcSTim J. Robbins 	struct trapframe *regs;
485d6e029adSKonstantin Belousov 	sigset_t bmask;
486ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
4874ab7403bSDmitry Chagin 	int eflags;
4889104847fSDavid Xu 	ksiginfo_t ksi;
489ea0fabbcSTim J. Robbins 
490ea0fabbcSTim J. Robbins 	regs = td->td_frame;
491ea0fabbcSTim J. Robbins 
492ea0fabbcSTim J. Robbins 	/*
493ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the sigframe.
494ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
495ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
496ea0fabbcSTim J. Robbins 	 */
497ea0fabbcSTim J. Robbins 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
498ea0fabbcSTim J. Robbins 		return (EFAULT);
499ea0fabbcSTim J. Robbins 
5004ba25759SEd Maste 	/* Check for security violations. */
501ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
502ea0fabbcSTim J. Robbins 	eflags = frame.sf_sc.sc_eflags;
5033d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
504ea0fabbcSTim J. Robbins 		return(EINVAL);
505ea0fabbcSTim J. Robbins 
506ea0fabbcSTim J. Robbins 	/*
507ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
508ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
509ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
510ea0fabbcSTim J. Robbins 	 */
511ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
512ea0fabbcSTim J. Robbins 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
5139104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5149104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5159104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5169104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5179104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
5189104847fSDavid Xu 		trapsignal(td, &ksi);
519ea0fabbcSTim J. Robbins 		return(EINVAL);
520ea0fabbcSTim J. Robbins 	}
521ea0fabbcSTim J. Robbins 
5224ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_sc.sc_mask;
5234ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_extramask[0];
524d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&lmask, &bmask);
525d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
526ea0fabbcSTim J. Robbins 
5274ba25759SEd Maste 	/* Restore signal context. */
528ea0fabbcSTim J. Robbins 	regs->tf_rdi    = frame.sf_sc.sc_edi;
529ea0fabbcSTim J. Robbins 	regs->tf_rsi    = frame.sf_sc.sc_esi;
530ea0fabbcSTim J. Robbins 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
531ea0fabbcSTim J. Robbins 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
532ea0fabbcSTim J. Robbins 	regs->tf_rdx    = frame.sf_sc.sc_edx;
533ea0fabbcSTim J. Robbins 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
534ea0fabbcSTim J. Robbins 	regs->tf_rax    = frame.sf_sc.sc_eax;
535ea0fabbcSTim J. Robbins 	regs->tf_rip    = frame.sf_sc.sc_eip;
536ea0fabbcSTim J. Robbins 	regs->tf_cs     = frame.sf_sc.sc_cs;
5372c66cccaSKonstantin Belousov 	regs->tf_ds     = frame.sf_sc.sc_ds;
5382c66cccaSKonstantin Belousov 	regs->tf_es     = frame.sf_sc.sc_es;
5392c66cccaSKonstantin Belousov 	regs->tf_fs     = frame.sf_sc.sc_fs;
5402c66cccaSKonstantin Belousov 	regs->tf_gs     = frame.sf_sc.sc_gs;
541ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
542ea0fabbcSTim J. Robbins 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
543ea0fabbcSTim J. Robbins 	regs->tf_ss     = frame.sf_sc.sc_ss;
544e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
545ea0fabbcSTim J. Robbins 
546ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
547ea0fabbcSTim J. Robbins }
548ea0fabbcSTim J. Robbins 
549ea0fabbcSTim J. Robbins /*
550ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
551ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
552ea0fabbcSTim J. Robbins  * stack state from context left by rt_sendsig (above).
553ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
554ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
555ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
556ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
557ea0fabbcSTim J. Robbins  * a machine fault.
558ea0fabbcSTim J. Robbins  */
559ea0fabbcSTim J. Robbins int
560ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
561ea0fabbcSTim J. Robbins {
562ea0fabbcSTim J. Robbins 	struct l_ucontext uc;
563ea0fabbcSTim J. Robbins 	struct l_sigcontext *context;
564d6e029adSKonstantin Belousov 	sigset_t bmask;
565ea0fabbcSTim J. Robbins 	l_stack_t *lss;
566ea0fabbcSTim J. Robbins 	stack_t ss;
567ea0fabbcSTim J. Robbins 	struct trapframe *regs;
568ea0fabbcSTim J. Robbins 	int eflags;
5699104847fSDavid Xu 	ksiginfo_t ksi;
570ea0fabbcSTim J. Robbins 
571ea0fabbcSTim J. Robbins 	regs = td->td_frame;
572ea0fabbcSTim J. Robbins 
573ea0fabbcSTim J. Robbins 	/*
574ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the ucontext.
575ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
576ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
577ea0fabbcSTim J. Robbins 	 */
578ea0fabbcSTim J. Robbins 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
579ea0fabbcSTim J. Robbins 		return (EFAULT);
580ea0fabbcSTim J. Robbins 
581ea0fabbcSTim J. Robbins 	context = &uc.uc_mcontext;
582ea0fabbcSTim J. Robbins 
5834ba25759SEd Maste 	/* Check for security violations. */
584ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
585ea0fabbcSTim J. Robbins 	eflags = context->sc_eflags;
5863d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
587ea0fabbcSTim J. Robbins 		return(EINVAL);
588ea0fabbcSTim J. Robbins 
589ea0fabbcSTim J. Robbins 	/*
590ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
591ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
592ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
593ea0fabbcSTim J. Robbins 	 */
594ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
595ea0fabbcSTim J. Robbins 	if (!CS_SECURE(context->sc_cs)) {
5969104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5979104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5989104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5999104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
6009104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
6019104847fSDavid Xu 		trapsignal(td, &ksi);
602ea0fabbcSTim J. Robbins 		return(EINVAL);
603ea0fabbcSTim J. Robbins 	}
604ea0fabbcSTim J. Robbins 
605d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
606d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
607ea0fabbcSTim J. Robbins 
608ea0fabbcSTim J. Robbins 	/*
609ea0fabbcSTim J. Robbins 	 * Restore signal context
610ea0fabbcSTim J. Robbins 	 */
6112c66cccaSKonstantin Belousov 	regs->tf_gs	= context->sc_gs;
6122c66cccaSKonstantin Belousov 	regs->tf_fs	= context->sc_fs;
6132c66cccaSKonstantin Belousov 	regs->tf_es	= context->sc_es;
6142c66cccaSKonstantin Belousov 	regs->tf_ds	= context->sc_ds;
615ea0fabbcSTim J. Robbins 	regs->tf_rdi    = context->sc_edi;
616ea0fabbcSTim J. Robbins 	regs->tf_rsi    = context->sc_esi;
617ea0fabbcSTim J. Robbins 	regs->tf_rbp    = context->sc_ebp;
618ea0fabbcSTim J. Robbins 	regs->tf_rbx    = context->sc_ebx;
619ea0fabbcSTim J. Robbins 	regs->tf_rdx    = context->sc_edx;
620ea0fabbcSTim J. Robbins 	regs->tf_rcx    = context->sc_ecx;
621ea0fabbcSTim J. Robbins 	regs->tf_rax    = context->sc_eax;
622ea0fabbcSTim J. Robbins 	regs->tf_rip    = context->sc_eip;
623ea0fabbcSTim J. Robbins 	regs->tf_cs     = context->sc_cs;
624ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
625ea0fabbcSTim J. Robbins 	regs->tf_rsp    = context->sc_esp_at_signal;
626ea0fabbcSTim J. Robbins 	regs->tf_ss     = context->sc_ss;
627e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
628ea0fabbcSTim J. Robbins 
629ea0fabbcSTim J. Robbins 	/*
630ea0fabbcSTim J. Robbins 	 * call sigaltstack & ignore results..
631ea0fabbcSTim J. Robbins 	 */
632ea0fabbcSTim J. Robbins 	lss = &uc.uc_stack;
633ea0fabbcSTim J. Robbins 	ss.ss_sp = PTRIN(lss->ss_sp);
634ea0fabbcSTim J. Robbins 	ss.ss_size = lss->ss_size;
635ea0fabbcSTim J. Robbins 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
636ea0fabbcSTim J. Robbins 
637ea0fabbcSTim J. Robbins 	(void)kern_sigaltstack(td, &ss, NULL);
638ea0fabbcSTim J. Robbins 
639ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
640ea0fabbcSTim J. Robbins }
641ea0fabbcSTim J. Robbins 
642afe1a688SKonstantin Belousov static int
6432d88da2fSKonstantin Belousov linux32_fetch_syscall_args(struct thread *td)
644ea0fabbcSTim J. Robbins {
645afe1a688SKonstantin Belousov 	struct proc *p;
646afe1a688SKonstantin Belousov 	struct trapframe *frame;
6472d88da2fSKonstantin Belousov 	struct syscall_args *sa;
648afe1a688SKonstantin Belousov 
649afe1a688SKonstantin Belousov 	p = td->td_proc;
650afe1a688SKonstantin Belousov 	frame = td->td_frame;
6512d88da2fSKonstantin Belousov 	sa = &td->td_sa;
652afe1a688SKonstantin Belousov 
653afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_rbx;
654afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_rcx;
655afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_rdx;
656afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_rsi;
657afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_rdi;
658afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
659afe1a688SKonstantin Belousov 	sa->code = frame->tf_rax;
660afe1a688SKonstantin Belousov 
661afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
662fcdffc03SDmitry Chagin 		/* nosys */
6635047105bSJohn Baldwin 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
664afe1a688SKonstantin Belousov 	else
665afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
666afe1a688SKonstantin Belousov 
667afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
668afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_rdx;
669afe1a688SKonstantin Belousov 
670afe1a688SKonstantin Belousov 	return (0);
671ea0fabbcSTim J. Robbins }
672ea0fabbcSTim J. Robbins 
673c26391f4SEdward Tomasz Napierala static void
674c26391f4SEdward Tomasz Napierala linux32_set_syscall_retval(struct thread *td, int error)
675c26391f4SEdward Tomasz Napierala {
676c26391f4SEdward Tomasz Napierala 	struct trapframe *frame = td->td_frame;
677c26391f4SEdward Tomasz Napierala 
678c26391f4SEdward Tomasz Napierala 	cpu_set_syscall_retval(td, error);
679c26391f4SEdward Tomasz Napierala 
680c26391f4SEdward Tomasz Napierala 	if (__predict_false(error != 0)) {
681c26391f4SEdward Tomasz Napierala 		if (error != ERESTART && error != EJUSTRETURN)
682866b1f51SEdward Tomasz Napierala 			frame->tf_rax = bsd_to_linux_errno(error);
683c26391f4SEdward Tomasz Napierala 	}
684c26391f4SEdward Tomasz Napierala }
685c26391f4SEdward Tomasz Napierala 
686ea0fabbcSTim J. Robbins /*
687ea0fabbcSTim J. Robbins  * Clear registers on exec
688ea0fabbcSTim J. Robbins  * XXX copied from ia32_signal.c.
689ea0fabbcSTim J. Robbins  */
690ea0fabbcSTim J. Robbins static void
69131174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
69231174518SJohn Baldwin     uintptr_t stack)
693ea0fabbcSTim J. Robbins {
694ea0fabbcSTim J. Robbins 	struct trapframe *regs = td->td_frame;
695ea0fabbcSTim J. Robbins 	struct pcb *pcb = td->td_pcb;
6967c5d1690SKonstantin Belousov 	register_t saved_rflags;
6977c5d1690SKonstantin Belousov 
6987c5d1690SKonstantin Belousov 	regs = td->td_frame;
6997c5d1690SKonstantin Belousov 	pcb = td->td_pcb;
700ea0fabbcSTim J. Robbins 
7012c66cccaSKonstantin Belousov 	if (td->td_proc->p_md.md_ldt != NULL)
7022c66cccaSKonstantin Belousov 		user_ldt_free(td);
7032c66cccaSKonstantin Belousov 
7049c5b213eSJung-uk Kim 	critical_enter();
705ea0fabbcSTim J. Robbins 	wrmsr(MSR_FSBASE, 0);
706ea0fabbcSTim J. Robbins 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
707ea0fabbcSTim J. Robbins 	pcb->pcb_fsbase = 0;
708ea0fabbcSTim J. Robbins 	pcb->pcb_gsbase = 0;
7099c5b213eSJung-uk Kim 	critical_exit();
7102ee8325fSJohn Baldwin 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
711ea0fabbcSTim J. Robbins 
7127c5d1690SKonstantin Belousov 	saved_rflags = regs->tf_rflags & PSL_T;
713ea0fabbcSTim J. Robbins 	bzero((char *)regs, sizeof(struct trapframe));
714a107d8aaSNathan Whitehorn 	regs->tf_rip = imgp->entry_addr;
715ea0fabbcSTim J. Robbins 	regs->tf_rsp = stack;
7167c5d1690SKonstantin Belousov 	regs->tf_rflags = PSL_USER | saved_rflags;
7172c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
7182c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
7192c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
7202c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
721ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
7222c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
723ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
724397df744SBrooks Davis 	regs->tf_rbx = (register_t)imgp->ps_strings;
725bdbf2db5SJung-uk Kim 
72694172affSKonstantin Belousov 	x86_clear_dbregs(pcb);
72794172affSKonstantin Belousov 
7282a988f7cSStephan Uphoff 	fpstate_drop(td);
729ea0fabbcSTim J. Robbins 
7301b3c3256SKonstantin Belousov 	/* Do full restore on return so that we can change to a different %cs */
731e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
732ea0fabbcSTim J. Robbins }
733ea0fabbcSTim J. Robbins 
734ea0fabbcSTim J. Robbins /*
735ea0fabbcSTim J. Robbins  * XXX copied from ia32_sysvec.c.
736ea0fabbcSTim J. Robbins  */
73703b0d68cSJohn Baldwin static int
73831174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
739ea0fabbcSTim J. Robbins {
74003b0d68cSJohn Baldwin 	int argc, envc, error;
741ea0fabbcSTim J. Robbins 	u_int32_t *vectp;
74231174518SJohn Baldwin 	char *stringp;
74331174518SJohn Baldwin 	uintptr_t destp, ustringp;
744ea0fabbcSTim J. Robbins 	struct linux32_ps_strings *arginfo;
7454048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
7464048f59cSDmitry Chagin 	size_t execpath_len;
747ea0fabbcSTim J. Robbins 
7484ba25759SEd Maste 	/* Calculate string base and vector table pointers. */
7494048f59cSDmitry Chagin 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
7504048f59cSDmitry Chagin 		execpath_len = strlen(imgp->execpath) + 1;
7514048f59cSDmitry Chagin 	else
7524048f59cSDmitry Chagin 		execpath_len = 0;
7534048f59cSDmitry Chagin 
754ea0fabbcSTim J. Robbins 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
75531174518SJohn Baldwin 	destp = (uintptr_t)arginfo;
756ea0fabbcSTim J. Robbins 
7574048f59cSDmitry Chagin 	if (execpath_len != 0) {
75831174518SJohn Baldwin 		destp -= execpath_len;
75923a5b4edSJohn Baldwin 		destp = rounddown2(destp, sizeof(uint32_t));
760b24e6ac8SBrooks Davis 		imgp->execpathp = (void *)destp;
761b24e6ac8SBrooks Davis 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
76203b0d68cSJohn Baldwin 		if (error != 0)
76303b0d68cSJohn Baldwin 			return (error);
7644048f59cSDmitry Chagin 	}
7654048f59cSDmitry Chagin 
7664ba25759SEd Maste 	/* Prepare the canary for SSP. */
7674048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
76823a5b4edSJohn Baldwin 	destp -= roundup(sizeof(canary), sizeof(uint32_t));
769b24e6ac8SBrooks Davis 	imgp->canary = (void *)destp;
770b24e6ac8SBrooks Davis 	error = copyout(canary, imgp->canary, sizeof(canary));
77103b0d68cSJohn Baldwin 	if (error != 0)
77203b0d68cSJohn Baldwin 		return (error);
7734048f59cSDmitry Chagin 
77431174518SJohn Baldwin 	/* Allocate room for the argument and environment strings. */
77531174518SJohn Baldwin 	destp -= ARG_MAX - imgp->args->stringspace;
77623a5b4edSJohn Baldwin 	destp = rounddown2(destp, sizeof(uint32_t));
77731174518SJohn Baldwin 	ustringp = destp;
77831174518SJohn Baldwin 
77903b0d68cSJohn Baldwin 	if (imgp->auxargs) {
780d8010b11SJohn Baldwin 		/*
781d8010b11SJohn Baldwin 		 * Allocate room on the stack for the ELF auxargs
782d8010b11SJohn Baldwin 		 * array.  It has LINUX_AT_COUNT entries.
783d8010b11SJohn Baldwin 		 */
784d8010b11SJohn Baldwin 		destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
78523a5b4edSJohn Baldwin 		destp = rounddown2(destp, sizeof(uint32_t));
78603b0d68cSJohn Baldwin 	}
787ea0fabbcSTim J. Robbins 
78831174518SJohn Baldwin 	vectp = (uint32_t *)destp;
78931174518SJohn Baldwin 
79073c8686eSJohn Baldwin 	/*
79173c8686eSJohn Baldwin 	 * Allocate room for the argv[] and env vectors including the
79273c8686eSJohn Baldwin 	 * terminating NULL pointers.
79373c8686eSJohn Baldwin 	 */
79473c8686eSJohn Baldwin 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
79573c8686eSJohn Baldwin 
7964ba25759SEd Maste 	/* vectp also becomes our initial stack base. */
79731174518SJohn Baldwin 	*stack_base = (uintptr_t)vectp;
798ea0fabbcSTim J. Robbins 
799610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
800610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
801610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
80231174518SJohn Baldwin 
8034ba25759SEd Maste 	/* Copy out strings - arguments and environment. */
80431174518SJohn Baldwin 	error = copyout(stringp, (void *)ustringp,
80531174518SJohn Baldwin 	    ARG_MAX - imgp->args->stringspace);
80603b0d68cSJohn Baldwin 	if (error != 0)
80703b0d68cSJohn Baldwin 		return (error);
808ea0fabbcSTim J. Robbins 
8094ba25759SEd Maste 	/* Fill in "ps_strings" struct for ps, w, etc. */
81003b0d68cSJohn Baldwin 	if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
81103b0d68cSJohn Baldwin 	    suword32(&arginfo->ps_nargvstr, argc) != 0)
81203b0d68cSJohn Baldwin 		return (EFAULT);
813ea0fabbcSTim J. Robbins 
8144ba25759SEd Maste 	/* Fill in argument portion of vector table. */
815ea0fabbcSTim J. Robbins 	for (; argc > 0; --argc) {
81631174518SJohn Baldwin 		if (suword32(vectp++, ustringp) != 0)
81703b0d68cSJohn Baldwin 			return (EFAULT);
818ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
81931174518SJohn Baldwin 			ustringp++;
82031174518SJohn Baldwin 		ustringp++;
821ea0fabbcSTim J. Robbins 	}
822ea0fabbcSTim J. Robbins 
8234ba25759SEd Maste 	/* A null vector table pointer separates the argp's from the envp's. */
82403b0d68cSJohn Baldwin 	if (suword32(vectp++, 0) != 0)
82503b0d68cSJohn Baldwin 		return (EFAULT);
826ea0fabbcSTim J. Robbins 
82703b0d68cSJohn Baldwin 	if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
82803b0d68cSJohn Baldwin 	    suword32(&arginfo->ps_nenvstr, envc) != 0)
82903b0d68cSJohn Baldwin 		return (EFAULT);
830ea0fabbcSTim J. Robbins 
8314ba25759SEd Maste 	/* Fill in environment portion of vector table. */
832ea0fabbcSTim J. Robbins 	for (; envc > 0; --envc) {
83331174518SJohn Baldwin 		if (suword32(vectp++, ustringp) != 0)
83403b0d68cSJohn Baldwin 			return (EFAULT);
835ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
83631174518SJohn Baldwin 			ustringp++;
83731174518SJohn Baldwin 		ustringp++;
838ea0fabbcSTim J. Robbins 	}
839ea0fabbcSTim J. Robbins 
8404ba25759SEd Maste 	/* The end of the vector table is a null pointer. */
84103b0d68cSJohn Baldwin 	if (suword32(vectp, 0) != 0)
84203b0d68cSJohn Baldwin 		return (EFAULT);
843ea0fabbcSTim J. Robbins 
844d8010b11SJohn Baldwin 	if (imgp->auxargs) {
845d8010b11SJohn Baldwin 		vectp++;
846d8010b11SJohn Baldwin 		error = imgp->sysent->sv_copyout_auxargs(imgp,
847d8010b11SJohn Baldwin 		    (uintptr_t)vectp);
848d8010b11SJohn Baldwin 		if (error != 0)
849d8010b11SJohn Baldwin 			return (error);
850d8010b11SJohn Baldwin 	}
851d8010b11SJohn Baldwin 
85203b0d68cSJohn Baldwin 	return (0);
853ea0fabbcSTim J. Robbins }
854ea0fabbcSTim J. Robbins 
8557029da5cSPawel Biernacki static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
856ea0fabbcSTim J. Robbins     "32-bit Linux emulation");
857ea0fabbcSTim J. Robbins 
858ea0fabbcSTim J. Robbins static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
859ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
860ea0fabbcSTim J. Robbins     &linux32_maxdsiz, 0, "");
861ea0fabbcSTim J. Robbins static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
862ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
863ea0fabbcSTim J. Robbins     &linux32_maxssiz, 0, "");
864ea0fabbcSTim J. Robbins static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
865ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
866ea0fabbcSTim J. Robbins     &linux32_maxvmem, 0, "");
867ea0fabbcSTim J. Robbins 
868ea0fabbcSTim J. Robbins static void
86919059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which)
870ea0fabbcSTim J. Robbins {
871ea0fabbcSTim J. Robbins 
87219059a13SJohn Baldwin 	switch (which) {
87319059a13SJohn Baldwin 	case RLIMIT_DATA:
874ea0fabbcSTim J. Robbins 		if (linux32_maxdsiz != 0) {
87519059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxdsiz)
87619059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxdsiz;
87719059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxdsiz)
87819059a13SJohn Baldwin 				rl->rlim_max = linux32_maxdsiz;
879ea0fabbcSTim J. Robbins 		}
88019059a13SJohn Baldwin 		break;
88119059a13SJohn Baldwin 	case RLIMIT_STACK:
882ea0fabbcSTim J. Robbins 		if (linux32_maxssiz != 0) {
88319059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxssiz)
88419059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxssiz;
88519059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxssiz)
88619059a13SJohn Baldwin 				rl->rlim_max = linux32_maxssiz;
887ea0fabbcSTim J. Robbins 		}
88819059a13SJohn Baldwin 		break;
88919059a13SJohn Baldwin 	case RLIMIT_VMEM:
890ea0fabbcSTim J. Robbins 		if (linux32_maxvmem != 0) {
89119059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxvmem)
89219059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxvmem;
89319059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxvmem)
89419059a13SJohn Baldwin 				rl->rlim_max = linux32_maxvmem;
895ea0fabbcSTim J. Robbins 		}
89619059a13SJohn Baldwin 		break;
89719059a13SJohn Baldwin 	}
898ea0fabbcSTim J. Robbins }
899ea0fabbcSTim J. Robbins 
900ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = {
9012f99bcceSJohn Baldwin 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
9022f99bcceSJohn Baldwin 	.sv_table	= linux32_sysent,
903dc858467SEd Maste 	.sv_transtrap	= linux_translate_traps,
904dc858467SEd Maste 	.sv_fixup	= linux_fixup_elf,
905a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
906bdc37934SDmitry Chagin 	.sv_sigcode	= &_binary_linux32_locore_o_start,
907a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
908a8d403e1SKonstantin Belousov 	.sv_name	= "Linux ELF32",
909a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
910dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
911a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
912a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
9138f1e49a6SDmitry Chagin 	.sv_maxuser	= LINUX32_MAXUSER,
914a8d403e1SKonstantin Belousov 	.sv_usrstack	= LINUX32_USRSTACK,
915a8d403e1SKonstantin Belousov 	.sv_psstrings	= LINUX32_PS_STRINGS,
916a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
9175caa67faSJohn Baldwin 	.sv_copyout_auxargs = linux_copyout_auxargs,
918a8d403e1SKonstantin Belousov 	.sv_copyout_strings = linux_copyout_strings,
919dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
920a8d403e1SKonstantin Belousov 	.sv_fixlimit	= linux32_fixlimit,
921a8d403e1SKonstantin Belousov 	.sv_maxssiz	= &linux32_maxssiz,
9228f1e49a6SDmitry Chagin 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
923c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux32_set_syscall_retval,
924afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
925afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
9268f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
9278f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
928e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
92981338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
930038c7205SDmitry Chagin 	.sv_trap	= NULL,
9314815f175SKonstantin Belousov 	.sv_onexec	= linux_on_exec,
9324815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
9334815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
934*598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
935ea0fabbcSTim J. Robbins };
936bdc37934SDmitry Chagin 
937bdc37934SDmitry Chagin static void
938bdc37934SDmitry Chagin linux_vdso_install(void *param)
939bdc37934SDmitry Chagin {
940bdc37934SDmitry Chagin 
941bdc37934SDmitry Chagin 	linux_szsigcode = (&_binary_linux32_locore_o_end -
942bdc37934SDmitry Chagin 	    &_binary_linux32_locore_o_start);
943bdc37934SDmitry Chagin 
944bdc37934SDmitry Chagin 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
945bdc37934SDmitry Chagin 		panic("Linux invalid vdso size\n");
946bdc37934SDmitry Chagin 
947bdc37934SDmitry Chagin 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
948bdc37934SDmitry Chagin 
949bdc37934SDmitry Chagin 	linux_shared_page_obj = __elfN(linux_shared_page_init)
950bdc37934SDmitry Chagin 	    (&linux_shared_page_mapping);
951bdc37934SDmitry Chagin 
952c151945cSDmitry Chagin 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec);
953bdc37934SDmitry Chagin 
954bdc37934SDmitry Chagin 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
955bdc37934SDmitry Chagin 	    linux_szsigcode);
956bdc37934SDmitry Chagin 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
9570020bdf1SDmitry Chagin 
9580020bdf1SDmitry Chagin 	linux_kplatform = linux_shared_page_mapping +
959c151945cSDmitry Chagin 	    (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
960bdc37934SDmitry Chagin }
961bdc37934SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
962f8268d4dSEd Maste     linux_vdso_install, NULL);
963bdc37934SDmitry Chagin 
964bdc37934SDmitry Chagin static void
965bdc37934SDmitry Chagin linux_vdso_deinstall(void *param)
966bdc37934SDmitry Chagin {
967bdc37934SDmitry Chagin 
9680fc8a796SMark Johnston 	__elfN(linux_shared_page_fini)(linux_shared_page_obj,
9690fc8a796SMark Johnston 	    linux_shared_page_mapping);
9707b194b3dSEd Maste }
971bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
972f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
973ea0fabbcSTim J. Robbins 
97489ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
97589ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
97689ffc202SBjoern A. Zeeb 
977a95659f7SEd Maste static bool
97889ffc202SBjoern A. Zeeb linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
97989ffc202SBjoern A. Zeeb {
98089ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
98189ffc202SBjoern A. Zeeb 	uintptr_t p;
98289ffc202SBjoern A. Zeeb 
98389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
98489ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
98589ffc202SBjoern A. Zeeb 
98689ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
98789ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
988a95659f7SEd Maste 		return (false);
98989ffc202SBjoern A. Zeeb 
99089ffc202SBjoern A. Zeeb 	/*
99135755049SChuck Tuffli 	 * For Linux we encode osrel using the Linux convention of
99235755049SChuck Tuffli 	 * 	(version << 16) | (major << 8) | (minor)
99335755049SChuck Tuffli 	 * See macro in linux_mib.h
99489ffc202SBjoern A. Zeeb 	 */
99535755049SChuck Tuffli 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
99689ffc202SBjoern A. Zeeb 
997a95659f7SEd Maste 	return (true);
99889ffc202SBjoern A. Zeeb }
99932c01de2SDmitry Chagin 
100032c01de2SDmitry Chagin static Elf_Brandnote linux32_brandnote = {
100189ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
100289ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
100332c01de2SDmitry Chagin 	.hdr.n_type	= 1,
100489ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
100589ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
100689ffc202SBjoern A. Zeeb 	.trans_osrel	= linux32_trans_osrel
100732c01de2SDmitry Chagin };
100832c01de2SDmitry Chagin 
1009ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = {
1010a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1011a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1012a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1013b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1014a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
1015a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1016a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
101732c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10182dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1019ea0fabbcSTim J. Robbins };
1020ea0fabbcSTim J. Robbins 
1021ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = {
1022a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1023a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1024a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1025b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1026a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1027a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1028a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
102932c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10302dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1031ea0fabbcSTim J. Robbins };
1032ea0fabbcSTim J. Robbins 
1033a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1034a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1035a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1036a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1037b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1038a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1039a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1040a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1041a0c59c7aSDmitry Chagin 	.brand_note	= &linux32_brandnote,
1042a0c59c7aSDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1043a0c59c7aSDmitry Chagin };
1044a0c59c7aSDmitry Chagin 
1045ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = {
1046ea0fabbcSTim J. Robbins 	&linux_brand,
1047ea0fabbcSTim J. Robbins 	&linux_glibc2brand,
1048a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1049ea0fabbcSTim J. Robbins 	NULL
1050ea0fabbcSTim J. Robbins };
1051ea0fabbcSTim J. Robbins 
1052ea0fabbcSTim J. Robbins static int
1053ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data)
1054ea0fabbcSTim J. Robbins {
1055ea0fabbcSTim J. Robbins 	Elf32_Brandinfo **brandinfo;
1056ea0fabbcSTim J. Robbins 	int error;
1057ea0fabbcSTim J. Robbins 	struct linux_ioctl_handler **lihp;
1058ea0fabbcSTim J. Robbins 
1059ea0fabbcSTim J. Robbins 	error = 0;
1060ea0fabbcSTim J. Robbins 
1061ea0fabbcSTim J. Robbins 	switch(type) {
1062ea0fabbcSTim J. Robbins 	case MOD_LOAD:
1063ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1064ea0fabbcSTim J. Robbins 		     ++brandinfo)
1065ea0fabbcSTim J. Robbins 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1066ea0fabbcSTim J. Robbins 				error = EINVAL;
1067ea0fabbcSTim J. Robbins 		if (error == 0) {
1068ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
10698fc08087STijl Coosemans 				linux32_ioctl_register_handler(*lihp);
10701ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
1071ea0fabbcSTim J. Robbins 			if (bootverbose)
1072ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler installed\n");
1073ea0fabbcSTim J. Robbins 		} else
1074ea0fabbcSTim J. Robbins 			printf("cannot insert Linux ELF brand handler\n");
1075ea0fabbcSTim J. Robbins 		break;
1076ea0fabbcSTim J. Robbins 	case MOD_UNLOAD:
1077ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1078ea0fabbcSTim J. Robbins 		     ++brandinfo)
1079ea0fabbcSTim J. Robbins 			if (elf32_brand_inuse(*brandinfo))
1080ea0fabbcSTim J. Robbins 				error = EBUSY;
1081ea0fabbcSTim J. Robbins 		if (error == 0) {
1082ea0fabbcSTim J. Robbins 			for (brandinfo = &linux_brandlist[0];
1083ea0fabbcSTim J. Robbins 			     *brandinfo != NULL; ++brandinfo)
1084ea0fabbcSTim J. Robbins 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1085ea0fabbcSTim J. Robbins 					error = EINVAL;
1086ea0fabbcSTim J. Robbins 		}
1087ea0fabbcSTim J. Robbins 		if (error == 0) {
1088ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
10898fc08087STijl Coosemans 				linux32_ioctl_unregister_handler(*lihp);
1090ea0fabbcSTim J. Robbins 			if (bootverbose)
1091ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler removed\n");
1092ea0fabbcSTim J. Robbins 		} else
1093ea0fabbcSTim J. Robbins 			printf("Could not deinstall ELF interpreter entry\n");
1094ea0fabbcSTim J. Robbins 		break;
1095ea0fabbcSTim J. Robbins 	default:
1096af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1097ea0fabbcSTim J. Robbins 	}
1098af682d48SDmitry Chagin 	return (error);
1099ea0fabbcSTim J. Robbins }
1100ea0fabbcSTim J. Robbins 
1101ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = {
1102ea0fabbcSTim J. Robbins 	"linuxelf",
1103ea0fabbcSTim J. Robbins 	linux_elf_modevent,
11049823d527SKevin Lo 	0
1105ea0fabbcSTim J. Robbins };
1106ea0fabbcSTim J. Robbins 
110778ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
110867d39748SDmitry Chagin MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1109b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
1110