xref: /freebsd/sys/i386/linux/linux_sysvec.c (revision 30c6d982190482857883fc96eefafcc6fd769fa0)
1d66a5066SPeter Wemm /*-
20ba1b365SEd Maste  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
383ef78beSPedro F. Giffuni  *
49a14aa01SUlrich Spörlein  * Copyright (c) 1994-1996 Søren Schmidt
5d66a5066SPeter Wemm  * All rights reserved.
6d66a5066SPeter Wemm  *
7d66a5066SPeter Wemm  * Redistribution and use in source and binary forms, with or without
8d66a5066SPeter Wemm  * modification, are permitted provided that the following conditions
9d66a5066SPeter Wemm  * are met:
10d66a5066SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
110ba1b365SEd Maste  *    notice, this list of conditions and the following disclaimer.
12d66a5066SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
13d66a5066SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
14d66a5066SPeter Wemm  *    documentation and/or other materials provided with the distribution.
15d66a5066SPeter Wemm  *
160ba1b365SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170ba1b365SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180ba1b365SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190ba1b365SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200ba1b365SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210ba1b365SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220ba1b365SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230ba1b365SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240ba1b365SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250ba1b365SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260ba1b365SEd Maste  * SUCH DAMAGE.
27d66a5066SPeter Wemm  */
28d66a5066SPeter Wemm 
2927e0099cSDavid E. O'Brien #include <sys/cdefs.h>
3027e0099cSDavid E. O'Brien __FBSDID("$FreeBSD$");
3127e0099cSDavid E. O'Brien 
32d66a5066SPeter Wemm #include <sys/param.h>
3375f83872SPeter Wemm #include <sys/systm.h>
34ff22c670SBruce Evans #include <sys/exec.h>
3557b4252eSKonstantin Belousov #include <sys/fcntl.h>
36d66a5066SPeter Wemm #include <sys/imgact.h>
3722d4b0fbSJohn Polstra #include <sys/imgact_aout.h>
38e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
39ff22c670SBruce Evans #include <sys/kernel.h>
407106ca0dSJohn Baldwin #include <sys/lock.h>
41e1743d02SSøren Schmidt #include <sys/malloc.h>
42ff22c670SBruce Evans #include <sys/module.h>
4323955314SAlfred Perlstein #include <sys/mutex.h>
44fb919e4dSMark Murray #include <sys/proc.h>
459931033bSDmitry Chagin #include <sys/stddef.h>
46fb919e4dSMark Murray #include <sys/signalvar.h>
47206a5d3aSIan Dowse #include <sys/syscallsubr.h>
4867d39748SDmitry Chagin #include <sys/sysctl.h>
49fb919e4dSMark Murray #include <sys/sysent.h>
50fb919e4dSMark Murray #include <sys/sysproto.h>
51a9148ab1SPeter Wemm #include <sys/vnode.h>
52fb919e4dSMark Murray 
53d66a5066SPeter Wemm #include <vm/vm.h>
54a9148ab1SPeter Wemm #include <vm/pmap.h>
55ff22c670SBruce Evans #include <vm/vm_extern.h>
56a9148ab1SPeter Wemm #include <vm/vm_map.h>
57a9148ab1SPeter Wemm #include <vm/vm_object.h>
58ff22c670SBruce Evans #include <vm/vm_page.h>
59ff22c670SBruce Evans #include <vm/vm_param.h>
60ff22c670SBruce Evans 
61ff22c670SBruce Evans #include <machine/cpu.h>
624d7c2e8aSDmitry Chagin #include <machine/cputypes.h>
63ff22c670SBruce Evans #include <machine/md_var.h>
64d3adf769SDavid Schultz #include <machine/pcb.h>
65d41e41f9SJohn Baldwin #include <machine/trap.h>
66a9148ab1SPeter Wemm 
679931033bSDmitry Chagin #include <x86/linux/linux_x86.h>
68d66a5066SPeter Wemm #include <i386/linux/linux.h>
69ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
7094cb2ecfSAlexander Leidinger #include <compat/linux/linux_emul.h>
71d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
720f9d6538SJohn Baldwin #include <compat/linux/linux_mib.h>
734d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
74b595ab37SAndrew Gallatin #include <compat/linux/linux_signal.h>
75322bfdc3SMarcel Moolenaar #include <compat/linux/linux_util.h>
76bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
77e1743d02SSøren Schmidt 
781d91482dSPeter Wemm MODULE_VERSION(linux, 1);
791d91482dSPeter Wemm 
809931033bSDmitry Chagin #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
819931033bSDmitry Chagin #define	LINUX_VDSOPAGE		(VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
829931033bSDmitry Chagin #define	LINUX_SHAREDPAGE	(LINUX_VDSOPAGE - PAGE_SIZE)
839931033bSDmitry Chagin 				/*
849931033bSDmitry Chagin 				 * PAGE_SIZE - the size
859931033bSDmitry Chagin 				 * of the native SHAREDPAGE
869931033bSDmitry Chagin 				 */
879931033bSDmitry Chagin #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
888f1e49a6SDmitry Chagin #define	LINUX_PS_STRINGS	(LINUX_USRSTACK - sizeof(struct ps_strings))
898f1e49a6SDmitry Chagin 
90bdc37934SDmitry Chagin static int linux_szsigcode;
919931033bSDmitry Chagin static vm_object_t linux_vdso_obj;
929931033bSDmitry Chagin static char *linux_vdso_mapping;
939931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start;
949931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end;
959931033bSDmitry Chagin static vm_offset_t linux_vdso_base;
9643bef515SMarcel Moolenaar 
9743bef515SMarcel Moolenaar extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
9843bef515SMarcel Moolenaar 
99f41325dbSPeter Wemm SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
10043bef515SMarcel Moolenaar 
10131174518SJohn Baldwin static int	linux_fixup(uintptr_t *stack_base,
10289c9a483SAlfred Perlstein 		    struct image_params *iparams);
10331174518SJohn Baldwin static int	linux_fixup_elf(uintptr_t *stack_base,
10489c9a483SAlfred Perlstein 		    struct image_params *iparams);
1059104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
106dc858467SEd Maste static void	linux_exec_setregs(struct thread *td,
10731174518SJohn Baldwin 		    struct image_params *imgp, uintptr_t stack);
1089931033bSDmitry Chagin static void	linux_exec_sysvec_init(void *param);
1095fd9cd53SDmitry Chagin static int	linux_on_exec_vmspace(struct proc *p,
1105fd9cd53SDmitry Chagin 		    struct image_params *imgp);
11103b0d68cSJohn Baldwin static int	linux_copyout_strings(struct image_params *imgp,
11231174518SJohn Baldwin 		    uintptr_t *stack_base);
113a95659f7SEd Maste static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
114a543556cSDmitry Chagin static void	linux_vdso_install(const void *param);
115a543556cSDmitry Chagin static void	linux_vdso_deinstall(const void *param);
1169931033bSDmitry Chagin static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
1174d7c2e8aSDmitry Chagin 
11827a828fcSPierre Beyssac #define LINUX_T_UNKNOWN  255
11927a828fcSPierre Beyssac static int _bsd_to_linux_trapcode[] = {
12027a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 0 */
12127a828fcSPierre Beyssac 	6,			/* 1  T_PRIVINFLT */
12227a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 2 */
12327a828fcSPierre Beyssac 	3,			/* 3  T_BPTFLT */
12427a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 4 */
12527a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 5 */
12627a828fcSPierre Beyssac 	16,			/* 6  T_ARITHTRAP */
12727a828fcSPierre Beyssac 	254,			/* 7  T_ASTFLT */
12827a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 8 */
12927a828fcSPierre Beyssac 	13,			/* 9  T_PROTFLT */
13027a828fcSPierre Beyssac 	1,			/* 10 T_TRCTRAP */
13127a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 11 */
13227a828fcSPierre Beyssac 	14,			/* 12 T_PAGEFLT */
13327a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 13 */
13427a828fcSPierre Beyssac 	17,			/* 14 T_ALIGNFLT */
13527a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 15 */
13627a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 16 */
13727a828fcSPierre Beyssac 	LINUX_T_UNKNOWN,	/* 17 */
13827a828fcSPierre Beyssac 	0,			/* 18 T_DIVIDE */
13927a828fcSPierre Beyssac 	2,			/* 19 T_NMI */
14027a828fcSPierre Beyssac 	4,			/* 20 T_OFLOW */
14127a828fcSPierre Beyssac 	5,			/* 21 T_BOUND */
14227a828fcSPierre Beyssac 	7,			/* 22 T_DNA */
14327a828fcSPierre Beyssac 	8,			/* 23 T_DOUBLEFLT */
14427a828fcSPierre Beyssac 	9,			/* 24 T_FPOPFLT */
14527a828fcSPierre Beyssac 	10,			/* 25 T_TSSFLT */
14627a828fcSPierre Beyssac 	11,			/* 26 T_SEGNPFLT */
14727a828fcSPierre Beyssac 	12,			/* 27 T_STKFLT */
14827a828fcSPierre Beyssac 	18,			/* 28 T_MCHK */
14927a828fcSPierre Beyssac 	19,			/* 29 T_XMMFLT */
15027a828fcSPierre Beyssac 	15			/* 30 T_RESERVED */
15127a828fcSPierre Beyssac };
15227a828fcSPierre Beyssac #define bsd_to_linux_trapcode(code) \
153ea24b056SPedro F. Giffuni     ((code)<nitems(_bsd_to_linux_trapcode)? \
15427a828fcSPierre Beyssac      _bsd_to_linux_trapcode[(code)]: \
15527a828fcSPierre Beyssac      LINUX_T_UNKNOWN)
15627a828fcSPierre Beyssac 
157c1da89feSDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
1589931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
1599931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__kernel_sigreturn);
1609931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__kernel_rt_sigreturn);
1619931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
1629931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
163bdc37934SDmitry Chagin 
164288078beSEivind Eklund /*
165288078beSEivind Eklund  * If FreeBSD & Linux have a difference of opinion about what a trap
166288078beSEivind Eklund  * means, deal with it here.
167356861dbSMatthew Dillon  *
168356861dbSMatthew Dillon  * MPSAFE
169288078beSEivind Eklund  */
170288078beSEivind Eklund static int
171dc858467SEd Maste linux_translate_traps(int signal, int trap_code)
172288078beSEivind Eklund {
173d563a53aSEivind Eklund 	if (signal != SIGBUS)
174af682d48SDmitry Chagin 		return (signal);
175288078beSEivind Eklund 	switch (trap_code) {
176288078beSEivind Eklund 	case T_PROTFLT:
177288078beSEivind Eklund 	case T_TSSFLT:
178288078beSEivind Eklund 	case T_DOUBLEFLT:
179288078beSEivind Eklund 	case T_PAGEFLT:
180af682d48SDmitry Chagin 		return (SIGSEGV);
181288078beSEivind Eklund 	default:
182af682d48SDmitry Chagin 		return (signal);
183288078beSEivind Eklund 	}
184288078beSEivind Eklund }
185288078beSEivind Eklund 
186303b270bSEivind Eklund static int
18731174518SJohn Baldwin linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
188d66a5066SPeter Wemm {
18931174518SJohn Baldwin 	register_t *base, *argv, *envp;
190d66a5066SPeter Wemm 
19131174518SJohn Baldwin 	base = (register_t *)*stack_base;
19231174518SJohn Baldwin 	argv = base;
19331174518SJohn Baldwin 	envp = base + (imgp->args->argc + 1);
19431174518SJohn Baldwin 	base--;
19531174518SJohn Baldwin 	suword(base, (intptr_t)envp);
19631174518SJohn Baldwin 	base--;
19731174518SJohn Baldwin 	suword(base, (intptr_t)argv);
19831174518SJohn Baldwin 	base--;
19931174518SJohn Baldwin 	suword(base, imgp->args->argc);
20031174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
2014d7c2e8aSDmitry Chagin 	return (0);
202d66a5066SPeter Wemm }
203d66a5066SPeter Wemm 
20403b0d68cSJohn Baldwin static int
205d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
206e1743d02SSøren Schmidt {
2074d7c2e8aSDmitry Chagin 	struct proc *p;
20843cf129cSJohn Baldwin 	Elf32_Auxargs *args;
2095f77b8a8SBrooks Davis 	Elf32_Auxinfo *argarray, *pos;
2104d7c2e8aSDmitry Chagin 	struct ps_strings *arginfo;
21103b0d68cSJohn Baldwin 	int error, issetugid;
2124d7c2e8aSDmitry Chagin 
2134d7c2e8aSDmitry Chagin 	p = imgp->proc;
214669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
2154d7c2e8aSDmitry Chagin 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
21643cf129cSJohn Baldwin 	args = (Elf32_Auxargs *)imgp->auxargs;
2175f77b8a8SBrooks Davis 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
2185f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
219e1743d02SSøren Schmidt 
2209931033bSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
2219931033bSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
2224d7c2e8aSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
2238d30f381SDmitry Chagin 
2248d30f381SDmitry Chagin 	/*
2258d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
2268d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
2278d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
2288d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
2298d30f381SDmitry Chagin 	 * is not present.
2308d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
2318d30f381SDmitry Chagin 	 */
2328d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
2331ca16454SDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
234e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
235e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
236e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
237e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
238e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
239e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
240e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
241669414e4SXin LI 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
242b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
243b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
244b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
245b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
246c1da89feSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
247b24e6ac8SBrooks Davis 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
2484048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
249b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
2504d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
2514d7c2e8aSDmitry Chagin 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
252e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
253e1743d02SSøren Schmidt 
254e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
255e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
256cbf7e0cbSBrooks Davis 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
2575f77b8a8SBrooks Davis 
258d8010b11SJohn Baldwin 	error = copyout(argarray, (void *)base,
259d8010b11SJohn Baldwin 	    sizeof(*argarray) * LINUX_AT_COUNT);
2605f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
26103b0d68cSJohn Baldwin 	return (error);
2625caa67faSJohn Baldwin }
2635caa67faSJohn Baldwin 
2645caa67faSJohn Baldwin static int
26531174518SJohn Baldwin linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
2665caa67faSJohn Baldwin {
26731174518SJohn Baldwin 	register_t *base;
268e1743d02SSøren Schmidt 
26931174518SJohn Baldwin 	base = (register_t *)*stack_base;
27031174518SJohn Baldwin 	base--;
27131174518SJohn Baldwin 	if (suword(base, (register_t)imgp->args->argc) == -1)
2725f77b8a8SBrooks Davis 		return (EFAULT);
27331174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
2744d7c2e8aSDmitry Chagin 	return (0);
275e1743d02SSøren Schmidt }
276d66a5066SPeter Wemm 
2774d7c2e8aSDmitry Chagin /*
2784d7c2e8aSDmitry Chagin  * Copied from kern/kern_exec.c
2794d7c2e8aSDmitry Chagin  */
28003b0d68cSJohn Baldwin static int
28131174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
2824d7c2e8aSDmitry Chagin {
28303b0d68cSJohn Baldwin 	int argc, envc, error;
2844d7c2e8aSDmitry Chagin 	char **vectp;
28531174518SJohn Baldwin 	char *stringp;
28631174518SJohn Baldwin 	uintptr_t destp, ustringp;
2874d7c2e8aSDmitry Chagin 	struct ps_strings *arginfo;
2884048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
2894048f59cSDmitry Chagin 	size_t execpath_len;
2904d7c2e8aSDmitry Chagin 	struct proc *p;
2914d7c2e8aSDmitry Chagin 
2924ba25759SEd Maste 	/* Calculate string base and vector table pointers. */
2934d7c2e8aSDmitry Chagin 	p = imgp->proc;
2944048f59cSDmitry Chagin 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2954048f59cSDmitry Chagin 		execpath_len = strlen(imgp->execpath) + 1;
2964048f59cSDmitry Chagin 	else
2974048f59cSDmitry Chagin 		execpath_len = 0;
2984d7c2e8aSDmitry Chagin 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
29931174518SJohn Baldwin 	destp = (uintptr_t)arginfo;
3004d7c2e8aSDmitry Chagin 
3014048f59cSDmitry Chagin 	if (execpath_len != 0) {
30231174518SJohn Baldwin 		destp -= execpath_len;
30331174518SJohn Baldwin 		destp = rounddown2(destp, sizeof(void *));
304b24e6ac8SBrooks Davis 		imgp->execpathp = (void *)destp;
305b24e6ac8SBrooks Davis 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
30603b0d68cSJohn Baldwin 		if (error != 0)
30703b0d68cSJohn Baldwin 			return (error);
3084048f59cSDmitry Chagin 	}
3094048f59cSDmitry Chagin 
3104ba25759SEd Maste 	/* Prepare the canary for SSP. */
3114048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
31231174518SJohn Baldwin 	destp -= roundup(sizeof(canary), sizeof(void *));
313b24e6ac8SBrooks Davis 	imgp->canary = (void *)destp;
314b24e6ac8SBrooks Davis 	error = copyout(canary, imgp->canary, sizeof(canary));
31503b0d68cSJohn Baldwin 	if (error != 0)
31603b0d68cSJohn Baldwin 		return (error);
3174048f59cSDmitry Chagin 
31831174518SJohn Baldwin 	/* Allocate room for the argument and environment strings. */
31931174518SJohn Baldwin 	destp -= ARG_MAX - imgp->args->stringspace;
32031174518SJohn Baldwin 	destp = rounddown2(destp, sizeof(void *));
32131174518SJohn Baldwin 	ustringp = destp;
32231174518SJohn Baldwin 
32303b0d68cSJohn Baldwin 	if (imgp->auxargs) {
324d8010b11SJohn Baldwin 		/*
325d8010b11SJohn Baldwin 		 * Allocate room on the stack for the ELF auxargs
326d8010b11SJohn Baldwin 		 * array.  It has LINUX_AT_COUNT entries.
327d8010b11SJohn Baldwin 		 */
328d8010b11SJohn Baldwin 		destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
329d8010b11SJohn Baldwin 		destp = rounddown2(destp, sizeof(void *));
33003b0d68cSJohn Baldwin 	}
3314d7c2e8aSDmitry Chagin 
33231174518SJohn Baldwin 	vectp = (char **)destp;
33331174518SJohn Baldwin 
33473c8686eSJohn Baldwin 	/*
33573c8686eSJohn Baldwin 	 * Allocate room for the argv[] and env vectors including the
33673c8686eSJohn Baldwin 	 * terminating NULL pointers.
33773c8686eSJohn Baldwin 	 */
33873c8686eSJohn Baldwin 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
33973c8686eSJohn Baldwin 
3404ba25759SEd Maste 	/* vectp also becomes our initial stack base. */
34131174518SJohn Baldwin 	*stack_base = (uintptr_t)vectp;
3424d7c2e8aSDmitry Chagin 
3434d7c2e8aSDmitry Chagin 	stringp = imgp->args->begin_argv;
3444d7c2e8aSDmitry Chagin 	argc = imgp->args->argc;
3454d7c2e8aSDmitry Chagin 	envc = imgp->args->envc;
3464d7c2e8aSDmitry Chagin 
3474ba25759SEd Maste 	/* Copy out strings - arguments and environment. */
34831174518SJohn Baldwin 	error = copyout(stringp, (void *)ustringp,
34931174518SJohn Baldwin 	    ARG_MAX - imgp->args->stringspace);
35003b0d68cSJohn Baldwin 	if (error != 0)
35103b0d68cSJohn Baldwin 		return (error);
3524d7c2e8aSDmitry Chagin 
3534ba25759SEd Maste 	/* Fill in "ps_strings" struct for ps, w, etc. */
35403b0d68cSJohn Baldwin 	if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
35503b0d68cSJohn Baldwin 	    suword(&arginfo->ps_nargvstr, argc) != 0)
35603b0d68cSJohn Baldwin 		return (EFAULT);
3574d7c2e8aSDmitry Chagin 
3584ba25759SEd Maste 	/* Fill in argument portion of vector table. */
3594d7c2e8aSDmitry Chagin 	for (; argc > 0; --argc) {
36031174518SJohn Baldwin 		if (suword(vectp++, ustringp) != 0)
36103b0d68cSJohn Baldwin 			return (EFAULT);
3624d7c2e8aSDmitry Chagin 		while (*stringp++ != 0)
36331174518SJohn Baldwin 			ustringp++;
36431174518SJohn Baldwin 		ustringp++;
3654d7c2e8aSDmitry Chagin 	}
3664d7c2e8aSDmitry Chagin 
3674ba25759SEd Maste 	/* A null vector table pointer separates the argp's from the envp's. */
36803b0d68cSJohn Baldwin 	if (suword(vectp++, 0) != 0)
36903b0d68cSJohn Baldwin 		return (EFAULT);
3704d7c2e8aSDmitry Chagin 
37103b0d68cSJohn Baldwin 	if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
37203b0d68cSJohn Baldwin 	    suword(&arginfo->ps_nenvstr, envc) != 0)
37303b0d68cSJohn Baldwin 		return (EFAULT);
3744d7c2e8aSDmitry Chagin 
3754ba25759SEd Maste 	/* Fill in environment portion of vector table. */
3764d7c2e8aSDmitry Chagin 	for (; envc > 0; --envc) {
37731174518SJohn Baldwin 		if (suword(vectp++, ustringp) != 0)
37803b0d68cSJohn Baldwin 			return (EFAULT);
3794d7c2e8aSDmitry Chagin 		while (*stringp++ != 0)
38031174518SJohn Baldwin 			ustringp++;
38131174518SJohn Baldwin 		ustringp++;
3824d7c2e8aSDmitry Chagin 	}
3834d7c2e8aSDmitry Chagin 
3844ba25759SEd Maste 	/* The end of the vector table is a null pointer. */
38503b0d68cSJohn Baldwin 	if (suword(vectp, 0) != 0)
38603b0d68cSJohn Baldwin 		return (EFAULT);
3874d7c2e8aSDmitry Chagin 
388d8010b11SJohn Baldwin 	if (imgp->auxargs) {
389d8010b11SJohn Baldwin 		vectp++;
390d8010b11SJohn Baldwin 		error = imgp->sysent->sv_copyout_auxargs(imgp,
391d8010b11SJohn Baldwin 		    (uintptr_t)vectp);
392d8010b11SJohn Baldwin 		if (error != 0)
393d8010b11SJohn Baldwin 			return (error);
394d8010b11SJohn Baldwin 	}
395d8010b11SJohn Baldwin 
39603b0d68cSJohn Baldwin 	return (0);
3974d7c2e8aSDmitry Chagin }
3984d7c2e8aSDmitry Chagin 
39979363394SAndrew Gallatin static void
4009104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
40179363394SAndrew Gallatin {
4021d062e2bSDag-Erling Smørgrav 	struct thread *td = curthread;
4031d062e2bSDag-Erling Smørgrav 	struct proc *p = td->td_proc;
40490af4afaSJohn Baldwin 	struct sigacts *psp;
4051d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
4065002a60fSMarcel Moolenaar 	struct l_rt_sigframe *fp, frame;
4079104847fSDavid Xu 	int sig, code;
40879363394SAndrew Gallatin 	int oonstack;
40979363394SAndrew Gallatin 
4109104847fSDavid Xu 	sig = ksi->ksi_signo;
4119104847fSDavid Xu 	code = ksi->ksi_code;
412df53e91cSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
41390af4afaSJohn Baldwin 	psp = p->p_sigacts;
41490af4afaSJohn Baldwin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
415b40ce416SJulian Elischer 	regs = td->td_frame;
416d034d459SMarcel Moolenaar 	oonstack = sigonstack(regs->tf_esp);
41779363394SAndrew Gallatin 
4184ba25759SEd Maste 	/* Allocate space for the signal handler context. */
419a30ec4b9SDavid Xu 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
42090af4afaSJohn Baldwin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
421aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
422a30ec4b9SDavid Xu 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
423d034d459SMarcel Moolenaar 	} else
4245002a60fSMarcel Moolenaar 		fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
42590af4afaSJohn Baldwin 	mtx_unlock(&psp->ps_mtx);
42679363394SAndrew Gallatin 
4274ba25759SEd Maste 	/* Build the argument list for the signal handler. */
4284ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
42979363394SAndrew Gallatin 
43099d45c5fSMarcel Moolenaar 	bzero(&frame, sizeof(frame));
43199d45c5fSMarcel Moolenaar 
43279363394SAndrew Gallatin 	frame.sf_handler = catcher;
43379363394SAndrew Gallatin 	frame.sf_sig = sig;
43479363394SAndrew Gallatin 	frame.sf_siginfo = &fp->sf_si;
43579363394SAndrew Gallatin 	frame.sf_ucontext = &fp->sf_sc;
436cc6ca9b3SMarcel Moolenaar 
4374ba25759SEd Maste 	/* Fill in POSIX parts. */
438f4e80108SDmitry Chagin 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
439cc6ca9b3SMarcel Moolenaar 
4404ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
441cc6ca9b3SMarcel Moolenaar 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
442cc6ca9b3SMarcel Moolenaar 	frame.sf_sc.uc_link = NULL;		/* XXX ??? */
443cc6ca9b3SMarcel Moolenaar 
444*30c6d982SEdward Tomasz Napierala 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
445a30ec4b9SDavid Xu 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
446a30ec4b9SDavid Xu 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
447d034d459SMarcel Moolenaar 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
448611d9407SJohn Baldwin 	PROC_UNLOCK(p);
449cc6ca9b3SMarcel Moolenaar 
450cc6ca9b3SMarcel Moolenaar 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
451cc6ca9b3SMarcel Moolenaar 
4524ab7403bSDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
45379363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_gs     = rgs();
45479363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
45579363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
45679363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
45779363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_edi;
45879363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_esi;
45979363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_ebp;
46079363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_ebx;
461bdc37934SDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_esp;
46279363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_edx;
46379363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_ecx;
46479363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_eax;
46579363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_eip;
46679363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
46779363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
46879363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
46979363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
47079363394SAndrew Gallatin 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
47196a2b635SKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
47227a828fcSPierre Beyssac 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
47379363394SAndrew Gallatin 
47479363394SAndrew Gallatin 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
47579363394SAndrew Gallatin 		/*
47679363394SAndrew Gallatin 		 * Process has trashed its stack; give it an illegal
47779363394SAndrew Gallatin 		 * instruction to halt it in its tracks.
47879363394SAndrew Gallatin 		 */
47919eb87d2SJohn Baldwin 		PROC_LOCK(p);
480b40ce416SJulian Elischer 		sigexit(td, SIGILL);
48179363394SAndrew Gallatin 	}
48279363394SAndrew Gallatin 
4834ba25759SEd Maste 	/* Build context to run handler in. */
48479363394SAndrew Gallatin 	regs->tf_esp = (int)fp;
4859931033bSDmitry Chagin 	regs->tf_eip = __kernel_rt_sigreturn;
48622eca0bfSKonstantin Belousov 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
48779363394SAndrew Gallatin 	regs->tf_cs = _ucodesel;
48879363394SAndrew Gallatin 	regs->tf_ds = _udatasel;
48979363394SAndrew Gallatin 	regs->tf_es = _udatasel;
49079363394SAndrew Gallatin 	regs->tf_fs = _udatasel;
49179363394SAndrew Gallatin 	regs->tf_ss = _udatasel;
492df53e91cSJohn Baldwin 	PROC_LOCK(p);
49390af4afaSJohn Baldwin 	mtx_lock(&psp->ps_mtx);
49479363394SAndrew Gallatin }
49579363394SAndrew Gallatin 
496d66a5066SPeter Wemm /*
497d66a5066SPeter Wemm  * Send an interrupt to process.
498d66a5066SPeter Wemm  *
499d66a5066SPeter Wemm  * Stack is set up to allow sigcode stored
500d66a5066SPeter Wemm  * in u. to call routine, followed by kcall
501d66a5066SPeter Wemm  * to sigreturn routine below.  After sigreturn
502d66a5066SPeter Wemm  * resets the signal mask, the stack, and the
503d66a5066SPeter Wemm  * frame pointer, it returns to the user
504d66a5066SPeter Wemm  * specified pc, psl.
505d66a5066SPeter Wemm  */
506303b270bSEivind Eklund static void
5079104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
508d66a5066SPeter Wemm {
5091d062e2bSDag-Erling Smørgrav 	struct thread *td = curthread;
5101d062e2bSDag-Erling Smørgrav 	struct proc *p = td->td_proc;
51190af4afaSJohn Baldwin 	struct sigacts *psp;
5121d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
5135002a60fSMarcel Moolenaar 	struct l_sigframe *fp, frame;
5145002a60fSMarcel Moolenaar 	l_sigset_t lmask;
5159104847fSDavid Xu 	int sig, code;
5164ab7403bSDmitry Chagin 	int oonstack;
517d66a5066SPeter Wemm 
5182509e6c2SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
51990af4afaSJohn Baldwin 	psp = p->p_sigacts;
5209104847fSDavid Xu 	sig = ksi->ksi_signo;
5219104847fSDavid Xu 	code = ksi->ksi_code;
52290af4afaSJohn Baldwin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
52390af4afaSJohn Baldwin 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
524cc6ca9b3SMarcel Moolenaar 		/* Signal handler installed with SA_SIGINFO. */
5259104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
526cc6ca9b3SMarcel Moolenaar 		return;
527cc6ca9b3SMarcel Moolenaar 	}
528b40ce416SJulian Elischer 	regs = td->td_frame;
529d034d459SMarcel Moolenaar 	oonstack = sigonstack(regs->tf_esp);
530d66a5066SPeter Wemm 
5314ba25759SEd Maste 	/* Allocate space for the signal handler context. */
532a30ec4b9SDavid Xu 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
53390af4afaSJohn Baldwin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
534aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
535a30ec4b9SDavid Xu 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
536d034d459SMarcel Moolenaar 	} else
5375002a60fSMarcel Moolenaar 		fp = (struct l_sigframe *)regs->tf_esp - 1;
53890af4afaSJohn Baldwin 	mtx_unlock(&psp->ps_mtx);
539611d9407SJohn Baldwin 	PROC_UNLOCK(p);
540d66a5066SPeter Wemm 
5414ba25759SEd Maste 	/* Build the argument list for the signal handler. */
5424ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
543d66a5066SPeter Wemm 
54499d45c5fSMarcel Moolenaar 	bzero(&frame, sizeof(frame));
54599d45c5fSMarcel Moolenaar 
546d66a5066SPeter Wemm 	frame.sf_handler = catcher;
547d66a5066SPeter Wemm 	frame.sf_sig = sig;
548d66a5066SPeter Wemm 
549cc6ca9b3SMarcel Moolenaar 	bsd_to_linux_sigset(mask, &lmask);
550cc6ca9b3SMarcel Moolenaar 
5514ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
5524ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
5535206bca1SLuoqi Chen 	frame.sf_sc.sc_gs     = rgs();
5545206bca1SLuoqi Chen 	frame.sf_sc.sc_fs     = regs->tf_fs;
555213fdd80SPeter Wemm 	frame.sf_sc.sc_es     = regs->tf_es;
556213fdd80SPeter Wemm 	frame.sf_sc.sc_ds     = regs->tf_ds;
557213fdd80SPeter Wemm 	frame.sf_sc.sc_edi    = regs->tf_edi;
558213fdd80SPeter Wemm 	frame.sf_sc.sc_esi    = regs->tf_esi;
559213fdd80SPeter Wemm 	frame.sf_sc.sc_ebp    = regs->tf_ebp;
560213fdd80SPeter Wemm 	frame.sf_sc.sc_ebx    = regs->tf_ebx;
561bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_esp;
562213fdd80SPeter Wemm 	frame.sf_sc.sc_edx    = regs->tf_edx;
563213fdd80SPeter Wemm 	frame.sf_sc.sc_ecx    = regs->tf_ecx;
564213fdd80SPeter Wemm 	frame.sf_sc.sc_eax    = regs->tf_eax;
565213fdd80SPeter Wemm 	frame.sf_sc.sc_eip    = regs->tf_eip;
566213fdd80SPeter Wemm 	frame.sf_sc.sc_cs     = regs->tf_cs;
567213fdd80SPeter Wemm 	frame.sf_sc.sc_eflags = regs->tf_eflags;
568213fdd80SPeter Wemm 	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
569213fdd80SPeter Wemm 	frame.sf_sc.sc_ss     = regs->tf_ss;
570213fdd80SPeter Wemm 	frame.sf_sc.sc_err    = regs->tf_err;
57196a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
5729104847fSDavid Xu 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
573cc6ca9b3SMarcel Moolenaar 
5744ab7403bSDmitry Chagin 	frame.sf_extramask[0] = lmask.__mask;
575d66a5066SPeter Wemm 
576d66a5066SPeter Wemm 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
577d66a5066SPeter Wemm 		/*
578d66a5066SPeter Wemm 		 * Process has trashed its stack; give it an illegal
579d66a5066SPeter Wemm 		 * instruction to halt it in its tracks.
580d66a5066SPeter Wemm 		 */
58119eb87d2SJohn Baldwin 		PROC_LOCK(p);
582b40ce416SJulian Elischer 		sigexit(td, SIGILL);
583d66a5066SPeter Wemm 	}
584d66a5066SPeter Wemm 
5854ba25759SEd Maste 	/* Build context to run handler in. */
586213fdd80SPeter Wemm 	regs->tf_esp = (int)fp;
5879931033bSDmitry Chagin 	regs->tf_eip = __kernel_sigreturn;
58822eca0bfSKonstantin Belousov 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
589213fdd80SPeter Wemm 	regs->tf_cs = _ucodesel;
590213fdd80SPeter Wemm 	regs->tf_ds = _udatasel;
591213fdd80SPeter Wemm 	regs->tf_es = _udatasel;
5925206bca1SLuoqi Chen 	regs->tf_fs = _udatasel;
593213fdd80SPeter Wemm 	regs->tf_ss = _udatasel;
5945002a60fSMarcel Moolenaar 	PROC_LOCK(p);
59590af4afaSJohn Baldwin 	mtx_lock(&psp->ps_mtx);
596d66a5066SPeter Wemm }
597d66a5066SPeter Wemm 
598d66a5066SPeter Wemm /*
599d66a5066SPeter Wemm  * System call to cleanup state after a signal
600d66a5066SPeter Wemm  * has been taken.  Reset signal mask and
601d66a5066SPeter Wemm  * stack state from context left by sendsig (above).
602d66a5066SPeter Wemm  * Return to previous pc and psl as specified by
603d66a5066SPeter Wemm  * context left by sendsig. Check carefully to
604d66a5066SPeter Wemm  * make sure that the user has not modified the
605d66a5066SPeter Wemm  * psl to gain improper privileges or to cause
606d66a5066SPeter Wemm  * a machine fault.
607d66a5066SPeter Wemm  */
608d66a5066SPeter Wemm int
609b07cd97eSMark Murray linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
610d66a5066SPeter Wemm {
6115002a60fSMarcel Moolenaar 	struct l_sigframe frame;
6121d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
6135002a60fSMarcel Moolenaar 	l_sigset_t lmask;
614d6e029adSKonstantin Belousov 	sigset_t bmask;
6154ab7403bSDmitry Chagin 	int eflags;
6169104847fSDavid Xu 	ksiginfo_t ksi;
617d66a5066SPeter Wemm 
618b40ce416SJulian Elischer 	regs = td->td_frame;
619d66a5066SPeter Wemm 
620d66a5066SPeter Wemm 	/*
621cc6ca9b3SMarcel Moolenaar 	 * The trampoline code hands us the sigframe.
622d66a5066SPeter Wemm 	 * It is unsafe to keep track of it ourselves, in the event that a
623d66a5066SPeter Wemm 	 * program jumps out of a signal handler.
624d66a5066SPeter Wemm 	 */
6254b7ef73dSDag-Erling Smørgrav 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
626d66a5066SPeter Wemm 		return (EFAULT);
627d66a5066SPeter Wemm 
6284ba25759SEd Maste 	/* Check for security violations. */
629d66a5066SPeter Wemm #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
630cc6ca9b3SMarcel Moolenaar 	eflags = frame.sf_sc.sc_eflags;
6313d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
632d66a5066SPeter Wemm 		return (EINVAL);
633d66a5066SPeter Wemm 
634d66a5066SPeter Wemm 	/*
635d66a5066SPeter Wemm 	 * Don't allow users to load a valid privileged %cs.  Let the
636d66a5066SPeter Wemm 	 * hardware check for invalid selectors, excess privilege in
637d66a5066SPeter Wemm 	 * other selectors, invalid %eip's and invalid %esp's.
638d66a5066SPeter Wemm 	 */
63940d50994SPhilippe Charnier #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
640cc6ca9b3SMarcel Moolenaar 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
6419104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
6429104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
6439104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
6449104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
6459104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_eip;
6469104847fSDavid Xu 		trapsignal(td, &ksi);
647d66a5066SPeter Wemm 		return (EINVAL);
648d66a5066SPeter Wemm 	}
649d66a5066SPeter Wemm 
6504ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_sc.sc_mask;
651d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&lmask, &bmask);
652d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
653956d3333SMarcel Moolenaar 
6544ba25759SEd Maste 	/* Restore signal context. */
6555206bca1SLuoqi Chen 	/* %gs was restored by the trampoline. */
656cc6ca9b3SMarcel Moolenaar 	regs->tf_fs     = frame.sf_sc.sc_fs;
657cc6ca9b3SMarcel Moolenaar 	regs->tf_es     = frame.sf_sc.sc_es;
658cc6ca9b3SMarcel Moolenaar 	regs->tf_ds     = frame.sf_sc.sc_ds;
659cc6ca9b3SMarcel Moolenaar 	regs->tf_edi    = frame.sf_sc.sc_edi;
660cc6ca9b3SMarcel Moolenaar 	regs->tf_esi    = frame.sf_sc.sc_esi;
661cc6ca9b3SMarcel Moolenaar 	regs->tf_ebp    = frame.sf_sc.sc_ebp;
662cc6ca9b3SMarcel Moolenaar 	regs->tf_ebx    = frame.sf_sc.sc_ebx;
663cc6ca9b3SMarcel Moolenaar 	regs->tf_edx    = frame.sf_sc.sc_edx;
664cc6ca9b3SMarcel Moolenaar 	regs->tf_ecx    = frame.sf_sc.sc_ecx;
665cc6ca9b3SMarcel Moolenaar 	regs->tf_eax    = frame.sf_sc.sc_eax;
666cc6ca9b3SMarcel Moolenaar 	regs->tf_eip    = frame.sf_sc.sc_eip;
667cc6ca9b3SMarcel Moolenaar 	regs->tf_cs     = frame.sf_sc.sc_cs;
668213fdd80SPeter Wemm 	regs->tf_eflags = eflags;
669cc6ca9b3SMarcel Moolenaar 	regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
670cc6ca9b3SMarcel Moolenaar 	regs->tf_ss     = frame.sf_sc.sc_ss;
671d66a5066SPeter Wemm 
672d66a5066SPeter Wemm 	return (EJUSTRETURN);
673d66a5066SPeter Wemm }
674d66a5066SPeter Wemm 
67579363394SAndrew Gallatin /*
67679363394SAndrew Gallatin  * System call to cleanup state after a signal
67779363394SAndrew Gallatin  * has been taken.  Reset signal mask and
67879363394SAndrew Gallatin  * stack state from context left by rt_sendsig (above).
67979363394SAndrew Gallatin  * Return to previous pc and psl as specified by
68079363394SAndrew Gallatin  * context left by sendsig. Check carefully to
68179363394SAndrew Gallatin  * make sure that the user has not modified the
68279363394SAndrew Gallatin  * psl to gain improper privileges or to cause
68379363394SAndrew Gallatin  * a machine fault.
68479363394SAndrew Gallatin  */
68579363394SAndrew Gallatin int
686b07cd97eSMark Murray linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
68779363394SAndrew Gallatin {
6885002a60fSMarcel Moolenaar 	struct l_ucontext uc;
6895002a60fSMarcel Moolenaar 	struct l_sigcontext *context;
690d6e029adSKonstantin Belousov 	sigset_t bmask;
6915002a60fSMarcel Moolenaar 	l_stack_t *lss;
692206a5d3aSIan Dowse 	stack_t ss;
6931d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
69479363394SAndrew Gallatin 	int eflags;
6959104847fSDavid Xu 	ksiginfo_t ksi;
69679363394SAndrew Gallatin 
697b40ce416SJulian Elischer 	regs = td->td_frame;
69879363394SAndrew Gallatin 
69979363394SAndrew Gallatin 	/*
700cc6ca9b3SMarcel Moolenaar 	 * The trampoline code hands us the ucontext.
70179363394SAndrew Gallatin 	 * It is unsafe to keep track of it ourselves, in the event that a
70279363394SAndrew Gallatin 	 * program jumps out of a signal handler.
70379363394SAndrew Gallatin 	 */
7044b7ef73dSDag-Erling Smørgrav 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
70579363394SAndrew Gallatin 		return (EFAULT);
70679363394SAndrew Gallatin 
70779363394SAndrew Gallatin 	context = &uc.uc_mcontext;
70879363394SAndrew Gallatin 
7094ba25759SEd Maste 	/* Check for security violations. */
71079363394SAndrew Gallatin #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
71179363394SAndrew Gallatin 	eflags = context->sc_eflags;
7123d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
71379363394SAndrew Gallatin 		return (EINVAL);
71479363394SAndrew Gallatin 
71579363394SAndrew Gallatin 	/*
71679363394SAndrew Gallatin 	 * Don't allow users to load a valid privileged %cs.  Let the
71779363394SAndrew Gallatin 	 * hardware check for invalid selectors, excess privilege in
71879363394SAndrew Gallatin 	 * other selectors, invalid %eip's and invalid %esp's.
71979363394SAndrew Gallatin 	 */
72079363394SAndrew Gallatin #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
72179363394SAndrew Gallatin 	if (!CS_SECURE(context->sc_cs)) {
7229104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
7239104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
7249104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
7259104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
7269104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_eip;
7279104847fSDavid Xu 		trapsignal(td, &ksi);
72879363394SAndrew Gallatin 		return (EINVAL);
72979363394SAndrew Gallatin 	}
73079363394SAndrew Gallatin 
731d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
732d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
73379363394SAndrew Gallatin 
7344ba25759SEd Maste 	/* Restore signal context. */
73579363394SAndrew Gallatin 	/* %gs was restored by the trampoline. */
73679363394SAndrew Gallatin 	regs->tf_fs     = context->sc_fs;
73779363394SAndrew Gallatin 	regs->tf_es     = context->sc_es;
73879363394SAndrew Gallatin 	regs->tf_ds     = context->sc_ds;
73979363394SAndrew Gallatin 	regs->tf_edi    = context->sc_edi;
74079363394SAndrew Gallatin 	regs->tf_esi    = context->sc_esi;
74179363394SAndrew Gallatin 	regs->tf_ebp    = context->sc_ebp;
74279363394SAndrew Gallatin 	regs->tf_ebx    = context->sc_ebx;
74379363394SAndrew Gallatin 	regs->tf_edx    = context->sc_edx;
74479363394SAndrew Gallatin 	regs->tf_ecx    = context->sc_ecx;
74579363394SAndrew Gallatin 	regs->tf_eax    = context->sc_eax;
74679363394SAndrew Gallatin 	regs->tf_eip    = context->sc_eip;
74779363394SAndrew Gallatin 	regs->tf_cs     = context->sc_cs;
74879363394SAndrew Gallatin 	regs->tf_eflags = eflags;
74979363394SAndrew Gallatin 	regs->tf_esp    = context->sc_esp_at_signal;
75079363394SAndrew Gallatin 	regs->tf_ss     = context->sc_ss;
75179363394SAndrew Gallatin 
7524ba25759SEd Maste 	/* Call sigaltstack & ignore results. */
75379363394SAndrew Gallatin 	lss = &uc.uc_stack;
754*30c6d982SEdward Tomasz Napierala 	ss.ss_sp = PTRIN(lss->ss_sp);
755206a5d3aSIan Dowse 	ss.ss_size = lss->ss_size;
756206a5d3aSIan Dowse 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
75779363394SAndrew Gallatin 
758206a5d3aSIan Dowse 	(void)kern_sigaltstack(td, &ss, NULL);
75979363394SAndrew Gallatin 
76079363394SAndrew Gallatin 	return (EJUSTRETURN);
76179363394SAndrew Gallatin }
76279363394SAndrew Gallatin 
763afe1a688SKonstantin Belousov static int
7642d88da2fSKonstantin Belousov linux_fetch_syscall_args(struct thread *td)
765d66a5066SPeter Wemm {
766afe1a688SKonstantin Belousov 	struct proc *p;
767afe1a688SKonstantin Belousov 	struct trapframe *frame;
7682d88da2fSKonstantin Belousov 	struct syscall_args *sa;
769afe1a688SKonstantin Belousov 
770afe1a688SKonstantin Belousov 	p = td->td_proc;
771afe1a688SKonstantin Belousov 	frame = td->td_frame;
7722d88da2fSKonstantin Belousov 	sa = &td->td_sa;
773afe1a688SKonstantin Belousov 
774afe1a688SKonstantin Belousov 	sa->code = frame->tf_eax;
775cf98bc28SDavid Chisnall 	sa->original_code = sa->code;
776afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_ebx;
777afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_ecx;
778afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_edx;
779afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_esi;
780afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_edi;
781afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_ebp;	/* Unconfirmed */
782afe1a688SKonstantin Belousov 
783afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
784fcdffc03SDmitry Chagin 		/* nosys */
7851b8d0393SConrad Meyer 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
786afe1a688SKonstantin Belousov 	else
787afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
788afe1a688SKonstantin Belousov 
789afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
790afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_edx;
791afe1a688SKonstantin Belousov 
792afe1a688SKonstantin Belousov 	return (0);
793d66a5066SPeter Wemm }
794d66a5066SPeter Wemm 
795c26391f4SEdward Tomasz Napierala static void
796c26391f4SEdward Tomasz Napierala linux_set_syscall_retval(struct thread *td, int error)
797c26391f4SEdward Tomasz Napierala {
798c26391f4SEdward Tomasz Napierala 	struct trapframe *frame = td->td_frame;
799c26391f4SEdward Tomasz Napierala 
800c26391f4SEdward Tomasz Napierala 	cpu_set_syscall_retval(td, error);
801c26391f4SEdward Tomasz Napierala 
802c26391f4SEdward Tomasz Napierala 	if (__predict_false(error != 0)) {
803c26391f4SEdward Tomasz Napierala 		if (error != ERESTART && error != EJUSTRETURN)
804866b1f51SEdward Tomasz Napierala 			frame->tf_eax = bsd_to_linux_errno(error);
805c26391f4SEdward Tomasz Napierala 	}
806c26391f4SEdward Tomasz Napierala }
807c26391f4SEdward Tomasz Napierala 
808d323ddf3SMatthew Dillon /*
809598d45beSMatthew N. Dodd  * exec_setregs may initialize some registers differently than Linux
810598d45beSMatthew N. Dodd  * does, thus potentially confusing Linux binaries. If necessary, we
811598d45beSMatthew N. Dodd  * override the exec_setregs default(s) here.
812598d45beSMatthew N. Dodd  */
813598d45beSMatthew N. Dodd static void
81431174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
81531174518SJohn Baldwin     uintptr_t stack)
816598d45beSMatthew N. Dodd {
817598d45beSMatthew N. Dodd 	struct pcb *pcb = td->td_pcb;
818598d45beSMatthew N. Dodd 
819a107d8aaSNathan Whitehorn 	exec_setregs(td, imgp, stack);
820598d45beSMatthew N. Dodd 
8214ba25759SEd Maste 	/* Linux sets %gs to 0, we default to _udatasel. */
8222ee8325fSJohn Baldwin 	pcb->pcb_gs = 0;
8232ee8325fSJohn Baldwin 	load_gs(0);
8242a51b9b0SDavid Schultz 
8252ee8325fSJohn Baldwin 	pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
826598d45beSMatthew N. Dodd }
827598d45beSMatthew N. Dodd 
828d66a5066SPeter Wemm struct sysentvec linux_sysvec = {
829a8d403e1SKonstantin Belousov 	.sv_size	= LINUX_SYS_MAXSYSCALL,
830a8d403e1SKonstantin Belousov 	.sv_table	= linux_sysent,
831dc858467SEd Maste 	.sv_transtrap	= linux_translate_traps,
832a8d403e1SKonstantin Belousov 	.sv_fixup	= linux_fixup,
833a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
8349931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
835a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
836a8d403e1SKonstantin Belousov 	.sv_name	= "Linux a.out",
837a8d403e1SKonstantin Belousov 	.sv_coredump	= NULL,
838dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
839a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
840a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
841a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
8428f1e49a6SDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK,
843a8d403e1SKonstantin Belousov 	.sv_psstrings	= PS_STRINGS,
844a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
845a8d403e1SKonstantin Belousov 	.sv_copyout_strings = exec_copyout_strings,
846dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
847a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
848b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
849870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
850870e197dSKonstantin Belousov 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
851c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux_set_syscall_retval,
852afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
853afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
854e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
85581338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
856038c7205SDmitry Chagin 	.sv_trap	= NULL,
8575fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
8584815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
8594815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
860598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
861d66a5066SPeter Wemm };
8628f1e49a6SDmitry Chagin INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
863e1743d02SSøren Schmidt 
864e1743d02SSøren Schmidt struct sysentvec elf_linux_sysvec = {
865a8d403e1SKonstantin Belousov 	.sv_size	= LINUX_SYS_MAXSYSCALL,
866a8d403e1SKonstantin Belousov 	.sv_table	= linux_sysent,
867dc858467SEd Maste 	.sv_transtrap	= linux_translate_traps,
868dc858467SEd Maste 	.sv_fixup	= linux_fixup_elf,
869a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
8709931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
871a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
8725faeda90SJessica Clarke 	.sv_name	= "Linux ELF32",
873a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
874435754a5SEdward Tomasz Napierala 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
875435754a5SEdward Tomasz Napierala 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
876435754a5SEdward Tomasz Napierala 	.sv_elf_core_prepare_notes = elf32_prepare_notes,
877dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
878a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
879a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
880a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
8818f1e49a6SDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK,
8828f1e49a6SDmitry Chagin 	.sv_psstrings	= LINUX_PS_STRINGS,
883a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
8845caa67faSJohn Baldwin 	.sv_copyout_auxargs = linux_copyout_auxargs,
8854d7c2e8aSDmitry Chagin 	.sv_copyout_strings = linux_copyout_strings,
886dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
887a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
888b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
889870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
8909931033bSDmitry Chagin 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
891c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux_set_syscall_retval,
892afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
893afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
8948f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX_SHAREDPAGE,
8958f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
896e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
89781338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
898038c7205SDmitry Chagin 	.sv_trap	= NULL,
8995fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
9004815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
9014815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
902598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
903e1743d02SSøren Schmidt };
904bdc37934SDmitry Chagin 
9055fd9cd53SDmitry Chagin static int
9065fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
9075fd9cd53SDmitry Chagin {
9089931033bSDmitry Chagin 	int error = 0;
9095fd9cd53SDmitry Chagin 
9109931033bSDmitry Chagin 	if (SV_PROC_FLAG(p, SV_SHP) != 0)
9119931033bSDmitry Chagin 		error = linux_map_vdso(p, linux_vdso_obj,
9129931033bSDmitry Chagin 		    linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
9139931033bSDmitry Chagin 	if (error == 0)
9145fd9cd53SDmitry Chagin 		linux_on_exec(p, imgp);
9159931033bSDmitry Chagin 	return (error);
9165fd9cd53SDmitry Chagin }
9175fd9cd53SDmitry Chagin 
91809cffde9SDmitry Chagin /*
91909cffde9SDmitry Chagin  * linux_vdso_install() and linux_exec_sysvec_init() must be called
92009cffde9SDmitry Chagin  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
92109cffde9SDmitry Chagin  */
922bdc37934SDmitry Chagin static void
9239931033bSDmitry Chagin linux_exec_sysvec_init(void *param)
9249931033bSDmitry Chagin {
9259931033bSDmitry Chagin 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
9269931033bSDmitry Chagin 	struct sysentvec *sv;
9279931033bSDmitry Chagin 	ptrdiff_t tkoff;
9289931033bSDmitry Chagin 
9299931033bSDmitry Chagin 	sv = param;
9309931033bSDmitry Chagin 	/* Fill timekeep_base */
9319931033bSDmitry Chagin 	exec_sysvec_init(sv);
9329931033bSDmitry Chagin 
9339931033bSDmitry Chagin 	tkoff = kern_timekeep_base - linux_vdso_base;
9349931033bSDmitry Chagin 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
9359931033bSDmitry Chagin 	*ktimekeep_base = sv->sv_timekeep_base;
9369931033bSDmitry Chagin 
9379931033bSDmitry Chagin 	tkoff = kern_tsc_selector - linux_vdso_base;
9389931033bSDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
9399931033bSDmitry Chagin 	*ktsc_selector = linux_vdso_tsc_selector_idx();
9409931033bSDmitry Chagin 	if (bootverbose)
9419931033bSDmitry Chagin 		printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
9429931033bSDmitry Chagin }
94309cffde9SDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
9449931033bSDmitry Chagin     linux_exec_sysvec_init, &elf_linux_sysvec);
9459931033bSDmitry Chagin 
9469931033bSDmitry Chagin static void
947a543556cSDmitry Chagin linux_vdso_install(const void *param)
948bdc37934SDmitry Chagin {
9499931033bSDmitry Chagin 	char *vdso_start = &_binary_linux_vdso_so_o_start;
9509931033bSDmitry Chagin 	char *vdso_end = &_binary_linux_vdso_so_o_end;
951bdc37934SDmitry Chagin 
9529931033bSDmitry Chagin 	linux_szsigcode = vdso_end - vdso_start;
9539931033bSDmitry Chagin 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
954bdc37934SDmitry Chagin 
9559931033bSDmitry Chagin 	linux_vdso_base = LINUX_VDSOPAGE;
956bdc37934SDmitry Chagin 
9579931033bSDmitry Chagin 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
958bdc37934SDmitry Chagin 
9599931033bSDmitry Chagin 	linux_vdso_obj = __elfN(linux_shared_page_init)
9609931033bSDmitry Chagin 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
9619931033bSDmitry Chagin 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
962bdc37934SDmitry Chagin 
9639931033bSDmitry Chagin 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
964bdc37934SDmitry Chagin }
96509cffde9SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
966f8268d4dSEd Maste     linux_vdso_install, NULL);
967bdc37934SDmitry Chagin 
968bdc37934SDmitry Chagin static void
969a543556cSDmitry Chagin linux_vdso_deinstall(const void *param)
970bdc37934SDmitry Chagin {
971bdc37934SDmitry Chagin 
9729931033bSDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
9739931033bSDmitry Chagin 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
9747b194b3dSEd Maste }
975bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
976f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
977e1743d02SSøren Schmidt 
9789931033bSDmitry Chagin static void
9799931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset)
9809931033bSDmitry Chagin {
9819931033bSDmitry Chagin 	const Elf_Shdr *shdr;
9829931033bSDmitry Chagin 	const Elf_Rel *rel;
9839931033bSDmitry Chagin 	const Elf_Ehdr *ehdr;
9849931033bSDmitry Chagin 	Elf_Addr *where;
9859931033bSDmitry Chagin 	Elf_Size rtype, symidx;
9869931033bSDmitry Chagin 	Elf_Addr addr, addend;
9879931033bSDmitry Chagin 	int i, relcnt;
9889931033bSDmitry Chagin 
9899931033bSDmitry Chagin 	MPASS(offset != 0);
9909931033bSDmitry Chagin 
9919931033bSDmitry Chagin 	relcnt = 0;
9929931033bSDmitry Chagin 	ehdr = (const Elf_Ehdr *)mapping;
9939931033bSDmitry Chagin 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
9949931033bSDmitry Chagin 	for (i = 0; i < ehdr->e_shnum; i++)
9959931033bSDmitry Chagin 	{
9969931033bSDmitry Chagin 		switch (shdr[i].sh_type) {
9979931033bSDmitry Chagin 		case SHT_REL:
9989931033bSDmitry Chagin 			rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
9999931033bSDmitry Chagin 			relcnt = shdr[i].sh_size / sizeof(*rel);
10009931033bSDmitry Chagin 			break;
10019931033bSDmitry Chagin 		case SHT_RELA:
10029931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected Rela section\n");
10039931033bSDmitry Chagin 			break;
10049931033bSDmitry Chagin 		}
10059931033bSDmitry Chagin 	}
10069931033bSDmitry Chagin 
10079931033bSDmitry Chagin 	for (i = 0; i < relcnt; i++, rel++) {
10089931033bSDmitry Chagin 		where = (Elf_Addr *)(mapping + rel->r_offset);
10099931033bSDmitry Chagin 		addend = *where;
10109931033bSDmitry Chagin 		rtype = ELF_R_TYPE(rel->r_info);
10119931033bSDmitry Chagin 		symidx = ELF_R_SYM(rel->r_info);
10129931033bSDmitry Chagin 
10139931033bSDmitry Chagin 		switch (rtype) {
10149931033bSDmitry Chagin 		case R_386_NONE:	/* none */
10159931033bSDmitry Chagin 			break;
10169931033bSDmitry Chagin 
10179931033bSDmitry Chagin 		case R_386_RELATIVE:	/* B + A */
10189931033bSDmitry Chagin 			addr = (Elf_Addr)PTROUT(offset + addend);
10199931033bSDmitry Chagin 			if (*where != addr)
10209931033bSDmitry Chagin 				*where = addr;
10219931033bSDmitry Chagin 			break;
10229931033bSDmitry Chagin 
10239931033bSDmitry Chagin 		case R_386_IRELATIVE:
10249931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected ifunc relocation, "
10259931033bSDmitry Chagin 			    "symbol index %d\n", symidx);
10269931033bSDmitry Chagin 			break;
10279931033bSDmitry Chagin 		default:
10289931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected relocation type %d, "
10299931033bSDmitry Chagin 			    "symbol index %d\n", rtype, symidx);
10309931033bSDmitry Chagin 		}
10319931033bSDmitry Chagin 	}
10329931033bSDmitry Chagin }
10339931033bSDmitry Chagin 
103489ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
103589ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
103689ffc202SBjoern A. Zeeb 
1037a95659f7SEd Maste static bool
103889ffc202SBjoern A. Zeeb linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
103989ffc202SBjoern A. Zeeb {
104089ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
104189ffc202SBjoern A. Zeeb 	uintptr_t p;
104289ffc202SBjoern A. Zeeb 
104389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
104489ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
104589ffc202SBjoern A. Zeeb 
104689ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
104789ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
1048a95659f7SEd Maste 		return (false);
104989ffc202SBjoern A. Zeeb 
105089ffc202SBjoern A. Zeeb 	/*
105135755049SChuck Tuffli 	 * For Linux we encode osrel using the Linux convention of
105235755049SChuck Tuffli 	 * 	(version << 16) | (major << 8) | (minor)
105335755049SChuck Tuffli 	 * See macro in linux_mib.h
105489ffc202SBjoern A. Zeeb 	 */
105535755049SChuck Tuffli 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
105689ffc202SBjoern A. Zeeb 
1057a95659f7SEd Maste 	return (true);
105889ffc202SBjoern A. Zeeb }
105932c01de2SDmitry Chagin 
106032c01de2SDmitry Chagin static Elf_Brandnote linux_brandnote = {
106189ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
106289ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
106332c01de2SDmitry Chagin 	.hdr.n_type	= 1,
106489ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
106589ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
106689ffc202SBjoern A. Zeeb 	.trans_osrel	= linux_trans_osrel
106732c01de2SDmitry Chagin };
106832c01de2SDmitry Chagin 
1069514058dcSAlexander Langer static Elf32_Brandinfo linux_brand = {
1070a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1071a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1072a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1073b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1074a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
1075a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1076a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
107732c01de2SDmitry Chagin 	.brand_note	= &linux_brandnote,
10782dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
10795cf588ebSPeter Wemm };
10805cf588ebSPeter Wemm 
1081514058dcSAlexander Langer static Elf32_Brandinfo linux_glibc2brand = {
1082a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1083a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1084a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1085b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1086a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1087a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1088a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
108932c01de2SDmitry Chagin 	.brand_note	= &linux_brandnote,
10902dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
10914e138a28SMike Smith };
10924e138a28SMike Smith 
1093a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1094a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1095a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1096a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1097b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1098a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1099a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1100a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1101a0c59c7aSDmitry Chagin 	.brand_note	= &linux_brandnote,
1102cf8d74e3SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1103cf8d74e3SDmitry Chagin 			    LINUX_BI_FUTEX_REQUEUE
1104a0c59c7aSDmitry Chagin };
1105a0c59c7aSDmitry Chagin 
1106514058dcSAlexander Langer Elf32_Brandinfo *linux_brandlist[] = {
1107514058dcSAlexander Langer 	&linux_brand,
1108514058dcSAlexander Langer 	&linux_glibc2brand,
1109a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1110514058dcSAlexander Langer 	NULL
1111514058dcSAlexander Langer };
1112514058dcSAlexander Langer 
1113aa855a59SPeter Wemm static int
1114c25ded31SBruce Evans linux_elf_modevent(module_t mod, int type, void *data)
1115d30ea4f5SPeter Wemm {
1116514058dcSAlexander Langer 	Elf32_Brandinfo **brandinfo;
1117514058dcSAlexander Langer 	int error;
1118f41325dbSPeter Wemm 	struct linux_ioctl_handler **lihp;
1119514058dcSAlexander Langer 
1120514058dcSAlexander Langer 	error = 0;
1121514058dcSAlexander Langer 
1122aa855a59SPeter Wemm 	switch(type) {
1123aa855a59SPeter Wemm 	case MOD_LOAD:
1124aa855a59SPeter Wemm 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1125aa855a59SPeter Wemm 		     ++brandinfo)
11263ebc1248SPeter Wemm 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1127aa855a59SPeter Wemm 				error = EINVAL;
1128466b14d7SMarcel Moolenaar 		if (error == 0) {
1129f41325dbSPeter Wemm 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1130f41325dbSPeter Wemm 				linux_ioctl_register_handler(*lihp);
11319b44bfc5SAlexander Leidinger 			LIST_INIT(&futex_list);
113279262bf1SDmitry Chagin 			mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1133044ab55eSEdward Tomasz Napierala 			linux_dev_shm_create();
11347ae27ff4SJamie Gritton 			linux_osd_jail_register();
11351ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
113643bef515SMarcel Moolenaar 			if (bootverbose)
1137466b14d7SMarcel Moolenaar 				printf("Linux ELF exec handler installed\n");
1138466b14d7SMarcel Moolenaar 		} else
1139466b14d7SMarcel Moolenaar 			printf("cannot insert Linux ELF brand handler\n");
1140aa855a59SPeter Wemm 		break;
1141aa855a59SPeter Wemm 	case MOD_UNLOAD:
1142aa855a59SPeter Wemm 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1143aa855a59SPeter Wemm 		     ++brandinfo)
11443ebc1248SPeter Wemm 			if (elf32_brand_inuse(*brandinfo))
1145d2758342SMark Newton 				error = EBUSY;
1146d2758342SMark Newton 		if (error == 0) {
1147d2758342SMark Newton 			for (brandinfo = &linux_brandlist[0];
1148d2758342SMark Newton 			     *brandinfo != NULL; ++brandinfo)
11493ebc1248SPeter Wemm 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1150aa855a59SPeter Wemm 					error = EINVAL;
1151d2758342SMark Newton 		}
1152466b14d7SMarcel Moolenaar 		if (error == 0) {
1153f41325dbSPeter Wemm 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1154f41325dbSPeter Wemm 				linux_ioctl_unregister_handler(*lihp);
115579262bf1SDmitry Chagin 			mtx_destroy(&futex_mtx);
1156044ab55eSEdward Tomasz Napierala 			linux_dev_shm_destroy();
11577ae27ff4SJamie Gritton 			linux_osd_jail_deregister();
1158466b14d7SMarcel Moolenaar 			if (bootverbose)
1159466b14d7SMarcel Moolenaar 				printf("Linux ELF exec handler removed\n");
1160466b14d7SMarcel Moolenaar 		} else
1161aa855a59SPeter Wemm 			printf("Could not deinstall ELF interpreter entry\n");
1162aa855a59SPeter Wemm 		break;
1163aa855a59SPeter Wemm 	default:
1164af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1165d30ea4f5SPeter Wemm 	}
1166af682d48SDmitry Chagin 	return (error);
1167aa855a59SPeter Wemm }
1168466b14d7SMarcel Moolenaar 
1169aa855a59SPeter Wemm static moduledata_t linux_elf_mod = {
1170aa855a59SPeter Wemm 	"linuxelf",
1171aa855a59SPeter Wemm 	linux_elf_modevent,
11729823d527SKevin Lo 	0
1173aa855a59SPeter Wemm };
1174466b14d7SMarcel Moolenaar 
117578ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1176b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
117709cffde9SDmitry Chagin 
117809cffde9SDmitry Chagin /*
117909cffde9SDmitry Chagin  * linux_vdso_install() and linux_exec_sysvec_init() must be called
118009cffde9SDmitry Chagin  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
118109cffde9SDmitry Chagin  */
1182