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