xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision c1da89fec254f9d079fc17a186e2665a32cc718d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2003 Peter Wemm
6  * Copyright (c) 2002 Doug Rabson
7  * Copyright (c) 1998-1999 Andrew Gallatin
8  * Copyright (c) 1994-1996 Søren Schmidt
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer
16  *    in this position and unchanged.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "opt_compat.h"
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #ifndef COMPAT_FREEBSD32
41 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
42 #endif
43 
44 #define	__ELF_WORD_SIZE	32
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/exec.h>
49 #include <sys/fcntl.h>
50 #include <sys/imgact.h>
51 #include <sys/imgact_elf.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/proc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/sysproto.h>
64 #include <sys/vnode.h>
65 #include <sys/eventhandler.h>
66 
67 #include <vm/vm.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_param.h>
74 
75 #include <machine/cpu.h>
76 #include <machine/md_var.h>
77 #include <machine/pcb.h>
78 #include <machine/specialreg.h>
79 #include <machine/trap.h>
80 
81 #include <amd64/linux32/linux.h>
82 #include <amd64/linux32/linux32_proto.h>
83 #include <compat/linux/linux_emul.h>
84 #include <compat/linux/linux_ioctl.h>
85 #include <compat/linux/linux_mib.h>
86 #include <compat/linux/linux_misc.h>
87 #include <compat/linux/linux_signal.h>
88 #include <compat/linux/linux_util.h>
89 #include <compat/linux/linux_vdso.h>
90 
91 MODULE_VERSION(linux, 1);
92 
93 static int linux_szsigcode;
94 static vm_object_t linux_shared_page_obj;
95 static char *linux_shared_page_mapping;
96 extern char _binary_linux32_locore_o_start;
97 extern char _binary_linux32_locore_o_end;
98 
99 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
100 
101 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
102 
103 static int	linux_fixup_elf(uintptr_t *stack_base,
104 		    struct image_params *iparams);
105 static int	linux_copyout_strings(struct image_params *imgp,
106 		    uintptr_t *stack_base);
107 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
108 static void	linux_exec_setregs(struct thread *td,
109 				   struct image_params *imgp, uintptr_t stack);
110 static void	linux32_fixlimit(struct rlimit *rl, int which);
111 static bool	linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
112 static void	linux_vdso_install(void *param);
113 static void	linux_vdso_deinstall(void *param);
114 static void	linux32_set_syscall_retval(struct thread *td, int error);
115 
116 #define LINUX_T_UNKNOWN  255
117 static int _bsd_to_linux_trapcode[] = {
118 	LINUX_T_UNKNOWN,	/* 0 */
119 	6,			/* 1  T_PRIVINFLT */
120 	LINUX_T_UNKNOWN,	/* 2 */
121 	3,			/* 3  T_BPTFLT */
122 	LINUX_T_UNKNOWN,	/* 4 */
123 	LINUX_T_UNKNOWN,	/* 5 */
124 	16,			/* 6  T_ARITHTRAP */
125 	254,			/* 7  T_ASTFLT */
126 	LINUX_T_UNKNOWN,	/* 8 */
127 	13,			/* 9  T_PROTFLT */
128 	1,			/* 10 T_TRCTRAP */
129 	LINUX_T_UNKNOWN,	/* 11 */
130 	14,			/* 12 T_PAGEFLT */
131 	LINUX_T_UNKNOWN,	/* 13 */
132 	17,			/* 14 T_ALIGNFLT */
133 	LINUX_T_UNKNOWN,	/* 15 */
134 	LINUX_T_UNKNOWN,	/* 16 */
135 	LINUX_T_UNKNOWN,	/* 17 */
136 	0,			/* 18 T_DIVIDE */
137 	2,			/* 19 T_NMI */
138 	4,			/* 20 T_OFLOW */
139 	5,			/* 21 T_BOUND */
140 	7,			/* 22 T_DNA */
141 	8,			/* 23 T_DOUBLEFLT */
142 	9,			/* 24 T_FPOPFLT */
143 	10,			/* 25 T_TSSFLT */
144 	11,			/* 26 T_SEGNPFLT */
145 	12,			/* 27 T_STKFLT */
146 	18,			/* 28 T_MCHK */
147 	19,			/* 29 T_XMMFLT */
148 	15			/* 30 T_RESERVED */
149 };
150 #define bsd_to_linux_trapcode(code) \
151     ((code)<nitems(_bsd_to_linux_trapcode)? \
152      _bsd_to_linux_trapcode[(code)]: \
153      LINUX_T_UNKNOWN)
154 
155 struct linux32_ps_strings {
156 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
157 	u_int ps_nargvstr;	/* the number of argument strings */
158 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
159 	u_int ps_nenvstr;	/* the number of environment strings */
160 };
161 
162 LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
163 LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
164 LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
165 LINUX_VDSO_SYM_CHAR(linux_platform);
166 
167 /*
168  * If FreeBSD & Linux have a difference of opinion about what a trap
169  * means, deal with it here.
170  *
171  * MPSAFE
172  */
173 static int
174 linux_translate_traps(int signal, int trap_code)
175 {
176 	if (signal != SIGBUS)
177 		return (signal);
178 	switch (trap_code) {
179 	case T_PROTFLT:
180 	case T_TSSFLT:
181 	case T_DOUBLEFLT:
182 	case T_PAGEFLT:
183 		return (SIGSEGV);
184 	default:
185 		return (signal);
186 	}
187 }
188 
189 static int
190 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
191 {
192 	Elf32_Auxargs *args;
193 	Elf32_Auxinfo *argarray, *pos;
194 	int error, issetugid;
195 
196 	args = (Elf32_Auxargs *)imgp->auxargs;
197 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
198 	    M_WAITOK | M_ZERO);
199 
200 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
201 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
202 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
203 	    imgp->proc->p_sysent->sv_shared_page_base);
204 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
205 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
206 
207 	/*
208 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
209 	 * as it has appeared in the 2.4.0-rc7 first time.
210 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
211 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
212 	 * is not present.
213 	 * Also see linux_times() implementation.
214 	 */
215 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
216 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
217 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
218 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
219 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
220 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
221 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
222 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
223 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
224 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
225 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
226 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
227 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
228 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
229 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
230 	if (imgp->execpathp != 0)
231 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
232 	if (args->execfd != -1)
233 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
234 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
235 	AUXARGS_ENTRY(pos, AT_NULL, 0);
236 
237 	free(imgp->auxargs, M_TEMP);
238 	imgp->auxargs = NULL;
239 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
240 
241 	error = copyout(argarray, (void *)base,
242 	    sizeof(*argarray) * LINUX_AT_COUNT);
243 	free(argarray, M_TEMP);
244 	return (error);
245 }
246 
247 static int
248 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
249 {
250 	Elf32_Addr *base;
251 
252 	base = (Elf32_Addr *)*stack_base;
253 	base--;
254 	if (suword32(base, (uint32_t)imgp->args->argc) == -1)
255 		return (EFAULT);
256 	*stack_base = (uintptr_t)base;
257 	return (0);
258 }
259 
260 static void
261 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
262 {
263 	struct thread *td = curthread;
264 	struct proc *p = td->td_proc;
265 	struct sigacts *psp;
266 	struct trapframe *regs;
267 	struct l_rt_sigframe *fp, frame;
268 	int oonstack;
269 	int sig;
270 	int code;
271 
272 	sig = ksi->ksi_signo;
273 	code = ksi->ksi_code;
274 	PROC_LOCK_ASSERT(p, MA_OWNED);
275 	psp = p->p_sigacts;
276 	mtx_assert(&psp->ps_mtx, MA_OWNED);
277 	regs = td->td_frame;
278 	oonstack = sigonstack(regs->tf_rsp);
279 
280 	/* Allocate space for the signal handler context. */
281 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
282 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
283 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
284 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
285 	} else
286 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
287 	mtx_unlock(&psp->ps_mtx);
288 
289 	/* Build the argument list for the signal handler. */
290 	sig = bsd_to_linux_signal(sig);
291 
292 	bzero(&frame, sizeof(frame));
293 
294 	frame.sf_handler = PTROUT(catcher);
295 	frame.sf_sig = sig;
296 	frame.sf_siginfo = PTROUT(&fp->sf_si);
297 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
298 
299 	/* Fill in POSIX parts. */
300 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
301 
302 	/*
303 	 * Build the signal context to be used by sigreturn and libgcc unwind.
304 	 */
305 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
306 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
307 
308 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
309 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
310 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
311 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
312 	PROC_UNLOCK(p);
313 
314 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
315 
316 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
317 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
318 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
319 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
320 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
321 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
322 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
323 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
324 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
325 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
326 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
327 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
328 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
329 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
330 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
331 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
332 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
333 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
334 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
335 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
336 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
337 
338 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
339 		/*
340 		 * Process has trashed its stack; give it an illegal
341 		 * instruction to halt it in its tracks.
342 		 */
343 		PROC_LOCK(p);
344 		sigexit(td, SIGILL);
345 	}
346 
347 	/* Build context to run handler in. */
348 	regs->tf_rsp = PTROUT(fp);
349 	regs->tf_rip = linux32_rt_sigcode;
350 	regs->tf_rflags &= ~(PSL_T | PSL_D);
351 	regs->tf_cs = _ucode32sel;
352 	regs->tf_ss = _udatasel;
353 	regs->tf_ds = _udatasel;
354 	regs->tf_es = _udatasel;
355 	regs->tf_fs = _ufssel;
356 	regs->tf_gs = _ugssel;
357 	regs->tf_flags = TF_HASSEGS;
358 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
359 	PROC_LOCK(p);
360 	mtx_lock(&psp->ps_mtx);
361 }
362 
363 /*
364  * Send an interrupt to process.
365  *
366  * Stack is set up to allow sigcode stored
367  * in u. to call routine, followed by kcall
368  * to sigreturn routine below.  After sigreturn
369  * resets the signal mask, the stack, and the
370  * frame pointer, it returns to the user
371  * specified pc, psl.
372  */
373 static void
374 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
375 {
376 	struct thread *td = curthread;
377 	struct proc *p = td->td_proc;
378 	struct sigacts *psp;
379 	struct trapframe *regs;
380 	struct l_sigframe *fp, frame;
381 	l_sigset_t lmask;
382 	int oonstack;
383 	int sig, code;
384 
385 	sig = ksi->ksi_signo;
386 	code = ksi->ksi_code;
387 	PROC_LOCK_ASSERT(p, MA_OWNED);
388 	psp = p->p_sigacts;
389 	mtx_assert(&psp->ps_mtx, MA_OWNED);
390 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
391 		/* Signal handler installed with SA_SIGINFO. */
392 		linux_rt_sendsig(catcher, ksi, mask);
393 		return;
394 	}
395 
396 	regs = td->td_frame;
397 	oonstack = sigonstack(regs->tf_rsp);
398 
399 	/* Allocate space for the signal handler context. */
400 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
401 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
402 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
403 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
404 	} else
405 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
406 	mtx_unlock(&psp->ps_mtx);
407 	PROC_UNLOCK(p);
408 
409 	/* Build the argument list for the signal handler. */
410 	sig = bsd_to_linux_signal(sig);
411 
412 	bzero(&frame, sizeof(frame));
413 
414 	frame.sf_handler = PTROUT(catcher);
415 	frame.sf_sig = sig;
416 
417 	bsd_to_linux_sigset(mask, &lmask);
418 
419 	/* Build the signal context to be used by sigreturn. */
420 	frame.sf_sc.sc_mask   = lmask.__mask;
421 	frame.sf_sc.sc_gs     = regs->tf_gs;
422 	frame.sf_sc.sc_fs     = regs->tf_fs;
423 	frame.sf_sc.sc_es     = regs->tf_es;
424 	frame.sf_sc.sc_ds     = regs->tf_ds;
425 	frame.sf_sc.sc_edi    = regs->tf_rdi;
426 	frame.sf_sc.sc_esi    = regs->tf_rsi;
427 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
428 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
429 	frame.sf_sc.sc_esp    = regs->tf_rsp;
430 	frame.sf_sc.sc_edx    = regs->tf_rdx;
431 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
432 	frame.sf_sc.sc_eax    = regs->tf_rax;
433 	frame.sf_sc.sc_eip    = regs->tf_rip;
434 	frame.sf_sc.sc_cs     = regs->tf_cs;
435 	frame.sf_sc.sc_eflags = regs->tf_rflags;
436 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
437 	frame.sf_sc.sc_ss     = regs->tf_ss;
438 	frame.sf_sc.sc_err    = regs->tf_err;
439 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
440 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
441 
442 	frame.sf_extramask[0] = lmask.__mask;
443 
444 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
445 		/*
446 		 * Process has trashed its stack; give it an illegal
447 		 * instruction to halt it in its tracks.
448 		 */
449 		PROC_LOCK(p);
450 		sigexit(td, SIGILL);
451 	}
452 
453 	/* Build context to run handler in. */
454 	regs->tf_rsp = PTROUT(fp);
455 	regs->tf_rip = linux32_sigcode;
456 	regs->tf_rflags &= ~(PSL_T | PSL_D);
457 	regs->tf_cs = _ucode32sel;
458 	regs->tf_ss = _udatasel;
459 	regs->tf_ds = _udatasel;
460 	regs->tf_es = _udatasel;
461 	regs->tf_fs = _ufssel;
462 	regs->tf_gs = _ugssel;
463 	regs->tf_flags = TF_HASSEGS;
464 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
465 	PROC_LOCK(p);
466 	mtx_lock(&psp->ps_mtx);
467 }
468 
469 /*
470  * System call to cleanup state after a signal
471  * has been taken.  Reset signal mask and
472  * stack state from context left by sendsig (above).
473  * Return to previous pc and psl as specified by
474  * context left by sendsig. Check carefully to
475  * make sure that the user has not modified the
476  * psl to gain improper privileges or to cause
477  * a machine fault.
478  */
479 int
480 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
481 {
482 	struct l_sigframe frame;
483 	struct trapframe *regs;
484 	sigset_t bmask;
485 	l_sigset_t lmask;
486 	int eflags;
487 	ksiginfo_t ksi;
488 
489 	regs = td->td_frame;
490 
491 	/*
492 	 * The trampoline code hands us the sigframe.
493 	 * It is unsafe to keep track of it ourselves, in the event that a
494 	 * program jumps out of a signal handler.
495 	 */
496 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
497 		return (EFAULT);
498 
499 	/* Check for security violations. */
500 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
501 	eflags = frame.sf_sc.sc_eflags;
502 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
503 		return(EINVAL);
504 
505 	/*
506 	 * Don't allow users to load a valid privileged %cs.  Let the
507 	 * hardware check for invalid selectors, excess privilege in
508 	 * other selectors, invalid %eip's and invalid %esp's.
509 	 */
510 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
511 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
512 		ksiginfo_init_trap(&ksi);
513 		ksi.ksi_signo = SIGBUS;
514 		ksi.ksi_code = BUS_OBJERR;
515 		ksi.ksi_trapno = T_PROTFLT;
516 		ksi.ksi_addr = (void *)regs->tf_rip;
517 		trapsignal(td, &ksi);
518 		return(EINVAL);
519 	}
520 
521 	lmask.__mask = frame.sf_sc.sc_mask;
522 	lmask.__mask = frame.sf_extramask[0];
523 	linux_to_bsd_sigset(&lmask, &bmask);
524 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
525 
526 	/* Restore signal context. */
527 	regs->tf_rdi    = frame.sf_sc.sc_edi;
528 	regs->tf_rsi    = frame.sf_sc.sc_esi;
529 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
530 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
531 	regs->tf_rdx    = frame.sf_sc.sc_edx;
532 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
533 	regs->tf_rax    = frame.sf_sc.sc_eax;
534 	regs->tf_rip    = frame.sf_sc.sc_eip;
535 	regs->tf_cs     = frame.sf_sc.sc_cs;
536 	regs->tf_ds     = frame.sf_sc.sc_ds;
537 	regs->tf_es     = frame.sf_sc.sc_es;
538 	regs->tf_fs     = frame.sf_sc.sc_fs;
539 	regs->tf_gs     = frame.sf_sc.sc_gs;
540 	regs->tf_rflags = eflags;
541 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
542 	regs->tf_ss     = frame.sf_sc.sc_ss;
543 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
544 
545 	return (EJUSTRETURN);
546 }
547 
548 /*
549  * System call to cleanup state after a signal
550  * has been taken.  Reset signal mask and
551  * stack state from context left by rt_sendsig (above).
552  * Return to previous pc and psl as specified by
553  * context left by sendsig. Check carefully to
554  * make sure that the user has not modified the
555  * psl to gain improper privileges or to cause
556  * a machine fault.
557  */
558 int
559 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
560 {
561 	struct l_ucontext uc;
562 	struct l_sigcontext *context;
563 	sigset_t bmask;
564 	l_stack_t *lss;
565 	stack_t ss;
566 	struct trapframe *regs;
567 	int eflags;
568 	ksiginfo_t ksi;
569 
570 	regs = td->td_frame;
571 
572 	/*
573 	 * The trampoline code hands us the ucontext.
574 	 * It is unsafe to keep track of it ourselves, in the event that a
575 	 * program jumps out of a signal handler.
576 	 */
577 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
578 		return (EFAULT);
579 
580 	context = &uc.uc_mcontext;
581 
582 	/* Check for security violations. */
583 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
584 	eflags = context->sc_eflags;
585 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
586 		return(EINVAL);
587 
588 	/*
589 	 * Don't allow users to load a valid privileged %cs.  Let the
590 	 * hardware check for invalid selectors, excess privilege in
591 	 * other selectors, invalid %eip's and invalid %esp's.
592 	 */
593 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
594 	if (!CS_SECURE(context->sc_cs)) {
595 		ksiginfo_init_trap(&ksi);
596 		ksi.ksi_signo = SIGBUS;
597 		ksi.ksi_code = BUS_OBJERR;
598 		ksi.ksi_trapno = T_PROTFLT;
599 		ksi.ksi_addr = (void *)regs->tf_rip;
600 		trapsignal(td, &ksi);
601 		return(EINVAL);
602 	}
603 
604 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
605 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
606 
607 	/*
608 	 * Restore signal context
609 	 */
610 	regs->tf_gs	= context->sc_gs;
611 	regs->tf_fs	= context->sc_fs;
612 	regs->tf_es	= context->sc_es;
613 	regs->tf_ds	= context->sc_ds;
614 	regs->tf_rdi    = context->sc_edi;
615 	regs->tf_rsi    = context->sc_esi;
616 	regs->tf_rbp    = context->sc_ebp;
617 	regs->tf_rbx    = context->sc_ebx;
618 	regs->tf_rdx    = context->sc_edx;
619 	regs->tf_rcx    = context->sc_ecx;
620 	regs->tf_rax    = context->sc_eax;
621 	regs->tf_rip    = context->sc_eip;
622 	regs->tf_cs     = context->sc_cs;
623 	regs->tf_rflags = eflags;
624 	regs->tf_rsp    = context->sc_esp_at_signal;
625 	regs->tf_ss     = context->sc_ss;
626 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
627 
628 	/*
629 	 * call sigaltstack & ignore results..
630 	 */
631 	lss = &uc.uc_stack;
632 	ss.ss_sp = PTRIN(lss->ss_sp);
633 	ss.ss_size = lss->ss_size;
634 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
635 
636 	(void)kern_sigaltstack(td, &ss, NULL);
637 
638 	return (EJUSTRETURN);
639 }
640 
641 static int
642 linux32_fetch_syscall_args(struct thread *td)
643 {
644 	struct proc *p;
645 	struct trapframe *frame;
646 	struct syscall_args *sa;
647 
648 	p = td->td_proc;
649 	frame = td->td_frame;
650 	sa = &td->td_sa;
651 
652 	sa->args[0] = frame->tf_rbx;
653 	sa->args[1] = frame->tf_rcx;
654 	sa->args[2] = frame->tf_rdx;
655 	sa->args[3] = frame->tf_rsi;
656 	sa->args[4] = frame->tf_rdi;
657 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
658 	sa->code = frame->tf_rax;
659 
660 	if (sa->code >= p->p_sysent->sv_size)
661 		/* nosys */
662 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
663 	else
664 		sa->callp = &p->p_sysent->sv_table[sa->code];
665 
666 	td->td_retval[0] = 0;
667 	td->td_retval[1] = frame->tf_rdx;
668 
669 	return (0);
670 }
671 
672 static void
673 linux32_set_syscall_retval(struct thread *td, int error)
674 {
675 	struct trapframe *frame = td->td_frame;
676 
677 	cpu_set_syscall_retval(td, error);
678 
679 	if (__predict_false(error != 0)) {
680 		if (error != ERESTART && error != EJUSTRETURN)
681 			frame->tf_rax = bsd_to_linux_errno(error);
682 	}
683 }
684 
685 /*
686  * Clear registers on exec
687  * XXX copied from ia32_signal.c.
688  */
689 static void
690 linux_exec_setregs(struct thread *td, struct image_params *imgp,
691     uintptr_t stack)
692 {
693 	struct trapframe *regs = td->td_frame;
694 	struct pcb *pcb = td->td_pcb;
695 	register_t saved_rflags;
696 
697 	regs = td->td_frame;
698 	pcb = td->td_pcb;
699 
700 	if (td->td_proc->p_md.md_ldt != NULL)
701 		user_ldt_free(td);
702 
703 	critical_enter();
704 	wrmsr(MSR_FSBASE, 0);
705 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
706 	pcb->pcb_fsbase = 0;
707 	pcb->pcb_gsbase = 0;
708 	critical_exit();
709 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
710 
711 	saved_rflags = regs->tf_rflags & PSL_T;
712 	bzero((char *)regs, sizeof(struct trapframe));
713 	regs->tf_rip = imgp->entry_addr;
714 	regs->tf_rsp = stack;
715 	regs->tf_rflags = PSL_USER | saved_rflags;
716 	regs->tf_gs = _ugssel;
717 	regs->tf_fs = _ufssel;
718 	regs->tf_es = _udatasel;
719 	regs->tf_ds = _udatasel;
720 	regs->tf_ss = _udatasel;
721 	regs->tf_flags = TF_HASSEGS;
722 	regs->tf_cs = _ucode32sel;
723 	regs->tf_rbx = (register_t)imgp->ps_strings;
724 
725 	x86_clear_dbregs(pcb);
726 
727 	fpstate_drop(td);
728 
729 	/* Do full restore on return so that we can change to a different %cs */
730 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
731 }
732 
733 /*
734  * XXX copied from ia32_sysvec.c.
735  */
736 static int
737 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
738 {
739 	int argc, envc, error;
740 	u_int32_t *vectp;
741 	char *stringp;
742 	uintptr_t destp, ustringp;
743 	struct linux32_ps_strings *arginfo;
744 	char canary[LINUX_AT_RANDOM_LEN];
745 	size_t execpath_len;
746 
747 	/* Calculate string base and vector table pointers. */
748 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
749 		execpath_len = strlen(imgp->execpath) + 1;
750 	else
751 		execpath_len = 0;
752 
753 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
754 	destp = (uintptr_t)arginfo;
755 
756 	if (execpath_len != 0) {
757 		destp -= execpath_len;
758 		destp = rounddown2(destp, sizeof(uint32_t));
759 		imgp->execpathp = (void *)destp;
760 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
761 		if (error != 0)
762 			return (error);
763 	}
764 
765 	/* Prepare the canary for SSP. */
766 	arc4rand(canary, sizeof(canary), 0);
767 	destp -= roundup(sizeof(canary), sizeof(uint32_t));
768 	imgp->canary = (void *)destp;
769 	error = copyout(canary, imgp->canary, sizeof(canary));
770 	if (error != 0)
771 		return (error);
772 
773 	/* Allocate room for the argument and environment strings. */
774 	destp -= ARG_MAX - imgp->args->stringspace;
775 	destp = rounddown2(destp, sizeof(uint32_t));
776 	ustringp = destp;
777 
778 	if (imgp->auxargs) {
779 		/*
780 		 * Allocate room on the stack for the ELF auxargs
781 		 * array.  It has LINUX_AT_COUNT entries.
782 		 */
783 		destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
784 		destp = rounddown2(destp, sizeof(uint32_t));
785 	}
786 
787 	vectp = (uint32_t *)destp;
788 
789 	/*
790 	 * Allocate room for the argv[] and env vectors including the
791 	 * terminating NULL pointers.
792 	 */
793 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
794 
795 	/* vectp also becomes our initial stack base. */
796 	*stack_base = (uintptr_t)vectp;
797 
798 	stringp = imgp->args->begin_argv;
799 	argc = imgp->args->argc;
800 	envc = imgp->args->envc;
801 
802 	/* Copy out strings - arguments and environment. */
803 	error = copyout(stringp, (void *)ustringp,
804 	    ARG_MAX - imgp->args->stringspace);
805 	if (error != 0)
806 		return (error);
807 
808 	/* Fill in "ps_strings" struct for ps, w, etc. */
809 	if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
810 	    suword32(&arginfo->ps_nargvstr, argc) != 0)
811 		return (EFAULT);
812 
813 	/* Fill in argument portion of vector table. */
814 	for (; argc > 0; --argc) {
815 		if (suword32(vectp++, ustringp) != 0)
816 			return (EFAULT);
817 		while (*stringp++ != 0)
818 			ustringp++;
819 		ustringp++;
820 	}
821 
822 	/* A null vector table pointer separates the argp's from the envp's. */
823 	if (suword32(vectp++, 0) != 0)
824 		return (EFAULT);
825 
826 	if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
827 	    suword32(&arginfo->ps_nenvstr, envc) != 0)
828 		return (EFAULT);
829 
830 	/* Fill in environment portion of vector table. */
831 	for (; envc > 0; --envc) {
832 		if (suword32(vectp++, ustringp) != 0)
833 			return (EFAULT);
834 		while (*stringp++ != 0)
835 			ustringp++;
836 		ustringp++;
837 	}
838 
839 	/* The end of the vector table is a null pointer. */
840 	if (suword32(vectp, 0) != 0)
841 		return (EFAULT);
842 
843 	if (imgp->auxargs) {
844 		vectp++;
845 		error = imgp->sysent->sv_copyout_auxargs(imgp,
846 		    (uintptr_t)vectp);
847 		if (error != 0)
848 			return (error);
849 	}
850 
851 	return (0);
852 }
853 
854 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
855     "32-bit Linux emulation");
856 
857 static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
858 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
859     &linux32_maxdsiz, 0, "");
860 static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
861 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
862     &linux32_maxssiz, 0, "");
863 static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
864 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
865     &linux32_maxvmem, 0, "");
866 
867 static void
868 linux32_fixlimit(struct rlimit *rl, int which)
869 {
870 
871 	switch (which) {
872 	case RLIMIT_DATA:
873 		if (linux32_maxdsiz != 0) {
874 			if (rl->rlim_cur > linux32_maxdsiz)
875 				rl->rlim_cur = linux32_maxdsiz;
876 			if (rl->rlim_max > linux32_maxdsiz)
877 				rl->rlim_max = linux32_maxdsiz;
878 		}
879 		break;
880 	case RLIMIT_STACK:
881 		if (linux32_maxssiz != 0) {
882 			if (rl->rlim_cur > linux32_maxssiz)
883 				rl->rlim_cur = linux32_maxssiz;
884 			if (rl->rlim_max > linux32_maxssiz)
885 				rl->rlim_max = linux32_maxssiz;
886 		}
887 		break;
888 	case RLIMIT_VMEM:
889 		if (linux32_maxvmem != 0) {
890 			if (rl->rlim_cur > linux32_maxvmem)
891 				rl->rlim_cur = linux32_maxvmem;
892 			if (rl->rlim_max > linux32_maxvmem)
893 				rl->rlim_max = linux32_maxvmem;
894 		}
895 		break;
896 	}
897 }
898 
899 struct sysentvec elf_linux_sysvec = {
900 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
901 	.sv_table	= linux32_sysent,
902 	.sv_transtrap	= linux_translate_traps,
903 	.sv_fixup	= linux_fixup_elf,
904 	.sv_sendsig	= linux_sendsig,
905 	.sv_sigcode	= &_binary_linux32_locore_o_start,
906 	.sv_szsigcode	= &linux_szsigcode,
907 	.sv_name	= "Linux ELF32",
908 	.sv_coredump	= elf32_coredump,
909 	.sv_imgact_try	= linux_exec_imgact_try,
910 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
911 	.sv_minuser	= VM_MIN_ADDRESS,
912 	.sv_maxuser	= LINUX32_MAXUSER,
913 	.sv_usrstack	= LINUX32_USRSTACK,
914 	.sv_psstrings	= LINUX32_PS_STRINGS,
915 	.sv_stackprot	= VM_PROT_ALL,
916 	.sv_copyout_auxargs = linux_copyout_auxargs,
917 	.sv_copyout_strings = linux_copyout_strings,
918 	.sv_setregs	= linux_exec_setregs,
919 	.sv_fixlimit	= linux32_fixlimit,
920 	.sv_maxssiz	= &linux32_maxssiz,
921 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP |
922 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
923 	.sv_set_syscall_retval = linux32_set_syscall_retval,
924 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
925 	.sv_syscallnames = NULL,
926 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
927 	.sv_shared_page_len = PAGE_SIZE,
928 	.sv_schedtail	= linux_schedtail,
929 	.sv_thread_detach = linux_thread_detach,
930 	.sv_trap	= NULL,
931 	.sv_onexec	= linux_on_exec,
932 	.sv_onexit	= linux_on_exit,
933 	.sv_ontdexit	= linux_thread_dtor,
934 	.sv_setid_allowed = &linux_setid_allowed_query,
935 };
936 
937 static void
938 linux_vdso_install(void *param)
939 {
940 
941 	linux_szsigcode = (&_binary_linux32_locore_o_end -
942 	    &_binary_linux32_locore_o_start);
943 
944 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
945 		panic("Linux invalid vdso size\n");
946 
947 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
948 
949 	linux_shared_page_obj = __elfN(linux_shared_page_init)
950 	    (&linux_shared_page_mapping);
951 
952 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec);
953 
954 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
955 	    linux_szsigcode);
956 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
957 }
958 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
959     linux_vdso_install, NULL);
960 
961 static void
962 linux_vdso_deinstall(void *param)
963 {
964 
965 	__elfN(linux_shared_page_fini)(linux_shared_page_obj,
966 	    linux_shared_page_mapping);
967 }
968 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
969     linux_vdso_deinstall, NULL);
970 
971 static char GNU_ABI_VENDOR[] = "GNU";
972 static int GNULINUX_ABI_DESC = 0;
973 
974 static bool
975 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
976 {
977 	const Elf32_Word *desc;
978 	uintptr_t p;
979 
980 	p = (uintptr_t)(note + 1);
981 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
982 
983 	desc = (const Elf32_Word *)p;
984 	if (desc[0] != GNULINUX_ABI_DESC)
985 		return (false);
986 
987 	/*
988 	 * For Linux we encode osrel using the Linux convention of
989 	 * 	(version << 16) | (major << 8) | (minor)
990 	 * See macro in linux_mib.h
991 	 */
992 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
993 
994 	return (true);
995 }
996 
997 static Elf_Brandnote linux32_brandnote = {
998 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
999 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
1000 	.hdr.n_type	= 1,
1001 	.vendor		= GNU_ABI_VENDOR,
1002 	.flags		= BN_TRANSLATE_OSREL,
1003 	.trans_osrel	= linux32_trans_osrel
1004 };
1005 
1006 static Elf32_Brandinfo linux_brand = {
1007 	.brand		= ELFOSABI_LINUX,
1008 	.machine	= EM_386,
1009 	.compat_3_brand	= "Linux",
1010 	.emul_path	= linux_emul_path,
1011 	.interp_path	= "/lib/ld-linux.so.1",
1012 	.sysvec		= &elf_linux_sysvec,
1013 	.interp_newpath	= NULL,
1014 	.brand_note	= &linux32_brandnote,
1015 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1016 };
1017 
1018 static Elf32_Brandinfo linux_glibc2brand = {
1019 	.brand		= ELFOSABI_LINUX,
1020 	.machine	= EM_386,
1021 	.compat_3_brand	= "Linux",
1022 	.emul_path	= linux_emul_path,
1023 	.interp_path	= "/lib/ld-linux.so.2",
1024 	.sysvec		= &elf_linux_sysvec,
1025 	.interp_newpath	= NULL,
1026 	.brand_note	= &linux32_brandnote,
1027 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1028 };
1029 
1030 static Elf32_Brandinfo linux_muslbrand = {
1031 	.brand		= ELFOSABI_LINUX,
1032 	.machine	= EM_386,
1033 	.compat_3_brand	= "Linux",
1034 	.emul_path	= linux_emul_path,
1035 	.interp_path	= "/lib/ld-musl-i386.so.1",
1036 	.sysvec		= &elf_linux_sysvec,
1037 	.interp_newpath	= NULL,
1038 	.brand_note	= &linux32_brandnote,
1039 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1040 };
1041 
1042 Elf32_Brandinfo *linux_brandlist[] = {
1043 	&linux_brand,
1044 	&linux_glibc2brand,
1045 	&linux_muslbrand,
1046 	NULL
1047 };
1048 
1049 static int
1050 linux_elf_modevent(module_t mod, int type, void *data)
1051 {
1052 	Elf32_Brandinfo **brandinfo;
1053 	int error;
1054 	struct linux_ioctl_handler **lihp;
1055 
1056 	error = 0;
1057 
1058 	switch(type) {
1059 	case MOD_LOAD:
1060 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1061 		     ++brandinfo)
1062 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1063 				error = EINVAL;
1064 		if (error == 0) {
1065 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1066 				linux32_ioctl_register_handler(*lihp);
1067 			stclohz = (stathz ? stathz : hz);
1068 			if (bootverbose)
1069 				printf("Linux ELF exec handler installed\n");
1070 		} else
1071 			printf("cannot insert Linux ELF brand handler\n");
1072 		break;
1073 	case MOD_UNLOAD:
1074 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1075 		     ++brandinfo)
1076 			if (elf32_brand_inuse(*brandinfo))
1077 				error = EBUSY;
1078 		if (error == 0) {
1079 			for (brandinfo = &linux_brandlist[0];
1080 			     *brandinfo != NULL; ++brandinfo)
1081 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1082 					error = EINVAL;
1083 		}
1084 		if (error == 0) {
1085 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1086 				linux32_ioctl_unregister_handler(*lihp);
1087 			if (bootverbose)
1088 				printf("Linux ELF exec handler removed\n");
1089 		} else
1090 			printf("Could not deinstall ELF interpreter entry\n");
1091 		break;
1092 	default:
1093 		return (EOPNOTSUPP);
1094 	}
1095 	return (error);
1096 }
1097 
1098 static moduledata_t linux_elf_mod = {
1099 	"linuxelf",
1100 	linux_elf_modevent,
1101 	0
1102 };
1103 
1104 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1105 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1106 FEATURE(linux, "Linux 32bit support");
1107