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