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