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