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