xref: /freebsd/sys/i386/linux/linux_sysvec.c (revision 4a6c2d075da0c5105269e6edcbe57bf6aaa0a0ae)
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>
40e1743d02SSøren Schmidt #include <sys/malloc.h>
41ff22c670SBruce Evans #include <sys/module.h>
42fb919e4dSMark Murray #include <sys/proc.h>
439931033bSDmitry Chagin #include <sys/stddef.h>
44fb919e4dSMark Murray #include <sys/signalvar.h>
45206a5d3aSIan Dowse #include <sys/syscallsubr.h>
4667d39748SDmitry Chagin #include <sys/sysctl.h>
47fb919e4dSMark Murray #include <sys/sysent.h>
48fb919e4dSMark Murray #include <sys/sysproto.h>
49a9148ab1SPeter Wemm #include <sys/vnode.h>
50fb919e4dSMark Murray 
51d66a5066SPeter Wemm #include <vm/vm.h>
52a9148ab1SPeter Wemm #include <vm/pmap.h>
53ff22c670SBruce Evans #include <vm/vm_extern.h>
54a9148ab1SPeter Wemm #include <vm/vm_map.h>
55a9148ab1SPeter Wemm #include <vm/vm_object.h>
56ff22c670SBruce Evans #include <vm/vm_page.h>
57ff22c670SBruce Evans #include <vm/vm_param.h>
58ff22c670SBruce Evans 
59ff22c670SBruce Evans #include <machine/cpu.h>
604d7c2e8aSDmitry Chagin #include <machine/cputypes.h>
61ff22c670SBruce Evans #include <machine/md_var.h>
62d3adf769SDavid Schultz #include <machine/pcb.h>
63d41e41f9SJohn Baldwin #include <machine/trap.h>
64a9148ab1SPeter Wemm 
659931033bSDmitry Chagin #include <x86/linux/linux_x86.h>
66d66a5066SPeter Wemm #include <i386/linux/linux.h>
67ebea8660SMarcel Moolenaar #include <i386/linux/linux_proto.h>
6894cb2ecfSAlexander Leidinger #include <compat/linux/linux_emul.h>
690a4b664aSDmitry Chagin #include <compat/linux/linux_fork.h>
70d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
710f9d6538SJohn Baldwin #include <compat/linux/linux_mib.h>
724d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
73b595ab37SAndrew Gallatin #include <compat/linux/linux_signal.h>
74322bfdc3SMarcel Moolenaar #include <compat/linux/linux_util.h>
75bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
76e1743d02SSøren Schmidt 
7721f24617SDmitry Chagin #include <x86/linux/linux_x86_sigframe.h>
7821f24617SDmitry Chagin 
791d91482dSPeter Wemm MODULE_VERSION(linux, 1);
801d91482dSPeter Wemm 
819931033bSDmitry Chagin #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
829931033bSDmitry Chagin #define	LINUX_VDSOPAGE		(VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE)
839931033bSDmitry Chagin #define	LINUX_SHAREDPAGE	(LINUX_VDSOPAGE - PAGE_SIZE)
849931033bSDmitry Chagin 				/*
859931033bSDmitry Chagin 				 * PAGE_SIZE - the size
869931033bSDmitry Chagin 				 * of the native SHAREDPAGE
879931033bSDmitry Chagin 				 */
889931033bSDmitry Chagin #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
898f1e49a6SDmitry Chagin #define	LINUX_PS_STRINGS	(LINUX_USRSTACK - sizeof(struct ps_strings))
908f1e49a6SDmitry Chagin 
91bdc37934SDmitry Chagin static int linux_szsigcode;
929931033bSDmitry Chagin static vm_object_t linux_vdso_obj;
939931033bSDmitry Chagin static char *linux_vdso_mapping;
949931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start;
959931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end;
969931033bSDmitry Chagin static vm_offset_t linux_vdso_base;
9743bef515SMarcel Moolenaar 
9843bef515SMarcel Moolenaar extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
9943bef515SMarcel Moolenaar 
100f41325dbSPeter Wemm SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
10143bef515SMarcel Moolenaar 
10231174518SJohn Baldwin static int	linux_fixup(uintptr_t *stack_base,
10389c9a483SAlfred Perlstein 		    struct image_params *iparams);
10431174518SJohn Baldwin static int	linux_fixup_elf(uintptr_t *stack_base,
10589c9a483SAlfred Perlstein 		    struct image_params *iparams);
1069104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
107dc858467SEd Maste static void	linux_exec_setregs(struct thread *td,
10831174518SJohn Baldwin 		    struct image_params *imgp, uintptr_t stack);
1099931033bSDmitry Chagin static void	linux_exec_sysvec_init(void *param);
1105fd9cd53SDmitry Chagin static int	linux_on_exec_vmspace(struct proc *p,
1115fd9cd53SDmitry Chagin 		    struct image_params *imgp);
11203b0d68cSJohn Baldwin static int	linux_copyout_strings(struct image_params *imgp,
11331174518SJohn Baldwin 		    uintptr_t *stack_base);
114de8374dfSDmitry Chagin static void	linux_set_fork_retval(struct thread *td);
115a95659f7SEd Maste static bool	linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
116a543556cSDmitry Chagin static void	linux_vdso_install(const void *param);
117a543556cSDmitry Chagin static void	linux_vdso_deinstall(const void *param);
1189931033bSDmitry Chagin static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
1194d7c2e8aSDmitry Chagin 
120c1da89feSDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
1219931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
1228f9635dcSDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
1238f9635dcSDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode);
1249931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
1259931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
1265a6a4fb2SDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
127bdc37934SDmitry Chagin 
128303b270bSEivind Eklund static int
12931174518SJohn Baldwin linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
130d66a5066SPeter Wemm {
13131174518SJohn Baldwin 	register_t *base, *argv, *envp;
132d66a5066SPeter Wemm 
13331174518SJohn Baldwin 	base = (register_t *)*stack_base;
13431174518SJohn Baldwin 	argv = base;
13531174518SJohn Baldwin 	envp = base + (imgp->args->argc + 1);
13631174518SJohn Baldwin 	base--;
13731174518SJohn Baldwin 	suword(base, (intptr_t)envp);
13831174518SJohn Baldwin 	base--;
13931174518SJohn Baldwin 	suword(base, (intptr_t)argv);
14031174518SJohn Baldwin 	base--;
14131174518SJohn Baldwin 	suword(base, imgp->args->argc);
14231174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
1434d7c2e8aSDmitry Chagin 	return (0);
144d66a5066SPeter Wemm }
145d66a5066SPeter Wemm 
14603b0d68cSJohn Baldwin static int
147d8010b11SJohn Baldwin linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
148e1743d02SSøren Schmidt {
14943cf129cSJohn Baldwin 	Elf32_Auxargs *args;
1505f77b8a8SBrooks Davis 	Elf32_Auxinfo *argarray, *pos;
15103b0d68cSJohn Baldwin 	int error, issetugid;
1524d7c2e8aSDmitry Chagin 
153669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
15443cf129cSJohn Baldwin 	args = (Elf32_Auxargs *)imgp->auxargs;
1555f77b8a8SBrooks Davis 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
1565f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
157e1743d02SSøren Schmidt 
1589931033bSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
1599931033bSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
1604d7c2e8aSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
1618d30f381SDmitry Chagin 
1628d30f381SDmitry Chagin 	/*
1638d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
1648d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
1658d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
1668d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
1678d30f381SDmitry Chagin 	 * is not present.
1688d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
1698d30f381SDmitry Chagin 	 */
1708d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
1711ca16454SDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
172e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
173e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
174e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
175e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
176e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
177e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
178e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
179669414e4SXin LI 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
180b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
181b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
182b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
183b1fc0ec1SRobert Watson 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
184c1da89feSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
185b24e6ac8SBrooks Davis 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
1864048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
187b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
1884d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
1894d7c2e8aSDmitry Chagin 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
190e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
191e1743d02SSøren Schmidt 
192e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
193e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
194cbf7e0cbSBrooks Davis 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
1955f77b8a8SBrooks Davis 
196d8010b11SJohn Baldwin 	error = copyout(argarray, (void *)base,
197d8010b11SJohn Baldwin 	    sizeof(*argarray) * LINUX_AT_COUNT);
1985f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
19903b0d68cSJohn Baldwin 	return (error);
2005caa67faSJohn Baldwin }
2015caa67faSJohn Baldwin 
2025caa67faSJohn Baldwin static int
20331174518SJohn Baldwin linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp)
2045caa67faSJohn Baldwin {
20531174518SJohn Baldwin 	register_t *base;
206e1743d02SSøren Schmidt 
20731174518SJohn Baldwin 	base = (register_t *)*stack_base;
20831174518SJohn Baldwin 	base--;
20931174518SJohn Baldwin 	if (suword(base, (register_t)imgp->args->argc) == -1)
2105f77b8a8SBrooks Davis 		return (EFAULT);
21131174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
2124d7c2e8aSDmitry Chagin 	return (0);
213e1743d02SSøren Schmidt }
214d66a5066SPeter Wemm 
2154d7c2e8aSDmitry Chagin /*
2164d7c2e8aSDmitry Chagin  * Copied from kern/kern_exec.c
2174d7c2e8aSDmitry Chagin  */
21803b0d68cSJohn Baldwin static int
21931174518SJohn Baldwin linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
2204d7c2e8aSDmitry Chagin {
22103b0d68cSJohn Baldwin 	int argc, envc, error;
2224d7c2e8aSDmitry Chagin 	char **vectp;
22331174518SJohn Baldwin 	char *stringp;
22431174518SJohn Baldwin 	uintptr_t destp, ustringp;
2254d7c2e8aSDmitry Chagin 	struct ps_strings *arginfo;
2264048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
2274048f59cSDmitry Chagin 	size_t execpath_len;
2284d7c2e8aSDmitry Chagin 	struct proc *p;
2294d7c2e8aSDmitry Chagin 
2304d7c2e8aSDmitry Chagin 	p = imgp->proc;
231706f4a81SMark Johnston 	arginfo = (struct ps_strings *)PROC_PS_STRINGS(p);
23231174518SJohn Baldwin 	destp = (uintptr_t)arginfo;
2334d7c2e8aSDmitry Chagin 
234f04a0960SMark Johnston 	if (imgp->execpath != NULL && imgp->auxargs != NULL) {
235f04a0960SMark Johnston 		execpath_len = strlen(imgp->execpath) + 1;
23631174518SJohn Baldwin 		destp -= execpath_len;
23731174518SJohn Baldwin 		destp = rounddown2(destp, sizeof(void *));
238b24e6ac8SBrooks Davis 		imgp->execpathp = (void *)destp;
239b24e6ac8SBrooks Davis 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
24003b0d68cSJohn Baldwin 		if (error != 0)
24103b0d68cSJohn Baldwin 			return (error);
2424048f59cSDmitry Chagin 	}
2434048f59cSDmitry Chagin 
2444ba25759SEd Maste 	/* Prepare the canary for SSP. */
2454048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
24631174518SJohn Baldwin 	destp -= roundup(sizeof(canary), sizeof(void *));
247b24e6ac8SBrooks Davis 	imgp->canary = (void *)destp;
248b24e6ac8SBrooks Davis 	error = copyout(canary, imgp->canary, sizeof(canary));
24903b0d68cSJohn Baldwin 	if (error != 0)
25003b0d68cSJohn Baldwin 		return (error);
2514048f59cSDmitry Chagin 
25231174518SJohn Baldwin 	/* Allocate room for the argument and environment strings. */
25331174518SJohn Baldwin 	destp -= ARG_MAX - imgp->args->stringspace;
25431174518SJohn Baldwin 	destp = rounddown2(destp, sizeof(void *));
25531174518SJohn Baldwin 	ustringp = destp;
25631174518SJohn Baldwin 
25703b0d68cSJohn Baldwin 	if (imgp->auxargs) {
258d8010b11SJohn Baldwin 		/*
259d8010b11SJohn Baldwin 		 * Allocate room on the stack for the ELF auxargs
260d8010b11SJohn Baldwin 		 * array.  It has LINUX_AT_COUNT entries.
261d8010b11SJohn Baldwin 		 */
262d8010b11SJohn Baldwin 		destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
263d8010b11SJohn Baldwin 		destp = rounddown2(destp, sizeof(void *));
26403b0d68cSJohn Baldwin 	}
2654d7c2e8aSDmitry Chagin 
26631174518SJohn Baldwin 	vectp = (char **)destp;
26731174518SJohn Baldwin 
26873c8686eSJohn Baldwin 	/*
26973c8686eSJohn Baldwin 	 * Allocate room for the argv[] and env vectors including the
27073c8686eSJohn Baldwin 	 * terminating NULL pointers.
27173c8686eSJohn Baldwin 	 */
27273c8686eSJohn Baldwin 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
27373c8686eSJohn Baldwin 
2744ba25759SEd Maste 	/* vectp also becomes our initial stack base. */
27531174518SJohn Baldwin 	*stack_base = (uintptr_t)vectp;
2764d7c2e8aSDmitry Chagin 
2774d7c2e8aSDmitry Chagin 	stringp = imgp->args->begin_argv;
2784d7c2e8aSDmitry Chagin 	argc = imgp->args->argc;
2794d7c2e8aSDmitry Chagin 	envc = imgp->args->envc;
2804d7c2e8aSDmitry Chagin 
2814ba25759SEd Maste 	/* Copy out strings - arguments and environment. */
28231174518SJohn Baldwin 	error = copyout(stringp, (void *)ustringp,
28331174518SJohn Baldwin 	    ARG_MAX - imgp->args->stringspace);
28403b0d68cSJohn Baldwin 	if (error != 0)
28503b0d68cSJohn Baldwin 		return (error);
2864d7c2e8aSDmitry Chagin 
2874ba25759SEd Maste 	/* Fill in "ps_strings" struct for ps, w, etc. */
28803b0d68cSJohn Baldwin 	if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 ||
28903b0d68cSJohn Baldwin 	    suword(&arginfo->ps_nargvstr, argc) != 0)
29003b0d68cSJohn Baldwin 		return (EFAULT);
2914d7c2e8aSDmitry Chagin 
2924ba25759SEd Maste 	/* Fill in argument portion of vector table. */
2934d7c2e8aSDmitry Chagin 	for (; argc > 0; --argc) {
29431174518SJohn Baldwin 		if (suword(vectp++, ustringp) != 0)
29503b0d68cSJohn Baldwin 			return (EFAULT);
2964d7c2e8aSDmitry Chagin 		while (*stringp++ != 0)
29731174518SJohn Baldwin 			ustringp++;
29831174518SJohn Baldwin 		ustringp++;
2994d7c2e8aSDmitry Chagin 	}
3004d7c2e8aSDmitry Chagin 
3014ba25759SEd Maste 	/* A null vector table pointer separates the argp's from the envp's. */
30203b0d68cSJohn Baldwin 	if (suword(vectp++, 0) != 0)
30303b0d68cSJohn Baldwin 		return (EFAULT);
3044d7c2e8aSDmitry Chagin 
30503b0d68cSJohn Baldwin 	if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 ||
30603b0d68cSJohn Baldwin 	    suword(&arginfo->ps_nenvstr, envc) != 0)
30703b0d68cSJohn Baldwin 		return (EFAULT);
3084d7c2e8aSDmitry Chagin 
3094ba25759SEd Maste 	/* Fill in environment portion of vector table. */
3104d7c2e8aSDmitry Chagin 	for (; envc > 0; --envc) {
31131174518SJohn Baldwin 		if (suword(vectp++, ustringp) != 0)
31203b0d68cSJohn Baldwin 			return (EFAULT);
3134d7c2e8aSDmitry Chagin 		while (*stringp++ != 0)
31431174518SJohn Baldwin 			ustringp++;
31531174518SJohn Baldwin 		ustringp++;
3164d7c2e8aSDmitry Chagin 	}
3174d7c2e8aSDmitry Chagin 
3184ba25759SEd Maste 	/* The end of the vector table is a null pointer. */
31903b0d68cSJohn Baldwin 	if (suword(vectp, 0) != 0)
32003b0d68cSJohn Baldwin 		return (EFAULT);
3214d7c2e8aSDmitry Chagin 
322d8010b11SJohn Baldwin 	if (imgp->auxargs) {
323d8010b11SJohn Baldwin 		vectp++;
324d8010b11SJohn Baldwin 		error = imgp->sysent->sv_copyout_auxargs(imgp,
325d8010b11SJohn Baldwin 		    (uintptr_t)vectp);
326d8010b11SJohn Baldwin 		if (error != 0)
327d8010b11SJohn Baldwin 			return (error);
328d8010b11SJohn Baldwin 	}
329d8010b11SJohn Baldwin 
33003b0d68cSJohn Baldwin 	return (0);
3314d7c2e8aSDmitry Chagin }
3324d7c2e8aSDmitry Chagin 
33379363394SAndrew Gallatin static void
3349104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
33579363394SAndrew Gallatin {
3361d062e2bSDag-Erling Smørgrav 	struct thread *td = curthread;
3371d062e2bSDag-Erling Smørgrav 	struct proc *p = td->td_proc;
33890af4afaSJohn Baldwin 	struct sigacts *psp;
3391d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
3405002a60fSMarcel Moolenaar 	struct l_rt_sigframe *fp, frame;
3419104847fSDavid Xu 	int sig, code;
34279363394SAndrew Gallatin 	int oonstack;
34379363394SAndrew Gallatin 
344eca368ecSDmitry Chagin 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
3459104847fSDavid Xu 	code = ksi->ksi_code;
346df53e91cSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
34790af4afaSJohn Baldwin 	psp = p->p_sigacts;
34890af4afaSJohn Baldwin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
349b40ce416SJulian Elischer 	regs = td->td_frame;
350d034d459SMarcel Moolenaar 	oonstack = sigonstack(regs->tf_esp);
35179363394SAndrew Gallatin 
3524ba25759SEd Maste 	/* Allocate space for the signal handler context. */
353a30ec4b9SDavid Xu 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
35490af4afaSJohn Baldwin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
355aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
356a30ec4b9SDavid Xu 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
357d034d459SMarcel Moolenaar 	} else
3585002a60fSMarcel Moolenaar 		fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
35990af4afaSJohn Baldwin 	mtx_unlock(&psp->ps_mtx);
36079363394SAndrew Gallatin 
3614ba25759SEd Maste 	/* Build the argument list for the signal handler. */
3624ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
36379363394SAndrew Gallatin 
36499d45c5fSMarcel Moolenaar 	bzero(&frame, sizeof(frame));
36599d45c5fSMarcel Moolenaar 
36679363394SAndrew Gallatin 	frame.sf_sig = sig;
36721f24617SDmitry Chagin 	frame.sf_siginfo = PTROUT(&fp->sf_si);
3686e826d27SDmitry Chagin 	frame.sf_ucontext = PTROUT(&fp->sf_uc);
369cc6ca9b3SMarcel Moolenaar 
3704ba25759SEd Maste 	/* Fill in POSIX parts. */
371f4e80108SDmitry Chagin 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
372cc6ca9b3SMarcel Moolenaar 
3734ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
3746e826d27SDmitry Chagin 	frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
3756e826d27SDmitry Chagin 	frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
3766e826d27SDmitry Chagin 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
377d034d459SMarcel Moolenaar 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
378611d9407SJohn Baldwin 	PROC_UNLOCK(p);
379cc6ca9b3SMarcel Moolenaar 
3806e826d27SDmitry Chagin 	bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
381cc6ca9b3SMarcel Moolenaar 
3826e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_mask   = frame.sf_uc.uc_sigmask.__mask;
3836e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_gs     = rgs();
3846e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_fs     = regs->tf_fs;
3856e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_es     = regs->tf_es;
3866e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ds     = regs->tf_ds;
3876e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_edi    = regs->tf_edi;
3886e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_esi    = regs->tf_esi;
3896e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ebp    = regs->tf_ebp;
3906e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ebx    = regs->tf_ebx;
3916e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_esp    = regs->tf_esp;
3926e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_edx    = regs->tf_edx;
3936e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ecx    = regs->tf_ecx;
3946e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_eax    = regs->tf_eax;
3956e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_eip    = regs->tf_eip;
3966e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
3976e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags;
3986e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
3996e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_ss     = regs->tf_ss;
4006e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
4016e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
4026e826d27SDmitry Chagin 	frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
40379363394SAndrew Gallatin 
40479363394SAndrew Gallatin 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
40579363394SAndrew Gallatin 		/*
40679363394SAndrew Gallatin 		 * Process has trashed its stack; give it an illegal
40779363394SAndrew Gallatin 		 * instruction to halt it in its tracks.
40879363394SAndrew Gallatin 		 */
40919eb87d2SJohn Baldwin 		PROC_LOCK(p);
410b40ce416SJulian Elischer 		sigexit(td, SIGILL);
41179363394SAndrew Gallatin 	}
41279363394SAndrew Gallatin 
4134ba25759SEd Maste 	/* Build context to run handler in. */
41421f24617SDmitry Chagin 	regs->tf_esp = PTROUT(fp);
4158f9635dcSDmitry Chagin 	regs->tf_eip = linux_vdso_rt_sigcode;
41621f24617SDmitry Chagin 	regs->tf_edi = PTROUT(catcher);
41722eca0bfSKonstantin Belousov 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
41879363394SAndrew Gallatin 	regs->tf_cs = _ucodesel;
41979363394SAndrew Gallatin 	regs->tf_ds = _udatasel;
42079363394SAndrew Gallatin 	regs->tf_es = _udatasel;
42179363394SAndrew Gallatin 	regs->tf_fs = _udatasel;
42279363394SAndrew Gallatin 	regs->tf_ss = _udatasel;
423df53e91cSJohn Baldwin 	PROC_LOCK(p);
42490af4afaSJohn Baldwin 	mtx_lock(&psp->ps_mtx);
42579363394SAndrew Gallatin }
42679363394SAndrew Gallatin 
427d66a5066SPeter Wemm /*
428d66a5066SPeter Wemm  * Send an interrupt to process.
429d66a5066SPeter Wemm  *
430d66a5066SPeter Wemm  * Stack is set up to allow sigcode stored
431d66a5066SPeter Wemm  * in u. to call routine, followed by kcall
432d66a5066SPeter Wemm  * to sigreturn routine below.  After sigreturn
433d66a5066SPeter Wemm  * resets the signal mask, the stack, and the
434d66a5066SPeter Wemm  * frame pointer, it returns to the user
435d66a5066SPeter Wemm  * specified pc, psl.
436d66a5066SPeter Wemm  */
437303b270bSEivind Eklund static void
4389104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
439d66a5066SPeter Wemm {
4401d062e2bSDag-Erling Smørgrav 	struct thread *td = curthread;
4411d062e2bSDag-Erling Smørgrav 	struct proc *p = td->td_proc;
44290af4afaSJohn Baldwin 	struct sigacts *psp;
4431d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
4445002a60fSMarcel Moolenaar 	struct l_sigframe *fp, frame;
4455002a60fSMarcel Moolenaar 	l_sigset_t lmask;
4469e701630SJohn Baldwin 	int sig;
4474ab7403bSDmitry Chagin 	int oonstack;
448d66a5066SPeter Wemm 
4492509e6c2SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
45090af4afaSJohn Baldwin 	psp = p->p_sigacts;
451eca368ecSDmitry Chagin 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
45290af4afaSJohn Baldwin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
45390af4afaSJohn Baldwin 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
454cc6ca9b3SMarcel Moolenaar 		/* Signal handler installed with SA_SIGINFO. */
4559104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
456cc6ca9b3SMarcel Moolenaar 		return;
457cc6ca9b3SMarcel Moolenaar 	}
458b40ce416SJulian Elischer 	regs = td->td_frame;
459d034d459SMarcel Moolenaar 	oonstack = sigonstack(regs->tf_esp);
460d66a5066SPeter Wemm 
4614ba25759SEd Maste 	/* Allocate space for the signal handler context. */
462a30ec4b9SDavid Xu 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
46390af4afaSJohn Baldwin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
464aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
465a30ec4b9SDavid Xu 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
466d034d459SMarcel Moolenaar 	} else
4675002a60fSMarcel Moolenaar 		fp = (struct l_sigframe *)regs->tf_esp - 1;
46890af4afaSJohn Baldwin 	mtx_unlock(&psp->ps_mtx);
469611d9407SJohn Baldwin 	PROC_UNLOCK(p);
470d66a5066SPeter Wemm 
4714ba25759SEd Maste 	/* Build the argument list for the signal handler. */
4724ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
473d66a5066SPeter Wemm 
47499d45c5fSMarcel Moolenaar 	bzero(&frame, sizeof(frame));
47599d45c5fSMarcel Moolenaar 
476d66a5066SPeter Wemm 	frame.sf_sig = sig;
477*4a6c2d07SDmitry Chagin 	frame.sf_sigmask = *mask;
478cc6ca9b3SMarcel Moolenaar 	bsd_to_linux_sigset(mask, &lmask);
479cc6ca9b3SMarcel Moolenaar 
4804ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
4814ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
4825206bca1SLuoqi Chen 	frame.sf_sc.sc_gs     = rgs();
4835206bca1SLuoqi Chen 	frame.sf_sc.sc_fs     = regs->tf_fs;
484213fdd80SPeter Wemm 	frame.sf_sc.sc_es     = regs->tf_es;
485213fdd80SPeter Wemm 	frame.sf_sc.sc_ds     = regs->tf_ds;
486213fdd80SPeter Wemm 	frame.sf_sc.sc_edi    = regs->tf_edi;
487213fdd80SPeter Wemm 	frame.sf_sc.sc_esi    = regs->tf_esi;
488213fdd80SPeter Wemm 	frame.sf_sc.sc_ebp    = regs->tf_ebp;
489213fdd80SPeter Wemm 	frame.sf_sc.sc_ebx    = regs->tf_ebx;
490bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_esp;
491213fdd80SPeter Wemm 	frame.sf_sc.sc_edx    = regs->tf_edx;
492213fdd80SPeter Wemm 	frame.sf_sc.sc_ecx    = regs->tf_ecx;
493213fdd80SPeter Wemm 	frame.sf_sc.sc_eax    = regs->tf_eax;
494213fdd80SPeter Wemm 	frame.sf_sc.sc_eip    = regs->tf_eip;
495213fdd80SPeter Wemm 	frame.sf_sc.sc_cs     = regs->tf_cs;
496213fdd80SPeter Wemm 	frame.sf_sc.sc_eflags = regs->tf_eflags;
497213fdd80SPeter Wemm 	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
498213fdd80SPeter Wemm 	frame.sf_sc.sc_ss     = regs->tf_ss;
499213fdd80SPeter Wemm 	frame.sf_sc.sc_err    = regs->tf_err;
50096a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
5019104847fSDavid Xu 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
502cc6ca9b3SMarcel Moolenaar 
503d66a5066SPeter Wemm 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
504d66a5066SPeter Wemm 		/*
505d66a5066SPeter Wemm 		 * Process has trashed its stack; give it an illegal
506d66a5066SPeter Wemm 		 * instruction to halt it in its tracks.
507d66a5066SPeter Wemm 		 */
50819eb87d2SJohn Baldwin 		PROC_LOCK(p);
509b40ce416SJulian Elischer 		sigexit(td, SIGILL);
510d66a5066SPeter Wemm 	}
511d66a5066SPeter Wemm 
5124ba25759SEd Maste 	/* Build context to run handler in. */
51321f24617SDmitry Chagin 	regs->tf_esp = PTROUT(fp);
5148f9635dcSDmitry Chagin 	regs->tf_eip = linux_vdso_sigcode;
51521f24617SDmitry Chagin 	regs->tf_edi = PTROUT(catcher);
51622eca0bfSKonstantin Belousov 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
517213fdd80SPeter Wemm 	regs->tf_cs = _ucodesel;
518213fdd80SPeter Wemm 	regs->tf_ds = _udatasel;
519213fdd80SPeter Wemm 	regs->tf_es = _udatasel;
5205206bca1SLuoqi Chen 	regs->tf_fs = _udatasel;
521213fdd80SPeter Wemm 	regs->tf_ss = _udatasel;
5225002a60fSMarcel Moolenaar 	PROC_LOCK(p);
52390af4afaSJohn Baldwin 	mtx_lock(&psp->ps_mtx);
524d66a5066SPeter Wemm }
525d66a5066SPeter Wemm 
526d66a5066SPeter Wemm /*
527d66a5066SPeter Wemm  * System call to cleanup state after a signal
528d66a5066SPeter Wemm  * has been taken.  Reset signal mask and
529d66a5066SPeter Wemm  * stack state from context left by sendsig (above).
530d66a5066SPeter Wemm  * Return to previous pc and psl as specified by
531d66a5066SPeter Wemm  * context left by sendsig. Check carefully to
532d66a5066SPeter Wemm  * make sure that the user has not modified the
533d66a5066SPeter Wemm  * psl to gain improper privileges or to cause
534d66a5066SPeter Wemm  * a machine fault.
535d66a5066SPeter Wemm  */
536d66a5066SPeter Wemm int
537b07cd97eSMark Murray linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
538d66a5066SPeter Wemm {
5395002a60fSMarcel Moolenaar 	struct l_sigframe frame;
5401d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
5414ab7403bSDmitry Chagin 	int eflags;
5429104847fSDavid Xu 	ksiginfo_t ksi;
543d66a5066SPeter Wemm 
544b40ce416SJulian Elischer 	regs = td->td_frame;
545d66a5066SPeter Wemm 
546d66a5066SPeter Wemm 	/*
547cc6ca9b3SMarcel Moolenaar 	 * The trampoline code hands us the sigframe.
548d66a5066SPeter Wemm 	 * It is unsafe to keep track of it ourselves, in the event that a
549d66a5066SPeter Wemm 	 * program jumps out of a signal handler.
550d66a5066SPeter Wemm 	 */
5514b7ef73dSDag-Erling Smørgrav 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
552d66a5066SPeter Wemm 		return (EFAULT);
553d66a5066SPeter Wemm 
5544ba25759SEd Maste 	/* Check for security violations. */
555d66a5066SPeter Wemm #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
556cc6ca9b3SMarcel Moolenaar 	eflags = frame.sf_sc.sc_eflags;
5573d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
558d66a5066SPeter Wemm 		return (EINVAL);
559d66a5066SPeter Wemm 
560d66a5066SPeter Wemm 	/*
561d66a5066SPeter Wemm 	 * Don't allow users to load a valid privileged %cs.  Let the
562d66a5066SPeter Wemm 	 * hardware check for invalid selectors, excess privilege in
563d66a5066SPeter Wemm 	 * other selectors, invalid %eip's and invalid %esp's.
564d66a5066SPeter Wemm 	 */
56540d50994SPhilippe Charnier #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
566cc6ca9b3SMarcel Moolenaar 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
5679104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5689104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5699104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5709104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5719104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_eip;
5729104847fSDavid Xu 		trapsignal(td, &ksi);
573d66a5066SPeter Wemm 		return (EINVAL);
574d66a5066SPeter Wemm 	}
575d66a5066SPeter Wemm 
576*4a6c2d07SDmitry Chagin 	kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
577956d3333SMarcel Moolenaar 
5784ba25759SEd Maste 	/* Restore signal context. */
5795206bca1SLuoqi Chen 	/* %gs was restored by the trampoline. */
580cc6ca9b3SMarcel Moolenaar 	regs->tf_fs     = frame.sf_sc.sc_fs;
581cc6ca9b3SMarcel Moolenaar 	regs->tf_es     = frame.sf_sc.sc_es;
582cc6ca9b3SMarcel Moolenaar 	regs->tf_ds     = frame.sf_sc.sc_ds;
583cc6ca9b3SMarcel Moolenaar 	regs->tf_edi    = frame.sf_sc.sc_edi;
584cc6ca9b3SMarcel Moolenaar 	regs->tf_esi    = frame.sf_sc.sc_esi;
585cc6ca9b3SMarcel Moolenaar 	regs->tf_ebp    = frame.sf_sc.sc_ebp;
586cc6ca9b3SMarcel Moolenaar 	regs->tf_ebx    = frame.sf_sc.sc_ebx;
587cc6ca9b3SMarcel Moolenaar 	regs->tf_edx    = frame.sf_sc.sc_edx;
588cc6ca9b3SMarcel Moolenaar 	regs->tf_ecx    = frame.sf_sc.sc_ecx;
589cc6ca9b3SMarcel Moolenaar 	regs->tf_eax    = frame.sf_sc.sc_eax;
590cc6ca9b3SMarcel Moolenaar 	regs->tf_eip    = frame.sf_sc.sc_eip;
591cc6ca9b3SMarcel Moolenaar 	regs->tf_cs     = frame.sf_sc.sc_cs;
592213fdd80SPeter Wemm 	regs->tf_eflags = eflags;
593cc6ca9b3SMarcel Moolenaar 	regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
594cc6ca9b3SMarcel Moolenaar 	regs->tf_ss     = frame.sf_sc.sc_ss;
595d66a5066SPeter Wemm 
596d66a5066SPeter Wemm 	return (EJUSTRETURN);
597d66a5066SPeter Wemm }
598d66a5066SPeter Wemm 
59979363394SAndrew Gallatin /*
60079363394SAndrew Gallatin  * System call to cleanup state after a signal
60179363394SAndrew Gallatin  * has been taken.  Reset signal mask and
60279363394SAndrew Gallatin  * stack state from context left by rt_sendsig (above).
60379363394SAndrew Gallatin  * Return to previous pc and psl as specified by
60479363394SAndrew Gallatin  * context left by sendsig. Check carefully to
60579363394SAndrew Gallatin  * make sure that the user has not modified the
60679363394SAndrew Gallatin  * psl to gain improper privileges or to cause
60779363394SAndrew Gallatin  * a machine fault.
60879363394SAndrew Gallatin  */
60979363394SAndrew Gallatin int
610b07cd97eSMark Murray linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
61179363394SAndrew Gallatin {
6125002a60fSMarcel Moolenaar 	struct l_ucontext uc;
6135002a60fSMarcel Moolenaar 	struct l_sigcontext *context;
614d6e029adSKonstantin Belousov 	sigset_t bmask;
6155002a60fSMarcel Moolenaar 	l_stack_t *lss;
616206a5d3aSIan Dowse 	stack_t ss;
6171d062e2bSDag-Erling Smørgrav 	struct trapframe *regs;
61879363394SAndrew Gallatin 	int eflags;
6199104847fSDavid Xu 	ksiginfo_t ksi;
62079363394SAndrew Gallatin 
621b40ce416SJulian Elischer 	regs = td->td_frame;
62279363394SAndrew Gallatin 
62379363394SAndrew Gallatin 	/*
624cc6ca9b3SMarcel Moolenaar 	 * The trampoline code hands us the ucontext.
62579363394SAndrew Gallatin 	 * It is unsafe to keep track of it ourselves, in the event that a
62679363394SAndrew Gallatin 	 * program jumps out of a signal handler.
62779363394SAndrew Gallatin 	 */
6284b7ef73dSDag-Erling Smørgrav 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
62979363394SAndrew Gallatin 		return (EFAULT);
63079363394SAndrew Gallatin 
63179363394SAndrew Gallatin 	context = &uc.uc_mcontext;
63279363394SAndrew Gallatin 
6334ba25759SEd Maste 	/* Check for security violations. */
63479363394SAndrew Gallatin #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
63579363394SAndrew Gallatin 	eflags = context->sc_eflags;
6363d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
63779363394SAndrew Gallatin 		return (EINVAL);
63879363394SAndrew Gallatin 
63979363394SAndrew Gallatin 	/*
64079363394SAndrew Gallatin 	 * Don't allow users to load a valid privileged %cs.  Let the
64179363394SAndrew Gallatin 	 * hardware check for invalid selectors, excess privilege in
64279363394SAndrew Gallatin 	 * other selectors, invalid %eip's and invalid %esp's.
64379363394SAndrew Gallatin 	 */
64479363394SAndrew Gallatin #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
64579363394SAndrew Gallatin 	if (!CS_SECURE(context->sc_cs)) {
6469104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
6479104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
6489104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
6499104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
6509104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_eip;
6519104847fSDavid Xu 		trapsignal(td, &ksi);
65279363394SAndrew Gallatin 		return (EINVAL);
65379363394SAndrew Gallatin 	}
65479363394SAndrew Gallatin 
655d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
656d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
65779363394SAndrew Gallatin 
6584ba25759SEd Maste 	/* Restore signal context. */
65979363394SAndrew Gallatin 	/* %gs was restored by the trampoline. */
66079363394SAndrew Gallatin 	regs->tf_fs     = context->sc_fs;
66179363394SAndrew Gallatin 	regs->tf_es     = context->sc_es;
66279363394SAndrew Gallatin 	regs->tf_ds     = context->sc_ds;
66379363394SAndrew Gallatin 	regs->tf_edi    = context->sc_edi;
66479363394SAndrew Gallatin 	regs->tf_esi    = context->sc_esi;
66579363394SAndrew Gallatin 	regs->tf_ebp    = context->sc_ebp;
66679363394SAndrew Gallatin 	regs->tf_ebx    = context->sc_ebx;
66779363394SAndrew Gallatin 	regs->tf_edx    = context->sc_edx;
66879363394SAndrew Gallatin 	regs->tf_ecx    = context->sc_ecx;
66979363394SAndrew Gallatin 	regs->tf_eax    = context->sc_eax;
67079363394SAndrew Gallatin 	regs->tf_eip    = context->sc_eip;
67179363394SAndrew Gallatin 	regs->tf_cs     = context->sc_cs;
67279363394SAndrew Gallatin 	regs->tf_eflags = eflags;
67379363394SAndrew Gallatin 	regs->tf_esp    = context->sc_esp_at_signal;
67479363394SAndrew Gallatin 	regs->tf_ss     = context->sc_ss;
67579363394SAndrew Gallatin 
6764ba25759SEd Maste 	/* Call sigaltstack & ignore results. */
67779363394SAndrew Gallatin 	lss = &uc.uc_stack;
67830c6d982SEdward Tomasz Napierala 	ss.ss_sp = PTRIN(lss->ss_sp);
679206a5d3aSIan Dowse 	ss.ss_size = lss->ss_size;
680206a5d3aSIan Dowse 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
68179363394SAndrew Gallatin 
682206a5d3aSIan Dowse 	(void)kern_sigaltstack(td, &ss, NULL);
68379363394SAndrew Gallatin 
68479363394SAndrew Gallatin 	return (EJUSTRETURN);
68579363394SAndrew Gallatin }
68679363394SAndrew Gallatin 
687afe1a688SKonstantin Belousov static int
6882d88da2fSKonstantin Belousov linux_fetch_syscall_args(struct thread *td)
689d66a5066SPeter Wemm {
690afe1a688SKonstantin Belousov 	struct proc *p;
691afe1a688SKonstantin Belousov 	struct trapframe *frame;
6922d88da2fSKonstantin Belousov 	struct syscall_args *sa;
693afe1a688SKonstantin Belousov 
694afe1a688SKonstantin Belousov 	p = td->td_proc;
695afe1a688SKonstantin Belousov 	frame = td->td_frame;
6962d88da2fSKonstantin Belousov 	sa = &td->td_sa;
697afe1a688SKonstantin Belousov 
698afe1a688SKonstantin Belousov 	sa->code = frame->tf_eax;
699cf98bc28SDavid Chisnall 	sa->original_code = sa->code;
700afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_ebx;
701afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_ecx;
702afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_edx;
703afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_esi;
704afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_edi;
705afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_ebp;	/* Unconfirmed */
706afe1a688SKonstantin Belousov 
707afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
708fcdffc03SDmitry Chagin 		/* nosys */
7091b8d0393SConrad Meyer 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
710afe1a688SKonstantin Belousov 	else
711afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
712afe1a688SKonstantin Belousov 
713afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
714afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_edx;
715afe1a688SKonstantin Belousov 
716afe1a688SKonstantin Belousov 	return (0);
717d66a5066SPeter Wemm }
718d66a5066SPeter Wemm 
719c26391f4SEdward Tomasz Napierala static void
720c26391f4SEdward Tomasz Napierala linux_set_syscall_retval(struct thread *td, int error)
721c26391f4SEdward Tomasz Napierala {
722c26391f4SEdward Tomasz Napierala 	struct trapframe *frame = td->td_frame;
723c26391f4SEdward Tomasz Napierala 
724c26391f4SEdward Tomasz Napierala 	cpu_set_syscall_retval(td, error);
725c26391f4SEdward Tomasz Napierala 
726c26391f4SEdward Tomasz Napierala 	if (__predict_false(error != 0)) {
727c26391f4SEdward Tomasz Napierala 		if (error != ERESTART && error != EJUSTRETURN)
728866b1f51SEdward Tomasz Napierala 			frame->tf_eax = bsd_to_linux_errno(error);
729c26391f4SEdward Tomasz Napierala 	}
730c26391f4SEdward Tomasz Napierala }
731c26391f4SEdward Tomasz Napierala 
732de8374dfSDmitry Chagin static void
733de8374dfSDmitry Chagin linux_set_fork_retval(struct thread *td)
734de8374dfSDmitry Chagin {
735de8374dfSDmitry Chagin 	struct trapframe *frame = td->td_frame;
736de8374dfSDmitry Chagin 
737de8374dfSDmitry Chagin 	frame->tf_eax = 0;
738de8374dfSDmitry Chagin }
739de8374dfSDmitry Chagin 
740d323ddf3SMatthew Dillon /*
741598d45beSMatthew N. Dodd  * exec_setregs may initialize some registers differently than Linux
742598d45beSMatthew N. Dodd  * does, thus potentially confusing Linux binaries. If necessary, we
743598d45beSMatthew N. Dodd  * override the exec_setregs default(s) here.
744598d45beSMatthew N. Dodd  */
745598d45beSMatthew N. Dodd static void
74631174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
74731174518SJohn Baldwin     uintptr_t stack)
748598d45beSMatthew N. Dodd {
749598d45beSMatthew N. Dodd 	struct pcb *pcb = td->td_pcb;
750598d45beSMatthew N. Dodd 
751a107d8aaSNathan Whitehorn 	exec_setregs(td, imgp, stack);
752598d45beSMatthew N. Dodd 
7534ba25759SEd Maste 	/* Linux sets %gs to 0, we default to _udatasel. */
7542ee8325fSJohn Baldwin 	pcb->pcb_gs = 0;
7552ee8325fSJohn Baldwin 	load_gs(0);
7562a51b9b0SDavid Schultz 
7572ee8325fSJohn Baldwin 	pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
758598d45beSMatthew N. Dodd }
759598d45beSMatthew N. Dodd 
760d66a5066SPeter Wemm struct sysentvec linux_sysvec = {
761a8d403e1SKonstantin Belousov 	.sv_size	= LINUX_SYS_MAXSYSCALL,
762a8d403e1SKonstantin Belousov 	.sv_table	= linux_sysent,
763a8d403e1SKonstantin Belousov 	.sv_fixup	= linux_fixup,
764a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
7659931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
766a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
767a8d403e1SKonstantin Belousov 	.sv_name	= "Linux a.out",
768a8d403e1SKonstantin Belousov 	.sv_coredump	= NULL,
769dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
770a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
771a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
772a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
7738f1e49a6SDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK,
774a8d403e1SKonstantin Belousov 	.sv_psstrings	= PS_STRINGS,
7753fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
776a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
777a8d403e1SKonstantin Belousov 	.sv_copyout_strings = exec_copyout_strings,
778dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
779a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
780b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
781870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
782870e197dSKonstantin Belousov 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
783c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux_set_syscall_retval,
784afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
785afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
786e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
78781338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
788038c7205SDmitry Chagin 	.sv_trap	= NULL,
7895fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
7904815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
7914815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
792598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
793de8374dfSDmitry Chagin 	.sv_set_fork_retval = linux_set_fork_retval,
794d66a5066SPeter Wemm };
7958f1e49a6SDmitry Chagin INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
796e1743d02SSøren Schmidt 
797e1743d02SSøren Schmidt struct sysentvec elf_linux_sysvec = {
798a8d403e1SKonstantin Belousov 	.sv_size	= LINUX_SYS_MAXSYSCALL,
799a8d403e1SKonstantin Belousov 	.sv_table	= linux_sysent,
800dc858467SEd Maste 	.sv_fixup	= linux_fixup_elf,
801a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
8029931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
803a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
8045faeda90SJessica Clarke 	.sv_name	= "Linux ELF32",
805a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
806435754a5SEdward Tomasz Napierala 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
807435754a5SEdward Tomasz Napierala 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
808435754a5SEdward Tomasz Napierala 	.sv_elf_core_prepare_notes = elf32_prepare_notes,
809dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
810a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
811a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
812a8d403e1SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
8138f1e49a6SDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK,
8148f1e49a6SDmitry Chagin 	.sv_psstrings	= LINUX_PS_STRINGS,
8153fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
816a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
8175caa67faSJohn Baldwin 	.sv_copyout_auxargs = linux_copyout_auxargs,
8184d7c2e8aSDmitry Chagin 	.sv_copyout_strings = linux_copyout_strings,
819dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
820a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
821b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
822870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
8239931033bSDmitry Chagin 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
824c26391f4SEdward Tomasz Napierala 	.sv_set_syscall_retval = linux_set_syscall_retval,
825afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
826afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
8278f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX_SHAREDPAGE,
8288f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
829e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
83081338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
831038c7205SDmitry Chagin 	.sv_trap	= NULL,
8325fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
8334815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
8344815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
835598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
836de8374dfSDmitry Chagin 	.sv_set_fork_retval = linux_set_fork_retval,
837e1743d02SSøren Schmidt };
838bdc37934SDmitry Chagin 
8395fd9cd53SDmitry Chagin static int
8405fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
8415fd9cd53SDmitry Chagin {
8429931033bSDmitry Chagin 	int error = 0;
8435fd9cd53SDmitry Chagin 
8449931033bSDmitry Chagin 	if (SV_PROC_FLAG(p, SV_SHP) != 0)
8459931033bSDmitry Chagin 		error = linux_map_vdso(p, linux_vdso_obj,
8469931033bSDmitry Chagin 		    linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
8479931033bSDmitry Chagin 	if (error == 0)
8485fd9cd53SDmitry Chagin 		linux_on_exec(p, imgp);
8499931033bSDmitry Chagin 	return (error);
8505fd9cd53SDmitry Chagin }
8515fd9cd53SDmitry Chagin 
85209cffde9SDmitry Chagin /*
85309cffde9SDmitry Chagin  * linux_vdso_install() and linux_exec_sysvec_init() must be called
85409cffde9SDmitry Chagin  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
85509cffde9SDmitry Chagin  */
856bdc37934SDmitry Chagin static void
8579931033bSDmitry Chagin linux_exec_sysvec_init(void *param)
8589931033bSDmitry Chagin {
8599931033bSDmitry Chagin 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
8609931033bSDmitry Chagin 	struct sysentvec *sv;
8619931033bSDmitry Chagin 	ptrdiff_t tkoff;
8629931033bSDmitry Chagin 
8639931033bSDmitry Chagin 	sv = param;
8649931033bSDmitry Chagin 	/* Fill timekeep_base */
8659931033bSDmitry Chagin 	exec_sysvec_init(sv);
8669931033bSDmitry Chagin 
8679931033bSDmitry Chagin 	tkoff = kern_timekeep_base - linux_vdso_base;
8689931033bSDmitry Chagin 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
8699931033bSDmitry Chagin 	*ktimekeep_base = sv->sv_timekeep_base;
8709931033bSDmitry Chagin 
8719931033bSDmitry Chagin 	tkoff = kern_tsc_selector - linux_vdso_base;
8729931033bSDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
8739931033bSDmitry Chagin 	*ktsc_selector = linux_vdso_tsc_selector_idx();
8749931033bSDmitry Chagin 	if (bootverbose)
8759931033bSDmitry Chagin 		printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
8765a6a4fb2SDmitry Chagin 
8775a6a4fb2SDmitry Chagin 	tkoff = kern_cpu_selector - linux_vdso_base;
8785a6a4fb2SDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
8795a6a4fb2SDmitry Chagin 	*ktsc_selector = linux_vdso_cpu_selector_idx();
8805a6a4fb2SDmitry Chagin 	if (bootverbose)
8815a6a4fb2SDmitry Chagin 		printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
8829931033bSDmitry Chagin }
88309cffde9SDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
8849931033bSDmitry Chagin     linux_exec_sysvec_init, &elf_linux_sysvec);
8859931033bSDmitry Chagin 
8869931033bSDmitry Chagin static void
887a543556cSDmitry Chagin linux_vdso_install(const void *param)
888bdc37934SDmitry Chagin {
8899931033bSDmitry Chagin 	char *vdso_start = &_binary_linux_vdso_so_o_start;
8909931033bSDmitry Chagin 	char *vdso_end = &_binary_linux_vdso_so_o_end;
891bdc37934SDmitry Chagin 
8929931033bSDmitry Chagin 	linux_szsigcode = vdso_end - vdso_start;
8939931033bSDmitry Chagin 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
894bdc37934SDmitry Chagin 
8959931033bSDmitry Chagin 	linux_vdso_base = LINUX_VDSOPAGE;
896bdc37934SDmitry Chagin 
8979931033bSDmitry Chagin 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
898bdc37934SDmitry Chagin 
8999931033bSDmitry Chagin 	linux_vdso_obj = __elfN(linux_shared_page_init)
9009931033bSDmitry Chagin 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
9019931033bSDmitry Chagin 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
902bdc37934SDmitry Chagin 
9039931033bSDmitry Chagin 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
904bdc37934SDmitry Chagin }
90509cffde9SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
906f8268d4dSEd Maste     linux_vdso_install, NULL);
907bdc37934SDmitry Chagin 
908bdc37934SDmitry Chagin static void
909a543556cSDmitry Chagin linux_vdso_deinstall(const void *param)
910bdc37934SDmitry Chagin {
911bdc37934SDmitry Chagin 
9129931033bSDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
9139931033bSDmitry Chagin 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
9147b194b3dSEd Maste }
915bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
916f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
917e1743d02SSøren Schmidt 
9189931033bSDmitry Chagin static void
9199931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset)
9209931033bSDmitry Chagin {
9219931033bSDmitry Chagin 	const Elf_Shdr *shdr;
9229931033bSDmitry Chagin 	const Elf_Rel *rel;
9239931033bSDmitry Chagin 	const Elf_Ehdr *ehdr;
9249931033bSDmitry Chagin 	Elf_Addr *where;
9259931033bSDmitry Chagin 	Elf_Size rtype, symidx;
9269931033bSDmitry Chagin 	Elf_Addr addr, addend;
9279931033bSDmitry Chagin 	int i, relcnt;
9289931033bSDmitry Chagin 
9299931033bSDmitry Chagin 	MPASS(offset != 0);
9309931033bSDmitry Chagin 
9319931033bSDmitry Chagin 	relcnt = 0;
9329931033bSDmitry Chagin 	ehdr = (const Elf_Ehdr *)mapping;
9339931033bSDmitry Chagin 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
9349931033bSDmitry Chagin 	for (i = 0; i < ehdr->e_shnum; i++)
9359931033bSDmitry Chagin 	{
9369931033bSDmitry Chagin 		switch (shdr[i].sh_type) {
9379931033bSDmitry Chagin 		case SHT_REL:
9389931033bSDmitry Chagin 			rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
9399931033bSDmitry Chagin 			relcnt = shdr[i].sh_size / sizeof(*rel);
9409931033bSDmitry Chagin 			break;
9419931033bSDmitry Chagin 		case SHT_RELA:
9429931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected Rela section\n");
9439931033bSDmitry Chagin 			break;
9449931033bSDmitry Chagin 		}
9459931033bSDmitry Chagin 	}
9469931033bSDmitry Chagin 
9479931033bSDmitry Chagin 	for (i = 0; i < relcnt; i++, rel++) {
9489931033bSDmitry Chagin 		where = (Elf_Addr *)(mapping + rel->r_offset);
9499931033bSDmitry Chagin 		addend = *where;
9509931033bSDmitry Chagin 		rtype = ELF_R_TYPE(rel->r_info);
9519931033bSDmitry Chagin 		symidx = ELF_R_SYM(rel->r_info);
9529931033bSDmitry Chagin 
9539931033bSDmitry Chagin 		switch (rtype) {
9549931033bSDmitry Chagin 		case R_386_NONE:	/* none */
9559931033bSDmitry Chagin 			break;
9569931033bSDmitry Chagin 
9579931033bSDmitry Chagin 		case R_386_RELATIVE:	/* B + A */
9589931033bSDmitry Chagin 			addr = (Elf_Addr)PTROUT(offset + addend);
9599931033bSDmitry Chagin 			if (*where != addr)
9609931033bSDmitry Chagin 				*where = addr;
9619931033bSDmitry Chagin 			break;
9629931033bSDmitry Chagin 
9639931033bSDmitry Chagin 		case R_386_IRELATIVE:
9649931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected ifunc relocation, "
9659931033bSDmitry Chagin 			    "symbol index %d\n", symidx);
9669931033bSDmitry Chagin 			break;
9679931033bSDmitry Chagin 		default:
9689931033bSDmitry Chagin 			printf("Linux i386 vDSO: unexpected relocation type %d, "
9699931033bSDmitry Chagin 			    "symbol index %d\n", rtype, symidx);
9709931033bSDmitry Chagin 		}
9719931033bSDmitry Chagin 	}
9729931033bSDmitry Chagin }
9739931033bSDmitry Chagin 
97489ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
97589ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
97689ffc202SBjoern A. Zeeb 
977a95659f7SEd Maste static bool
97889ffc202SBjoern A. Zeeb linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
97989ffc202SBjoern A. Zeeb {
98089ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
98189ffc202SBjoern A. Zeeb 	uintptr_t p;
98289ffc202SBjoern A. Zeeb 
98389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
98489ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
98589ffc202SBjoern A. Zeeb 
98689ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
98789ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
988a95659f7SEd Maste 		return (false);
98989ffc202SBjoern A. Zeeb 
99089ffc202SBjoern A. Zeeb 	/*
99135755049SChuck Tuffli 	 * For Linux we encode osrel using the Linux convention of
99235755049SChuck Tuffli 	 * 	(version << 16) | (major << 8) | (minor)
99335755049SChuck Tuffli 	 * See macro in linux_mib.h
99489ffc202SBjoern A. Zeeb 	 */
99535755049SChuck Tuffli 	*osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
99689ffc202SBjoern A. Zeeb 
997a95659f7SEd Maste 	return (true);
99889ffc202SBjoern A. Zeeb }
99932c01de2SDmitry Chagin 
100032c01de2SDmitry Chagin static Elf_Brandnote linux_brandnote = {
100189ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
100289ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
100332c01de2SDmitry Chagin 	.hdr.n_type	= 1,
100489ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
100589ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
100689ffc202SBjoern A. Zeeb 	.trans_osrel	= linux_trans_osrel
100732c01de2SDmitry Chagin };
100832c01de2SDmitry Chagin 
1009514058dcSAlexander Langer static Elf32_Brandinfo linux_brand = {
1010a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1011a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1012a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1013b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1014a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
1015a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1016a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
101732c01de2SDmitry Chagin 	.brand_note	= &linux_brandnote,
10182dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
10195cf588ebSPeter Wemm };
10205cf588ebSPeter Wemm 
1021514058dcSAlexander Langer static Elf32_Brandinfo linux_glibc2brand = {
1022a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1023a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1024a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1025b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1026a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1027a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1028a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
102932c01de2SDmitry Chagin 	.brand_note	= &linux_brandnote,
10302dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
10314e138a28SMike Smith };
10324e138a28SMike Smith 
1033a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1034a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1035a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1036a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1037b5f20658SEdward Tomasz Napierala 	.emul_path	= linux_emul_path,
1038a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1039a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1040a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1041a0c59c7aSDmitry Chagin 	.brand_note	= &linux_brandnote,
1042cf8d74e3SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1043cf8d74e3SDmitry Chagin 			    LINUX_BI_FUTEX_REQUEUE
1044a0c59c7aSDmitry Chagin };
1045a0c59c7aSDmitry Chagin 
1046514058dcSAlexander Langer Elf32_Brandinfo *linux_brandlist[] = {
1047514058dcSAlexander Langer 	&linux_brand,
1048514058dcSAlexander Langer 	&linux_glibc2brand,
1049a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1050514058dcSAlexander Langer 	NULL
1051514058dcSAlexander Langer };
1052514058dcSAlexander Langer 
1053aa855a59SPeter Wemm static int
1054c25ded31SBruce Evans linux_elf_modevent(module_t mod, int type, void *data)
1055d30ea4f5SPeter Wemm {
1056514058dcSAlexander Langer 	Elf32_Brandinfo **brandinfo;
1057514058dcSAlexander Langer 	int error;
1058f41325dbSPeter Wemm 	struct linux_ioctl_handler **lihp;
1059514058dcSAlexander Langer 
1060514058dcSAlexander Langer 	error = 0;
1061514058dcSAlexander Langer 
1062aa855a59SPeter Wemm 	switch(type) {
1063aa855a59SPeter Wemm 	case MOD_LOAD:
1064aa855a59SPeter Wemm 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1065aa855a59SPeter Wemm 		     ++brandinfo)
10663ebc1248SPeter Wemm 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1067aa855a59SPeter Wemm 				error = EINVAL;
1068466b14d7SMarcel Moolenaar 		if (error == 0) {
1069f41325dbSPeter Wemm 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1070f41325dbSPeter Wemm 				linux_ioctl_register_handler(*lihp);
1071044ab55eSEdward Tomasz Napierala 			linux_dev_shm_create();
10727ae27ff4SJamie Gritton 			linux_osd_jail_register();
10731ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
107443bef515SMarcel Moolenaar 			if (bootverbose)
1075466b14d7SMarcel Moolenaar 				printf("Linux ELF exec handler installed\n");
1076466b14d7SMarcel Moolenaar 		} else
1077466b14d7SMarcel Moolenaar 			printf("cannot insert Linux ELF brand handler\n");
1078aa855a59SPeter Wemm 		break;
1079aa855a59SPeter Wemm 	case MOD_UNLOAD:
1080aa855a59SPeter Wemm 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1081aa855a59SPeter Wemm 		     ++brandinfo)
10823ebc1248SPeter Wemm 			if (elf32_brand_inuse(*brandinfo))
1083d2758342SMark Newton 				error = EBUSY;
1084d2758342SMark Newton 		if (error == 0) {
1085d2758342SMark Newton 			for (brandinfo = &linux_brandlist[0];
1086d2758342SMark Newton 			     *brandinfo != NULL; ++brandinfo)
10873ebc1248SPeter Wemm 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1088aa855a59SPeter Wemm 					error = EINVAL;
1089d2758342SMark Newton 		}
1090466b14d7SMarcel Moolenaar 		if (error == 0) {
1091f41325dbSPeter Wemm 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1092f41325dbSPeter Wemm 				linux_ioctl_unregister_handler(*lihp);
1093044ab55eSEdward Tomasz Napierala 			linux_dev_shm_destroy();
10947ae27ff4SJamie Gritton 			linux_osd_jail_deregister();
1095466b14d7SMarcel Moolenaar 			if (bootverbose)
1096466b14d7SMarcel Moolenaar 				printf("Linux ELF exec handler removed\n");
1097466b14d7SMarcel Moolenaar 		} else
1098aa855a59SPeter Wemm 			printf("Could not deinstall ELF interpreter entry\n");
1099aa855a59SPeter Wemm 		break;
1100aa855a59SPeter Wemm 	default:
1101af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1102d30ea4f5SPeter Wemm 	}
1103af682d48SDmitry Chagin 	return (error);
1104aa855a59SPeter Wemm }
1105466b14d7SMarcel Moolenaar 
1106aa855a59SPeter Wemm static moduledata_t linux_elf_mod = {
1107aa855a59SPeter Wemm 	"linuxelf",
1108aa855a59SPeter Wemm 	linux_elf_modevent,
11099823d527SKevin Lo 	0
1110aa855a59SPeter Wemm };
1111466b14d7SMarcel Moolenaar 
111278ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1113b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
1114