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