xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
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	LINUX32_SYS_linux_rt_sendsig	0
109 #define	LINUX32_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 linux32_sysent[LINUX32_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)<nitems(_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 	int issetugid;
234 
235 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
236 
237 	KASSERT(curthread->td_proc == imgp->proc,
238 	    ("unsafe elf_linux_fixup(), should be curproc"));
239 	base = (Elf32_Addr *)*stack_base;
240 	args = (Elf32_Auxargs *)imgp->auxargs;
241 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
242 
243 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
244 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO_EHDR,
245 	    imgp->proc->p_sysent->sv_shared_page_base);
246 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
247 	AUXARGS_ENTRY_32(pos, LINUX_AT_HWCAP, cpu_feature);
248 
249 	/*
250 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
251 	 * as it has appeared in the 2.4.0-rc7 first time.
252 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
253 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
254 	 * is not present.
255 	 * Also see linux_times() implementation.
256 	 */
257 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
258 		AUXARGS_ENTRY_32(pos, LINUX_AT_CLKTCK, stclohz);
259 	AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr);
260 	AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent);
261 	AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum);
262 	AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz);
263 	AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags);
264 	AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry);
265 	AUXARGS_ENTRY_32(pos, AT_BASE, args->base);
266 	AUXARGS_ENTRY_32(pos, LINUX_AT_SECURE, issetugid);
267 	AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
268 	AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
269 	AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
270 	AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
271 	AUXARGS_ENTRY_32(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
272 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
273 	if (imgp->execpathp != 0)
274 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
275 	if (args->execfd != -1)
276 		AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd);
277 	AUXARGS_ENTRY_32(pos, AT_NULL, 0);
278 
279 	free(imgp->auxargs, M_TEMP);
280 	imgp->auxargs = NULL;
281 
282 	base--;
283 	suword32(base, (uint32_t)imgp->args->argc);
284 	*stack_base = (register_t *)base;
285 	return (0);
286 }
287 
288 static void
289 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
290 {
291 	struct thread *td = curthread;
292 	struct proc *p = td->td_proc;
293 	struct sigacts *psp;
294 	struct trapframe *regs;
295 	struct l_rt_sigframe *fp, frame;
296 	int oonstack;
297 	int sig;
298 	int code;
299 
300 	sig = ksi->ksi_signo;
301 	code = ksi->ksi_code;
302 	PROC_LOCK_ASSERT(p, MA_OWNED);
303 	psp = p->p_sigacts;
304 	mtx_assert(&psp->ps_mtx, MA_OWNED);
305 	regs = td->td_frame;
306 	oonstack = sigonstack(regs->tf_rsp);
307 
308 #ifdef DEBUG
309 	if (ldebug(rt_sendsig))
310 		printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
311 		    catcher, sig, (void*)mask, code);
312 #endif
313 	/*
314 	 * Allocate space for the signal handler context.
315 	 */
316 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
317 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
318 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
319 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
320 	} else
321 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
322 	mtx_unlock(&psp->ps_mtx);
323 
324 	/*
325 	 * Build the argument list for the signal handler.
326 	 */
327 	sig = bsd_to_linux_signal(sig);
328 
329 	bzero(&frame, sizeof(frame));
330 
331 	frame.sf_handler = PTROUT(catcher);
332 	frame.sf_sig = sig;
333 	frame.sf_siginfo = PTROUT(&fp->sf_si);
334 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
335 
336 	/* Fill in POSIX parts */
337 	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
338 
339 	/*
340 	 * Build the signal context to be used by sigreturn
341 	 * and libgcc unwind.
342 	 */
343 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
344 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
345 
346 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
347 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
348 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
349 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
350 	PROC_UNLOCK(p);
351 
352 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
353 
354 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
355 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
356 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
357 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
358 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
359 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
360 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
361 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
362 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
363 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
364 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
365 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
366 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
367 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
368 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
369 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
370 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
371 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
372 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
373 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
374 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
375 
376 #ifdef DEBUG
377 	if (ldebug(rt_sendsig))
378 		printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
379 		    frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
380 		    td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
381 #endif
382 
383 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
384 		/*
385 		 * Process has trashed its stack; give it an illegal
386 		 * instruction to halt it in its tracks.
387 		 */
388 #ifdef DEBUG
389 		if (ldebug(rt_sendsig))
390 			printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
391 			    fp, oonstack);
392 #endif
393 		PROC_LOCK(p);
394 		sigexit(td, SIGILL);
395 	}
396 
397 	/*
398 	 * Build context to run handler in.
399 	 */
400 	regs->tf_rsp = PTROUT(fp);
401 	regs->tf_rip = linux32_rt_sigcode;
402 	regs->tf_rflags &= ~(PSL_T | PSL_D);
403 	regs->tf_cs = _ucode32sel;
404 	regs->tf_ss = _udatasel;
405 	regs->tf_ds = _udatasel;
406 	regs->tf_es = _udatasel;
407 	regs->tf_fs = _ufssel;
408 	regs->tf_gs = _ugssel;
409 	regs->tf_flags = TF_HASSEGS;
410 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
411 	PROC_LOCK(p);
412 	mtx_lock(&psp->ps_mtx);
413 }
414 
415 
416 /*
417  * Send an interrupt to process.
418  *
419  * Stack is set up to allow sigcode stored
420  * in u. to call routine, followed by kcall
421  * to sigreturn routine below.  After sigreturn
422  * resets the signal mask, the stack, and the
423  * frame pointer, it returns to the user
424  * specified pc, psl.
425  */
426 static void
427 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
428 {
429 	struct thread *td = curthread;
430 	struct proc *p = td->td_proc;
431 	struct sigacts *psp;
432 	struct trapframe *regs;
433 	struct l_sigframe *fp, frame;
434 	l_sigset_t lmask;
435 	int oonstack;
436 	int sig, code;
437 
438 	sig = ksi->ksi_signo;
439 	code = ksi->ksi_code;
440 	PROC_LOCK_ASSERT(p, MA_OWNED);
441 	psp = p->p_sigacts;
442 	mtx_assert(&psp->ps_mtx, MA_OWNED);
443 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
444 		/* Signal handler installed with SA_SIGINFO. */
445 		linux_rt_sendsig(catcher, ksi, mask);
446 		return;
447 	}
448 
449 	regs = td->td_frame;
450 	oonstack = sigonstack(regs->tf_rsp);
451 
452 #ifdef DEBUG
453 	if (ldebug(sendsig))
454 		printf(ARGS(sendsig, "%p, %d, %p, %u"),
455 		    catcher, sig, (void*)mask, code);
456 #endif
457 
458 	/*
459 	 * Allocate space for the signal handler context.
460 	 */
461 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
462 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
463 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
464 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
465 	} else
466 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
467 	mtx_unlock(&psp->ps_mtx);
468 	PROC_UNLOCK(p);
469 
470 	/*
471 	 * Build the argument list for the signal handler.
472 	 */
473 	sig = bsd_to_linux_signal(sig);
474 
475 	bzero(&frame, sizeof(frame));
476 
477 	frame.sf_handler = PTROUT(catcher);
478 	frame.sf_sig = sig;
479 
480 	bsd_to_linux_sigset(mask, &lmask);
481 
482 	/*
483 	 * Build the signal context to be used by sigreturn.
484 	 */
485 	frame.sf_sc.sc_mask   = lmask.__mask;
486 	frame.sf_sc.sc_gs     = regs->tf_gs;
487 	frame.sf_sc.sc_fs     = regs->tf_fs;
488 	frame.sf_sc.sc_es     = regs->tf_es;
489 	frame.sf_sc.sc_ds     = regs->tf_ds;
490 	frame.sf_sc.sc_edi    = regs->tf_rdi;
491 	frame.sf_sc.sc_esi    = regs->tf_rsi;
492 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
493 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
494 	frame.sf_sc.sc_esp    = regs->tf_rsp;
495 	frame.sf_sc.sc_edx    = regs->tf_rdx;
496 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
497 	frame.sf_sc.sc_eax    = regs->tf_rax;
498 	frame.sf_sc.sc_eip    = regs->tf_rip;
499 	frame.sf_sc.sc_cs     = regs->tf_cs;
500 	frame.sf_sc.sc_eflags = regs->tf_rflags;
501 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
502 	frame.sf_sc.sc_ss     = regs->tf_ss;
503 	frame.sf_sc.sc_err    = regs->tf_err;
504 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
505 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
506 
507 	frame.sf_extramask[0] = lmask.__mask;
508 
509 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
510 		/*
511 		 * Process has trashed its stack; give it an illegal
512 		 * instruction to halt it in its tracks.
513 		 */
514 		PROC_LOCK(p);
515 		sigexit(td, SIGILL);
516 	}
517 
518 	/*
519 	 * Build context to run handler in.
520 	 */
521 	regs->tf_rsp = PTROUT(fp);
522 	regs->tf_rip = linux32_sigcode;
523 	regs->tf_rflags &= ~(PSL_T | PSL_D);
524 	regs->tf_cs = _ucode32sel;
525 	regs->tf_ss = _udatasel;
526 	regs->tf_ds = _udatasel;
527 	regs->tf_es = _udatasel;
528 	regs->tf_fs = _ufssel;
529 	regs->tf_gs = _ugssel;
530 	regs->tf_flags = TF_HASSEGS;
531 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
532 	PROC_LOCK(p);
533 	mtx_lock(&psp->ps_mtx);
534 }
535 
536 /*
537  * System call to cleanup state after a signal
538  * has been taken.  Reset signal mask and
539  * stack state from context left by sendsig (above).
540  * Return to previous pc and psl as specified by
541  * context left by sendsig. Check carefully to
542  * make sure that the user has not modified the
543  * psl to gain improper privileges or to cause
544  * a machine fault.
545  */
546 int
547 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
548 {
549 	struct l_sigframe frame;
550 	struct trapframe *regs;
551 	sigset_t bmask;
552 	l_sigset_t lmask;
553 	int eflags;
554 	ksiginfo_t ksi;
555 
556 	regs = td->td_frame;
557 
558 #ifdef DEBUG
559 	if (ldebug(sigreturn))
560 		printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
561 #endif
562 	/*
563 	 * The trampoline code hands us the sigframe.
564 	 * It is unsafe to keep track of it ourselves, in the event that a
565 	 * program jumps out of a signal handler.
566 	 */
567 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
568 		return (EFAULT);
569 
570 	/*
571 	 * Check for security violations.
572 	 */
573 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
574 	eflags = frame.sf_sc.sc_eflags;
575 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
576 		return(EINVAL);
577 
578 	/*
579 	 * Don't allow users to load a valid privileged %cs.  Let the
580 	 * hardware check for invalid selectors, excess privilege in
581 	 * other selectors, invalid %eip's and invalid %esp's.
582 	 */
583 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
584 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
585 		ksiginfo_init_trap(&ksi);
586 		ksi.ksi_signo = SIGBUS;
587 		ksi.ksi_code = BUS_OBJERR;
588 		ksi.ksi_trapno = T_PROTFLT;
589 		ksi.ksi_addr = (void *)regs->tf_rip;
590 		trapsignal(td, &ksi);
591 		return(EINVAL);
592 	}
593 
594 	lmask.__mask = frame.sf_sc.sc_mask;
595 	lmask.__mask = frame.sf_extramask[0];
596 	linux_to_bsd_sigset(&lmask, &bmask);
597 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
598 
599 	/*
600 	 * Restore signal context.
601 	 */
602 	regs->tf_rdi    = frame.sf_sc.sc_edi;
603 	regs->tf_rsi    = frame.sf_sc.sc_esi;
604 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
605 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
606 	regs->tf_rdx    = frame.sf_sc.sc_edx;
607 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
608 	regs->tf_rax    = frame.sf_sc.sc_eax;
609 	regs->tf_rip    = frame.sf_sc.sc_eip;
610 	regs->tf_cs     = frame.sf_sc.sc_cs;
611 	regs->tf_ds     = frame.sf_sc.sc_ds;
612 	regs->tf_es     = frame.sf_sc.sc_es;
613 	regs->tf_fs     = frame.sf_sc.sc_fs;
614 	regs->tf_gs     = frame.sf_sc.sc_gs;
615 	regs->tf_rflags = eflags;
616 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
617 	regs->tf_ss     = frame.sf_sc.sc_ss;
618 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
619 
620 	return (EJUSTRETURN);
621 }
622 
623 /*
624  * System call to cleanup state after a signal
625  * has been taken.  Reset signal mask and
626  * stack state from context left by rt_sendsig (above).
627  * Return to previous pc and psl as specified by
628  * context left by sendsig. Check carefully to
629  * make sure that the user has not modified the
630  * psl to gain improper privileges or to cause
631  * a machine fault.
632  */
633 int
634 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
635 {
636 	struct l_ucontext uc;
637 	struct l_sigcontext *context;
638 	sigset_t bmask;
639 	l_stack_t *lss;
640 	stack_t ss;
641 	struct trapframe *regs;
642 	int eflags;
643 	ksiginfo_t ksi;
644 
645 	regs = td->td_frame;
646 
647 #ifdef DEBUG
648 	if (ldebug(rt_sigreturn))
649 		printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
650 #endif
651 	/*
652 	 * The trampoline code hands us the ucontext.
653 	 * It is unsafe to keep track of it ourselves, in the event that a
654 	 * program jumps out of a signal handler.
655 	 */
656 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
657 		return (EFAULT);
658 
659 	context = &uc.uc_mcontext;
660 
661 	/*
662 	 * Check for security violations.
663 	 */
664 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
665 	eflags = context->sc_eflags;
666 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
667 		return(EINVAL);
668 
669 	/*
670 	 * Don't allow users to load a valid privileged %cs.  Let the
671 	 * hardware check for invalid selectors, excess privilege in
672 	 * other selectors, invalid %eip's and invalid %esp's.
673 	 */
674 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
675 	if (!CS_SECURE(context->sc_cs)) {
676 		ksiginfo_init_trap(&ksi);
677 		ksi.ksi_signo = SIGBUS;
678 		ksi.ksi_code = BUS_OBJERR;
679 		ksi.ksi_trapno = T_PROTFLT;
680 		ksi.ksi_addr = (void *)regs->tf_rip;
681 		trapsignal(td, &ksi);
682 		return(EINVAL);
683 	}
684 
685 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
686 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
687 
688 	/*
689 	 * Restore signal context
690 	 */
691 	regs->tf_gs	= context->sc_gs;
692 	regs->tf_fs	= context->sc_fs;
693 	regs->tf_es	= context->sc_es;
694 	regs->tf_ds	= context->sc_ds;
695 	regs->tf_rdi    = context->sc_edi;
696 	regs->tf_rsi    = context->sc_esi;
697 	regs->tf_rbp    = context->sc_ebp;
698 	regs->tf_rbx    = context->sc_ebx;
699 	regs->tf_rdx    = context->sc_edx;
700 	regs->tf_rcx    = context->sc_ecx;
701 	regs->tf_rax    = context->sc_eax;
702 	regs->tf_rip    = context->sc_eip;
703 	regs->tf_cs     = context->sc_cs;
704 	regs->tf_rflags = eflags;
705 	regs->tf_rsp    = context->sc_esp_at_signal;
706 	regs->tf_ss     = context->sc_ss;
707 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
708 
709 	/*
710 	 * call sigaltstack & ignore results..
711 	 */
712 	lss = &uc.uc_stack;
713 	ss.ss_sp = PTRIN(lss->ss_sp);
714 	ss.ss_size = lss->ss_size;
715 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
716 
717 #ifdef DEBUG
718 	if (ldebug(rt_sigreturn))
719 		printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
720 		    ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
721 #endif
722 	(void)kern_sigaltstack(td, &ss, NULL);
723 
724 	return (EJUSTRETURN);
725 }
726 
727 static int
728 linux32_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
729 {
730 	struct proc *p;
731 	struct trapframe *frame;
732 
733 	p = td->td_proc;
734 	frame = td->td_frame;
735 
736 	sa->args[0] = frame->tf_rbx;
737 	sa->args[1] = frame->tf_rcx;
738 	sa->args[2] = frame->tf_rdx;
739 	sa->args[3] = frame->tf_rsi;
740 	sa->args[4] = frame->tf_rdi;
741 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
742 	sa->code = frame->tf_rax;
743 
744 	if (sa->code >= p->p_sysent->sv_size)
745 		/* nosys */
746 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
747 	else
748 		sa->callp = &p->p_sysent->sv_table[sa->code];
749 	sa->narg = sa->callp->sy_narg;
750 
751 	td->td_retval[0] = 0;
752 	td->td_retval[1] = frame->tf_rdx;
753 
754 	return (0);
755 }
756 
757 /*
758  * If a linux binary is exec'ing something, try this image activator
759  * first.  We override standard shell script execution in order to
760  * be able to modify the interpreter path.  We only do this if a linux
761  * binary is doing the exec, so we do not create an EXEC module for it.
762  */
763 static int	exec_linux_imgact_try(struct image_params *iparams);
764 
765 static int
766 exec_linux_imgact_try(struct image_params *imgp)
767 {
768 	const char *head = (const char *)imgp->image_header;
769 	char *rpath;
770 	int error = -1;
771 
772 	/*
773 	* The interpreter for shell scripts run from a linux binary needs
774 	* to be located in /compat/linux if possible in order to recursively
775 	* maintain linux path emulation.
776 	*/
777 	if (((const short *)head)[0] == SHELLMAGIC) {
778 		/*
779 		* Run our normal shell image activator.  If it succeeds attempt
780 		* to use the alternate path for the interpreter.  If an
781 		* alternate * path is found, use our stringspace to store it.
782 		*/
783 		if ((error = exec_shell_imgact(imgp)) == 0) {
784 			linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
785 			    imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
786 			    AT_FDCWD);
787 			if (rpath != NULL)
788 				imgp->args->fname_buf =
789 				    imgp->interpreter_name = rpath;
790 		}
791 	}
792 	return (error);
793 }
794 
795 /*
796  * Clear registers on exec
797  * XXX copied from ia32_signal.c.
798  */
799 static void
800 exec_linux_setregs(struct thread *td, struct image_params *imgp, u_long stack)
801 {
802 	struct trapframe *regs = td->td_frame;
803 	struct pcb *pcb = td->td_pcb;
804 
805 	mtx_lock(&dt_lock);
806 	if (td->td_proc->p_md.md_ldt != NULL)
807 		user_ldt_free(td);
808 	else
809 		mtx_unlock(&dt_lock);
810 
811 	critical_enter();
812 	wrmsr(MSR_FSBASE, 0);
813 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
814 	pcb->pcb_fsbase = 0;
815 	pcb->pcb_gsbase = 0;
816 	critical_exit();
817 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
818 
819 	bzero((char *)regs, sizeof(struct trapframe));
820 	regs->tf_rip = imgp->entry_addr;
821 	regs->tf_rsp = stack;
822 	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
823 	regs->tf_gs = _ugssel;
824 	regs->tf_fs = _ufssel;
825 	regs->tf_es = _udatasel;
826 	regs->tf_ds = _udatasel;
827 	regs->tf_ss = _udatasel;
828 	regs->tf_flags = TF_HASSEGS;
829 	regs->tf_cs = _ucode32sel;
830 	regs->tf_rbx = imgp->ps_strings;
831 
832 	fpstate_drop(td);
833 
834 	/* Do full restore on return so that we can change to a different %cs */
835 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
836 	td->td_retval[1] = 0;
837 }
838 
839 /*
840  * XXX copied from ia32_sysvec.c.
841  */
842 static register_t *
843 linux_copyout_strings(struct image_params *imgp)
844 {
845 	int argc, envc;
846 	u_int32_t *vectp;
847 	char *stringp, *destp;
848 	u_int32_t *stack_base;
849 	struct linux32_ps_strings *arginfo;
850 	char canary[LINUX_AT_RANDOM_LEN];
851 	size_t execpath_len;
852 
853 	/*
854 	 * Calculate string base and vector table pointers.
855 	 */
856 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
857 		execpath_len = strlen(imgp->execpath) + 1;
858 	else
859 		execpath_len = 0;
860 
861 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
862 	destp =	(caddr_t)arginfo - SPARE_USRSPACE -
863 	    roundup(sizeof(canary), sizeof(char *)) -
864 	    roundup(execpath_len, sizeof(char *)) -
865 	    roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
866 
867 	if (execpath_len != 0) {
868 		imgp->execpathp = (uintptr_t)arginfo - execpath_len;
869 		copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
870 	}
871 
872 	/*
873 	 * Prepare the canary for SSP.
874 	 */
875 	arc4rand(canary, sizeof(canary), 0);
876 	imgp->canary = (uintptr_t)arginfo -
877 	    roundup(execpath_len, sizeof(char *)) -
878 	    roundup(sizeof(canary), sizeof(char *));
879 	copyout(canary, (void *)imgp->canary, sizeof(canary));
880 
881 	/*
882 	 * If we have a valid auxargs ptr, prepare some room
883 	 * on the stack.
884 	 */
885 	if (imgp->auxargs) {
886 		/*
887 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
888 		 * lower compatibility.
889 		 */
890 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
891 		    (LINUX_AT_COUNT * 2);
892 		/*
893 		 * The '+ 2' is for the null pointers at the end of each of
894 		 * the arg and env vector sets,and imgp->auxarg_size is room
895 		 * for argument of Runtime loader.
896 		 */
897 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
898 		    imgp->args->envc + 2 + imgp->auxarg_size) *
899 		    sizeof(u_int32_t));
900 
901 	} else
902 		/*
903 		 * The '+ 2' is for the null pointers at the end of each of
904 		 * the arg and env vector sets
905 		 */
906 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
907 		    imgp->args->envc + 2) * sizeof(u_int32_t));
908 
909 	/*
910 	 * vectp also becomes our initial stack base
911 	 */
912 	stack_base = vectp;
913 
914 	stringp = imgp->args->begin_argv;
915 	argc = imgp->args->argc;
916 	envc = imgp->args->envc;
917 	/*
918 	 * Copy out strings - arguments and environment.
919 	 */
920 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
921 
922 	/*
923 	 * Fill in "ps_strings" struct for ps, w, etc.
924 	 */
925 	suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
926 	suword32(&arginfo->ps_nargvstr, argc);
927 
928 	/*
929 	 * Fill in argument portion of vector table.
930 	 */
931 	for (; argc > 0; --argc) {
932 		suword32(vectp++, (uint32_t)(intptr_t)destp);
933 		while (*stringp++ != 0)
934 			destp++;
935 		destp++;
936 	}
937 
938 	/* a null vector table pointer separates the argp's from the envp's */
939 	suword32(vectp++, 0);
940 
941 	suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
942 	suword32(&arginfo->ps_nenvstr, envc);
943 
944 	/*
945 	 * Fill in environment portion of vector table.
946 	 */
947 	for (; envc > 0; --envc) {
948 		suword32(vectp++, (uint32_t)(intptr_t)destp);
949 		while (*stringp++ != 0)
950 			destp++;
951 		destp++;
952 	}
953 
954 	/* end of vector table is a null pointer */
955 	suword32(vectp, 0);
956 
957 	return ((register_t *)stack_base);
958 }
959 
960 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
961     "32-bit Linux emulation");
962 
963 static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
964 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
965     &linux32_maxdsiz, 0, "");
966 static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
967 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
968     &linux32_maxssiz, 0, "");
969 static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
970 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
971     &linux32_maxvmem, 0, "");
972 
973 #if defined(DEBUG)
974 SYSCTL_PROC(_compat_linux32, OID_AUTO, debug,
975             CTLTYPE_STRING | CTLFLAG_RW,
976             0, 0, linux_sysctl_debug, "A",
977             "Linux debugging control");
978 #endif
979 
980 static void
981 linux32_fixlimit(struct rlimit *rl, int which)
982 {
983 
984 	switch (which) {
985 	case RLIMIT_DATA:
986 		if (linux32_maxdsiz != 0) {
987 			if (rl->rlim_cur > linux32_maxdsiz)
988 				rl->rlim_cur = linux32_maxdsiz;
989 			if (rl->rlim_max > linux32_maxdsiz)
990 				rl->rlim_max = linux32_maxdsiz;
991 		}
992 		break;
993 	case RLIMIT_STACK:
994 		if (linux32_maxssiz != 0) {
995 			if (rl->rlim_cur > linux32_maxssiz)
996 				rl->rlim_cur = linux32_maxssiz;
997 			if (rl->rlim_max > linux32_maxssiz)
998 				rl->rlim_max = linux32_maxssiz;
999 		}
1000 		break;
1001 	case RLIMIT_VMEM:
1002 		if (linux32_maxvmem != 0) {
1003 			if (rl->rlim_cur > linux32_maxvmem)
1004 				rl->rlim_cur = linux32_maxvmem;
1005 			if (rl->rlim_max > linux32_maxvmem)
1006 				rl->rlim_max = linux32_maxvmem;
1007 		}
1008 		break;
1009 	}
1010 }
1011 
1012 struct sysentvec elf_linux_sysvec = {
1013 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
1014 	.sv_table	= linux32_sysent,
1015 	.sv_mask	= 0,
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_name	= "Linux ELF32",
1024 	.sv_coredump	= elf32_coredump,
1025 	.sv_imgact_try	= exec_linux_imgact_try,
1026 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
1027 	.sv_pagesize	= PAGE_SIZE,
1028 	.sv_minuser	= VM_MIN_ADDRESS,
1029 	.sv_maxuser	= LINUX32_MAXUSER,
1030 	.sv_usrstack	= LINUX32_USRSTACK,
1031 	.sv_psstrings	= LINUX32_PS_STRINGS,
1032 	.sv_stackprot	= VM_PROT_ALL,
1033 	.sv_copyout_strings = linux_copyout_strings,
1034 	.sv_setregs	= exec_linux_setregs,
1035 	.sv_fixlimit	= linux32_fixlimit,
1036 	.sv_maxssiz	= &linux32_maxssiz,
1037 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
1038 	.sv_set_syscall_retval = cpu_set_syscall_retval,
1039 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
1040 	.sv_syscallnames = NULL,
1041 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
1042 	.sv_shared_page_len = PAGE_SIZE,
1043 	.sv_schedtail	= linux_schedtail,
1044 	.sv_thread_detach = linux_thread_detach,
1045 	.sv_trap	= NULL,
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 FEATURE(linux, "Linux 32bit support");
1209