xref: /freebsd/sys/amd64/linux/linux_sysvec.c (revision a687910fc4352117413b8e0275383e4c687d4c4c)
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 #define	__ELF_WORD_SIZE	64
35 
36 #include <sys/param.h>
37 #include <sys/exec.h>
38 #include <sys/imgact.h>
39 #include <sys/imgact_elf.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/stddef.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 
51 #include <vm/pmap.h>
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 
55 #include <machine/md_var.h>
56 #include <machine/trap.h>
57 
58 #include <x86/linux/linux_x86.h>
59 #include <amd64/linux/linux.h>
60 #include <amd64/linux/linux_proto.h>
61 #include <compat/linux/linux_elf.h>
62 #include <compat/linux/linux_emul.h>
63 #include <compat/linux/linux_fork.h>
64 #include <compat/linux/linux_ioctl.h>
65 #include <compat/linux/linux_mib.h>
66 #include <compat/linux/linux_misc.h>
67 #include <compat/linux/linux_signal.h>
68 #include <compat/linux/linux_sysproto.h>
69 #include <compat/linux/linux_util.h>
70 #include <compat/linux/linux_vdso.h>
71 
72 #include <x86/linux/linux_x86_sigframe.h>
73 
74 _Static_assert(sizeof(struct l_fpstate) ==
75     sizeof(__typeof(((mcontext_t *)0)->mc_fpstate)),
76     "fxsave area size incorrect");
77 
78 MODULE_VERSION(linux64, 1);
79 
80 #define	LINUX_VDSOPAGE_SIZE	PAGE_SIZE * 2
81 #define	LINUX_VDSOPAGE_LA48	(VM_MAXUSER_ADDRESS_LA48 - \
82 				    LINUX_VDSOPAGE_SIZE)
83 #define	LINUX_SHAREDPAGE_LA48	(LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
84 				/*
85 				 * PAGE_SIZE - the size
86 				 * of the native SHAREDPAGE
87 				 */
88 #define	LINUX_USRSTACK_LA48	LINUX_SHAREDPAGE_LA48
89 #define	LINUX_PS_STRINGS_LA48	(LINUX_USRSTACK_LA48 - \
90 				    sizeof(struct ps_strings))
91 
92 static int linux_szsigcode;
93 static vm_object_t linux_vdso_obj;
94 static char *linux_vdso_mapping;
95 extern char _binary_linux_vdso_so_o_start;
96 extern char _binary_linux_vdso_so_o_end;
97 static vm_offset_t linux_vdso_base;
98 
99 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
100 extern const char *linux_syscallnames[];
101 
102 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
103 
104 static void	linux_vdso_install(const void *param);
105 static void	linux_vdso_deinstall(const void *param);
106 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
107 static void	linux_set_syscall_retval(struct thread *td, int error);
108 static int	linux_fetch_syscall_args(struct thread *td);
109 static void	linux_exec_setregs(struct thread *td, struct image_params *imgp,
110 		    uintptr_t stack);
111 static void	linux_exec_sysvec_init(void *param);
112 static int	linux_on_exec_vmspace(struct proc *p,
113 		    struct image_params *imgp);
114 static void	linux_set_fork_retval(struct thread *td);
115 static int	linux_vsyscall(struct thread *td);
116 
117 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
118 LINUX_VDSO_SYM_CHAR(linux_platform);
119 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
120 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
121 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
122 
123 /*
124  * According to the Intel x86 ISA 64-bit syscall
125  * saves %rip to %rcx and rflags to %r11. Registers on syscall entry:
126  * %rax  system call number
127  * %rcx  return address
128  * %r11  saved rflags
129  * %rdi  arg1
130  * %rsi  arg2
131  * %rdx  arg3
132  * %r10  arg4
133  * %r8   arg5
134  * %r9   arg6
135  *
136  * Then FreeBSD fast_syscall() move registers:
137  * %rcx -> trapframe.tf_rip
138  * %r10 -> trapframe.tf_rcx
139  */
140 static int
141 linux_fetch_syscall_args(struct thread *td)
142 {
143 	struct proc *p;
144 	struct trapframe *frame;
145 	struct syscall_args *sa;
146 
147 	p = td->td_proc;
148 	frame = td->td_frame;
149 	sa = &td->td_sa;
150 
151 	sa->args[0] = frame->tf_rdi;
152 	sa->args[1] = frame->tf_rsi;
153 	sa->args[2] = frame->tf_rdx;
154 	sa->args[3] = frame->tf_rcx;
155 	sa->args[4] = frame->tf_r8;
156 	sa->args[5] = frame->tf_r9;
157 	sa->code = frame->tf_rax;
158 	sa->original_code = sa->code;
159 
160 	if (sa->code >= p->p_sysent->sv_size)
161 		/* nosys */
162 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
163 	else
164 		sa->callp = &p->p_sysent->sv_table[sa->code];
165 
166 	/* Restore r10 earlier to avoid doing this multiply times. */
167 	frame->tf_r10 = frame->tf_rcx;
168 	/* Restore %rcx for machine context. */
169 	frame->tf_rcx = frame->tf_rip;
170 
171 	td->td_retval[0] = 0;
172 	return (0);
173 }
174 
175 static void
176 linux_set_syscall_retval(struct thread *td, int error)
177 {
178 	struct trapframe *frame;
179 
180 	frame = td->td_frame;
181 
182 	switch (error) {
183 	case 0:
184 		frame->tf_rax = td->td_retval[0];
185 		break;
186 
187 	case ERESTART:
188 		/*
189 		 * Reconstruct pc, we know that 'syscall' is 2 bytes,
190 		 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
191 		 * We saved this in tf_err.
192 		 *
193 		 */
194 		frame->tf_rip -= frame->tf_err;
195 		break;
196 
197 	case EJUSTRETURN:
198 		break;
199 
200 	default:
201 		frame->tf_rax = bsd_to_linux_errno(error);
202 		break;
203 	}
204 
205 	/*
206 	 * Differently from FreeBSD native ABI, on Linux only %rcx
207 	 * and %r11 values are not preserved across the syscall.
208 	 * Require full context restore to get all registers except
209 	 * those two restored at return to usermode.
210 	 */
211 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
212 }
213 
214 static void
215 linux_set_fork_retval(struct thread *td)
216 {
217 	struct trapframe *frame = td->td_frame;
218 
219 	frame->tf_rax = 0;
220 }
221 
222 void
223 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
224 {
225 
226 	AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
227 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
228 	AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());
229 	AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
230 }
231 
232 /*
233  * Reset registers to default values on exec.
234  */
235 static void
236 linux_exec_setregs(struct thread *td, struct image_params *imgp,
237     uintptr_t stack)
238 {
239 	struct trapframe *regs;
240 	struct pcb *pcb;
241 	register_t saved_rflags;
242 
243 	regs = td->td_frame;
244 	pcb = td->td_pcb;
245 
246 	if (td->td_proc->p_md.md_ldt != NULL)
247 		user_ldt_free(td);
248 
249 	pcb->pcb_fsbase = 0;
250 	pcb->pcb_gsbase = 0;
251 	clear_pcb_flags(pcb, PCB_32BIT);
252 	pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
253 	set_pcb_flags(pcb, PCB_FULL_IRET);
254 
255 	saved_rflags = regs->tf_rflags & PSL_T;
256 	bzero((char *)regs, sizeof(struct trapframe));
257 	regs->tf_rip = imgp->entry_addr;
258 	regs->tf_rsp = stack;
259 	regs->tf_rflags = PSL_USER | saved_rflags;
260 	regs->tf_ss = _udatasel;
261 	regs->tf_cs = _ucodesel;
262 	regs->tf_ds = _udatasel;
263 	regs->tf_es = _udatasel;
264 	regs->tf_fs = _ufssel;
265 	regs->tf_gs = _ugssel;
266 	regs->tf_flags = TF_HASSEGS;
267 
268 	x86_clear_dbregs(pcb);
269 
270 	/*
271 	 * Drop the FP state if we hold it, so that the process gets a
272 	 * clean FP state if it uses the FPU again.
273 	 */
274 	fpstate_drop(td);
275 }
276 
277 static int
278 linux_fxrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
279 {
280 	struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
281 	int error;
282 
283 	error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
284 	if (error != 0)
285 		return (error);
286 	bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
287 	return (set_fpcontext(td, mcp, NULL, 0));
288 }
289 
290 static int
291 linux_xrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
292 {
293 	struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
294 	char *xfpustate;
295 	struct proc *p;
296 	uint32_t magic2;
297 	int error;
298 
299 	p = td->td_proc;
300 	mcp->mc_xfpustate_len = cpu_max_ext_state_size - sizeof(struct savefpu);
301 
302 	/* Legacy region of an xsave area. */
303 	error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
304 	if (error != 0)
305 		return (error);
306 	bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
307 
308 	/* Extended region of an xsave area. */
309 	sc->sc_fpstate += sizeof(mcp->mc_fpstate);
310 	xfpustate = (char *)fpu_save_area_alloc();
311 	error = copyin(PTRIN(sc->sc_fpstate), xfpustate, mcp->mc_xfpustate_len);
312 	if (error != 0) {
313 		fpu_save_area_free((struct savefpu *)xfpustate);
314 		uprintf("pid %d (%s): linux xrstor failed\n", p->p_pid,
315 		    td->td_name);
316 		return (error);
317 	}
318 
319 	/* Linux specific end of xsave area marker. */
320 	sc->sc_fpstate += mcp->mc_xfpustate_len;
321 	error = copyin(PTRIN(sc->sc_fpstate), &magic2, LINUX_FP_XSTATE_MAGIC2_SIZE);
322 	if (error != 0 || magic2 != LINUX_FP_XSTATE_MAGIC2) {
323 		fpu_save_area_free((struct savefpu *)xfpustate);
324 		uprintf("pid %d (%s): sigreturn magic2 0x%x error %d\n",
325 		    p->p_pid, td->td_name, magic2, error);
326 		return (error);
327 	}
328 
329 	error = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
330 	fpu_save_area_free((struct savefpu *)xfpustate);
331 	if (error != 0) {
332 		uprintf("pid %d (%s): sigreturn set_fpcontext error %d\n",
333 		    p->p_pid, td->td_name, error);
334 	}
335 	return (error);
336 }
337 
338 static int
339 linux_copyin_fpstate(struct thread *td, struct l_ucontext *uc)
340 {
341 	mcontext_t mc;
342 
343 	bzero(&mc, sizeof(mc));
344 	mc.mc_ownedfp = _MC_FPOWNED_FPU;
345 	mc.mc_fpformat = _MC_FPFMT_XMM;
346 
347 	if ((uc->uc_flags & LINUX_UC_FP_XSTATE) != 0)
348 		return (linux_xrstor(td, &mc, &uc->uc_mcontext));
349 	else
350 		return (linux_fxrstor(td, &mc, &uc->uc_mcontext));
351 }
352 
353 /*
354  * Copied from amd64/amd64/machdep.c
355  */
356 int
357 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
358 {
359 	struct proc *p;
360 	struct l_rt_sigframe sf;
361 	struct l_sigcontext *context;
362 	struct trapframe *regs;
363 	unsigned long rflags;
364 	sigset_t bmask;
365 	int error;
366 	ksiginfo_t ksi;
367 
368 	regs = td->td_frame;
369 	error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf));
370 	if (error != 0)
371 		return (error);
372 
373 	p = td->td_proc;
374 	context = &sf.sf_uc.uc_mcontext;
375 	rflags = context->sc_rflags;
376 
377 	/*
378 	 * Don't allow users to change privileged or reserved flags.
379 	 */
380 	/*
381 	 * XXX do allow users to change the privileged flag PSL_RF.
382 	 * The cpu sets PSL_RF in tf_rflags for faults.  Debuggers
383 	 * should sometimes set it there too.  tf_rflags is kept in
384 	 * the signal context during signal handling and there is no
385 	 * other place to remember it, so the PSL_RF bit may be
386 	 * corrupted by the signal handler without us knowing.
387 	 * Corruption of the PSL_RF bit at worst causes one more or
388 	 * one less debugger trap, so allowing it is fairly harmless.
389 	 */
390 	if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
391 		uprintf("pid %d comm %s linux mangled rflags %#lx\n",
392 		    p->p_pid, p->p_comm, rflags);
393 		return (EINVAL);
394 	}
395 
396 	/*
397 	 * Don't allow users to load a valid privileged %cs.  Let the
398 	 * hardware check for invalid selectors, excess privilege in
399 	 * other selectors, invalid %eip's and invalid %esp's.
400 	 */
401 	if (!CS_SECURE(context->sc_cs)) {
402 		uprintf("pid %d comm %s linux mangled cs %#x\n",
403 		    p->p_pid, p->p_comm, context->sc_cs);
404 		ksiginfo_init_trap(&ksi);
405 		ksi.ksi_signo = SIGBUS;
406 		ksi.ksi_code = BUS_OBJERR;
407 		ksi.ksi_trapno = T_PROTFLT;
408 		ksi.ksi_addr = (void *)regs->tf_rip;
409 		trapsignal(td, &ksi);
410 		return (EINVAL);
411 	}
412 
413 	linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask);
414 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
415 
416 	regs->tf_rdi    = context->sc_rdi;
417 	regs->tf_rsi    = context->sc_rsi;
418 	regs->tf_rdx    = context->sc_rdx;
419 	regs->tf_rbp    = context->sc_rbp;
420 	regs->tf_rbx    = context->sc_rbx;
421 	regs->tf_rcx    = context->sc_rcx;
422 	regs->tf_rax    = context->sc_rax;
423 	regs->tf_rip    = context->sc_rip;
424 	regs->tf_rsp    = context->sc_rsp;
425 	regs->tf_r8     = context->sc_r8;
426 	regs->tf_r9     = context->sc_r9;
427 	regs->tf_r10    = context->sc_r10;
428 	regs->tf_r11    = context->sc_r11;
429 	regs->tf_r12    = context->sc_r12;
430 	regs->tf_r13    = context->sc_r13;
431 	regs->tf_r14    = context->sc_r14;
432 	regs->tf_r15    = context->sc_r15;
433 	regs->tf_cs     = context->sc_cs;
434 	regs->tf_err    = context->sc_err;
435 	regs->tf_rflags = rflags;
436 
437 	error = linux_copyin_fpstate(td, &sf.sf_uc);
438 	if (error != 0) {
439 		uprintf("pid %d comm %s linux can't restore fpu state %d\n",
440 		    p->p_pid, p->p_comm, error);
441 		return (error);
442 	}
443 
444 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
445 	return (EJUSTRETURN);
446 }
447 
448 static int
449 linux_fxsave(mcontext_t *mcp, void *ufp)
450 {
451 	struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
452 
453 	bzero(&fx->reserved2[0], sizeof(fx->reserved2));
454 	return (copyout(fx, ufp, sizeof(*fx)));
455 }
456 
457 static int
458 linux_xsave(mcontext_t *mcp, char *xfpusave, char *ufp)
459 {
460 	struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
461 	uint32_t magic2;
462 	int error;
463 
464 	/* Legacy region of an xsave area. */
465 	fx->sw_reserved.magic1 = LINUX_FP_XSTATE_MAGIC1;
466 	fx->sw_reserved.xstate_size = mcp->mc_xfpustate_len + sizeof(*fx);
467 	fx->sw_reserved.extended_size = fx->sw_reserved.xstate_size +
468 	    LINUX_FP_XSTATE_MAGIC2_SIZE;
469 	fx->sw_reserved.xfeatures = xsave_mask;
470 
471 	error = copyout(fx, ufp, sizeof(*fx));
472 	if (error != 0)
473 		return (error);
474 	ufp += sizeof(*fx);
475 
476 	/* Extended region of an xsave area. */
477 	error = copyout(xfpusave, ufp, mcp->mc_xfpustate_len);
478 	if (error != 0)
479 		return (error);
480 
481 	/* Linux specific end of xsave area marker. */
482 	ufp += mcp->mc_xfpustate_len;
483 	magic2 = LINUX_FP_XSTATE_MAGIC2;
484 	return (copyout(&magic2, ufp, LINUX_FP_XSTATE_MAGIC2_SIZE));
485 }
486 
487 static int
488 linux_copyout_fpstate(struct thread *td, struct l_ucontext *uc, char **sp)
489 {
490 	size_t xfpusave_len;
491 	char *xfpusave;
492 	mcontext_t mc;
493 	char *ufp = *sp;
494 
495 	get_fpcontext(td, &mc, &xfpusave, &xfpusave_len);
496 	KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present"));
497 
498 	/* Room for fxsave area. */
499 	ufp -= sizeof(struct l_fpstate);
500 	if (xfpusave != NULL) {
501 		/* Room for xsave area. */
502 		ufp -= (xfpusave_len + LINUX_FP_XSTATE_MAGIC2_SIZE);
503 		uc->uc_flags |= LINUX_UC_FP_XSTATE;
504 	}
505 	*sp = ufp = (char *)((unsigned long)ufp & ~0x3Ful);
506 
507 	if (xfpusave != NULL)
508 		return (linux_xsave(&mc, xfpusave, ufp));
509 	else
510 		return (linux_fxsave(&mc, ufp));
511 }
512 
513 /*
514  * copied from amd64/amd64/machdep.c
515  *
516  * Send an interrupt to process.
517  */
518 static void
519 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
520 {
521 	struct l_rt_sigframe sf, *sfp;
522 	struct proc *p;
523 	struct thread *td;
524 	struct sigacts *psp;
525 	char *sp;
526 	struct trapframe *regs;
527 	int sig, code;
528 	int oonstack, issiginfo;
529 
530 	td = curthread;
531 	p = td->td_proc;
532 	PROC_LOCK_ASSERT(p, MA_OWNED);
533 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
534 	psp = p->p_sigacts;
535 	issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
536 	code = ksi->ksi_code;
537 	mtx_assert(&psp->ps_mtx, MA_OWNED);
538 	regs = td->td_frame;
539 	oonstack = sigonstack(regs->tf_rsp);
540 
541 	LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
542 	    catcher, sig, mask, code);
543 
544 	bzero(&sf, sizeof(sf));
545 	sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
546 	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
547 	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
548 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
549 
550 	/* Allocate space for the signal handler context. */
551 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
552 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
553 		sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
554 	} else
555 		sp = (char *)regs->tf_rsp - 128;
556 
557 	mtx_unlock(&psp->ps_mtx);
558 	PROC_UNLOCK(p);
559 
560 	if (linux_copyout_fpstate(td, &sf.sf_uc, &sp) != 0) {
561 		uprintf("pid %d comm %s linux can't save fpu state, killing\n",
562 		    p->p_pid, p->p_comm);
563 		PROC_LOCK(p);
564 		sigexit(td, SIGILL);
565 	}
566 	sf.sf_uc.uc_mcontext.sc_fpstate = (register_t)sp;
567 
568 	/* Make room, keeping the stack aligned. */
569 	sp -= sizeof(struct l_rt_sigframe);
570 	sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
571 
572 	/* Save user context. */
573 	bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
574 	sf.sf_uc.uc_mcontext.sc_mask   = sf.sf_uc.uc_sigmask;
575 	sf.sf_uc.uc_mcontext.sc_rdi    = regs->tf_rdi;
576 	sf.sf_uc.uc_mcontext.sc_rsi    = regs->tf_rsi;
577 	sf.sf_uc.uc_mcontext.sc_rdx    = regs->tf_rdx;
578 	sf.sf_uc.uc_mcontext.sc_rbp    = regs->tf_rbp;
579 	sf.sf_uc.uc_mcontext.sc_rbx    = regs->tf_rbx;
580 	sf.sf_uc.uc_mcontext.sc_rcx    = regs->tf_rcx;
581 	sf.sf_uc.uc_mcontext.sc_rax    = regs->tf_rax;
582 	sf.sf_uc.uc_mcontext.sc_rip    = regs->tf_rip;
583 	sf.sf_uc.uc_mcontext.sc_rsp    = regs->tf_rsp;
584 	sf.sf_uc.uc_mcontext.sc_r8     = regs->tf_r8;
585 	sf.sf_uc.uc_mcontext.sc_r9     = regs->tf_r9;
586 	sf.sf_uc.uc_mcontext.sc_r10    = regs->tf_r10;
587 	sf.sf_uc.uc_mcontext.sc_r11    = regs->tf_r11;
588 	sf.sf_uc.uc_mcontext.sc_r12    = regs->tf_r12;
589 	sf.sf_uc.uc_mcontext.sc_r13    = regs->tf_r13;
590 	sf.sf_uc.uc_mcontext.sc_r14    = regs->tf_r14;
591 	sf.sf_uc.uc_mcontext.sc_r15    = regs->tf_r15;
592 	sf.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
593 	sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
594 	sf.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
595 	sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
596 	sf.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
597 
598 	/* Translate the signal. */
599 	sig = bsd_to_linux_signal(sig);
600 	/* Fill in POSIX parts. */
601 	siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
602 
603 	/* Copy the sigframe out to the user's stack. */
604 	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
605 		uprintf("pid %d comm %s has trashed its stack, killing\n",
606 		    p->p_pid, p->p_comm);
607 		PROC_LOCK(p);
608 		sigexit(td, SIGILL);
609 	}
610 
611 	fpstate_drop(td);
612 	/* Build the argument list for the signal handler. */
613 	regs->tf_rdi = sig;			/* arg 1 in %rdi */
614 	regs->tf_rax = 0;
615 	if (issiginfo) {
616 		regs->tf_rsi = (register_t)&sfp->sf_si;	/* arg 2 in %rsi */
617 		regs->tf_rdx = (register_t)&sfp->sf_uc;	/* arg 3 in %rdx */
618 	} else {
619 		regs->tf_rsi = 0;
620 		regs->tf_rdx = 0;
621 	}
622 	regs->tf_rcx = (register_t)catcher;
623 	regs->tf_rsp = (long)sfp;
624 	regs->tf_rip = linux_rt_sigcode;
625 	regs->tf_rflags &= ~(PSL_T | PSL_D);
626 	regs->tf_cs = _ucodesel;
627 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
628 	PROC_LOCK(p);
629 	mtx_lock(&psp->ps_mtx);
630 }
631 
632 #define	LINUX_VSYSCALL_START		(-10UL << 20)
633 #define	LINUX_VSYSCALL_SZ		1024
634 
635 const unsigned long linux_vsyscall_vector[] = {
636 	LINUX_SYS_gettimeofday,
637 	LINUX_SYS_linux_time,
638 	LINUX_SYS_linux_getcpu,
639 };
640 
641 static int
642 linux_vsyscall(struct thread *td)
643 {
644 	struct trapframe *frame;
645 	uint64_t retqaddr;
646 	int code, traced;
647 	int error;
648 
649 	frame = td->td_frame;
650 
651 	/* Check %rip for vsyscall area. */
652 	if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
653 		return (EINVAL);
654 	if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
655 		return (EINVAL);
656 	code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
657 	if (code >= nitems(linux_vsyscall_vector))
658 		return (EINVAL);
659 
660 	/*
661 	 * vsyscall called as callq *(%rax), so we must
662 	 * use return address from %rsp and also fixup %rsp.
663 	 */
664 	error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
665 	if (error)
666 		return (error);
667 
668 	frame->tf_rip = retqaddr;
669 	frame->tf_rax = linux_vsyscall_vector[code];
670 	frame->tf_rsp += 8;
671 
672 	traced = (frame->tf_flags & PSL_T);
673 
674 	amd64_syscall(td, traced);
675 
676 	return (0);
677 }
678 
679 struct sysentvec elf_linux_sysvec = {
680 	.sv_size	= LINUX_SYS_MAXSYSCALL,
681 	.sv_table	= linux_sysent,
682 	.sv_fixup	= __elfN(freebsd_fixup),
683 	.sv_sendsig	= linux_rt_sendsig,
684 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
685 	.sv_szsigcode	= &linux_szsigcode,
686 	.sv_name	= "Linux ELF64",
687 	.sv_coredump	= elf64_coredump,
688 	.sv_elf_core_osabi = ELFOSABI_NONE,
689 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
690 	.sv_elf_core_prepare_notes = linux64_prepare_notes,
691 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
692 	.sv_minuser	= VM_MIN_ADDRESS,
693 	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA48,
694 	.sv_usrstack	= LINUX_USRSTACK_LA48,
695 	.sv_psstrings	= LINUX_PS_STRINGS_LA48,
696 	.sv_psstringssz	= sizeof(struct ps_strings),
697 	.sv_stackprot	= VM_PROT_ALL,
698 	.sv_copyout_auxargs = __linuxN(copyout_auxargs),
699 	.sv_copyout_strings = __linuxN(copyout_strings),
700 	.sv_setregs	= linux_exec_setregs,
701 	.sv_fixlimit	= NULL,
702 	.sv_maxssiz	= NULL,
703 	.sv_flags	= SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
704 	    SV_SIG_WAITNDQ | SV_TIMEKEEP,
705 	.sv_set_syscall_retval = linux_set_syscall_retval,
706 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
707 	.sv_syscallnames = linux_syscallnames,
708 	.sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
709 	.sv_shared_page_len = PAGE_SIZE,
710 	.sv_schedtail	= linux_schedtail,
711 	.sv_thread_detach = linux_thread_detach,
712 	.sv_trap	= linux_vsyscall,
713 	.sv_hwcap	= NULL,
714 	.sv_hwcap2	= NULL,
715 	.sv_onexec	= linux_on_exec_vmspace,
716 	.sv_onexit	= linux_on_exit,
717 	.sv_ontdexit	= linux_thread_dtor,
718 	.sv_setid_allowed = &linux_setid_allowed_query,
719 	.sv_set_fork_retval = linux_set_fork_retval,
720 };
721 
722 static int
723 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
724 {
725 	int error;
726 
727 	error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
728 	    LINUX_VDSOPAGE_SIZE, imgp);
729 	if (error == 0)
730 		error = linux_on_exec(p, imgp);
731 	return (error);
732 }
733 
734 /*
735  * linux_vdso_install() and linux_exec_sysvec_init() must be called
736  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
737  */
738 static void
739 linux_exec_sysvec_init(void *param)
740 {
741 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
742 	struct sysentvec *sv;
743 	ptrdiff_t tkoff;
744 
745 	sv = param;
746 	amd64_lower_shared_page(sv);
747 	/* Fill timekeep_base */
748 	exec_sysvec_init(sv);
749 
750 	tkoff = kern_timekeep_base - linux_vdso_base;
751 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
752 	*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
753 
754 	tkoff = kern_tsc_selector - linux_vdso_base;
755 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
756 	*ktsc_selector = linux_vdso_tsc_selector_idx();
757 	if (bootverbose)
758 		printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
759 
760 	tkoff = kern_cpu_selector - linux_vdso_base;
761 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
762 	*ktsc_selector = linux_vdso_cpu_selector_idx();
763 	if (bootverbose)
764 		printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
765 }
766 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
767     linux_exec_sysvec_init, &elf_linux_sysvec);
768 
769 static void
770 linux_vdso_install(const void *param)
771 {
772 	char *vdso_start = &_binary_linux_vdso_so_o_start;
773 	char *vdso_end = &_binary_linux_vdso_so_o_end;
774 
775 	linux_szsigcode = vdso_end - vdso_start;
776 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
777 
778 	linux_vdso_base = LINUX_VDSOPAGE_LA48;
779 	if (hw_lower_amd64_sharedpage != 0)
780 		linux_vdso_base -= PAGE_SIZE;
781 
782 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
783 
784 	linux_vdso_obj = __elfN(linux_shared_page_init)
785 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
786 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
787 
788 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
789 }
790 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
791     linux_vdso_install, NULL);
792 
793 static void
794 linux_vdso_deinstall(const void *param)
795 {
796 
797 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
798 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
799 }
800 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
801     linux_vdso_deinstall, NULL);
802 
803 static void
804 linux_vdso_reloc(char *mapping, Elf_Addr offset)
805 {
806 	const Elf_Ehdr *ehdr;
807 	const Elf_Shdr *shdr;
808 	Elf64_Addr *where, val;
809 	Elf_Size rtype, symidx;
810 	const Elf_Rela *rela;
811 	Elf_Addr addr, addend;
812 	int relacnt;
813 	int i, j;
814 
815 	MPASS(offset != 0);
816 
817 	relacnt = 0;
818 	ehdr = (const Elf_Ehdr *)mapping;
819 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
820 	for (i = 0; i < ehdr->e_shnum; i++)
821 	{
822 		switch (shdr[i].sh_type) {
823 		case SHT_REL:
824 			printf("Linux x86_64 vDSO: unexpected Rel section\n");
825 			break;
826 		case SHT_RELA:
827 			rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
828 			relacnt = shdr[i].sh_size / sizeof(*rela);
829 		}
830 	}
831 
832 	for (j = 0; j < relacnt; j++, rela++) {
833 		where = (Elf_Addr *)(mapping + rela->r_offset);
834 		addend = rela->r_addend;
835 		rtype = ELF_R_TYPE(rela->r_info);
836 		symidx = ELF_R_SYM(rela->r_info);
837 
838 		switch (rtype) {
839 		case R_X86_64_NONE:	/* none */
840 			break;
841 
842 		case R_X86_64_RELATIVE:	/* B + A */
843 			addr = (Elf_Addr)(offset + addend);
844 			val = addr;
845 			if (*where != val)
846 				*where = val;
847 			break;
848 		case R_X86_64_IRELATIVE:
849 			printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
850 			    "symbol index %ld\n", symidx);
851 			break;
852 		default:
853 			printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
854 			    "symbol index %ld\n", rtype, symidx);
855 		}
856 	}
857 }
858 
859 static Elf_Brandnote linux64_brandnote = {
860 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
861 	.hdr.n_descsz	= 16,
862 	.hdr.n_type	= 1,
863 	.vendor		= GNU_ABI_VENDOR,
864 	.flags		= BN_TRANSLATE_OSREL,
865 	.trans_osrel	= linux_trans_osrel
866 };
867 
868 static Elf64_Brandinfo linux_glibc2brand = {
869 	.brand		= ELFOSABI_LINUX,
870 	.machine	= EM_X86_64,
871 	.compat_3_brand	= "Linux",
872 	.interp_path	= "/lib64/ld-linux-x86-64.so.2",
873 	.sysvec		= &elf_linux_sysvec,
874 	.interp_newpath	= NULL,
875 	.brand_note	= &linux64_brandnote,
876 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
877 };
878 
879 static Elf64_Brandinfo linux_glibc2brandshort = {
880 	.brand		= ELFOSABI_LINUX,
881 	.machine	= EM_X86_64,
882 	.compat_3_brand	= "Linux",
883 	.interp_path	= "/lib64/ld-linux.so.2",
884 	.sysvec		= &elf_linux_sysvec,
885 	.interp_newpath	= NULL,
886 	.brand_note	= &linux64_brandnote,
887 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
888 };
889 
890 static Elf64_Brandinfo linux_muslbrand = {
891 	.brand		= ELFOSABI_LINUX,
892 	.machine	= EM_X86_64,
893 	.compat_3_brand	= "Linux",
894 	.interp_path	= "/lib/ld-musl-x86_64.so.1",
895 	.sysvec		= &elf_linux_sysvec,
896 	.interp_newpath	= NULL,
897 	.brand_note	= &linux64_brandnote,
898 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
899 			    LINUX_BI_FUTEX_REQUEUE
900 };
901 
902 Elf64_Brandinfo *linux_brandlist[] = {
903 	&linux_glibc2brand,
904 	&linux_glibc2brandshort,
905 	&linux_muslbrand,
906 	NULL
907 };
908 
909 static int
910 linux64_elf_modevent(module_t mod, int type, void *data)
911 {
912 	Elf64_Brandinfo **brandinfo;
913 	int error;
914 	struct linux_ioctl_handler **lihp;
915 
916 	error = 0;
917 
918 	switch(type) {
919 	case MOD_LOAD:
920 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
921 		     ++brandinfo)
922 			if (elf64_insert_brand_entry(*brandinfo) < 0)
923 				error = EINVAL;
924 		if (error == 0) {
925 			SET_FOREACH(lihp, linux_ioctl_handler_set)
926 				linux_ioctl_register_handler(*lihp);
927 			stclohz = (stathz ? stathz : hz);
928 			if (bootverbose)
929 				printf("Linux x86-64 ELF exec handler installed\n");
930 		} else
931 			printf("cannot insert Linux x86-64 ELF brand handler\n");
932 		break;
933 	case MOD_UNLOAD:
934 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
935 		     ++brandinfo)
936 			if (elf64_brand_inuse(*brandinfo))
937 				error = EBUSY;
938 		if (error == 0) {
939 			for (brandinfo = &linux_brandlist[0];
940 			     *brandinfo != NULL; ++brandinfo)
941 				if (elf64_remove_brand_entry(*brandinfo) < 0)
942 					error = EINVAL;
943 		}
944 		if (error == 0) {
945 			SET_FOREACH(lihp, linux_ioctl_handler_set)
946 				linux_ioctl_unregister_handler(*lihp);
947 			if (bootverbose)
948 				printf("Linux x86_64 ELF exec handler removed\n");
949 		} else
950 			printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
951 		break;
952 	default:
953 		return (EOPNOTSUPP);
954 	}
955 	return (error);
956 }
957 
958 static moduledata_t linux64_elf_mod = {
959 	"linux64elf",
960 	linux64_elf_modevent,
961 	0
962 };
963 
964 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
965 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
966 FEATURE(linux64, "Linux 64bit support");
967