xref: /freebsd-src/sys/amd64/linux/linux_sysvec.c (revision be707ee09556a3fa345bc30c04aeeaa2a5d2efa2)
1b2f58791SDmitry Chagin /*-
2b2f58791SDmitry Chagin  * Copyright (c) 2004 Tim J. Robbins
3b2f58791SDmitry Chagin  * Copyright (c) 2003 Peter Wemm
4b2f58791SDmitry Chagin  * Copyright (c) 2002 Doug Rabson
5b2f58791SDmitry Chagin  * Copyright (c) 1998-1999 Andrew Gallatin
6b2f58791SDmitry Chagin  * Copyright (c) 1994-1996 Søren Schmidt
7b2f58791SDmitry Chagin  * All rights reserved.
81ca6b15bSDmitry Chagin  * Copyright (c) 2013, 2021 Dmitry Chagin <dchagin@FreeBSD.org>
9b2f58791SDmitry Chagin  *
10b2f58791SDmitry Chagin  * Redistribution and use in source and binary forms, with or without
11b2f58791SDmitry Chagin  * modification, are permitted provided that the following conditions
12b2f58791SDmitry Chagin  * are met:
13b2f58791SDmitry Chagin  * 1. Redistributions of source code must retain the above copyright
14b2f58791SDmitry Chagin  *    notice, this list of conditions and the following disclaimer
15b2f58791SDmitry Chagin  *    in this position and unchanged.
16b2f58791SDmitry Chagin  * 2. Redistributions in binary form must reproduce the above copyright
17b2f58791SDmitry Chagin  *    notice, this list of conditions and the following disclaimer in the
18b2f58791SDmitry Chagin  *    documentation and/or other materials provided with the distribution.
19b2f58791SDmitry Chagin  * 3. The name of the author may not be used to endorse or promote products
20b2f58791SDmitry Chagin  *    derived from this software without specific prior written permission
21b2f58791SDmitry Chagin  *
22b2f58791SDmitry Chagin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23b2f58791SDmitry Chagin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24b2f58791SDmitry Chagin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25b2f58791SDmitry Chagin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26b2f58791SDmitry Chagin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27b2f58791SDmitry Chagin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28b2f58791SDmitry Chagin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29b2f58791SDmitry Chagin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30b2f58791SDmitry Chagin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31b2f58791SDmitry Chagin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32b2f58791SDmitry Chagin  */
33b2f58791SDmitry Chagin 
34b2f58791SDmitry Chagin #define	__ELF_WORD_SIZE	64
35b2f58791SDmitry Chagin 
36b2f58791SDmitry Chagin #include <sys/param.h>
37b2f58791SDmitry Chagin #include <sys/exec.h>
38b2f58791SDmitry Chagin #include <sys/imgact.h>
39b2f58791SDmitry Chagin #include <sys/imgact_elf.h>
40b2f58791SDmitry Chagin #include <sys/kernel.h>
41b2f58791SDmitry Chagin #include <sys/ktr.h>
42b2f58791SDmitry Chagin #include <sys/lock.h>
43b2f58791SDmitry Chagin #include <sys/module.h>
44b2f58791SDmitry Chagin #include <sys/mutex.h>
45b2f58791SDmitry Chagin #include <sys/proc.h>
469931033bSDmitry Chagin #include <sys/stddef.h>
47b2f58791SDmitry Chagin #include <sys/syscallsubr.h>
48c0aa0e2cSEd Maste #include <sys/sysctl.h>
49b2f58791SDmitry Chagin #include <sys/sysent.h>
50b2f58791SDmitry Chagin 
51b2f58791SDmitry Chagin #include <vm/pmap.h>
522456a459SDmitry Chagin #include <vm/vm.h>
5368df2376SDmitry Chagin #include <vm/vm_param.h>
54b2f58791SDmitry Chagin 
55b2f58791SDmitry Chagin #include <machine/md_var.h>
56d41e41f9SJohn Baldwin #include <machine/trap.h>
57b2f58791SDmitry Chagin 
589931033bSDmitry Chagin #include <x86/linux/linux_x86.h>
59b2f58791SDmitry Chagin #include <amd64/linux/linux.h>
60b2f58791SDmitry Chagin #include <amd64/linux/linux_proto.h>
6174465145SDmitry Chagin #include <compat/linux/linux_elf.h>
62b2f58791SDmitry Chagin #include <compat/linux/linux_emul.h>
630a4b664aSDmitry Chagin #include <compat/linux/linux_fork.h>
64b2f58791SDmitry Chagin #include <compat/linux/linux_ioctl.h>
65b2f58791SDmitry Chagin #include <compat/linux/linux_mib.h>
66b2f58791SDmitry Chagin #include <compat/linux/linux_misc.h>
67b2f58791SDmitry Chagin #include <compat/linux/linux_signal.h>
68b2f58791SDmitry Chagin #include <compat/linux/linux_util.h>
69b2f58791SDmitry Chagin #include <compat/linux/linux_vdso.h>
70b2f58791SDmitry Chagin 
7121f24617SDmitry Chagin #include <x86/linux/linux_x86_sigframe.h>
7221f24617SDmitry Chagin 
73920184edSDmitry Chagin _Static_assert(sizeof(struct l_fpstate) ==
74920184edSDmitry Chagin     sizeof(__typeof(((mcontext_t *)0)->mc_fpstate)),
75920184edSDmitry Chagin     "fxsave area size incorrect");
76920184edSDmitry Chagin 
77b2f58791SDmitry Chagin MODULE_VERSION(linux64, 1);
78b2f58791SDmitry Chagin 
799931033bSDmitry Chagin #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
809931033bSDmitry Chagin #define	LINUX_VDSOPAGE_LA48	(VM_MAXUSER_ADDRESS_LA48 - \
819931033bSDmitry Chagin 				    LINUX_VDSOPAGE_SIZE)
829931033bSDmitry Chagin #define	LINUX_SHAREDPAGE_LA48	(LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
839931033bSDmitry Chagin 				/*
849931033bSDmitry Chagin 				 * PAGE_SIZE - the size
859931033bSDmitry Chagin 				 * of the native SHAREDPAGE
869931033bSDmitry Chagin 				 */
879931033bSDmitry Chagin #define	LINUX_USRSTACK_LA48	LINUX_SHAREDPAGE_LA48
889931033bSDmitry Chagin #define	LINUX_PS_STRINGS_LA48	(LINUX_USRSTACK_LA48 - \
899931033bSDmitry Chagin 				    sizeof(struct ps_strings))
909931033bSDmitry Chagin 
91b2f58791SDmitry Chagin static int linux_szsigcode;
929931033bSDmitry Chagin static vm_object_t linux_vdso_obj;
939931033bSDmitry Chagin static char *linux_vdso_mapping;
949931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_start;
959931033bSDmitry Chagin extern char _binary_linux_vdso_so_o_end;
969931033bSDmitry Chagin static vm_offset_t linux_vdso_base;
97b2f58791SDmitry Chagin 
98b2f58791SDmitry Chagin extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
991da65dcbSMitchell Horne extern const char *linux_syscallnames[];
100b2f58791SDmitry Chagin 
101b2f58791SDmitry Chagin SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
102b2f58791SDmitry Chagin 
103a543556cSDmitry Chagin static void	linux_vdso_install(const void *param);
104a543556cSDmitry Chagin static void	linux_vdso_deinstall(const void *param);
1059931033bSDmitry Chagin static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
106b2f58791SDmitry Chagin static void	linux_set_syscall_retval(struct thread *td, int error);
1072d88da2fSKonstantin Belousov static int	linux_fetch_syscall_args(struct thread *td);
108b2f58791SDmitry Chagin static void	linux_exec_setregs(struct thread *td, struct image_params *imgp,
10931174518SJohn Baldwin 		    uintptr_t stack);
1109931033bSDmitry Chagin static void	linux_exec_sysvec_init(void *param);
1115fd9cd53SDmitry Chagin static int	linux_on_exec_vmspace(struct proc *p,
1125fd9cd53SDmitry Chagin 		    struct image_params *imgp);
113de8374dfSDmitry Chagin static void	linux_set_fork_retval(struct thread *td);
114038c7205SDmitry Chagin static int	linux_vsyscall(struct thread *td);
115b2f58791SDmitry Chagin 
116b2f58791SDmitry Chagin LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
117b2f58791SDmitry Chagin LINUX_VDSO_SYM_CHAR(linux_platform);
1189931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
1199931033bSDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
1205a6a4fb2SDmitry Chagin LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
121b2f58791SDmitry Chagin 
122a463dd81SDmitry Chagin /*
123a463dd81SDmitry Chagin  * According to the Intel x86 ISA 64-bit syscall
124a463dd81SDmitry Chagin  * saves %rip to %rcx and rflags to %r11. Registers on syscall entry:
125a463dd81SDmitry Chagin  * %rax  system call number
126a463dd81SDmitry Chagin  * %rcx  return address
127a463dd81SDmitry Chagin  * %r11  saved rflags
128a463dd81SDmitry Chagin  * %rdi  arg1
129a463dd81SDmitry Chagin  * %rsi  arg2
130a463dd81SDmitry Chagin  * %rdx  arg3
131a463dd81SDmitry Chagin  * %r10  arg4
132a463dd81SDmitry Chagin  * %r8   arg5
133a463dd81SDmitry Chagin  * %r9   arg6
134a463dd81SDmitry Chagin  *
135a463dd81SDmitry Chagin  * Then FreeBSD fast_syscall() move registers:
136a463dd81SDmitry Chagin  * %rcx -> trapframe.tf_rip
137a463dd81SDmitry Chagin  * %r10 -> trapframe.tf_rcx
138a463dd81SDmitry Chagin  */
139b2f58791SDmitry Chagin static int
linux_fetch_syscall_args(struct thread * td)1402d88da2fSKonstantin Belousov linux_fetch_syscall_args(struct thread *td)
141b2f58791SDmitry Chagin {
142b2f58791SDmitry Chagin 	struct proc *p;
143b2f58791SDmitry Chagin 	struct trapframe *frame;
1442d88da2fSKonstantin Belousov 	struct syscall_args *sa;
145b2f58791SDmitry Chagin 
146b2f58791SDmitry Chagin 	p = td->td_proc;
147b2f58791SDmitry Chagin 	frame = td->td_frame;
1482d88da2fSKonstantin Belousov 	sa = &td->td_sa;
149b2f58791SDmitry Chagin 
150b2f58791SDmitry Chagin 	sa->args[0] = frame->tf_rdi;
151b2f58791SDmitry Chagin 	sa->args[1] = frame->tf_rsi;
152b2f58791SDmitry Chagin 	sa->args[2] = frame->tf_rdx;
153b2f58791SDmitry Chagin 	sa->args[3] = frame->tf_rcx;
154b2f58791SDmitry Chagin 	sa->args[4] = frame->tf_r8;
155b2f58791SDmitry Chagin 	sa->args[5] = frame->tf_r9;
156b2f58791SDmitry Chagin 	sa->code = frame->tf_rax;
157cf98bc28SDavid Chisnall 	sa->original_code = sa->code;
158b2f58791SDmitry Chagin 
159fcdffc03SDmitry Chagin 	if (sa->code >= p->p_sysent->sv_size)
160fcdffc03SDmitry Chagin 		/* nosys */
1617acc4240SKonstantin Belousov 		sa->callp = &nosys_sysent;
162fcdffc03SDmitry Chagin 	else
163b2f58791SDmitry Chagin 		sa->callp = &p->p_sysent->sv_table[sa->code];
164b2f58791SDmitry Chagin 
165185bd9faSDmitry Chagin 	/* Restore r10 earlier to avoid doing this multiply times. */
166185bd9faSDmitry Chagin 	frame->tf_r10 = frame->tf_rcx;
167037b60fbSDmitry Chagin 	/* Restore %rcx for machine context. */
168037b60fbSDmitry Chagin 	frame->tf_rcx = frame->tf_rip;
169185bd9faSDmitry Chagin 
170b2f58791SDmitry Chagin 	td->td_retval[0] = 0;
171b2f58791SDmitry Chagin 	return (0);
172b2f58791SDmitry Chagin }
173b2f58791SDmitry Chagin 
174b2f58791SDmitry Chagin static void
linux_set_syscall_retval(struct thread * td,int error)175b2f58791SDmitry Chagin linux_set_syscall_retval(struct thread *td, int error)
176b2f58791SDmitry Chagin {
1776221ec60SEdward Tomasz Napierala 	struct trapframe *frame;
178b2f58791SDmitry Chagin 
1796221ec60SEdward Tomasz Napierala 	frame = td->td_frame;
1806221ec60SEdward Tomasz Napierala 
1816221ec60SEdward Tomasz Napierala 	switch (error) {
1826221ec60SEdward Tomasz Napierala 	case 0:
1836221ec60SEdward Tomasz Napierala 		frame->tf_rax = td->td_retval[0];
1846221ec60SEdward Tomasz Napierala 		break;
185b2f58791SDmitry Chagin 
1866221ec60SEdward Tomasz Napierala 	case ERESTART:
1876221ec60SEdward Tomasz Napierala 		/*
1886221ec60SEdward Tomasz Napierala 		 * Reconstruct pc, we know that 'syscall' is 2 bytes,
1896221ec60SEdward Tomasz Napierala 		 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
1906221ec60SEdward Tomasz Napierala 		 * We saved this in tf_err.
1916221ec60SEdward Tomasz Napierala 		 *
1926221ec60SEdward Tomasz Napierala 		 */
1936221ec60SEdward Tomasz Napierala 		frame->tf_rip -= frame->tf_err;
1946221ec60SEdward Tomasz Napierala 		break;
195b2f58791SDmitry Chagin 
1966221ec60SEdward Tomasz Napierala 	case EJUSTRETURN:
1976221ec60SEdward Tomasz Napierala 		break;
1986221ec60SEdward Tomasz Napierala 
1996221ec60SEdward Tomasz Napierala 	default:
200866b1f51SEdward Tomasz Napierala 		frame->tf_rax = bsd_to_linux_errno(error);
2016221ec60SEdward Tomasz Napierala 		break;
202c26391f4SEdward Tomasz Napierala 	}
203c26391f4SEdward Tomasz Napierala 
2046221ec60SEdward Tomasz Napierala 	/*
2056221ec60SEdward Tomasz Napierala 	 * Differently from FreeBSD native ABI, on Linux only %rcx
2066221ec60SEdward Tomasz Napierala 	 * and %r11 values are not preserved across the syscall.
2076221ec60SEdward Tomasz Napierala 	 * Require full context restore to get all registers except
2086221ec60SEdward Tomasz Napierala 	 * those two restored at return to usermode.
2096221ec60SEdward Tomasz Napierala 	 */
210b2f58791SDmitry Chagin 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
211b2f58791SDmitry Chagin }
212b2f58791SDmitry Chagin 
213de8374dfSDmitry Chagin static void
linux_set_fork_retval(struct thread * td)214de8374dfSDmitry Chagin linux_set_fork_retval(struct thread *td)
215de8374dfSDmitry Chagin {
216de8374dfSDmitry Chagin 	struct trapframe *frame = td->td_frame;
217de8374dfSDmitry Chagin 
218de8374dfSDmitry Chagin 	frame->tf_rax = 0;
219de8374dfSDmitry Chagin }
220de8374dfSDmitry Chagin 
2217d8c9839SDmitry Chagin void
linux64_arch_copyout_auxargs(struct image_params * imgp,Elf_Auxinfo ** pos)2227d8c9839SDmitry Chagin linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
223b2f58791SDmitry Chagin {
224b2f58791SDmitry Chagin 
2257d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
2267d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
2274281dab8SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());
2287d8c9839SDmitry Chagin 	AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
2295caa67faSJohn Baldwin }
230b2f58791SDmitry Chagin 
231b2f58791SDmitry Chagin /*
232b2f58791SDmitry Chagin  * Reset registers to default values on exec.
233b2f58791SDmitry Chagin  */
234b2f58791SDmitry Chagin static void
linux_exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)23531174518SJohn Baldwin linux_exec_setregs(struct thread *td, struct image_params *imgp,
23631174518SJohn Baldwin     uintptr_t stack)
237b2f58791SDmitry Chagin {
2387c5d1690SKonstantin Belousov 	struct trapframe *regs;
2397c5d1690SKonstantin Belousov 	struct pcb *pcb;
2407c5d1690SKonstantin Belousov 	register_t saved_rflags;
2417c5d1690SKonstantin Belousov 
2427c5d1690SKonstantin Belousov 	regs = td->td_frame;
2437c5d1690SKonstantin Belousov 	pcb = td->td_pcb;
244b2f58791SDmitry Chagin 
245b2f58791SDmitry Chagin 	if (td->td_proc->p_md.md_ldt != NULL)
246b2f58791SDmitry Chagin 		user_ldt_free(td);
247b2f58791SDmitry Chagin 
248b2f58791SDmitry Chagin 	pcb->pcb_fsbase = 0;
249b2f58791SDmitry Chagin 	pcb->pcb_gsbase = 0;
250b2f58791SDmitry Chagin 	clear_pcb_flags(pcb, PCB_32BIT);
251b2f58791SDmitry Chagin 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
252b2f58791SDmitry Chagin 	set_pcb_flags(pcb, PCB_FULL_IRET);
253b2f58791SDmitry Chagin 
2547c5d1690SKonstantin Belousov 	saved_rflags = regs->tf_rflags & PSL_T;
255b2f58791SDmitry Chagin 	bzero((char *)regs, sizeof(struct trapframe));
256b2f58791SDmitry Chagin 	regs->tf_rip = imgp->entry_addr;
257b2f58791SDmitry Chagin 	regs->tf_rsp = stack;
2587c5d1690SKonstantin Belousov 	regs->tf_rflags = PSL_USER | saved_rflags;
259b2f58791SDmitry Chagin 	regs->tf_ss = _udatasel;
260b2f58791SDmitry Chagin 	regs->tf_cs = _ucodesel;
261b2f58791SDmitry Chagin 	regs->tf_ds = _udatasel;
262b2f58791SDmitry Chagin 	regs->tf_es = _udatasel;
263b2f58791SDmitry Chagin 	regs->tf_fs = _ufssel;
264b2f58791SDmitry Chagin 	regs->tf_gs = _ugssel;
265b2f58791SDmitry Chagin 	regs->tf_flags = TF_HASSEGS;
266b2f58791SDmitry Chagin 
2672f158847SKonstantin Belousov 	x86_clear_dbregs(pcb);
268b2f58791SDmitry Chagin 
269b2f58791SDmitry Chagin 	/*
270b2f58791SDmitry Chagin 	 * Drop the FP state if we hold it, so that the process gets a
271b2f58791SDmitry Chagin 	 * clean FP state if it uses the FPU again.
272b2f58791SDmitry Chagin 	 */
273b2f58791SDmitry Chagin 	fpstate_drop(td);
274b2f58791SDmitry Chagin }
275b2f58791SDmitry Chagin 
276920184edSDmitry Chagin static int
linux_fxrstor(struct thread * td,mcontext_t * mcp,struct l_sigcontext * sc)277920184edSDmitry Chagin linux_fxrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
278920184edSDmitry Chagin {
279920184edSDmitry Chagin 	struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
280920184edSDmitry Chagin 	int error;
281920184edSDmitry Chagin 
282920184edSDmitry Chagin 	error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
283920184edSDmitry Chagin 	if (error != 0)
284920184edSDmitry Chagin 		return (error);
285920184edSDmitry Chagin 	bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
286920184edSDmitry Chagin 	return (set_fpcontext(td, mcp, NULL, 0));
287920184edSDmitry Chagin }
288920184edSDmitry Chagin 
289920184edSDmitry Chagin static int
linux_xrstor(struct thread * td,mcontext_t * mcp,struct l_sigcontext * sc)290cbbac560SDmitry Chagin linux_xrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
291cbbac560SDmitry Chagin {
292cbbac560SDmitry Chagin 	struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
293cbbac560SDmitry Chagin 	char *xfpustate;
294cbbac560SDmitry Chagin 	struct proc *p;
295cbbac560SDmitry Chagin 	uint32_t magic2;
296cbbac560SDmitry Chagin 	int error;
297cbbac560SDmitry Chagin 
298cbbac560SDmitry Chagin 	p = td->td_proc;
299cbbac560SDmitry Chagin 	mcp->mc_xfpustate_len = cpu_max_ext_state_size - sizeof(struct savefpu);
300cbbac560SDmitry Chagin 
301cbbac560SDmitry Chagin 	/* Legacy region of an xsave area. */
302cbbac560SDmitry Chagin 	error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
303cbbac560SDmitry Chagin 	if (error != 0)
304cbbac560SDmitry Chagin 		return (error);
305cbbac560SDmitry Chagin 	bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
306cbbac560SDmitry Chagin 
307cbbac560SDmitry Chagin 	/* Extended region of an xsave area. */
308cbbac560SDmitry Chagin 	sc->sc_fpstate += sizeof(mcp->mc_fpstate);
309cbbac560SDmitry Chagin 	xfpustate = (char *)fpu_save_area_alloc();
310cbbac560SDmitry Chagin 	error = copyin(PTRIN(sc->sc_fpstate), xfpustate, mcp->mc_xfpustate_len);
311cbbac560SDmitry Chagin 	if (error != 0) {
312cbbac560SDmitry Chagin 		fpu_save_area_free((struct savefpu *)xfpustate);
313cbbac560SDmitry Chagin 		uprintf("pid %d (%s): linux xrstor failed\n", p->p_pid,
314cbbac560SDmitry Chagin 		    td->td_name);
315cbbac560SDmitry Chagin 		return (error);
316cbbac560SDmitry Chagin 	}
317cbbac560SDmitry Chagin 
318cbbac560SDmitry Chagin 	/* Linux specific end of xsave area marker. */
319cbbac560SDmitry Chagin 	sc->sc_fpstate += mcp->mc_xfpustate_len;
320cbbac560SDmitry Chagin 	error = copyin(PTRIN(sc->sc_fpstate), &magic2, LINUX_FP_XSTATE_MAGIC2_SIZE);
321cbbac560SDmitry Chagin 	if (error != 0 || magic2 != LINUX_FP_XSTATE_MAGIC2) {
322cbbac560SDmitry Chagin 		fpu_save_area_free((struct savefpu *)xfpustate);
323cbbac560SDmitry Chagin 		uprintf("pid %d (%s): sigreturn magic2 0x%x error %d\n",
324cbbac560SDmitry Chagin 		    p->p_pid, td->td_name, magic2, error);
325cbbac560SDmitry Chagin 		return (error);
326cbbac560SDmitry Chagin 	}
327cbbac560SDmitry Chagin 
328cbbac560SDmitry Chagin 	error = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
329cbbac560SDmitry Chagin 	fpu_save_area_free((struct savefpu *)xfpustate);
330cbbac560SDmitry Chagin 	if (error != 0) {
331cbbac560SDmitry Chagin 		uprintf("pid %d (%s): sigreturn set_fpcontext error %d\n",
332cbbac560SDmitry Chagin 		    p->p_pid, td->td_name, error);
333cbbac560SDmitry Chagin 	}
334cbbac560SDmitry Chagin 	return (error);
335cbbac560SDmitry Chagin }
336cbbac560SDmitry Chagin 
337cbbac560SDmitry Chagin static int
linux_copyin_fpstate(struct thread * td,struct l_ucontext * uc)338920184edSDmitry Chagin linux_copyin_fpstate(struct thread *td, struct l_ucontext *uc)
339920184edSDmitry Chagin {
340920184edSDmitry Chagin 	mcontext_t mc;
341920184edSDmitry Chagin 
342920184edSDmitry Chagin 	bzero(&mc, sizeof(mc));
343920184edSDmitry Chagin 	mc.mc_ownedfp = _MC_FPOWNED_FPU;
344920184edSDmitry Chagin 	mc.mc_fpformat = _MC_FPFMT_XMM;
345920184edSDmitry Chagin 
346cbbac560SDmitry Chagin 	if ((uc->uc_flags & LINUX_UC_FP_XSTATE) != 0)
347cbbac560SDmitry Chagin 		return (linux_xrstor(td, &mc, &uc->uc_mcontext));
348cbbac560SDmitry Chagin 	else
349920184edSDmitry Chagin 		return (linux_fxrstor(td, &mc, &uc->uc_mcontext));
350920184edSDmitry Chagin }
351920184edSDmitry Chagin 
352b2f58791SDmitry Chagin /*
353b2f58791SDmitry Chagin  * Copied from amd64/amd64/machdep.c
354b2f58791SDmitry Chagin  */
355b2f58791SDmitry Chagin int
linux_rt_sigreturn(struct thread * td,struct linux_rt_sigreturn_args * args)356b2f58791SDmitry Chagin linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
357b2f58791SDmitry Chagin {
358b2f58791SDmitry Chagin 	struct proc *p;
359a95cb95eSDmitry Chagin 	struct l_rt_sigframe sf;
360b2f58791SDmitry Chagin 	struct l_sigcontext *context;
361b2f58791SDmitry Chagin 	struct trapframe *regs;
362b2f58791SDmitry Chagin 	unsigned long rflags;
3632ab9b59fSDmitry Chagin 	sigset_t bmask;
364920184edSDmitry Chagin 	int error;
365b2f58791SDmitry Chagin 	ksiginfo_t ksi;
366b2f58791SDmitry Chagin 
367b2f58791SDmitry Chagin 	regs = td->td_frame;
368a95cb95eSDmitry Chagin 	error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf));
369b2f58791SDmitry Chagin 	if (error != 0)
370b2f58791SDmitry Chagin 		return (error);
371b2f58791SDmitry Chagin 
372b2f58791SDmitry Chagin 	p = td->td_proc;
373a95cb95eSDmitry Chagin 	context = &sf.sf_uc.uc_mcontext;
374b2f58791SDmitry Chagin 	rflags = context->sc_rflags;
375b2f58791SDmitry Chagin 
376b2f58791SDmitry Chagin 	/*
377b2f58791SDmitry Chagin 	 * Don't allow users to change privileged or reserved flags.
378b2f58791SDmitry Chagin 	 */
379b2f58791SDmitry Chagin 	/*
380b2f58791SDmitry Chagin 	 * XXX do allow users to change the privileged flag PSL_RF.
381b2f58791SDmitry Chagin 	 * The cpu sets PSL_RF in tf_rflags for faults.  Debuggers
382b2f58791SDmitry Chagin 	 * should sometimes set it there too.  tf_rflags is kept in
383b2f58791SDmitry Chagin 	 * the signal context during signal handling and there is no
384b2f58791SDmitry Chagin 	 * other place to remember it, so the PSL_RF bit may be
385b2f58791SDmitry Chagin 	 * corrupted by the signal handler without us knowing.
386b2f58791SDmitry Chagin 	 * Corruption of the PSL_RF bit at worst causes one more or
387b2f58791SDmitry Chagin 	 * one less debugger trap, so allowing it is fairly harmless.
388b2f58791SDmitry Chagin 	 */
389a42d362bSKonstantin Belousov 	if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
3902e79a216SKonstantin Belousov 		uprintf("pid %d comm %s linux mangled rflags %#lx\n",
3912e79a216SKonstantin Belousov 		    p->p_pid, p->p_comm, rflags);
392b2f58791SDmitry Chagin 		return (EINVAL);
393b2f58791SDmitry Chagin 	}
394b2f58791SDmitry Chagin 
395b2f58791SDmitry Chagin 	/*
396b2f58791SDmitry Chagin 	 * Don't allow users to load a valid privileged %cs.  Let the
397b2f58791SDmitry Chagin 	 * hardware check for invalid selectors, excess privilege in
398b2f58791SDmitry Chagin 	 * other selectors, invalid %eip's and invalid %esp's.
399b2f58791SDmitry Chagin 	 */
400b2f58791SDmitry Chagin 	if (!CS_SECURE(context->sc_cs)) {
4012e79a216SKonstantin Belousov 		uprintf("pid %d comm %s linux mangled cs %#x\n",
4022e79a216SKonstantin Belousov 		    p->p_pid, p->p_comm, context->sc_cs);
403b2f58791SDmitry Chagin 		ksiginfo_init_trap(&ksi);
404b2f58791SDmitry Chagin 		ksi.ksi_signo = SIGBUS;
405b2f58791SDmitry Chagin 		ksi.ksi_code = BUS_OBJERR;
406b2f58791SDmitry Chagin 		ksi.ksi_trapno = T_PROTFLT;
407b2f58791SDmitry Chagin 		ksi.ksi_addr = (void *)regs->tf_rip;
408b2f58791SDmitry Chagin 		trapsignal(td, &ksi);
409b2f58791SDmitry Chagin 		return (EINVAL);
410b2f58791SDmitry Chagin 	}
411b2f58791SDmitry Chagin 
412a95cb95eSDmitry Chagin 	linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask);
4132ab9b59fSDmitry Chagin 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
414b2f58791SDmitry Chagin 
415b2f58791SDmitry Chagin 	regs->tf_rdi    = context->sc_rdi;
416b2f58791SDmitry Chagin 	regs->tf_rsi    = context->sc_rsi;
417b2f58791SDmitry Chagin 	regs->tf_rdx    = context->sc_rdx;
418b2f58791SDmitry Chagin 	regs->tf_rbp    = context->sc_rbp;
419b2f58791SDmitry Chagin 	regs->tf_rbx    = context->sc_rbx;
420b2f58791SDmitry Chagin 	regs->tf_rcx    = context->sc_rcx;
421b2f58791SDmitry Chagin 	regs->tf_rax    = context->sc_rax;
422b2f58791SDmitry Chagin 	regs->tf_rip    = context->sc_rip;
423b2f58791SDmitry Chagin 	regs->tf_rsp    = context->sc_rsp;
424b2f58791SDmitry Chagin 	regs->tf_r8     = context->sc_r8;
425b2f58791SDmitry Chagin 	regs->tf_r9     = context->sc_r9;
426b2f58791SDmitry Chagin 	regs->tf_r10    = context->sc_r10;
427b2f58791SDmitry Chagin 	regs->tf_r11    = context->sc_r11;
428b2f58791SDmitry Chagin 	regs->tf_r12    = context->sc_r12;
429b2f58791SDmitry Chagin 	regs->tf_r13    = context->sc_r13;
430b2f58791SDmitry Chagin 	regs->tf_r14    = context->sc_r14;
431b2f58791SDmitry Chagin 	regs->tf_r15    = context->sc_r15;
432b2f58791SDmitry Chagin 	regs->tf_cs     = context->sc_cs;
433b2f58791SDmitry Chagin 	regs->tf_err    = context->sc_err;
434b2f58791SDmitry Chagin 	regs->tf_rflags = rflags;
435b2f58791SDmitry Chagin 
436920184edSDmitry Chagin 	error = linux_copyin_fpstate(td, &sf.sf_uc);
437a95cb95eSDmitry Chagin 	if (error != 0) {
438a95cb95eSDmitry Chagin 		uprintf("pid %d comm %s linux can't restore fpu state %d\n",
439a95cb95eSDmitry Chagin 		    p->p_pid, p->p_comm, error);
440a95cb95eSDmitry Chagin 		return (error);
441a95cb95eSDmitry Chagin 	}
442a95cb95eSDmitry Chagin 
443b2f58791SDmitry Chagin 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
444b2f58791SDmitry Chagin 	return (EJUSTRETURN);
445b2f58791SDmitry Chagin }
446b2f58791SDmitry Chagin 
447920184edSDmitry Chagin static int
linux_fxsave(mcontext_t * mcp,void * ufp)448920184edSDmitry Chagin linux_fxsave(mcontext_t *mcp, void *ufp)
449920184edSDmitry Chagin {
450920184edSDmitry Chagin 	struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
451920184edSDmitry Chagin 
452920184edSDmitry Chagin 	bzero(&fx->reserved2[0], sizeof(fx->reserved2));
453920184edSDmitry Chagin 	return (copyout(fx, ufp, sizeof(*fx)));
454920184edSDmitry Chagin }
455920184edSDmitry Chagin 
456920184edSDmitry Chagin static int
linux_xsave(mcontext_t * mcp,char * xfpusave,char * ufp)457cbbac560SDmitry Chagin linux_xsave(mcontext_t *mcp, char *xfpusave, char *ufp)
458cbbac560SDmitry Chagin {
459cbbac560SDmitry Chagin 	struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
460cbbac560SDmitry Chagin 	uint32_t magic2;
461cbbac560SDmitry Chagin 	int error;
462cbbac560SDmitry Chagin 
463cbbac560SDmitry Chagin 	/* Legacy region of an xsave area. */
464cbbac560SDmitry Chagin 	fx->sw_reserved.magic1 = LINUX_FP_XSTATE_MAGIC1;
465cbbac560SDmitry Chagin 	fx->sw_reserved.xstate_size = mcp->mc_xfpustate_len + sizeof(*fx);
466cbbac560SDmitry Chagin 	fx->sw_reserved.extended_size = fx->sw_reserved.xstate_size +
467cbbac560SDmitry Chagin 	    LINUX_FP_XSTATE_MAGIC2_SIZE;
468cbbac560SDmitry Chagin 	fx->sw_reserved.xfeatures = xsave_mask;
469cbbac560SDmitry Chagin 
470cbbac560SDmitry Chagin 	error = copyout(fx, ufp, sizeof(*fx));
471cbbac560SDmitry Chagin 	if (error != 0)
472cbbac560SDmitry Chagin 		return (error);
473cbbac560SDmitry Chagin 	ufp += sizeof(*fx);
474cbbac560SDmitry Chagin 
475cbbac560SDmitry Chagin 	/* Extended region of an xsave area. */
476cbbac560SDmitry Chagin 	error = copyout(xfpusave, ufp, mcp->mc_xfpustate_len);
477cbbac560SDmitry Chagin 	if (error != 0)
478cbbac560SDmitry Chagin 		return (error);
479cbbac560SDmitry Chagin 
480cbbac560SDmitry Chagin 	/* Linux specific end of xsave area marker. */
481cbbac560SDmitry Chagin 	ufp += mcp->mc_xfpustate_len;
482cbbac560SDmitry Chagin 	magic2 = LINUX_FP_XSTATE_MAGIC2;
483cbbac560SDmitry Chagin 	return (copyout(&magic2, ufp, LINUX_FP_XSTATE_MAGIC2_SIZE));
484cbbac560SDmitry Chagin }
485cbbac560SDmitry Chagin 
486cbbac560SDmitry Chagin static int
linux_copyout_fpstate(struct thread * td,struct l_ucontext * uc,char ** sp)487920184edSDmitry Chagin linux_copyout_fpstate(struct thread *td, struct l_ucontext *uc, char **sp)
488920184edSDmitry Chagin {
489cbbac560SDmitry Chagin 	size_t xfpusave_len;
490cbbac560SDmitry Chagin 	char *xfpusave;
491920184edSDmitry Chagin 	mcontext_t mc;
492920184edSDmitry Chagin 	char *ufp = *sp;
493920184edSDmitry Chagin 
494cbbac560SDmitry Chagin 	get_fpcontext(td, &mc, &xfpusave, &xfpusave_len);
495920184edSDmitry Chagin 	KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present"));
496920184edSDmitry Chagin 
497cbbac560SDmitry Chagin 	/* Room for fxsave area. */
498920184edSDmitry Chagin 	ufp -= sizeof(struct l_fpstate);
499cbbac560SDmitry Chagin 	if (xfpusave != NULL) {
500cbbac560SDmitry Chagin 		/* Room for xsave area. */
501cbbac560SDmitry Chagin 		ufp -= (xfpusave_len + LINUX_FP_XSTATE_MAGIC2_SIZE);
502cbbac560SDmitry Chagin 		uc->uc_flags |= LINUX_UC_FP_XSTATE;
503cbbac560SDmitry Chagin 	}
504920184edSDmitry Chagin 	*sp = ufp = (char *)((unsigned long)ufp & ~0x3Ful);
505920184edSDmitry Chagin 
506cbbac560SDmitry Chagin 	if (xfpusave != NULL)
507cbbac560SDmitry Chagin 		return (linux_xsave(&mc, xfpusave, ufp));
508cbbac560SDmitry Chagin 	else
509920184edSDmitry Chagin 		return (linux_fxsave(&mc, ufp));
510920184edSDmitry Chagin }
511920184edSDmitry Chagin 
512b2f58791SDmitry Chagin /*
513b2f58791SDmitry Chagin  * copied from amd64/amd64/machdep.c
514b2f58791SDmitry Chagin  *
515b2f58791SDmitry Chagin  * Send an interrupt to process.
516b2f58791SDmitry Chagin  */
517b2f58791SDmitry Chagin static void
linux_rt_sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)518b2f58791SDmitry Chagin linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
519b2f58791SDmitry Chagin {
520b2f58791SDmitry Chagin 	struct l_rt_sigframe sf, *sfp;
521b2f58791SDmitry Chagin 	struct proc *p;
522b2f58791SDmitry Chagin 	struct thread *td;
523b2f58791SDmitry Chagin 	struct sigacts *psp;
52484617f6fSDmitry Chagin 	char *sp;
525b2f58791SDmitry Chagin 	struct trapframe *regs;
526b2f58791SDmitry Chagin 	int sig, code;
527920184edSDmitry Chagin 	int oonstack, issiginfo;
528b2f58791SDmitry Chagin 
529b2f58791SDmitry Chagin 	td = curthread;
530b2f58791SDmitry Chagin 	p = td->td_proc;
531b2f58791SDmitry Chagin 	PROC_LOCK_ASSERT(p, MA_OWNED);
532eca368ecSDmitry Chagin 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
533b2f58791SDmitry Chagin 	psp = p->p_sigacts;
534109fd18aSDmitry Chagin 	issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
535b2f58791SDmitry Chagin 	code = ksi->ksi_code;
536b2f58791SDmitry Chagin 	mtx_assert(&psp->ps_mtx, MA_OWNED);
537b2f58791SDmitry Chagin 	regs = td->td_frame;
538b2f58791SDmitry Chagin 	oonstack = sigonstack(regs->tf_rsp);
539b2f58791SDmitry Chagin 
540b2f58791SDmitry Chagin 	LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
541b2f58791SDmitry Chagin 	    catcher, sig, mask, code);
542b2f58791SDmitry Chagin 
543b2f58791SDmitry Chagin 	bzero(&sf, sizeof(sf));
5446e826d27SDmitry Chagin 	sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
5456e826d27SDmitry Chagin 	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
5466e826d27SDmitry Chagin 	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
547b2f58791SDmitry Chagin 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
548b2f58791SDmitry Chagin 
5496ad07a4bSDmitry Chagin 	/* Allocate space for the signal handler context. */
5506ad07a4bSDmitry Chagin 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
5516ad07a4bSDmitry Chagin 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
55284617f6fSDmitry Chagin 		sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
5536ad07a4bSDmitry Chagin 	} else
55484617f6fSDmitry Chagin 		sp = (char *)regs->tf_rsp - 128;
5556ad07a4bSDmitry Chagin 
5566ad07a4bSDmitry Chagin 	mtx_unlock(&psp->ps_mtx);
5576ad07a4bSDmitry Chagin 	PROC_UNLOCK(p);
5586ad07a4bSDmitry Chagin 
559920184edSDmitry Chagin 	if (linux_copyout_fpstate(td, &sf.sf_uc, &sp) != 0) {
560920184edSDmitry Chagin 		uprintf("pid %d comm %s linux can't save fpu state, killing\n",
561920184edSDmitry Chagin 		    p->p_pid, p->p_comm);
562920184edSDmitry Chagin 		PROC_LOCK(p);
563920184edSDmitry Chagin 		sigexit(td, SIGILL);
564920184edSDmitry Chagin 	}
565920184edSDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_fpstate = (register_t)sp;
566920184edSDmitry Chagin 
5676ad07a4bSDmitry Chagin 	/* Make room, keeping the stack aligned. */
5686ad07a4bSDmitry Chagin 	sp -= sizeof(struct l_rt_sigframe);
5696ad07a4bSDmitry Chagin 	sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
5706ad07a4bSDmitry Chagin 
5716ad07a4bSDmitry Chagin 	/* Save user context. */
5726ad07a4bSDmitry Chagin 	bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
5736ad07a4bSDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_mask   = sf.sf_uc.uc_sigmask;
5746e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rdi    = regs->tf_rdi;
5756e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rsi    = regs->tf_rsi;
5766e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rdx    = regs->tf_rdx;
5776e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rbp    = regs->tf_rbp;
5786e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rbx    = regs->tf_rbx;
5796e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rcx    = regs->tf_rcx;
5806e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rax    = regs->tf_rax;
5816e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rip    = regs->tf_rip;
5826e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rsp    = regs->tf_rsp;
5836e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r8     = regs->tf_r8;
5846e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r9     = regs->tf_r9;
5856e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r10    = regs->tf_r10;
5866e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r11    = regs->tf_r11;
5876e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r12    = regs->tf_r12;
5886e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r13    = regs->tf_r13;
5896e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r14    = regs->tf_r14;
5906e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_r15    = regs->tf_r15;
5916e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
5926e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
5936e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
5946e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
5956e826d27SDmitry Chagin 	sf.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
596b2f58791SDmitry Chagin 
597109fd18aSDmitry Chagin 	/* Translate the signal. */
598109fd18aSDmitry Chagin 	sig = bsd_to_linux_signal(sig);
599109fd18aSDmitry Chagin 	/* Fill in POSIX parts. */
600109fd18aSDmitry Chagin 	siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
601109fd18aSDmitry Chagin 
6024ba25759SEd Maste 	/* Copy the sigframe out to the user's stack. */
603b2f58791SDmitry Chagin 	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
604f7b04c53SEdward Tomasz Napierala 		uprintf("pid %d comm %s has trashed its stack, killing\n",
605f7b04c53SEdward Tomasz Napierala 		    p->p_pid, p->p_comm);
606b2f58791SDmitry Chagin 		PROC_LOCK(p);
607b2f58791SDmitry Chagin 		sigexit(td, SIGILL);
608b2f58791SDmitry Chagin 	}
609b2f58791SDmitry Chagin 
610a95cb95eSDmitry Chagin 	fpstate_drop(td);
611109fd18aSDmitry Chagin 	/* Build the argument list for the signal handler. */
612109fd18aSDmitry Chagin 	regs->tf_rdi = sig;			/* arg 1 in %rdi */
613109fd18aSDmitry Chagin 	regs->tf_rax = 0;
614109fd18aSDmitry Chagin 	if (issiginfo) {
615109fd18aSDmitry Chagin 		regs->tf_rsi = (register_t)&sfp->sf_si;	/* arg 2 in %rsi */
616109fd18aSDmitry Chagin 		regs->tf_rdx = (register_t)&sfp->sf_uc;	/* arg 3 in %rdx */
617109fd18aSDmitry Chagin 	} else {
618109fd18aSDmitry Chagin 		regs->tf_rsi = 0;
619109fd18aSDmitry Chagin 		regs->tf_rdx = 0;
620109fd18aSDmitry Chagin 	}
621109fd18aSDmitry Chagin 	regs->tf_rcx = (register_t)catcher;
622b2f58791SDmitry Chagin 	regs->tf_rsp = (long)sfp;
623b2f58791SDmitry Chagin 	regs->tf_rip = linux_rt_sigcode;
624b2f58791SDmitry Chagin 	regs->tf_rflags &= ~(PSL_T | PSL_D);
625b2f58791SDmitry Chagin 	regs->tf_cs = _ucodesel;
626b2f58791SDmitry Chagin 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
627b2f58791SDmitry Chagin 	PROC_LOCK(p);
628b2f58791SDmitry Chagin 	mtx_lock(&psp->ps_mtx);
629b2f58791SDmitry Chagin }
630b2f58791SDmitry Chagin 
631038c7205SDmitry Chagin #define	LINUX_VSYSCALL_START		(-10UL << 20)
632038c7205SDmitry Chagin #define	LINUX_VSYSCALL_SZ		1024
633038c7205SDmitry Chagin 
634038c7205SDmitry Chagin const unsigned long linux_vsyscall_vector[] = {
635038c7205SDmitry Chagin 	LINUX_SYS_gettimeofday,
636038c7205SDmitry Chagin 	LINUX_SYS_linux_time,
6370cfac4d5SMark Johnston 	LINUX_SYS_linux_getcpu,
638038c7205SDmitry Chagin };
639038c7205SDmitry Chagin 
640038c7205SDmitry Chagin static int
linux_vsyscall(struct thread * td)641038c7205SDmitry Chagin linux_vsyscall(struct thread *td)
642038c7205SDmitry Chagin {
643038c7205SDmitry Chagin 	struct trapframe *frame;
644038c7205SDmitry Chagin 	uint64_t retqaddr;
645038c7205SDmitry Chagin 	int code, traced;
646038c7205SDmitry Chagin 	int error;
647038c7205SDmitry Chagin 
648038c7205SDmitry Chagin 	frame = td->td_frame;
649038c7205SDmitry Chagin 
6504ba25759SEd Maste 	/* Check %rip for vsyscall area. */
651038c7205SDmitry Chagin 	if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
652038c7205SDmitry Chagin 		return (EINVAL);
653038c7205SDmitry Chagin 	if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
654038c7205SDmitry Chagin 		return (EINVAL);
655038c7205SDmitry Chagin 	code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
656038c7205SDmitry Chagin 	if (code >= nitems(linux_vsyscall_vector))
657038c7205SDmitry Chagin 		return (EINVAL);
658038c7205SDmitry Chagin 
659038c7205SDmitry Chagin 	/*
660038c7205SDmitry Chagin 	 * vsyscall called as callq *(%rax), so we must
6614ba25759SEd Maste 	 * use return address from %rsp and also fixup %rsp.
662038c7205SDmitry Chagin 	 */
663038c7205SDmitry Chagin 	error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
664038c7205SDmitry Chagin 	if (error)
665038c7205SDmitry Chagin 		return (error);
666038c7205SDmitry Chagin 
667038c7205SDmitry Chagin 	frame->tf_rip = retqaddr;
668038c7205SDmitry Chagin 	frame->tf_rax = linux_vsyscall_vector[code];
669038c7205SDmitry Chagin 	frame->tf_rsp += 8;
670038c7205SDmitry Chagin 
671038c7205SDmitry Chagin 	traced = (frame->tf_flags & PSL_T);
672038c7205SDmitry Chagin 
673038c7205SDmitry Chagin 	amd64_syscall(td, traced);
674038c7205SDmitry Chagin 
675038c7205SDmitry Chagin 	return (0);
676038c7205SDmitry Chagin }
677038c7205SDmitry Chagin 
678b2f58791SDmitry Chagin struct sysentvec elf_linux_sysvec = {
679b2f58791SDmitry Chagin 	.sv_size	= LINUX_SYS_MAXSYSCALL,
680b2f58791SDmitry Chagin 	.sv_table	= linux_sysent,
6819e550625SDmitry Chagin 	.sv_fixup	= __elfN(freebsd_fixup),
682b2f58791SDmitry Chagin 	.sv_sendsig	= linux_rt_sendsig,
6839931033bSDmitry Chagin 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
684b2f58791SDmitry Chagin 	.sv_szsigcode	= &linux_szsigcode,
685b2f58791SDmitry Chagin 	.sv_name	= "Linux ELF64",
686b2f58791SDmitry Chagin 	.sv_coredump	= elf64_coredump,
687435754a5SEdward Tomasz Napierala 	.sv_elf_core_osabi = ELFOSABI_NONE,
688447636e4SEdward Tomasz Napierala 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
689447636e4SEdward Tomasz Napierala 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
690b2f58791SDmitry Chagin 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
691b2f58791SDmitry Chagin 	.sv_minuser	= VM_MIN_ADDRESS,
6929ce875d9SKonstantin Belousov 	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA48,
6939931033bSDmitry Chagin 	.sv_usrstack	= LINUX_USRSTACK_LA48,
6949931033bSDmitry Chagin 	.sv_psstrings	= LINUX_PS_STRINGS_LA48,
6953fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
696b2f58791SDmitry Chagin 	.sv_stackprot	= VM_PROT_ALL,
6977d8c9839SDmitry Chagin 	.sv_copyout_auxargs = __linuxN(copyout_auxargs),
6986039e966SDmitry Chagin 	.sv_copyout_strings = __linuxN(copyout_strings),
699b2f58791SDmitry Chagin 	.sv_setregs	= linux_exec_setregs,
700b2f58791SDmitry Chagin 	.sv_fixlimit	= NULL,
701b2f58791SDmitry Chagin 	.sv_maxssiz	= NULL,
702870e197dSKonstantin Belousov 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
7039931033bSDmitry Chagin 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
704b2f58791SDmitry Chagin 	.sv_set_syscall_retval = linux_set_syscall_retval,
705b2f58791SDmitry Chagin 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
7061da65dcbSMitchell Horne 	.sv_syscallnames = linux_syscallnames,
7079931033bSDmitry Chagin 	.sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
708b2f58791SDmitry Chagin 	.sv_shared_page_len = PAGE_SIZE,
709b2f58791SDmitry Chagin 	.sv_schedtail	= linux_schedtail,
710038c7205SDmitry Chagin 	.sv_thread_detach = linux_thread_detach,
711038c7205SDmitry Chagin 	.sv_trap	= linux_vsyscall,
7127d8c9839SDmitry Chagin 	.sv_hwcap	= NULL,
7137d8c9839SDmitry Chagin 	.sv_hwcap2	= NULL,
7145fd9cd53SDmitry Chagin 	.sv_onexec	= linux_on_exec_vmspace,
7154815f175SKonstantin Belousov 	.sv_onexit	= linux_on_exit,
7164815f175SKonstantin Belousov 	.sv_ontdexit	= linux_thread_dtor,
717598f6fb4SKonstantin Belousov 	.sv_setid_allowed = &linux_setid_allowed_query,
718de8374dfSDmitry Chagin 	.sv_set_fork_retval = linux_set_fork_retval,
719b2f58791SDmitry Chagin };
720b2f58791SDmitry Chagin 
7215fd9cd53SDmitry Chagin static int
linux_on_exec_vmspace(struct proc * p,struct image_params * imgp)7225fd9cd53SDmitry Chagin linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
7235fd9cd53SDmitry Chagin {
7249931033bSDmitry Chagin 	int error;
7255fd9cd53SDmitry Chagin 
7269931033bSDmitry Chagin 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
7279931033bSDmitry Chagin 	    LINUX_VDSOPAGE_SIZE, imgp);
7289931033bSDmitry Chagin 	if (error == 0)
729fd745e1dSDmitry Chagin 		error = linux_on_exec(p, imgp);
7309931033bSDmitry Chagin 	return (error);
7315fd9cd53SDmitry Chagin }
7325fd9cd53SDmitry Chagin 
73309cffde9SDmitry Chagin /*
73409cffde9SDmitry Chagin  * linux_vdso_install() and linux_exec_sysvec_init() must be called
73509cffde9SDmitry Chagin  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
73609cffde9SDmitry Chagin  */
737b2f58791SDmitry Chagin static void
linux_exec_sysvec_init(void * param)7389931033bSDmitry Chagin linux_exec_sysvec_init(void *param)
7399931033bSDmitry Chagin {
7409931033bSDmitry Chagin 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
7419931033bSDmitry Chagin 	struct sysentvec *sv;
7429931033bSDmitry Chagin 	ptrdiff_t tkoff;
7439931033bSDmitry Chagin 
7449931033bSDmitry Chagin 	sv = param;
7459931033bSDmitry Chagin 	amd64_lower_shared_page(sv);
7469931033bSDmitry Chagin 	/* Fill timekeep_base */
7479931033bSDmitry Chagin 	exec_sysvec_init(sv);
7489931033bSDmitry Chagin 
7499931033bSDmitry Chagin 	tkoff = kern_timekeep_base - linux_vdso_base;
7509931033bSDmitry Chagin 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
751361971fbSKornel Dulęba 	*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
7529931033bSDmitry Chagin 
7539931033bSDmitry Chagin 	tkoff = kern_tsc_selector - linux_vdso_base;
7549931033bSDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
7559931033bSDmitry Chagin 	*ktsc_selector = linux_vdso_tsc_selector_idx();
7569931033bSDmitry Chagin 	if (bootverbose)
7579931033bSDmitry Chagin 		printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
7585a6a4fb2SDmitry Chagin 
7595a6a4fb2SDmitry Chagin 	tkoff = kern_cpu_selector - linux_vdso_base;
7605a6a4fb2SDmitry Chagin 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
7615a6a4fb2SDmitry Chagin 	*ktsc_selector = linux_vdso_cpu_selector_idx();
7625a6a4fb2SDmitry Chagin 	if (bootverbose)
7635a6a4fb2SDmitry Chagin 		printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
7649931033bSDmitry Chagin }
76509cffde9SDmitry Chagin SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
7669931033bSDmitry Chagin     linux_exec_sysvec_init, &elf_linux_sysvec);
7679931033bSDmitry Chagin 
7689931033bSDmitry Chagin static void
linux_vdso_install(const void * param)769a543556cSDmitry Chagin linux_vdso_install(const void *param)
770b2f58791SDmitry Chagin {
7719931033bSDmitry Chagin 	char *vdso_start = &_binary_linux_vdso_so_o_start;
7729931033bSDmitry Chagin 	char *vdso_end = &_binary_linux_vdso_so_o_end;
773b2f58791SDmitry Chagin 
7749931033bSDmitry Chagin 	linux_szsigcode = vdso_end - vdso_start;
7759931033bSDmitry Chagin 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
776cd155b56SDon Lewis 
7779931033bSDmitry Chagin 	linux_vdso_base = LINUX_VDSOPAGE_LA48;
7789931033bSDmitry Chagin 	if (hw_lower_amd64_sharedpage != 0)
7799931033bSDmitry Chagin 		linux_vdso_base -= PAGE_SIZE;
780b2f58791SDmitry Chagin 
7819931033bSDmitry Chagin 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
782b2f58791SDmitry Chagin 
7839931033bSDmitry Chagin 	linux_vdso_obj = __elfN(linux_shared_page_init)
7849931033bSDmitry Chagin 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
7859931033bSDmitry Chagin 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
786b2f58791SDmitry Chagin 
7879931033bSDmitry Chagin 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
788b2f58791SDmitry Chagin }
78909cffde9SDmitry Chagin SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
790f8268d4dSEd Maste     linux_vdso_install, NULL);
791b2f58791SDmitry Chagin 
792b2f58791SDmitry Chagin static void
linux_vdso_deinstall(const void * param)793a543556cSDmitry Chagin linux_vdso_deinstall(const void *param)
794b2f58791SDmitry Chagin {
795b2f58791SDmitry Chagin 
7969931033bSDmitry Chagin 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
7979931033bSDmitry Chagin 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
7987b194b3dSEd Maste }
799b2f58791SDmitry Chagin SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
800f8268d4dSEd Maste     linux_vdso_deinstall, NULL);
801b2f58791SDmitry Chagin 
8029931033bSDmitry Chagin static void
linux_vdso_reloc(char * mapping,Elf_Addr offset)8039931033bSDmitry Chagin linux_vdso_reloc(char *mapping, Elf_Addr offset)
8049931033bSDmitry Chagin {
8059931033bSDmitry Chagin 	const Elf_Ehdr *ehdr;
8069931033bSDmitry Chagin 	const Elf_Shdr *shdr;
8079931033bSDmitry Chagin 	Elf64_Addr *where, val;
8089931033bSDmitry Chagin 	Elf_Size rtype, symidx;
8099931033bSDmitry Chagin 	const Elf_Rela *rela;
8109931033bSDmitry Chagin 	Elf_Addr addr, addend;
8119931033bSDmitry Chagin 	int relacnt;
8129931033bSDmitry Chagin 	int i, j;
8139931033bSDmitry Chagin 
8149931033bSDmitry Chagin 	MPASS(offset != 0);
8159931033bSDmitry Chagin 
8169931033bSDmitry Chagin 	relacnt = 0;
8179931033bSDmitry Chagin 	ehdr = (const Elf_Ehdr *)mapping;
8189931033bSDmitry Chagin 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
8199931033bSDmitry Chagin 	for (i = 0; i < ehdr->e_shnum; i++)
8209931033bSDmitry Chagin 	{
8219931033bSDmitry Chagin 		switch (shdr[i].sh_type) {
8229931033bSDmitry Chagin 		case SHT_REL:
8239931033bSDmitry Chagin 			printf("Linux x86_64 vDSO: unexpected Rel section\n");
8249931033bSDmitry Chagin 			break;
8259931033bSDmitry Chagin 		case SHT_RELA:
8269931033bSDmitry Chagin 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
8279931033bSDmitry Chagin 			relacnt = shdr[i].sh_size / sizeof(*rela);
8289931033bSDmitry Chagin 		}
8299931033bSDmitry Chagin 	}
8309931033bSDmitry Chagin 
8319931033bSDmitry Chagin 	for (j = 0; j < relacnt; j++, rela++) {
8329931033bSDmitry Chagin 		where = (Elf_Addr *)(mapping + rela->r_offset);
8339931033bSDmitry Chagin 		addend = rela->r_addend;
8349931033bSDmitry Chagin 		rtype = ELF_R_TYPE(rela->r_info);
8359931033bSDmitry Chagin 		symidx = ELF_R_SYM(rela->r_info);
8369931033bSDmitry Chagin 
8379931033bSDmitry Chagin 		switch (rtype) {
8389931033bSDmitry Chagin 		case R_X86_64_NONE:	/* none */
8399931033bSDmitry Chagin 			break;
8409931033bSDmitry Chagin 
8419931033bSDmitry Chagin 		case R_X86_64_RELATIVE:	/* B + A */
8429931033bSDmitry Chagin 			addr = (Elf_Addr)(offset + addend);
8439931033bSDmitry Chagin 			val = addr;
8449931033bSDmitry Chagin 			if (*where != val)
8459931033bSDmitry Chagin 				*where = val;
8469931033bSDmitry Chagin 			break;
8479931033bSDmitry Chagin 		case R_X86_64_IRELATIVE:
8489931033bSDmitry Chagin 			printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
8499931033bSDmitry Chagin 			    "symbol index %ld\n", symidx);
8509931033bSDmitry Chagin 			break;
8519931033bSDmitry Chagin 		default:
8529931033bSDmitry Chagin 			printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
8539931033bSDmitry Chagin 			    "symbol index %ld\n", rtype, symidx);
8549931033bSDmitry Chagin 		}
8559931033bSDmitry Chagin 	}
8569931033bSDmitry Chagin }
8579931033bSDmitry Chagin 
858b2f58791SDmitry Chagin static Elf_Brandnote linux64_brandnote = {
85995b86034SDmitry Chagin 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
860b2f58791SDmitry Chagin 	.hdr.n_descsz	= 16,
861b2f58791SDmitry Chagin 	.hdr.n_type	= 1,
86295b86034SDmitry Chagin 	.vendor		= GNU_ABI_VENDOR,
863b2f58791SDmitry Chagin 	.flags		= BN_TRANSLATE_OSREL,
864b2f58791SDmitry Chagin 	.trans_osrel	= linux_trans_osrel
865b2f58791SDmitry Chagin };
866b2f58791SDmitry Chagin 
867b2f58791SDmitry Chagin static Elf64_Brandinfo linux_glibc2brand = {
868b2f58791SDmitry Chagin 	.brand		= ELFOSABI_LINUX,
869b2f58791SDmitry Chagin 	.machine	= EM_X86_64,
870b2f58791SDmitry Chagin 	.compat_3_brand	= "Linux",
871b2f58791SDmitry Chagin 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
872b2f58791SDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
873b2f58791SDmitry Chagin 	.interp_newpath	= NULL,
874b2f58791SDmitry Chagin 	.brand_note	= &linux64_brandnote,
875b2f58791SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
876b2f58791SDmitry Chagin };
877b2f58791SDmitry Chagin 
878b2f58791SDmitry Chagin static Elf64_Brandinfo linux_glibc2brandshort = {
879b2f58791SDmitry Chagin 	.brand		= ELFOSABI_LINUX,
880b2f58791SDmitry Chagin 	.machine	= EM_X86_64,
881b2f58791SDmitry Chagin 	.compat_3_brand	= "Linux",
882b2f58791SDmitry Chagin 	.interp_path	= "/lib64/ld-linux.so.2",
883b2f58791SDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
884b2f58791SDmitry Chagin 	.interp_newpath	= NULL,
885b2f58791SDmitry Chagin 	.brand_note	= &linux64_brandnote,
886b2f58791SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
887b2f58791SDmitry Chagin };
888b2f58791SDmitry Chagin 
889a0c59c7aSDmitry Chagin static Elf64_Brandinfo linux_muslbrand = {
890a0c59c7aSDmitry Chagin 	.brand		= ELFOSABI_LINUX,
891a0c59c7aSDmitry Chagin 	.machine	= EM_X86_64,
892a0c59c7aSDmitry Chagin 	.compat_3_brand	= "Linux",
893a0c59c7aSDmitry Chagin 	.interp_path	= "/lib/ld-musl-x86_64.so.1",
894a0c59c7aSDmitry Chagin 	.sysvec		= &elf_linux_sysvec,
895a0c59c7aSDmitry Chagin 	.interp_newpath	= NULL,
896a0c59c7aSDmitry Chagin 	.brand_note	= &linux64_brandnote,
897cf8d74e3SDmitry Chagin 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
898cf8d74e3SDmitry Chagin 			    LINUX_BI_FUTEX_REQUEUE
899a0c59c7aSDmitry Chagin };
900a0c59c7aSDmitry Chagin 
901*be707ee0SKonstantin Belousov static Elf64_Brandinfo *linux_brandlist[] = {
902b2f58791SDmitry Chagin 	&linux_glibc2brand,
903b2f58791SDmitry Chagin 	&linux_glibc2brandshort,
904a0c59c7aSDmitry Chagin 	&linux_muslbrand,
905b2f58791SDmitry Chagin 	NULL
906b2f58791SDmitry Chagin };
907b2f58791SDmitry Chagin 
908b2f58791SDmitry Chagin static int
linux64_elf_modevent(module_t mod,int type,void * data)909b2f58791SDmitry Chagin linux64_elf_modevent(module_t mod, int type, void *data)
910b2f58791SDmitry Chagin {
911b2f58791SDmitry Chagin 	Elf64_Brandinfo **brandinfo;
912b2f58791SDmitry Chagin 	int error;
913b2f58791SDmitry Chagin 	struct linux_ioctl_handler **lihp;
914b2f58791SDmitry Chagin 
915b2f58791SDmitry Chagin 	error = 0;
916b2f58791SDmitry Chagin 
917b2f58791SDmitry Chagin 	switch(type) {
918b2f58791SDmitry Chagin 	case MOD_LOAD:
919b2f58791SDmitry Chagin 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
920b2f58791SDmitry Chagin 		     ++brandinfo)
921b2f58791SDmitry Chagin 			if (elf64_insert_brand_entry(*brandinfo) < 0)
922b2f58791SDmitry Chagin 				error = EINVAL;
923b2f58791SDmitry Chagin 		if (error == 0) {
924b2f58791SDmitry Chagin 			SET_FOREACH(lihp, linux_ioctl_handler_set)
925b2f58791SDmitry Chagin 				linux_ioctl_register_handler(*lihp);
926b2f58791SDmitry Chagin 			stclohz = (stathz ? stathz : hz);
927b2f58791SDmitry Chagin 			if (bootverbose)
928b2f58791SDmitry Chagin 				printf("Linux x86-64 ELF exec handler installed\n");
929b2f58791SDmitry Chagin 		} else
930b2f58791SDmitry Chagin 			printf("cannot insert Linux x86-64 ELF brand handler\n");
931b2f58791SDmitry Chagin 		break;
932b2f58791SDmitry Chagin 	case MOD_UNLOAD:
933b2f58791SDmitry Chagin 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
934b2f58791SDmitry Chagin 		     ++brandinfo)
935b2f58791SDmitry Chagin 			if (elf64_brand_inuse(*brandinfo))
936b2f58791SDmitry Chagin 				error = EBUSY;
937b2f58791SDmitry Chagin 		if (error == 0) {
938b2f58791SDmitry Chagin 			for (brandinfo = &linux_brandlist[0];
939b2f58791SDmitry Chagin 			     *brandinfo != NULL; ++brandinfo)
940b2f58791SDmitry Chagin 				if (elf64_remove_brand_entry(*brandinfo) < 0)
941b2f58791SDmitry Chagin 					error = EINVAL;
942b2f58791SDmitry Chagin 		}
943b2f58791SDmitry Chagin 		if (error == 0) {
944b2f58791SDmitry Chagin 			SET_FOREACH(lihp, linux_ioctl_handler_set)
945b2f58791SDmitry Chagin 				linux_ioctl_unregister_handler(*lihp);
946b2f58791SDmitry Chagin 			if (bootverbose)
947ae8330b4SDmitry Chagin 				printf("Linux x86_64 ELF exec handler removed\n");
948b2f58791SDmitry Chagin 		} else
949ae8330b4SDmitry Chagin 			printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
950b2f58791SDmitry Chagin 		break;
951b2f58791SDmitry Chagin 	default:
952b2f58791SDmitry Chagin 		return (EOPNOTSUPP);
953b2f58791SDmitry Chagin 	}
954b2f58791SDmitry Chagin 	return (error);
955b2f58791SDmitry Chagin }
956b2f58791SDmitry Chagin 
957b2f58791SDmitry Chagin static moduledata_t linux64_elf_mod = {
958b2f58791SDmitry Chagin 	"linux64elf",
959b2f58791SDmitry Chagin 	linux64_elf_modevent,
960b2f58791SDmitry Chagin 	0
961b2f58791SDmitry Chagin };
962b2f58791SDmitry Chagin 
963b2f58791SDmitry Chagin DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
964b2f58791SDmitry Chagin MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
965b6348be7SBaptiste Daroussin FEATURE(linux64, "Linux 64bit support");
966