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