xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision c49761dd572ca667babda4253e14498c7161e21a)
1ea0fabbcSTim J. Robbins /*-
2*c49761ddSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*c49761ddSPedro 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 
35ea0fabbcSTim J. Robbins #include <sys/cdefs.h>
36ea0fabbcSTim J. Robbins __FBSDID("$FreeBSD$");
37aefce619SRuslan Ermilov #include "opt_compat.h"
38ea0fabbcSTim J. Robbins 
39841c0c7eSNathan Whitehorn #ifndef COMPAT_FREEBSD32
40841c0c7eSNathan Whitehorn #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
41ea0fabbcSTim J. Robbins #endif
42ea0fabbcSTim J. Robbins 
43ea0fabbcSTim J. Robbins #define	__ELF_WORD_SIZE	32
44ea0fabbcSTim J. Robbins 
45ea0fabbcSTim J. Robbins #include <sys/param.h>
46ea0fabbcSTim J. Robbins #include <sys/systm.h>
47ea0fabbcSTim J. Robbins #include <sys/exec.h>
4848b05c3fSKonstantin Belousov #include <sys/fcntl.h>
49ea0fabbcSTim J. Robbins #include <sys/imgact.h>
50ea0fabbcSTim J. Robbins #include <sys/imgact_elf.h>
51ea0fabbcSTim J. Robbins #include <sys/kernel.h>
52ea0fabbcSTim J. Robbins #include <sys/lock.h>
53ea0fabbcSTim J. Robbins #include <sys/malloc.h>
54ea0fabbcSTim J. Robbins #include <sys/module.h>
55ea0fabbcSTim J. Robbins #include <sys/mutex.h>
56ea0fabbcSTim J. Robbins #include <sys/proc.h>
576004362eSDavid Schultz #include <sys/resourcevar.h>
58ea0fabbcSTim J. Robbins #include <sys/signalvar.h>
59ea0fabbcSTim J. Robbins #include <sys/sysctl.h>
60ea0fabbcSTim J. Robbins #include <sys/syscallsubr.h>
61ea0fabbcSTim J. Robbins #include <sys/sysent.h>
62ea0fabbcSTim J. Robbins #include <sys/sysproto.h>
63ea0fabbcSTim J. Robbins #include <sys/vnode.h>
647c09e6c0SAlexander Leidinger #include <sys/eventhandler.h>
65ea0fabbcSTim J. Robbins 
66ea0fabbcSTim J. Robbins #include <vm/vm.h>
67ea0fabbcSTim J. Robbins #include <vm/pmap.h>
68ea0fabbcSTim J. Robbins #include <vm/vm_extern.h>
69ea0fabbcSTim J. Robbins #include <vm/vm_map.h>
70ea0fabbcSTim J. Robbins #include <vm/vm_object.h>
71ea0fabbcSTim J. Robbins #include <vm/vm_page.h>
72ea0fabbcSTim J. Robbins #include <vm/vm_param.h>
73ea0fabbcSTim J. Robbins 
74ea0fabbcSTim J. Robbins #include <machine/cpu.h>
75ea0fabbcSTim J. Robbins #include <machine/md_var.h>
766004362eSDavid Schultz #include <machine/pcb.h>
77ea0fabbcSTim J. Robbins #include <machine/specialreg.h>
78ea0fabbcSTim J. Robbins 
79ea0fabbcSTim J. Robbins #include <amd64/linux32/linux.h>
80ea0fabbcSTim J. Robbins #include <amd64/linux32/linux32_proto.h>
817c09e6c0SAlexander Leidinger #include <compat/linux/linux_emul.h>
82fde63162SDmitry Chagin #include <compat/linux/linux_futex.h>
83d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
84ea0fabbcSTim J. Robbins #include <compat/linux/linux_mib.h>
854d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
86ea0fabbcSTim J. Robbins #include <compat/linux/linux_signal.h>
87ea0fabbcSTim J. Robbins #include <compat/linux/linux_util.h>
88bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
89ea0fabbcSTim J. Robbins 
90ea0fabbcSTim J. Robbins MODULE_VERSION(linux, 1);
91ea0fabbcSTim J. Robbins 
92ea0fabbcSTim J. Robbins #define	AUXARGS_ENTRY_32(pos, id, val)	\
93ea0fabbcSTim J. Robbins 	do {				\
94ea0fabbcSTim J. Robbins 		suword32(pos++, id);	\
95ea0fabbcSTim J. Robbins 		suword32(pos++, val);	\
96ea0fabbcSTim J. Robbins 	} while (0)
97ea0fabbcSTim J. Robbins 
98ea0fabbcSTim J. Robbins #if BYTE_ORDER == LITTLE_ENDIAN
99ea0fabbcSTim J. Robbins #define SHELLMAGIC      0x2123 /* #! */
100ea0fabbcSTim J. Robbins #else
101ea0fabbcSTim J. Robbins #define SHELLMAGIC      0x2321
102ea0fabbcSTim J. Robbins #endif
103ea0fabbcSTim J. Robbins 
104ea0fabbcSTim J. Robbins /*
105ea0fabbcSTim J. Robbins  * Allow the sendsig functions to use the ldebug() facility
106ea0fabbcSTim J. Robbins  * even though they are not syscalls themselves. Map them
107ea0fabbcSTim J. Robbins  * to syscall 0. This is slightly less bogus than using
108ea0fabbcSTim J. Robbins  * ldebug(sigreturn).
109ea0fabbcSTim J. Robbins  */
1106cea44a7SJohn Baldwin #define	LINUX32_SYS_linux_rt_sendsig	0
1116cea44a7SJohn Baldwin #define	LINUX32_SYS_linux_sendsig	0
112ea0fabbcSTim J. Robbins 
1130020bdf1SDmitry Chagin const char *linux_kplatform;
114bdc37934SDmitry Chagin static int linux_szsigcode;
115bdc37934SDmitry Chagin static vm_object_t linux_shared_page_obj;
116bdc37934SDmitry Chagin static char *linux_shared_page_mapping;
117bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_start;
118bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_end;
119ea0fabbcSTim J. Robbins 
1202f99bcceSJohn Baldwin extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
121ea0fabbcSTim J. Robbins 
122ea0fabbcSTim J. Robbins SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
123ea0fabbcSTim J. Robbins 
124ea0fabbcSTim J. Robbins static int	elf_linux_fixup(register_t **stack_base,
125ea0fabbcSTim J. Robbins 		    struct image_params *iparams);
126ea0fabbcSTim J. Robbins static register_t *linux_copyout_strings(struct image_params *imgp);
1279104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
128a107d8aaSNathan Whitehorn static void	exec_linux_setregs(struct thread *td,
129a107d8aaSNathan Whitehorn 				   struct image_params *imgp, u_long stack);
13019059a13SJohn Baldwin static void	linux32_fixlimit(struct rlimit *rl, int which);
13189ffc202SBjoern A. Zeeb static boolean_t linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
132bdc37934SDmitry Chagin static void	linux_vdso_install(void *param);
133bdc37934SDmitry Chagin static void	linux_vdso_deinstall(void *param);
134ea0fabbcSTim J. Robbins 
135ea0fabbcSTim J. Robbins /*
136ea0fabbcSTim J. Robbins  * Linux syscalls return negative errno's, we do positive and map them
13750e422f0SAlexander Leidinger  * Reference:
13850e422f0SAlexander Leidinger  *   FreeBSD: src/sys/sys/errno.h
13950e422f0SAlexander Leidinger  *   Linux:   linux-2.6.17.8/include/asm-generic/errno-base.h
14050e422f0SAlexander Leidinger  *            linux-2.6.17.8/include/asm-generic/errno.h
141ea0fabbcSTim J. Robbins  */
142ea0fabbcSTim J. Robbins static int bsd_to_linux_errno[ELAST + 1] = {
143ea0fabbcSTim J. Robbins 	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
144ea0fabbcSTim J. Robbins 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
145ea0fabbcSTim J. Robbins 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
146ea0fabbcSTim J. Robbins 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
147ea0fabbcSTim J. Robbins 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
148ea0fabbcSTim J. Robbins 	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
149ea0fabbcSTim J. Robbins 	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
150ea0fabbcSTim J. Robbins 	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
15150e422f0SAlexander Leidinger 	  -6,  -6, -43, -42, -75,-125, -84, -95, -16, -74,
15250e422f0SAlexander Leidinger 	 -72, -67, -71
153ea0fabbcSTim J. Robbins };
154ea0fabbcSTim J. Robbins 
155ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN  255
156ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = {
157ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 0 */
158ea0fabbcSTim J. Robbins 	6,			/* 1  T_PRIVINFLT */
159ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 2 */
160ea0fabbcSTim J. Robbins 	3,			/* 3  T_BPTFLT */
161ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 4 */
162ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 5 */
163ea0fabbcSTim J. Robbins 	16,			/* 6  T_ARITHTRAP */
164ea0fabbcSTim J. Robbins 	254,			/* 7  T_ASTFLT */
165ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 8 */
166ea0fabbcSTim J. Robbins 	13,			/* 9  T_PROTFLT */
167ea0fabbcSTim J. Robbins 	1,			/* 10 T_TRCTRAP */
168ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 11 */
169ea0fabbcSTim J. Robbins 	14,			/* 12 T_PAGEFLT */
170ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 13 */
171ea0fabbcSTim J. Robbins 	17,			/* 14 T_ALIGNFLT */
172ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 15 */
173ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 16 */
174ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 17 */
175ea0fabbcSTim J. Robbins 	0,			/* 18 T_DIVIDE */
176ea0fabbcSTim J. Robbins 	2,			/* 19 T_NMI */
177ea0fabbcSTim J. Robbins 	4,			/* 20 T_OFLOW */
178ea0fabbcSTim J. Robbins 	5,			/* 21 T_BOUND */
179ea0fabbcSTim J. Robbins 	7,			/* 22 T_DNA */
180ea0fabbcSTim J. Robbins 	8,			/* 23 T_DOUBLEFLT */
181ea0fabbcSTim J. Robbins 	9,			/* 24 T_FPOPFLT */
182ea0fabbcSTim J. Robbins 	10,			/* 25 T_TSSFLT */
183ea0fabbcSTim J. Robbins 	11,			/* 26 T_SEGNPFLT */
184ea0fabbcSTim J. Robbins 	12,			/* 27 T_STKFLT */
185ea0fabbcSTim J. Robbins 	18,			/* 28 T_MCHK */
186ea0fabbcSTim J. Robbins 	19,			/* 29 T_XMMFLT */
187ea0fabbcSTim J. Robbins 	15			/* 30 T_RESERVED */
188ea0fabbcSTim J. Robbins };
189ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \
190ea24b056SPedro F. Giffuni     ((code)<nitems(_bsd_to_linux_trapcode)? \
191ea0fabbcSTim J. Robbins      _bsd_to_linux_trapcode[(code)]: \
192ea0fabbcSTim J. Robbins      LINUX_T_UNKNOWN)
193ea0fabbcSTim J. Robbins 
194ea0fabbcSTim J. Robbins struct linux32_ps_strings {
195ea0fabbcSTim J. Robbins 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
196f2c7668eSDavid Schultz 	u_int ps_nargvstr;	/* the number of argument strings */
197ea0fabbcSTim J. Robbins 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
198f2c7668eSDavid Schultz 	u_int ps_nenvstr;	/* the number of environment strings */
199ea0fabbcSTim J. Robbins };
200ea0fabbcSTim J. Robbins 
201bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
202bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
203bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
2040020bdf1SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
205bdc37934SDmitry Chagin 
206ea0fabbcSTim J. Robbins /*
207ea0fabbcSTim J. Robbins  * If FreeBSD & Linux have a difference of opinion about what a trap
208ea0fabbcSTim J. Robbins  * means, deal with it here.
209ea0fabbcSTim J. Robbins  *
210ea0fabbcSTim J. Robbins  * MPSAFE
211ea0fabbcSTim J. Robbins  */
212ea0fabbcSTim J. Robbins static int
213ea0fabbcSTim J. Robbins translate_traps(int signal, int trap_code)
214ea0fabbcSTim J. Robbins {
215ea0fabbcSTim J. Robbins 	if (signal != SIGBUS)
216ea0fabbcSTim J. Robbins 		return signal;
217ea0fabbcSTim J. Robbins 	switch (trap_code) {
218ea0fabbcSTim J. Robbins 	case T_PROTFLT:
219ea0fabbcSTim J. Robbins 	case T_TSSFLT:
220ea0fabbcSTim J. Robbins 	case T_DOUBLEFLT:
221ea0fabbcSTim J. Robbins 	case T_PAGEFLT:
222ea0fabbcSTim J. Robbins 		return SIGSEGV;
223ea0fabbcSTim J. Robbins 	default:
224ea0fabbcSTim J. Robbins 		return signal;
225ea0fabbcSTim J. Robbins 	}
226ea0fabbcSTim J. Robbins }
227ea0fabbcSTim J. Robbins 
228ea0fabbcSTim J. Robbins static int
229ea0fabbcSTim J. Robbins elf_linux_fixup(register_t **stack_base, struct image_params *imgp)
230ea0fabbcSTim J. Robbins {
231ea0fabbcSTim J. Robbins 	Elf32_Auxargs *args;
232ea0fabbcSTim J. Robbins 	Elf32_Addr *base;
2330020bdf1SDmitry Chagin 	Elf32_Addr *pos;
2344d7c2e8aSDmitry Chagin 	struct linux32_ps_strings *arginfo;
235669414e4SXin LI 	int issetugid;
2364d7c2e8aSDmitry Chagin 
2374d7c2e8aSDmitry Chagin 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
238ea0fabbcSTim J. Robbins 
2396617724cSJeff Roberson 	KASSERT(curthread->td_proc == imgp->proc,
240ea0fabbcSTim J. Robbins 	    ("unsafe elf_linux_fixup(), should be curproc"));
241ea0fabbcSTim J. Robbins 	base = (Elf32_Addr *)*stack_base;
242ea0fabbcSTim J. Robbins 	args = (Elf32_Auxargs *)imgp->auxargs;
243610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
244ea0fabbcSTim J. Robbins 
245669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
246bdc37934SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO_EHDR,
247bdc37934SDmitry Chagin 	    imgp->proc->p_sysent->sv_shared_page_base);
248bdc37934SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
2494d7c2e8aSDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_HWCAP, cpu_feature);
2508d30f381SDmitry Chagin 
2518d30f381SDmitry Chagin 	/*
2528d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
2538d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
2548d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
2558d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
2568d30f381SDmitry Chagin 	 * is not present.
2578d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
2588d30f381SDmitry Chagin 	 */
2598d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
2601ca16454SDmitry Chagin 		AUXARGS_ENTRY_32(pos, LINUX_AT_CLKTCK, stclohz);
261ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr);
262ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent);
263ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum);
264ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz);
265ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags);
266ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry);
267ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_BASE, args->base);
268669414e4SXin LI 	AUXARGS_ENTRY_32(pos, LINUX_AT_SECURE, issetugid);
269ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
270ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
271ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
272ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
2730020bdf1SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
2744048f59cSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
2754048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
2764048f59cSDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
2774d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
2784d7c2e8aSDmitry Chagin 		AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd);
279ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_NULL, 0);
280ea0fabbcSTim J. Robbins 
281ea0fabbcSTim J. Robbins 	free(imgp->auxargs, M_TEMP);
282ea0fabbcSTim J. Robbins 	imgp->auxargs = NULL;
283ea0fabbcSTim J. Robbins 
284ea0fabbcSTim J. Robbins 	base--;
285610ecfe0SMaxim Sobolev 	suword32(base, (uint32_t)imgp->args->argc);
286ea0fabbcSTim J. Robbins 	*stack_base = (register_t *)base;
287af682d48SDmitry Chagin 	return (0);
288ea0fabbcSTim J. Robbins }
289ea0fabbcSTim J. Robbins 
290ea0fabbcSTim J. Robbins static void
2919104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
292ea0fabbcSTim J. Robbins {
293ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
294ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
295ea0fabbcSTim J. Robbins 	struct sigacts *psp;
296ea0fabbcSTim J. Robbins 	struct trapframe *regs;
297ea0fabbcSTim J. Robbins 	struct l_rt_sigframe *fp, frame;
298ea0fabbcSTim J. Robbins 	int oonstack;
2999104847fSDavid Xu 	int sig;
3009104847fSDavid Xu 	int code;
301ea0fabbcSTim J. Robbins 
3029104847fSDavid Xu 	sig = ksi->ksi_signo;
3039104847fSDavid Xu 	code = ksi->ksi_code;
304ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
305ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
306ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
307ea0fabbcSTim J. Robbins 	regs = td->td_frame;
308ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
309ea0fabbcSTim J. Robbins 
310ea0fabbcSTim J. Robbins #ifdef DEBUG
311ea0fabbcSTim J. Robbins 	if (ldebug(rt_sendsig))
312728ef954SJohn Baldwin 		printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
313ea0fabbcSTim J. Robbins 		    catcher, sig, (void*)mask, code);
314ea0fabbcSTim J. Robbins #endif
315ea0fabbcSTim J. Robbins 	/*
316ea0fabbcSTim J. Robbins 	 * Allocate space for the signal handler context.
317ea0fabbcSTim J. Robbins 	 */
318ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
319ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
320aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
321ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
322ea0fabbcSTim J. Robbins 	} else
323ea0fabbcSTim J. Robbins 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
324ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
325ea0fabbcSTim J. Robbins 
326ea0fabbcSTim J. Robbins 	/*
327ea0fabbcSTim J. Robbins 	 * Build the argument list for the signal handler.
328ea0fabbcSTim J. Robbins 	 */
3294ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
330ea0fabbcSTim J. Robbins 
331ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
332ea0fabbcSTim J. Robbins 
333ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
334ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
335ea0fabbcSTim J. Robbins 	frame.sf_siginfo = PTROUT(&fp->sf_si);
336ea0fabbcSTim J. Robbins 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
337ea0fabbcSTim J. Robbins 
338ea0fabbcSTim J. Robbins 	/* Fill in POSIX parts */
339aa8b2011SKonstantin Belousov 	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
340ea0fabbcSTim J. Robbins 
341ea0fabbcSTim J. Robbins 	/*
342bdc37934SDmitry Chagin 	 * Build the signal context to be used by sigreturn
343bdc37934SDmitry Chagin 	 * and libgcc unwind.
344ea0fabbcSTim J. Robbins 	 */
345ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
346ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
347ea0fabbcSTim J. Robbins 
348ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
349ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
350ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
351ea0fabbcSTim J. Robbins 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
352ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
353ea0fabbcSTim J. Robbins 
354ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
355ea0fabbcSTim J. Robbins 
3564ab7403bSDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
357ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
358ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
359ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
360ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
361bdc37934SDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
362ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
363ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
364ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
365ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
366ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
3672c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
3682c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
3692c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
3702c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
371ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
372ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
373ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
374ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
37596a2b635SKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
376ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
377ea0fabbcSTim J. Robbins 
378ea0fabbcSTim J. Robbins #ifdef DEBUG
379ea0fabbcSTim J. Robbins 	if (ldebug(rt_sendsig))
380c680f6b1SDavid E. O'Brien 		printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
381ea0fabbcSTim J. Robbins 		    frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
382ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
383ea0fabbcSTim J. Robbins #endif
384ea0fabbcSTim J. Robbins 
385ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
386ea0fabbcSTim J. Robbins 		/*
387ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
388ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
389ea0fabbcSTim J. Robbins 		 */
390ea0fabbcSTim J. Robbins #ifdef DEBUG
391ea0fabbcSTim J. Robbins 		if (ldebug(rt_sendsig))
392ea0fabbcSTim J. Robbins 			printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
393ea0fabbcSTim J. Robbins 			    fp, oonstack);
394ea0fabbcSTim J. Robbins #endif
395ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
396ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
397ea0fabbcSTim J. Robbins 	}
398ea0fabbcSTim J. Robbins 
399ea0fabbcSTim J. Robbins 	/*
400ea0fabbcSTim J. Robbins 	 * Build context to run handler in.
401ea0fabbcSTim J. Robbins 	 */
402ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
403bdc37934SDmitry Chagin 	regs->tf_rip = linux32_rt_sigcode;
40422eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
405ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
406ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
4072c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
4082c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
4092c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
4102c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
4112c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
412e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
413ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
414ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
415ea0fabbcSTim J. Robbins }
416ea0fabbcSTim J. Robbins 
417ea0fabbcSTim J. Robbins 
418ea0fabbcSTim J. Robbins /*
419ea0fabbcSTim J. Robbins  * Send an interrupt to process.
420ea0fabbcSTim J. Robbins  *
421ea0fabbcSTim J. Robbins  * Stack is set up to allow sigcode stored
422ea0fabbcSTim J. Robbins  * in u. to call routine, followed by kcall
423ea0fabbcSTim J. Robbins  * to sigreturn routine below.  After sigreturn
424ea0fabbcSTim J. Robbins  * resets the signal mask, the stack, and the
425ea0fabbcSTim J. Robbins  * frame pointer, it returns to the user
426ea0fabbcSTim J. Robbins  * specified pc, psl.
427ea0fabbcSTim J. Robbins  */
428ea0fabbcSTim J. Robbins static void
4299104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
430ea0fabbcSTim J. Robbins {
431ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
432ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
433ea0fabbcSTim J. Robbins 	struct sigacts *psp;
434ea0fabbcSTim J. Robbins 	struct trapframe *regs;
435ea0fabbcSTim J. Robbins 	struct l_sigframe *fp, frame;
436ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
4374ab7403bSDmitry Chagin 	int oonstack;
4389104847fSDavid Xu 	int sig, code;
439ea0fabbcSTim J. Robbins 
4409104847fSDavid Xu 	sig = ksi->ksi_signo;
4419104847fSDavid Xu 	code = ksi->ksi_code;
442ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
443ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
444ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
445ea0fabbcSTim J. Robbins 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
446ea0fabbcSTim J. Robbins 		/* Signal handler installed with SA_SIGINFO. */
4479104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
448ea0fabbcSTim J. Robbins 		return;
449ea0fabbcSTim J. Robbins 	}
450ea0fabbcSTim J. Robbins 
451ea0fabbcSTim J. Robbins 	regs = td->td_frame;
452ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
453ea0fabbcSTim J. Robbins 
454ea0fabbcSTim J. Robbins #ifdef DEBUG
455ea0fabbcSTim J. Robbins 	if (ldebug(sendsig))
456728ef954SJohn Baldwin 		printf(ARGS(sendsig, "%p, %d, %p, %u"),
457ea0fabbcSTim J. Robbins 		    catcher, sig, (void*)mask, code);
458ea0fabbcSTim J. Robbins #endif
459ea0fabbcSTim J. Robbins 
460ea0fabbcSTim J. Robbins 	/*
461ea0fabbcSTim J. Robbins 	 * Allocate space for the signal handler context.
462ea0fabbcSTim J. Robbins 	 */
463ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
464ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
465aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
466ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
467ea0fabbcSTim J. Robbins 	} else
468ea0fabbcSTim J. Robbins 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
469ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
470ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
471ea0fabbcSTim J. Robbins 
472ea0fabbcSTim J. Robbins 	/*
473ea0fabbcSTim J. Robbins 	 * Build the argument list for the signal handler.
474ea0fabbcSTim J. Robbins 	 */
4754ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
476ea0fabbcSTim J. Robbins 
477ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
478ea0fabbcSTim J. Robbins 
479ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
480ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
481ea0fabbcSTim J. Robbins 
482ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &lmask);
483ea0fabbcSTim J. Robbins 
484ea0fabbcSTim J. Robbins 	/*
485ea0fabbcSTim J. Robbins 	 * Build the signal context to be used by sigreturn.
486ea0fabbcSTim J. Robbins 	 */
4874ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
4882c66cccaSKonstantin Belousov 	frame.sf_sc.sc_gs     = regs->tf_gs;
4892c66cccaSKonstantin Belousov 	frame.sf_sc.sc_fs     = regs->tf_fs;
4902c66cccaSKonstantin Belousov 	frame.sf_sc.sc_es     = regs->tf_es;
4912c66cccaSKonstantin Belousov 	frame.sf_sc.sc_ds     = regs->tf_ds;
492ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edi    = regs->tf_rdi;
493ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esi    = regs->tf_rsi;
494ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
495ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
496bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_rsp;
497ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edx    = regs->tf_rdx;
498ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
499ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eax    = regs->tf_rax;
500ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eip    = regs->tf_rip;
501ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_cs     = regs->tf_cs;
502ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eflags = regs->tf_rflags;
503ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
504ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ss     = regs->tf_ss;
505ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_err    = regs->tf_err;
50696a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
507ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
508ea0fabbcSTim J. Robbins 
5094ab7403bSDmitry Chagin 	frame.sf_extramask[0] = lmask.__mask;
510ea0fabbcSTim J. Robbins 
511ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
512ea0fabbcSTim J. Robbins 		/*
513ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
514ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
515ea0fabbcSTim J. Robbins 		 */
516ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
517ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
518ea0fabbcSTim J. Robbins 	}
519ea0fabbcSTim J. Robbins 
520ea0fabbcSTim J. Robbins 	/*
521ea0fabbcSTim J. Robbins 	 * Build context to run handler in.
522ea0fabbcSTim J. Robbins 	 */
523ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
524bdc37934SDmitry Chagin 	regs->tf_rip = linux32_sigcode;
52522eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
526ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
527ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
5282c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
5292c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
5302c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
5312c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
5322c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
533e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
534ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
535ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
536ea0fabbcSTim J. Robbins }
537ea0fabbcSTim J. Robbins 
538ea0fabbcSTim J. Robbins /*
539ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
540ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
541ea0fabbcSTim J. Robbins  * stack state from context left by sendsig (above).
542ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
543ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
544ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
545ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
546ea0fabbcSTim J. Robbins  * a machine fault.
547ea0fabbcSTim J. Robbins  */
548ea0fabbcSTim J. Robbins int
549ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
550ea0fabbcSTim J. Robbins {
551ea0fabbcSTim J. Robbins 	struct l_sigframe frame;
552ea0fabbcSTim J. Robbins 	struct trapframe *regs;
553d6e029adSKonstantin Belousov 	sigset_t bmask;
554ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
5554ab7403bSDmitry Chagin 	int eflags;
5569104847fSDavid Xu 	ksiginfo_t ksi;
557ea0fabbcSTim J. Robbins 
558ea0fabbcSTim J. Robbins 	regs = td->td_frame;
559ea0fabbcSTim J. Robbins 
560ea0fabbcSTim J. Robbins #ifdef DEBUG
561ea0fabbcSTim J. Robbins 	if (ldebug(sigreturn))
562ea0fabbcSTim J. Robbins 		printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
563ea0fabbcSTim J. Robbins #endif
564ea0fabbcSTim J. Robbins 	/*
565ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the sigframe.
566ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
567ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
568ea0fabbcSTim J. Robbins 	 */
569ea0fabbcSTim J. Robbins 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
570ea0fabbcSTim J. Robbins 		return (EFAULT);
571ea0fabbcSTim J. Robbins 
572ea0fabbcSTim J. Robbins 	/*
573ea0fabbcSTim J. Robbins 	 * Check for security violations.
574ea0fabbcSTim J. Robbins 	 */
575ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
576ea0fabbcSTim J. Robbins 	eflags = frame.sf_sc.sc_eflags;
5773d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
578ea0fabbcSTim J. Robbins 		return(EINVAL);
579ea0fabbcSTim J. Robbins 
580ea0fabbcSTim J. Robbins 	/*
581ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
582ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
583ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
584ea0fabbcSTim J. Robbins 	 */
585ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
586ea0fabbcSTim J. Robbins 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
5879104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5889104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5899104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5909104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5919104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
5929104847fSDavid Xu 		trapsignal(td, &ksi);
593ea0fabbcSTim J. Robbins 		return(EINVAL);
594ea0fabbcSTim J. Robbins 	}
595ea0fabbcSTim J. Robbins 
5964ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_sc.sc_mask;
5974ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_extramask[0];
598d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&lmask, &bmask);
599d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
600ea0fabbcSTim J. Robbins 
601ea0fabbcSTim J. Robbins 	/*
602ea0fabbcSTim J. Robbins 	 * Restore signal context.
603ea0fabbcSTim J. Robbins 	 */
604ea0fabbcSTim J. Robbins 	regs->tf_rdi    = frame.sf_sc.sc_edi;
605ea0fabbcSTim J. Robbins 	regs->tf_rsi    = frame.sf_sc.sc_esi;
606ea0fabbcSTim J. Robbins 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
607ea0fabbcSTim J. Robbins 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
608ea0fabbcSTim J. Robbins 	regs->tf_rdx    = frame.sf_sc.sc_edx;
609ea0fabbcSTim J. Robbins 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
610ea0fabbcSTim J. Robbins 	regs->tf_rax    = frame.sf_sc.sc_eax;
611ea0fabbcSTim J. Robbins 	regs->tf_rip    = frame.sf_sc.sc_eip;
612ea0fabbcSTim J. Robbins 	regs->tf_cs     = frame.sf_sc.sc_cs;
6132c66cccaSKonstantin Belousov 	regs->tf_ds     = frame.sf_sc.sc_ds;
6142c66cccaSKonstantin Belousov 	regs->tf_es     = frame.sf_sc.sc_es;
6152c66cccaSKonstantin Belousov 	regs->tf_fs     = frame.sf_sc.sc_fs;
6162c66cccaSKonstantin Belousov 	regs->tf_gs     = frame.sf_sc.sc_gs;
617ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
618ea0fabbcSTim J. Robbins 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
619ea0fabbcSTim J. Robbins 	regs->tf_ss     = frame.sf_sc.sc_ss;
620e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
621ea0fabbcSTim J. Robbins 
622ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
623ea0fabbcSTim J. Robbins }
624ea0fabbcSTim J. Robbins 
625ea0fabbcSTim J. Robbins /*
626ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
627ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
628ea0fabbcSTim J. Robbins  * stack state from context left by rt_sendsig (above).
629ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
630ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
631ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
632ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
633ea0fabbcSTim J. Robbins  * a machine fault.
634ea0fabbcSTim J. Robbins  */
635ea0fabbcSTim J. Robbins int
636ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
637ea0fabbcSTim J. Robbins {
638ea0fabbcSTim J. Robbins 	struct l_ucontext uc;
639ea0fabbcSTim J. Robbins 	struct l_sigcontext *context;
640d6e029adSKonstantin Belousov 	sigset_t bmask;
641ea0fabbcSTim J. Robbins 	l_stack_t *lss;
642ea0fabbcSTim J. Robbins 	stack_t ss;
643ea0fabbcSTim J. Robbins 	struct trapframe *regs;
644ea0fabbcSTim J. Robbins 	int eflags;
6459104847fSDavid Xu 	ksiginfo_t ksi;
646ea0fabbcSTim J. Robbins 
647ea0fabbcSTim J. Robbins 	regs = td->td_frame;
648ea0fabbcSTim J. Robbins 
649ea0fabbcSTim J. Robbins #ifdef DEBUG
650ea0fabbcSTim J. Robbins 	if (ldebug(rt_sigreturn))
651ea0fabbcSTim J. Robbins 		printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
652ea0fabbcSTim J. Robbins #endif
653ea0fabbcSTim J. Robbins 	/*
654ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the ucontext.
655ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
656ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
657ea0fabbcSTim J. Robbins 	 */
658ea0fabbcSTim J. Robbins 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
659ea0fabbcSTim J. Robbins 		return (EFAULT);
660ea0fabbcSTim J. Robbins 
661ea0fabbcSTim J. Robbins 	context = &uc.uc_mcontext;
662ea0fabbcSTim J. Robbins 
663ea0fabbcSTim J. Robbins 	/*
664ea0fabbcSTim J. Robbins 	 * Check for security violations.
665ea0fabbcSTim J. Robbins 	 */
666ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
667ea0fabbcSTim J. Robbins 	eflags = context->sc_eflags;
6683d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
669ea0fabbcSTim J. Robbins 		return(EINVAL);
670ea0fabbcSTim J. Robbins 
671ea0fabbcSTim J. Robbins 	/*
672ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
673ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
674ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
675ea0fabbcSTim J. Robbins 	 */
676ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
677ea0fabbcSTim J. Robbins 	if (!CS_SECURE(context->sc_cs)) {
6789104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
6799104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
6809104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
6819104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
6829104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
6839104847fSDavid Xu 		trapsignal(td, &ksi);
684ea0fabbcSTim J. Robbins 		return(EINVAL);
685ea0fabbcSTim J. Robbins 	}
686ea0fabbcSTim J. Robbins 
687d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
688d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
689ea0fabbcSTim J. Robbins 
690ea0fabbcSTim J. Robbins 	/*
691ea0fabbcSTim J. Robbins 	 * Restore signal context
692ea0fabbcSTim J. Robbins 	 */
6932c66cccaSKonstantin Belousov 	regs->tf_gs	= context->sc_gs;
6942c66cccaSKonstantin Belousov 	regs->tf_fs	= context->sc_fs;
6952c66cccaSKonstantin Belousov 	regs->tf_es	= context->sc_es;
6962c66cccaSKonstantin Belousov 	regs->tf_ds	= context->sc_ds;
697ea0fabbcSTim J. Robbins 	regs->tf_rdi    = context->sc_edi;
698ea0fabbcSTim J. Robbins 	regs->tf_rsi    = context->sc_esi;
699ea0fabbcSTim J. Robbins 	regs->tf_rbp    = context->sc_ebp;
700ea0fabbcSTim J. Robbins 	regs->tf_rbx    = context->sc_ebx;
701ea0fabbcSTim J. Robbins 	regs->tf_rdx    = context->sc_edx;
702ea0fabbcSTim J. Robbins 	regs->tf_rcx    = context->sc_ecx;
703ea0fabbcSTim J. Robbins 	regs->tf_rax    = context->sc_eax;
704ea0fabbcSTim J. Robbins 	regs->tf_rip    = context->sc_eip;
705ea0fabbcSTim J. Robbins 	regs->tf_cs     = context->sc_cs;
706ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
707ea0fabbcSTim J. Robbins 	regs->tf_rsp    = context->sc_esp_at_signal;
708ea0fabbcSTim J. Robbins 	regs->tf_ss     = context->sc_ss;
709e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
710ea0fabbcSTim J. Robbins 
711ea0fabbcSTim J. Robbins 	/*
712ea0fabbcSTim J. Robbins 	 * call sigaltstack & ignore results..
713ea0fabbcSTim J. Robbins 	 */
714ea0fabbcSTim J. Robbins 	lss = &uc.uc_stack;
715ea0fabbcSTim J. Robbins 	ss.ss_sp = PTRIN(lss->ss_sp);
716ea0fabbcSTim J. Robbins 	ss.ss_size = lss->ss_size;
717ea0fabbcSTim J. Robbins 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
718ea0fabbcSTim J. Robbins 
719ea0fabbcSTim J. Robbins #ifdef DEBUG
720ea0fabbcSTim J. Robbins 	if (ldebug(rt_sigreturn))
721c680f6b1SDavid E. O'Brien 		printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
722ea0fabbcSTim J. Robbins 		    ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
723ea0fabbcSTim J. Robbins #endif
724ea0fabbcSTim J. Robbins 	(void)kern_sigaltstack(td, &ss, NULL);
725ea0fabbcSTim J. Robbins 
726ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
727ea0fabbcSTim J. Robbins }
728ea0fabbcSTim J. Robbins 
729afe1a688SKonstantin Belousov static int
7302d88da2fSKonstantin Belousov linux32_fetch_syscall_args(struct thread *td)
731ea0fabbcSTim J. Robbins {
732afe1a688SKonstantin Belousov 	struct proc *p;
733afe1a688SKonstantin Belousov 	struct trapframe *frame;
7342d88da2fSKonstantin Belousov 	struct syscall_args *sa;
735afe1a688SKonstantin Belousov 
736afe1a688SKonstantin Belousov 	p = td->td_proc;
737afe1a688SKonstantin Belousov 	frame = td->td_frame;
7382d88da2fSKonstantin Belousov 	sa = &td->td_sa;
739afe1a688SKonstantin Belousov 
740afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_rbx;
741afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_rcx;
742afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_rdx;
743afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_rsi;
744afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_rdi;
745afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
746afe1a688SKonstantin Belousov 	sa->code = frame->tf_rax;
747afe1a688SKonstantin Belousov 
748afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
749fcdffc03SDmitry Chagin 		/* nosys */
7505047105bSJohn Baldwin 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
751afe1a688SKonstantin Belousov 	else
752afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
753afe1a688SKonstantin Belousov 	sa->narg = sa->callp->sy_narg;
754afe1a688SKonstantin Belousov 
755afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
756afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_rdx;
757afe1a688SKonstantin Belousov 
758afe1a688SKonstantin Belousov 	return (0);
759ea0fabbcSTim J. Robbins }
760ea0fabbcSTim J. Robbins 
761ea0fabbcSTim J. Robbins /*
762ea0fabbcSTim J. Robbins  * If a linux binary is exec'ing something, try this image activator
763ea0fabbcSTim J. Robbins  * first.  We override standard shell script execution in order to
764ea0fabbcSTim J. Robbins  * be able to modify the interpreter path.  We only do this if a linux
765ea0fabbcSTim J. Robbins  * binary is doing the exec, so we do not create an EXEC module for it.
766ea0fabbcSTim J. Robbins  */
767ea0fabbcSTim J. Robbins static int	exec_linux_imgact_try(struct image_params *iparams);
768ea0fabbcSTim J. Robbins 
769ea0fabbcSTim J. Robbins static int
770ea0fabbcSTim J. Robbins exec_linux_imgact_try(struct image_params *imgp)
771ea0fabbcSTim J. Robbins {
772ea0fabbcSTim J. Robbins 	const char *head = (const char *)imgp->image_header;
7731d15fdd9SJohn Baldwin 	char *rpath;
774a14a9498SAlan Cox 	int error = -1;
775ea0fabbcSTim J. Robbins 
776ea0fabbcSTim J. Robbins 	/*
777ea0fabbcSTim J. Robbins 	* The interpreter for shell scripts run from a linux binary needs
778ea0fabbcSTim J. Robbins 	* to be located in /compat/linux if possible in order to recursively
779ea0fabbcSTim J. Robbins 	* maintain linux path emulation.
780ea0fabbcSTim J. Robbins 	*/
781ea0fabbcSTim J. Robbins 	if (((const short *)head)[0] == SHELLMAGIC) {
782ea0fabbcSTim J. Robbins 		/*
783ea0fabbcSTim J. Robbins 		* Run our normal shell image activator.  If it succeeds attempt
784d065e13dSDavid E. O'Brien 		* to use the alternate path for the interpreter.  If an
785d065e13dSDavid E. O'Brien 		* alternate * path is found, use our stringspace to store it.
786ea0fabbcSTim J. Robbins 		*/
787ea0fabbcSTim J. Robbins 		if ((error = exec_shell_imgact(imgp)) == 0) {
7881d15fdd9SJohn Baldwin 			linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
789d065e13dSDavid E. O'Brien 			    imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
790d065e13dSDavid E. O'Brien 			    AT_FDCWD);
791a14a9498SAlan Cox 			if (rpath != NULL)
792a14a9498SAlan Cox 				imgp->args->fname_buf =
793a14a9498SAlan Cox 				    imgp->interpreter_name = rpath;
794ea0fabbcSTim J. Robbins 		}
795ea0fabbcSTim J. Robbins 	}
796ea0fabbcSTim J. Robbins 	return (error);
797ea0fabbcSTim J. Robbins }
798ea0fabbcSTim J. Robbins 
799ea0fabbcSTim J. Robbins /*
800ea0fabbcSTim J. Robbins  * Clear registers on exec
801ea0fabbcSTim J. Robbins  * XXX copied from ia32_signal.c.
802ea0fabbcSTim J. Robbins  */
803ea0fabbcSTim J. Robbins static void
804a107d8aaSNathan Whitehorn exec_linux_setregs(struct thread *td, struct image_params *imgp, u_long stack)
805ea0fabbcSTim J. Robbins {
806ea0fabbcSTim J. Robbins 	struct trapframe *regs = td->td_frame;
807ea0fabbcSTim J. Robbins 	struct pcb *pcb = td->td_pcb;
808ea0fabbcSTim J. Robbins 
8092c66cccaSKonstantin Belousov 	if (td->td_proc->p_md.md_ldt != NULL)
8102c66cccaSKonstantin Belousov 		user_ldt_free(td);
8112c66cccaSKonstantin Belousov 
8129c5b213eSJung-uk Kim 	critical_enter();
813ea0fabbcSTim J. Robbins 	wrmsr(MSR_FSBASE, 0);
814ea0fabbcSTim J. Robbins 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
815ea0fabbcSTim J. Robbins 	pcb->pcb_fsbase = 0;
816ea0fabbcSTim J. Robbins 	pcb->pcb_gsbase = 0;
8179c5b213eSJung-uk Kim 	critical_exit();
8182ee8325fSJohn Baldwin 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
819ea0fabbcSTim J. Robbins 
820ea0fabbcSTim J. Robbins 	bzero((char *)regs, sizeof(struct trapframe));
821a107d8aaSNathan Whitehorn 	regs->tf_rip = imgp->entry_addr;
822ea0fabbcSTim J. Robbins 	regs->tf_rsp = stack;
823ea0fabbcSTim J. Robbins 	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
8242c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
8252c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
8262c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
8272c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
828ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
8292c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
830ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
831a107d8aaSNathan Whitehorn 	regs->tf_rbx = imgp->ps_strings;
832bdbf2db5SJung-uk Kim 
8332a988f7cSStephan Uphoff 	fpstate_drop(td);
834ea0fabbcSTim J. Robbins 
8351b3c3256SKonstantin Belousov 	/* Do full restore on return so that we can change to a different %cs */
836e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
837ea0fabbcSTim J. Robbins }
838ea0fabbcSTim J. Robbins 
839ea0fabbcSTim J. Robbins /*
840ea0fabbcSTim J. Robbins  * XXX copied from ia32_sysvec.c.
841ea0fabbcSTim J. Robbins  */
842ea0fabbcSTim J. Robbins static register_t *
843ea0fabbcSTim J. Robbins linux_copyout_strings(struct image_params *imgp)
844ea0fabbcSTim J. Robbins {
845ea0fabbcSTim J. Robbins 	int argc, envc;
846ea0fabbcSTim J. Robbins 	u_int32_t *vectp;
847ea0fabbcSTim J. Robbins 	char *stringp, *destp;
848ea0fabbcSTim J. Robbins 	u_int32_t *stack_base;
849ea0fabbcSTim J. Robbins 	struct linux32_ps_strings *arginfo;
8504048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
8514048f59cSDmitry Chagin 	size_t execpath_len;
852ea0fabbcSTim J. Robbins 
853ea0fabbcSTim J. Robbins 	/*
854ea0fabbcSTim J. Robbins 	 * Calculate string base and vector table pointers.
855ea0fabbcSTim J. Robbins 	 */
8564048f59cSDmitry Chagin 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
8574048f59cSDmitry Chagin 		execpath_len = strlen(imgp->execpath) + 1;
8584048f59cSDmitry Chagin 	else
8594048f59cSDmitry Chagin 		execpath_len = 0;
8604048f59cSDmitry Chagin 
861ea0fabbcSTim J. Robbins 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
8620020bdf1SDmitry Chagin 	destp =	(caddr_t)arginfo - SPARE_USRSPACE -
8634048f59cSDmitry Chagin 	    roundup(sizeof(canary), sizeof(char *)) -
8644048f59cSDmitry Chagin 	    roundup(execpath_len, sizeof(char *)) -
865b66bb393SPedro F. Giffuni 	    roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
866ea0fabbcSTim J. Robbins 
8674048f59cSDmitry Chagin 	if (execpath_len != 0) {
8684048f59cSDmitry Chagin 		imgp->execpathp = (uintptr_t)arginfo - execpath_len;
8694048f59cSDmitry Chagin 		copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
8704048f59cSDmitry Chagin 	}
8714048f59cSDmitry Chagin 
8724048f59cSDmitry Chagin 	/*
8734048f59cSDmitry Chagin 	 * Prepare the canary for SSP.
8744048f59cSDmitry Chagin 	 */
8754048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
8764048f59cSDmitry Chagin 	imgp->canary = (uintptr_t)arginfo -
8774048f59cSDmitry Chagin 	    roundup(execpath_len, sizeof(char *)) -
8784048f59cSDmitry Chagin 	    roundup(sizeof(canary), sizeof(char *));
8794048f59cSDmitry Chagin 	copyout(canary, (void *)imgp->canary, sizeof(canary));
8804048f59cSDmitry Chagin 
881ea0fabbcSTim J. Robbins 	/*
882ea0fabbcSTim J. Robbins 	 * If we have a valid auxargs ptr, prepare some room
883ea0fabbcSTim J. Robbins 	 * on the stack.
884ea0fabbcSTim J. Robbins 	 */
885ea0fabbcSTim J. Robbins 	if (imgp->auxargs) {
886ea0fabbcSTim J. Robbins 		/*
887ea0fabbcSTim J. Robbins 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
888ea0fabbcSTim J. Robbins 		 * lower compatibility.
889ea0fabbcSTim J. Robbins 		 */
890d065e13dSDavid E. O'Brien 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
8914d7c2e8aSDmitry Chagin 		    (LINUX_AT_COUNT * 2);
892ea0fabbcSTim J. Robbins 		/*
893ea0fabbcSTim J. Robbins 		 * The '+ 2' is for the null pointers at the end of each of
894ea0fabbcSTim J. Robbins 		 * the arg and env vector sets,and imgp->auxarg_size is room
895ea0fabbcSTim J. Robbins 		 * for argument of Runtime loader.
896ea0fabbcSTim J. Robbins 		 */
897d065e13dSDavid E. O'Brien 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
898d065e13dSDavid E. O'Brien 		    imgp->args->envc + 2 + imgp->auxarg_size) *
899d065e13dSDavid E. O'Brien 		    sizeof(u_int32_t));
900ea0fabbcSTim J. Robbins 
901ea0fabbcSTim J. Robbins 	} else
902ea0fabbcSTim J. Robbins 		/*
903ea0fabbcSTim J. Robbins 		 * The '+ 2' is for the null pointers at the end of each of
904ea0fabbcSTim J. Robbins 		 * the arg and env vector sets
905ea0fabbcSTim J. Robbins 		 */
906d065e13dSDavid E. O'Brien 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
907d065e13dSDavid E. O'Brien 		    imgp->args->envc + 2) * sizeof(u_int32_t));
908ea0fabbcSTim J. Robbins 
909ea0fabbcSTim J. Robbins 	/*
910ea0fabbcSTim J. Robbins 	 * vectp also becomes our initial stack base
911ea0fabbcSTim J. Robbins 	 */
912ea0fabbcSTim J. Robbins 	stack_base = vectp;
913ea0fabbcSTim J. Robbins 
914610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
915610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
916610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
917ea0fabbcSTim J. Robbins 	/*
918ea0fabbcSTim J. Robbins 	 * Copy out strings - arguments and environment.
919ea0fabbcSTim J. Robbins 	 */
920610ecfe0SMaxim Sobolev 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
921ea0fabbcSTim J. Robbins 
922ea0fabbcSTim J. Robbins 	/*
923ea0fabbcSTim J. Robbins 	 * Fill in "ps_strings" struct for ps, w, etc.
924ea0fabbcSTim J. Robbins 	 */
9254d7c2e8aSDmitry Chagin 	suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
926ea0fabbcSTim J. Robbins 	suword32(&arginfo->ps_nargvstr, argc);
927ea0fabbcSTim J. Robbins 
928ea0fabbcSTim J. Robbins 	/*
929ea0fabbcSTim J. Robbins 	 * Fill in argument portion of vector table.
930ea0fabbcSTim J. Robbins 	 */
931ea0fabbcSTim J. Robbins 	for (; argc > 0; --argc) {
9324d7c2e8aSDmitry Chagin 		suword32(vectp++, (uint32_t)(intptr_t)destp);
933ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
934ea0fabbcSTim J. Robbins 			destp++;
935ea0fabbcSTim J. Robbins 		destp++;
936ea0fabbcSTim J. Robbins 	}
937ea0fabbcSTim J. Robbins 
938ea0fabbcSTim J. Robbins 	/* a null vector table pointer separates the argp's from the envp's */
939ea0fabbcSTim J. Robbins 	suword32(vectp++, 0);
940ea0fabbcSTim J. Robbins 
9414d7c2e8aSDmitry Chagin 	suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
942ea0fabbcSTim J. Robbins 	suword32(&arginfo->ps_nenvstr, envc);
943ea0fabbcSTim J. Robbins 
944ea0fabbcSTim J. Robbins 	/*
945ea0fabbcSTim J. Robbins 	 * Fill in environment portion of vector table.
946ea0fabbcSTim J. Robbins 	 */
947ea0fabbcSTim J. Robbins 	for (; envc > 0; --envc) {
9484d7c2e8aSDmitry Chagin 		suword32(vectp++, (uint32_t)(intptr_t)destp);
949ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
950ea0fabbcSTim J. Robbins 			destp++;
951ea0fabbcSTim J. Robbins 		destp++;
952ea0fabbcSTim J. Robbins 	}
953ea0fabbcSTim J. Robbins 
954ea0fabbcSTim J. Robbins 	/* end of vector table is a null pointer */
955ea0fabbcSTim J. Robbins 	suword32(vectp, 0);
956ea0fabbcSTim J. Robbins 
957ea0fabbcSTim J. Robbins 	return ((register_t *)stack_base);
958ea0fabbcSTim J. Robbins }
959ea0fabbcSTim J. Robbins 
9606472ac3dSEd Schouten static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
961ea0fabbcSTim J. Robbins     "32-bit Linux emulation");
962ea0fabbcSTim J. Robbins 
963ea0fabbcSTim J. Robbins static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
964ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
965ea0fabbcSTim J. Robbins     &linux32_maxdsiz, 0, "");
966ea0fabbcSTim J. Robbins static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
967ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
968ea0fabbcSTim J. Robbins     &linux32_maxssiz, 0, "");
969ea0fabbcSTim J. Robbins static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
970ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
971ea0fabbcSTim J. Robbins     &linux32_maxvmem, 0, "");
972ea0fabbcSTim J. Robbins 
97367d39748SDmitry Chagin #if defined(DEBUG)
97467d39748SDmitry Chagin SYSCTL_PROC(_compat_linux32, OID_AUTO, debug,
97567d39748SDmitry Chagin             CTLTYPE_STRING | CTLFLAG_RW,
97667d39748SDmitry Chagin             0, 0, linux_sysctl_debug, "A",
97767d39748SDmitry Chagin             "Linux debugging control");
97867d39748SDmitry Chagin #endif
97967d39748SDmitry Chagin 
980ea0fabbcSTim J. Robbins static void
98119059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which)
982ea0fabbcSTim J. Robbins {
983ea0fabbcSTim J. Robbins 
98419059a13SJohn Baldwin 	switch (which) {
98519059a13SJohn Baldwin 	case RLIMIT_DATA:
986ea0fabbcSTim J. Robbins 		if (linux32_maxdsiz != 0) {
98719059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxdsiz)
98819059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxdsiz;
98919059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxdsiz)
99019059a13SJohn Baldwin 				rl->rlim_max = linux32_maxdsiz;
991ea0fabbcSTim J. Robbins 		}
99219059a13SJohn Baldwin 		break;
99319059a13SJohn Baldwin 	case RLIMIT_STACK:
994ea0fabbcSTim J. Robbins 		if (linux32_maxssiz != 0) {
99519059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxssiz)
99619059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxssiz;
99719059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxssiz)
99819059a13SJohn Baldwin 				rl->rlim_max = linux32_maxssiz;
999ea0fabbcSTim J. Robbins 		}
100019059a13SJohn Baldwin 		break;
100119059a13SJohn Baldwin 	case RLIMIT_VMEM:
1002ea0fabbcSTim J. Robbins 		if (linux32_maxvmem != 0) {
100319059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxvmem)
100419059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxvmem;
100519059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxvmem)
100619059a13SJohn Baldwin 				rl->rlim_max = linux32_maxvmem;
1007ea0fabbcSTim J. Robbins 		}
100819059a13SJohn Baldwin 		break;
100919059a13SJohn Baldwin 	}
1010ea0fabbcSTim J. Robbins }
1011ea0fabbcSTim J. Robbins 
1012ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = {
10132f99bcceSJohn Baldwin 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
10142f99bcceSJohn Baldwin 	.sv_table	= linux32_sysent,
1015a8d403e1SKonstantin Belousov 	.sv_mask	= 0,
1016a8d403e1SKonstantin Belousov 	.sv_errsize	= ELAST + 1,
1017a8d403e1SKonstantin Belousov 	.sv_errtbl	= bsd_to_linux_errno,
1018a8d403e1SKonstantin Belousov 	.sv_transtrap	= translate_traps,
1019a8d403e1SKonstantin Belousov 	.sv_fixup	= elf_linux_fixup,
1020a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
1021bdc37934SDmitry Chagin 	.sv_sigcode	= &_binary_linux32_locore_o_start,
1022a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
1023a8d403e1SKonstantin Belousov 	.sv_name	= "Linux ELF32",
1024a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
1025a8d403e1SKonstantin Belousov 	.sv_imgact_try	= exec_linux_imgact_try,
1026a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
1027a8d403e1SKonstantin Belousov 	.sv_pagesize	= PAGE_SIZE,
1028a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
10298f1e49a6SDmitry Chagin 	.sv_maxuser	= LINUX32_MAXUSER,
1030a8d403e1SKonstantin Belousov 	.sv_usrstack	= LINUX32_USRSTACK,
1031a8d403e1SKonstantin Belousov 	.sv_psstrings	= LINUX32_PS_STRINGS,
1032a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
1033a8d403e1SKonstantin Belousov 	.sv_copyout_strings = linux_copyout_strings,
1034a8d403e1SKonstantin Belousov 	.sv_setregs	= exec_linux_setregs,
1035a8d403e1SKonstantin Belousov 	.sv_fixlimit	= linux32_fixlimit,
1036a8d403e1SKonstantin Belousov 	.sv_maxssiz	= &linux32_maxssiz,
10378f1e49a6SDmitry Chagin 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
1038afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = cpu_set_syscall_retval,
1039afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
1040afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
10418f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
10428f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
1043e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
104481338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
1045038c7205SDmitry Chagin 	.sv_trap	= NULL,
1046ea0fabbcSTim J. Robbins };
1047bdc37934SDmitry Chagin 
1048bdc37934SDmitry Chagin static void
1049bdc37934SDmitry Chagin linux_vdso_install(void *param)
1050bdc37934SDmitry Chagin {
1051bdc37934SDmitry Chagin 
1052bdc37934SDmitry Chagin 	linux_szsigcode = (&_binary_linux32_locore_o_end -
1053bdc37934SDmitry Chagin 	    &_binary_linux32_locore_o_start);
1054bdc37934SDmitry Chagin 
1055bdc37934SDmitry Chagin 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
1056bdc37934SDmitry Chagin 		panic("Linux invalid vdso size\n");
1057bdc37934SDmitry Chagin 
1058bdc37934SDmitry Chagin 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
1059bdc37934SDmitry Chagin 
1060bdc37934SDmitry Chagin 	linux_shared_page_obj = __elfN(linux_shared_page_init)
1061bdc37934SDmitry Chagin 	    (&linux_shared_page_mapping);
1062bdc37934SDmitry Chagin 
1063c151945cSDmitry Chagin 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec);
1064bdc37934SDmitry Chagin 
1065bdc37934SDmitry Chagin 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
1066bdc37934SDmitry Chagin 	    linux_szsigcode);
1067bdc37934SDmitry Chagin 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
10680020bdf1SDmitry Chagin 
10690020bdf1SDmitry Chagin 	linux_kplatform = linux_shared_page_mapping +
1070c151945cSDmitry Chagin 	    (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
1071bdc37934SDmitry Chagin }
1072bdc37934SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
1073bdc37934SDmitry Chagin     (sysinit_cfunc_t)linux_vdso_install, NULL);
1074bdc37934SDmitry Chagin 
1075bdc37934SDmitry Chagin static void
1076bdc37934SDmitry Chagin linux_vdso_deinstall(void *param)
1077bdc37934SDmitry Chagin {
1078bdc37934SDmitry Chagin 
1079bdc37934SDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_shared_page_obj);
1080bdc37934SDmitry Chagin };
1081bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
1082bdc37934SDmitry Chagin     (sysinit_cfunc_t)linux_vdso_deinstall, NULL);
1083ea0fabbcSTim J. Robbins 
108489ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
108589ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
108689ffc202SBjoern A. Zeeb 
108789ffc202SBjoern A. Zeeb static boolean_t
108889ffc202SBjoern A. Zeeb linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
108989ffc202SBjoern A. Zeeb {
109089ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
109189ffc202SBjoern A. Zeeb 	uintptr_t p;
109289ffc202SBjoern A. Zeeb 
109389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
109489ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
109589ffc202SBjoern A. Zeeb 
109689ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
109789ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
109889ffc202SBjoern A. Zeeb 		return (FALSE);
109989ffc202SBjoern A. Zeeb 
110089ffc202SBjoern A. Zeeb 	/*
110189ffc202SBjoern A. Zeeb 	 * For linux we encode osrel as follows (see linux_mib.c):
110289ffc202SBjoern A. Zeeb 	 * VVVMMMIII (version, major, minor), see linux_mib.c.
110389ffc202SBjoern A. Zeeb 	 */
110489ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
110589ffc202SBjoern A. Zeeb 
110689ffc202SBjoern A. Zeeb 	return (TRUE);
110789ffc202SBjoern A. Zeeb }
110832c01de2SDmitry Chagin 
110932c01de2SDmitry Chagin static Elf_Brandnote linux32_brandnote = {
111089ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
111189ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
111232c01de2SDmitry Chagin 	.hdr.n_type	= 1,
111389ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
111489ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
111589ffc202SBjoern A. Zeeb 	.trans_osrel	= linux32_trans_osrel
111632c01de2SDmitry Chagin };
111732c01de2SDmitry Chagin 
1118ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = {
1119a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1120a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1121a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1122a8d403e1SKonstantin Belousov 	.emul_path	= "/compat/linux",
1123a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
1124a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1125a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
112632c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
11272dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1128ea0fabbcSTim J. Robbins };
1129ea0fabbcSTim J. Robbins 
1130ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = {
1131a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1132a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1133a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1134a8d403e1SKonstantin Belousov 	.emul_path	= "/compat/linux",
1135a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1136a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1137a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
113832c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
11392dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1140ea0fabbcSTim J. Robbins };
1141ea0fabbcSTim J. Robbins 
1142a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1143a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1144a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1145a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1146a0c59c7aSDmitry Chagin 	.emul_path	= "/compat/linux",
1147a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1148a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1149a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1150a0c59c7aSDmitry Chagin 	.brand_note	= &linux32_brandnote,
1151a0c59c7aSDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1152a0c59c7aSDmitry Chagin };
1153a0c59c7aSDmitry Chagin 
1154ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = {
1155ea0fabbcSTim J. Robbins 	&linux_brand,
1156ea0fabbcSTim J. Robbins 	&linux_glibc2brand,
1157a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1158ea0fabbcSTim J. Robbins 	NULL
1159ea0fabbcSTim J. Robbins };
1160ea0fabbcSTim J. Robbins 
1161ea0fabbcSTim J. Robbins static int
1162ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data)
1163ea0fabbcSTim J. Robbins {
1164ea0fabbcSTim J. Robbins 	Elf32_Brandinfo **brandinfo;
1165ea0fabbcSTim J. Robbins 	int error;
1166ea0fabbcSTim J. Robbins 	struct linux_ioctl_handler **lihp;
1167ea0fabbcSTim J. Robbins 
1168ea0fabbcSTim J. Robbins 	error = 0;
1169ea0fabbcSTim J. Robbins 
1170ea0fabbcSTim J. Robbins 	switch(type) {
1171ea0fabbcSTim J. Robbins 	case MOD_LOAD:
1172ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1173ea0fabbcSTim J. Robbins 		     ++brandinfo)
1174ea0fabbcSTim J. Robbins 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1175ea0fabbcSTim J. Robbins 				error = EINVAL;
1176ea0fabbcSTim J. Robbins 		if (error == 0) {
1177ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1178ea0fabbcSTim J. Robbins 				linux_ioctl_register_handler(*lihp);
11797c09e6c0SAlexander Leidinger 			LIST_INIT(&futex_list);
118079262bf1SDmitry Chagin 			mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
11811ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
1182ea0fabbcSTim J. Robbins 			if (bootverbose)
1183ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler installed\n");
1184ea0fabbcSTim J. Robbins 		} else
1185ea0fabbcSTim J. Robbins 			printf("cannot insert Linux ELF brand handler\n");
1186ea0fabbcSTim J. Robbins 		break;
1187ea0fabbcSTim J. Robbins 	case MOD_UNLOAD:
1188ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1189ea0fabbcSTim J. Robbins 		     ++brandinfo)
1190ea0fabbcSTim J. Robbins 			if (elf32_brand_inuse(*brandinfo))
1191ea0fabbcSTim J. Robbins 				error = EBUSY;
1192ea0fabbcSTim J. Robbins 		if (error == 0) {
1193ea0fabbcSTim J. Robbins 			for (brandinfo = &linux_brandlist[0];
1194ea0fabbcSTim J. Robbins 			     *brandinfo != NULL; ++brandinfo)
1195ea0fabbcSTim J. Robbins 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1196ea0fabbcSTim J. Robbins 					error = EINVAL;
1197ea0fabbcSTim J. Robbins 		}
1198ea0fabbcSTim J. Robbins 		if (error == 0) {
1199ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1200ea0fabbcSTim J. Robbins 				linux_ioctl_unregister_handler(*lihp);
120179262bf1SDmitry Chagin 			mtx_destroy(&futex_mtx);
1202ea0fabbcSTim J. Robbins 			if (bootverbose)
1203ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler removed\n");
1204ea0fabbcSTim J. Robbins 		} else
1205ea0fabbcSTim J. Robbins 			printf("Could not deinstall ELF interpreter entry\n");
1206ea0fabbcSTim J. Robbins 		break;
1207ea0fabbcSTim J. Robbins 	default:
1208af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1209ea0fabbcSTim J. Robbins 	}
1210af682d48SDmitry Chagin 	return (error);
1211ea0fabbcSTim J. Robbins }
1212ea0fabbcSTim J. Robbins 
1213ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = {
1214ea0fabbcSTim J. Robbins 	"linuxelf",
1215ea0fabbcSTim J. Robbins 	linux_elf_modevent,
12169823d527SKevin Lo 	0
1217ea0fabbcSTim J. Robbins };
1218ea0fabbcSTim J. Robbins 
121978ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
122067d39748SDmitry Chagin MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1221b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
1222