xref: /freebsd/sys/amd64/linux/linux_sysvec.c (revision 2b833162)
1 /*-
2  * Copyright (c) 2004 Tim J. Robbins
3  * Copyright (c) 2003 Peter Wemm
4  * Copyright (c) 2002 Doug Rabson
5  * Copyright (c) 1998-1999 Andrew Gallatin
6  * Copyright (c) 1994-1996 Søren Schmidt
7  * All rights reserved.
8  * Copyright (c) 2013, 2021 Dmitry Chagin <dchagin@FreeBSD.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer
15  *    in this position and unchanged.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #define	__ELF_WORD_SIZE	64
38 
39 #include <sys/param.h>
40 #include <sys/exec.h>
41 #include <sys/imgact.h>
42 #include <sys/imgact_elf.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/stddef.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysent.h>
54 
55 #include <vm/pmap.h>
56 #include <vm/vm.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 
60 #include <machine/cpu.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/specialreg.h>
64 #include <machine/trap.h>
65 
66 #include <x86/linux/linux_x86.h>
67 #include <amd64/linux/linux.h>
68 #include <amd64/linux/linux_proto.h>
69 #include <compat/linux/linux_elf.h>
70 #include <compat/linux/linux_emul.h>
71 #include <compat/linux/linux_fork.h>
72 #include <compat/linux/linux_ioctl.h>
73 #include <compat/linux/linux_mib.h>
74 #include <compat/linux/linux_misc.h>
75 #include <compat/linux/linux_signal.h>
76 #include <compat/linux/linux_sysproto.h>
77 #include <compat/linux/linux_util.h>
78 #include <compat/linux/linux_vdso.h>
79 
80 #include <x86/linux/linux_x86_sigframe.h>
81 
82 MODULE_VERSION(linux64, 1);
83 
84 #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
85 #define	LINUX_VDSOPAGE_LA48	(VM_MAXUSER_ADDRESS_LA48 - \
86 				    LINUX_VDSOPAGE_SIZE)
87 #define	LINUX_SHAREDPAGE_LA48	(LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
88 				/*
89 				 * PAGE_SIZE - the size
90 				 * of the native SHAREDPAGE
91 				 */
92 #define	LINUX_USRSTACK_LA48	LINUX_SHAREDPAGE_LA48
93 #define	LINUX_PS_STRINGS_LA48	(LINUX_USRSTACK_LA48 - \
94 				    sizeof(struct ps_strings))
95 
96 static int linux_szsigcode;
97 static vm_object_t linux_vdso_obj;
98 static char *linux_vdso_mapping;
99 extern char _binary_linux_vdso_so_o_start;
100 extern char _binary_linux_vdso_so_o_end;
101 static vm_offset_t linux_vdso_base;
102 
103 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
104 extern const char *linux_syscallnames[];
105 
106 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
107 
108 static void	linux_vdso_install(const void *param);
109 static void	linux_vdso_deinstall(const void *param);
110 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
111 static void	linux_set_syscall_retval(struct thread *td, int error);
112 static int	linux_fetch_syscall_args(struct thread *td);
113 static void	linux_exec_setregs(struct thread *td, struct image_params *imgp,
114 		    uintptr_t stack);
115 static void	linux_exec_sysvec_init(void *param);
116 static int	linux_on_exec_vmspace(struct proc *p,
117 		    struct image_params *imgp);
118 static void	linux_set_fork_retval(struct thread *td);
119 static int	linux_vsyscall(struct thread *td);
120 
121 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
122 LINUX_VDSO_SYM_CHAR(linux_platform);
123 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
124 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
125 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
126 
127 static int
128 linux_fetch_syscall_args(struct thread *td)
129 {
130 	struct proc *p;
131 	struct trapframe *frame;
132 	struct syscall_args *sa;
133 
134 	p = td->td_proc;
135 	frame = td->td_frame;
136 	sa = &td->td_sa;
137 
138 	sa->args[0] = frame->tf_rdi;
139 	sa->args[1] = frame->tf_rsi;
140 	sa->args[2] = frame->tf_rdx;
141 	sa->args[3] = frame->tf_rcx;
142 	sa->args[4] = frame->tf_r8;
143 	sa->args[5] = frame->tf_r9;
144 	sa->code = frame->tf_rax;
145 	sa->original_code = sa->code;
146 
147 	if (sa->code >= p->p_sysent->sv_size)
148 		/* nosys */
149 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
150 	else
151 		sa->callp = &p->p_sysent->sv_table[sa->code];
152 
153 	td->td_retval[0] = 0;
154 	return (0);
155 }
156 
157 static void
158 linux_set_syscall_retval(struct thread *td, int error)
159 {
160 	struct trapframe *frame;
161 
162 	frame = td->td_frame;
163 
164 	switch (error) {
165 	case 0:
166 		frame->tf_rax = td->td_retval[0];
167 		frame->tf_r10 = frame->tf_rcx;
168 		break;
169 
170 	case ERESTART:
171 		/*
172 		 * Reconstruct pc, we know that 'syscall' is 2 bytes,
173 		 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
174 		 * We saved this in tf_err.
175 		 *
176 		 */
177 		frame->tf_rip -= frame->tf_err;
178 		frame->tf_r10 = frame->tf_rcx;
179 		break;
180 
181 	case EJUSTRETURN:
182 		break;
183 
184 	default:
185 		frame->tf_rax = bsd_to_linux_errno(error);
186 		frame->tf_r10 = frame->tf_rcx;
187 		break;
188 	}
189 
190 	/*
191 	 * Differently from FreeBSD native ABI, on Linux only %rcx
192 	 * and %r11 values are not preserved across the syscall.
193 	 * Require full context restore to get all registers except
194 	 * those two restored at return to usermode.
195 	 *
196 	 * XXX: Would be great to be able to avoid PCB_FULL_IRET
197 	 *      for the error == 0 case.
198 	 */
199 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
200 }
201 
202 static void
203 linux_set_fork_retval(struct thread *td)
204 {
205 	struct trapframe *frame = td->td_frame;
206 
207 	frame->tf_rax = 0;
208 }
209 
210 static int
211 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
212 {
213 	Elf_Auxargs *args;
214 	Elf_Auxinfo *argarray, *pos;
215 	struct proc *p;
216 	int error, issetugid;
217 
218 	p = imgp->proc;
219 	args = (Elf64_Auxargs *)imgp->auxargs;
220 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
221 	    M_WAITOK | M_ZERO);
222 
223 	issetugid = p->p_flag & P_SUGID ? 1 : 0;
224 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
225 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
226 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
227 	AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
228 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
229 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
230 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
231 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
232 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
233 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
234 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
235 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
236 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
237 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
238 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
239 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
240 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
241 	if (imgp->execpathp != 0)
242 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
243 	if (args->execfd != -1)
244 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
245 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
246 	AUXARGS_ENTRY(pos, AT_NULL, 0);
247 
248 	free(imgp->auxargs, M_TEMP);
249 	imgp->auxargs = NULL;
250 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
251 
252 	error = copyout(argarray, (void *)base,
253 	    sizeof(*argarray) * LINUX_AT_COUNT);
254 	free(argarray, M_TEMP);
255 	return (error);
256 }
257 
258 /*
259  * Reset registers to default values on exec.
260  */
261 static void
262 linux_exec_setregs(struct thread *td, struct image_params *imgp,
263     uintptr_t stack)
264 {
265 	struct trapframe *regs;
266 	struct pcb *pcb;
267 	register_t saved_rflags;
268 
269 	regs = td->td_frame;
270 	pcb = td->td_pcb;
271 
272 	if (td->td_proc->p_md.md_ldt != NULL)
273 		user_ldt_free(td);
274 
275 	pcb->pcb_fsbase = 0;
276 	pcb->pcb_gsbase = 0;
277 	clear_pcb_flags(pcb, PCB_32BIT);
278 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
279 	set_pcb_flags(pcb, PCB_FULL_IRET);
280 
281 	saved_rflags = regs->tf_rflags & PSL_T;
282 	bzero((char *)regs, sizeof(struct trapframe));
283 	regs->tf_rip = imgp->entry_addr;
284 	regs->tf_rsp = stack;
285 	regs->tf_rflags = PSL_USER | saved_rflags;
286 	regs->tf_ss = _udatasel;
287 	regs->tf_cs = _ucodesel;
288 	regs->tf_ds = _udatasel;
289 	regs->tf_es = _udatasel;
290 	regs->tf_fs = _ufssel;
291 	regs->tf_gs = _ugssel;
292 	regs->tf_flags = TF_HASSEGS;
293 
294 	x86_clear_dbregs(pcb);
295 
296 	/*
297 	 * Drop the FP state if we hold it, so that the process gets a
298 	 * clean FP state if it uses the FPU again.
299 	 */
300 	fpstate_drop(td);
301 }
302 
303 /*
304  * Copied from amd64/amd64/machdep.c
305  */
306 int
307 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
308 {
309 	struct proc *p;
310 	struct l_rt_sigframe sf;
311 	struct l_sigcontext *context;
312 	struct trapframe *regs;
313 	mcontext_t mc;
314 	unsigned long rflags;
315 	sigset_t bmask;
316 	int error, i;
317 	ksiginfo_t ksi;
318 
319 	regs = td->td_frame;
320 	error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf));
321 	if (error != 0)
322 		return (error);
323 
324 	p = td->td_proc;
325 	context = &sf.sf_uc.uc_mcontext;
326 	rflags = context->sc_rflags;
327 
328 	/*
329 	 * Don't allow users to change privileged or reserved flags.
330 	 */
331 	/*
332 	 * XXX do allow users to change the privileged flag PSL_RF.
333 	 * The cpu sets PSL_RF in tf_rflags for faults.  Debuggers
334 	 * should sometimes set it there too.  tf_rflags is kept in
335 	 * the signal context during signal handling and there is no
336 	 * other place to remember it, so the PSL_RF bit may be
337 	 * corrupted by the signal handler without us knowing.
338 	 * Corruption of the PSL_RF bit at worst causes one more or
339 	 * one less debugger trap, so allowing it is fairly harmless.
340 	 */
341 	if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
342 		uprintf("pid %d comm %s linux mangled rflags %#lx\n",
343 		    p->p_pid, p->p_comm, rflags);
344 		return (EINVAL);
345 	}
346 
347 	/*
348 	 * Don't allow users to load a valid privileged %cs.  Let the
349 	 * hardware check for invalid selectors, excess privilege in
350 	 * other selectors, invalid %eip's and invalid %esp's.
351 	 */
352 	if (!CS_SECURE(context->sc_cs)) {
353 		uprintf("pid %d comm %s linux mangled cs %#x\n",
354 		    p->p_pid, p->p_comm, context->sc_cs);
355 		ksiginfo_init_trap(&ksi);
356 		ksi.ksi_signo = SIGBUS;
357 		ksi.ksi_code = BUS_OBJERR;
358 		ksi.ksi_trapno = T_PROTFLT;
359 		ksi.ksi_addr = (void *)regs->tf_rip;
360 		trapsignal(td, &ksi);
361 		return (EINVAL);
362 	}
363 
364 	linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask);
365 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
366 
367 	regs->tf_rdi    = context->sc_rdi;
368 	regs->tf_rsi    = context->sc_rsi;
369 	regs->tf_rdx    = context->sc_rdx;
370 	regs->tf_rbp    = context->sc_rbp;
371 	regs->tf_rbx    = context->sc_rbx;
372 	regs->tf_rcx    = context->sc_rcx;
373 	regs->tf_rax    = context->sc_rax;
374 	regs->tf_rip    = context->sc_rip;
375 	regs->tf_rsp    = context->sc_rsp;
376 	regs->tf_r8     = context->sc_r8;
377 	regs->tf_r9     = context->sc_r9;
378 	regs->tf_r10    = context->sc_r10;
379 	regs->tf_r11    = context->sc_r11;
380 	regs->tf_r12    = context->sc_r12;
381 	regs->tf_r13    = context->sc_r13;
382 	regs->tf_r14    = context->sc_r14;
383 	regs->tf_r15    = context->sc_r15;
384 	regs->tf_cs     = context->sc_cs;
385 	regs->tf_err    = context->sc_err;
386 	regs->tf_rflags = rflags;
387 
388 	if (sf.sf_uc.uc_mcontext.sc_fpstate != NULL) {
389 		struct savefpu *svfp = (struct savefpu *)mc.mc_fpstate;
390 
391 		bzero(&mc, sizeof(mc));
392 		mc.mc_ownedfp = _MC_FPOWNED_FPU;
393 		mc.mc_fpformat = _MC_FPFMT_XMM;
394 
395 		svfp->sv_env.en_cw = sf.sf_fs.cwd;
396 		svfp->sv_env.en_sw = sf.sf_fs.swd;
397 		svfp->sv_env.en_tw = sf.sf_fs.twd;
398 		svfp->sv_env.en_opcode = sf.sf_fs.fop;
399 		svfp->sv_env.en_rip = sf.sf_fs.rip;
400 		svfp->sv_env.en_rdp = sf.sf_fs.rdp;
401 		svfp->sv_env.en_mxcsr = sf.sf_fs.mxcsr;
402 		svfp->sv_env.en_mxcsr_mask = sf.sf_fs.mxcsr_mask;
403 		/* FPU registers */
404 		for (i = 0; i < nitems(svfp->sv_fp); ++i)
405 			bcopy(&sf.sf_fs.st[i], svfp->sv_fp[i].fp_acc.fp_bytes,
406 			    sizeof(svfp->sv_fp[i].fp_acc.fp_bytes));
407 		/* SSE registers */
408 		for (i = 0; i < nitems(svfp->sv_xmm); ++i)
409 			bcopy(&sf.sf_fs.xmm[i], svfp->sv_xmm[i].xmm_bytes,
410 			    sizeof(svfp->sv_xmm[i].xmm_bytes));
411 		error = set_fpcontext(td, &mc, NULL, 0);
412 		if (error != 0) {
413 			uprintf("pid %d comm %s linux can't restore fpu state %d\n",
414 			    p->p_pid, p->p_comm, error);
415 			return (error);
416 		}
417 	}
418 
419 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
420 	return (EJUSTRETURN);
421 }
422 
423 /*
424  * copied from amd64/amd64/machdep.c
425  *
426  * Send an interrupt to process.
427  */
428 static void
429 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
430 {
431 	struct l_rt_sigframe sf, *sfp;
432 	struct proc *p;
433 	struct thread *td;
434 	struct sigacts *psp;
435 	caddr_t sp;
436 	struct trapframe *regs;
437 	struct savefpu *svfp;
438 	mcontext_t mc;
439 	int sig, code;
440 	int oonstack, issiginfo, i;
441 
442 	td = curthread;
443 	p = td->td_proc;
444 	PROC_LOCK_ASSERT(p, MA_OWNED);
445 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
446 	psp = p->p_sigacts;
447 	issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
448 	code = ksi->ksi_code;
449 	mtx_assert(&psp->ps_mtx, MA_OWNED);
450 	regs = td->td_frame;
451 	oonstack = sigonstack(regs->tf_rsp);
452 
453 	LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
454 	    catcher, sig, mask, code);
455 
456 	bzero(&sf, sizeof(sf));
457 	sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
458 	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
459 	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
460 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
461 
462 	/* Allocate space for the signal handler context. */
463 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
464 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
465 		sp = (caddr_t)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
466 	} else
467 		sp = (caddr_t)regs->tf_rsp - 128;
468 
469 	mtx_unlock(&psp->ps_mtx);
470 	PROC_UNLOCK(p);
471 
472 	/* Make room, keeping the stack aligned. */
473 	sp -= sizeof(struct l_rt_sigframe);
474 	sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
475 
476 	/* Save user context. */
477 	bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
478 	sf.sf_uc.uc_mcontext.sc_mask   = sf.sf_uc.uc_sigmask;
479 	sf.sf_uc.uc_mcontext.sc_rdi    = regs->tf_rdi;
480 	sf.sf_uc.uc_mcontext.sc_rsi    = regs->tf_rsi;
481 	sf.sf_uc.uc_mcontext.sc_rdx    = regs->tf_rdx;
482 	sf.sf_uc.uc_mcontext.sc_rbp    = regs->tf_rbp;
483 	sf.sf_uc.uc_mcontext.sc_rbx    = regs->tf_rbx;
484 	sf.sf_uc.uc_mcontext.sc_rcx    = regs->tf_rcx;
485 	sf.sf_uc.uc_mcontext.sc_rax    = regs->tf_rax;
486 	sf.sf_uc.uc_mcontext.sc_rip    = regs->tf_rip;
487 	sf.sf_uc.uc_mcontext.sc_rsp    = regs->tf_rsp;
488 	sf.sf_uc.uc_mcontext.sc_r8     = regs->tf_r8;
489 	sf.sf_uc.uc_mcontext.sc_r9     = regs->tf_r9;
490 	sf.sf_uc.uc_mcontext.sc_r10    = regs->tf_r10;
491 	sf.sf_uc.uc_mcontext.sc_r11    = regs->tf_r11;
492 	sf.sf_uc.uc_mcontext.sc_r12    = regs->tf_r12;
493 	sf.sf_uc.uc_mcontext.sc_r13    = regs->tf_r13;
494 	sf.sf_uc.uc_mcontext.sc_r14    = regs->tf_r14;
495 	sf.sf_uc.uc_mcontext.sc_r15    = regs->tf_r15;
496 	sf.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
497 	sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
498 	sf.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
499 	sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
500 	sf.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
501 
502 	get_fpcontext(td, &mc, NULL, NULL);
503 	KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present"));
504 	svfp = (struct savefpu *)mc.mc_fpstate;
505 
506 	sf.sf_fs.cwd = svfp->sv_env.en_cw;
507 	sf.sf_fs.swd = svfp->sv_env.en_sw;
508 	sf.sf_fs.twd = svfp->sv_env.en_tw;
509 	sf.sf_fs.fop = svfp->sv_env.en_opcode;
510 	sf.sf_fs.rip = svfp->sv_env.en_rip;
511 	sf.sf_fs.rdp = svfp->sv_env.en_rdp;
512 	sf.sf_fs.mxcsr = svfp->sv_env.en_mxcsr;
513 	sf.sf_fs.mxcsr_mask = svfp->sv_env.en_mxcsr_mask;
514 	/* FPU registers */
515 	for (i = 0; i < nitems(svfp->sv_fp); ++i)
516 		bcopy(svfp->sv_fp[i].fp_acc.fp_bytes, &sf.sf_fs.st[i],
517 		    sizeof(svfp->sv_fp[i].fp_acc.fp_bytes));
518 	/* SSE registers */
519 	for (i = 0; i < nitems(svfp->sv_xmm); ++i)
520 		bcopy(svfp->sv_xmm[i].xmm_bytes, &sf.sf_fs.xmm[i],
521 		    sizeof(svfp->sv_xmm[i].xmm_bytes));
522 	sf.sf_uc.uc_mcontext.sc_fpstate = (struct l_fpstate *)((caddr_t)sfp +
523 	    offsetof(struct l_rt_sigframe, sf_fs));
524 
525 	/* Translate the signal. */
526 	sig = bsd_to_linux_signal(sig);
527 	/* Fill in POSIX parts. */
528 	siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
529 
530 	/* Copy the sigframe out to the user's stack. */
531 	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
532 		uprintf("pid %d comm %s has trashed its stack, killing\n",
533 		    p->p_pid, p->p_comm);
534 		PROC_LOCK(p);
535 		sigexit(td, SIGILL);
536 	}
537 
538 	fpstate_drop(td);
539 	/* Build the argument list for the signal handler. */
540 	regs->tf_rdi = sig;			/* arg 1 in %rdi */
541 	regs->tf_rax = 0;
542 	if (issiginfo) {
543 		regs->tf_rsi = (register_t)&sfp->sf_si;	/* arg 2 in %rsi */
544 		regs->tf_rdx = (register_t)&sfp->sf_uc;	/* arg 3 in %rdx */
545 	} else {
546 		regs->tf_rsi = 0;
547 		regs->tf_rdx = 0;
548 	}
549 	regs->tf_rcx = (register_t)catcher;
550 	regs->tf_rsp = (long)sfp;
551 	regs->tf_rip = linux_rt_sigcode;
552 	regs->tf_rflags &= ~(PSL_T | PSL_D);
553 	regs->tf_cs = _ucodesel;
554 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
555 	PROC_LOCK(p);
556 	mtx_lock(&psp->ps_mtx);
557 }
558 
559 #define	LINUX_VSYSCALL_START		(-10UL << 20)
560 #define	LINUX_VSYSCALL_SZ		1024
561 
562 const unsigned long linux_vsyscall_vector[] = {
563 	LINUX_SYS_gettimeofday,
564 	LINUX_SYS_linux_time,
565 	LINUX_SYS_linux_getcpu,
566 };
567 
568 static int
569 linux_vsyscall(struct thread *td)
570 {
571 	struct trapframe *frame;
572 	uint64_t retqaddr;
573 	int code, traced;
574 	int error;
575 
576 	frame = td->td_frame;
577 
578 	/* Check %rip for vsyscall area. */
579 	if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
580 		return (EINVAL);
581 	if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
582 		return (EINVAL);
583 	code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
584 	if (code >= nitems(linux_vsyscall_vector))
585 		return (EINVAL);
586 
587 	/*
588 	 * vsyscall called as callq *(%rax), so we must
589 	 * use return address from %rsp and also fixup %rsp.
590 	 */
591 	error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
592 	if (error)
593 		return (error);
594 
595 	frame->tf_rip = retqaddr;
596 	frame->tf_rax = linux_vsyscall_vector[code];
597 	frame->tf_rsp += 8;
598 
599 	traced = (frame->tf_flags & PSL_T);
600 
601 	amd64_syscall(td, traced);
602 
603 	return (0);
604 }
605 
606 struct sysentvec elf_linux_sysvec = {
607 	.sv_size	= LINUX_SYS_MAXSYSCALL,
608 	.sv_table	= linux_sysent,
609 	.sv_fixup	= __elfN(freebsd_fixup),
610 	.sv_sendsig	= linux_rt_sendsig,
611 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
612 	.sv_szsigcode	= &linux_szsigcode,
613 	.sv_name	= "Linux ELF64",
614 	.sv_coredump	= elf64_coredump,
615 	.sv_elf_core_osabi = ELFOSABI_NONE,
616 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
617 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
618 	.sv_imgact_try	= linux_exec_imgact_try,
619 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
620 	.sv_minuser	= VM_MIN_ADDRESS,
621 	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA48,
622 	.sv_usrstack	= LINUX_USRSTACK_LA48,
623 	.sv_psstrings	= LINUX_PS_STRINGS_LA48,
624 	.sv_psstringssz	= sizeof(struct ps_strings),
625 	.sv_stackprot	= VM_PROT_ALL,
626 	.sv_copyout_auxargs = linux_copyout_auxargs,
627 	.sv_copyout_strings = __linuxN(copyout_strings),
628 	.sv_setregs	= linux_exec_setregs,
629 	.sv_fixlimit	= NULL,
630 	.sv_maxssiz	= NULL,
631 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
632 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
633 	.sv_set_syscall_retval = linux_set_syscall_retval,
634 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
635 	.sv_syscallnames = linux_syscallnames,
636 	.sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
637 	.sv_shared_page_len = PAGE_SIZE,
638 	.sv_schedtail	= linux_schedtail,
639 	.sv_thread_detach = linux_thread_detach,
640 	.sv_trap	= linux_vsyscall,
641 	.sv_onexec	= linux_on_exec_vmspace,
642 	.sv_onexit	= linux_on_exit,
643 	.sv_ontdexit	= linux_thread_dtor,
644 	.sv_setid_allowed = &linux_setid_allowed_query,
645 	.sv_set_fork_retval = linux_set_fork_retval,
646 };
647 
648 static int
649 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
650 {
651 	int error;
652 
653 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
654 	    LINUX_VDSOPAGE_SIZE, imgp);
655 	if (error == 0)
656 		linux_on_exec(p, imgp);
657 	return (error);
658 }
659 
660 /*
661  * linux_vdso_install() and linux_exec_sysvec_init() must be called
662  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
663  */
664 static void
665 linux_exec_sysvec_init(void *param)
666 {
667 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
668 	struct sysentvec *sv;
669 	ptrdiff_t tkoff;
670 
671 	sv = param;
672 	amd64_lower_shared_page(sv);
673 	/* Fill timekeep_base */
674 	exec_sysvec_init(sv);
675 
676 	tkoff = kern_timekeep_base - linux_vdso_base;
677 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
678 	*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
679 
680 	tkoff = kern_tsc_selector - linux_vdso_base;
681 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
682 	*ktsc_selector = linux_vdso_tsc_selector_idx();
683 	if (bootverbose)
684 		printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
685 
686 	tkoff = kern_cpu_selector - linux_vdso_base;
687 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
688 	*ktsc_selector = linux_vdso_cpu_selector_idx();
689 	if (bootverbose)
690 		printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
691 }
692 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
693     linux_exec_sysvec_init, &elf_linux_sysvec);
694 
695 static void
696 linux_vdso_install(const void *param)
697 {
698 	char *vdso_start = &_binary_linux_vdso_so_o_start;
699 	char *vdso_end = &_binary_linux_vdso_so_o_end;
700 
701 	linux_szsigcode = vdso_end - vdso_start;
702 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
703 
704 	linux_vdso_base = LINUX_VDSOPAGE_LA48;
705 	if (hw_lower_amd64_sharedpage != 0)
706 		linux_vdso_base -= PAGE_SIZE;
707 
708 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
709 
710 	linux_vdso_obj = __elfN(linux_shared_page_init)
711 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
712 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
713 
714 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
715 }
716 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
717     linux_vdso_install, NULL);
718 
719 static void
720 linux_vdso_deinstall(const void *param)
721 {
722 
723 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
724 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
725 }
726 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
727     linux_vdso_deinstall, NULL);
728 
729 static void
730 linux_vdso_reloc(char *mapping, Elf_Addr offset)
731 {
732 	const Elf_Ehdr *ehdr;
733 	const Elf_Shdr *shdr;
734 	Elf64_Addr *where, val;
735 	Elf_Size rtype, symidx;
736 	const Elf_Rela *rela;
737 	Elf_Addr addr, addend;
738 	int relacnt;
739 	int i, j;
740 
741 	MPASS(offset != 0);
742 
743 	relacnt = 0;
744 	ehdr = (const Elf_Ehdr *)mapping;
745 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
746 	for (i = 0; i < ehdr->e_shnum; i++)
747 	{
748 		switch (shdr[i].sh_type) {
749 		case SHT_REL:
750 			printf("Linux x86_64 vDSO: unexpected Rel section\n");
751 			break;
752 		case SHT_RELA:
753 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
754 			relacnt = shdr[i].sh_size / sizeof(*rela);
755 		}
756 	}
757 
758 	for (j = 0; j < relacnt; j++, rela++) {
759 		where = (Elf_Addr *)(mapping + rela->r_offset);
760 		addend = rela->r_addend;
761 		rtype = ELF_R_TYPE(rela->r_info);
762 		symidx = ELF_R_SYM(rela->r_info);
763 
764 		switch (rtype) {
765 		case R_X86_64_NONE:	/* none */
766 			break;
767 
768 		case R_X86_64_RELATIVE:	/* B + A */
769 			addr = (Elf_Addr)(offset + addend);
770 			val = addr;
771 			if (*where != val)
772 				*where = val;
773 			break;
774 		case R_X86_64_IRELATIVE:
775 			printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
776 			    "symbol index %ld\n", symidx);
777 			break;
778 		default:
779 			printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
780 			    "symbol index %ld\n", rtype, symidx);
781 		}
782 	}
783 }
784 
785 static Elf_Brandnote linux64_brandnote = {
786 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
787 	.hdr.n_descsz	= 16,
788 	.hdr.n_type	= 1,
789 	.vendor		= GNU_ABI_VENDOR,
790 	.flags		= BN_TRANSLATE_OSREL,
791 	.trans_osrel	= linux_trans_osrel
792 };
793 
794 static Elf64_Brandinfo linux_glibc2brand = {
795 	.brand		= ELFOSABI_LINUX,
796 	.machine	= EM_X86_64,
797 	.compat_3_brand	= "Linux",
798 	.emul_path	= linux_emul_path,
799 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
800 	.sysvec		= &elf_linux_sysvec,
801 	.interp_newpath	= NULL,
802 	.brand_note	= &linux64_brandnote,
803 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
804 };
805 
806 static Elf64_Brandinfo linux_glibc2brandshort = {
807 	.brand		= ELFOSABI_LINUX,
808 	.machine	= EM_X86_64,
809 	.compat_3_brand	= "Linux",
810 	.emul_path	= linux_emul_path,
811 	.interp_path	= "/lib64/ld-linux.so.2",
812 	.sysvec		= &elf_linux_sysvec,
813 	.interp_newpath	= NULL,
814 	.brand_note	= &linux64_brandnote,
815 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
816 };
817 
818 static Elf64_Brandinfo linux_muslbrand = {
819 	.brand		= ELFOSABI_LINUX,
820 	.machine	= EM_X86_64,
821 	.compat_3_brand	= "Linux",
822 	.emul_path	= linux_emul_path,
823 	.interp_path	= "/lib/ld-musl-x86_64.so.1",
824 	.sysvec		= &elf_linux_sysvec,
825 	.interp_newpath	= NULL,
826 	.brand_note	= &linux64_brandnote,
827 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
828 			    LINUX_BI_FUTEX_REQUEUE
829 };
830 
831 Elf64_Brandinfo *linux_brandlist[] = {
832 	&linux_glibc2brand,
833 	&linux_glibc2brandshort,
834 	&linux_muslbrand,
835 	NULL
836 };
837 
838 static int
839 linux64_elf_modevent(module_t mod, int type, void *data)
840 {
841 	Elf64_Brandinfo **brandinfo;
842 	int error;
843 	struct linux_ioctl_handler **lihp;
844 
845 	error = 0;
846 
847 	switch(type) {
848 	case MOD_LOAD:
849 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
850 		     ++brandinfo)
851 			if (elf64_insert_brand_entry(*brandinfo) < 0)
852 				error = EINVAL;
853 		if (error == 0) {
854 			SET_FOREACH(lihp, linux_ioctl_handler_set)
855 				linux_ioctl_register_handler(*lihp);
856 			stclohz = (stathz ? stathz : hz);
857 			if (bootverbose)
858 				printf("Linux x86-64 ELF exec handler installed\n");
859 		} else
860 			printf("cannot insert Linux x86-64 ELF brand handler\n");
861 		break;
862 	case MOD_UNLOAD:
863 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
864 		     ++brandinfo)
865 			if (elf64_brand_inuse(*brandinfo))
866 				error = EBUSY;
867 		if (error == 0) {
868 			for (brandinfo = &linux_brandlist[0];
869 			     *brandinfo != NULL; ++brandinfo)
870 				if (elf64_remove_brand_entry(*brandinfo) < 0)
871 					error = EINVAL;
872 		}
873 		if (error == 0) {
874 			SET_FOREACH(lihp, linux_ioctl_handler_set)
875 				linux_ioctl_unregister_handler(*lihp);
876 			if (bootverbose)
877 				printf("Linux x86_64 ELF exec handler removed\n");
878 		} else
879 			printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
880 		break;
881 	default:
882 		return (EOPNOTSUPP);
883 	}
884 	return (error);
885 }
886 
887 static moduledata_t linux64_elf_mod = {
888 	"linux64elf",
889 	linux64_elf_modevent,
890 	0
891 };
892 
893 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
894 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
895 FEATURE(linux64, "Linux 64bit support");
896