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