xref: /freebsd/sys/i386/linux/linux_sysvec.c (revision 10ff414c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1994-1996 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/proc.h>
43 #include <sys/stddef.h>
44 #include <sys/signalvar.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/vnode.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_param.h>
58 
59 #include <machine/cpu.h>
60 #include <machine/cputypes.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/trap.h>
64 
65 #include <x86/linux/linux_x86.h>
66 #include <i386/linux/linux.h>
67 #include <i386/linux/linux_proto.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_fork.h>
70 #include <compat/linux/linux_ioctl.h>
71 #include <compat/linux/linux_mib.h>
72 #include <compat/linux/linux_misc.h>
73 #include <compat/linux/linux_signal.h>
74 #include <compat/linux/linux_util.h>
75 #include <compat/linux/linux_vdso.h>
76 
77 MODULE_VERSION(linux, 1);
78 
79 #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
80 #define	LINUX_VDSOPAGE		(VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
81 #define	LINUX_SHAREDPAGE	(LINUX_VDSOPAGE - PAGE_SIZE)
82 				/*
83 				 * PAGE_SIZE - the size
84 				 * of the native SHAREDPAGE
85 				 */
86 #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
87 #define	LINUX_PS_STRINGS	(LINUX_USRSTACK - sizeof(struct ps_strings))
88 
89 static int linux_szsigcode;
90 static vm_object_t linux_vdso_obj;
91 static char *linux_vdso_mapping;
92 extern char _binary_linux_vdso_so_o_start;
93 extern char _binary_linux_vdso_so_o_end;
94 static vm_offset_t linux_vdso_base;
95 
96 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
97 
98 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
99 
100 static int	linux_fixup(uintptr_t *stack_base,
101 		    struct image_params *iparams);
102 static int	linux_fixup_elf(uintptr_t *stack_base,
103 		    struct image_params *iparams);
104 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
105 static void	linux_exec_setregs(struct thread *td,
106 		    struct image_params *imgp, uintptr_t stack);
107 static void	linux_exec_sysvec_init(void *param);
108 static int	linux_on_exec_vmspace(struct proc *p,
109 		    struct image_params *imgp);
110 static int	linux_copyout_strings(struct image_params *imgp,
111 		    uintptr_t *stack_base);
112 static void	linux_set_fork_retval(struct thread *td);
113 static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
114 static void	linux_vdso_install(const void *param);
115 static void	linux_vdso_deinstall(const void *param);
116 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
117 
118 #define LINUX_T_UNKNOWN  255
119 static int _bsd_to_linux_trapcode[] = {
120 	LINUX_T_UNKNOWN,	/* 0 */
121 	6,			/* 1  T_PRIVINFLT */
122 	LINUX_T_UNKNOWN,	/* 2 */
123 	3,			/* 3  T_BPTFLT */
124 	LINUX_T_UNKNOWN,	/* 4 */
125 	LINUX_T_UNKNOWN,	/* 5 */
126 	16,			/* 6  T_ARITHTRAP */
127 	254,			/* 7  T_ASTFLT */
128 	LINUX_T_UNKNOWN,	/* 8 */
129 	13,			/* 9  T_PROTFLT */
130 	1,			/* 10 T_TRCTRAP */
131 	LINUX_T_UNKNOWN,	/* 11 */
132 	14,			/* 12 T_PAGEFLT */
133 	LINUX_T_UNKNOWN,	/* 13 */
134 	17,			/* 14 T_ALIGNFLT */
135 	LINUX_T_UNKNOWN,	/* 15 */
136 	LINUX_T_UNKNOWN,	/* 16 */
137 	LINUX_T_UNKNOWN,	/* 17 */
138 	0,			/* 18 T_DIVIDE */
139 	2,			/* 19 T_NMI */
140 	4,			/* 20 T_OFLOW */
141 	5,			/* 21 T_BOUND */
142 	7,			/* 22 T_DNA */
143 	8,			/* 23 T_DOUBLEFLT */
144 	9,			/* 24 T_FPOPFLT */
145 	10,			/* 25 T_TSSFLT */
146 	11,			/* 26 T_SEGNPFLT */
147 	12,			/* 27 T_STKFLT */
148 	18,			/* 28 T_MCHK */
149 	19,			/* 29 T_XMMFLT */
150 	15			/* 30 T_RESERVED */
151 };
152 #define bsd_to_linux_trapcode(code) \
153     ((code)<nitems(_bsd_to_linux_trapcode)? \
154      _bsd_to_linux_trapcode[(code)]: \
155      LINUX_T_UNKNOWN)
156 
157 LINUX_VDSO_SYM_CHAR(linux_platform);
158 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
159 LINUX_VDSO_SYM_INTPTR(__kernel_sigreturn);
160 LINUX_VDSO_SYM_INTPTR(__kernel_rt_sigreturn);
161 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
162 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
163 
164 /*
165  * If FreeBSD & Linux have a difference of opinion about what a trap
166  * means, deal with it here.
167  *
168  * MPSAFE
169  */
170 static int
171 linux_translate_traps(int signal, int trap_code)
172 {
173 	if (signal != SIGBUS)
174 		return (signal);
175 	switch (trap_code) {
176 	case T_PROTFLT:
177 	case T_TSSFLT:
178 	case T_DOUBLEFLT:
179 	case T_PAGEFLT:
180 		return (SIGSEGV);
181 	default:
182 		return (signal);
183 	}
184 }
185 
186 static int
187 linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
188 {
189 	register_t *base, *argv, *envp;
190 
191 	base = (register_t *)*stack_base;
192 	argv = base;
193 	envp = base + (imgp->args->argc + 1);
194 	base--;
195 	suword(base, (intptr_t)envp);
196 	base--;
197 	suword(base, (intptr_t)argv);
198 	base--;
199 	suword(base, imgp->args->argc);
200 	*stack_base = (uintptr_t)base;
201 	return (0);
202 }
203 
204 static int
205 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
206 {
207 	struct proc *p;
208 	Elf32_Auxargs *args;
209 	Elf32_Auxinfo *argarray, *pos;
210 	struct ps_strings *arginfo;
211 	int error, issetugid;
212 
213 	p = imgp->proc;
214 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
215 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
216 	args = (Elf32_Auxargs *)imgp->auxargs;
217 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
218 	    M_WAITOK | M_ZERO);
219 
220 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
221 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
222 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
223 
224 	/*
225 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
226 	 * as it has appeared in the 2.4.0-rc7 first time.
227 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
228 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
229 	 * is not present.
230 	 * Also see linux_times() implementation.
231 	 */
232 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
233 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
234 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
235 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
236 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
237 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
238 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
239 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
240 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
241 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
242 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
243 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
244 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
245 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
246 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
247 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
248 	if (imgp->execpathp != 0)
249 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
250 	if (args->execfd != -1)
251 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
252 	AUXARGS_ENTRY(pos, AT_NULL, 0);
253 
254 	free(imgp->auxargs, M_TEMP);
255 	imgp->auxargs = NULL;
256 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
257 
258 	error = copyout(argarray, (void *)base,
259 	    sizeof(*argarray) * LINUX_AT_COUNT);
260 	free(argarray, M_TEMP);
261 	return (error);
262 }
263 
264 static int
265 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
266 {
267 	register_t *base;
268 
269 	base = (register_t *)*stack_base;
270 	base--;
271 	if (suword(base, (register_t)imgp->args->argc) == -1)
272 		return (EFAULT);
273 	*stack_base = (uintptr_t)base;
274 	return (0);
275 }
276 
277 /*
278  * Copied from kern/kern_exec.c
279  */
280 static int
281 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
282 {
283 	int argc, envc, error;
284 	char **vectp;
285 	char *stringp;
286 	uintptr_t destp, ustringp;
287 	struct ps_strings *arginfo;
288 	char canary[LINUX_AT_RANDOM_LEN];
289 	size_t execpath_len;
290 	struct proc *p;
291 
292 	/* Calculate string base and vector table pointers. */
293 	p = imgp->proc;
294 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
295 		execpath_len = strlen(imgp->execpath) + 1;
296 	else
297 		execpath_len = 0;
298 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
299 	destp = (uintptr_t)arginfo;
300 
301 	if (execpath_len != 0) {
302 		destp -= execpath_len;
303 		destp = rounddown2(destp, sizeof(void *));
304 		imgp->execpathp = (void *)destp;
305 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
306 		if (error != 0)
307 			return (error);
308 	}
309 
310 	/* Prepare the canary for SSP. */
311 	arc4rand(canary, sizeof(canary), 0);
312 	destp -= roundup(sizeof(canary), sizeof(void *));
313 	imgp->canary = (void *)destp;
314 	error = copyout(canary, imgp->canary, sizeof(canary));
315 	if (error != 0)
316 		return (error);
317 
318 	/* Allocate room for the argument and environment strings. */
319 	destp -= ARG_MAX - imgp->args->stringspace;
320 	destp = rounddown2(destp, sizeof(void *));
321 	ustringp = destp;
322 
323 	if (imgp->auxargs) {
324 		/*
325 		 * Allocate room on the stack for the ELF auxargs
326 		 * array.  It has LINUX_AT_COUNT entries.
327 		 */
328 		destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
329 		destp = rounddown2(destp, sizeof(void *));
330 	}
331 
332 	vectp = (char **)destp;
333 
334 	/*
335 	 * Allocate room for the argv[] and env vectors including the
336 	 * terminating NULL pointers.
337 	 */
338 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
339 
340 	/* vectp also becomes our initial stack base. */
341 	*stack_base = (uintptr_t)vectp;
342 
343 	stringp = imgp->args->begin_argv;
344 	argc = imgp->args->argc;
345 	envc = imgp->args->envc;
346 
347 	/* Copy out strings - arguments and environment. */
348 	error = copyout(stringp, (void *)ustringp,
349 	    ARG_MAX - imgp->args->stringspace);
350 	if (error != 0)
351 		return (error);
352 
353 	/* Fill in "ps_strings" struct for ps, w, etc. */
354 	if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
355 	    suword(&arginfo->ps_nargvstr, argc) != 0)
356 		return (EFAULT);
357 
358 	/* Fill in argument portion of vector table. */
359 	for (; argc > 0; --argc) {
360 		if (suword(vectp++, ustringp) != 0)
361 			return (EFAULT);
362 		while (*stringp++ != 0)
363 			ustringp++;
364 		ustringp++;
365 	}
366 
367 	/* A null vector table pointer separates the argp's from the envp's. */
368 	if (suword(vectp++, 0) != 0)
369 		return (EFAULT);
370 
371 	if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
372 	    suword(&arginfo->ps_nenvstr, envc) != 0)
373 		return (EFAULT);
374 
375 	/* Fill in environment portion of vector table. */
376 	for (; envc > 0; --envc) {
377 		if (suword(vectp++, ustringp) != 0)
378 			return (EFAULT);
379 		while (*stringp++ != 0)
380 			ustringp++;
381 		ustringp++;
382 	}
383 
384 	/* The end of the vector table is a null pointer. */
385 	if (suword(vectp, 0) != 0)
386 		return (EFAULT);
387 
388 	if (imgp->auxargs) {
389 		vectp++;
390 		error = imgp->sysent->sv_copyout_auxargs(imgp,
391 		    (uintptr_t)vectp);
392 		if (error != 0)
393 			return (error);
394 	}
395 
396 	return (0);
397 }
398 
399 static void
400 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
401 {
402 	struct thread *td = curthread;
403 	struct proc *p = td->td_proc;
404 	struct sigacts *psp;
405 	struct trapframe *regs;
406 	struct l_rt_sigframe *fp, frame;
407 	int sig, code;
408 	int oonstack;
409 
410 	sig = ksi->ksi_signo;
411 	code = ksi->ksi_code;
412 	PROC_LOCK_ASSERT(p, MA_OWNED);
413 	psp = p->p_sigacts;
414 	mtx_assert(&psp->ps_mtx, MA_OWNED);
415 	regs = td->td_frame;
416 	oonstack = sigonstack(regs->tf_esp);
417 
418 	/* Allocate space for the signal handler context. */
419 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
420 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
421 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
422 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
423 	} else
424 		fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
425 	mtx_unlock(&psp->ps_mtx);
426 
427 	/* Build the argument list for the signal handler. */
428 	sig = bsd_to_linux_signal(sig);
429 
430 	bzero(&frame, sizeof(frame));
431 
432 	frame.sf_handler = catcher;
433 	frame.sf_sig = sig;
434 	frame.sf_siginfo = &fp->sf_si;
435 	frame.sf_ucontext = &fp->sf_sc;
436 
437 	/* Fill in POSIX parts. */
438 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
439 
440 	/* Build the signal context to be used by sigreturn. */
441 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
442 	frame.sf_sc.uc_link = NULL;		/* XXX ??? */
443 
444 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
445 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
446 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
447 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
448 	PROC_UNLOCK(p);
449 
450 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
451 
452 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
453 	frame.sf_sc.uc_mcontext.sc_gs     = rgs();
454 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
455 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
456 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
457 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_edi;
458 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_esi;
459 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_ebp;
460 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_ebx;
461 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_esp;
462 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_edx;
463 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_ecx;
464 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_eax;
465 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_eip;
466 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
467 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
468 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
469 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
470 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
471 	frame.sf_sc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
472 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
473 
474 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
475 		/*
476 		 * Process has trashed its stack; give it an illegal
477 		 * instruction to halt it in its tracks.
478 		 */
479 		PROC_LOCK(p);
480 		sigexit(td, SIGILL);
481 	}
482 
483 	/* Build context to run handler in. */
484 	regs->tf_esp = (int)fp;
485 	regs->tf_eip = __kernel_rt_sigreturn;
486 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
487 	regs->tf_cs = _ucodesel;
488 	regs->tf_ds = _udatasel;
489 	regs->tf_es = _udatasel;
490 	regs->tf_fs = _udatasel;
491 	regs->tf_ss = _udatasel;
492 	PROC_LOCK(p);
493 	mtx_lock(&psp->ps_mtx);
494 }
495 
496 /*
497  * Send an interrupt to process.
498  *
499  * Stack is set up to allow sigcode stored
500  * in u. to call routine, followed by kcall
501  * to sigreturn routine below.  After sigreturn
502  * resets the signal mask, the stack, and the
503  * frame pointer, it returns to the user
504  * specified pc, psl.
505  */
506 static void
507 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
508 {
509 	struct thread *td = curthread;
510 	struct proc *p = td->td_proc;
511 	struct sigacts *psp;
512 	struct trapframe *regs;
513 	struct l_sigframe *fp, frame;
514 	l_sigset_t lmask;
515 	int sig, code;
516 	int oonstack;
517 
518 	PROC_LOCK_ASSERT(p, MA_OWNED);
519 	psp = p->p_sigacts;
520 	sig = ksi->ksi_signo;
521 	code = ksi->ksi_code;
522 	mtx_assert(&psp->ps_mtx, MA_OWNED);
523 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
524 		/* Signal handler installed with SA_SIGINFO. */
525 		linux_rt_sendsig(catcher, ksi, mask);
526 		return;
527 	}
528 	regs = td->td_frame;
529 	oonstack = sigonstack(regs->tf_esp);
530 
531 	/* Allocate space for the signal handler context. */
532 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
533 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
534 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
535 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
536 	} else
537 		fp = (struct l_sigframe *)regs->tf_esp - 1;
538 	mtx_unlock(&psp->ps_mtx);
539 	PROC_UNLOCK(p);
540 
541 	/* Build the argument list for the signal handler. */
542 	sig = bsd_to_linux_signal(sig);
543 
544 	bzero(&frame, sizeof(frame));
545 
546 	frame.sf_handler = catcher;
547 	frame.sf_sig = sig;
548 
549 	bsd_to_linux_sigset(mask, &lmask);
550 
551 	/* Build the signal context to be used by sigreturn. */
552 	frame.sf_sc.sc_mask   = lmask.__mask;
553 	frame.sf_sc.sc_gs     = rgs();
554 	frame.sf_sc.sc_fs     = regs->tf_fs;
555 	frame.sf_sc.sc_es     = regs->tf_es;
556 	frame.sf_sc.sc_ds     = regs->tf_ds;
557 	frame.sf_sc.sc_edi    = regs->tf_edi;
558 	frame.sf_sc.sc_esi    = regs->tf_esi;
559 	frame.sf_sc.sc_ebp    = regs->tf_ebp;
560 	frame.sf_sc.sc_ebx    = regs->tf_ebx;
561 	frame.sf_sc.sc_esp    = regs->tf_esp;
562 	frame.sf_sc.sc_edx    = regs->tf_edx;
563 	frame.sf_sc.sc_ecx    = regs->tf_ecx;
564 	frame.sf_sc.sc_eax    = regs->tf_eax;
565 	frame.sf_sc.sc_eip    = regs->tf_eip;
566 	frame.sf_sc.sc_cs     = regs->tf_cs;
567 	frame.sf_sc.sc_eflags = regs->tf_eflags;
568 	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
569 	frame.sf_sc.sc_ss     = regs->tf_ss;
570 	frame.sf_sc.sc_err    = regs->tf_err;
571 	frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
572 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
573 
574 	frame.sf_extramask[0] = lmask.__mask;
575 
576 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
577 		/*
578 		 * Process has trashed its stack; give it an illegal
579 		 * instruction to halt it in its tracks.
580 		 */
581 		PROC_LOCK(p);
582 		sigexit(td, SIGILL);
583 	}
584 
585 	/* Build context to run handler in. */
586 	regs->tf_esp = (int)fp;
587 	regs->tf_eip = __kernel_sigreturn;
588 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
589 	regs->tf_cs = _ucodesel;
590 	regs->tf_ds = _udatasel;
591 	regs->tf_es = _udatasel;
592 	regs->tf_fs = _udatasel;
593 	regs->tf_ss = _udatasel;
594 	PROC_LOCK(p);
595 	mtx_lock(&psp->ps_mtx);
596 }
597 
598 /*
599  * System call to cleanup state after a signal
600  * has been taken.  Reset signal mask and
601  * stack state from context left by sendsig (above).
602  * Return to previous pc and psl as specified by
603  * context left by sendsig. Check carefully to
604  * make sure that the user has not modified the
605  * psl to gain improper privileges or to cause
606  * a machine fault.
607  */
608 int
609 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
610 {
611 	struct l_sigframe frame;
612 	struct trapframe *regs;
613 	l_sigset_t lmask;
614 	sigset_t bmask;
615 	int eflags;
616 	ksiginfo_t ksi;
617 
618 	regs = td->td_frame;
619 
620 	/*
621 	 * The trampoline code hands us the sigframe.
622 	 * It is unsafe to keep track of it ourselves, in the event that a
623 	 * program jumps out of a signal handler.
624 	 */
625 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
626 		return (EFAULT);
627 
628 	/* Check for security violations. */
629 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
630 	eflags = frame.sf_sc.sc_eflags;
631 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
632 		return (EINVAL);
633 
634 	/*
635 	 * Don't allow users to load a valid privileged %cs.  Let the
636 	 * hardware check for invalid selectors, excess privilege in
637 	 * other selectors, invalid %eip's and invalid %esp's.
638 	 */
639 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
640 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
641 		ksiginfo_init_trap(&ksi);
642 		ksi.ksi_signo = SIGBUS;
643 		ksi.ksi_code = BUS_OBJERR;
644 		ksi.ksi_trapno = T_PROTFLT;
645 		ksi.ksi_addr = (void *)regs->tf_eip;
646 		trapsignal(td, &ksi);
647 		return (EINVAL);
648 	}
649 
650 	lmask.__mask = frame.sf_sc.sc_mask;
651 	linux_to_bsd_sigset(&lmask, &bmask);
652 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
653 
654 	/* Restore signal context. */
655 	/* %gs was restored by the trampoline. */
656 	regs->tf_fs     = frame.sf_sc.sc_fs;
657 	regs->tf_es     = frame.sf_sc.sc_es;
658 	regs->tf_ds     = frame.sf_sc.sc_ds;
659 	regs->tf_edi    = frame.sf_sc.sc_edi;
660 	regs->tf_esi    = frame.sf_sc.sc_esi;
661 	regs->tf_ebp    = frame.sf_sc.sc_ebp;
662 	regs->tf_ebx    = frame.sf_sc.sc_ebx;
663 	regs->tf_edx    = frame.sf_sc.sc_edx;
664 	regs->tf_ecx    = frame.sf_sc.sc_ecx;
665 	regs->tf_eax    = frame.sf_sc.sc_eax;
666 	regs->tf_eip    = frame.sf_sc.sc_eip;
667 	regs->tf_cs     = frame.sf_sc.sc_cs;
668 	regs->tf_eflags = eflags;
669 	regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
670 	regs->tf_ss     = frame.sf_sc.sc_ss;
671 
672 	return (EJUSTRETURN);
673 }
674 
675 /*
676  * System call to cleanup state after a signal
677  * has been taken.  Reset signal mask and
678  * stack state from context left by rt_sendsig (above).
679  * Return to previous pc and psl as specified by
680  * context left by sendsig. Check carefully to
681  * make sure that the user has not modified the
682  * psl to gain improper privileges or to cause
683  * a machine fault.
684  */
685 int
686 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
687 {
688 	struct l_ucontext uc;
689 	struct l_sigcontext *context;
690 	sigset_t bmask;
691 	l_stack_t *lss;
692 	stack_t ss;
693 	struct trapframe *regs;
694 	int eflags;
695 	ksiginfo_t ksi;
696 
697 	regs = td->td_frame;
698 
699 	/*
700 	 * The trampoline code hands us the ucontext.
701 	 * It is unsafe to keep track of it ourselves, in the event that a
702 	 * program jumps out of a signal handler.
703 	 */
704 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
705 		return (EFAULT);
706 
707 	context = &uc.uc_mcontext;
708 
709 	/* Check for security violations. */
710 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
711 	eflags = context->sc_eflags;
712 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
713 		return (EINVAL);
714 
715 	/*
716 	 * Don't allow users to load a valid privileged %cs.  Let the
717 	 * hardware check for invalid selectors, excess privilege in
718 	 * other selectors, invalid %eip's and invalid %esp's.
719 	 */
720 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
721 	if (!CS_SECURE(context->sc_cs)) {
722 		ksiginfo_init_trap(&ksi);
723 		ksi.ksi_signo = SIGBUS;
724 		ksi.ksi_code = BUS_OBJERR;
725 		ksi.ksi_trapno = T_PROTFLT;
726 		ksi.ksi_addr = (void *)regs->tf_eip;
727 		trapsignal(td, &ksi);
728 		return (EINVAL);
729 	}
730 
731 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
732 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
733 
734 	/* Restore signal context. */
735 	/* %gs was restored by the trampoline. */
736 	regs->tf_fs     = context->sc_fs;
737 	regs->tf_es     = context->sc_es;
738 	regs->tf_ds     = context->sc_ds;
739 	regs->tf_edi    = context->sc_edi;
740 	regs->tf_esi    = context->sc_esi;
741 	regs->tf_ebp    = context->sc_ebp;
742 	regs->tf_ebx    = context->sc_ebx;
743 	regs->tf_edx    = context->sc_edx;
744 	regs->tf_ecx    = context->sc_ecx;
745 	regs->tf_eax    = context->sc_eax;
746 	regs->tf_eip    = context->sc_eip;
747 	regs->tf_cs     = context->sc_cs;
748 	regs->tf_eflags = eflags;
749 	regs->tf_esp    = context->sc_esp_at_signal;
750 	regs->tf_ss     = context->sc_ss;
751 
752 	/* Call sigaltstack & ignore results. */
753 	lss = &uc.uc_stack;
754 	ss.ss_sp = PTRIN(lss->ss_sp);
755 	ss.ss_size = lss->ss_size;
756 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
757 
758 	(void)kern_sigaltstack(td, &ss, NULL);
759 
760 	return (EJUSTRETURN);
761 }
762 
763 static int
764 linux_fetch_syscall_args(struct thread *td)
765 {
766 	struct proc *p;
767 	struct trapframe *frame;
768 	struct syscall_args *sa;
769 
770 	p = td->td_proc;
771 	frame = td->td_frame;
772 	sa = &td->td_sa;
773 
774 	sa->code = frame->tf_eax;
775 	sa->original_code = sa->code;
776 	sa->args[0] = frame->tf_ebx;
777 	sa->args[1] = frame->tf_ecx;
778 	sa->args[2] = frame->tf_edx;
779 	sa->args[3] = frame->tf_esi;
780 	sa->args[4] = frame->tf_edi;
781 	sa->args[5] = frame->tf_ebp;	/* Unconfirmed */
782 
783 	if (sa->code >= p->p_sysent->sv_size)
784 		/* nosys */
785 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
786 	else
787 		sa->callp = &p->p_sysent->sv_table[sa->code];
788 
789 	td->td_retval[0] = 0;
790 	td->td_retval[1] = frame->tf_edx;
791 
792 	return (0);
793 }
794 
795 static void
796 linux_set_syscall_retval(struct thread *td, int error)
797 {
798 	struct trapframe *frame = td->td_frame;
799 
800 	cpu_set_syscall_retval(td, error);
801 
802 	if (__predict_false(error != 0)) {
803 		if (error != ERESTART && error != EJUSTRETURN)
804 			frame->tf_eax = bsd_to_linux_errno(error);
805 	}
806 }
807 
808 static void
809 linux_set_fork_retval(struct thread *td)
810 {
811 	struct trapframe *frame = td->td_frame;
812 
813 	frame->tf_eax = 0;
814 }
815 
816 /*
817  * exec_setregs may initialize some registers differently than Linux
818  * does, thus potentially confusing Linux binaries. If necessary, we
819  * override the exec_setregs default(s) here.
820  */
821 static void
822 linux_exec_setregs(struct thread *td, struct image_params *imgp,
823     uintptr_t stack)
824 {
825 	struct pcb *pcb = td->td_pcb;
826 
827 	exec_setregs(td, imgp, stack);
828 
829 	/* Linux sets %gs to 0, we default to _udatasel. */
830 	pcb->pcb_gs = 0;
831 	load_gs(0);
832 
833 	pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
834 }
835 
836 struct sysentvec linux_sysvec = {
837 	.sv_size	= LINUX_SYS_MAXSYSCALL,
838 	.sv_table	= linux_sysent,
839 	.sv_transtrap	= linux_translate_traps,
840 	.sv_fixup	= linux_fixup,
841 	.sv_sendsig	= linux_sendsig,
842 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
843 	.sv_szsigcode	= &linux_szsigcode,
844 	.sv_name	= "Linux a.out",
845 	.sv_coredump	= NULL,
846 	.sv_imgact_try	= linux_exec_imgact_try,
847 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
848 	.sv_minuser	= VM_MIN_ADDRESS,
849 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
850 	.sv_usrstack	= LINUX_USRSTACK,
851 	.sv_psstrings	= PS_STRINGS,
852 	.sv_stackprot	= VM_PROT_ALL,
853 	.sv_copyout_strings = exec_copyout_strings,
854 	.sv_setregs	= linux_exec_setregs,
855 	.sv_fixlimit	= NULL,
856 	.sv_maxssiz	= NULL,
857 	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
858 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
859 	.sv_set_syscall_retval = linux_set_syscall_retval,
860 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
861 	.sv_syscallnames = NULL,
862 	.sv_schedtail	= linux_schedtail,
863 	.sv_thread_detach = linux_thread_detach,
864 	.sv_trap	= NULL,
865 	.sv_onexec	= linux_on_exec_vmspace,
866 	.sv_onexit	= linux_on_exit,
867 	.sv_ontdexit	= linux_thread_dtor,
868 	.sv_setid_allowed = &linux_setid_allowed_query,
869 	.sv_set_fork_retval = linux_set_fork_retval,
870 };
871 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
872 
873 struct sysentvec elf_linux_sysvec = {
874 	.sv_size	= LINUX_SYS_MAXSYSCALL,
875 	.sv_table	= linux_sysent,
876 	.sv_transtrap	= linux_translate_traps,
877 	.sv_fixup	= linux_fixup_elf,
878 	.sv_sendsig	= linux_sendsig,
879 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
880 	.sv_szsigcode	= &linux_szsigcode,
881 	.sv_name	= "Linux ELF32",
882 	.sv_coredump	= elf32_coredump,
883 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
884 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
885 	.sv_elf_core_prepare_notes = elf32_prepare_notes,
886 	.sv_imgact_try	= linux_exec_imgact_try,
887 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
888 	.sv_minuser	= VM_MIN_ADDRESS,
889 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
890 	.sv_usrstack	= LINUX_USRSTACK,
891 	.sv_psstrings	= LINUX_PS_STRINGS,
892 	.sv_stackprot	= VM_PROT_ALL,
893 	.sv_copyout_auxargs = linux_copyout_auxargs,
894 	.sv_copyout_strings = linux_copyout_strings,
895 	.sv_setregs	= linux_exec_setregs,
896 	.sv_fixlimit	= NULL,
897 	.sv_maxssiz	= NULL,
898 	.sv_flags	= SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
899 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
900 	.sv_set_syscall_retval = linux_set_syscall_retval,
901 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
902 	.sv_syscallnames = NULL,
903 	.sv_shared_page_base = LINUX_SHAREDPAGE,
904 	.sv_shared_page_len = PAGE_SIZE,
905 	.sv_schedtail	= linux_schedtail,
906 	.sv_thread_detach = linux_thread_detach,
907 	.sv_trap	= NULL,
908 	.sv_onexec	= linux_on_exec_vmspace,
909 	.sv_onexit	= linux_on_exit,
910 	.sv_ontdexit	= linux_thread_dtor,
911 	.sv_setid_allowed = &linux_setid_allowed_query,
912 	.sv_set_fork_retval = linux_set_fork_retval,
913 };
914 
915 static int
916 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
917 {
918 	int error = 0;
919 
920 	if (SV_PROC_FLAG(p, SV_SHP) != 0)
921 		error = linux_map_vdso(p, linux_vdso_obj,
922 		    linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
923 	if (error == 0)
924 		linux_on_exec(p, imgp);
925 	return (error);
926 }
927 
928 /*
929  * linux_vdso_install() and linux_exec_sysvec_init() must be called
930  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
931  */
932 static void
933 linux_exec_sysvec_init(void *param)
934 {
935 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
936 	struct sysentvec *sv;
937 	ptrdiff_t tkoff;
938 
939 	sv = param;
940 	/* Fill timekeep_base */
941 	exec_sysvec_init(sv);
942 
943 	tkoff = kern_timekeep_base - linux_vdso_base;
944 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
945 	*ktimekeep_base = sv->sv_timekeep_base;
946 
947 	tkoff = kern_tsc_selector - linux_vdso_base;
948 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
949 	*ktsc_selector = linux_vdso_tsc_selector_idx();
950 	if (bootverbose)
951 		printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
952 }
953 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
954     linux_exec_sysvec_init, &elf_linux_sysvec);
955 
956 static void
957 linux_vdso_install(const void *param)
958 {
959 	char *vdso_start = &_binary_linux_vdso_so_o_start;
960 	char *vdso_end = &_binary_linux_vdso_so_o_end;
961 
962 	linux_szsigcode = vdso_end - vdso_start;
963 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
964 
965 	linux_vdso_base = LINUX_VDSOPAGE;
966 
967 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
968 
969 	linux_vdso_obj = __elfN(linux_shared_page_init)
970 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
971 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
972 
973 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
974 }
975 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
976     linux_vdso_install, NULL);
977 
978 static void
979 linux_vdso_deinstall(const void *param)
980 {
981 
982 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
983 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
984 }
985 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
986     linux_vdso_deinstall, NULL);
987 
988 static void
989 linux_vdso_reloc(char *mapping, Elf_Addr offset)
990 {
991 	const Elf_Shdr *shdr;
992 	const Elf_Rel *rel;
993 	const Elf_Ehdr *ehdr;
994 	Elf_Addr *where;
995 	Elf_Size rtype, symidx;
996 	Elf_Addr addr, addend;
997 	int i, relcnt;
998 
999 	MPASS(offset != 0);
1000 
1001 	relcnt = 0;
1002 	ehdr = (const Elf_Ehdr *)mapping;
1003 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
1004 	for (i = 0; i < ehdr->e_shnum; i++)
1005 	{
1006 		switch (shdr[i].sh_type) {
1007 		case SHT_REL:
1008 			rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
1009 			relcnt = shdr[i].sh_size / sizeof(*rel);
1010 			break;
1011 		case SHT_RELA:
1012 			printf("Linux i386 vDSO: unexpected Rela section\n");
1013 			break;
1014 		}
1015 	}
1016 
1017 	for (i = 0; i < relcnt; i++, rel++) {
1018 		where = (Elf_Addr *)(mapping + rel->r_offset);
1019 		addend = *where;
1020 		rtype = ELF_R_TYPE(rel->r_info);
1021 		symidx = ELF_R_SYM(rel->r_info);
1022 
1023 		switch (rtype) {
1024 		case R_386_NONE:	/* none */
1025 			break;
1026 
1027 		case R_386_RELATIVE:	/* B + A */
1028 			addr = (Elf_Addr)PTROUT(offset + addend);
1029 			if (*where != addr)
1030 				*where = addr;
1031 			break;
1032 
1033 		case R_386_IRELATIVE:
1034 			printf("Linux i386 vDSO: unexpected ifunc relocation, "
1035 			    "symbol index %d\n", symidx);
1036 			break;
1037 		default:
1038 			printf("Linux i386 vDSO: unexpected relocation type %d, "
1039 			    "symbol index %d\n", rtype, symidx);
1040 		}
1041 	}
1042 }
1043 
1044 static char GNU_ABI_VENDOR[] = "GNU";
1045 static int GNULINUX_ABI_DESC = 0;
1046 
1047 static bool
1048 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
1049 {
1050 	const Elf32_Word *desc;
1051 	uintptr_t p;
1052 
1053 	p = (uintptr_t)(note + 1);
1054 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1055 
1056 	desc = (const Elf32_Word *)p;
1057 	if (desc[0] != GNULINUX_ABI_DESC)
1058 		return (false);
1059 
1060 	/*
1061 	 * For Linux we encode osrel using the Linux convention of
1062 	 * 	(version << 16) | (major << 8) | (minor)
1063 	 * See macro in linux_mib.h
1064 	 */
1065 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
1066 
1067 	return (true);
1068 }
1069 
1070 static Elf_Brandnote linux_brandnote = {
1071 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
1072 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
1073 	.hdr.n_type	= 1,
1074 	.vendor		= GNU_ABI_VENDOR,
1075 	.flags		= BN_TRANSLATE_OSREL,
1076 	.trans_osrel	= linux_trans_osrel
1077 };
1078 
1079 static Elf32_Brandinfo linux_brand = {
1080 	.brand		= ELFOSABI_LINUX,
1081 	.machine	= EM_386,
1082 	.compat_3_brand	= "Linux",
1083 	.emul_path	= linux_emul_path,
1084 	.interp_path	= "/lib/ld-linux.so.1",
1085 	.sysvec		= &elf_linux_sysvec,
1086 	.interp_newpath	= NULL,
1087 	.brand_note	= &linux_brandnote,
1088 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1089 };
1090 
1091 static Elf32_Brandinfo linux_glibc2brand = {
1092 	.brand		= ELFOSABI_LINUX,
1093 	.machine	= EM_386,
1094 	.compat_3_brand	= "Linux",
1095 	.emul_path	= linux_emul_path,
1096 	.interp_path	= "/lib/ld-linux.so.2",
1097 	.sysvec		= &elf_linux_sysvec,
1098 	.interp_newpath	= NULL,
1099 	.brand_note	= &linux_brandnote,
1100 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1101 };
1102 
1103 static Elf32_Brandinfo linux_muslbrand = {
1104 	.brand		= ELFOSABI_LINUX,
1105 	.machine	= EM_386,
1106 	.compat_3_brand	= "Linux",
1107 	.emul_path	= linux_emul_path,
1108 	.interp_path	= "/lib/ld-musl-i386.so.1",
1109 	.sysvec		= &elf_linux_sysvec,
1110 	.interp_newpath	= NULL,
1111 	.brand_note	= &linux_brandnote,
1112 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1113 			    LINUX_BI_FUTEX_REQUEUE
1114 };
1115 
1116 Elf32_Brandinfo *linux_brandlist[] = {
1117 	&linux_brand,
1118 	&linux_glibc2brand,
1119 	&linux_muslbrand,
1120 	NULL
1121 };
1122 
1123 static int
1124 linux_elf_modevent(module_t mod, int type, void *data)
1125 {
1126 	Elf32_Brandinfo **brandinfo;
1127 	int error;
1128 	struct linux_ioctl_handler **lihp;
1129 
1130 	error = 0;
1131 
1132 	switch(type) {
1133 	case MOD_LOAD:
1134 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1135 		     ++brandinfo)
1136 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1137 				error = EINVAL;
1138 		if (error == 0) {
1139 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1140 				linux_ioctl_register_handler(*lihp);
1141 			linux_dev_shm_create();
1142 			linux_osd_jail_register();
1143 			stclohz = (stathz ? stathz : hz);
1144 			if (bootverbose)
1145 				printf("Linux ELF exec handler installed\n");
1146 		} else
1147 			printf("cannot insert Linux ELF brand handler\n");
1148 		break;
1149 	case MOD_UNLOAD:
1150 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1151 		     ++brandinfo)
1152 			if (elf32_brand_inuse(*brandinfo))
1153 				error = EBUSY;
1154 		if (error == 0) {
1155 			for (brandinfo = &linux_brandlist[0];
1156 			     *brandinfo != NULL; ++brandinfo)
1157 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1158 					error = EINVAL;
1159 		}
1160 		if (error == 0) {
1161 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1162 				linux_ioctl_unregister_handler(*lihp);
1163 			linux_dev_shm_destroy();
1164 			linux_osd_jail_deregister();
1165 			if (bootverbose)
1166 				printf("Linux ELF exec handler removed\n");
1167 		} else
1168 			printf("Could not deinstall ELF interpreter entry\n");
1169 		break;
1170 	default:
1171 		return (EOPNOTSUPP);
1172 	}
1173 	return (error);
1174 }
1175 
1176 static moduledata_t linux_elf_mod = {
1177 	"linuxelf",
1178 	linux_elf_modevent,
1179 	0
1180 };
1181 
1182 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1183 FEATURE(linux, "Linux 32bit support");
1184