linux_sysvec.c revision 294136
12061Sjkh/*-
250479Speter * Copyright (c) 1994-1996 S��ren Schmidt
32061Sjkh * All rights reserved.
438666Sjb *
532427Sjb * Redistribution and use in source and binary forms, with or without
638666Sjb * modification, are permitted provided that the following conditions
738666Sjb * are met:
838666Sjb * 1. Redistributions of source code must retain the above copyright
938666Sjb *    notice, this list of conditions and the following disclaimer
1064049Salex *    in this position and unchanged.
1164049Salex * 2. Redistributions in binary form must reproduce the above copyright
1266071Smarkm *    notice, this list of conditions and the following disclaimer in the
1373504Sobrien *    documentation and/or other materials provided with the distribution.
1438666Sjb * 3. The name of the author may not be used to endorse or promote products
1544918Sjkh *    derived from this software without specific prior written permission
1638666Sjb *
1738666Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1838666Sjb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1938666Sjb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2038666Sjb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2138666Sjb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2238666Sjb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2338978Sjb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2438978Sjb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2532427Sjb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2638666Sjb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2738666Sjb */
2838666Sjb
2938666Sjb#include <sys/cdefs.h>
3038666Sjb__FBSDID("$FreeBSD: stable/10/sys/i386/linux/linux_sysvec.c 294136 2016-01-16 07:56:49Z dchagin $");
3138666Sjb
3217308Speter#include <sys/param.h>
3391606Skeramida#include <sys/systm.h>
3419175Sbde#include <sys/exec.h>
3596205Sjwd#include <sys/fcntl.h>
3696205Sjwd#include <sys/imgact.h>
3738042Sbde#include <sys/imgact_aout.h>
3896205Sjwd#include <sys/imgact_elf.h>
3996205Sjwd#include <sys/kernel.h>
4038042Sbde#include <sys/lock.h>
4196205Sjwd#include <sys/malloc.h>
4296205Sjwd#include <sys/module.h>
4317308Speter#include <sys/mutex.h>
4496205Sjwd#include <sys/proc.h>
4596205Sjwd#include <sys/signalvar.h>
4617308Speter#include <sys/syscallsubr.h>
4796205Sjwd#include <sys/sysctl.h>
4896205Sjwd#include <sys/sysent.h>
4996205Sjwd#include <sys/sysproto.h>
5096205Sjwd#include <sys/vnode.h>
5196205Sjwd#include <sys/eventhandler.h>
5296205Sjwd
5396205Sjwd#include <vm/vm.h>
5496205Sjwd#include <vm/pmap.h>
5596205Sjwd#include <vm/vm_extern.h>
5696205Sjwd#include <vm/vm_map.h>
5796205Sjwd#include <vm/vm_object.h>
5896205Sjwd#include <vm/vm_page.h>
5945108Sobrien#include <vm/vm_param.h>
6042128Speter
6142128Speter#include <machine/cpu.h>
6238666Sjb#include <machine/cputypes.h>
6351361Sjb#include <machine/md_var.h>
6438666Sjb#include <machine/pcb.h>
6517308Speter
6698775Sdillon#include <i386/linux/linux.h>
6798723Sdillon#include <i386/linux/linux_proto.h>
6898723Sdillon#include <compat/linux/linux_emul.h>
6998723Sdillon#include <compat/linux/linux_futex.h>
7098723Sdillon#include <compat/linux/linux_ioctl.h>
7138666Sjb#include <compat/linux/linux_mib.h>
7217308Speter#include <compat/linux/linux_misc.h>
7338666Sjb#include <compat/linux/linux_signal.h>
7417308Speter#include <compat/linux/linux_util.h>
7527910Sasami#include <compat/linux/linux_vdso.h>
7643226Sjkh
7743226SjkhMODULE_VERSION(linux, 1);
7843226Sjkh
7938666Sjb#if BYTE_ORDER == LITTLE_ENDIAN
8027910Sasami#define SHELLMAGIC      0x2123 /* #! */
8138666Sjb#else
8238666Sjb#define SHELLMAGIC      0x2321
8338666Sjb#endif
8427910Sasami
8538666Sjb#if defined(DEBUG)
8638666SjbSYSCTL_PROC(_compat_linux, OID_AUTO, debug,
8743226Sjkh            CTLTYPE_STRING | CTLFLAG_RW,
8843226Sjkh            0, 0, linux_sysctl_debug, "A",
8927910Sasami            "Linux debugging control");
9038666Sjb#endif
9138666Sjb
9227910Sasami/*
9338666Sjb * Allow the sendsig functions to use the ldebug() facility
9427910Sasami * even though they are not syscalls themselves. Map them
9517308Speter * to syscall 0. This is slightly less bogus than using
9638666Sjb * ldebug(sigreturn).
9738666Sjb */
9817308Speter#define	LINUX_SYS_linux_rt_sendsig	0
9995509Sru#define	LINUX_SYS_linux_sendsig		0
10095793Sru
10197252Sru#define	LINUX_PS_STRINGS	(LINUX_USRSTACK - sizeof(struct ps_strings))
10296164Sru
10395146Sgshapirostatic int linux_szsigcode;
1042061Sjkhstatic vm_object_t linux_shared_page_obj;
10597769Srustatic char *linux_shared_page_mapping;
10697252Sruextern char _binary_linux_locore_o_start;
10797252Sruextern char _binary_linux_locore_o_end;
10895730Sru
10995793Sruextern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
11095730Sru
11195730SruSET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
11295730Sru
11355026Smarcelstatic int	linux_fixup(register_t **stack_base,
11455026Smarcel		    struct image_params *iparams);
11554324Smarcelstatic int	elf_linux_fixup(register_t **stack_base,
11617308Speter		    struct image_params *iparams);
11738666Sjbstatic void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
11817308Speterstatic void	exec_linux_setregs(struct thread *td,
11997252Sru		    struct image_params *imgp, u_long stack);
12038666Sjbstatic register_t *linux_copyout_strings(struct image_params *imgp);
12154324Smarcelstatic boolean_t linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
1222302Spaulstatic void	linux_vdso_install(void *param);
12339206Sjkhstatic void	linux_vdso_deinstall(void *param);
12439206Sjkh
12539206Sjkhstatic int linux_szplatform;
12673349Sruconst char *linux_kplatform;
12717308Speter
12854324Smarcelstatic eventhandler_tag linux_exit_tag;
12954324Smarcelstatic eventhandler_tag linux_exec_tag;
13054324Smarcelstatic eventhandler_tag linux_thread_dtor_tag;
13154324Smarcel
13254324Smarcel/*
13354324Smarcel * Linux syscalls return negative errno's, we do positive and map them
13454324Smarcel * Reference:
13569659Sobrien *   FreeBSD: src/sys/sys/errno.h
13654324Smarcel *   Linux:   linux-2.6.17.8/include/asm-generic/errno-base.h
13754324Smarcel *            linux-2.6.17.8/include/asm-generic/errno.h
13854324Smarcel */
13954324Smarcelstatic int bsd_to_linux_errno[ELAST + 1] = {
14054324Smarcel	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
14154324Smarcel	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
14254324Smarcel	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
14354324Smarcel	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
14454324Smarcel	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
14554324Smarcel	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
14654324Smarcel	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
14754324Smarcel	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
14854324Smarcel	  -6,  -6, -43, -42, -75,-125, -84, -95, -16, -74,
14954324Smarcel	 -72, -67, -71
15054324Smarcel};
15154324Smarcel
15254324Smarcel#define LINUX_T_UNKNOWN  255
15354324Smarcelstatic int _bsd_to_linux_trapcode[] = {
15454324Smarcel	LINUX_T_UNKNOWN,	/* 0 */
15597536Sobrien	6,			/* 1  T_PRIVINFLT */
15654324Smarcel	LINUX_T_UNKNOWN,	/* 2 */
15754324Smarcel	3,			/* 3  T_BPTFLT */
15854324Smarcel	LINUX_T_UNKNOWN,	/* 4 */
15995730Sru	LINUX_T_UNKNOWN,	/* 5 */
16095730Sru	16,			/* 6  T_ARITHTRAP */
16195730Sru	254,			/* 7  T_ASTFLT */
16295730Sru	LINUX_T_UNKNOWN,	/* 8 */
16395730Sru	13,			/* 9  T_PROTFLT */
16495730Sru	1,			/* 10 T_TRCTRAP */
16595730Sru	LINUX_T_UNKNOWN,	/* 11 */
16638666Sjb	14,			/* 12 T_PAGEFLT */
16738666Sjb	LINUX_T_UNKNOWN,	/* 13 */
16817308Speter	17,			/* 14 T_ALIGNFLT */
16938666Sjb	LINUX_T_UNKNOWN,	/* 15 */
17038666Sjb	LINUX_T_UNKNOWN,	/* 16 */
17138666Sjb	LINUX_T_UNKNOWN,	/* 17 */
17217308Speter	0,			/* 18 T_DIVIDE */
17355678Smarcel	2,			/* 19 T_NMI */
17455678Smarcel	4,			/* 20 T_OFLOW */
175100402Sru	5,			/* 21 T_BOUND */
176100402Sru	7,			/* 22 T_DNA */
177100402Sru	8,			/* 23 T_DOUBLEFLT */
17890395Sru	9,			/* 24 T_FPOPFLT */
179100402Sru	10,			/* 25 T_TSSFLT */
180100402Sru	11,			/* 26 T_SEGNPFLT */
181100402Sru	12,			/* 27 T_STKFLT */
182100402Sru	18,			/* 28 T_MCHK */
1832061Sjkh	19,			/* 29 T_XMMFLT */
18417308Speter	15			/* 30 T_RESERVED */
18538666Sjb};
186100401Sru#define bsd_to_linux_trapcode(code) \
187100401Sru    ((code)<sizeof(_bsd_to_linux_trapcode)/sizeof(*_bsd_to_linux_trapcode)? \
18817308Speter     _bsd_to_linux_trapcode[(code)]: \
18955678Smarcel     LINUX_T_UNKNOWN)
190100401Sru
191100401SruLINUX_VDSO_SYM_INTPTR(linux_sigcode);
192100401SruLINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
193100401SruLINUX_VDSO_SYM_INTPTR(linux_vsyscall);
194100401Sru
1953626Swollman/*
19617308Speter * If FreeBSD & Linux have a difference of opinion about what a trap
19755678Smarcel * means, deal with it here.
19855678Smarcel *
19955678Smarcel * MPSAFE
20055678Smarcel */
20155678Smarcelstatic int
20255678Smarceltranslate_traps(int signal, int trap_code)
20355678Smarcel{
20455678Smarcel	if (signal != SIGBUS)
20555678Smarcel		return (signal);
20655678Smarcel	switch (trap_code) {
20755678Smarcel	case T_PROTFLT:
20855678Smarcel	case T_TSSFLT:
20955678Smarcel	case T_DOUBLEFLT:
21038666Sjb	case T_PAGEFLT:
21138666Sjb		return (SIGSEGV);
21217308Speter	default:
21355678Smarcel		return (signal);
21438978Sjb	}
2153626Swollman}
21617308Speter
21738666Sjbstatic int
21817308Speterlinux_fixup(register_t **stack_base, struct image_params *imgp)
21943226Sjkh{
22043226Sjkh	register_t *argv, *envp;
22143226Sjkh
22238666Sjb	argv = *stack_base;
22338666Sjb	envp = *stack_base + (imgp->args->argc + 1);
22444103Smsmith	(*stack_base)--;
225	suword(*stack_base, (intptr_t)(void *)envp);
226	(*stack_base)--;
227	suword(*stack_base, (intptr_t)(void *)argv);
228	(*stack_base)--;
229	suword(*stack_base, imgp->args->argc);
230	return (0);
231}
232
233static int
234elf_linux_fixup(register_t **stack_base, struct image_params *imgp)
235{
236	struct proc *p;
237	Elf32_Auxargs *args;
238	Elf32_Addr *uplatform;
239	struct ps_strings *arginfo;
240	register_t *pos;
241
242	KASSERT(curthread->td_proc == imgp->proc,
243	    ("unsafe elf_linux_fixup(), should be curproc"));
244
245	p = imgp->proc;
246	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
247	uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform);
248	args = (Elf32_Auxargs *)imgp->auxargs;
249	pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
250
251	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
252	    imgp->proc->p_sysent->sv_shared_page_base);
253	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall);
254	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
255
256	/*
257	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
258	 * as it has appeared in the 2.4.0-rc7 first time.
259	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
260	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
261	 * is not present.
262	 * Also see linux_times() implementation.
263	 */
264	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
265		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
266	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
267	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
268	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
269	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
270	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
271	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
272	AUXARGS_ENTRY(pos, AT_BASE, args->base);
273	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, 0);
274	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
275	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
276	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
277	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
278	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform));
279	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
280	if (imgp->execpathp != 0)
281		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
282	if (args->execfd != -1)
283		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
284	AUXARGS_ENTRY(pos, AT_NULL, 0);
285
286	free(imgp->auxargs, M_TEMP);
287	imgp->auxargs = NULL;
288
289	(*stack_base)--;
290	suword(*stack_base, (register_t)imgp->args->argc);
291	return (0);
292}
293
294/*
295 * Copied from kern/kern_exec.c
296 */
297static register_t *
298linux_copyout_strings(struct image_params *imgp)
299{
300	int argc, envc;
301	char **vectp;
302	char *stringp, *destp;
303	register_t *stack_base;
304	struct ps_strings *arginfo;
305	char canary[LINUX_AT_RANDOM_LEN];
306	size_t execpath_len;
307	struct proc *p;
308
309	/*
310	 * Calculate string base and vector table pointers.
311	 */
312	p = imgp->proc;
313	if (imgp->execpath != NULL && imgp->auxargs != NULL)
314		execpath_len = strlen(imgp->execpath) + 1;
315	else
316		execpath_len = 0;
317	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
318	destp = (caddr_t)arginfo - SPARE_USRSPACE - linux_szplatform -
319	    roundup(sizeof(canary), sizeof(char *)) -
320	    roundup(execpath_len, sizeof(char *)) -
321	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
322
323	/*
324	 * install LINUX_PLATFORM
325	 */
326	copyout(linux_kplatform, ((caddr_t)arginfo - linux_szplatform),
327	    linux_szplatform);
328
329	if (execpath_len != 0) {
330		imgp->execpathp = (uintptr_t)arginfo -
331		linux_szplatform - execpath_len;
332		copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
333	}
334
335	/*
336	 * Prepare the canary for SSP.
337	 */
338	arc4rand(canary, sizeof(canary), 0);
339	imgp->canary = (uintptr_t)arginfo - linux_szplatform -
340	    roundup(execpath_len, sizeof(char *)) -
341	    roundup(sizeof(canary), sizeof(char *));
342	copyout(canary, (void *)imgp->canary, sizeof(canary));
343
344	/*
345	 * If we have a valid auxargs ptr, prepare some room
346	 * on the stack.
347	 */
348	if (imgp->auxargs) {
349		/*
350		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
351		 * lower compatibility.
352		 */
353		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
354		    (LINUX_AT_COUNT * 2);
355		/*
356		 * The '+ 2' is for the null pointers at the end of each of
357		 * the arg and env vector sets,and imgp->auxarg_size is room
358		 * for argument of Runtime loader.
359		 */
360		vectp = (char **)(destp - (imgp->args->argc +
361		    imgp->args->envc + 2 + imgp->auxarg_size) * sizeof(char *));
362	} else {
363		/*
364		 * The '+ 2' is for the null pointers at the end of each of
365		 * the arg and env vector sets
366		 */
367		vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
368		    sizeof(char *));
369	}
370
371	/*
372	 * vectp also becomes our initial stack base
373	 */
374	stack_base = (register_t *)vectp;
375
376	stringp = imgp->args->begin_argv;
377	argc = imgp->args->argc;
378	envc = imgp->args->envc;
379
380	/*
381	 * Copy out strings - arguments and environment.
382	 */
383	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
384
385	/*
386	 * Fill in "ps_strings" struct for ps, w, etc.
387	 */
388	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
389	suword(&arginfo->ps_nargvstr, argc);
390
391	/*
392	 * Fill in argument portion of vector table.
393	 */
394	for (; argc > 0; --argc) {
395		suword(vectp++, (long)(intptr_t)destp);
396		while (*stringp++ != 0)
397			destp++;
398		destp++;
399	}
400
401	/* a null vector table pointer separates the argp's from the envp's */
402	suword(vectp++, 0);
403
404	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
405	suword(&arginfo->ps_nenvstr, envc);
406
407	/*
408	 * Fill in environment portion of vector table.
409	 */
410	for (; envc > 0; --envc) {
411		suword(vectp++, (long)(intptr_t)destp);
412		while (*stringp++ != 0)
413			destp++;
414		destp++;
415	}
416
417	/* end of vector table is a null pointer */
418	suword(vectp, 0);
419
420	return (stack_base);
421}
422
423static void
424linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
425{
426	struct thread *td = curthread;
427	struct proc *p = td->td_proc;
428	struct sigacts *psp;
429	struct trapframe *regs;
430	struct l_rt_sigframe *fp, frame;
431	int sig, code;
432	int oonstack;
433
434	sig = ksi->ksi_signo;
435	code = ksi->ksi_code;
436	PROC_LOCK_ASSERT(p, MA_OWNED);
437	psp = p->p_sigacts;
438	mtx_assert(&psp->ps_mtx, MA_OWNED);
439	regs = td->td_frame;
440	oonstack = sigonstack(regs->tf_esp);
441
442#ifdef DEBUG
443	if (ldebug(rt_sendsig))
444		printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
445		    catcher, sig, (void*)mask, code);
446#endif
447	/*
448	 * Allocate space for the signal handler context.
449	 */
450	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
451	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
452		fp = (struct l_rt_sigframe *)(td->td_sigstk.ss_sp +
453		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
454	} else
455		fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
456	mtx_unlock(&psp->ps_mtx);
457
458	/*
459	 * Build the argument list for the signal handler.
460	 */
461	sig = bsd_to_linux_signal(sig);
462
463	bzero(&frame, sizeof(frame));
464
465	frame.sf_handler = catcher;
466	frame.sf_sig = sig;
467	frame.sf_siginfo = &fp->sf_si;
468	frame.sf_ucontext = &fp->sf_sc;
469
470	/* Fill in POSIX parts */
471	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
472
473	/*
474	 * Build the signal context to be used by sigreturn.
475	 */
476	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
477	frame.sf_sc.uc_link = NULL;		/* XXX ??? */
478
479	frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp;
480	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
481	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
482	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
483	PROC_UNLOCK(p);
484
485	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
486
487	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
488	frame.sf_sc.uc_mcontext.sc_gs     = rgs();
489	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
490	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
491	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
492	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_edi;
493	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_esi;
494	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_ebp;
495	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_ebx;
496	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_esp;
497	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_edx;
498	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_ecx;
499	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_eax;
500	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_eip;
501	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
502	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
503	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
504	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
505	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
506	frame.sf_sc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
507	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
508
509#ifdef DEBUG
510	if (ldebug(rt_sendsig))
511		printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
512		    frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
513		    td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
514#endif
515
516	if (copyout(&frame, fp, sizeof(frame)) != 0) {
517		/*
518		 * Process has trashed its stack; give it an illegal
519		 * instruction to halt it in its tracks.
520		 */
521#ifdef DEBUG
522		if (ldebug(rt_sendsig))
523			printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
524			    fp, oonstack);
525#endif
526		PROC_LOCK(p);
527		sigexit(td, SIGILL);
528	}
529
530	/*
531	 * Build context to run handler in.
532	 */
533	regs->tf_esp = (int)fp;
534	regs->tf_eip = linux_rt_sigcode;
535	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
536	regs->tf_cs = _ucodesel;
537	regs->tf_ds = _udatasel;
538	regs->tf_es = _udatasel;
539	regs->tf_fs = _udatasel;
540	regs->tf_ss = _udatasel;
541	PROC_LOCK(p);
542	mtx_lock(&psp->ps_mtx);
543}
544
545
546/*
547 * Send an interrupt to process.
548 *
549 * Stack is set up to allow sigcode stored
550 * in u. to call routine, followed by kcall
551 * to sigreturn routine below.  After sigreturn
552 * resets the signal mask, the stack, and the
553 * frame pointer, it returns to the user
554 * specified pc, psl.
555 */
556static void
557linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
558{
559	struct thread *td = curthread;
560	struct proc *p = td->td_proc;
561	struct sigacts *psp;
562	struct trapframe *regs;
563	struct l_sigframe *fp, frame;
564	l_sigset_t lmask;
565	int sig, code;
566	int oonstack;
567
568	PROC_LOCK_ASSERT(p, MA_OWNED);
569	psp = p->p_sigacts;
570	sig = ksi->ksi_signo;
571	code = ksi->ksi_code;
572	mtx_assert(&psp->ps_mtx, MA_OWNED);
573	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
574		/* Signal handler installed with SA_SIGINFO. */
575		linux_rt_sendsig(catcher, ksi, mask);
576		return;
577	}
578	regs = td->td_frame;
579	oonstack = sigonstack(regs->tf_esp);
580
581#ifdef DEBUG
582	if (ldebug(sendsig))
583		printf(ARGS(sendsig, "%p, %d, %p, %u"),
584		    catcher, sig, (void*)mask, code);
585#endif
586
587	/*
588	 * Allocate space for the signal handler context.
589	 */
590	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
591	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
592		fp = (struct l_sigframe *)(td->td_sigstk.ss_sp +
593		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
594	} else
595		fp = (struct l_sigframe *)regs->tf_esp - 1;
596	mtx_unlock(&psp->ps_mtx);
597	PROC_UNLOCK(p);
598
599	/*
600	 * Build the argument list for the signal handler.
601	 */
602	sig = bsd_to_linux_signal(sig);
603
604	bzero(&frame, sizeof(frame));
605
606	frame.sf_handler = catcher;
607	frame.sf_sig = sig;
608
609	bsd_to_linux_sigset(mask, &lmask);
610
611	/*
612	 * Build the signal context to be used by sigreturn.
613	 */
614	frame.sf_sc.sc_mask   = lmask.__mask;
615	frame.sf_sc.sc_gs     = rgs();
616	frame.sf_sc.sc_fs     = regs->tf_fs;
617	frame.sf_sc.sc_es     = regs->tf_es;
618	frame.sf_sc.sc_ds     = regs->tf_ds;
619	frame.sf_sc.sc_edi    = regs->tf_edi;
620	frame.sf_sc.sc_esi    = regs->tf_esi;
621	frame.sf_sc.sc_ebp    = regs->tf_ebp;
622	frame.sf_sc.sc_ebx    = regs->tf_ebx;
623	frame.sf_sc.sc_esp    = regs->tf_esp;
624	frame.sf_sc.sc_edx    = regs->tf_edx;
625	frame.sf_sc.sc_ecx    = regs->tf_ecx;
626	frame.sf_sc.sc_eax    = regs->tf_eax;
627	frame.sf_sc.sc_eip    = regs->tf_eip;
628	frame.sf_sc.sc_cs     = regs->tf_cs;
629	frame.sf_sc.sc_eflags = regs->tf_eflags;
630	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
631	frame.sf_sc.sc_ss     = regs->tf_ss;
632	frame.sf_sc.sc_err    = regs->tf_err;
633	frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
634	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
635
636	frame.sf_extramask[0] = lmask.__mask;
637
638	if (copyout(&frame, fp, sizeof(frame)) != 0) {
639		/*
640		 * Process has trashed its stack; give it an illegal
641		 * instruction to halt it in its tracks.
642		 */
643		PROC_LOCK(p);
644		sigexit(td, SIGILL);
645	}
646
647	/*
648	 * Build context to run handler in.
649	 */
650	regs->tf_esp = (int)fp;
651	regs->tf_eip = linux_sigcode;
652	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
653	regs->tf_cs = _ucodesel;
654	regs->tf_ds = _udatasel;
655	regs->tf_es = _udatasel;
656	regs->tf_fs = _udatasel;
657	regs->tf_ss = _udatasel;
658	PROC_LOCK(p);
659	mtx_lock(&psp->ps_mtx);
660}
661
662/*
663 * System call to cleanup state after a signal
664 * has been taken.  Reset signal mask and
665 * stack state from context left by sendsig (above).
666 * Return to previous pc and psl as specified by
667 * context left by sendsig. Check carefully to
668 * make sure that the user has not modified the
669 * psl to gain improper privileges or to cause
670 * a machine fault.
671 */
672int
673linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
674{
675	struct l_sigframe frame;
676	struct trapframe *regs;
677	l_sigset_t lmask;
678	sigset_t bmask;
679	int eflags;
680	ksiginfo_t ksi;
681
682	regs = td->td_frame;
683
684#ifdef DEBUG
685	if (ldebug(sigreturn))
686		printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
687#endif
688	/*
689	 * The trampoline code hands us the sigframe.
690	 * It is unsafe to keep track of it ourselves, in the event that a
691	 * program jumps out of a signal handler.
692	 */
693	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
694		return (EFAULT);
695
696	/*
697	 * Check for security violations.
698	 */
699#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
700	eflags = frame.sf_sc.sc_eflags;
701	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
702		return (EINVAL);
703
704	/*
705	 * Don't allow users to load a valid privileged %cs.  Let the
706	 * hardware check for invalid selectors, excess privilege in
707	 * other selectors, invalid %eip's and invalid %esp's.
708	 */
709#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
710	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
711		ksiginfo_init_trap(&ksi);
712		ksi.ksi_signo = SIGBUS;
713		ksi.ksi_code = BUS_OBJERR;
714		ksi.ksi_trapno = T_PROTFLT;
715		ksi.ksi_addr = (void *)regs->tf_eip;
716		trapsignal(td, &ksi);
717		return (EINVAL);
718	}
719
720	lmask.__mask = frame.sf_sc.sc_mask;
721	linux_to_bsd_sigset(&lmask, &bmask);
722	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
723
724	/*
725	 * Restore signal context.
726	 */
727	/* %gs was restored by the trampoline. */
728	regs->tf_fs     = frame.sf_sc.sc_fs;
729	regs->tf_es     = frame.sf_sc.sc_es;
730	regs->tf_ds     = frame.sf_sc.sc_ds;
731	regs->tf_edi    = frame.sf_sc.sc_edi;
732	regs->tf_esi    = frame.sf_sc.sc_esi;
733	regs->tf_ebp    = frame.sf_sc.sc_ebp;
734	regs->tf_ebx    = frame.sf_sc.sc_ebx;
735	regs->tf_edx    = frame.sf_sc.sc_edx;
736	regs->tf_ecx    = frame.sf_sc.sc_ecx;
737	regs->tf_eax    = frame.sf_sc.sc_eax;
738	regs->tf_eip    = frame.sf_sc.sc_eip;
739	regs->tf_cs     = frame.sf_sc.sc_cs;
740	regs->tf_eflags = eflags;
741	regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
742	regs->tf_ss     = frame.sf_sc.sc_ss;
743
744	return (EJUSTRETURN);
745}
746
747/*
748 * System call to cleanup state after a signal
749 * has been taken.  Reset signal mask and
750 * stack state from context left by rt_sendsig (above).
751 * Return to previous pc and psl as specified by
752 * context left by sendsig. Check carefully to
753 * make sure that the user has not modified the
754 * psl to gain improper privileges or to cause
755 * a machine fault.
756 */
757int
758linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
759{
760	struct l_ucontext uc;
761	struct l_sigcontext *context;
762	sigset_t bmask;
763	l_stack_t *lss;
764	stack_t ss;
765	struct trapframe *regs;
766	int eflags;
767	ksiginfo_t ksi;
768
769	regs = td->td_frame;
770
771#ifdef DEBUG
772	if (ldebug(rt_sigreturn))
773		printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
774#endif
775	/*
776	 * The trampoline code hands us the ucontext.
777	 * It is unsafe to keep track of it ourselves, in the event that a
778	 * program jumps out of a signal handler.
779	 */
780	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
781		return (EFAULT);
782
783	context = &uc.uc_mcontext;
784
785	/*
786	 * Check for security violations.
787	 */
788#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
789	eflags = context->sc_eflags;
790	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
791		return (EINVAL);
792
793	/*
794	 * Don't allow users to load a valid privileged %cs.  Let the
795	 * hardware check for invalid selectors, excess privilege in
796	 * other selectors, invalid %eip's and invalid %esp's.
797	 */
798#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
799	if (!CS_SECURE(context->sc_cs)) {
800		ksiginfo_init_trap(&ksi);
801		ksi.ksi_signo = SIGBUS;
802		ksi.ksi_code = BUS_OBJERR;
803		ksi.ksi_trapno = T_PROTFLT;
804		ksi.ksi_addr = (void *)regs->tf_eip;
805		trapsignal(td, &ksi);
806		return (EINVAL);
807	}
808
809	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
810	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
811
812	/*
813	 * Restore signal context
814	 */
815	/* %gs was restored by the trampoline. */
816	regs->tf_fs     = context->sc_fs;
817	regs->tf_es     = context->sc_es;
818	regs->tf_ds     = context->sc_ds;
819	regs->tf_edi    = context->sc_edi;
820	regs->tf_esi    = context->sc_esi;
821	regs->tf_ebp    = context->sc_ebp;
822	regs->tf_ebx    = context->sc_ebx;
823	regs->tf_edx    = context->sc_edx;
824	regs->tf_ecx    = context->sc_ecx;
825	regs->tf_eax    = context->sc_eax;
826	regs->tf_eip    = context->sc_eip;
827	regs->tf_cs     = context->sc_cs;
828	regs->tf_eflags = eflags;
829	regs->tf_esp    = context->sc_esp_at_signal;
830	regs->tf_ss     = context->sc_ss;
831
832	/*
833	 * call sigaltstack & ignore results..
834	 */
835	lss = &uc.uc_stack;
836	ss.ss_sp = lss->ss_sp;
837	ss.ss_size = lss->ss_size;
838	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
839
840#ifdef DEBUG
841	if (ldebug(rt_sigreturn))
842		printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
843		    ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
844#endif
845	(void)kern_sigaltstack(td, &ss, NULL);
846
847	return (EJUSTRETURN);
848}
849
850static int
851linux_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
852{
853	struct proc *p;
854	struct trapframe *frame;
855
856	p = td->td_proc;
857	frame = td->td_frame;
858
859	sa->code = frame->tf_eax;
860	sa->args[0] = frame->tf_ebx;
861	sa->args[1] = frame->tf_ecx;
862	sa->args[2] = frame->tf_edx;
863	sa->args[3] = frame->tf_esi;
864	sa->args[4] = frame->tf_edi;
865	sa->args[5] = frame->tf_ebp;	/* Unconfirmed */
866
867	if (sa->code >= p->p_sysent->sv_size)
868		/* nosys */
869		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
870 	else
871 		sa->callp = &p->p_sysent->sv_table[sa->code];
872	sa->narg = sa->callp->sy_narg;
873
874	td->td_retval[0] = 0;
875	td->td_retval[1] = frame->tf_edx;
876
877	return (0);
878}
879
880/*
881 * If a linux binary is exec'ing something, try this image activator
882 * first.  We override standard shell script execution in order to
883 * be able to modify the interpreter path.  We only do this if a linux
884 * binary is doing the exec, so we do not create an EXEC module for it.
885 */
886static int	exec_linux_imgact_try(struct image_params *iparams);
887
888static int
889exec_linux_imgact_try(struct image_params *imgp)
890{
891    const char *head = (const char *)imgp->image_header;
892    char *rpath;
893    int error = -1;
894
895    /*
896     * The interpreter for shell scripts run from a linux binary needs
897     * to be located in /compat/linux if possible in order to recursively
898     * maintain linux path emulation.
899     */
900    if (((const short *)head)[0] == SHELLMAGIC) {
901	    /*
902	     * Run our normal shell image activator.  If it succeeds attempt
903	     * to use the alternate path for the interpreter.  If an alternate
904	     * path is found, use our stringspace to store it.
905	     */
906	    if ((error = exec_shell_imgact(imgp)) == 0) {
907		    linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
908			imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0, AT_FDCWD);
909		    if (rpath != NULL)
910			    imgp->args->fname_buf =
911				imgp->interpreter_name = rpath;
912	    }
913    }
914    return (error);
915}
916
917/*
918 * exec_setregs may initialize some registers differently than Linux
919 * does, thus potentially confusing Linux binaries. If necessary, we
920 * override the exec_setregs default(s) here.
921 */
922static void
923exec_linux_setregs(struct thread *td, struct image_params *imgp, u_long stack)
924{
925	struct pcb *pcb = td->td_pcb;
926
927	exec_setregs(td, imgp, stack);
928
929	/* Linux sets %gs to 0, we default to _udatasel */
930	pcb->pcb_gs = 0;
931	load_gs(0);
932
933	pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
934}
935
936static void
937linux_get_machine(const char **dst)
938{
939
940	switch (cpu_class) {
941	case CPUCLASS_686:
942		*dst = "i686";
943		break;
944	case CPUCLASS_586:
945		*dst = "i586";
946		break;
947	case CPUCLASS_486:
948		*dst = "i486";
949		break;
950	default:
951		*dst = "i386";
952	}
953}
954
955struct sysentvec linux_sysvec = {
956	.sv_size	= LINUX_SYS_MAXSYSCALL,
957	.sv_table	= linux_sysent,
958	.sv_mask	= 0,
959	.sv_sigsize	= 0,
960	.sv_sigtbl	= NULL,
961	.sv_errsize	= ELAST + 1,
962	.sv_errtbl	= bsd_to_linux_errno,
963	.sv_transtrap	= translate_traps,
964	.sv_fixup	= linux_fixup,
965	.sv_sendsig	= linux_sendsig,
966	.sv_sigcode	= &_binary_linux_locore_o_start,
967	.sv_szsigcode	= &linux_szsigcode,
968	.sv_prepsyscall	= NULL,
969	.sv_name	= "Linux a.out",
970	.sv_coredump	= NULL,
971	.sv_imgact_try	= exec_linux_imgact_try,
972	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
973	.sv_pagesize	= PAGE_SIZE,
974	.sv_minuser	= VM_MIN_ADDRESS,
975	.sv_maxuser	= VM_MAXUSER_ADDRESS,
976	.sv_usrstack	= LINUX_USRSTACK,
977	.sv_psstrings	= PS_STRINGS,
978	.sv_stackprot	= VM_PROT_ALL,
979	.sv_copyout_strings = exec_copyout_strings,
980	.sv_setregs	= exec_linux_setregs,
981	.sv_fixlimit	= NULL,
982	.sv_maxssiz	= NULL,
983	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32,
984	.sv_set_syscall_retval = cpu_set_syscall_retval,
985	.sv_fetch_syscall_args = linux_fetch_syscall_args,
986	.sv_syscallnames = NULL,
987	.sv_shared_page_base = LINUX_SHAREDPAGE,
988	.sv_shared_page_len = PAGE_SIZE,
989	.sv_schedtail	= linux_schedtail,
990	.sv_thread_detach = linux_thread_detach,
991	.sv_trap	= NULL,
992};
993INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
994
995struct sysentvec elf_linux_sysvec = {
996	.sv_size	= LINUX_SYS_MAXSYSCALL,
997	.sv_table	= linux_sysent,
998	.sv_mask	= 0,
999	.sv_sigsize	= 0,
1000	.sv_sigtbl	= NULL,
1001	.sv_errsize	= ELAST + 1,
1002	.sv_errtbl	= bsd_to_linux_errno,
1003	.sv_transtrap	= translate_traps,
1004	.sv_fixup	= elf_linux_fixup,
1005	.sv_sendsig	= linux_sendsig,
1006	.sv_sigcode	= &_binary_linux_locore_o_start,
1007	.sv_szsigcode	= &linux_szsigcode,
1008	.sv_prepsyscall	= NULL,
1009	.sv_name	= "Linux ELF",
1010	.sv_coredump	= elf32_coredump,
1011	.sv_imgact_try	= exec_linux_imgact_try,
1012	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
1013	.sv_pagesize	= PAGE_SIZE,
1014	.sv_minuser	= VM_MIN_ADDRESS,
1015	.sv_maxuser	= VM_MAXUSER_ADDRESS,
1016	.sv_usrstack	= LINUX_USRSTACK,
1017	.sv_psstrings	= LINUX_PS_STRINGS,
1018	.sv_stackprot	= VM_PROT_ALL,
1019	.sv_copyout_strings = linux_copyout_strings,
1020	.sv_setregs	= exec_linux_setregs,
1021	.sv_fixlimit	= NULL,
1022	.sv_maxssiz	= NULL,
1023	.sv_flags	= SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP,
1024	.sv_set_syscall_retval = cpu_set_syscall_retval,
1025	.sv_fetch_syscall_args = linux_fetch_syscall_args,
1026	.sv_syscallnames = NULL,
1027	.sv_shared_page_base = LINUX_SHAREDPAGE,
1028	.sv_shared_page_len = PAGE_SIZE,
1029	.sv_schedtail	= linux_schedtail,
1030	.sv_thread_detach = linux_thread_detach,
1031	.sv_trap	= NULL,
1032};
1033
1034static void
1035linux_vdso_install(void *param)
1036{
1037
1038	linux_szsigcode = (&_binary_linux_locore_o_end -
1039	    &_binary_linux_locore_o_start);
1040
1041	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
1042		panic("Linux invalid vdso size\n");
1043
1044	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
1045
1046	linux_shared_page_obj = __elfN(linux_shared_page_init)
1047	    (&linux_shared_page_mapping);
1048
1049	__elfN(linux_vdso_reloc)(&elf_linux_sysvec, LINUX_SHAREDPAGE);
1050
1051	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
1052	    linux_szsigcode);
1053	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
1054}
1055SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
1056    (sysinit_cfunc_t)linux_vdso_install, NULL);
1057
1058static void
1059linux_vdso_deinstall(void *param)
1060{
1061
1062	__elfN(linux_shared_page_fini)(linux_shared_page_obj);
1063};
1064SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
1065    (sysinit_cfunc_t)linux_vdso_deinstall, NULL);
1066
1067static char GNU_ABI_VENDOR[] = "GNU";
1068static int GNULINUX_ABI_DESC = 0;
1069
1070static boolean_t
1071linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
1072{
1073	const Elf32_Word *desc;
1074	uintptr_t p;
1075
1076	p = (uintptr_t)(note + 1);
1077	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1078
1079	desc = (const Elf32_Word *)p;
1080	if (desc[0] != GNULINUX_ABI_DESC)
1081		return (FALSE);
1082
1083	/*
1084	 * For linux we encode osrel as follows (see linux_mib.c):
1085	 * VVVMMMIII (version, major, minor), see linux_mib.c.
1086	 */
1087	*osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
1088
1089	return (TRUE);
1090}
1091
1092static Elf_Brandnote linux_brandnote = {
1093	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
1094	.hdr.n_descsz	= 16,	/* XXX at least 16 */
1095	.hdr.n_type	= 1,
1096	.vendor		= GNU_ABI_VENDOR,
1097	.flags		= BN_TRANSLATE_OSREL,
1098	.trans_osrel	= linux_trans_osrel
1099};
1100
1101static Elf32_Brandinfo linux_brand = {
1102	.brand		= ELFOSABI_LINUX,
1103	.machine	= EM_386,
1104	.compat_3_brand	= "Linux",
1105	.emul_path	= "/compat/linux",
1106	.interp_path	= "/lib/ld-linux.so.1",
1107	.sysvec		= &elf_linux_sysvec,
1108	.interp_newpath	= NULL,
1109	.brand_note	= &linux_brandnote,
1110	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1111};
1112
1113static Elf32_Brandinfo linux_glibc2brand = {
1114	.brand		= ELFOSABI_LINUX,
1115	.machine	= EM_386,
1116	.compat_3_brand	= "Linux",
1117	.emul_path	= "/compat/linux",
1118	.interp_path	= "/lib/ld-linux.so.2",
1119	.sysvec		= &elf_linux_sysvec,
1120	.interp_newpath	= NULL,
1121	.brand_note	= &linux_brandnote,
1122	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1123};
1124
1125Elf32_Brandinfo *linux_brandlist[] = {
1126	&linux_brand,
1127	&linux_glibc2brand,
1128	NULL
1129};
1130
1131static int
1132linux_elf_modevent(module_t mod, int type, void *data)
1133{
1134	Elf32_Brandinfo **brandinfo;
1135	int error;
1136	struct linux_ioctl_handler **lihp;
1137
1138	error = 0;
1139
1140	switch(type) {
1141	case MOD_LOAD:
1142		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1143		     ++brandinfo)
1144			if (elf32_insert_brand_entry(*brandinfo) < 0)
1145				error = EINVAL;
1146		if (error == 0) {
1147			SET_FOREACH(lihp, linux_ioctl_handler_set)
1148				linux_ioctl_register_handler(*lihp);
1149			LIST_INIT(&futex_list);
1150			mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1151			linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit,
1152			      NULL, 1000);
1153			linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec,
1154			      NULL, 1000);
1155			linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
1156			    linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
1157			linux_get_machine(&linux_kplatform);
1158			linux_szplatform = roundup(strlen(linux_kplatform) + 1,
1159			    sizeof(char *));
1160			linux_osd_jail_register();
1161			stclohz = (stathz ? stathz : hz);
1162			if (bootverbose)
1163				printf("Linux ELF exec handler installed\n");
1164		} else
1165			printf("cannot insert Linux ELF brand handler\n");
1166		break;
1167	case MOD_UNLOAD:
1168		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1169		     ++brandinfo)
1170			if (elf32_brand_inuse(*brandinfo))
1171				error = EBUSY;
1172		if (error == 0) {
1173			for (brandinfo = &linux_brandlist[0];
1174			     *brandinfo != NULL; ++brandinfo)
1175				if (elf32_remove_brand_entry(*brandinfo) < 0)
1176					error = EINVAL;
1177		}
1178		if (error == 0) {
1179			SET_FOREACH(lihp, linux_ioctl_handler_set)
1180				linux_ioctl_unregister_handler(*lihp);
1181			mtx_destroy(&futex_mtx);
1182			EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag);
1183			EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag);
1184			EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag);
1185			linux_osd_jail_deregister();
1186			if (bootverbose)
1187				printf("Linux ELF exec handler removed\n");
1188		} else
1189			printf("Could not deinstall ELF interpreter entry\n");
1190		break;
1191	default:
1192		return (EOPNOTSUPP);
1193	}
1194	return (error);
1195}
1196
1197static moduledata_t linux_elf_mod = {
1198	"linuxelf",
1199	linux_elf_modevent,
1200	0
1201};
1202
1203DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1204