xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
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);
115ea0fabbcSTim J. Robbins 
116ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN  255
117ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = {
118ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 0 */
119ea0fabbcSTim J. Robbins 	6,			/* 1  T_PRIVINFLT */
120ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 2 */
121ea0fabbcSTim J. Robbins 	3,			/* 3  T_BPTFLT */
122ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 4 */
123ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 5 */
124ea0fabbcSTim J. Robbins 	16,			/* 6  T_ARITHTRAP */
125ea0fabbcSTim J. Robbins 	254,			/* 7  T_ASTFLT */
126ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 8 */
127ea0fabbcSTim J. Robbins 	13,			/* 9  T_PROTFLT */
128ea0fabbcSTim J. Robbins 	1,			/* 10 T_TRCTRAP */
129ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 11 */
130ea0fabbcSTim J. Robbins 	14,			/* 12 T_PAGEFLT */
131ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 13 */
132ea0fabbcSTim J. Robbins 	17,			/* 14 T_ALIGNFLT */
133ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 15 */
134ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 16 */
135ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 17 */
136ea0fabbcSTim J. Robbins 	0,			/* 18 T_DIVIDE */
137ea0fabbcSTim J. Robbins 	2,			/* 19 T_NMI */
138ea0fabbcSTim J. Robbins 	4,			/* 20 T_OFLOW */
139ea0fabbcSTim J. Robbins 	5,			/* 21 T_BOUND */
140ea0fabbcSTim J. Robbins 	7,			/* 22 T_DNA */
141ea0fabbcSTim J. Robbins 	8,			/* 23 T_DOUBLEFLT */
142ea0fabbcSTim J. Robbins 	9,			/* 24 T_FPOPFLT */
143ea0fabbcSTim J. Robbins 	10,			/* 25 T_TSSFLT */
144ea0fabbcSTim J. Robbins 	11,			/* 26 T_SEGNPFLT */
145ea0fabbcSTim J. Robbins 	12,			/* 27 T_STKFLT */
146ea0fabbcSTim J. Robbins 	18,			/* 28 T_MCHK */
147ea0fabbcSTim J. Robbins 	19,			/* 29 T_XMMFLT */
148ea0fabbcSTim J. Robbins 	15			/* 30 T_RESERVED */
149ea0fabbcSTim J. Robbins };
150ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \
151ea24b056SPedro F. Giffuni     ((code)<nitems(_bsd_to_linux_trapcode)? \
152ea0fabbcSTim J. Robbins      _bsd_to_linux_trapcode[(code)]: \
153ea0fabbcSTim J. Robbins      LINUX_T_UNKNOWN)
154ea0fabbcSTim J. Robbins 
155ea0fabbcSTim J. Robbins struct linux32_ps_strings {
156ea0fabbcSTim J. Robbins 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
157f2c7668eSDavid Schultz 	u_int ps_nargvstr;	/* the number of argument strings */
158ea0fabbcSTim J. Robbins 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
159f2c7668eSDavid Schultz 	u_int ps_nenvstr;	/* the number of environment strings */
160ea0fabbcSTim J. Robbins };
161ea0fabbcSTim J. Robbins 
162bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
163bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
164bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
1650020bdf1SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
166bdc37934SDmitry Chagin 
167ea0fabbcSTim J. Robbins /*
168ea0fabbcSTim J. Robbins  * If FreeBSD & Linux have a difference of opinion about what a trap
169ea0fabbcSTim J. Robbins  * means, deal with it here.
170ea0fabbcSTim J. Robbins  *
171ea0fabbcSTim J. Robbins  * MPSAFE
172ea0fabbcSTim J. Robbins  */
173ea0fabbcSTim J. Robbins static int
174dc858467SEd Maste linux_translate_traps(int signal, int trap_code)
175ea0fabbcSTim J. Robbins {
176ea0fabbcSTim J. Robbins 	if (signal != SIGBUS)
177ad448975SEd Maste 		return (signal);
178ea0fabbcSTim J. Robbins 	switch (trap_code) {
179ea0fabbcSTim J. Robbins 	case T_PROTFLT:
180ea0fabbcSTim J. Robbins 	case T_TSSFLT:
181ea0fabbcSTim J. Robbins 	case T_DOUBLEFLT:
182ea0fabbcSTim J. Robbins 	case T_PAGEFLT:
183ad448975SEd Maste 		return (SIGSEGV);
184ea0fabbcSTim J. Robbins 	default:
185ad448975SEd Maste 		return (signal);
186ea0fabbcSTim J. Robbins 	}
187ea0fabbcSTim J. Robbins }
188ea0fabbcSTim J. Robbins 
18903b0d68cSJohn Baldwin static int
190d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
191ea0fabbcSTim J. Robbins {
192ea0fabbcSTim J. Robbins 	Elf32_Auxargs *args;
1935f77b8a8SBrooks Davis 	Elf32_Auxinfo *argarray, *pos;
19403b0d68cSJohn Baldwin 	int error, issetugid;
1954d7c2e8aSDmitry Chagin 
196ea0fabbcSTim J. Robbins 	args = (Elf32_Auxargs *)imgp->auxargs;
1975f77b8a8SBrooks Davis 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
1985f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
199ea0fabbcSTim J. Robbins 
200669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
2015f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
202bdc37934SDmitry Chagin 	    imgp->proc->p_sysent->sv_shared_page_base);
2035f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
2045f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
2058d30f381SDmitry Chagin 
2068d30f381SDmitry Chagin 	/*
2078d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
2088d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
2098d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
2108d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
2118d30f381SDmitry Chagin 	 * is not present.
2128d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
2138d30f381SDmitry Chagin 	 */
2148d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
2155f77b8a8SBrooks Davis 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
2165f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
2175f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
2185f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
2195f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
2205f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
2215f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
2225f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
2235f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
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);
2285f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
2294048f59cSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
2304048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
2314048f59cSDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
2324d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
2335f77b8a8SBrooks Davis 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
2345f77b8a8SBrooks Davis 	AUXARGS_ENTRY(pos, AT_NULL, 0);
235ea0fabbcSTim J. Robbins 
236ea0fabbcSTim J. Robbins 	free(imgp->auxargs, M_TEMP);
237ea0fabbcSTim J. Robbins 	imgp->auxargs = NULL;
2386362b1a6SJung-uk Kim 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
2395f77b8a8SBrooks Davis 
240d8010b11SJohn Baldwin 	error = copyout(argarray, (void *)base,
241d8010b11SJohn Baldwin 	    sizeof(*argarray) * LINUX_AT_COUNT);
2425f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
24303b0d68cSJohn Baldwin 	return (error);
2445caa67faSJohn Baldwin }
245ea0fabbcSTim J. Robbins 
2465caa67faSJohn Baldwin static int
24731174518SJohn Baldwin linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
2485caa67faSJohn Baldwin {
2495caa67faSJohn Baldwin 	Elf32_Addr *base;
2505caa67faSJohn Baldwin 
2515caa67faSJohn Baldwin 	base = (Elf32_Addr *)*stack_base;
252ea0fabbcSTim J. Robbins 	base--;
2535f77b8a8SBrooks Davis 	if (suword32(base, (uint32_t)imgp->args->argc) == -1)
2545f77b8a8SBrooks Davis 		return (EFAULT);
25531174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
256af682d48SDmitry Chagin 	return (0);
257ea0fabbcSTim J. Robbins }
258ea0fabbcSTim J. Robbins 
259ea0fabbcSTim J. Robbins static void
2609104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
261ea0fabbcSTim J. Robbins {
262ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
263ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
264ea0fabbcSTim J. Robbins 	struct sigacts *psp;
265ea0fabbcSTim J. Robbins 	struct trapframe *regs;
266ea0fabbcSTim J. Robbins 	struct l_rt_sigframe *fp, frame;
267ea0fabbcSTim J. Robbins 	int oonstack;
2689104847fSDavid Xu 	int sig;
2699104847fSDavid Xu 	int code;
270ea0fabbcSTim J. Robbins 
2719104847fSDavid Xu 	sig = ksi->ksi_signo;
2729104847fSDavid Xu 	code = ksi->ksi_code;
273ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
274ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
275ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
276ea0fabbcSTim J. Robbins 	regs = td->td_frame;
277ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
278ea0fabbcSTim J. Robbins 
2794ba25759SEd Maste 	/* Allocate space for the signal handler context. */
280ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
281ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
282aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
283ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
284ea0fabbcSTim J. Robbins 	} else
285ea0fabbcSTim J. Robbins 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
286ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
287ea0fabbcSTim J. Robbins 
2884ba25759SEd Maste 	/* Build the argument list for the signal handler. */
2894ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
290ea0fabbcSTim J. Robbins 
291ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
292ea0fabbcSTim J. Robbins 
293ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
294ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
295ea0fabbcSTim J. Robbins 	frame.sf_siginfo = PTROUT(&fp->sf_si);
296ea0fabbcSTim J. Robbins 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
297ea0fabbcSTim J. Robbins 
2984ba25759SEd Maste 	/* Fill in POSIX parts. */
299aa8b2011SKonstantin Belousov 	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
300ea0fabbcSTim J. Robbins 
301ea0fabbcSTim J. Robbins 	/*
3024ba25759SEd Maste 	 * Build the signal context to be used by sigreturn and libgcc unwind.
303ea0fabbcSTim J. Robbins 	 */
304ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
305ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
306ea0fabbcSTim J. Robbins 
307ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
308ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
309ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
310ea0fabbcSTim J. Robbins 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
311ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
312ea0fabbcSTim J. Robbins 
313ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
314ea0fabbcSTim J. Robbins 
3154ab7403bSDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
316ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
317ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
318ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
319ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
320bdc37934SDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
321ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
322ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
323ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
324ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
325ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
3262c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
3272c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
3282c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
3292c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
330ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
331ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
332ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
333ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
33496a2b635SKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
335ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
336ea0fabbcSTim J. Robbins 
337ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
338ea0fabbcSTim J. Robbins 		/*
339ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
340ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
341ea0fabbcSTim J. Robbins 		 */
342ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
343ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
344ea0fabbcSTim J. Robbins 	}
345ea0fabbcSTim J. Robbins 
3464ba25759SEd Maste 	/* Build context to run handler in. */
347ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
348bdc37934SDmitry Chagin 	regs->tf_rip = linux32_rt_sigcode;
34922eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
350ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
351ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
3522c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
3532c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
3542c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
3552c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
3562c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
357e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
358ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
359ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
360ea0fabbcSTim J. Robbins }
361ea0fabbcSTim J. Robbins 
362ea0fabbcSTim J. Robbins 
363ea0fabbcSTim J. Robbins /*
364ea0fabbcSTim J. Robbins  * Send an interrupt to process.
365ea0fabbcSTim J. Robbins  *
366ea0fabbcSTim J. Robbins  * Stack is set up to allow sigcode stored
367ea0fabbcSTim J. Robbins  * in u. to call routine, followed by kcall
368ea0fabbcSTim J. Robbins  * to sigreturn routine below.  After sigreturn
369ea0fabbcSTim J. Robbins  * resets the signal mask, the stack, and the
370ea0fabbcSTim J. Robbins  * frame pointer, it returns to the user
371ea0fabbcSTim J. Robbins  * specified pc, psl.
372ea0fabbcSTim J. Robbins  */
373ea0fabbcSTim J. Robbins static void
3749104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
375ea0fabbcSTim J. Robbins {
376ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
377ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
378ea0fabbcSTim J. Robbins 	struct sigacts *psp;
379ea0fabbcSTim J. Robbins 	struct trapframe *regs;
380ea0fabbcSTim J. Robbins 	struct l_sigframe *fp, frame;
381ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
3824ab7403bSDmitry Chagin 	int oonstack;
3839104847fSDavid Xu 	int sig, code;
384ea0fabbcSTim J. Robbins 
3859104847fSDavid Xu 	sig = ksi->ksi_signo;
3869104847fSDavid Xu 	code = ksi->ksi_code;
387ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
388ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
389ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
390ea0fabbcSTim J. Robbins 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
391ea0fabbcSTim J. Robbins 		/* Signal handler installed with SA_SIGINFO. */
3929104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
393ea0fabbcSTim J. Robbins 		return;
394ea0fabbcSTim J. Robbins 	}
395ea0fabbcSTim J. Robbins 
396ea0fabbcSTim J. Robbins 	regs = td->td_frame;
397ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
398ea0fabbcSTim J. Robbins 
3994ba25759SEd Maste 	/* Allocate space for the signal handler context. */
400ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
401ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
402aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
403ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
404ea0fabbcSTim J. Robbins 	} else
405ea0fabbcSTim J. Robbins 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
406ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
407ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
408ea0fabbcSTim J. Robbins 
4094ba25759SEd Maste 	/* Build the argument list for the signal handler. */
4104ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
411ea0fabbcSTim J. Robbins 
412ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
413ea0fabbcSTim J. Robbins 
414ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
415ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
416ea0fabbcSTim J. Robbins 
417ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &lmask);
418ea0fabbcSTim J. Robbins 
4194ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
4204ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
4212c66cccaSKonstantin Belousov 	frame.sf_sc.sc_gs     = regs->tf_gs;
4222c66cccaSKonstantin Belousov 	frame.sf_sc.sc_fs     = regs->tf_fs;
4232c66cccaSKonstantin Belousov 	frame.sf_sc.sc_es     = regs->tf_es;
4242c66cccaSKonstantin Belousov 	frame.sf_sc.sc_ds     = regs->tf_ds;
425ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edi    = regs->tf_rdi;
426ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esi    = regs->tf_rsi;
427ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
428ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
429bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_rsp;
430ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edx    = regs->tf_rdx;
431ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
432ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eax    = regs->tf_rax;
433ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eip    = regs->tf_rip;
434ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_cs     = regs->tf_cs;
435ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eflags = regs->tf_rflags;
436ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
437ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ss     = regs->tf_ss;
438ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_err    = regs->tf_err;
43996a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
440ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
441ea0fabbcSTim J. Robbins 
4424ab7403bSDmitry Chagin 	frame.sf_extramask[0] = lmask.__mask;
443ea0fabbcSTim J. Robbins 
444ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
445ea0fabbcSTim J. Robbins 		/*
446ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
447ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
448ea0fabbcSTim J. Robbins 		 */
449ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
450ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
451ea0fabbcSTim J. Robbins 	}
452ea0fabbcSTim J. Robbins 
4534ba25759SEd Maste 	/* Build context to run handler in. */
454ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
455bdc37934SDmitry Chagin 	regs->tf_rip = linux32_sigcode;
45622eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
457ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
458ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
4592c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
4602c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
4612c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
4622c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
4632c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
464e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
465ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
466ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
467ea0fabbcSTim J. Robbins }
468ea0fabbcSTim J. Robbins 
469ea0fabbcSTim J. Robbins /*
470ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
471ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
472ea0fabbcSTim J. Robbins  * stack state from context left by sendsig (above).
473ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
474ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
475ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
476ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
477ea0fabbcSTim J. Robbins  * a machine fault.
478ea0fabbcSTim J. Robbins  */
479ea0fabbcSTim J. Robbins int
480ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
481ea0fabbcSTim J. Robbins {
482ea0fabbcSTim J. Robbins 	struct l_sigframe frame;
483ea0fabbcSTim J. Robbins 	struct trapframe *regs;
484d6e029adSKonstantin Belousov 	sigset_t bmask;
485ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
4864ab7403bSDmitry Chagin 	int eflags;
4879104847fSDavid Xu 	ksiginfo_t ksi;
488ea0fabbcSTim J. Robbins 
489ea0fabbcSTim J. Robbins 	regs = td->td_frame;
490ea0fabbcSTim J. Robbins 
491ea0fabbcSTim J. Robbins 	/*
492ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the sigframe.
493ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
494ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
495ea0fabbcSTim J. Robbins 	 */
496ea0fabbcSTim J. Robbins 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
497ea0fabbcSTim J. Robbins 		return (EFAULT);
498ea0fabbcSTim J. Robbins 
4994ba25759SEd Maste 	/* Check for security violations. */
500ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
501ea0fabbcSTim J. Robbins 	eflags = frame.sf_sc.sc_eflags;
5023d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
503ea0fabbcSTim J. Robbins 		return(EINVAL);
504ea0fabbcSTim J. Robbins 
505ea0fabbcSTim J. Robbins 	/*
506ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
507ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
508ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
509ea0fabbcSTim J. Robbins 	 */
510ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
511ea0fabbcSTim J. Robbins 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
5129104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5139104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5149104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5159104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5169104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
5179104847fSDavid Xu 		trapsignal(td, &ksi);
518ea0fabbcSTim J. Robbins 		return(EINVAL);
519ea0fabbcSTim J. Robbins 	}
520ea0fabbcSTim J. Robbins 
5214ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_sc.sc_mask;
5224ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_extramask[0];
523d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&lmask, &bmask);
524d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
525ea0fabbcSTim J. Robbins 
5264ba25759SEd Maste 	/* Restore signal context. */
527ea0fabbcSTim J. Robbins 	regs->tf_rdi    = frame.sf_sc.sc_edi;
528ea0fabbcSTim J. Robbins 	regs->tf_rsi    = frame.sf_sc.sc_esi;
529ea0fabbcSTim J. Robbins 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
530ea0fabbcSTim J. Robbins 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
531ea0fabbcSTim J. Robbins 	regs->tf_rdx    = frame.sf_sc.sc_edx;
532ea0fabbcSTim J. Robbins 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
533ea0fabbcSTim J. Robbins 	regs->tf_rax    = frame.sf_sc.sc_eax;
534ea0fabbcSTim J. Robbins 	regs->tf_rip    = frame.sf_sc.sc_eip;
535ea0fabbcSTim J. Robbins 	regs->tf_cs     = frame.sf_sc.sc_cs;
5362c66cccaSKonstantin Belousov 	regs->tf_ds     = frame.sf_sc.sc_ds;
5372c66cccaSKonstantin Belousov 	regs->tf_es     = frame.sf_sc.sc_es;
5382c66cccaSKonstantin Belousov 	regs->tf_fs     = frame.sf_sc.sc_fs;
5392c66cccaSKonstantin Belousov 	regs->tf_gs     = frame.sf_sc.sc_gs;
540ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
541ea0fabbcSTim J. Robbins 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
542ea0fabbcSTim J. Robbins 	regs->tf_ss     = frame.sf_sc.sc_ss;
543e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
544ea0fabbcSTim J. Robbins 
545ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
546ea0fabbcSTim J. Robbins }
547ea0fabbcSTim J. Robbins 
548ea0fabbcSTim J. Robbins /*
549ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
550ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
551ea0fabbcSTim J. Robbins  * stack state from context left by rt_sendsig (above).
552ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
553ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
554ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
555ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
556ea0fabbcSTim J. Robbins  * a machine fault.
557ea0fabbcSTim J. Robbins  */
558ea0fabbcSTim J. Robbins int
559ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
560ea0fabbcSTim J. Robbins {
561ea0fabbcSTim J. Robbins 	struct l_ucontext uc;
562ea0fabbcSTim J. Robbins 	struct l_sigcontext *context;
563d6e029adSKonstantin Belousov 	sigset_t bmask;
564ea0fabbcSTim J. Robbins 	l_stack_t *lss;
565ea0fabbcSTim J. Robbins 	stack_t ss;
566ea0fabbcSTim J. Robbins 	struct trapframe *regs;
567ea0fabbcSTim J. Robbins 	int eflags;
5689104847fSDavid Xu 	ksiginfo_t ksi;
569ea0fabbcSTim J. Robbins 
570ea0fabbcSTim J. Robbins 	regs = td->td_frame;
571ea0fabbcSTim J. Robbins 
572ea0fabbcSTim J. Robbins 	/*
573ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the ucontext.
574ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
575ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
576ea0fabbcSTim J. Robbins 	 */
577ea0fabbcSTim J. Robbins 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
578ea0fabbcSTim J. Robbins 		return (EFAULT);
579ea0fabbcSTim J. Robbins 
580ea0fabbcSTim J. Robbins 	context = &uc.uc_mcontext;
581ea0fabbcSTim J. Robbins 
5824ba25759SEd Maste 	/* Check for security violations. */
583ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
584ea0fabbcSTim J. Robbins 	eflags = context->sc_eflags;
5853d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
586ea0fabbcSTim J. Robbins 		return(EINVAL);
587ea0fabbcSTim J. Robbins 
588ea0fabbcSTim J. Robbins 	/*
589ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
590ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
591ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
592ea0fabbcSTim J. Robbins 	 */
593ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
594ea0fabbcSTim J. Robbins 	if (!CS_SECURE(context->sc_cs)) {
5959104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5969104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5979104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5989104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5999104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
6009104847fSDavid Xu 		trapsignal(td, &ksi);
601ea0fabbcSTim J. Robbins 		return(EINVAL);
602ea0fabbcSTim J. Robbins 	}
603ea0fabbcSTim J. Robbins 
604d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
605d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
606ea0fabbcSTim J. Robbins 
607ea0fabbcSTim J. Robbins 	/*
608ea0fabbcSTim J. Robbins 	 * Restore signal context
609ea0fabbcSTim J. Robbins 	 */
6102c66cccaSKonstantin Belousov 	regs->tf_gs	= context->sc_gs;
6112c66cccaSKonstantin Belousov 	regs->tf_fs	= context->sc_fs;
6122c66cccaSKonstantin Belousov 	regs->tf_es	= context->sc_es;
6132c66cccaSKonstantin Belousov 	regs->tf_ds	= context->sc_ds;
614ea0fabbcSTim J. Robbins 	regs->tf_rdi    = context->sc_edi;
615ea0fabbcSTim J. Robbins 	regs->tf_rsi    = context->sc_esi;
616ea0fabbcSTim J. Robbins 	regs->tf_rbp    = context->sc_ebp;
617ea0fabbcSTim J. Robbins 	regs->tf_rbx    = context->sc_ebx;
618ea0fabbcSTim J. Robbins 	regs->tf_rdx    = context->sc_edx;
619ea0fabbcSTim J. Robbins 	regs->tf_rcx    = context->sc_ecx;
620ea0fabbcSTim J. Robbins 	regs->tf_rax    = context->sc_eax;
621ea0fabbcSTim J. Robbins 	regs->tf_rip    = context->sc_eip;
622ea0fabbcSTim J. Robbins 	regs->tf_cs     = context->sc_cs;
623ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
624ea0fabbcSTim J. Robbins 	regs->tf_rsp    = context->sc_esp_at_signal;
625ea0fabbcSTim J. Robbins 	regs->tf_ss     = context->sc_ss;
626e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
627ea0fabbcSTim J. Robbins 
628ea0fabbcSTim J. Robbins 	/*
629ea0fabbcSTim J. Robbins 	 * call sigaltstack & ignore results..
630ea0fabbcSTim J. Robbins 	 */
631ea0fabbcSTim J. Robbins 	lss = &uc.uc_stack;
632ea0fabbcSTim J. Robbins 	ss.ss_sp = PTRIN(lss->ss_sp);
633ea0fabbcSTim J. Robbins 	ss.ss_size = lss->ss_size;
634ea0fabbcSTim J. Robbins 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
635ea0fabbcSTim J. Robbins 
636ea0fabbcSTim J. Robbins 	(void)kern_sigaltstack(td, &ss, NULL);
637ea0fabbcSTim J. Robbins 
638ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
639ea0fabbcSTim J. Robbins }
640ea0fabbcSTim J. Robbins 
641afe1a688SKonstantin Belousov static int
6422d88da2fSKonstantin Belousov linux32_fetch_syscall_args(struct thread *td)
643ea0fabbcSTim J. Robbins {
644afe1a688SKonstantin Belousov 	struct proc *p;
645afe1a688SKonstantin Belousov 	struct trapframe *frame;
6462d88da2fSKonstantin Belousov 	struct syscall_args *sa;
647afe1a688SKonstantin Belousov 
648afe1a688SKonstantin Belousov 	p = td->td_proc;
649afe1a688SKonstantin Belousov 	frame = td->td_frame;
6502d88da2fSKonstantin Belousov 	sa = &td->td_sa;
651afe1a688SKonstantin Belousov 
652afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_rbx;
653afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_rcx;
654afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_rdx;
655afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_rsi;
656afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_rdi;
657afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
658afe1a688SKonstantin Belousov 	sa->code = frame->tf_rax;
659afe1a688SKonstantin Belousov 
660afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
661fcdffc03SDmitry Chagin 		/* nosys */
6625047105bSJohn Baldwin 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
663afe1a688SKonstantin Belousov 	else
664afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
665afe1a688SKonstantin Belousov 	sa->narg = sa->callp->sy_narg;
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 
673ea0fabbcSTim J. Robbins /*
674ea0fabbcSTim J. Robbins  * Clear registers on exec
675ea0fabbcSTim J. Robbins  * XXX copied from ia32_signal.c.
676ea0fabbcSTim J. Robbins  */
677ea0fabbcSTim J. Robbins static void
67831174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
67931174518SJohn Baldwin     uintptr_t stack)
680ea0fabbcSTim J. Robbins {
681ea0fabbcSTim J. Robbins 	struct trapframe *regs = td->td_frame;
682ea0fabbcSTim J. Robbins 	struct pcb *pcb = td->td_pcb;
6837c5d1690SKonstantin Belousov 	register_t saved_rflags;
6847c5d1690SKonstantin Belousov 
6857c5d1690SKonstantin Belousov 	regs = td->td_frame;
6867c5d1690SKonstantin Belousov 	pcb = td->td_pcb;
687ea0fabbcSTim J. Robbins 
6882c66cccaSKonstantin Belousov 	if (td->td_proc->p_md.md_ldt != NULL)
6892c66cccaSKonstantin Belousov 		user_ldt_free(td);
6902c66cccaSKonstantin Belousov 
6919c5b213eSJung-uk Kim 	critical_enter();
692ea0fabbcSTim J. Robbins 	wrmsr(MSR_FSBASE, 0);
693ea0fabbcSTim J. Robbins 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
694ea0fabbcSTim J. Robbins 	pcb->pcb_fsbase = 0;
695ea0fabbcSTim J. Robbins 	pcb->pcb_gsbase = 0;
6969c5b213eSJung-uk Kim 	critical_exit();
6972ee8325fSJohn Baldwin 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
698ea0fabbcSTim J. Robbins 
6997c5d1690SKonstantin Belousov 	saved_rflags = regs->tf_rflags & PSL_T;
700ea0fabbcSTim J. Robbins 	bzero((char *)regs, sizeof(struct trapframe));
701a107d8aaSNathan Whitehorn 	regs->tf_rip = imgp->entry_addr;
702ea0fabbcSTim J. Robbins 	regs->tf_rsp = stack;
7037c5d1690SKonstantin Belousov 	regs->tf_rflags = PSL_USER | saved_rflags;
7042c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
7052c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
7062c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
7072c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
708ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
7092c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
710ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
711a107d8aaSNathan Whitehorn 	regs->tf_rbx = imgp->ps_strings;
712bdbf2db5SJung-uk Kim 
7132a988f7cSStephan Uphoff 	fpstate_drop(td);
714ea0fabbcSTim J. Robbins 
7151b3c3256SKonstantin Belousov 	/* Do full restore on return so that we can change to a different %cs */
716e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
717ea0fabbcSTim J. Robbins }
718ea0fabbcSTim J. Robbins 
719ea0fabbcSTim J. Robbins /*
720ea0fabbcSTim J. Robbins  * XXX copied from ia32_sysvec.c.
721ea0fabbcSTim J. Robbins  */
72203b0d68cSJohn Baldwin static int
72331174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
724ea0fabbcSTim J. Robbins {
72503b0d68cSJohn Baldwin 	int argc, envc, error;
726ea0fabbcSTim J. Robbins 	u_int32_t *vectp;
72731174518SJohn Baldwin 	char *stringp;
72831174518SJohn Baldwin 	uintptr_t destp, ustringp;
729ea0fabbcSTim J. Robbins 	struct linux32_ps_strings *arginfo;
7304048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
7314048f59cSDmitry Chagin 	size_t execpath_len;
732ea0fabbcSTim J. Robbins 
7334ba25759SEd Maste 	/* Calculate string base and vector table pointers. */
7344048f59cSDmitry Chagin 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
7354048f59cSDmitry Chagin 		execpath_len = strlen(imgp->execpath) + 1;
7364048f59cSDmitry Chagin 	else
7374048f59cSDmitry Chagin 		execpath_len = 0;
7384048f59cSDmitry Chagin 
739ea0fabbcSTim J. Robbins 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
74031174518SJohn Baldwin 	destp = (uintptr_t)arginfo;
741ea0fabbcSTim J. Robbins 
7424048f59cSDmitry Chagin 	if (execpath_len != 0) {
74331174518SJohn Baldwin 		destp -= execpath_len;
74423a5b4edSJohn Baldwin 		destp = rounddown2(destp, sizeof(uint32_t));
74531174518SJohn Baldwin 		imgp->execpathp = destp;
74631174518SJohn Baldwin 		error = copyout(imgp->execpath, (void *)destp, execpath_len);
74703b0d68cSJohn Baldwin 		if (error != 0)
74803b0d68cSJohn Baldwin 			return (error);
7494048f59cSDmitry Chagin 	}
7504048f59cSDmitry Chagin 
7514ba25759SEd Maste 	/* Prepare the canary for SSP. */
7524048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
75323a5b4edSJohn Baldwin 	destp -= roundup(sizeof(canary), sizeof(uint32_t));
75431174518SJohn Baldwin 	imgp->canary = destp;
75531174518SJohn Baldwin 	error = copyout(canary, (void *)destp, sizeof(canary));
75603b0d68cSJohn Baldwin 	if (error != 0)
75703b0d68cSJohn Baldwin 		return (error);
7584048f59cSDmitry Chagin 
75931174518SJohn Baldwin 	/* Allocate room for the argument and environment strings. */
76031174518SJohn Baldwin 	destp -= ARG_MAX - imgp->args->stringspace;
76123a5b4edSJohn Baldwin 	destp = rounddown2(destp, sizeof(uint32_t));
76231174518SJohn Baldwin 	ustringp = destp;
76331174518SJohn Baldwin 
76403b0d68cSJohn Baldwin 	if (imgp->auxargs) {
765d8010b11SJohn Baldwin 		/*
766d8010b11SJohn Baldwin 		 * Allocate room on the stack for the ELF auxargs
767d8010b11SJohn Baldwin 		 * array.  It has LINUX_AT_COUNT entries.
768d8010b11SJohn Baldwin 		 */
769d8010b11SJohn Baldwin 		destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
77023a5b4edSJohn Baldwin 		destp = rounddown2(destp, sizeof(uint32_t));
77103b0d68cSJohn Baldwin 	}
772ea0fabbcSTim J. Robbins 
77331174518SJohn Baldwin 	vectp = (uint32_t *)destp;
77431174518SJohn Baldwin 
77573c8686eSJohn Baldwin 	/*
77673c8686eSJohn Baldwin 	 * Allocate room for the argv[] and env vectors including the
77773c8686eSJohn Baldwin 	 * terminating NULL pointers.
77873c8686eSJohn Baldwin 	 */
77973c8686eSJohn Baldwin 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
78073c8686eSJohn Baldwin 
7814ba25759SEd Maste 	/* vectp also becomes our initial stack base. */
78231174518SJohn Baldwin 	*stack_base = (uintptr_t)vectp;
783ea0fabbcSTim J. Robbins 
784610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
785610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
786610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
78731174518SJohn Baldwin 
7884ba25759SEd Maste 	/* Copy out strings - arguments and environment. */
78931174518SJohn Baldwin 	error = copyout(stringp, (void *)ustringp,
79031174518SJohn Baldwin 	    ARG_MAX - imgp->args->stringspace);
79103b0d68cSJohn Baldwin 	if (error != 0)
79203b0d68cSJohn Baldwin 		return (error);
793ea0fabbcSTim J. Robbins 
7944ba25759SEd Maste 	/* Fill in "ps_strings" struct for ps, w, etc. */
79503b0d68cSJohn Baldwin 	if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
79603b0d68cSJohn Baldwin 	    suword32(&arginfo->ps_nargvstr, argc) != 0)
79703b0d68cSJohn Baldwin 		return (EFAULT);
798ea0fabbcSTim J. Robbins 
7994ba25759SEd Maste 	/* Fill in argument portion of vector table. */
800ea0fabbcSTim J. Robbins 	for (; argc > 0; --argc) {
80131174518SJohn Baldwin 		if (suword32(vectp++, ustringp) != 0)
80203b0d68cSJohn Baldwin 			return (EFAULT);
803ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
80431174518SJohn Baldwin 			ustringp++;
80531174518SJohn Baldwin 		ustringp++;
806ea0fabbcSTim J. Robbins 	}
807ea0fabbcSTim J. Robbins 
8084ba25759SEd Maste 	/* A null vector table pointer separates the argp's from the envp's. */
80903b0d68cSJohn Baldwin 	if (suword32(vectp++, 0) != 0)
81003b0d68cSJohn Baldwin 		return (EFAULT);
811ea0fabbcSTim J. Robbins 
81203b0d68cSJohn Baldwin 	if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
81303b0d68cSJohn Baldwin 	    suword32(&arginfo->ps_nenvstr, envc) != 0)
81403b0d68cSJohn Baldwin 		return (EFAULT);
815ea0fabbcSTim J. Robbins 
8164ba25759SEd Maste 	/* Fill in environment portion of vector table. */
817ea0fabbcSTim J. Robbins 	for (; envc > 0; --envc) {
81831174518SJohn Baldwin 		if (suword32(vectp++, ustringp) != 0)
81903b0d68cSJohn Baldwin 			return (EFAULT);
820ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
82131174518SJohn Baldwin 			ustringp++;
82231174518SJohn Baldwin 		ustringp++;
823ea0fabbcSTim J. Robbins 	}
824ea0fabbcSTim J. Robbins 
8254ba25759SEd Maste 	/* The end of the vector table is a null pointer. */
82603b0d68cSJohn Baldwin 	if (suword32(vectp, 0) != 0)
82703b0d68cSJohn Baldwin 		return (EFAULT);
828ea0fabbcSTim J. Robbins 
829d8010b11SJohn Baldwin 	if (imgp->auxargs) {
830d8010b11SJohn Baldwin 		vectp++;
831d8010b11SJohn Baldwin 		error = imgp->sysent->sv_copyout_auxargs(imgp,
832d8010b11SJohn Baldwin 		    (uintptr_t)vectp);
833d8010b11SJohn Baldwin 		if (error != 0)
834d8010b11SJohn Baldwin 			return (error);
835d8010b11SJohn Baldwin 	}
836d8010b11SJohn Baldwin 
83703b0d68cSJohn Baldwin 	return (0);
838ea0fabbcSTim J. Robbins }
839ea0fabbcSTim J. Robbins 
840*7029da5cSPawel Biernacki static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
841ea0fabbcSTim J. Robbins     "32-bit Linux emulation");
842ea0fabbcSTim J. Robbins 
843ea0fabbcSTim J. Robbins static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
844ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
845ea0fabbcSTim J. Robbins     &linux32_maxdsiz, 0, "");
846ea0fabbcSTim J. Robbins static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
847ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
848ea0fabbcSTim J. Robbins     &linux32_maxssiz, 0, "");
849ea0fabbcSTim J. Robbins static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
850ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
851ea0fabbcSTim J. Robbins     &linux32_maxvmem, 0, "");
852ea0fabbcSTim J. Robbins 
853ea0fabbcSTim J. Robbins static void
85419059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which)
855ea0fabbcSTim J. Robbins {
856ea0fabbcSTim J. Robbins 
85719059a13SJohn Baldwin 	switch (which) {
85819059a13SJohn Baldwin 	case RLIMIT_DATA:
859ea0fabbcSTim J. Robbins 		if (linux32_maxdsiz != 0) {
86019059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxdsiz)
86119059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxdsiz;
86219059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxdsiz)
86319059a13SJohn Baldwin 				rl->rlim_max = linux32_maxdsiz;
864ea0fabbcSTim J. Robbins 		}
86519059a13SJohn Baldwin 		break;
86619059a13SJohn Baldwin 	case RLIMIT_STACK:
867ea0fabbcSTim J. Robbins 		if (linux32_maxssiz != 0) {
86819059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxssiz)
86919059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxssiz;
87019059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxssiz)
87119059a13SJohn Baldwin 				rl->rlim_max = linux32_maxssiz;
872ea0fabbcSTim J. Robbins 		}
87319059a13SJohn Baldwin 		break;
87419059a13SJohn Baldwin 	case RLIMIT_VMEM:
875ea0fabbcSTim J. Robbins 		if (linux32_maxvmem != 0) {
87619059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxvmem)
87719059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxvmem;
87819059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxvmem)
87919059a13SJohn Baldwin 				rl->rlim_max = linux32_maxvmem;
880ea0fabbcSTim J. Robbins 		}
88119059a13SJohn Baldwin 		break;
88219059a13SJohn Baldwin 	}
883ea0fabbcSTim J. Robbins }
884ea0fabbcSTim J. Robbins 
885ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = {
8862f99bcceSJohn Baldwin 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
8872f99bcceSJohn Baldwin 	.sv_table	= linux32_sysent,
888a8d403e1SKonstantin Belousov 	.sv_errsize	= ELAST + 1,
8891ac2776bSEd Maste 	.sv_errtbl	= linux_errtbl,
890dc858467SEd Maste 	.sv_transtrap	= linux_translate_traps,
891dc858467SEd Maste 	.sv_fixup	= linux_fixup_elf,
892a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
893bdc37934SDmitry Chagin 	.sv_sigcode	= &_binary_linux32_locore_o_start,
894a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
895a8d403e1SKonstantin Belousov 	.sv_name	= "Linux ELF32",
896a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
897dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
898a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
899a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
9008f1e49a6SDmitry Chagin 	.sv_maxuser	= LINUX32_MAXUSER,
901a8d403e1SKonstantin Belousov 	.sv_usrstack	= LINUX32_USRSTACK,
902a8d403e1SKonstantin Belousov 	.sv_psstrings	= LINUX32_PS_STRINGS,
903a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
9045caa67faSJohn Baldwin 	.sv_copyout_auxargs = linux_copyout_auxargs,
905a8d403e1SKonstantin Belousov 	.sv_copyout_strings = linux_copyout_strings,
906dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
907a8d403e1SKonstantin Belousov 	.sv_fixlimit	= linux32_fixlimit,
908a8d403e1SKonstantin Belousov 	.sv_maxssiz	= &linux32_maxssiz,
9098f1e49a6SDmitry Chagin 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
910afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = cpu_set_syscall_retval,
911afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
912afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
9138f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
9148f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
915e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
91681338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
917038c7205SDmitry Chagin 	.sv_trap	= NULL,
918ea0fabbcSTim J. Robbins };
919bdc37934SDmitry Chagin 
920bdc37934SDmitry Chagin static void
921bdc37934SDmitry Chagin linux_vdso_install(void *param)
922bdc37934SDmitry Chagin {
923bdc37934SDmitry Chagin 
924bdc37934SDmitry Chagin 	linux_szsigcode = (&_binary_linux32_locore_o_end -
925bdc37934SDmitry Chagin 	    &_binary_linux32_locore_o_start);
926bdc37934SDmitry Chagin 
927bdc37934SDmitry Chagin 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
928bdc37934SDmitry Chagin 		panic("Linux invalid vdso size\n");
929bdc37934SDmitry Chagin 
930bdc37934SDmitry Chagin 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
931bdc37934SDmitry Chagin 
932bdc37934SDmitry Chagin 	linux_shared_page_obj = __elfN(linux_shared_page_init)
933bdc37934SDmitry Chagin 	    (&linux_shared_page_mapping);
934bdc37934SDmitry Chagin 
935c151945cSDmitry Chagin 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec);
936bdc37934SDmitry Chagin 
937bdc37934SDmitry Chagin 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
938bdc37934SDmitry Chagin 	    linux_szsigcode);
939bdc37934SDmitry Chagin 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
9400020bdf1SDmitry Chagin 
9410020bdf1SDmitry Chagin 	linux_kplatform = linux_shared_page_mapping +
942c151945cSDmitry Chagin 	    (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
943bdc37934SDmitry Chagin }
944bdc37934SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
945f8268d4dSEd Maste     linux_vdso_install, NULL);
946bdc37934SDmitry Chagin 
947bdc37934SDmitry Chagin static void
948bdc37934SDmitry Chagin linux_vdso_deinstall(void *param)
949bdc37934SDmitry Chagin {
950bdc37934SDmitry Chagin 
951bdc37934SDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_shared_page_obj);
9527b194b3dSEd Maste }
953bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
954f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
955ea0fabbcSTim J. Robbins 
95689ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
95789ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
95889ffc202SBjoern A. Zeeb 
959a95659f7SEd Maste static bool
96089ffc202SBjoern A. Zeeb linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
96189ffc202SBjoern A. Zeeb {
96289ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
96389ffc202SBjoern A. Zeeb 	uintptr_t p;
96489ffc202SBjoern A. Zeeb 
96589ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
96689ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
96789ffc202SBjoern A. Zeeb 
96889ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
96989ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
970a95659f7SEd Maste 		return (false);
97189ffc202SBjoern A. Zeeb 
97289ffc202SBjoern A. Zeeb 	/*
97335755049SChuck Tuffli 	 * For Linux we encode osrel using the Linux convention of
97435755049SChuck Tuffli 	 * 	(version << 16) | (major << 8) | (minor)
97535755049SChuck Tuffli 	 * See macro in linux_mib.h
97689ffc202SBjoern A. Zeeb 	 */
97735755049SChuck Tuffli 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
97889ffc202SBjoern A. Zeeb 
979a95659f7SEd Maste 	return (true);
98089ffc202SBjoern A. Zeeb }
98132c01de2SDmitry Chagin 
98232c01de2SDmitry Chagin static Elf_Brandnote linux32_brandnote = {
98389ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
98489ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
98532c01de2SDmitry Chagin 	.hdr.n_type	= 1,
98689ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
98789ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
98889ffc202SBjoern A. Zeeb 	.trans_osrel	= linux32_trans_osrel
98932c01de2SDmitry Chagin };
99032c01de2SDmitry Chagin 
991ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = {
992a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
993a8d403e1SKonstantin Belousov 	.machine	= EM_386,
994a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
995b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
996a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
997a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
998a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
99932c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10002dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1001ea0fabbcSTim J. Robbins };
1002ea0fabbcSTim J. Robbins 
1003ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = {
1004a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1005a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1006a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1007b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1008a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1009a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1010a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
101132c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10122dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1013ea0fabbcSTim J. Robbins };
1014ea0fabbcSTim J. Robbins 
1015a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1016a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1017a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1018a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1019b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1020a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1021a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1022a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1023a0c59c7aSDmitry Chagin 	.brand_note	= &linux32_brandnote,
1024a0c59c7aSDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1025a0c59c7aSDmitry Chagin };
1026a0c59c7aSDmitry Chagin 
1027ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = {
1028ea0fabbcSTim J. Robbins 	&linux_brand,
1029ea0fabbcSTim J. Robbins 	&linux_glibc2brand,
1030a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1031ea0fabbcSTim J. Robbins 	NULL
1032ea0fabbcSTim J. Robbins };
1033ea0fabbcSTim J. Robbins 
1034ea0fabbcSTim J. Robbins static int
1035ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data)
1036ea0fabbcSTim J. Robbins {
1037ea0fabbcSTim J. Robbins 	Elf32_Brandinfo **brandinfo;
1038ea0fabbcSTim J. Robbins 	int error;
1039ea0fabbcSTim J. Robbins 	struct linux_ioctl_handler **lihp;
1040ea0fabbcSTim J. Robbins 
1041ea0fabbcSTim J. Robbins 	error = 0;
1042ea0fabbcSTim J. Robbins 
1043ea0fabbcSTim J. Robbins 	switch(type) {
1044ea0fabbcSTim J. Robbins 	case MOD_LOAD:
1045ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1046ea0fabbcSTim J. Robbins 		     ++brandinfo)
1047ea0fabbcSTim J. Robbins 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1048ea0fabbcSTim J. Robbins 				error = EINVAL;
1049ea0fabbcSTim J. Robbins 		if (error == 0) {
1050ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
10518fc08087STijl Coosemans 				linux32_ioctl_register_handler(*lihp);
10521ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
1053ea0fabbcSTim J. Robbins 			if (bootverbose)
1054ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler installed\n");
1055ea0fabbcSTim J. Robbins 		} else
1056ea0fabbcSTim J. Robbins 			printf("cannot insert Linux ELF brand handler\n");
1057ea0fabbcSTim J. Robbins 		break;
1058ea0fabbcSTim J. Robbins 	case MOD_UNLOAD:
1059ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1060ea0fabbcSTim J. Robbins 		     ++brandinfo)
1061ea0fabbcSTim J. Robbins 			if (elf32_brand_inuse(*brandinfo))
1062ea0fabbcSTim J. Robbins 				error = EBUSY;
1063ea0fabbcSTim J. Robbins 		if (error == 0) {
1064ea0fabbcSTim J. Robbins 			for (brandinfo = &linux_brandlist[0];
1065ea0fabbcSTim J. Robbins 			     *brandinfo != NULL; ++brandinfo)
1066ea0fabbcSTim J. Robbins 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1067ea0fabbcSTim J. Robbins 					error = EINVAL;
1068ea0fabbcSTim J. Robbins 		}
1069ea0fabbcSTim J. Robbins 		if (error == 0) {
1070ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
10718fc08087STijl Coosemans 				linux32_ioctl_unregister_handler(*lihp);
1072ea0fabbcSTim J. Robbins 			if (bootverbose)
1073ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler removed\n");
1074ea0fabbcSTim J. Robbins 		} else
1075ea0fabbcSTim J. Robbins 			printf("Could not deinstall ELF interpreter entry\n");
1076ea0fabbcSTim J. Robbins 		break;
1077ea0fabbcSTim J. Robbins 	default:
1078af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1079ea0fabbcSTim J. Robbins 	}
1080af682d48SDmitry Chagin 	return (error);
1081ea0fabbcSTim J. Robbins }
1082ea0fabbcSTim J. Robbins 
1083ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = {
1084ea0fabbcSTim J. Robbins 	"linuxelf",
1085ea0fabbcSTim J. Robbins 	linux_elf_modevent,
10869823d527SKevin Lo 	0
1087ea0fabbcSTim J. Robbins };
1088ea0fabbcSTim J. Robbins 
108978ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
109067d39748SDmitry Chagin MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1091b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
1092