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