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