xref: /netbsd-src/sys/compat/linux/arch/amd64/linux_machdep.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: linux_machdep.c,v 1.38 2010/07/07 01:30:33 chs Exp $ */
2 
3 /*-
4  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.38 2010/07/07 01:30:33 chs Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/signal.h>
42 #include <sys/exec.h>
43 #include <sys/proc.h>
44 #include <sys/ptrace.h> /* for process_read_fpregs() */
45 #include <sys/ucontext.h>
46 #include <sys/conf.h>
47 
48 #include <machine/reg.h>
49 #include <machine/pcb.h>
50 #include <machine/fpu.h>
51 #include <machine/mcontext.h>
52 #include <machine/specialreg.h>
53 #include <machine/vmparam.h>
54 #include <machine/cpufunc.h>
55 
56 /*
57  * To see whether wscons is configured (for virtual console ioctl calls).
58  */
59 #if defined(_KERNEL_OPT)
60 #include "wsdisplay.h"
61 #endif
62 #if (NWSDISPLAY > 0)
63 #include <dev/wscons/wsconsio.h>
64 #include <dev/wscons/wsdisplay_usl_io.h>
65 #endif
66 
67 
68 #include <compat/linux/common/linux_signal.h>
69 #include <compat/linux/common/linux_errno.h>
70 #include <compat/linux/common/linux_exec.h>
71 #include <compat/linux/common/linux_ioctl.h>
72 #include <compat/linux/common/linux_prctl.h>
73 #include <compat/linux/common/linux_machdep.h>
74 #include <compat/linux/common/linux_ipc.h>
75 #include <compat/linux/common/linux_sem.h>
76 #include <compat/linux/linux_syscall.h>
77 #include <compat/linux/linux_syscallargs.h>
78 
79 static void linux_buildcontext(struct lwp *, void *, void *);
80 
81 void
82 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
83 {
84 	struct pcb *pcb = lwp_getpcb(l);
85 	struct trapframe *tf;
86 
87 	/* If we were using the FPU, forget about it. */
88 	if (pcb->pcb_fpcpu != NULL)
89 		fpusave_lwp(l, 0);
90 
91 	l->l_md.md_flags &= ~MDP_USEDFPU;
92 	pcb->pcb_flags = 0;
93 	pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
94 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
95 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
96 
97 	l->l_proc->p_flag &= ~PK_32;
98 
99 	tf = l->l_md.md_regs;
100 	tf->tf_rax = 0;
101 	tf->tf_rbx = 0;
102 	tf->tf_rcx = epp->ep_entry;
103 	tf->tf_rdx = 0;
104 	tf->tf_rsi = 0;
105 	tf->tf_rdi = 0;
106 	tf->tf_rbp = 0;
107 	tf->tf_rsp = stack;
108 	tf->tf_r8 = 0;
109 	tf->tf_r9 = 0;
110 	tf->tf_r10 = 0;
111 	tf->tf_r11 = 0;
112 	tf->tf_r12 = 0;
113 	tf->tf_r13 = 0;
114 	tf->tf_r14 = 0;
115 	tf->tf_r15 = 0;
116 	tf->tf_rip = epp->ep_entry;
117 	tf->tf_rflags = PSL_USERSET;
118 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
119 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
120 	tf->tf_ds = 0;
121 	tf->tf_es = 0;
122 	cpu_fsgs_zero(l);
123 
124 	return;
125 }
126 
127 void
128 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
129 {
130 	struct lwp *l = curlwp;
131 	struct proc *p = l->l_proc;
132 	struct pcb *pcb = lwp_getpcb(l);
133 	struct sigacts *ps = p->p_sigacts;
134 	int onstack, error;
135 	int sig = ksi->ksi_signo;
136 	struct linux_rt_sigframe *sfp, sigframe;
137 	struct linux__fpstate *fpsp, fpstate;
138 	struct fpreg fpregs;
139 	struct trapframe *tf = l->l_md.md_regs;
140 	sig_t catcher = SIGACTION(p, sig).sa_handler;
141 	linux_sigset_t lmask;
142 	char *sp;
143 
144 	/* Do we need to jump onto the signal stack? */
145 	onstack =
146 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
147 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
148 
149 	/* Allocate space for the signal handler context. */
150 	if (onstack)
151 		sp = ((char *)l->l_sigstk.ss_sp +
152 		    l->l_sigstk.ss_size);
153 	else
154 		sp = (char *)tf->tf_rsp - 128;
155 
156 	/*
157 	 * Save FPU state, if any
158 	 */
159 	if (l->l_md.md_flags & MDP_USEDFPU) {
160 		sp = (char *)
161 		    (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
162 		fpsp = (struct linux__fpstate *)sp;
163 	} else
164 		fpsp = NULL;
165 
166 	/*
167 	 * Populate the rt_sigframe
168 	 */
169 	sp = (char *)
170 	    ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
171 	sfp = (struct linux_rt_sigframe *)sp;
172 
173 	memset(&sigframe, 0, sizeof(sigframe));
174 	if (ps->sa_sigdesc[sig].sd_vers != 0)
175 		sigframe.pretcode =
176 		    (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
177 	else
178 		sigframe.pretcode = NULL;
179 
180 	/*
181 	 * The user context
182 	 */
183 	sigframe.uc.luc_flags = 0;
184 	sigframe.uc.luc_link = NULL;
185 
186 	/* This is used regardless of SA_ONSTACK in Linux */
187 	sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
188 	sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
189 	sigframe.uc.luc_stack.ss_flags = 0;
190 	if (l->l_sigstk.ss_flags & SS_ONSTACK)
191 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
192 	if (l->l_sigstk.ss_flags & SS_DISABLE)
193 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
194 
195 	sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
196 	sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
197 	sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
198 	sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
199 	sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
200 	sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
201 	sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
202 	sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
203 	sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
204 	sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
205 	sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
206 	sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
207 	sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
208 	sigframe.uc.luc_mcontext.rax = tf->tf_rax;
209 	sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
210 	sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
211 	sigframe.uc.luc_mcontext.rip = tf->tf_rip;
212 	sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
213 	sigframe.uc.luc_mcontext.cs = tf->tf_cs;
214 	sigframe.uc.luc_mcontext.gs = tf->tf_gs;
215 	sigframe.uc.luc_mcontext.fs = tf->tf_fs;
216 	sigframe.uc.luc_mcontext.err = tf->tf_err;
217 	sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
218 	native_to_linux_sigset(&lmask, mask);
219 	sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
220 	sigframe.uc.luc_mcontext.cr2 = (long)pcb->pcb_onfault;
221 	sigframe.uc.luc_mcontext.fpstate = fpsp;
222 	native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
223 
224 	/*
225 	 * the siginfo structure
226 	 */
227 	sigframe.info.lsi_signo = native_to_linux_signo[sig];
228 	sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
229 	sigframe.info.lsi_code = native_to_linux_si_code(ksi->ksi_code);
230 
231 	/* XXX This is a rought conversion, taken from i386 code */
232 	switch (sigframe.info.lsi_signo) {
233 	case LINUX_SIGILL:
234 	case LINUX_SIGFPE:
235 	case LINUX_SIGSEGV:
236 	case LINUX_SIGBUS:
237 	case LINUX_SIGTRAP:
238 		sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
239 		break;
240 	case LINUX_SIGCHLD:
241 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
242 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
243 		sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
244 		sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
245 		sigframe.info._sifields._sigchld._status =
246 		    native_to_linux_si_status(ksi->ksi_code, ksi->ksi_status);
247 		break;
248 	case LINUX_SIGIO:
249 		sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
250 		sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
251 		break;
252 	default:
253 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
254 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
255 		if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
256 		    (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
257 			sigframe.info._sifields._timer._sigval.sival_ptr =
258 			     ksi->ksi_value.sival_ptr;
259 		break;
260 	}
261 
262 	sendsig_reset(l, sig);
263 	mutex_exit(p->p_lock);
264 	error = 0;
265 
266 	/*
267 	 * Save FPU state, if any
268 	 */
269 	if (fpsp != NULL) {
270 		(void)process_read_fpregs(l, &fpregs);
271 		memset(&fpstate, 0, sizeof(fpstate));
272 		fpstate.cwd = fpregs.fp_fcw;
273 		fpstate.swd = fpregs.fp_fsw;
274 		fpstate.twd = fpregs.fp_ftw;
275 		fpstate.fop = fpregs.fp_fop;
276 		fpstate.rip = fpregs.fp_rip;
277 		fpstate.rdp = fpregs.fp_rdp;
278 		fpstate.mxcsr = fpregs.fp_mxcsr;
279 		fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
280 		memcpy(&fpstate.st_space, &fpregs.fp_st,
281 		    sizeof(fpstate.st_space));
282 		memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
283 		    sizeof(fpstate.xmm_space));
284 		error = copyout(&fpstate, fpsp, sizeof(fpstate));
285 	}
286 
287 	if (error == 0)
288 		error = copyout(&sigframe, sp, sizeof(sigframe));
289 
290 	mutex_enter(p->p_lock);
291 
292 	if (error != 0) {
293 		sigexit(l, SIGILL);
294 		return;
295 	}
296 
297 	linux_buildcontext(l, catcher, sp);
298 	tf->tf_rdi = sigframe.info.lsi_signo;
299 	tf->tf_rax = 0;
300 	tf->tf_rsi = (long)&sfp->info;
301 	tf->tf_rdx = (long)&sfp->uc;
302 
303 	/*
304 	 * Remember we use signal stack
305 	 */
306 	if (onstack)
307 		l->l_sigstk.ss_flags |= SS_ONSTACK;
308 	return;
309 }
310 
311 int
312 linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
313 {
314 	printf("linux_sys_modify_ldt\n");
315 	return 0;
316 }
317 
318 int
319 linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
320 {
321 	return 0;
322 }
323 
324 int
325 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
326 {
327 	return 0;
328 }
329 
330 dev_t
331 linux_fakedev(dev_t dev, int raw)
332 {
333 
334        extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
335        const struct cdevsw *cd = cdevsw_lookup(dev);
336 
337        if (raw) {
338 #if (NWSDISPLAY > 0)
339 	       extern const struct cdevsw wsdisplay_cdevsw;
340 	       if (cd == &wsdisplay_cdevsw)
341 		       return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
342 #endif
343        }
344 
345        if (cd == &ptc_cdevsw)
346 	       return makedev(LINUX_PTC_MAJOR, minor(dev));
347        if (cd == &pts_cdevsw)
348 	       return makedev(LINUX_PTS_MAJOR, minor(dev));
349 
350 	return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
351 	    | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
352 	    | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
353 }
354 
355 int
356 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
357 {
358 	return 0;
359 }
360 
361 int
362 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
363 {
364 	struct linux_ucontext *luctx;
365 	struct trapframe *tf = l->l_md.md_regs;
366 	struct linux_sigcontext *lsigctx;
367 	struct linux__fpstate fpstate;
368 	struct linux_rt_sigframe frame, *fp;
369 	ucontext_t uctx;
370 	mcontext_t *mctx;
371 	struct fxsave64 *fxarea;
372 	int error;
373 
374 	fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
375 	if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
376 		mutex_enter(l->l_proc->p_lock);
377 		sigexit(l, SIGILL);
378 		return error;
379 	}
380 	luctx = &frame.uc;
381 	lsigctx = &luctx->luc_mcontext;
382 
383 	memset(&uctx, 0, sizeof(uctx));
384 	mctx = (mcontext_t *)&uctx.uc_mcontext;
385 	fxarea = (struct fxsave64 *)&mctx->__fpregs;
386 
387 	/*
388 	 * Set the flags. Linux always have CPU, stack and signal state,
389 	 * FPU is optional. uc_flags is not used to tell what we have.
390 	 */
391 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
392 	if (lsigctx->fpstate != NULL)
393 		uctx.uc_flags |= _UC_FPU;
394 	uctx.uc_link = NULL;
395 
396 	/*
397 	 * Signal set
398 	 */
399 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
400 
401 	/*
402 	 * CPU state
403 	 */
404 	mctx->__gregs[_REG_R8] = lsigctx->r8;
405 	mctx->__gregs[_REG_R9] = lsigctx->r9;
406 	mctx->__gregs[_REG_R10] = lsigctx->r10;
407 	mctx->__gregs[_REG_R11] = lsigctx->r11;
408 	mctx->__gregs[_REG_R12] = lsigctx->r12;
409 	mctx->__gregs[_REG_R13] = lsigctx->r13;
410 	mctx->__gregs[_REG_R14] = lsigctx->r14;
411 	mctx->__gregs[_REG_R15] = lsigctx->r15;
412 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
413 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
414 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
415 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
416 	mctx->__gregs[_REG_RAX] = lsigctx->rax;
417 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
418 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
419 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
420 	mctx->__gregs[_REG_RFLAGS] = lsigctx->eflags;
421 	mctx->__gregs[_REG_CS] = lsigctx->cs;
422 	mctx->__gregs[_REG_GS] = lsigctx->gs;
423 	mctx->__gregs[_REG_FS] = lsigctx->fs;
424 	mctx->__gregs[_REG_ERR] = lsigctx->err;
425 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
426 	mctx->__gregs[_REG_ES] = tf->tf_es;
427 	mctx->__gregs[_REG_DS] = tf->tf_ds;
428 	mctx->__gregs[_REG_RSP] = lsigctx->rsp; /* XXX */
429 	mctx->__gregs[_REG_SS] = tf->tf_ss;
430 
431 	/*
432 	 * FPU state
433 	 */
434 	if (lsigctx->fpstate != NULL) {
435 		error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
436 		if (error != 0) {
437 			mutex_enter(l->l_proc->p_lock);
438 			sigexit(l, SIGILL);
439 			return error;
440 		}
441 
442 		fxarea->fx_fcw = fpstate.cwd;
443 		fxarea->fx_fsw = fpstate.swd;
444 		fxarea->fx_ftw = fpstate.twd;
445 		fxarea->fx_fop = fpstate.fop;
446 		fxarea->fx_rip = fpstate.rip;
447 		fxarea->fx_rdp = fpstate.rdp;
448 		fxarea->fx_mxcsr = fpstate.mxcsr;
449 		fxarea->fx_mxcsr_mask = fpstate.mxcsr_mask;
450 		memcpy(&fxarea->fx_st, &fpstate.st_space,
451 		    sizeof(fxarea->fx_st));
452 		memcpy(&fxarea->fx_xmm, &fpstate.xmm_space,
453 		    sizeof(fxarea->fx_xmm));
454 	}
455 
456 	/*
457 	 * And the stack
458 	 */
459 	uctx.uc_stack.ss_flags = 0;
460 	if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
461 		uctx.uc_stack.ss_flags |= SS_ONSTACK;
462 
463 	if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
464 		uctx.uc_stack.ss_flags |= SS_DISABLE;
465 
466 	uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
467 	uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
468 
469 	/*
470 	 * And let setucontext deal with that.
471 	 */
472 	mutex_enter(l->l_proc->p_lock);
473 	error = setucontext(l, &uctx);
474 	mutex_exit(l->l_proc->p_lock);
475 	if (error)
476 		return error;
477 
478 	return EJUSTRETURN;
479 }
480 
481 int
482 linux_sys_arch_prctl(struct lwp *l,
483     const struct linux_sys_arch_prctl_args *uap, register_t *retval)
484 {
485 	/* {
486 		syscallarg(int) code;
487 		syscallarg(unsigned long) addr;
488 	} */
489 	void *addr = (void *)SCARG(uap, addr);
490 
491 	switch(SCARG(uap, code)) {
492 	case LINUX_ARCH_SET_GS:
493 		return x86_set_sdbase(addr, 'g', l, true);
494 
495 	case LINUX_ARCH_GET_GS:
496 		return x86_get_sdbase(addr, 'g');
497 
498 	case LINUX_ARCH_SET_FS:
499 		return x86_set_sdbase(addr, 'f', l, true);
500 
501 	case LINUX_ARCH_GET_FS:
502 		return x86_get_sdbase(addr, 'f');
503 
504 	default:
505 #ifdef DEBUG_LINUX
506 		printf("linux_sys_arch_prctl: unexpected code %d\n",
507 		    SCARG(uap, code));
508 #endif
509 		return EINVAL;
510 	}
511 	/* NOTREACHED */
512 }
513 
514 const int linux_vsyscall_to_syscall[] = {
515 	LINUX_SYS_gettimeofday,
516 	LINUX_SYS_time,
517 	LINUX_SYS_nosys,	/* nosys */
518 	LINUX_SYS_nosys,	/* nosys */
519 };
520 
521 int
522 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
523 {
524 	struct trapframe *tf = arg;
525 	uint64_t retaddr;
526 	int vsyscallnr;
527 
528 	/*
529 	 * Check for a vsyscall. %rip must be the fault address,
530 	 * and the address must be in the Linux vsyscall area.
531 	 * Also, vsyscalls are only done at 1024-byte boundaries.
532 	 */
533 
534 	if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
535 		return 0;
536 
537 	if (trapaddr != tf->tf_rip)
538 		return 0;
539 
540 	if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
541 		return 0;
542 
543 	vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
544 
545 	if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
546 		return 0;
547 
548 	/*
549 	 * Get the return address from the top of the stack,
550 	 * and fix up the return address.
551 	 * This assumes the faulting instruction was callq *reg,
552 	 * which is the only way that vsyscalls are ever entered.
553 	 */
554 	if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
555 		return 0;
556 	tf->tf_rip = retaddr;
557 	tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
558 	tf->tf_rsp += 8;	/* "pop" the return address */
559 
560 #if 0
561 	printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
562 	    (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
563 	    vsyscallnr, (int)tf->tf_rax);
564 #endif
565 
566 	(*l->l_proc->p_md.md_syscall)(tf);
567 
568 	return 1;
569 }
570 
571 static void
572 linux_buildcontext(struct lwp *l, void *catcher, void *f)
573 {
574 	struct trapframe *tf = l->l_md.md_regs;
575 
576 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
577 	tf->tf_rip = (u_int64_t)catcher;
578 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
579 	tf->tf_rflags &= ~PSL_CLEARSIG;
580 	tf->tf_rsp = (u_int64_t)f;
581 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
582 }
583