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