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