xref: /freebsd/sys/amd64/linux32/linux32_sysvec.c (revision dc85846736dffed97360a1c17b89f34c03e4c022)
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/sysctl.h>
60ea0fabbcSTim J. Robbins #include <sys/syscallsubr.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>
78ea0fabbcSTim J. Robbins 
79ea0fabbcSTim J. Robbins #include <amd64/linux32/linux.h>
80ea0fabbcSTim J. Robbins #include <amd64/linux32/linux32_proto.h>
817c09e6c0SAlexander Leidinger #include <compat/linux/linux_emul.h>
82fde63162SDmitry Chagin #include <compat/linux/linux_futex.h>
83d825ce0aSJohn Baldwin #include <compat/linux/linux_ioctl.h>
84ea0fabbcSTim J. Robbins #include <compat/linux/linux_mib.h>
854d7c2e8aSDmitry Chagin #include <compat/linux/linux_misc.h>
86ea0fabbcSTim J. Robbins #include <compat/linux/linux_signal.h>
87ea0fabbcSTim J. Robbins #include <compat/linux/linux_util.h>
88bdc37934SDmitry Chagin #include <compat/linux/linux_vdso.h>
89ea0fabbcSTim J. Robbins 
90ea0fabbcSTim J. Robbins MODULE_VERSION(linux, 1);
91ea0fabbcSTim J. Robbins 
92ea0fabbcSTim J. Robbins #define	AUXARGS_ENTRY_32(pos, id, val)	\
93ea0fabbcSTim J. Robbins 	do {				\
94ea0fabbcSTim J. Robbins 		suword32(pos++, id);	\
95ea0fabbcSTim J. Robbins 		suword32(pos++, val);	\
96ea0fabbcSTim J. Robbins 	} while (0)
97ea0fabbcSTim J. Robbins 
98ea0fabbcSTim J. Robbins #if BYTE_ORDER == LITTLE_ENDIAN
99ea0fabbcSTim J. Robbins #define SHELLMAGIC      0x2123 /* #! */
100ea0fabbcSTim J. Robbins #else
101ea0fabbcSTim J. Robbins #define SHELLMAGIC      0x2321
102ea0fabbcSTim J. Robbins #endif
103ea0fabbcSTim J. Robbins 
104ea0fabbcSTim J. Robbins /*
1054ba25759SEd Maste  * Allow the sendsig functions to use the ldebug() facility even though they
1064ba25759SEd Maste  * are not syscalls themselves.  Map them to syscall 0.  This is slightly less
1074ba25759SEd Maste  * bogus than using ldebug(sigreturn).
108ea0fabbcSTim J. Robbins  */
1096cea44a7SJohn Baldwin #define	LINUX32_SYS_linux_rt_sendsig	0
1106cea44a7SJohn Baldwin #define	LINUX32_SYS_linux_sendsig	0
111ea0fabbcSTim J. Robbins 
1120020bdf1SDmitry Chagin const char *linux_kplatform;
113bdc37934SDmitry Chagin static int linux_szsigcode;
114bdc37934SDmitry Chagin static vm_object_t linux_shared_page_obj;
115bdc37934SDmitry Chagin static char *linux_shared_page_mapping;
116bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_start;
117bdc37934SDmitry Chagin extern char _binary_linux32_locore_o_end;
118ea0fabbcSTim J. Robbins 
1192f99bcceSJohn Baldwin extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
120ea0fabbcSTim J. Robbins 
121ea0fabbcSTim J. Robbins SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
122ea0fabbcSTim J. Robbins 
123*dc858467SEd Maste static int	linux_fixup_elf(register_t **stack_base,
124ea0fabbcSTim J. Robbins 		    struct image_params *iparams);
125ea0fabbcSTim J. Robbins static register_t *linux_copyout_strings(struct image_params *imgp);
1269104847fSDavid Xu static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
127*dc858467SEd Maste static int	linux_exec_imgact_try(struct image_params *iparams);
128*dc858467SEd Maste static void	linux_exec_setregs(struct thread *td,
129a107d8aaSNathan Whitehorn 				   struct image_params *imgp, u_long stack);
13019059a13SJohn Baldwin static void	linux32_fixlimit(struct rlimit *rl, int which);
131a95659f7SEd Maste static bool	linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
132bdc37934SDmitry Chagin static void	linux_vdso_install(void *param);
133bdc37934SDmitry Chagin static void	linux_vdso_deinstall(void *param);
134ea0fabbcSTim J. Robbins 
135ea0fabbcSTim J. Robbins #define LINUX_T_UNKNOWN  255
136ea0fabbcSTim J. Robbins static int _bsd_to_linux_trapcode[] = {
137ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 0 */
138ea0fabbcSTim J. Robbins 	6,			/* 1  T_PRIVINFLT */
139ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 2 */
140ea0fabbcSTim J. Robbins 	3,			/* 3  T_BPTFLT */
141ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 4 */
142ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 5 */
143ea0fabbcSTim J. Robbins 	16,			/* 6  T_ARITHTRAP */
144ea0fabbcSTim J. Robbins 	254,			/* 7  T_ASTFLT */
145ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 8 */
146ea0fabbcSTim J. Robbins 	13,			/* 9  T_PROTFLT */
147ea0fabbcSTim J. Robbins 	1,			/* 10 T_TRCTRAP */
148ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 11 */
149ea0fabbcSTim J. Robbins 	14,			/* 12 T_PAGEFLT */
150ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 13 */
151ea0fabbcSTim J. Robbins 	17,			/* 14 T_ALIGNFLT */
152ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 15 */
153ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 16 */
154ea0fabbcSTim J. Robbins 	LINUX_T_UNKNOWN,	/* 17 */
155ea0fabbcSTim J. Robbins 	0,			/* 18 T_DIVIDE */
156ea0fabbcSTim J. Robbins 	2,			/* 19 T_NMI */
157ea0fabbcSTim J. Robbins 	4,			/* 20 T_OFLOW */
158ea0fabbcSTim J. Robbins 	5,			/* 21 T_BOUND */
159ea0fabbcSTim J. Robbins 	7,			/* 22 T_DNA */
160ea0fabbcSTim J. Robbins 	8,			/* 23 T_DOUBLEFLT */
161ea0fabbcSTim J. Robbins 	9,			/* 24 T_FPOPFLT */
162ea0fabbcSTim J. Robbins 	10,			/* 25 T_TSSFLT */
163ea0fabbcSTim J. Robbins 	11,			/* 26 T_SEGNPFLT */
164ea0fabbcSTim J. Robbins 	12,			/* 27 T_STKFLT */
165ea0fabbcSTim J. Robbins 	18,			/* 28 T_MCHK */
166ea0fabbcSTim J. Robbins 	19,			/* 29 T_XMMFLT */
167ea0fabbcSTim J. Robbins 	15			/* 30 T_RESERVED */
168ea0fabbcSTim J. Robbins };
169ea0fabbcSTim J. Robbins #define bsd_to_linux_trapcode(code) \
170ea24b056SPedro F. Giffuni     ((code)<nitems(_bsd_to_linux_trapcode)? \
171ea0fabbcSTim J. Robbins      _bsd_to_linux_trapcode[(code)]: \
172ea0fabbcSTim J. Robbins      LINUX_T_UNKNOWN)
173ea0fabbcSTim J. Robbins 
174ea0fabbcSTim J. Robbins struct linux32_ps_strings {
175ea0fabbcSTim J. Robbins 	u_int32_t ps_argvstr;	/* first of 0 or more argument strings */
176f2c7668eSDavid Schultz 	u_int ps_nargvstr;	/* the number of argument strings */
177ea0fabbcSTim J. Robbins 	u_int32_t ps_envstr;	/* first of 0 or more environment strings */
178f2c7668eSDavid Schultz 	u_int ps_nenvstr;	/* the number of environment strings */
179ea0fabbcSTim J. Robbins };
180ea0fabbcSTim J. Robbins 
181bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_sigcode);
182bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_rt_sigcode);
183bdc37934SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux32_vsyscall);
1840020bdf1SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
185bdc37934SDmitry Chagin 
186ea0fabbcSTim J. Robbins /*
187ea0fabbcSTim J. Robbins  * If FreeBSD & Linux have a difference of opinion about what a trap
188ea0fabbcSTim J. Robbins  * means, deal with it here.
189ea0fabbcSTim J. Robbins  *
190ea0fabbcSTim J. Robbins  * MPSAFE
191ea0fabbcSTim J. Robbins  */
192ea0fabbcSTim J. Robbins static int
193*dc858467SEd Maste linux_translate_traps(int signal, int trap_code)
194ea0fabbcSTim J. Robbins {
195ea0fabbcSTim J. Robbins 	if (signal != SIGBUS)
196ea0fabbcSTim J. Robbins 		return signal;
197ea0fabbcSTim J. Robbins 	switch (trap_code) {
198ea0fabbcSTim J. Robbins 	case T_PROTFLT:
199ea0fabbcSTim J. Robbins 	case T_TSSFLT:
200ea0fabbcSTim J. Robbins 	case T_DOUBLEFLT:
201ea0fabbcSTim J. Robbins 	case T_PAGEFLT:
202ea0fabbcSTim J. Robbins 		return SIGSEGV;
203ea0fabbcSTim J. Robbins 	default:
204ea0fabbcSTim J. Robbins 		return signal;
205ea0fabbcSTim J. Robbins 	}
206ea0fabbcSTim J. Robbins }
207ea0fabbcSTim J. Robbins 
208ea0fabbcSTim J. Robbins static int
209*dc858467SEd Maste linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
210ea0fabbcSTim J. Robbins {
211ea0fabbcSTim J. Robbins 	Elf32_Auxargs *args;
212ea0fabbcSTim J. Robbins 	Elf32_Addr *base;
2130020bdf1SDmitry Chagin 	Elf32_Addr *pos;
2144d7c2e8aSDmitry Chagin 	struct linux32_ps_strings *arginfo;
215669414e4SXin LI 	int issetugid;
2164d7c2e8aSDmitry Chagin 
2174d7c2e8aSDmitry Chagin 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
218ea0fabbcSTim J. Robbins 
2196617724cSJeff Roberson 	KASSERT(curthread->td_proc == imgp->proc,
220*dc858467SEd Maste 	    ("unsafe linux_fixup_elf(), should be curproc"));
221ea0fabbcSTim J. Robbins 	base = (Elf32_Addr *)*stack_base;
222ea0fabbcSTim J. Robbins 	args = (Elf32_Auxargs *)imgp->auxargs;
223610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
224ea0fabbcSTim J. Robbins 
225669414e4SXin LI 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
226bdc37934SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO_EHDR,
227bdc37934SDmitry Chagin 	    imgp->proc->p_sysent->sv_shared_page_base);
228bdc37934SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_SYSINFO, linux32_vsyscall);
2294d7c2e8aSDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_HWCAP, cpu_feature);
2308d30f381SDmitry Chagin 
2318d30f381SDmitry Chagin 	/*
2328d30f381SDmitry Chagin 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
2338d30f381SDmitry Chagin 	 * as it has appeared in the 2.4.0-rc7 first time.
2348d30f381SDmitry Chagin 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
2358d30f381SDmitry Chagin 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
2368d30f381SDmitry Chagin 	 * is not present.
2378d30f381SDmitry Chagin 	 * Also see linux_times() implementation.
2388d30f381SDmitry Chagin 	 */
2398d30f381SDmitry Chagin 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
2401ca16454SDmitry Chagin 		AUXARGS_ENTRY_32(pos, LINUX_AT_CLKTCK, stclohz);
241ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHDR, args->phdr);
242ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHENT, args->phent);
243ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PHNUM, args->phnum);
244ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_PAGESZ, args->pagesz);
245ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_FLAGS, args->flags);
246ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_ENTRY, args->entry);
247ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_BASE, args->base);
248669414e4SXin LI 	AUXARGS_ENTRY_32(pos, LINUX_AT_SECURE, issetugid);
249ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
250ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
251ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
252ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
2530020bdf1SDmitry Chagin 	AUXARGS_ENTRY_32(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
2544048f59cSDmitry Chagin 	AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
2554048f59cSDmitry Chagin 	if (imgp->execpathp != 0)
2564048f59cSDmitry Chagin 		AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
2574d7c2e8aSDmitry Chagin 	if (args->execfd != -1)
2584d7c2e8aSDmitry Chagin 		AUXARGS_ENTRY_32(pos, AT_EXECFD, args->execfd);
259ea0fabbcSTim J. Robbins 	AUXARGS_ENTRY_32(pos, AT_NULL, 0);
260ea0fabbcSTim J. Robbins 
261ea0fabbcSTim J. Robbins 	free(imgp->auxargs, M_TEMP);
262ea0fabbcSTim J. Robbins 	imgp->auxargs = NULL;
263ea0fabbcSTim J. Robbins 
264ea0fabbcSTim J. Robbins 	base--;
265610ecfe0SMaxim Sobolev 	suword32(base, (uint32_t)imgp->args->argc);
266ea0fabbcSTim J. Robbins 	*stack_base = (register_t *)base;
267af682d48SDmitry Chagin 	return (0);
268ea0fabbcSTim J. Robbins }
269ea0fabbcSTim J. Robbins 
270ea0fabbcSTim J. Robbins static void
2719104847fSDavid Xu linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
272ea0fabbcSTim J. Robbins {
273ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
274ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
275ea0fabbcSTim J. Robbins 	struct sigacts *psp;
276ea0fabbcSTim J. Robbins 	struct trapframe *regs;
277ea0fabbcSTim J. Robbins 	struct l_rt_sigframe *fp, frame;
278ea0fabbcSTim J. Robbins 	int oonstack;
2799104847fSDavid Xu 	int sig;
2809104847fSDavid Xu 	int code;
281ea0fabbcSTim J. Robbins 
2829104847fSDavid Xu 	sig = ksi->ksi_signo;
2839104847fSDavid Xu 	code = ksi->ksi_code;
284ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
285ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
286ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
287ea0fabbcSTim J. Robbins 	regs = td->td_frame;
288ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
289ea0fabbcSTim J. Robbins 
290ea0fabbcSTim J. Robbins #ifdef DEBUG
291ea0fabbcSTim J. Robbins 	if (ldebug(rt_sendsig))
292728ef954SJohn Baldwin 		printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
293ea0fabbcSTim J. Robbins 		    catcher, sig, (void*)mask, code);
294ea0fabbcSTim J. Robbins #endif
2954ba25759SEd Maste 	/* Allocate space for the signal handler context. */
296ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
297ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
298aa949be5SJohn Baldwin 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
299ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
300ea0fabbcSTim J. Robbins 	} else
301ea0fabbcSTim J. Robbins 		fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
302ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
303ea0fabbcSTim J. Robbins 
3044ba25759SEd Maste 	/* Build the argument list for the signal handler. */
3054ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
306ea0fabbcSTim J. Robbins 
307ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
308ea0fabbcSTim J. Robbins 
309ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
310ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
311ea0fabbcSTim J. Robbins 	frame.sf_siginfo = PTROUT(&fp->sf_si);
312ea0fabbcSTim J. Robbins 	frame.sf_ucontext = PTROUT(&fp->sf_sc);
313ea0fabbcSTim J. Robbins 
3144ba25759SEd Maste 	/* Fill in POSIX parts. */
315aa8b2011SKonstantin Belousov 	ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
316ea0fabbcSTim J. Robbins 
317ea0fabbcSTim J. Robbins 	/*
3184ba25759SEd Maste 	 * Build the signal context to be used by sigreturn and libgcc unwind.
319ea0fabbcSTim J. Robbins 	 */
320ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_flags = 0;		/* XXX ??? */
321ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_link = 0;		/* XXX ??? */
322ea0fabbcSTim J. Robbins 
323ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
324ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
325ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
326ea0fabbcSTim J. Robbins 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
327ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
328ea0fabbcSTim J. Robbins 
329ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
330ea0fabbcSTim J. Robbins 
3314ab7403bSDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
332ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_rdi;
333ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_rsi;
334ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_rbp;
335ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_rbx;
336bdc37934SDmitry Chagin 	frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_rsp;
337ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_rdx;
338ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_rcx;
339ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_rax;
340ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_rip;
341ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
3422c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_gs     = regs->tf_gs;
3432c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
3442c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
3452c66cccaSKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
346ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_rflags;
347ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
348ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
349ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
35096a2b635SKonstantin Belousov 	frame.sf_sc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
351ea0fabbcSTim J. Robbins 	frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
352ea0fabbcSTim J. Robbins 
353ea0fabbcSTim J. Robbins #ifdef DEBUG
354ea0fabbcSTim J. Robbins 	if (ldebug(rt_sendsig))
355c680f6b1SDavid E. O'Brien 		printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
356ea0fabbcSTim J. Robbins 		    frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
357ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
358ea0fabbcSTim J. Robbins #endif
359ea0fabbcSTim J. Robbins 
360ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
361ea0fabbcSTim J. Robbins 		/*
362ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
363ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
364ea0fabbcSTim J. Robbins 		 */
365ea0fabbcSTim J. Robbins #ifdef DEBUG
366ea0fabbcSTim J. Robbins 		if (ldebug(rt_sendsig))
367ea0fabbcSTim J. Robbins 			printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
368ea0fabbcSTim J. Robbins 			    fp, oonstack);
369ea0fabbcSTim J. Robbins #endif
370ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
371ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
372ea0fabbcSTim J. Robbins 	}
373ea0fabbcSTim J. Robbins 
3744ba25759SEd Maste 	/* Build context to run handler in. */
375ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
376bdc37934SDmitry Chagin 	regs->tf_rip = linux32_rt_sigcode;
37722eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
378ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
379ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
3802c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
3812c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
3822c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
3832c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
3842c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
385e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
386ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
387ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
388ea0fabbcSTim J. Robbins }
389ea0fabbcSTim J. Robbins 
390ea0fabbcSTim J. Robbins 
391ea0fabbcSTim J. Robbins /*
392ea0fabbcSTim J. Robbins  * Send an interrupt to process.
393ea0fabbcSTim J. Robbins  *
394ea0fabbcSTim J. Robbins  * Stack is set up to allow sigcode stored
395ea0fabbcSTim J. Robbins  * in u. to call routine, followed by kcall
396ea0fabbcSTim J. Robbins  * to sigreturn routine below.  After sigreturn
397ea0fabbcSTim J. Robbins  * resets the signal mask, the stack, and the
398ea0fabbcSTim J. Robbins  * frame pointer, it returns to the user
399ea0fabbcSTim J. Robbins  * specified pc, psl.
400ea0fabbcSTim J. Robbins  */
401ea0fabbcSTim J. Robbins static void
4029104847fSDavid Xu linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
403ea0fabbcSTim J. Robbins {
404ea0fabbcSTim J. Robbins 	struct thread *td = curthread;
405ea0fabbcSTim J. Robbins 	struct proc *p = td->td_proc;
406ea0fabbcSTim J. Robbins 	struct sigacts *psp;
407ea0fabbcSTim J. Robbins 	struct trapframe *regs;
408ea0fabbcSTim J. Robbins 	struct l_sigframe *fp, frame;
409ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
4104ab7403bSDmitry Chagin 	int oonstack;
4119104847fSDavid Xu 	int sig, code;
412ea0fabbcSTim J. Robbins 
4139104847fSDavid Xu 	sig = ksi->ksi_signo;
4149104847fSDavid Xu 	code = ksi->ksi_code;
415ea0fabbcSTim J. Robbins 	PROC_LOCK_ASSERT(p, MA_OWNED);
416ea0fabbcSTim J. Robbins 	psp = p->p_sigacts;
417ea0fabbcSTim J. Robbins 	mtx_assert(&psp->ps_mtx, MA_OWNED);
418ea0fabbcSTim J. Robbins 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
419ea0fabbcSTim J. Robbins 		/* Signal handler installed with SA_SIGINFO. */
4209104847fSDavid Xu 		linux_rt_sendsig(catcher, ksi, mask);
421ea0fabbcSTim J. Robbins 		return;
422ea0fabbcSTim J. Robbins 	}
423ea0fabbcSTim J. Robbins 
424ea0fabbcSTim J. Robbins 	regs = td->td_frame;
425ea0fabbcSTim J. Robbins 	oonstack = sigonstack(regs->tf_rsp);
426ea0fabbcSTim J. Robbins 
427ea0fabbcSTim J. Robbins #ifdef DEBUG
428ea0fabbcSTim J. Robbins 	if (ldebug(sendsig))
429728ef954SJohn Baldwin 		printf(ARGS(sendsig, "%p, %d, %p, %u"),
430ea0fabbcSTim J. Robbins 		    catcher, sig, (void*)mask, code);
431ea0fabbcSTim J. Robbins #endif
432ea0fabbcSTim J. Robbins 
4334ba25759SEd Maste 	/* Allocate space for the signal handler context. */
434ea0fabbcSTim J. Robbins 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
435ea0fabbcSTim J. Robbins 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
436aa949be5SJohn Baldwin 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
437ea0fabbcSTim J. Robbins 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
438ea0fabbcSTim J. Robbins 	} else
439ea0fabbcSTim J. Robbins 		fp = (struct l_sigframe *)regs->tf_rsp - 1;
440ea0fabbcSTim J. Robbins 	mtx_unlock(&psp->ps_mtx);
441ea0fabbcSTim J. Robbins 	PROC_UNLOCK(p);
442ea0fabbcSTim J. Robbins 
4434ba25759SEd Maste 	/* Build the argument list for the signal handler. */
4444ab7403bSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
445ea0fabbcSTim J. Robbins 
446ea0fabbcSTim J. Robbins 	bzero(&frame, sizeof(frame));
447ea0fabbcSTim J. Robbins 
448ea0fabbcSTim J. Robbins 	frame.sf_handler = PTROUT(catcher);
449ea0fabbcSTim J. Robbins 	frame.sf_sig = sig;
450ea0fabbcSTim J. Robbins 
451ea0fabbcSTim J. Robbins 	bsd_to_linux_sigset(mask, &lmask);
452ea0fabbcSTim J. Robbins 
4534ba25759SEd Maste 	/* Build the signal context to be used by sigreturn. */
4544ab7403bSDmitry Chagin 	frame.sf_sc.sc_mask   = lmask.__mask;
4552c66cccaSKonstantin Belousov 	frame.sf_sc.sc_gs     = regs->tf_gs;
4562c66cccaSKonstantin Belousov 	frame.sf_sc.sc_fs     = regs->tf_fs;
4572c66cccaSKonstantin Belousov 	frame.sf_sc.sc_es     = regs->tf_es;
4582c66cccaSKonstantin Belousov 	frame.sf_sc.sc_ds     = regs->tf_ds;
459ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edi    = regs->tf_rdi;
460ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esi    = regs->tf_rsi;
461ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebp    = regs->tf_rbp;
462ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ebx    = regs->tf_rbx;
463bdc37934SDmitry Chagin 	frame.sf_sc.sc_esp    = regs->tf_rsp;
464ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_edx    = regs->tf_rdx;
465ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ecx    = regs->tf_rcx;
466ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eax    = regs->tf_rax;
467ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eip    = regs->tf_rip;
468ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_cs     = regs->tf_cs;
469ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_eflags = regs->tf_rflags;
470ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
471ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_ss     = regs->tf_ss;
472ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_err    = regs->tf_err;
47396a2b635SKonstantin Belousov 	frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
474ea0fabbcSTim J. Robbins 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
475ea0fabbcSTim J. Robbins 
4764ab7403bSDmitry Chagin 	frame.sf_extramask[0] = lmask.__mask;
477ea0fabbcSTim J. Robbins 
478ea0fabbcSTim J. Robbins 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
479ea0fabbcSTim J. Robbins 		/*
480ea0fabbcSTim J. Robbins 		 * Process has trashed its stack; give it an illegal
481ea0fabbcSTim J. Robbins 		 * instruction to halt it in its tracks.
482ea0fabbcSTim J. Robbins 		 */
483ea0fabbcSTim J. Robbins 		PROC_LOCK(p);
484ea0fabbcSTim J. Robbins 		sigexit(td, SIGILL);
485ea0fabbcSTim J. Robbins 	}
486ea0fabbcSTim J. Robbins 
4874ba25759SEd Maste 	/* Build context to run handler in. */
488ea0fabbcSTim J. Robbins 	regs->tf_rsp = PTROUT(fp);
489bdc37934SDmitry Chagin 	regs->tf_rip = linux32_sigcode;
49022eca0bfSKonstantin Belousov 	regs->tf_rflags &= ~(PSL_T | PSL_D);
491ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
492ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
4932c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
4942c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
4952c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
4962c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
4972c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
498e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
499ea0fabbcSTim J. Robbins 	PROC_LOCK(p);
500ea0fabbcSTim J. Robbins 	mtx_lock(&psp->ps_mtx);
501ea0fabbcSTim J. Robbins }
502ea0fabbcSTim J. Robbins 
503ea0fabbcSTim J. Robbins /*
504ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
505ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
506ea0fabbcSTim J. Robbins  * stack state from context left by sendsig (above).
507ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
508ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
509ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
510ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
511ea0fabbcSTim J. Robbins  * a machine fault.
512ea0fabbcSTim J. Robbins  */
513ea0fabbcSTim J. Robbins int
514ea0fabbcSTim J. Robbins linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
515ea0fabbcSTim J. Robbins {
516ea0fabbcSTim J. Robbins 	struct l_sigframe frame;
517ea0fabbcSTim J. Robbins 	struct trapframe *regs;
518d6e029adSKonstantin Belousov 	sigset_t bmask;
519ea0fabbcSTim J. Robbins 	l_sigset_t lmask;
5204ab7403bSDmitry Chagin 	int eflags;
5219104847fSDavid Xu 	ksiginfo_t ksi;
522ea0fabbcSTim J. Robbins 
523ea0fabbcSTim J. Robbins 	regs = td->td_frame;
524ea0fabbcSTim J. Robbins 
525ea0fabbcSTim J. Robbins #ifdef DEBUG
526ea0fabbcSTim J. Robbins 	if (ldebug(sigreturn))
527ea0fabbcSTim J. Robbins 		printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
528ea0fabbcSTim J. Robbins #endif
529ea0fabbcSTim J. Robbins 	/*
530ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the sigframe.
531ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
532ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
533ea0fabbcSTim J. Robbins 	 */
534ea0fabbcSTim J. Robbins 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
535ea0fabbcSTim J. Robbins 		return (EFAULT);
536ea0fabbcSTim J. Robbins 
5374ba25759SEd Maste 	/* Check for security violations. */
538ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
539ea0fabbcSTim J. Robbins 	eflags = frame.sf_sc.sc_eflags;
5403d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
541ea0fabbcSTim J. Robbins 		return(EINVAL);
542ea0fabbcSTim J. Robbins 
543ea0fabbcSTim J. Robbins 	/*
544ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
545ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
546ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
547ea0fabbcSTim J. Robbins 	 */
548ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
549ea0fabbcSTim J. Robbins 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
5509104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
5519104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
5529104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
5539104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
5549104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
5559104847fSDavid Xu 		trapsignal(td, &ksi);
556ea0fabbcSTim J. Robbins 		return(EINVAL);
557ea0fabbcSTim J. Robbins 	}
558ea0fabbcSTim J. Robbins 
5594ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_sc.sc_mask;
5604ab7403bSDmitry Chagin 	lmask.__mask = frame.sf_extramask[0];
561d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&lmask, &bmask);
562d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
563ea0fabbcSTim J. Robbins 
5644ba25759SEd Maste 	/* Restore signal context. */
565ea0fabbcSTim J. Robbins 	regs->tf_rdi    = frame.sf_sc.sc_edi;
566ea0fabbcSTim J. Robbins 	regs->tf_rsi    = frame.sf_sc.sc_esi;
567ea0fabbcSTim J. Robbins 	regs->tf_rbp    = frame.sf_sc.sc_ebp;
568ea0fabbcSTim J. Robbins 	regs->tf_rbx    = frame.sf_sc.sc_ebx;
569ea0fabbcSTim J. Robbins 	regs->tf_rdx    = frame.sf_sc.sc_edx;
570ea0fabbcSTim J. Robbins 	regs->tf_rcx    = frame.sf_sc.sc_ecx;
571ea0fabbcSTim J. Robbins 	regs->tf_rax    = frame.sf_sc.sc_eax;
572ea0fabbcSTim J. Robbins 	regs->tf_rip    = frame.sf_sc.sc_eip;
573ea0fabbcSTim J. Robbins 	regs->tf_cs     = frame.sf_sc.sc_cs;
5742c66cccaSKonstantin Belousov 	regs->tf_ds     = frame.sf_sc.sc_ds;
5752c66cccaSKonstantin Belousov 	regs->tf_es     = frame.sf_sc.sc_es;
5762c66cccaSKonstantin Belousov 	regs->tf_fs     = frame.sf_sc.sc_fs;
5772c66cccaSKonstantin Belousov 	regs->tf_gs     = frame.sf_sc.sc_gs;
578ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
579ea0fabbcSTim J. Robbins 	regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
580ea0fabbcSTim J. Robbins 	regs->tf_ss     = frame.sf_sc.sc_ss;
581e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
582ea0fabbcSTim J. Robbins 
583ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
584ea0fabbcSTim J. Robbins }
585ea0fabbcSTim J. Robbins 
586ea0fabbcSTim J. Robbins /*
587ea0fabbcSTim J. Robbins  * System call to cleanup state after a signal
588ea0fabbcSTim J. Robbins  * has been taken.  Reset signal mask and
589ea0fabbcSTim J. Robbins  * stack state from context left by rt_sendsig (above).
590ea0fabbcSTim J. Robbins  * Return to previous pc and psl as specified by
591ea0fabbcSTim J. Robbins  * context left by sendsig. Check carefully to
592ea0fabbcSTim J. Robbins  * make sure that the user has not modified the
593ea0fabbcSTim J. Robbins  * psl to gain improper privileges or to cause
594ea0fabbcSTim J. Robbins  * a machine fault.
595ea0fabbcSTim J. Robbins  */
596ea0fabbcSTim J. Robbins int
597ea0fabbcSTim J. Robbins linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
598ea0fabbcSTim J. Robbins {
599ea0fabbcSTim J. Robbins 	struct l_ucontext uc;
600ea0fabbcSTim J. Robbins 	struct l_sigcontext *context;
601d6e029adSKonstantin Belousov 	sigset_t bmask;
602ea0fabbcSTim J. Robbins 	l_stack_t *lss;
603ea0fabbcSTim J. Robbins 	stack_t ss;
604ea0fabbcSTim J. Robbins 	struct trapframe *regs;
605ea0fabbcSTim J. Robbins 	int eflags;
6069104847fSDavid Xu 	ksiginfo_t ksi;
607ea0fabbcSTim J. Robbins 
608ea0fabbcSTim J. Robbins 	regs = td->td_frame;
609ea0fabbcSTim J. Robbins 
610ea0fabbcSTim J. Robbins #ifdef DEBUG
611ea0fabbcSTim J. Robbins 	if (ldebug(rt_sigreturn))
612ea0fabbcSTim J. Robbins 		printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
613ea0fabbcSTim J. Robbins #endif
614ea0fabbcSTim J. Robbins 	/*
615ea0fabbcSTim J. Robbins 	 * The trampoline code hands us the ucontext.
616ea0fabbcSTim J. Robbins 	 * It is unsafe to keep track of it ourselves, in the event that a
617ea0fabbcSTim J. Robbins 	 * program jumps out of a signal handler.
618ea0fabbcSTim J. Robbins 	 */
619ea0fabbcSTim J. Robbins 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
620ea0fabbcSTim J. Robbins 		return (EFAULT);
621ea0fabbcSTim J. Robbins 
622ea0fabbcSTim J. Robbins 	context = &uc.uc_mcontext;
623ea0fabbcSTim J. Robbins 
6244ba25759SEd Maste 	/* Check for security violations. */
625ea0fabbcSTim J. Robbins #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
626ea0fabbcSTim J. Robbins 	eflags = context->sc_eflags;
6273d271aaaSEd Maste 	if (!EFLAGS_SECURE(eflags, regs->tf_rflags))
628ea0fabbcSTim J. Robbins 		return(EINVAL);
629ea0fabbcSTim J. Robbins 
630ea0fabbcSTim J. Robbins 	/*
631ea0fabbcSTim J. Robbins 	 * Don't allow users to load a valid privileged %cs.  Let the
632ea0fabbcSTim J. Robbins 	 * hardware check for invalid selectors, excess privilege in
633ea0fabbcSTim J. Robbins 	 * other selectors, invalid %eip's and invalid %esp's.
634ea0fabbcSTim J. Robbins 	 */
635ea0fabbcSTim J. Robbins #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
636ea0fabbcSTim J. Robbins 	if (!CS_SECURE(context->sc_cs)) {
6379104847fSDavid Xu 		ksiginfo_init_trap(&ksi);
6389104847fSDavid Xu 		ksi.ksi_signo = SIGBUS;
6399104847fSDavid Xu 		ksi.ksi_code = BUS_OBJERR;
6409104847fSDavid Xu 		ksi.ksi_trapno = T_PROTFLT;
6419104847fSDavid Xu 		ksi.ksi_addr = (void *)regs->tf_rip;
6429104847fSDavid Xu 		trapsignal(td, &ksi);
643ea0fabbcSTim J. Robbins 		return(EINVAL);
644ea0fabbcSTim J. Robbins 	}
645ea0fabbcSTim J. Robbins 
646d6e029adSKonstantin Belousov 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
647d6e029adSKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
648ea0fabbcSTim J. Robbins 
649ea0fabbcSTim J. Robbins 	/*
650ea0fabbcSTim J. Robbins 	 * Restore signal context
651ea0fabbcSTim J. Robbins 	 */
6522c66cccaSKonstantin Belousov 	regs->tf_gs	= context->sc_gs;
6532c66cccaSKonstantin Belousov 	regs->tf_fs	= context->sc_fs;
6542c66cccaSKonstantin Belousov 	regs->tf_es	= context->sc_es;
6552c66cccaSKonstantin Belousov 	regs->tf_ds	= context->sc_ds;
656ea0fabbcSTim J. Robbins 	regs->tf_rdi    = context->sc_edi;
657ea0fabbcSTim J. Robbins 	regs->tf_rsi    = context->sc_esi;
658ea0fabbcSTim J. Robbins 	regs->tf_rbp    = context->sc_ebp;
659ea0fabbcSTim J. Robbins 	regs->tf_rbx    = context->sc_ebx;
660ea0fabbcSTim J. Robbins 	regs->tf_rdx    = context->sc_edx;
661ea0fabbcSTim J. Robbins 	regs->tf_rcx    = context->sc_ecx;
662ea0fabbcSTim J. Robbins 	regs->tf_rax    = context->sc_eax;
663ea0fabbcSTim J. Robbins 	regs->tf_rip    = context->sc_eip;
664ea0fabbcSTim J. Robbins 	regs->tf_cs     = context->sc_cs;
665ea0fabbcSTim J. Robbins 	regs->tf_rflags = eflags;
666ea0fabbcSTim J. Robbins 	regs->tf_rsp    = context->sc_esp_at_signal;
667ea0fabbcSTim J. Robbins 	regs->tf_ss     = context->sc_ss;
668e6c006d9SJung-uk Kim 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
669ea0fabbcSTim J. Robbins 
670ea0fabbcSTim J. Robbins 	/*
671ea0fabbcSTim J. Robbins 	 * call sigaltstack & ignore results..
672ea0fabbcSTim J. Robbins 	 */
673ea0fabbcSTim J. Robbins 	lss = &uc.uc_stack;
674ea0fabbcSTim J. Robbins 	ss.ss_sp = PTRIN(lss->ss_sp);
675ea0fabbcSTim J. Robbins 	ss.ss_size = lss->ss_size;
676ea0fabbcSTim J. Robbins 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
677ea0fabbcSTim J. Robbins 
678ea0fabbcSTim J. Robbins #ifdef DEBUG
679ea0fabbcSTim J. Robbins 	if (ldebug(rt_sigreturn))
680c680f6b1SDavid E. O'Brien 		printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%lx, mask: 0x%x"),
681ea0fabbcSTim J. Robbins 		    ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
682ea0fabbcSTim J. Robbins #endif
683ea0fabbcSTim J. Robbins 	(void)kern_sigaltstack(td, &ss, NULL);
684ea0fabbcSTim J. Robbins 
685ea0fabbcSTim J. Robbins 	return (EJUSTRETURN);
686ea0fabbcSTim J. Robbins }
687ea0fabbcSTim J. Robbins 
688afe1a688SKonstantin Belousov static int
6892d88da2fSKonstantin Belousov linux32_fetch_syscall_args(struct thread *td)
690ea0fabbcSTim J. Robbins {
691afe1a688SKonstantin Belousov 	struct proc *p;
692afe1a688SKonstantin Belousov 	struct trapframe *frame;
6932d88da2fSKonstantin Belousov 	struct syscall_args *sa;
694afe1a688SKonstantin Belousov 
695afe1a688SKonstantin Belousov 	p = td->td_proc;
696afe1a688SKonstantin Belousov 	frame = td->td_frame;
6972d88da2fSKonstantin Belousov 	sa = &td->td_sa;
698afe1a688SKonstantin Belousov 
699afe1a688SKonstantin Belousov 	sa->args[0] = frame->tf_rbx;
700afe1a688SKonstantin Belousov 	sa->args[1] = frame->tf_rcx;
701afe1a688SKonstantin Belousov 	sa->args[2] = frame->tf_rdx;
702afe1a688SKonstantin Belousov 	sa->args[3] = frame->tf_rsi;
703afe1a688SKonstantin Belousov 	sa->args[4] = frame->tf_rdi;
704afe1a688SKonstantin Belousov 	sa->args[5] = frame->tf_rbp;	/* Unconfirmed */
705afe1a688SKonstantin Belousov 	sa->code = frame->tf_rax;
706afe1a688SKonstantin Belousov 
707afe1a688SKonstantin Belousov 	if (sa->code >= p->p_sysent->sv_size)
708fcdffc03SDmitry Chagin 		/* nosys */
7095047105bSJohn Baldwin 		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 	sa->narg = sa->callp->sy_narg;
713afe1a688SKonstantin Belousov 
714afe1a688SKonstantin Belousov 	td->td_retval[0] = 0;
715afe1a688SKonstantin Belousov 	td->td_retval[1] = frame->tf_rdx;
716afe1a688SKonstantin Belousov 
717afe1a688SKonstantin Belousov 	return (0);
718ea0fabbcSTim J. Robbins }
719ea0fabbcSTim J. Robbins 
720ea0fabbcSTim J. Robbins /*
721eae594f7SEd Maste  * If a Linux binary is exec'ing something, try this image activator
722ea0fabbcSTim J. Robbins  * first.  We override standard shell script execution in order to
723eae594f7SEd Maste  * be able to modify the interpreter path.  We only do this if a Linux
724ea0fabbcSTim J. Robbins  * binary is doing the exec, so we do not create an EXEC module for it.
725ea0fabbcSTim J. Robbins  */
726ea0fabbcSTim J. Robbins static int
727*dc858467SEd Maste linux_exec_imgact_try(struct image_params *imgp)
728ea0fabbcSTim J. Robbins {
729ea0fabbcSTim J. Robbins 	const char *head = (const char *)imgp->image_header;
7301d15fdd9SJohn Baldwin 	char *rpath;
731a14a9498SAlan Cox 	int error = -1;
732ea0fabbcSTim J. Robbins 
733ea0fabbcSTim J. Robbins 	/*
734eae594f7SEd Maste 	 * The interpreter for shell scripts run from a Linux binary needs
735ea0fabbcSTim J. Robbins 	 * to be located in /compat/linux if possible in order to recursively
736eae594f7SEd Maste 	 * maintain Linux path emulation.
737ea0fabbcSTim J. Robbins 	 */
738ea0fabbcSTim J. Robbins 	if (((const short *)head)[0] == SHELLMAGIC) {
739ea0fabbcSTim J. Robbins 		/*
7409bec2ea6SEd Maste 		 * Run our normal shell image activator.  If it succeeds then
7419bec2ea6SEd Maste 		 * attempt to use the alternate path for the interpreter.  If
7429bec2ea6SEd Maste 		 * an alternate path is found, use our stringspace to store it.
743ea0fabbcSTim J. Robbins 		 */
744ea0fabbcSTim J. Robbins 		if ((error = exec_shell_imgact(imgp)) == 0) {
7451d15fdd9SJohn Baldwin 			linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
746d065e13dSDavid E. O'Brien 			    imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
747d065e13dSDavid E. O'Brien 			    AT_FDCWD);
748a14a9498SAlan Cox 			if (rpath != NULL)
749a14a9498SAlan Cox 				imgp->args->fname_buf =
750a14a9498SAlan Cox 				    imgp->interpreter_name = rpath;
751ea0fabbcSTim J. Robbins 		}
752ea0fabbcSTim J. Robbins 	}
753ea0fabbcSTim J. Robbins 	return (error);
754ea0fabbcSTim J. Robbins }
755ea0fabbcSTim J. Robbins 
756ea0fabbcSTim J. Robbins /*
757ea0fabbcSTim J. Robbins  * Clear registers on exec
758ea0fabbcSTim J. Robbins  * XXX copied from ia32_signal.c.
759ea0fabbcSTim J. Robbins  */
760ea0fabbcSTim J. Robbins static void
761*dc858467SEd Maste linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
762ea0fabbcSTim J. Robbins {
763ea0fabbcSTim J. Robbins 	struct trapframe *regs = td->td_frame;
764ea0fabbcSTim J. Robbins 	struct pcb *pcb = td->td_pcb;
765ea0fabbcSTim J. Robbins 
7662c66cccaSKonstantin Belousov 	if (td->td_proc->p_md.md_ldt != NULL)
7672c66cccaSKonstantin Belousov 		user_ldt_free(td);
7682c66cccaSKonstantin Belousov 
7699c5b213eSJung-uk Kim 	critical_enter();
770ea0fabbcSTim J. Robbins 	wrmsr(MSR_FSBASE, 0);
771ea0fabbcSTim J. Robbins 	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
772ea0fabbcSTim J. Robbins 	pcb->pcb_fsbase = 0;
773ea0fabbcSTim J. Robbins 	pcb->pcb_gsbase = 0;
7749c5b213eSJung-uk Kim 	critical_exit();
7752ee8325fSJohn Baldwin 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
776ea0fabbcSTim J. Robbins 
777ea0fabbcSTim J. Robbins 	bzero((char *)regs, sizeof(struct trapframe));
778a107d8aaSNathan Whitehorn 	regs->tf_rip = imgp->entry_addr;
779ea0fabbcSTim J. Robbins 	regs->tf_rsp = stack;
780ea0fabbcSTim J. Robbins 	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
7812c66cccaSKonstantin Belousov 	regs->tf_gs = _ugssel;
7822c66cccaSKonstantin Belousov 	regs->tf_fs = _ufssel;
7832c66cccaSKonstantin Belousov 	regs->tf_es = _udatasel;
7842c66cccaSKonstantin Belousov 	regs->tf_ds = _udatasel;
785ea0fabbcSTim J. Robbins 	regs->tf_ss = _udatasel;
7862c66cccaSKonstantin Belousov 	regs->tf_flags = TF_HASSEGS;
787ea0fabbcSTim J. Robbins 	regs->tf_cs = _ucode32sel;
788a107d8aaSNathan Whitehorn 	regs->tf_rbx = imgp->ps_strings;
789bdbf2db5SJung-uk Kim 
7902a988f7cSStephan Uphoff 	fpstate_drop(td);
791ea0fabbcSTim J. Robbins 
7921b3c3256SKonstantin Belousov 	/* Do full restore on return so that we can change to a different %cs */
793e6c006d9SJung-uk Kim 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
794ea0fabbcSTim J. Robbins }
795ea0fabbcSTim J. Robbins 
796ea0fabbcSTim J. Robbins /*
797ea0fabbcSTim J. Robbins  * XXX copied from ia32_sysvec.c.
798ea0fabbcSTim J. Robbins  */
799ea0fabbcSTim J. Robbins static register_t *
800ea0fabbcSTim J. Robbins linux_copyout_strings(struct image_params *imgp)
801ea0fabbcSTim J. Robbins {
802ea0fabbcSTim J. Robbins 	int argc, envc;
803ea0fabbcSTim J. Robbins 	u_int32_t *vectp;
804ea0fabbcSTim J. Robbins 	char *stringp, *destp;
805ea0fabbcSTim J. Robbins 	u_int32_t *stack_base;
806ea0fabbcSTim J. Robbins 	struct linux32_ps_strings *arginfo;
8074048f59cSDmitry Chagin 	char canary[LINUX_AT_RANDOM_LEN];
8084048f59cSDmitry Chagin 	size_t execpath_len;
809ea0fabbcSTim J. Robbins 
8104ba25759SEd Maste 	/* Calculate string base and vector table pointers. */
8114048f59cSDmitry Chagin 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
8124048f59cSDmitry Chagin 		execpath_len = strlen(imgp->execpath) + 1;
8134048f59cSDmitry Chagin 	else
8144048f59cSDmitry Chagin 		execpath_len = 0;
8154048f59cSDmitry Chagin 
816ea0fabbcSTim J. Robbins 	arginfo = (struct linux32_ps_strings *)LINUX32_PS_STRINGS;
8170020bdf1SDmitry Chagin 	destp =	(caddr_t)arginfo - SPARE_USRSPACE -
8184048f59cSDmitry Chagin 	    roundup(sizeof(canary), sizeof(char *)) -
8194048f59cSDmitry Chagin 	    roundup(execpath_len, sizeof(char *)) -
820b66bb393SPedro F. Giffuni 	    roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
821ea0fabbcSTim J. Robbins 
8224048f59cSDmitry Chagin 	if (execpath_len != 0) {
8234048f59cSDmitry Chagin 		imgp->execpathp = (uintptr_t)arginfo - execpath_len;
8244048f59cSDmitry Chagin 		copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
8254048f59cSDmitry Chagin 	}
8264048f59cSDmitry Chagin 
8274ba25759SEd Maste 	/* Prepare the canary for SSP. */
8284048f59cSDmitry Chagin 	arc4rand(canary, sizeof(canary), 0);
8294048f59cSDmitry Chagin 	imgp->canary = (uintptr_t)arginfo -
8304048f59cSDmitry Chagin 	    roundup(execpath_len, sizeof(char *)) -
8314048f59cSDmitry Chagin 	    roundup(sizeof(canary), sizeof(char *));
8324048f59cSDmitry Chagin 	copyout(canary, (void *)imgp->canary, sizeof(canary));
8334048f59cSDmitry Chagin 
8344ba25759SEd Maste 	/* If we have a valid auxargs ptr, prepare some room on the stack. */
835ea0fabbcSTim J. Robbins 	if (imgp->auxargs) {
836ea0fabbcSTim J. Robbins 		/*
837ea0fabbcSTim J. Robbins 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
838ea0fabbcSTim J. Robbins 		 * lower compatibility.
839ea0fabbcSTim J. Robbins 		 */
840d065e13dSDavid E. O'Brien 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
8414d7c2e8aSDmitry Chagin 		    (LINUX_AT_COUNT * 2);
842ea0fabbcSTim J. Robbins 		/*
843ea0fabbcSTim J. Robbins 		 * The '+ 2' is for the null pointers at the end of each of
844ea0fabbcSTim J. Robbins 		 * the arg and env vector sets,and imgp->auxarg_size is room
845ea0fabbcSTim J. Robbins 		 * for argument of Runtime loader.
846ea0fabbcSTim J. Robbins 		 */
847d065e13dSDavid E. O'Brien 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
848d065e13dSDavid E. O'Brien 		    imgp->args->envc + 2 + imgp->auxarg_size) *
849d065e13dSDavid E. O'Brien 		    sizeof(u_int32_t));
850ea0fabbcSTim J. Robbins 
851ea0fabbcSTim J. Robbins 	} else
852ea0fabbcSTim J. Robbins 		/*
853ea0fabbcSTim J. Robbins 		 * The '+ 2' is for the null pointers at the end of each of
854ea0fabbcSTim J. Robbins 		 * the arg and env vector sets
855ea0fabbcSTim J. Robbins 		 */
856d065e13dSDavid E. O'Brien 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
857d065e13dSDavid E. O'Brien 		    imgp->args->envc + 2) * sizeof(u_int32_t));
858ea0fabbcSTim J. Robbins 
8594ba25759SEd Maste 	/* vectp also becomes our initial stack base. */
860ea0fabbcSTim J. Robbins 	stack_base = vectp;
861ea0fabbcSTim J. Robbins 
862610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
863610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
864610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
8654ba25759SEd Maste 	/* Copy out strings - arguments and environment. */
866610ecfe0SMaxim Sobolev 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
867ea0fabbcSTim J. Robbins 
8684ba25759SEd Maste 	/* Fill in "ps_strings" struct for ps, w, etc. */
8694d7c2e8aSDmitry Chagin 	suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp);
870ea0fabbcSTim J. Robbins 	suword32(&arginfo->ps_nargvstr, argc);
871ea0fabbcSTim J. Robbins 
8724ba25759SEd Maste 	/* Fill in argument portion of vector table. */
873ea0fabbcSTim J. Robbins 	for (; argc > 0; --argc) {
8744d7c2e8aSDmitry Chagin 		suword32(vectp++, (uint32_t)(intptr_t)destp);
875ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
876ea0fabbcSTim J. Robbins 			destp++;
877ea0fabbcSTim J. Robbins 		destp++;
878ea0fabbcSTim J. Robbins 	}
879ea0fabbcSTim J. Robbins 
8804ba25759SEd Maste 	/* A null vector table pointer separates the argp's from the envp's. */
881ea0fabbcSTim J. Robbins 	suword32(vectp++, 0);
882ea0fabbcSTim J. Robbins 
8834d7c2e8aSDmitry Chagin 	suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp);
884ea0fabbcSTim J. Robbins 	suword32(&arginfo->ps_nenvstr, envc);
885ea0fabbcSTim J. Robbins 
8864ba25759SEd Maste 	/* Fill in environment portion of vector table. */
887ea0fabbcSTim J. Robbins 	for (; envc > 0; --envc) {
8884d7c2e8aSDmitry Chagin 		suword32(vectp++, (uint32_t)(intptr_t)destp);
889ea0fabbcSTim J. Robbins 		while (*stringp++ != 0)
890ea0fabbcSTim J. Robbins 			destp++;
891ea0fabbcSTim J. Robbins 		destp++;
892ea0fabbcSTim J. Robbins 	}
893ea0fabbcSTim J. Robbins 
8944ba25759SEd Maste 	/* The end of the vector table is a null pointer. */
895ea0fabbcSTim J. Robbins 	suword32(vectp, 0);
896ea0fabbcSTim J. Robbins 
897ea0fabbcSTim J. Robbins 	return ((register_t *)stack_base);
898ea0fabbcSTim J. Robbins }
899ea0fabbcSTim J. Robbins 
9006472ac3dSEd Schouten static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW, 0,
901ea0fabbcSTim J. Robbins     "32-bit Linux emulation");
902ea0fabbcSTim J. Robbins 
903ea0fabbcSTim J. Robbins static u_long	linux32_maxdsiz = LINUX32_MAXDSIZ;
904ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
905ea0fabbcSTim J. Robbins     &linux32_maxdsiz, 0, "");
906ea0fabbcSTim J. Robbins static u_long	linux32_maxssiz = LINUX32_MAXSSIZ;
907ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
908ea0fabbcSTim J. Robbins     &linux32_maxssiz, 0, "");
909ea0fabbcSTim J. Robbins static u_long	linux32_maxvmem = LINUX32_MAXVMEM;
910ea0fabbcSTim J. Robbins SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
911ea0fabbcSTim J. Robbins     &linux32_maxvmem, 0, "");
912ea0fabbcSTim J. Robbins 
91367d39748SDmitry Chagin #if defined(DEBUG)
914132f90c6SEd Maste SYSCTL_PROC(_compat_linux32, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
915132f90c6SEd Maste     linux_sysctl_debug, "A", "Linux debugging control");
91667d39748SDmitry Chagin #endif
91767d39748SDmitry Chagin 
918ea0fabbcSTim J. Robbins static void
91919059a13SJohn Baldwin linux32_fixlimit(struct rlimit *rl, int which)
920ea0fabbcSTim J. Robbins {
921ea0fabbcSTim J. Robbins 
92219059a13SJohn Baldwin 	switch (which) {
92319059a13SJohn Baldwin 	case RLIMIT_DATA:
924ea0fabbcSTim J. Robbins 		if (linux32_maxdsiz != 0) {
92519059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxdsiz)
92619059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxdsiz;
92719059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxdsiz)
92819059a13SJohn Baldwin 				rl->rlim_max = linux32_maxdsiz;
929ea0fabbcSTim J. Robbins 		}
93019059a13SJohn Baldwin 		break;
93119059a13SJohn Baldwin 	case RLIMIT_STACK:
932ea0fabbcSTim J. Robbins 		if (linux32_maxssiz != 0) {
93319059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxssiz)
93419059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxssiz;
93519059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxssiz)
93619059a13SJohn Baldwin 				rl->rlim_max = linux32_maxssiz;
937ea0fabbcSTim J. Robbins 		}
93819059a13SJohn Baldwin 		break;
93919059a13SJohn Baldwin 	case RLIMIT_VMEM:
940ea0fabbcSTim J. Robbins 		if (linux32_maxvmem != 0) {
94119059a13SJohn Baldwin 			if (rl->rlim_cur > linux32_maxvmem)
94219059a13SJohn Baldwin 				rl->rlim_cur = linux32_maxvmem;
94319059a13SJohn Baldwin 			if (rl->rlim_max > linux32_maxvmem)
94419059a13SJohn Baldwin 				rl->rlim_max = linux32_maxvmem;
945ea0fabbcSTim J. Robbins 		}
94619059a13SJohn Baldwin 		break;
94719059a13SJohn Baldwin 	}
948ea0fabbcSTim J. Robbins }
949ea0fabbcSTim J. Robbins 
950ea0fabbcSTim J. Robbins struct sysentvec elf_linux_sysvec = {
9512f99bcceSJohn Baldwin 	.sv_size	= LINUX32_SYS_MAXSYSCALL,
9522f99bcceSJohn Baldwin 	.sv_table	= linux32_sysent,
953a8d403e1SKonstantin Belousov 	.sv_mask	= 0,
954a8d403e1SKonstantin Belousov 	.sv_errsize	= ELAST + 1,
9556e481f83SEd Maste 	.sv_errtbl	= bsd_to_linux_errno_generic,
956*dc858467SEd Maste 	.sv_transtrap	= linux_translate_traps,
957*dc858467SEd Maste 	.sv_fixup	= linux_fixup_elf,
958a8d403e1SKonstantin Belousov 	.sv_sendsig	= linux_sendsig,
959bdc37934SDmitry Chagin 	.sv_sigcode	= &_binary_linux32_locore_o_start,
960a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &linux_szsigcode,
961a8d403e1SKonstantin Belousov 	.sv_name	= "Linux ELF32",
962a8d403e1SKonstantin Belousov 	.sv_coredump	= elf32_coredump,
963*dc858467SEd Maste 	.sv_imgact_try	= linux_exec_imgact_try,
964a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
965a8d403e1SKonstantin Belousov 	.sv_pagesize	= PAGE_SIZE,
966a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
9678f1e49a6SDmitry Chagin 	.sv_maxuser	= LINUX32_MAXUSER,
968a8d403e1SKonstantin Belousov 	.sv_usrstack	= LINUX32_USRSTACK,
969a8d403e1SKonstantin Belousov 	.sv_psstrings	= LINUX32_PS_STRINGS,
970a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
971a8d403e1SKonstantin Belousov 	.sv_copyout_strings = linux_copyout_strings,
972*dc858467SEd Maste 	.sv_setregs	= linux_exec_setregs,
973a8d403e1SKonstantin Belousov 	.sv_fixlimit	= linux32_fixlimit,
974a8d403e1SKonstantin Belousov 	.sv_maxssiz	= &linux32_maxssiz,
9758f1e49a6SDmitry Chagin 	.sv_flags	= SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP,
976afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = cpu_set_syscall_retval,
977afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = linux32_fetch_syscall_args,
978afe1a688SKonstantin Belousov 	.sv_syscallnames = NULL,
9798f1e49a6SDmitry Chagin 	.sv_shared_page_base = LINUX32_SHAREDPAGE,
9808f1e49a6SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
981e5d81ef1SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
98281338031SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
983038c7205SDmitry Chagin 	.sv_trap	= NULL,
984ea0fabbcSTim J. Robbins };
985bdc37934SDmitry Chagin 
986bdc37934SDmitry Chagin static void
987bdc37934SDmitry Chagin linux_vdso_install(void *param)
988bdc37934SDmitry Chagin {
989bdc37934SDmitry Chagin 
990bdc37934SDmitry Chagin 	linux_szsigcode = (&_binary_linux32_locore_o_end -
991bdc37934SDmitry Chagin 	    &_binary_linux32_locore_o_start);
992bdc37934SDmitry Chagin 
993bdc37934SDmitry Chagin 	if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
994bdc37934SDmitry Chagin 		panic("Linux invalid vdso size\n");
995bdc37934SDmitry Chagin 
996bdc37934SDmitry Chagin 	__elfN(linux_vdso_fixup)(&elf_linux_sysvec);
997bdc37934SDmitry Chagin 
998bdc37934SDmitry Chagin 	linux_shared_page_obj = __elfN(linux_shared_page_init)
999bdc37934SDmitry Chagin 	    (&linux_shared_page_mapping);
1000bdc37934SDmitry Chagin 
1001c151945cSDmitry Chagin 	__elfN(linux_vdso_reloc)(&elf_linux_sysvec);
1002bdc37934SDmitry Chagin 
1003bdc37934SDmitry Chagin 	bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
1004bdc37934SDmitry Chagin 	    linux_szsigcode);
1005bdc37934SDmitry Chagin 	elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
10060020bdf1SDmitry Chagin 
10070020bdf1SDmitry Chagin 	linux_kplatform = linux_shared_page_mapping +
1008c151945cSDmitry Chagin 	    (linux_platform - (caddr_t)elf_linux_sysvec.sv_shared_page_base);
1009bdc37934SDmitry Chagin }
1010bdc37934SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
1011bdc37934SDmitry Chagin     (sysinit_cfunc_t)linux_vdso_install, NULL);
1012bdc37934SDmitry Chagin 
1013bdc37934SDmitry Chagin static void
1014bdc37934SDmitry Chagin linux_vdso_deinstall(void *param)
1015bdc37934SDmitry Chagin {
1016bdc37934SDmitry Chagin 
1017bdc37934SDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_shared_page_obj);
10187b194b3dSEd Maste }
1019bdc37934SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
1020bdc37934SDmitry Chagin     (sysinit_cfunc_t)linux_vdso_deinstall, NULL);
1021ea0fabbcSTim J. Robbins 
102289ffc202SBjoern A. Zeeb static char GNU_ABI_VENDOR[] = "GNU";
102389ffc202SBjoern A. Zeeb static int GNULINUX_ABI_DESC = 0;
102489ffc202SBjoern A. Zeeb 
1025a95659f7SEd Maste static bool
102689ffc202SBjoern A. Zeeb linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
102789ffc202SBjoern A. Zeeb {
102889ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
102989ffc202SBjoern A. Zeeb 	uintptr_t p;
103089ffc202SBjoern A. Zeeb 
103189ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
103289ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
103389ffc202SBjoern A. Zeeb 
103489ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
103589ffc202SBjoern A. Zeeb 	if (desc[0] != GNULINUX_ABI_DESC)
1036a95659f7SEd Maste 		return (false);
103789ffc202SBjoern A. Zeeb 
103889ffc202SBjoern A. Zeeb 	/*
1039eae594f7SEd Maste 	 * For Linux we encode osrel as follows (see linux_mib.c):
104089ffc202SBjoern A. Zeeb 	 * VVVMMMIII (version, major, minor), see linux_mib.c.
104189ffc202SBjoern A. Zeeb 	 */
104289ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
104389ffc202SBjoern A. Zeeb 
1044a95659f7SEd Maste 	return (true);
104589ffc202SBjoern A. Zeeb }
104632c01de2SDmitry Chagin 
104732c01de2SDmitry Chagin static Elf_Brandnote linux32_brandnote = {
104889ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
104989ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
105032c01de2SDmitry Chagin 	.hdr.n_type	= 1,
105189ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
105289ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
105389ffc202SBjoern A. Zeeb 	.trans_osrel	= linux32_trans_osrel
105432c01de2SDmitry Chagin };
105532c01de2SDmitry Chagin 
1056ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_brand = {
1057a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1058a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1059a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1060a8d403e1SKonstantin Belousov 	.emul_path	= "/compat/linux",
1061a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.1",
1062a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1063a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
106432c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10652dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1066ea0fabbcSTim J. Robbins };
1067ea0fabbcSTim J. Robbins 
1068ea0fabbcSTim J. Robbins static Elf32_Brandinfo linux_glibc2brand = {
1069a8d403e1SKonstantin Belousov 	.brand		= ELFOSABI_LINUX,
1070a8d403e1SKonstantin Belousov 	.machine	= EM_386,
1071a8d403e1SKonstantin Belousov 	.compat_3_brand	= "Linux",
1072a8d403e1SKonstantin Belousov 	.emul_path	= "/compat/linux",
1073a8d403e1SKonstantin Belousov 	.interp_path	= "/lib/ld-linux.so.2",
1074a8d403e1SKonstantin Belousov 	.sysvec		= &elf_linux_sysvec,
1075a8d403e1SKonstantin Belousov 	.interp_newpath	= NULL,
107632c01de2SDmitry Chagin 	.brand_note	= &linux32_brandnote,
10772dedc128SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1078ea0fabbcSTim J. Robbins };
1079ea0fabbcSTim J. Robbins 
1080a0c59c7aSDmitry Chagin static Elf32_Brandinfo linux_muslbrand = {
1081a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
1082a0c59c7aSDmitry Chagin 	.machine	= EM_386,
1083a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
1084a0c59c7aSDmitry Chagin 	.emul_path	= "/compat/linux",
1085a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-i386.so.1",
1086a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
1087a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
1088a0c59c7aSDmitry Chagin 	.brand_note	= &linux32_brandnote,
1089a0c59c7aSDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1090a0c59c7aSDmitry Chagin };
1091a0c59c7aSDmitry Chagin 
1092ea0fabbcSTim J. Robbins Elf32_Brandinfo *linux_brandlist[] = {
1093ea0fabbcSTim J. Robbins 	&linux_brand,
1094ea0fabbcSTim J. Robbins 	&linux_glibc2brand,
1095a0c59c7aSDmitry Chagin 	&linux_muslbrand,
1096ea0fabbcSTim J. Robbins 	NULL
1097ea0fabbcSTim J. Robbins };
1098ea0fabbcSTim J. Robbins 
1099ea0fabbcSTim J. Robbins static int
1100ea0fabbcSTim J. Robbins linux_elf_modevent(module_t mod, int type, void *data)
1101ea0fabbcSTim J. Robbins {
1102ea0fabbcSTim J. Robbins 	Elf32_Brandinfo **brandinfo;
1103ea0fabbcSTim J. Robbins 	int error;
1104ea0fabbcSTim J. Robbins 	struct linux_ioctl_handler **lihp;
1105ea0fabbcSTim J. Robbins 
1106ea0fabbcSTim J. Robbins 	error = 0;
1107ea0fabbcSTim J. Robbins 
1108ea0fabbcSTim J. Robbins 	switch(type) {
1109ea0fabbcSTim J. Robbins 	case MOD_LOAD:
1110ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1111ea0fabbcSTim J. Robbins 		     ++brandinfo)
1112ea0fabbcSTim J. Robbins 			if (elf32_insert_brand_entry(*brandinfo) < 0)
1113ea0fabbcSTim J. Robbins 				error = EINVAL;
1114ea0fabbcSTim J. Robbins 		if (error == 0) {
1115ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1116ea0fabbcSTim J. Robbins 				linux_ioctl_register_handler(*lihp);
11177c09e6c0SAlexander Leidinger 			LIST_INIT(&futex_list);
111879262bf1SDmitry Chagin 			mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
11191ca16454SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
1120ea0fabbcSTim J. Robbins 			if (bootverbose)
1121ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler installed\n");
1122ea0fabbcSTim J. Robbins 		} else
1123ea0fabbcSTim J. Robbins 			printf("cannot insert Linux ELF brand handler\n");
1124ea0fabbcSTim J. Robbins 		break;
1125ea0fabbcSTim J. Robbins 	case MOD_UNLOAD:
1126ea0fabbcSTim J. Robbins 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1127ea0fabbcSTim J. Robbins 		     ++brandinfo)
1128ea0fabbcSTim J. Robbins 			if (elf32_brand_inuse(*brandinfo))
1129ea0fabbcSTim J. Robbins 				error = EBUSY;
1130ea0fabbcSTim J. Robbins 		if (error == 0) {
1131ea0fabbcSTim J. Robbins 			for (brandinfo = &linux_brandlist[0];
1132ea0fabbcSTim J. Robbins 			     *brandinfo != NULL; ++brandinfo)
1133ea0fabbcSTim J. Robbins 				if (elf32_remove_brand_entry(*brandinfo) < 0)
1134ea0fabbcSTim J. Robbins 					error = EINVAL;
1135ea0fabbcSTim J. Robbins 		}
1136ea0fabbcSTim J. Robbins 		if (error == 0) {
1137ea0fabbcSTim J. Robbins 			SET_FOREACH(lihp, linux_ioctl_handler_set)
1138ea0fabbcSTim J. Robbins 				linux_ioctl_unregister_handler(*lihp);
113979262bf1SDmitry Chagin 			mtx_destroy(&futex_mtx);
1140ea0fabbcSTim J. Robbins 			if (bootverbose)
1141ea0fabbcSTim J. Robbins 				printf("Linux ELF exec handler removed\n");
1142ea0fabbcSTim J. Robbins 		} else
1143ea0fabbcSTim J. Robbins 			printf("Could not deinstall ELF interpreter entry\n");
1144ea0fabbcSTim J. Robbins 		break;
1145ea0fabbcSTim J. Robbins 	default:
1146af682d48SDmitry Chagin 		return (EOPNOTSUPP);
1147ea0fabbcSTim J. Robbins 	}
1148af682d48SDmitry Chagin 	return (error);
1149ea0fabbcSTim J. Robbins }
1150ea0fabbcSTim J. Robbins 
1151ea0fabbcSTim J. Robbins static moduledata_t linux_elf_mod = {
1152ea0fabbcSTim J. Robbins 	"linuxelf",
1153ea0fabbcSTim J. Robbins 	linux_elf_modevent,
11549823d527SKevin Lo 	0
1155ea0fabbcSTim J. Robbins };
1156ea0fabbcSTim J. Robbins 
115778ae4338SKonstantin Belousov DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
115867d39748SDmitry Chagin MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1159b6348be7SBaptiste Daroussin FEATURE(linux, "Linux 32bit support");
1160