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