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