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