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