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