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