xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision d41e41f9f0ba9fdd2916584215b88ef7725280d7)
1ea0fabbcSTim J. Robbins /*-
2c49761ddSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3c49761ddSPedro F. Giffuni  *
4ea0fabbcSTim J. Robbins  * Copyright (c) 2004 Tim J. Robbins
5ea0fabbcSTim J. Robbins  * Copyright (c) 2003 Peter Wemm
6ea0fabbcSTim J. Robbins  * Copyright (c) 2002 Doug Rabson
7ea0fabbcSTim J. Robbins  * Copyright (c) 1998-1999 Andrew Gallatin
89a14aa01SUlrich Spörlein  * Copyright (c) 1994-1996 Søren Schmidt
9ea0fabbcSTim J. Robbins  * All rights reserved.
10ea0fabbcSTim J. Robbins  *
11ea0fabbcSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
12ea0fabbcSTim J. Robbins  * modification, are permitted provided that the following conditions
13ea0fabbcSTim J. Robbins  * are met:
14ea0fabbcSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
15ea0fabbcSTim J. Robbins  *    notice, this list of conditions and the following disclaimer
16ea0fabbcSTim J. Robbins  *    in this position and unchanged.
17ea0fabbcSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
18ea0fabbcSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
19ea0fabbcSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
20ea0fabbcSTim J. Robbins  * 3. The name of the author may not be used to endorse or promote products
21ea0fabbcSTim J. Robbins  *    derived from this software without specific prior written permission
22ea0fabbcSTim J. Robbins  *
23ea0fabbcSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24ea0fabbcSTim J. Robbins  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25ea0fabbcSTim J. Robbins  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26ea0fabbcSTim J. Robbins  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27ea0fabbcSTim J. Robbins  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28ea0fabbcSTim J. Robbins  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29ea0fabbcSTim J. Robbins  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30ea0fabbcSTim J. Robbins  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31ea0fabbcSTim J. Robbins  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32ea0fabbcSTim J. Robbins  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33ea0fabbcSTim J. Robbins  */
34ea0fabbcSTim J. Robbins 
35ea0fabbcSTim J. Robbins #include <sys/cdefs.h>
36ea0fabbcSTim J. Robbins __FBSDID("$FreeBSD$");
37aefce619SRuslan Ermilov #include "opt_compat.h"
38ea0fabbcSTim J. Robbins 
39841c0c7eSNathan Whitehorn #ifndef COMPAT_FREEBSD32
40841c0c7eSNathan Whitehorn #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
41ea0fabbcSTim J. Robbins #endif
42ea0fabbcSTim J. Robbins 
43ea0fabbcSTim J. Robbins #define	__ELF_WORD_SIZE	32
44ea0fabbcSTim J. Robbins 
45ea0fabbcSTim J. Robbins #include <sys/param.h>
46ea0fabbcSTim J. Robbins #include <sys/systm.h>
47ea0fabbcSTim J. Robbins #include <sys/exec.h>
4848b05c3fSKonstantin Belousov #include <sys/fcntl.h>
49ea0fabbcSTim J. Robbins #include <sys/imgact.h>
50ea0fabbcSTim J. Robbins #include <sys/imgact_elf.h>
51ea0fabbcSTim J. Robbins #include <sys/kernel.h>
52ea0fabbcSTim J. Robbins #include <sys/lock.h>
53ea0fabbcSTim J. Robbins #include <sys/malloc.h>
54ea0fabbcSTim J. Robbins #include <sys/module.h>
55ea0fabbcSTim J. Robbins #include <sys/mutex.h>
56ea0fabbcSTim J. Robbins #include <sys/proc.h>
576004362eSDavid Schultz #include <sys/resourcevar.h>
58ea0fabbcSTim J. Robbins #include <sys/signalvar.h>
59ea0fabbcSTim J. Robbins #include <sys/syscallsubr.h>
60c0aa0e2cSEd Maste #include <sys/sysctl.h>
61ea0fabbcSTim J. Robbins #include <sys/sysent.h>
62ea0fabbcSTim J. Robbins #include <sys/sysproto.h>
63ea0fabbcSTim J. Robbins #include <sys/vnode.h>
647c09e6c0SAlexander Leidinger #include <sys/eventhandler.h>
65ea0fabbcSTim J. Robbins 
66ea0fabbcSTim J. Robbins #include <vm/vm.h>
67ea0fabbcSTim J. Robbins #include <vm/pmap.h>
68ea0fabbcSTim J. Robbins #include <vm/vm_extern.h>
69ea0fabbcSTim J. Robbins #include <vm/vm_map.h>
70ea0fabbcSTim J. Robbins #include <vm/vm_object.h>
71ea0fabbcSTim J. Robbins #include <vm/vm_page.h>
72ea0fabbcSTim J. Robbins #include <vm/vm_param.h>
73ea0fabbcSTim J. Robbins 
74ea0fabbcSTim J. Robbins #include <machine/cpu.h>
75ea0fabbcSTim J. Robbins #include <machine/md_var.h>
766004362eSDavid Schultz #include <machine/pcb.h>
77ea0fabbcSTim J. Robbins #include <machine/specialreg.h>
78*d41e41f9SJohn Baldwin #include <machine/trap.h>
79ea0fabbcSTim J. Robbins 
80ea0fabbcSTim J. Robbins #include <amd64/linux32/linux.h>
81ea0fabbcSTim J. Robbins #include <amd64/linux32/linux32_proto.h>
827c09e6c0SAlexander Leidinger #include <compat/linux/linux_emul.h>
83fde63162SDmitry Chagin #include <compat/linux/linux_futex.h>
84d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
85ea0fabbcSTim J. Robbins #include <compat/linux/linux_mib.h>
864d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
87ea0fabbcSTim J. Robbins #include <compat/linux/linux_signal.h>
88ea0fabbcSTim J. Robbins #include <compat/linux/linux_util.h>
89bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
90ea0fabbcSTim J. Robbins 
91ea0fabbcSTim J. Robbins MODULE_VERSION(linux, 1);
92ea0fabbcSTim J. Robbins 
93ea0fabbcSTim J. Robbins #define	AUXARGS_ENTRY_32(pos, id, val)	\
94ea0fabbcSTim J. Robbins 	do {				\
95ea0fabbcSTim J. Robbins 		suword32(pos++, id);	\
96ea0fabbcSTim J. Robbins 		suword32(pos++, val);	\
97ea0fabbcSTim J. Robbins 	} while (0)
98ea0fabbcSTim J. Robbins 
99ea0fabbcSTim J. Robbins #if BYTE_ORDER == LITTLE_ENDIAN
100ea0fabbcSTim J. Robbins #define SHELLMAGIC      0x2123 /* #! */
101ea0fabbcSTim J. Robbins #else
102ea0fabbcSTim J. Robbins #define SHELLMAGIC      0x2321
103ea0fabbcSTim J. Robbins #endif
104ea0fabbcSTim J. Robbins 
105ea0fabbcSTim J. Robbins /*
1064ba25759SEd Maste  * Allow the sendsig functions to use the ldebug() facility even though they
1074ba25759SEd Maste  * are not syscalls themselves.  Map them to syscall 0.  This is slightly less
1084ba25759SEd Maste  * bogus than using ldebug(sigreturn).
109ea0fabbcSTim J. Robbins  */
1106cea44a7SJohn Baldwin #define	LINUX32_SYS_linux_rt_sendsig	0
1116cea44a7SJohn Baldwin #define	LINUX32_SYS_linux_sendsig	0
112ea0fabbcSTim J. Robbins 
1130020bdf1SDmitry Chagin const char *linux_kplatform;
114bdc37934SDmitry Chagin static int linux_szsigcode;
115bdc37934SDmitry Chagin static vm_object_t linux_shared_page_obj;
116bdc37934SDmitry Chagin static char *linux_shared_page_mapping;
117bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_start;
118bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_end;
119ea0fabbcSTim J. Robbins 
1202f99bcceSJohn Baldwin extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
121ea0fabbcSTim J. Robbins 
122ea0fabbcSTim J. Robbins SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
123ea0fabbcSTim J. Robbins 
124dc858467SEd Maste static int	linux_fixup_elf(register_t **stack_base,
125ea0fabbcSTim J. Robbins 		    struct image_params *iparams);
126ea0fabbcSTim J. Robbins static register_t *linux_copyout_strings(struct image_params *imgp);
1279104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
128dc858467SEd Maste static int	linux_exec_imgact_try(struct image_params *iparams);
129dc858467SEd Maste static void	linux_exec_setregs(struct thread *td,
130a107d8aaSNathan Whitehorn 				   struct image_params *imgp, u_long stack);
13119059a13SJohn Baldwin static void	linux32_fixlimit(struct rlimit *rl, int which);
132a95659f7SEd Maste static bool	linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
133bdc37934SDmitry Chagin static void	linux_vdso_install(void *param);
134bdc37934SDmitry Chagin static void	linux_vdso_deinstall(void *param);
135ea0fabbcSTim J. Robbins 
136ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN  255
137ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = {
138ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 0 */
139ea0fabbcSTim J. Robbins 	6,			/* 1  T_PRIVINFLT */
140ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 2 */
141ea0fabbcSTim J. Robbins 	3,			/* 3  T_BPTFLT */
142ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 4 */
143ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 5 */
144ea0fabbcSTim J. Robbins 	16,			/* 6  T_ARITHTRAP */
145ea0fabbcSTim J. Robbins 	254,			/* 7  T_ASTFLT */
146ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 8 */
147ea0fabbcSTim J. Robbins 	13,			/* 9  T_PROTFLT */
148ea0fabbcSTim J. Robbins 	1,			/* 10 T_TRCTRAP */
149ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 11 */
150ea0fabbcSTim J. Robbins 	14,			/* 12 T_PAGEFLT */
151ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 13 */
152ea0fabbcSTim J. Robbins 	17,			/* 14 T_ALIGNFLT */
153ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 15 */
154ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 16 */
155ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 17 */
156ea0fabbcSTim J. Robbins 	0,			/* 18 T_DIVIDE */
157ea0fabbcSTim J. Robbins 	2,			/* 19 T_NMI */
158ea0fabbcSTim J. Robbins 	4,			/* 20 T_OFLOW */
159ea0fabbcSTim J. Robbins 	5,			/* 21 T_BOUND */
160ea0fabbcSTim J. Robbins 	7,			/* 22 T_DNA */
161ea0fabbcSTim J. Robbins 	8,			/* 23 T_DOUBLEFLT */
162ea0fabbcSTim J. Robbins 	9,			/* 24 T_FPOPFLT */
163ea0fabbcSTim J. Robbins 	10,			/* 25 T_TSSFLT */
164ea0fabbcSTim J. Robbins 	11,			/* 26 T_SEGNPFLT */
165ea0fabbcSTim J. Robbins 	12,			/* 27 T_STKFLT */
166ea0fabbcSTim J. Robbins 	18,			/* 28 T_MCHK */
167ea0fabbcSTim J. Robbins 	19,			/* 29 T_XMMFLT */
168ea0fabbcSTim J. Robbins 	15			/* 30 T_RESERVED */
169ea0fabbcSTim J. Robbins };
170ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \
171ea24b056SPedro F. Giffuni     ((code)<nitems(_bsd_to_linux_trapcode)? \
172ea0fabbcSTim J. Robbins      _bsd_to_linux_trapcode[(code)]: \
173ea0fabbcSTim J. Robbins      LINUX_T_UNKNOWN)
174ea0fabbcSTim J. Robbins 
175ea0fabbcSTim J. Robbins struct linux32_ps_strings {
176ea0fabbcSTim J. Robbins 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
177f2c7668eSDavid Schultz 	u_int ps_nargvstr;	/* the number of argument strings */
178ea0fabbcSTim J. Robbins 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
179f2c7668eSDavid Schultz 	u_int ps_nenvstr;	/* the number of environment strings */
180ea0fabbcSTim J. Robbins };
181ea0fabbcSTim J. Robbins 
182bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
183bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
184bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
1850020bdf1SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
186bdc37934SDmitry Chagin 
187ea0fabbcSTim J. Robbins /*
188ea0fabbcSTim J. Robbins  * If FreeBSD & Linux have a difference of opinion about what a trap
189ea0fabbcSTim J. Robbins  * means, deal with it here.
190ea0fabbcSTim J. Robbins  *
191ea0fabbcSTim J. Robbins  * MPSAFE
192ea0fabbcSTim J. Robbins  */
193ea0fabbcSTim J. Robbins static int
194dc858467SEd Maste linux_translate_traps(int signal, int trap_code)
195ea0fabbcSTim J. Robbins {
196ea0fabbcSTim J. Robbins 	if (signal != SIGBUS)
197ad448975SEd Maste 		return (signal);
198ea0fabbcSTim J. Robbins 	switch (trap_code) {
199ea0fabbcSTim J. Robbins 	case T_PROTFLT:
200ea0fabbcSTim J. Robbins 	case T_TSSFLT:
201ea0fabbcSTim J. Robbins 	case T_DOUBLEFLT:
202ea0fabbcSTim J. Robbins 	case T_PAGEFLT:
203ad448975SEd Maste 		return (SIGSEGV);
204ea0fabbcSTim J. Robbins 	default:
205ad448975SEd Maste 		return (signal);
206ea0fabbcSTim J. Robbins 	}
207ea0fabbcSTim J. Robbins }
208ea0fabbcSTim J. Robbins 
209ea0fabbcSTim J. Robbins static int
210dc858467SEd Maste linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
211ea0fabbcSTim J. Robbins {
212ea0fabbcSTim J. Robbins 	Elf32_Auxargs *args;
213ea0fabbcSTim J. Robbins 	Elf32_Addr *base;
2140020bdf1SDmitry Chagin 	Elf32_Addr *pos;
2154d7c2e8aSDmitry Chagin 	struct linux32_ps_strings *arginfo;
216669414e4SXin LI 	int issetugid;
2174d7c2e8aSDmitry Chagin 
2184d7c2e8aSDmitry Chagin 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
219ea0fabbcSTim J. Robbins 
2206617724cSJeff Roberson 	KASSERT(curthread->td_proc == imgp->proc,
221dc858467SEd Maste 	    ("unsafe linux_fixup_elf(), should be curproc"));
222ea0fabbcSTim J. Robbins 	base = (Elf32_Addr *)*stack_base;
223ea0fabbcSTim J. Robbins 	args = (Elf32_Auxargs *)imgp->auxargs;
224610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
225ea0fabbcSTim J. Robbins 
226669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
227bdc37934SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO_EHDR,
228bdc37934SDmitry Chagin 	    imgp->proc->p_sysent->sv_shared_page_base);
229bdc37934SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
2304d7c2e8aSDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_HWCAP, cpu_feature);
2318d30f381SDmitry Chagin 
2328d30f381SDmitry Chagin 	/*
2338d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
2348d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
2358d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
2368d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
2378d30f381SDmitry Chagin 	 * is not present.
2388d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
2398d30f381SDmitry Chagin 	 */
2408d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
2411ca16454SDmitry Chagin 		AUXARGS_ENTRY_32(pos, LINUX_AT_CLKTCK, stclohz);
242ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr);
243ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent);
244ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum);
245ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz);
246ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags);
247ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry);
248ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_BASE, args->base);
249669414e4SXin LI 	AUXARGS_ENTRY_32(pos, LINUX_AT_SECURE, issetugid);
250ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
251ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
252ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
253ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
2540020bdf1SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
2554048f59cSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
2564048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
2574048f59cSDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
2584d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
2594d7c2e8aSDmitry Chagin 		AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd);
260ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_NULL, 0);
261ea0fabbcSTim J. Robbins 
262ea0fabbcSTim J. Robbins 	free(imgp->auxargs, M_TEMP);
263ea0fabbcSTim J. Robbins 	imgp->auxargs = NULL;
264ea0fabbcSTim J. Robbins 
265ea0fabbcSTim J. Robbins 	base--;
266610ecfe0SMaxim Sobolev 	suword32(base, (uint32_t)imgp->args->argc);
267ea0fabbcSTim J. Robbins 	*stack_base = (register_t *)base;
268af682d48SDmitry Chagin 	return (0);
269ea0fabbcSTim J. Robbins }
270ea0fabbcSTim J. Robbins 
271ea0fabbcSTim J. Robbins static void
2729104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
273ea0fabbcSTim J. Robbins {
274ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
275ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
276ea0fabbcSTim J. Robbins 	struct sigacts *psp;
277ea0fabbcSTim J. Robbins 	struct trapframe *regs;
278ea0fabbcSTim J. Robbins 	struct l_rt_sigframe *fp, frame;
279ea0fabbcSTim J. Robbins 	int oonstack;
2809104847fSDavid Xu 	int sig;
2819104847fSDavid Xu 	int code;
282ea0fabbcSTim J. Robbins 
2839104847fSDavid Xu 	sig = ksi->ksi_signo;
2849104847fSDavid Xu 	code = ksi->ksi_code;
285ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
286ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
287ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
288ea0fabbcSTim J. Robbins 	regs = td->td_frame;
289ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
290ea0fabbcSTim J. Robbins 
291ea0fabbcSTim J. Robbins #ifdef DEBUG
292ea0fabbcSTim J. Robbins 	if (ldebug(rt_sendsig))
293728ef954SJohn Baldwin 		printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
294ea0fabbcSTim J. Robbins 		    catcher, sig, (void*)mask, code);
295ea0fabbcSTim J. Robbins #endif
2964ba25759SEd Maste 	/* Allocate space for the signal handler context. */
297ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
298ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
299aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
300ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
301ea0fabbcSTim J. Robbins 	} else
302ea0fabbcSTim J. Robbins 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
303ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
304ea0fabbcSTim J. Robbins 
3054ba25759SEd Maste 	/* Build the argument list for the signal handler. */
3064ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
307ea0fabbcSTim J. Robbins 
308ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
309ea0fabbcSTim J. Robbins 
310ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
311ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
312ea0fabbcSTim J. Robbins 	frame.sf_siginfo = PTROUT(&fp->sf_si);
313ea0fabbcSTim J. Robbins 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
314ea0fabbcSTim J. Robbins 
3154ba25759SEd Maste 	/* Fill in POSIX parts. */
316aa8b2011SKonstantin Belousov 	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
317ea0fabbcSTim J. Robbins 
318ea0fabbcSTim J. Robbins 	/*
3194ba25759SEd Maste 	 * Build the signal context to be used by sigreturn and libgcc unwind.
320ea0fabbcSTim J. Robbins 	 */
321ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
322ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
323ea0fabbcSTim J. Robbins 
324ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
325ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
326ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
327ea0fabbcSTim J. Robbins 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
328ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
329ea0fabbcSTim J. Robbins 
330ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
331ea0fabbcSTim J. Robbins 
3324ab7403bSDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
333ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
334ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
335ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
336ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
337bdc37934SDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
338ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
339ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
340ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
341ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
342ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
3432c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
3442c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
3452c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
3462c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
347ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
348ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
349ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
350ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
35196a2b635SKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
352ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
353ea0fabbcSTim J. Robbins 
354ea0fabbcSTim J. Robbins #ifdef DEBUG
355ea0fabbcSTim J. Robbins 	if (ldebug(rt_sendsig))
356c680f6b1SDavid E. O'Brien 		printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
357ea0fabbcSTim J. Robbins 		    frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
358ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
359ea0fabbcSTim J. Robbins #endif
360ea0fabbcSTim J. Robbins 
361ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
362ea0fabbcSTim J. Robbins 		/*
363ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
364ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
365ea0fabbcSTim J. Robbins 		 */
366ea0fabbcSTim J. Robbins #ifdef DEBUG
367ea0fabbcSTim J. Robbins 		if (ldebug(rt_sendsig))
368ea0fabbcSTim J. Robbins 			printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
369ea0fabbcSTim J. Robbins 			    fp, oonstack);
370ea0fabbcSTim J. Robbins #endif
371ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
372ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
373ea0fabbcSTim J. Robbins 	}
374ea0fabbcSTim J. Robbins 
3754ba25759SEd Maste 	/* Build context to run handler in. */
376ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
377bdc37934SDmitry Chagin 	regs->tf_rip = linux32_rt_sigcode;
37822eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
379ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
380ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
3812c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
3822c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
3832c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
3842c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
3852c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
386e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
387ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
388ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
389ea0fabbcSTim J. Robbins }
390ea0fabbcSTim J. Robbins 
391ea0fabbcSTim J. Robbins 
392ea0fabbcSTim J. Robbins /*
393ea0fabbcSTim J. Robbins  * Send an interrupt to process.
394ea0fabbcSTim J. Robbins  *
395ea0fabbcSTim J. Robbins  * Stack is set up to allow sigcode stored
396ea0fabbcSTim J. Robbins  * in u. to call routine, followed by kcall
397ea0fabbcSTim J. Robbins  * to sigreturn routine below.  After sigreturn
398ea0fabbcSTim J. Robbins  * resets the signal mask, the stack, and the
399ea0fabbcSTim J. Robbins  * frame pointer, it returns to the user
400ea0fabbcSTim J. Robbins  * specified pc, psl.
401ea0fabbcSTim J. Robbins  */
402ea0fabbcSTim J. Robbins static void
4039104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
404ea0fabbcSTim J. Robbins {
405ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
406ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
407ea0fabbcSTim J. Robbins 	struct sigacts *psp;
408ea0fabbcSTim J. Robbins 	struct trapframe *regs;
409ea0fabbcSTim J. Robbins 	struct l_sigframe *fp, frame;
410ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
4114ab7403bSDmitry Chagin 	int oonstack;
4129104847fSDavid Xu 	int sig, code;
413ea0fabbcSTim J. Robbins 
4149104847fSDavid Xu 	sig = ksi->ksi_signo;
4159104847fSDavid Xu 	code = ksi->ksi_code;
416ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
417ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
418ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
419ea0fabbcSTim J. Robbins 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
420ea0fabbcSTim J. Robbins 		/* Signal handler installed with SA_SIGINFO. */
4219104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
422ea0fabbcSTim J. Robbins 		return;
423ea0fabbcSTim J. Robbins 	}
424ea0fabbcSTim J. Robbins 
425ea0fabbcSTim J. Robbins 	regs = td->td_frame;
426ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
427ea0fabbcSTim J. Robbins 
428ea0fabbcSTim J. Robbins #ifdef DEBUG
429ea0fabbcSTim J. Robbins 	if (ldebug(sendsig))
430728ef954SJohn Baldwin 		printf(ARGS(sendsig, "%p, %d, %p, %u"),
431ea0fabbcSTim J. Robbins 		    catcher, sig, (void*)mask, code);
432ea0fabbcSTim J. Robbins #endif
433ea0fabbcSTim J. Robbins 
4344ba25759SEd Maste 	/* Allocate space for the signal handler context. */
435ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
436ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
437aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
438ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
439ea0fabbcSTim J. Robbins 	} else
440ea0fabbcSTim J. Robbins 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
441ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
442ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
443ea0fabbcSTim J. Robbins 
4444ba25759SEd Maste 	/* Build the argument list for the signal handler. */
4454ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
446ea0fabbcSTim J. Robbins 
447ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
448ea0fabbcSTim J. Robbins 
449ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
450ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
451ea0fabbcSTim J. Robbins 
452ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &lmask);
453ea0fabbcSTim J. Robbins 
4544ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
4554ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
4562c66cccaSKonstantin Belousov 	frame.sf_sc.sc_gs     = regs->tf_gs;
4572c66cccaSKonstantin Belousov 	frame.sf_sc.sc_fs     = regs->tf_fs;
4582c66cccaSKonstantin Belousov 	frame.sf_sc.sc_es     = regs->tf_es;
4592c66cccaSKonstantin Belousov 	frame.sf_sc.sc_ds     = regs->tf_ds;
460ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edi    = regs->tf_rdi;
461ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esi    = regs->tf_rsi;
462ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
463ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
464bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_rsp;
465ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edx    = regs->tf_rdx;
466ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
467ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eax    = regs->tf_rax;
468ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eip    = regs->tf_rip;
469ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_cs     = regs->tf_cs;
470ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eflags = regs->tf_rflags;
471ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
472ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ss     = regs->tf_ss;
473ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_err    = regs->tf_err;
47496a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
475ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
476ea0fabbcSTim J. Robbins 
4774ab7403bSDmitry Chagin 	frame.sf_extramask[0] = lmask.__mask;
478ea0fabbcSTim J. Robbins 
479ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
480ea0fabbcSTim J. Robbins 		/*
481ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
482ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
483ea0fabbcSTim J. Robbins 		 */
484ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
485ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
486ea0fabbcSTim J. Robbins 	}
487ea0fabbcSTim J. Robbins 
4884ba25759SEd Maste 	/* Build context to run handler in. */
489ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
490bdc37934SDmitry Chagin 	regs->tf_rip = linux32_sigcode;
49122eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
492ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
493ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
4942c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
4952c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
4962c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
4972c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
4982c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
499e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
500ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
501ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
502ea0fabbcSTim J. Robbins }
503ea0fabbcSTim J. Robbins 
504ea0fabbcSTim J. Robbins /*
505ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
506ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
507ea0fabbcSTim J. Robbins  * stack state from context left by sendsig (above).
508ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
509ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
510ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
511ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
512ea0fabbcSTim J. Robbins  * a machine fault.
513ea0fabbcSTim J. Robbins  */
514ea0fabbcSTim J. Robbins int
515ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
516ea0fabbcSTim J. Robbins {
517ea0fabbcSTim J. Robbins 	struct l_sigframe frame;
518ea0fabbcSTim J. Robbins 	struct trapframe *regs;
519d6e029adSKonstantin Belousov 	sigset_t bmask;
520ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
5214ab7403bSDmitry Chagin 	int eflags;
5229104847fSDavid Xu 	ksiginfo_t ksi;
523ea0fabbcSTim J. Robbins 
524ea0fabbcSTim J. Robbins 	regs = td->td_frame;
525ea0fabbcSTim J. Robbins 
526ea0fabbcSTim J. Robbins #ifdef DEBUG
527ea0fabbcSTim J. Robbins 	if (ldebug(sigreturn))
528ea0fabbcSTim J. Robbins 		printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
529ea0fabbcSTim J. Robbins #endif
530ea0fabbcSTim J. Robbins 	/*
531ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the sigframe.
532ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
533ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
534ea0fabbcSTim J. Robbins 	 */
535ea0fabbcSTim J. Robbins 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
536ea0fabbcSTim J. Robbins 		return (EFAULT);
537ea0fabbcSTim J. Robbins 
5384ba25759SEd Maste 	/* Check for security violations. */
539ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
540ea0fabbcSTim J. Robbins 	eflags = frame.sf_sc.sc_eflags;
5413d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
542ea0fabbcSTim J. Robbins 		return(EINVAL);
543ea0fabbcSTim J. Robbins 
544ea0fabbcSTim J. Robbins 	/*
545ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
546ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
547ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
548ea0fabbcSTim J. Robbins 	 */
549ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
550ea0fabbcSTim J. Robbins 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
5519104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5529104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5539104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5549104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5559104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
5569104847fSDavid Xu 		trapsignal(td, &ksi);
557ea0fabbcSTim J. Robbins 		return(EINVAL);
558ea0fabbcSTim J. Robbins 	}
559ea0fabbcSTim J. Robbins 
5604ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_sc.sc_mask;
5614ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_extramask[0];
562d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&lmask, &bmask);
563d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
564ea0fabbcSTim J. Robbins 
5654ba25759SEd Maste 	/* Restore signal context. */
566ea0fabbcSTim J. Robbins 	regs->tf_rdi    = frame.sf_sc.sc_edi;
567ea0fabbcSTim J. Robbins 	regs->tf_rsi    = frame.sf_sc.sc_esi;
568ea0fabbcSTim J. Robbins 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
569ea0fabbcSTim J. Robbins 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
570ea0fabbcSTim J. Robbins 	regs->tf_rdx    = frame.sf_sc.sc_edx;
571ea0fabbcSTim J. Robbins 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
572ea0fabbcSTim J. Robbins 	regs->tf_rax    = frame.sf_sc.sc_eax;
573ea0fabbcSTim J. Robbins 	regs->tf_rip    = frame.sf_sc.sc_eip;
574ea0fabbcSTim J. Robbins 	regs->tf_cs     = frame.sf_sc.sc_cs;
5752c66cccaSKonstantin Belousov 	regs->tf_ds     = frame.sf_sc.sc_ds;
5762c66cccaSKonstantin Belousov 	regs->tf_es     = frame.sf_sc.sc_es;
5772c66cccaSKonstantin Belousov 	regs->tf_fs     = frame.sf_sc.sc_fs;
5782c66cccaSKonstantin Belousov 	regs->tf_gs     = frame.sf_sc.sc_gs;
579ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
580ea0fabbcSTim J. Robbins 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
581ea0fabbcSTim J. Robbins 	regs->tf_ss     = frame.sf_sc.sc_ss;
582e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
583ea0fabbcSTim J. Robbins 
584ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
585ea0fabbcSTim J. Robbins }
586ea0fabbcSTim J. Robbins 
587ea0fabbcSTim J. Robbins /*
588ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
589ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
590ea0fabbcSTim J. Robbins  * stack state from context left by rt_sendsig (above).
591ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
592ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
593ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
594ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
595ea0fabbcSTim J. Robbins  * a machine fault.
596ea0fabbcSTim J. Robbins  */
597ea0fabbcSTim J. Robbins int
598ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
599ea0fabbcSTim J. Robbins {
600ea0fabbcSTim J. Robbins 	struct l_ucontext uc;
601ea0fabbcSTim J. Robbins 	struct l_sigcontext *context;
602d6e029adSKonstantin Belousov 	sigset_t bmask;
603ea0fabbcSTim J. Robbins 	l_stack_t *lss;
604ea0fabbcSTim J. Robbins 	stack_t ss;
605ea0fabbcSTim J. Robbins 	struct trapframe *regs;
606ea0fabbcSTim J. Robbins 	int eflags;
6079104847fSDavid Xu 	ksiginfo_t ksi;
608ea0fabbcSTim J. Robbins 
609ea0fabbcSTim J. Robbins 	regs = td->td_frame;
610ea0fabbcSTim J. Robbins 
611ea0fabbcSTim J. Robbins #ifdef DEBUG
612ea0fabbcSTim J. Robbins 	if (ldebug(rt_sigreturn))
613ea0fabbcSTim J. Robbins 		printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
614ea0fabbcSTim J. Robbins #endif
615ea0fabbcSTim J. Robbins 	/*
616ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the ucontext.
617ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
618ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
619ea0fabbcSTim J. Robbins 	 */
620ea0fabbcSTim J. Robbins 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
621ea0fabbcSTim J. Robbins 		return (EFAULT);
622ea0fabbcSTim J. Robbins 
623ea0fabbcSTim J. Robbins 	context = &uc.uc_mcontext;
624ea0fabbcSTim J. Robbins 
6254ba25759SEd Maste 	/* Check for security violations. */
626ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
627ea0fabbcSTim J. Robbins 	eflags = context->sc_eflags;
6283d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
629ea0fabbcSTim J. Robbins 		return(EINVAL);
630ea0fabbcSTim J. Robbins 
631ea0fabbcSTim J. Robbins 	/*
632ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
633ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
634ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
635ea0fabbcSTim J. Robbins 	 */
636ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
637ea0fabbcSTim J. Robbins 	if (!CS_SECURE(context->sc_cs)) {
6389104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
6399104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
6409104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
6419104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
6429104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
6439104847fSDavid Xu 		trapsignal(td, &ksi);
644ea0fabbcSTim J. Robbins 		return(EINVAL);
645ea0fabbcSTim J. Robbins 	}
646ea0fabbcSTim J. Robbins 
647d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
648d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
649ea0fabbcSTim J. Robbins 
650ea0fabbcSTim J. Robbins 	/*
651ea0fabbcSTim J. Robbins 	 * Restore signal context
652ea0fabbcSTim J. Robbins 	 */
6532c66cccaSKonstantin Belousov 	regs->tf_gs	= context->sc_gs;
6542c66cccaSKonstantin Belousov 	regs->tf_fs	= context->sc_fs;
6552c66cccaSKonstantin Belousov 	regs->tf_es	= context->sc_es;
6562c66cccaSKonstantin Belousov 	regs->tf_ds	= context->sc_ds;
657ea0fabbcSTim J. Robbins 	regs->tf_rdi    = context->sc_edi;
658ea0fabbcSTim J. Robbins 	regs->tf_rsi    = context->sc_esi;
659ea0fabbcSTim J. Robbins 	regs->tf_rbp    = context->sc_ebp;
660ea0fabbcSTim J. Robbins 	regs->tf_rbx    = context->sc_ebx;
661ea0fabbcSTim J. Robbins 	regs->tf_rdx    = context->sc_edx;
662ea0fabbcSTim J. Robbins 	regs->tf_rcx    = context->sc_ecx;
663ea0fabbcSTim J. Robbins 	regs->tf_rax    = context->sc_eax;
664ea0fabbcSTim J. Robbins 	regs->tf_rip    = context->sc_eip;
665ea0fabbcSTim J. Robbins 	regs->tf_cs     = context->sc_cs;
666ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
667ea0fabbcSTim J. Robbins 	regs->tf_rsp    = context->sc_esp_at_signal;
668ea0fabbcSTim J. Robbins 	regs->tf_ss     = context->sc_ss;
669e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
670ea0fabbcSTim J. Robbins 
671ea0fabbcSTim J. Robbins 	/*
672ea0fabbcSTim J. Robbins 	 * call sigaltstack & ignore results..
673ea0fabbcSTim J. Robbins 	 */
674ea0fabbcSTim J. Robbins 	lss = &uc.uc_stack;
675ea0fabbcSTim J. Robbins 	ss.ss_sp = PTRIN(lss->ss_sp);
676ea0fabbcSTim J. Robbins 	ss.ss_size = lss->ss_size;
677ea0fabbcSTim J. Robbins 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
678ea0fabbcSTim J. Robbins 
679ea0fabbcSTim J. Robbins #ifdef DEBUG
680ea0fabbcSTim J. Robbins 	if (ldebug(rt_sigreturn))
681c680f6b1SDavid E. O'Brien 		printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
682ea0fabbcSTim J. Robbins 		    ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
683ea0fabbcSTim J. Robbins #endif
684ea0fabbcSTim J. Robbins 	(void)kern_sigaltstack(td, &ss, NULL);
685ea0fabbcSTim J. Robbins 
686ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
687ea0fabbcSTim J. Robbins }
688ea0fabbcSTim J. Robbins 
689afe1a688SKonstantin Belousov static int
6902d88da2fSKonstantin Belousov linux32_fetch_syscall_args(struct thread *td)
691ea0fabbcSTim J. Robbins {
692afe1a688SKonstantin Belousov 	struct proc *p;
693afe1a688SKonstantin Belousov 	struct trapframe *frame;
6942d88da2fSKonstantin Belousov 	struct syscall_args *sa;
695afe1a688SKonstantin Belousov 
696afe1a688SKonstantin Belousov 	p = td->td_proc;
697afe1a688SKonstantin Belousov 	frame = td->td_frame;
6982d88da2fSKonstantin Belousov 	sa = &td->td_sa;
699afe1a688SKonstantin Belousov 
700afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_rbx;
701afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_rcx;
702afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_rdx;
703afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_rsi;
704afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_rdi;
705afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
706afe1a688SKonstantin Belousov 	sa->code = frame->tf_rax;
707afe1a688SKonstantin Belousov 
708afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
709fcdffc03SDmitry Chagin 		/* nosys */
7105047105bSJohn Baldwin 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
711afe1a688SKonstantin Belousov 	else
712afe1a688SKonstantin Belousov 		sa->callp = &p->p_sysent->sv_table[sa->code];
713afe1a688SKonstantin Belousov 	sa->narg = sa->callp->sy_narg;
714afe1a688SKonstantin Belousov 
715afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
716afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_rdx;
717afe1a688SKonstantin Belousov 
718afe1a688SKonstantin Belousov 	return (0);
719ea0fabbcSTim J. Robbins }
720ea0fabbcSTim J. Robbins 
721ea0fabbcSTim J. Robbins /*
722eae594f7SEd Maste  * If a Linux binary is exec'ing something, try this image activator
723ea0fabbcSTim J. Robbins  * first.  We override standard shell script execution in order to
724eae594f7SEd Maste  * be able to modify the interpreter path.  We only do this if a Linux
725ea0fabbcSTim J. Robbins  * binary is doing the exec, so we do not create an EXEC module for it.
726ea0fabbcSTim J. Robbins  */
727ea0fabbcSTim J. Robbins static int
728dc858467SEd Maste linux_exec_imgact_try(struct image_params *imgp)
729ea0fabbcSTim J. Robbins {
730ea0fabbcSTim J. Robbins 	const char *head = (const char *)imgp->image_header;
7311d15fdd9SJohn Baldwin 	char *rpath;
732a14a9498SAlan Cox 	int error = -1;
733ea0fabbcSTim J. Robbins 
734ea0fabbcSTim J. Robbins 	/*
735eae594f7SEd Maste 	 * The interpreter for shell scripts run from a Linux binary needs
736ea0fabbcSTim J. Robbins 	 * to be located in /compat/linux if possible in order to recursively
737eae594f7SEd Maste 	 * maintain Linux path emulation.
738ea0fabbcSTim J. Robbins 	 */
739ea0fabbcSTim J. Robbins 	if (((const short *)head)[0] == SHELLMAGIC) {
740ea0fabbcSTim J. Robbins 		/*
7419bec2ea6SEd Maste 		 * Run our normal shell image activator.  If it succeeds then
7429bec2ea6SEd Maste 		 * attempt to use the alternate path for the interpreter.  If
7439bec2ea6SEd Maste 		 * an alternate path is found, use our stringspace to store it.
744ea0fabbcSTim J. Robbins 		 */
745ea0fabbcSTim J. Robbins 		if ((error = exec_shell_imgact(imgp)) == 0) {
7461d15fdd9SJohn Baldwin 			linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
747d065e13dSDavid E. O'Brien 			    imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
748d065e13dSDavid E. O'Brien 			    AT_FDCWD);
749a14a9498SAlan Cox 			if (rpath != NULL)
750a14a9498SAlan Cox 				imgp->args->fname_buf =
751a14a9498SAlan Cox 				    imgp->interpreter_name = rpath;
752ea0fabbcSTim J. Robbins 		}
753ea0fabbcSTim J. Robbins 	}
754ea0fabbcSTim J. Robbins 	return (error);
755ea0fabbcSTim J. Robbins }
756ea0fabbcSTim J. Robbins 
757ea0fabbcSTim J. Robbins /*
758ea0fabbcSTim J. Robbins  * Clear registers on exec
759ea0fabbcSTim J. Robbins  * XXX copied from ia32_signal.c.
760ea0fabbcSTim J. Robbins  */
761ea0fabbcSTim J. Robbins static void
762dc858467SEd Maste linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
763ea0fabbcSTim J. Robbins {
764ea0fabbcSTim J. Robbins 	struct trapframe *regs = td->td_frame;
765ea0fabbcSTim J. Robbins 	struct pcb *pcb = td->td_pcb;
766ea0fabbcSTim J. Robbins 
7672c66cccaSKonstantin Belousov 	if (td->td_proc->p_md.md_ldt != NULL)
7682c66cccaSKonstantin Belousov 		user_ldt_free(td);
7692c66cccaSKonstantin Belousov 
7709c5b213eSJung-uk Kim 	critical_enter();
771ea0fabbcSTim J. Robbins 	wrmsr(MSR_FSBASE, 0);
772ea0fabbcSTim J. Robbins 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
773ea0fabbcSTim J. Robbins 	pcb->pcb_fsbase = 0;
774ea0fabbcSTim J. Robbins 	pcb->pcb_gsbase = 0;
7759c5b213eSJung-uk Kim 	critical_exit();
7762ee8325fSJohn Baldwin 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
777ea0fabbcSTim J. Robbins 
778ea0fabbcSTim J. Robbins 	bzero((char *)regs, sizeof(struct trapframe));
779a107d8aaSNathan Whitehorn 	regs->tf_rip = imgp->entry_addr;
780ea0fabbcSTim J. Robbins 	regs->tf_rsp = stack;
781ea0fabbcSTim J. Robbins 	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
7822c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
7832c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
7842c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
7852c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
786ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
7872c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
788ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
789a107d8aaSNathan Whitehorn 	regs->tf_rbx = imgp->ps_strings;
790bdbf2db5SJung-uk Kim 
7912a988f7cSStephan Uphoff 	fpstate_drop(td);
792ea0fabbcSTim J. Robbins 
7931b3c3256SKonstantin Belousov 	/* Do full restore on return so that we can change to a different %cs */
794e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
795ea0fabbcSTim J. Robbins }
796ea0fabbcSTim J. Robbins 
797ea0fabbcSTim J. Robbins /*
798ea0fabbcSTim J. Robbins  * XXX copied from ia32_sysvec.c.
799ea0fabbcSTim J. Robbins  */
800ea0fabbcSTim J. Robbins static register_t *
801ea0fabbcSTim J. Robbins linux_copyout_strings(struct image_params *imgp)
802ea0fabbcSTim J. Robbins {
803ea0fabbcSTim J. Robbins 	int argc, envc;
804ea0fabbcSTim J. Robbins 	u_int32_t *vectp;
805ea0fabbcSTim J. Robbins 	char *stringp, *destp;
806ea0fabbcSTim J. Robbins 	u_int32_t *stack_base;
807ea0fabbcSTim J. Robbins 	struct linux32_ps_strings *arginfo;
8084048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
8094048f59cSDmitry Chagin 	size_t execpath_len;
810ea0fabbcSTim J. Robbins 
8114ba25759SEd Maste 	/* Calculate string base and vector table pointers. */
8124048f59cSDmitry Chagin 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
8134048f59cSDmitry Chagin 		execpath_len = strlen(imgp->execpath) + 1;
8144048f59cSDmitry Chagin 	else
8154048f59cSDmitry Chagin 		execpath_len = 0;
8164048f59cSDmitry Chagin 
817ea0fabbcSTim J. Robbins 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
8180020bdf1SDmitry Chagin 	destp =	(caddr_t)arginfo - SPARE_USRSPACE -
8194048f59cSDmitry Chagin 	    roundup(sizeof(canary), sizeof(char *)) -
8204048f59cSDmitry Chagin 	    roundup(execpath_len, sizeof(char *)) -
821b66bb393SPedro F. Giffuni 	    roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
822ea0fabbcSTim J. Robbins 
8234048f59cSDmitry Chagin 	if (execpath_len != 0) {
8244048f59cSDmitry Chagin 		imgp->execpathp = (uintptr_t)arginfo - execpath_len;
8254048f59cSDmitry Chagin 		copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
8264048f59cSDmitry Chagin 	}
8274048f59cSDmitry Chagin 
8284ba25759SEd Maste 	/* Prepare the canary for SSP. */
8294048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
8304048f59cSDmitry Chagin 	imgp->canary = (uintptr_t)arginfo -
8314048f59cSDmitry Chagin 	    roundup(execpath_len, sizeof(char *)) -
8324048f59cSDmitry Chagin 	    roundup(sizeof(canary), sizeof(char *));
8334048f59cSDmitry Chagin 	copyout(canary, (void *)imgp->canary, sizeof(canary));
8344048f59cSDmitry Chagin 
8354ba25759SEd Maste 	/* If we have a valid auxargs ptr, prepare some room on the stack. */
836ea0fabbcSTim J. Robbins 	if (imgp->auxargs) {
837ea0fabbcSTim J. Robbins 		/*
838ea0fabbcSTim J. Robbins 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
839ea0fabbcSTim J. Robbins 		 * lower compatibility.
840ea0fabbcSTim J. Robbins 		 */
841d065e13dSDavid E. O'Brien 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
8424d7c2e8aSDmitry Chagin 		    (LINUX_AT_COUNT * 2);
843ea0fabbcSTim J. Robbins 		/*
844ea0fabbcSTim J. Robbins 		 * The '+ 2' is for the null pointers at the end of each of
845ea0fabbcSTim J. Robbins 		 * the arg and env vector sets,and imgp->auxarg_size is room
846ea0fabbcSTim J. Robbins 		 * for argument of Runtime loader.
847ea0fabbcSTim J. Robbins 		 */
848d065e13dSDavid E. O'Brien 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
849d065e13dSDavid E. O'Brien 		    imgp->args->envc + 2 + imgp->auxarg_size) *
850d065e13dSDavid E. O'Brien 		    sizeof(u_int32_t));
851ea0fabbcSTim J. Robbins 
852ea0fabbcSTim J. Robbins 	} else
853ea0fabbcSTim J. Robbins 		/*
854ea0fabbcSTim J. Robbins 		 * The '+ 2' is for the null pointers at the end of each of
855ea0fabbcSTim J. Robbins 		 * the arg and env vector sets
856ea0fabbcSTim J. Robbins 		 */
857d065e13dSDavid E. O'Brien 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
858d065e13dSDavid E. O'Brien 		    imgp->args->envc + 2) * sizeof(u_int32_t));
859ea0fabbcSTim J. Robbins 
8604ba25759SEd Maste 	/* vectp also becomes our initial stack base. */
861ea0fabbcSTim J. Robbins 	stack_base = vectp;
862ea0fabbcSTim J. Robbins 
863610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
864610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
865610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
8664ba25759SEd Maste 	/* Copy out strings - arguments and environment. */
867610ecfe0SMaxim Sobolev 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
868ea0fabbcSTim J. Robbins 
8694ba25759SEd Maste 	/* Fill in "ps_strings" struct for ps, w, etc. */
8704d7c2e8aSDmitry Chagin 	suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
871ea0fabbcSTim J. Robbins 	suword32(&arginfo->ps_nargvstr, argc);
872ea0fabbcSTim J. Robbins 
8734ba25759SEd Maste 	/* Fill in argument portion of vector table. */
874ea0fabbcSTim J. Robbins 	for (; argc > 0; --argc) {
8754d7c2e8aSDmitry Chagin 		suword32(vectp++, (uint32_t)(intptr_t)destp);
876ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
877ea0fabbcSTim J. Robbins 			destp++;
878ea0fabbcSTim J. Robbins 		destp++;
879ea0fabbcSTim J. Robbins 	}
880ea0fabbcSTim J. Robbins 
8814ba25759SEd Maste 	/* A null vector table pointer separates the argp's from the envp's. */
882ea0fabbcSTim J. Robbins 	suword32(vectp++, 0);
883ea0fabbcSTim J. Robbins 
8844d7c2e8aSDmitry Chagin 	suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
885ea0fabbcSTim J. Robbins 	suword32(&arginfo->ps_nenvstr, envc);
886ea0fabbcSTim J. Robbins 
8874ba25759SEd Maste 	/* Fill in environment portion of vector table. */
888ea0fabbcSTim J. Robbins 	for (; envc > 0; --envc) {
8894d7c2e8aSDmitry Chagin 		suword32(vectp++, (uint32_t)(intptr_t)destp);
890ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
891ea0fabbcSTim J. Robbins 			destp++;
892ea0fabbcSTim J. Robbins 		destp++;
893ea0fabbcSTim J. Robbins 	}
894ea0fabbcSTim J. Robbins 
8954ba25759SEd Maste 	/* The end of the vector table is a null pointer. */
896ea0fabbcSTim J. Robbins 	suword32(vectp, 0);
897ea0fabbcSTim J. Robbins 
898ea0fabbcSTim J. Robbins 	return ((register_t *)stack_base);
899ea0fabbcSTim J. Robbins }
900ea0fabbcSTim J. Robbins 
9016472ac3dSEd Schouten static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
902ea0fabbcSTim J. Robbins     "32-bit Linux emulation");
903ea0fabbcSTim J. Robbins 
904ea0fabbcSTim J. Robbins static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
905ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
906ea0fabbcSTim J. Robbins     &linux32_maxdsiz, 0, "");
907ea0fabbcSTim J. Robbins static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
908ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
909ea0fabbcSTim J. Robbins     &linux32_maxssiz, 0, "");
910ea0fabbcSTim J. Robbins static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
911ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
912ea0fabbcSTim J. Robbins     &linux32_maxvmem, 0, "");
913ea0fabbcSTim J. Robbins 
91467d39748SDmitry Chagin #if defined(DEBUG)
915132f90c6SEd Maste SYSCTL_PROC(_compat_linux32, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
916132f90c6SEd Maste     linux_sysctl_debug, "A", "Linux debugging control");
91767d39748SDmitry Chagin #endif
91867d39748SDmitry Chagin 
919ea0fabbcSTim J. Robbins static void
92019059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which)
921ea0fabbcSTim J. Robbins {
922ea0fabbcSTim J. Robbins 
92319059a13SJohn Baldwin 	switch (which) {
92419059a13SJohn Baldwin 	case RLIMIT_DATA:
925ea0fabbcSTim J. Robbins 		if (linux32_maxdsiz != 0) {
92619059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxdsiz)
92719059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxdsiz;
92819059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxdsiz)
92919059a13SJohn Baldwin 				rl->rlim_max = linux32_maxdsiz;
930ea0fabbcSTim J. Robbins 		}
93119059a13SJohn Baldwin 		break;
93219059a13SJohn Baldwin 	case RLIMIT_STACK:
933ea0fabbcSTim J. Robbins 		if (linux32_maxssiz != 0) {
93419059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxssiz)
93519059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxssiz;
93619059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxssiz)
93719059a13SJohn Baldwin 				rl->rlim_max = linux32_maxssiz;
938ea0fabbcSTim J. Robbins 		}
93919059a13SJohn Baldwin 		break;
94019059a13SJohn Baldwin 	case RLIMIT_VMEM:
941ea0fabbcSTim J. Robbins 		if (linux32_maxvmem != 0) {
94219059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxvmem)
94319059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxvmem;
94419059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxvmem)
94519059a13SJohn Baldwin 				rl->rlim_max = linux32_maxvmem;
946ea0fabbcSTim J. Robbins 		}
94719059a13SJohn Baldwin 		break;
94819059a13SJohn Baldwin 	}
949ea0fabbcSTim J. Robbins }
950ea0fabbcSTim J. Robbins 
951ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = {
9522f99bcceSJohn Baldwin 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
9532f99bcceSJohn Baldwin 	.sv_table	= linux32_sysent,
954a8d403e1SKonstantin Belousov 	.sv_mask	= 0,
955a8d403e1SKonstantin Belousov 	.sv_errsize	= ELAST + 1,
9561ac2776bSEd Maste 	.sv_errtbl	= linux_errtbl,
957dc858467SEd Maste 	.sv_transtrap	= linux_translate_traps,
958dc858467SEd Maste 	.sv_fixup	= linux_fixup_elf,
959a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
960bdc37934SDmitry Chagin 	.sv_sigcode	= &_binary_linux32_locore_o_start,
961a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
962a8d403e1SKonstantin Belousov 	.sv_name	= "Linux ELF32",
963a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
964dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
965a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
966a8d403e1SKonstantin Belousov 	.sv_pagesize	= PAGE_SIZE,
967a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
9688f1e49a6SDmitry Chagin 	.sv_maxuser	= LINUX32_MAXUSER,
969a8d403e1SKonstantin Belousov 	.sv_usrstack	= LINUX32_USRSTACK,
970a8d403e1SKonstantin Belousov 	.sv_psstrings	= LINUX32_PS_STRINGS,
971a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
972a8d403e1SKonstantin Belousov 	.sv_copyout_strings = linux_copyout_strings,
973dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
974a8d403e1SKonstantin Belousov 	.sv_fixlimit	= linux32_fixlimit,
975a8d403e1SKonstantin Belousov 	.sv_maxssiz	= &linux32_maxssiz,
9768f1e49a6SDmitry Chagin 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
977afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = cpu_set_syscall_retval,
978afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
979afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
9808f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
9818f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
982e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
98381338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
984038c7205SDmitry Chagin 	.sv_trap	= NULL,
985ea0fabbcSTim J. Robbins };
986bdc37934SDmitry Chagin 
987bdc37934SDmitry Chagin static void
988bdc37934SDmitry Chagin linux_vdso_install(void *param)
989bdc37934SDmitry Chagin {
990bdc37934SDmitry Chagin 
991bdc37934SDmitry Chagin 	linux_szsigcode = (&_binary_linux32_locore_o_end -
992bdc37934SDmitry Chagin 	    &_binary_linux32_locore_o_start);
993bdc37934SDmitry Chagin 
994bdc37934SDmitry Chagin 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
995bdc37934SDmitry Chagin 		panic("Linux invalid vdso size\n");
996bdc37934SDmitry Chagin 
997bdc37934SDmitry Chagin 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
998bdc37934SDmitry Chagin 
999bdc37934SDmitry Chagin 	linux_shared_page_obj = __elfN(linux_shared_page_init)
1000bdc37934SDmitry Chagin 	    (&linux_shared_page_mapping);
1001bdc37934SDmitry Chagin 
1002c151945cSDmitry Chagin 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec);
1003bdc37934SDmitry Chagin 
1004bdc37934SDmitry Chagin 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
1005bdc37934SDmitry Chagin 	    linux_szsigcode);
1006bdc37934SDmitry Chagin 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
10070020bdf1SDmitry Chagin 
10080020bdf1SDmitry Chagin 	linux_kplatform = linux_shared_page_mapping +
1009c151945cSDmitry Chagin 	    (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
1010bdc37934SDmitry Chagin }
1011bdc37934SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
1012f8268d4dSEd Maste     linux_vdso_install, NULL);
1013bdc37934SDmitry Chagin 
1014bdc37934SDmitry Chagin static void
1015bdc37934SDmitry Chagin linux_vdso_deinstall(void *param)
1016bdc37934SDmitry Chagin {
1017bdc37934SDmitry Chagin 
1018bdc37934SDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_shared_page_obj);
10197b194b3dSEd Maste }
1020bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
1021f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
1022ea0fabbcSTim J. Robbins 
102389ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
102489ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
102589ffc202SBjoern A. Zeeb 
1026a95659f7SEd Maste static bool
102789ffc202SBjoern A. Zeeb linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
102889ffc202SBjoern A. Zeeb {
102989ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
103089ffc202SBjoern A. Zeeb 	uintptr_t p;
103189ffc202SBjoern A. Zeeb 
103289ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
103389ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
103489ffc202SBjoern A. Zeeb 
103589ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
103689ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
1037a95659f7SEd Maste 		return (false);
103889ffc202SBjoern A. Zeeb 
103989ffc202SBjoern A. Zeeb 	/*
1040eae594f7SEd Maste 	 * For Linux we encode osrel as follows (see linux_mib.c):
104189ffc202SBjoern A. Zeeb 	 * VVVMMMIII (version, major, minor), see linux_mib.c.
104289ffc202SBjoern A. Zeeb 	 */
104389ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
104489ffc202SBjoern A. Zeeb 
1045a95659f7SEd Maste 	return (true);
104689ffc202SBjoern A. Zeeb }
104732c01de2SDmitry Chagin 
104832c01de2SDmitry Chagin static Elf_Brandnote linux32_brandnote = {
104989ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
105089ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
105132c01de2SDmitry Chagin 	.hdr.n_type	= 1,
105289ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
105389ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
105489ffc202SBjoern A. Zeeb 	.trans_osrel	= linux32_trans_osrel
105532c01de2SDmitry Chagin };
105632c01de2SDmitry Chagin 
1057ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = {
1058a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1059a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1060a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1061a8d403e1SKonstantin Belousov 	.emul_path	= "/compat/linux",
1062a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
1063a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1064a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
106532c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10662dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1067ea0fabbcSTim J. Robbins };
1068ea0fabbcSTim J. Robbins 
1069ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = {
1070a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1071a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1072a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1073a8d403e1SKonstantin Belousov 	.emul_path	= "/compat/linux",
1074a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1075a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1076a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
107732c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10782dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1079ea0fabbcSTim J. Robbins };
1080ea0fabbcSTim J. Robbins 
1081a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1082a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1083a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1084a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1085a0c59c7aSDmitry Chagin 	.emul_path	= "/compat/linux",
1086a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1087a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1088a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1089a0c59c7aSDmitry Chagin 	.brand_note	= &linux32_brandnote,
1090a0c59c7aSDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1091a0c59c7aSDmitry Chagin };
1092a0c59c7aSDmitry Chagin 
1093ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = {
1094ea0fabbcSTim J. Robbins 	&linux_brand,
1095ea0fabbcSTim J. Robbins 	&linux_glibc2brand,
1096a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1097ea0fabbcSTim J. Robbins 	NULL
1098ea0fabbcSTim J. Robbins };
1099ea0fabbcSTim J. Robbins 
1100ea0fabbcSTim J. Robbins static int
1101ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data)
1102ea0fabbcSTim J. Robbins {
1103ea0fabbcSTim J. Robbins 	Elf32_Brandinfo **brandinfo;
1104ea0fabbcSTim J. Robbins 	int error;
1105ea0fabbcSTim J. Robbins 	struct linux_ioctl_handler **lihp;
1106ea0fabbcSTim J. Robbins 
1107ea0fabbcSTim J. Robbins 	error = 0;
1108ea0fabbcSTim J. Robbins 
1109ea0fabbcSTim J. Robbins 	switch(type) {
1110ea0fabbcSTim J. Robbins 	case MOD_LOAD:
1111ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1112ea0fabbcSTim J. Robbins 		     ++brandinfo)
1113ea0fabbcSTim J. Robbins 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1114ea0fabbcSTim J. Robbins 				error = EINVAL;
1115ea0fabbcSTim J. Robbins 		if (error == 0) {
1116ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1117ea0fabbcSTim J. Robbins 				linux_ioctl_register_handler(*lihp);
11187c09e6c0SAlexander Leidinger 			LIST_INIT(&futex_list);
111979262bf1SDmitry Chagin 			mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
11201ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
1121ea0fabbcSTim J. Robbins 			if (bootverbose)
1122ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler installed\n");
1123ea0fabbcSTim J. Robbins 		} else
1124ea0fabbcSTim J. Robbins 			printf("cannot insert Linux ELF brand handler\n");
1125ea0fabbcSTim J. Robbins 		break;
1126ea0fabbcSTim J. Robbins 	case MOD_UNLOAD:
1127ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1128ea0fabbcSTim J. Robbins 		     ++brandinfo)
1129ea0fabbcSTim J. Robbins 			if (elf32_brand_inuse(*brandinfo))
1130ea0fabbcSTim J. Robbins 				error = EBUSY;
1131ea0fabbcSTim J. Robbins 		if (error == 0) {
1132ea0fabbcSTim J. Robbins 			for (brandinfo = &linux_brandlist[0];
1133ea0fabbcSTim J. Robbins 			     *brandinfo != NULL; ++brandinfo)
1134ea0fabbcSTim J. Robbins 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1135ea0fabbcSTim J. Robbins 					error = EINVAL;
1136ea0fabbcSTim J. Robbins 		}
1137ea0fabbcSTim J. Robbins 		if (error == 0) {
1138ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1139ea0fabbcSTim J. Robbins 				linux_ioctl_unregister_handler(*lihp);
114079262bf1SDmitry Chagin 			mtx_destroy(&futex_mtx);
1141ea0fabbcSTim J. Robbins 			if (bootverbose)
1142ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler removed\n");
1143ea0fabbcSTim J. Robbins 		} else
1144ea0fabbcSTim J. Robbins 			printf("Could not deinstall ELF interpreter entry\n");
1145ea0fabbcSTim J. Robbins 		break;
1146ea0fabbcSTim J. Robbins 	default:
1147af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1148ea0fabbcSTim J. Robbins 	}
1149af682d48SDmitry Chagin 	return (error);
1150ea0fabbcSTim J. Robbins }
1151ea0fabbcSTim J. Robbins 
1152ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = {
1153ea0fabbcSTim J. Robbins 	"linuxelf",
1154ea0fabbcSTim J. Robbins 	linux_elf_modevent,
11559823d527SKevin Lo 	0
1156ea0fabbcSTim J. Robbins };
1157ea0fabbcSTim J. Robbins 
115878ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
115967d39748SDmitry Chagin MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1160b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
1161