xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision 4436b51dff5736e74da464946049ea6899a88938)
1 /*-
2  * Copyright (c) 2004 Tim J. Robbins
3  * Copyright (c) 2003 Peter Wemm
4  * Copyright (c) 2002 Doug Rabson
5  * Copyright (c) 1998-1999 Andrew Gallatin
6  * Copyright (c) 1994-1996 Søren Schmidt
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 
37 #ifndef COMPAT_FREEBSD32
38 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
39 #endif
40 
41 #define	__ELF_WORD_SIZE	32
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/exec.h>
46 #include <sys/fcntl.h>
47 #include <sys/imgact.h>
48 #include <sys/imgact_elf.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysent.h>
60 #include <sys/sysproto.h>
61 #include <sys/vnode.h>
62 #include <sys/eventhandler.h>
63 
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_param.h>
71 
72 #include <machine/cpu.h>
73 #include <machine/md_var.h>
74 #include <machine/pcb.h>
75 #include <machine/specialreg.h>
76 
77 #include <amd64/linux32/linux.h>
78 #include <amd64/linux32/linux32_proto.h>
79 #include <compat/linux/linux_emul.h>
80 #include <compat/linux/linux_futex.h>
81 #include <compat/linux/linux_ioctl.h>
82 #include <compat/linux/linux_mib.h>
83 #include <compat/linux/linux_misc.h>
84 #include <compat/linux/linux_signal.h>
85 #include <compat/linux/linux_util.h>
86 #include <compat/linux/linux_vdso.h>
87 
88 MODULE_VERSION(linux, 1);
89 
90 #define	AUXARGS_ENTRY_32(pos, id, val)	\
91 	do {				\
92 		suword32(pos++, id);	\
93 		suword32(pos++, val);	\
94 	} while (0)
95 
96 #if BYTE_ORDER == LITTLE_ENDIAN
97 #define SHELLMAGIC      0x2123 /* #! */
98 #else
99 #define SHELLMAGIC      0x2321
100 #endif
101 
102 /*
103  * Allow the sendsig functions to use the ldebug() facility
104  * even though they are not syscalls themselves. Map them
105  * to syscall 0. This is slightly less bogus than using
106  * ldebug(sigreturn).
107  */
108 #define	LINUX_SYS_linux_rt_sendsig	0
109 #define	LINUX_SYS_linux_sendsig		0
110 
111 const char *linux_kplatform;
112 static int linux_szsigcode;
113 static vm_object_t linux_shared_page_obj;
114 static char *linux_shared_page_mapping;
115 extern char _binary_linux32_locore_o_start;
116 extern char _binary_linux32_locore_o_end;
117 
118 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
119 
120 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
121 
122 static int	elf_linux_fixup(register_t **stack_base,
123 		    struct image_params *iparams);
124 static register_t *linux_copyout_strings(struct image_params *imgp);
125 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
126 static void	exec_linux_setregs(struct thread *td,
127 				   struct image_params *imgp, u_long stack);
128 static void	linux32_fixlimit(struct rlimit *rl, int which);
129 static boolean_t linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
130 static void	linux_vdso_install(void *param);
131 static void	linux_vdso_deinstall(void *param);
132 
133 /*
134  * Linux syscalls return negative errno's, we do positive and map them
135  * Reference:
136  *   FreeBSD: src/sys/sys/errno.h
137  *   Linux:   linux-2.6.17.8/include/asm-generic/errno-base.h
138  *            linux-2.6.17.8/include/asm-generic/errno.h
139  */
140 static int bsd_to_linux_errno[ELAST + 1] = {
141 	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
142 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
143 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
144 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
145 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
146 	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
147 	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
148 	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
149 	  -6,  -6, -43, -42, -75,-125, -84, -95, -16, -74,
150 	 -72, -67, -71
151 };
152 
153 #define LINUX_T_UNKNOWN  255
154 static int _bsd_to_linux_trapcode[] = {
155 	LINUX_T_UNKNOWN,	/* 0 */
156 	6,			/* 1  T_PRIVINFLT */
157 	LINUX_T_UNKNOWN,	/* 2 */
158 	3,			/* 3  T_BPTFLT */
159 	LINUX_T_UNKNOWN,	/* 4 */
160 	LINUX_T_UNKNOWN,	/* 5 */
161 	16,			/* 6  T_ARITHTRAP */
162 	254,			/* 7  T_ASTFLT */
163 	LINUX_T_UNKNOWN,	/* 8 */
164 	13,			/* 9  T_PROTFLT */
165 	1,			/* 10 T_TRCTRAP */
166 	LINUX_T_UNKNOWN,	/* 11 */
167 	14,			/* 12 T_PAGEFLT */
168 	LINUX_T_UNKNOWN,	/* 13 */
169 	17,			/* 14 T_ALIGNFLT */
170 	LINUX_T_UNKNOWN,	/* 15 */
171 	LINUX_T_UNKNOWN,	/* 16 */
172 	LINUX_T_UNKNOWN,	/* 17 */
173 	0,			/* 18 T_DIVIDE */
174 	2,			/* 19 T_NMI */
175 	4,			/* 20 T_OFLOW */
176 	5,			/* 21 T_BOUND */
177 	7,			/* 22 T_DNA */
178 	8,			/* 23 T_DOUBLEFLT */
179 	9,			/* 24 T_FPOPFLT */
180 	10,			/* 25 T_TSSFLT */
181 	11,			/* 26 T_SEGNPFLT */
182 	12,			/* 27 T_STKFLT */
183 	18,			/* 28 T_MCHK */
184 	19,			/* 29 T_XMMFLT */
185 	15			/* 30 T_RESERVED */
186 };
187 #define bsd_to_linux_trapcode(code) \
188     ((code)<sizeof(_bsd_to_linux_trapcode)/sizeof(*_bsd_to_linux_trapcode)? \
189      _bsd_to_linux_trapcode[(code)]: \
190      LINUX_T_UNKNOWN)
191 
192 struct linux32_ps_strings {
193 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
194 	u_int ps_nargvstr;	/* the number of argument strings */
195 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
196 	u_int ps_nenvstr;	/* the number of environment strings */
197 };
198 
199 LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
200 LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
201 LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
202 LINUX_VDSO_SYM_CHAR(linux_platform);
203 
204 /*
205  * If FreeBSD & Linux have a difference of opinion about what a trap
206  * means, deal with it here.
207  *
208  * MPSAFE
209  */
210 static int
211 translate_traps(int signal, int trap_code)
212 {
213 	if (signal != SIGBUS)
214 		return signal;
215 	switch (trap_code) {
216 	case T_PROTFLT:
217 	case T_TSSFLT:
218 	case T_DOUBLEFLT:
219 	case T_PAGEFLT:
220 		return SIGSEGV;
221 	default:
222 		return signal;
223 	}
224 }
225 
226 static int
227 elf_linux_fixup(register_t **stack_base, struct image_params *imgp)
228 {
229 	Elf32_Auxargs *args;
230 	Elf32_Addr *base;
231 	Elf32_Addr *pos;
232 	struct linux32_ps_strings *arginfo;
233 
234 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
235 
236 	KASSERT(curthread->td_proc == imgp->proc,
237 	    ("unsafe elf_linux_fixup(), should be curproc"));
238 	base = (Elf32_Addr *)*stack_base;
239 	args = (Elf32_Auxargs *)imgp->auxargs;
240 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
241 
242 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO_EHDR,
243 	    imgp->proc->p_sysent->sv_shared_page_base);
244 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
245 	AUXARGS_ENTRY_32(pos, LINUX_AT_HWCAP, cpu_feature);
246 
247 	/*
248 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
249 	 * as it has appeared in the 2.4.0-rc7 first time.
250 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
251 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
252 	 * is not present.
253 	 * Also see linux_times() implementation.
254 	 */
255 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
256 		AUXARGS_ENTRY_32(pos, LINUX_AT_CLKTCK, stclohz);
257 	AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr);
258 	AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent);
259 	AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum);
260 	AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz);
261 	AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags);
262 	AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry);
263 	AUXARGS_ENTRY_32(pos, AT_BASE, args->base);
264 	AUXARGS_ENTRY_32(pos, LINUX_AT_SECURE, 0);
265 	AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
266 	AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
267 	AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
268 	AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
269 	AUXARGS_ENTRY_32(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
270 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
271 	if (imgp->execpathp != 0)
272 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
273 	if (args->execfd != -1)
274 		AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd);
275 	AUXARGS_ENTRY_32(pos, AT_NULL, 0);
276 
277 	free(imgp->auxargs, M_TEMP);
278 	imgp->auxargs = NULL;
279 
280 	base--;
281 	suword32(base, (uint32_t)imgp->args->argc);
282 	*stack_base = (register_t *)base;
283 	return (0);
284 }
285 
286 static void
287 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
288 {
289 	struct thread *td = curthread;
290 	struct proc *p = td->td_proc;
291 	struct sigacts *psp;
292 	struct trapframe *regs;
293 	struct l_rt_sigframe *fp, frame;
294 	int oonstack;
295 	int sig;
296 	int code;
297 
298 	sig = ksi->ksi_signo;
299 	code = ksi->ksi_code;
300 	PROC_LOCK_ASSERT(p, MA_OWNED);
301 	psp = p->p_sigacts;
302 	mtx_assert(&psp->ps_mtx, MA_OWNED);
303 	regs = td->td_frame;
304 	oonstack = sigonstack(regs->tf_rsp);
305 
306 #ifdef DEBUG
307 	if (ldebug(rt_sendsig))
308 		printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
309 		    catcher, sig, (void*)mask, code);
310 #endif
311 	/*
312 	 * Allocate space for the signal handler context.
313 	 */
314 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
315 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
316 		fp = (struct l_rt_sigframe *)(td->td_sigstk.ss_sp +
317 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
318 	} else
319 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
320 	mtx_unlock(&psp->ps_mtx);
321 
322 	/*
323 	 * Build the argument list for the signal handler.
324 	 */
325 	sig = bsd_to_linux_signal(sig);
326 
327 	bzero(&frame, sizeof(frame));
328 
329 	frame.sf_handler = PTROUT(catcher);
330 	frame.sf_sig = sig;
331 	frame.sf_siginfo = PTROUT(&fp->sf_si);
332 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
333 
334 	/* Fill in POSIX parts */
335 	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
336 
337 	/*
338 	 * Build the signal context to be used by sigreturn
339 	 * and libgcc unwind.
340 	 */
341 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
342 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
343 
344 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
345 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
346 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
347 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
348 	PROC_UNLOCK(p);
349 
350 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
351 
352 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
353 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
354 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
355 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
356 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
357 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
358 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
359 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
360 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
361 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
362 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
363 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
364 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
365 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
366 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
367 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
368 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
369 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
370 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
371 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
372 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
373 
374 #ifdef DEBUG
375 	if (ldebug(rt_sendsig))
376 		printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
377 		    frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
378 		    td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
379 #endif
380 
381 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
382 		/*
383 		 * Process has trashed its stack; give it an illegal
384 		 * instruction to halt it in its tracks.
385 		 */
386 #ifdef DEBUG
387 		if (ldebug(rt_sendsig))
388 			printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
389 			    fp, oonstack);
390 #endif
391 		PROC_LOCK(p);
392 		sigexit(td, SIGILL);
393 	}
394 
395 	/*
396 	 * Build context to run handler in.
397 	 */
398 	regs->tf_rsp = PTROUT(fp);
399 	regs->tf_rip = linux32_rt_sigcode;
400 	regs->tf_rflags &= ~(PSL_T | PSL_D);
401 	regs->tf_cs = _ucode32sel;
402 	regs->tf_ss = _udatasel;
403 	regs->tf_ds = _udatasel;
404 	regs->tf_es = _udatasel;
405 	regs->tf_fs = _ufssel;
406 	regs->tf_gs = _ugssel;
407 	regs->tf_flags = TF_HASSEGS;
408 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
409 	PROC_LOCK(p);
410 	mtx_lock(&psp->ps_mtx);
411 }
412 
413 
414 /*
415  * Send an interrupt to process.
416  *
417  * Stack is set up to allow sigcode stored
418  * in u. to call routine, followed by kcall
419  * to sigreturn routine below.  After sigreturn
420  * resets the signal mask, the stack, and the
421  * frame pointer, it returns to the user
422  * specified pc, psl.
423  */
424 static void
425 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
426 {
427 	struct thread *td = curthread;
428 	struct proc *p = td->td_proc;
429 	struct sigacts *psp;
430 	struct trapframe *regs;
431 	struct l_sigframe *fp, frame;
432 	l_sigset_t lmask;
433 	int oonstack;
434 	int sig, code;
435 
436 	sig = ksi->ksi_signo;
437 	code = ksi->ksi_code;
438 	PROC_LOCK_ASSERT(p, MA_OWNED);
439 	psp = p->p_sigacts;
440 	mtx_assert(&psp->ps_mtx, MA_OWNED);
441 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
442 		/* Signal handler installed with SA_SIGINFO. */
443 		linux_rt_sendsig(catcher, ksi, mask);
444 		return;
445 	}
446 
447 	regs = td->td_frame;
448 	oonstack = sigonstack(regs->tf_rsp);
449 
450 #ifdef DEBUG
451 	if (ldebug(sendsig))
452 		printf(ARGS(sendsig, "%p, %d, %p, %u"),
453 		    catcher, sig, (void*)mask, code);
454 #endif
455 
456 	/*
457 	 * Allocate space for the signal handler context.
458 	 */
459 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
460 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
461 		fp = (struct l_sigframe *)(td->td_sigstk.ss_sp +
462 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
463 	} else
464 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
465 	mtx_unlock(&psp->ps_mtx);
466 	PROC_UNLOCK(p);
467 
468 	/*
469 	 * Build the argument list for the signal handler.
470 	 */
471 	sig = bsd_to_linux_signal(sig);
472 
473 	bzero(&frame, sizeof(frame));
474 
475 	frame.sf_handler = PTROUT(catcher);
476 	frame.sf_sig = sig;
477 
478 	bsd_to_linux_sigset(mask, &lmask);
479 
480 	/*
481 	 * Build the signal context to be used by sigreturn.
482 	 */
483 	frame.sf_sc.sc_mask   = lmask.__mask;
484 	frame.sf_sc.sc_gs     = regs->tf_gs;
485 	frame.sf_sc.sc_fs     = regs->tf_fs;
486 	frame.sf_sc.sc_es     = regs->tf_es;
487 	frame.sf_sc.sc_ds     = regs->tf_ds;
488 	frame.sf_sc.sc_edi    = regs->tf_rdi;
489 	frame.sf_sc.sc_esi    = regs->tf_rsi;
490 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
491 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
492 	frame.sf_sc.sc_esp    = regs->tf_rsp;
493 	frame.sf_sc.sc_edx    = regs->tf_rdx;
494 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
495 	frame.sf_sc.sc_eax    = regs->tf_rax;
496 	frame.sf_sc.sc_eip    = regs->tf_rip;
497 	frame.sf_sc.sc_cs     = regs->tf_cs;
498 	frame.sf_sc.sc_eflags = regs->tf_rflags;
499 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
500 	frame.sf_sc.sc_ss     = regs->tf_ss;
501 	frame.sf_sc.sc_err    = regs->tf_err;
502 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
503 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
504 
505 	frame.sf_extramask[0] = lmask.__mask;
506 
507 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
508 		/*
509 		 * Process has trashed its stack; give it an illegal
510 		 * instruction to halt it in its tracks.
511 		 */
512 		PROC_LOCK(p);
513 		sigexit(td, SIGILL);
514 	}
515 
516 	/*
517 	 * Build context to run handler in.
518 	 */
519 	regs->tf_rsp = PTROUT(fp);
520 	regs->tf_rip = linux32_sigcode;
521 	regs->tf_rflags &= ~(PSL_T | PSL_D);
522 	regs->tf_cs = _ucode32sel;
523 	regs->tf_ss = _udatasel;
524 	regs->tf_ds = _udatasel;
525 	regs->tf_es = _udatasel;
526 	regs->tf_fs = _ufssel;
527 	regs->tf_gs = _ugssel;
528 	regs->tf_flags = TF_HASSEGS;
529 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
530 	PROC_LOCK(p);
531 	mtx_lock(&psp->ps_mtx);
532 }
533 
534 /*
535  * System call to cleanup state after a signal
536  * has been taken.  Reset signal mask and
537  * stack state from context left by sendsig (above).
538  * Return to previous pc and psl as specified by
539  * context left by sendsig. Check carefully to
540  * make sure that the user has not modified the
541  * psl to gain improper privileges or to cause
542  * a machine fault.
543  */
544 int
545 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
546 {
547 	struct l_sigframe frame;
548 	struct trapframe *regs;
549 	sigset_t bmask;
550 	l_sigset_t lmask;
551 	int eflags;
552 	ksiginfo_t ksi;
553 
554 	regs = td->td_frame;
555 
556 #ifdef DEBUG
557 	if (ldebug(sigreturn))
558 		printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
559 #endif
560 	/*
561 	 * The trampoline code hands us the sigframe.
562 	 * It is unsafe to keep track of it ourselves, in the event that a
563 	 * program jumps out of a signal handler.
564 	 */
565 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
566 		return (EFAULT);
567 
568 	/*
569 	 * Check for security violations.
570 	 */
571 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
572 	eflags = frame.sf_sc.sc_eflags;
573 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
574 		return(EINVAL);
575 
576 	/*
577 	 * Don't allow users to load a valid privileged %cs.  Let the
578 	 * hardware check for invalid selectors, excess privilege in
579 	 * other selectors, invalid %eip's and invalid %esp's.
580 	 */
581 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
582 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
583 		ksiginfo_init_trap(&ksi);
584 		ksi.ksi_signo = SIGBUS;
585 		ksi.ksi_code = BUS_OBJERR;
586 		ksi.ksi_trapno = T_PROTFLT;
587 		ksi.ksi_addr = (void *)regs->tf_rip;
588 		trapsignal(td, &ksi);
589 		return(EINVAL);
590 	}
591 
592 	lmask.__mask = frame.sf_sc.sc_mask;
593 	lmask.__mask = frame.sf_extramask[0];
594 	linux_to_bsd_sigset(&lmask, &bmask);
595 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
596 
597 	/*
598 	 * Restore signal context.
599 	 */
600 	regs->tf_rdi    = frame.sf_sc.sc_edi;
601 	regs->tf_rsi    = frame.sf_sc.sc_esi;
602 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
603 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
604 	regs->tf_rdx    = frame.sf_sc.sc_edx;
605 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
606 	regs->tf_rax    = frame.sf_sc.sc_eax;
607 	regs->tf_rip    = frame.sf_sc.sc_eip;
608 	regs->tf_cs     = frame.sf_sc.sc_cs;
609 	regs->tf_ds     = frame.sf_sc.sc_ds;
610 	regs->tf_es     = frame.sf_sc.sc_es;
611 	regs->tf_fs     = frame.sf_sc.sc_fs;
612 	regs->tf_gs     = frame.sf_sc.sc_gs;
613 	regs->tf_rflags = eflags;
614 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
615 	regs->tf_ss     = frame.sf_sc.sc_ss;
616 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
617 
618 	return (EJUSTRETURN);
619 }
620 
621 /*
622  * System call to cleanup state after a signal
623  * has been taken.  Reset signal mask and
624  * stack state from context left by rt_sendsig (above).
625  * Return to previous pc and psl as specified by
626  * context left by sendsig. Check carefully to
627  * make sure that the user has not modified the
628  * psl to gain improper privileges or to cause
629  * a machine fault.
630  */
631 int
632 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
633 {
634 	struct l_ucontext uc;
635 	struct l_sigcontext *context;
636 	sigset_t bmask;
637 	l_stack_t *lss;
638 	stack_t ss;
639 	struct trapframe *regs;
640 	int eflags;
641 	ksiginfo_t ksi;
642 
643 	regs = td->td_frame;
644 
645 #ifdef DEBUG
646 	if (ldebug(rt_sigreturn))
647 		printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
648 #endif
649 	/*
650 	 * The trampoline code hands us the ucontext.
651 	 * It is unsafe to keep track of it ourselves, in the event that a
652 	 * program jumps out of a signal handler.
653 	 */
654 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
655 		return (EFAULT);
656 
657 	context = &uc.uc_mcontext;
658 
659 	/*
660 	 * Check for security violations.
661 	 */
662 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
663 	eflags = context->sc_eflags;
664 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
665 		return(EINVAL);
666 
667 	/*
668 	 * Don't allow users to load a valid privileged %cs.  Let the
669 	 * hardware check for invalid selectors, excess privilege in
670 	 * other selectors, invalid %eip's and invalid %esp's.
671 	 */
672 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
673 	if (!CS_SECURE(context->sc_cs)) {
674 		ksiginfo_init_trap(&ksi);
675 		ksi.ksi_signo = SIGBUS;
676 		ksi.ksi_code = BUS_OBJERR;
677 		ksi.ksi_trapno = T_PROTFLT;
678 		ksi.ksi_addr = (void *)regs->tf_rip;
679 		trapsignal(td, &ksi);
680 		return(EINVAL);
681 	}
682 
683 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
684 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
685 
686 	/*
687 	 * Restore signal context
688 	 */
689 	regs->tf_gs	= context->sc_gs;
690 	regs->tf_fs	= context->sc_fs;
691 	regs->tf_es	= context->sc_es;
692 	regs->tf_ds	= context->sc_ds;
693 	regs->tf_rdi    = context->sc_edi;
694 	regs->tf_rsi    = context->sc_esi;
695 	regs->tf_rbp    = context->sc_ebp;
696 	regs->tf_rbx    = context->sc_ebx;
697 	regs->tf_rdx    = context->sc_edx;
698 	regs->tf_rcx    = context->sc_ecx;
699 	regs->tf_rax    = context->sc_eax;
700 	regs->tf_rip    = context->sc_eip;
701 	regs->tf_cs     = context->sc_cs;
702 	regs->tf_rflags = eflags;
703 	regs->tf_rsp    = context->sc_esp_at_signal;
704 	regs->tf_ss     = context->sc_ss;
705 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
706 
707 	/*
708 	 * call sigaltstack & ignore results..
709 	 */
710 	lss = &uc.uc_stack;
711 	ss.ss_sp = PTRIN(lss->ss_sp);
712 	ss.ss_size = lss->ss_size;
713 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
714 
715 #ifdef DEBUG
716 	if (ldebug(rt_sigreturn))
717 		printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
718 		    ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
719 #endif
720 	(void)kern_sigaltstack(td, &ss, NULL);
721 
722 	return (EJUSTRETURN);
723 }
724 
725 static int
726 linux32_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
727 {
728 	struct proc *p;
729 	struct trapframe *frame;
730 
731 	p = td->td_proc;
732 	frame = td->td_frame;
733 
734 	sa->args[0] = frame->tf_rbx;
735 	sa->args[1] = frame->tf_rcx;
736 	sa->args[2] = frame->tf_rdx;
737 	sa->args[3] = frame->tf_rsi;
738 	sa->args[4] = frame->tf_rdi;
739 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
740 	sa->code = frame->tf_rax;
741 
742 	if (sa->code >= p->p_sysent->sv_size)
743 		/* nosys */
744 		sa->callp = &p->p_sysent->sv_table[LINUX_SYS_MAXSYSCALL];
745 	else
746 		sa->callp = &p->p_sysent->sv_table[sa->code];
747 	sa->narg = sa->callp->sy_narg;
748 
749 	td->td_retval[0] = 0;
750 	td->td_retval[1] = frame->tf_rdx;
751 
752 	return (0);
753 }
754 
755 /*
756  * If a linux binary is exec'ing something, try this image activator
757  * first.  We override standard shell script execution in order to
758  * be able to modify the interpreter path.  We only do this if a linux
759  * binary is doing the exec, so we do not create an EXEC module for it.
760  */
761 static int	exec_linux_imgact_try(struct image_params *iparams);
762 
763 static int
764 exec_linux_imgact_try(struct image_params *imgp)
765 {
766 	const char *head = (const char *)imgp->image_header;
767 	char *rpath;
768 	int error = -1;
769 
770 	/*
771 	* The interpreter for shell scripts run from a linux binary needs
772 	* to be located in /compat/linux if possible in order to recursively
773 	* maintain linux path emulation.
774 	*/
775 	if (((const short *)head)[0] == SHELLMAGIC) {
776 		/*
777 		* Run our normal shell image activator.  If it succeeds attempt
778 		* to use the alternate path for the interpreter.  If an
779 		* alternate * path is found, use our stringspace to store it.
780 		*/
781 		if ((error = exec_shell_imgact(imgp)) == 0) {
782 			linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
783 			    imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
784 			    AT_FDCWD);
785 			if (rpath != NULL)
786 				imgp->args->fname_buf =
787 				    imgp->interpreter_name = rpath;
788 		}
789 	}
790 	return (error);
791 }
792 
793 /*
794  * Clear registers on exec
795  * XXX copied from ia32_signal.c.
796  */
797 static void
798 exec_linux_setregs(struct thread *td, struct image_params *imgp, u_long stack)
799 {
800 	struct trapframe *regs = td->td_frame;
801 	struct pcb *pcb = td->td_pcb;
802 
803 	mtx_lock(&dt_lock);
804 	if (td->td_proc->p_md.md_ldt != NULL)
805 		user_ldt_free(td);
806 	else
807 		mtx_unlock(&dt_lock);
808 
809 	critical_enter();
810 	wrmsr(MSR_FSBASE, 0);
811 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
812 	pcb->pcb_fsbase = 0;
813 	pcb->pcb_gsbase = 0;
814 	critical_exit();
815 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
816 
817 	bzero((char *)regs, sizeof(struct trapframe));
818 	regs->tf_rip = imgp->entry_addr;
819 	regs->tf_rsp = stack;
820 	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
821 	regs->tf_gs = _ugssel;
822 	regs->tf_fs = _ufssel;
823 	regs->tf_es = _udatasel;
824 	regs->tf_ds = _udatasel;
825 	regs->tf_ss = _udatasel;
826 	regs->tf_flags = TF_HASSEGS;
827 	regs->tf_cs = _ucode32sel;
828 	regs->tf_rbx = imgp->ps_strings;
829 
830 	fpstate_drop(td);
831 
832 	/* Do full restore on return so that we can change to a different %cs */
833 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
834 	td->td_retval[1] = 0;
835 }
836 
837 /*
838  * XXX copied from ia32_sysvec.c.
839  */
840 static register_t *
841 linux_copyout_strings(struct image_params *imgp)
842 {
843 	int argc, envc;
844 	u_int32_t *vectp;
845 	char *stringp, *destp;
846 	u_int32_t *stack_base;
847 	struct linux32_ps_strings *arginfo;
848 	char canary[LINUX_AT_RANDOM_LEN];
849 	size_t execpath_len;
850 
851 	/*
852 	 * Calculate string base and vector table pointers.
853 	 */
854 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
855 		execpath_len = strlen(imgp->execpath) + 1;
856 	else
857 		execpath_len = 0;
858 
859 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
860 	destp =	(caddr_t)arginfo - SPARE_USRSPACE -
861 	    roundup(sizeof(canary), sizeof(char *)) -
862 	    roundup(execpath_len, sizeof(char *)) -
863 	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
864 
865 	if (execpath_len != 0) {
866 		imgp->execpathp = (uintptr_t)arginfo - execpath_len;
867 		copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
868 	}
869 
870 	/*
871 	 * Prepare the canary for SSP.
872 	 */
873 	arc4rand(canary, sizeof(canary), 0);
874 	imgp->canary = (uintptr_t)arginfo -
875 	    roundup(execpath_len, sizeof(char *)) -
876 	    roundup(sizeof(canary), sizeof(char *));
877 	copyout(canary, (void *)imgp->canary, sizeof(canary));
878 
879 	/*
880 	 * If we have a valid auxargs ptr, prepare some room
881 	 * on the stack.
882 	 */
883 	if (imgp->auxargs) {
884 		/*
885 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
886 		 * lower compatibility.
887 		 */
888 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
889 		    (LINUX_AT_COUNT * 2);
890 		/*
891 		 * The '+ 2' is for the null pointers at the end of each of
892 		 * the arg and env vector sets,and imgp->auxarg_size is room
893 		 * for argument of Runtime loader.
894 		 */
895 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
896 		    imgp->args->envc + 2 + imgp->auxarg_size) *
897 		    sizeof(u_int32_t));
898 
899 	} else
900 		/*
901 		 * The '+ 2' is for the null pointers at the end of each of
902 		 * the arg and env vector sets
903 		 */
904 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
905 		    imgp->args->envc + 2) * sizeof(u_int32_t));
906 
907 	/*
908 	 * vectp also becomes our initial stack base
909 	 */
910 	stack_base = vectp;
911 
912 	stringp = imgp->args->begin_argv;
913 	argc = imgp->args->argc;
914 	envc = imgp->args->envc;
915 	/*
916 	 * Copy out strings - arguments and environment.
917 	 */
918 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
919 
920 	/*
921 	 * Fill in "ps_strings" struct for ps, w, etc.
922 	 */
923 	suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
924 	suword32(&arginfo->ps_nargvstr, argc);
925 
926 	/*
927 	 * Fill in argument portion of vector table.
928 	 */
929 	for (; argc > 0; --argc) {
930 		suword32(vectp++, (uint32_t)(intptr_t)destp);
931 		while (*stringp++ != 0)
932 			destp++;
933 		destp++;
934 	}
935 
936 	/* a null vector table pointer separates the argp's from the envp's */
937 	suword32(vectp++, 0);
938 
939 	suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
940 	suword32(&arginfo->ps_nenvstr, envc);
941 
942 	/*
943 	 * Fill in environment portion of vector table.
944 	 */
945 	for (; envc > 0; --envc) {
946 		suword32(vectp++, (uint32_t)(intptr_t)destp);
947 		while (*stringp++ != 0)
948 			destp++;
949 		destp++;
950 	}
951 
952 	/* end of vector table is a null pointer */
953 	suword32(vectp, 0);
954 
955 	return ((register_t *)stack_base);
956 }
957 
958 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
959     "32-bit Linux emulation");
960 
961 static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
962 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
963     &linux32_maxdsiz, 0, "");
964 static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
965 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
966     &linux32_maxssiz, 0, "");
967 static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
968 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
969     &linux32_maxvmem, 0, "");
970 
971 #if defined(DEBUG)
972 SYSCTL_PROC(_compat_linux32, OID_AUTO, debug,
973             CTLTYPE_STRING | CTLFLAG_RW,
974             0, 0, linux_sysctl_debug, "A",
975             "Linux debugging control");
976 #endif
977 
978 static void
979 linux32_fixlimit(struct rlimit *rl, int which)
980 {
981 
982 	switch (which) {
983 	case RLIMIT_DATA:
984 		if (linux32_maxdsiz != 0) {
985 			if (rl->rlim_cur > linux32_maxdsiz)
986 				rl->rlim_cur = linux32_maxdsiz;
987 			if (rl->rlim_max > linux32_maxdsiz)
988 				rl->rlim_max = linux32_maxdsiz;
989 		}
990 		break;
991 	case RLIMIT_STACK:
992 		if (linux32_maxssiz != 0) {
993 			if (rl->rlim_cur > linux32_maxssiz)
994 				rl->rlim_cur = linux32_maxssiz;
995 			if (rl->rlim_max > linux32_maxssiz)
996 				rl->rlim_max = linux32_maxssiz;
997 		}
998 		break;
999 	case RLIMIT_VMEM:
1000 		if (linux32_maxvmem != 0) {
1001 			if (rl->rlim_cur > linux32_maxvmem)
1002 				rl->rlim_cur = linux32_maxvmem;
1003 			if (rl->rlim_max > linux32_maxvmem)
1004 				rl->rlim_max = linux32_maxvmem;
1005 		}
1006 		break;
1007 	}
1008 }
1009 
1010 struct sysentvec elf_linux_sysvec = {
1011 	.sv_size	= LINUX_SYS_MAXSYSCALL,
1012 	.sv_table	= linux_sysent,
1013 	.sv_mask	= 0,
1014 	.sv_sigsize	= 0,
1015 	.sv_sigtbl	= NULL,
1016 	.sv_errsize	= ELAST + 1,
1017 	.sv_errtbl	= bsd_to_linux_errno,
1018 	.sv_transtrap	= translate_traps,
1019 	.sv_fixup	= elf_linux_fixup,
1020 	.sv_sendsig	= linux_sendsig,
1021 	.sv_sigcode	= &_binary_linux32_locore_o_start,
1022 	.sv_szsigcode	= &linux_szsigcode,
1023 	.sv_prepsyscall	= NULL,
1024 	.sv_name	= "Linux ELF32",
1025 	.sv_coredump	= elf32_coredump,
1026 	.sv_imgact_try	= exec_linux_imgact_try,
1027 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
1028 	.sv_pagesize	= PAGE_SIZE,
1029 	.sv_minuser	= VM_MIN_ADDRESS,
1030 	.sv_maxuser	= LINUX32_MAXUSER,
1031 	.sv_usrstack	= LINUX32_USRSTACK,
1032 	.sv_psstrings	= LINUX32_PS_STRINGS,
1033 	.sv_stackprot	= VM_PROT_ALL,
1034 	.sv_copyout_strings = linux_copyout_strings,
1035 	.sv_setregs	= exec_linux_setregs,
1036 	.sv_fixlimit	= linux32_fixlimit,
1037 	.sv_maxssiz	= &linux32_maxssiz,
1038 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
1039 	.sv_set_syscall_retval = cpu_set_syscall_retval,
1040 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
1041 	.sv_syscallnames = NULL,
1042 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
1043 	.sv_shared_page_len = PAGE_SIZE,
1044 	.sv_schedtail	= linux_schedtail,
1045 	.sv_thread_detach = linux_thread_detach,
1046 };
1047 
1048 static void
1049 linux_vdso_install(void *param)
1050 {
1051 
1052 	linux_szsigcode = (&_binary_linux32_locore_o_end -
1053 	    &_binary_linux32_locore_o_start);
1054 
1055 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
1056 		panic("Linux invalid vdso size\n");
1057 
1058 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
1059 
1060 	linux_shared_page_obj = __elfN(linux_shared_page_init)
1061 	    (&linux_shared_page_mapping);
1062 
1063 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec, LINUX32_SHAREDPAGE);
1064 
1065 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
1066 	    linux_szsigcode);
1067 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
1068 
1069 	linux_kplatform = linux_shared_page_mapping +
1070 	    (linux_platform - (caddr_t)LINUX32_SHAREDPAGE);
1071 }
1072 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
1073     (sysinit_cfunc_t)linux_vdso_install, NULL);
1074 
1075 static void
1076 linux_vdso_deinstall(void *param)
1077 {
1078 
1079 	__elfN(linux_shared_page_fini)(linux_shared_page_obj);
1080 };
1081 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
1082     (sysinit_cfunc_t)linux_vdso_deinstall, NULL);
1083 
1084 static char GNU_ABI_VENDOR[] = "GNU";
1085 static int GNULINUX_ABI_DESC = 0;
1086 
1087 static boolean_t
1088 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
1089 {
1090 	const Elf32_Word *desc;
1091 	uintptr_t p;
1092 
1093 	p = (uintptr_t)(note + 1);
1094 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1095 
1096 	desc = (const Elf32_Word *)p;
1097 	if (desc[0] != GNULINUX_ABI_DESC)
1098 		return (FALSE);
1099 
1100 	/*
1101 	 * For linux we encode osrel as follows (see linux_mib.c):
1102 	 * VVVMMMIII (version, major, minor), see linux_mib.c.
1103 	 */
1104 	*osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
1105 
1106 	return (TRUE);
1107 }
1108 
1109 static Elf_Brandnote linux32_brandnote = {
1110 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
1111 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
1112 	.hdr.n_type	= 1,
1113 	.vendor		= GNU_ABI_VENDOR,
1114 	.flags		= BN_TRANSLATE_OSREL,
1115 	.trans_osrel	= linux32_trans_osrel
1116 };
1117 
1118 static Elf32_Brandinfo linux_brand = {
1119 	.brand		= ELFOSABI_LINUX,
1120 	.machine	= EM_386,
1121 	.compat_3_brand	= "Linux",
1122 	.emul_path	= "/compat/linux",
1123 	.interp_path	= "/lib/ld-linux.so.1",
1124 	.sysvec		= &elf_linux_sysvec,
1125 	.interp_newpath	= NULL,
1126 	.brand_note	= &linux32_brandnote,
1127 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1128 };
1129 
1130 static Elf32_Brandinfo linux_glibc2brand = {
1131 	.brand		= ELFOSABI_LINUX,
1132 	.machine	= EM_386,
1133 	.compat_3_brand	= "Linux",
1134 	.emul_path	= "/compat/linux",
1135 	.interp_path	= "/lib/ld-linux.so.2",
1136 	.sysvec		= &elf_linux_sysvec,
1137 	.interp_newpath	= NULL,
1138 	.brand_note	= &linux32_brandnote,
1139 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1140 };
1141 
1142 Elf32_Brandinfo *linux_brandlist[] = {
1143 	&linux_brand,
1144 	&linux_glibc2brand,
1145 	NULL
1146 };
1147 
1148 static int
1149 linux_elf_modevent(module_t mod, int type, void *data)
1150 {
1151 	Elf32_Brandinfo **brandinfo;
1152 	int error;
1153 	struct linux_ioctl_handler **lihp;
1154 
1155 	error = 0;
1156 
1157 	switch(type) {
1158 	case MOD_LOAD:
1159 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1160 		     ++brandinfo)
1161 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1162 				error = EINVAL;
1163 		if (error == 0) {
1164 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1165 				linux_ioctl_register_handler(*lihp);
1166 			LIST_INIT(&futex_list);
1167 			mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1168 			stclohz = (stathz ? stathz : hz);
1169 			if (bootverbose)
1170 				printf("Linux ELF exec handler installed\n");
1171 		} else
1172 			printf("cannot insert Linux ELF brand handler\n");
1173 		break;
1174 	case MOD_UNLOAD:
1175 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1176 		     ++brandinfo)
1177 			if (elf32_brand_inuse(*brandinfo))
1178 				error = EBUSY;
1179 		if (error == 0) {
1180 			for (brandinfo = &linux_brandlist[0];
1181 			     *brandinfo != NULL; ++brandinfo)
1182 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1183 					error = EINVAL;
1184 		}
1185 		if (error == 0) {
1186 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1187 				linux_ioctl_unregister_handler(*lihp);
1188 			mtx_destroy(&futex_mtx);
1189 			if (bootverbose)
1190 				printf("Linux ELF exec handler removed\n");
1191 		} else
1192 			printf("Could not deinstall ELF interpreter entry\n");
1193 		break;
1194 	default:
1195 		return (EOPNOTSUPP);
1196 	}
1197 	return (error);
1198 }
1199 
1200 static moduledata_t linux_elf_mod = {
1201 	"linuxelf",
1202 	linux_elf_modevent,
1203 	0
1204 };
1205 
1206 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1207 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1208