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