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