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