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