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