Home | History | Annotate | Line # | Download | only in amd64
linux_machdep.c revision 1.6
      1 /*	$NetBSD: linux_machdep.c,v 1.6 2005/05/22 19:31:15 fvdl 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.6 2005/05/22 19:31:15 fvdl 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/user.h>
     46 #include <sys/ucontext.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 
     55 #include <compat/linux/common/linux_signal.h>
     56 #include <compat/linux/common/linux_errno.h>
     57 #include <compat/linux/common/linux_exec.h>
     58 #include <compat/linux/common/linux_ioctl.h>
     59 #include <compat/linux/common/linux_prctl.h>
     60 #include <compat/linux/common/linux_machdep.h>
     61 #include <compat/linux/linux_syscall.h>
     62 #include <compat/linux/linux_syscallargs.h>
     63 
     64 static void linux_buildcontext(struct lwp *, void *, void *);
     65 
     66 void
     67 linux_setregs(l, epp, stack)
     68         struct lwp *l;
     69 	struct exec_package *epp;
     70 	u_long stack;
     71 {
     72 	struct pcb *pcb = &l->l_addr->u_pcb;
     73 	struct trapframe *tf;
     74 
     75 	/* If we were using the FPU, forget about it. */
     76 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
     77 		fpusave_lwp(l, 0);
     78 
     79 	l->l_md.md_flags &= ~MDP_USEDFPU;
     80 	pcb->pcb_flags = 0;
     81 	pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
     82 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
     83 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
     84 	pcb->pcb_fs = 0;
     85 	pcb->pcb_gs = 0;
     86 
     87 	l->l_proc->p_flag &= ~P_32;
     88 
     89 	tf = l->l_md.md_regs;
     90 	tf->tf_rax = 0;
     91 	tf->tf_rbx = 0;
     92 	tf->tf_rcx = epp->ep_entry;
     93 	tf->tf_rdx = 0;
     94 	tf->tf_rsi = 0;
     95 	tf->tf_rdi = 0;
     96 	tf->tf_rbp = 0;
     97 	tf->tf_rsp = stack;
     98 	tf->tf_r8 = 0;
     99 	tf->tf_r9 = 0;
    100 	tf->tf_r10 = 0;
    101 	tf->tf_r11 = 0;
    102 	tf->tf_r12 = 0;
    103 	tf->tf_r13 = 0;
    104 	tf->tf_r14 = 0;
    105 	tf->tf_r15 = 0;
    106 	tf->tf_rip = epp->ep_entry;
    107 	tf->tf_rflags = PSL_USERSET;
    108 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    109 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    110 	tf->tf_ds = 0;
    111 	tf->tf_es = 0;
    112 	tf->tf_fs = 0;
    113 	tf->tf_gs = 0;
    114 
    115 	return;
    116 }
    117 
    118 void
    119 linux_sendsig(ksi, mask)
    120 	const ksiginfo_t *ksi;
    121 	const sigset_t *mask;
    122 {
    123 	struct lwp *l = curlwp;
    124 	struct proc *p = l->l_proc;
    125 	struct sigacts *ps = p->p_sigacts;
    126 	int onstack;
    127 	int sig = ksi->ksi_signo;
    128 	struct linux_rt_sigframe *sfp, sigframe;
    129 	struct linux__fpstate *fpsp, fpstate;
    130 	struct fpreg fpregs;
    131 	struct trapframe *tf = l->l_md.md_regs;
    132 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    133 	linux_sigset_t lmask;
    134 	char *sp;
    135 	int error;
    136 
    137 	/* Do we need to jump onto the signal stack? */
    138 	onstack =
    139 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    140 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    141 
    142 	/* Allocate space for the signal handler context. */
    143 	if (onstack)
    144 		sp = ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
    145 		    p->p_sigctx.ps_sigstk.ss_size);
    146 	else
    147 		sp = (caddr_t)tf->tf_rsp - 128;
    148 
    149 
    150 	/*
    151 	 * Save FPU state, if any
    152 	 */
    153 	if (l->l_md.md_flags & MDP_USEDFPU) {
    154 		sp = (char *)
    155 		    (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
    156 		fpsp = (struct linux__fpstate *)sp;
    157 
    158 		(void)process_read_fpregs(l, &fpregs);
    159 		bzero(&fpstate, sizeof(fpstate));
    160 
    161 		fpstate.cwd = fpregs.fp_fcw;
    162 		fpstate.swd = fpregs.fp_fsw;
    163 		fpstate.twd = fpregs.fp_ftw;
    164 		fpstate.fop = fpregs.fp_fop;
    165 		fpstate.rip = fpregs.fp_rip;
    166 		fpstate.rdp = fpregs.fp_rdp;
    167 		fpstate.mxcsr = fpregs.fp_mxcsr;
    168 		fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
    169 		memcpy(&fpstate.st_space, &fpregs.fp_st,
    170 		    sizeof(fpstate.st_space));
    171 		memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
    172 		    sizeof(fpstate.xmm_space));
    173 
    174 		if ((error = copyout(&fpstate, fpsp, sizeof(fpstate))) != 0) {
    175 			sigexit(l, SIGILL);
    176 			return;
    177 		}
    178 	} else {
    179 		fpsp = NULL;
    180 	}
    181 
    182 	/*
    183 	 * Populate the rt_sigframe
    184 	 */
    185 	sp = (char *)
    186 	    ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
    187 	sfp = (struct linux_rt_sigframe *)sp;
    188 
    189 	bzero(&sigframe, sizeof(sigframe));
    190 	if (ps->sa_sigdesc[sig].sd_vers != 0)
    191 		sigframe.pretcode = (char *)ps->sa_sigdesc[sig].sd_tramp;
    192 	else
    193 		sigframe.pretcode = NULL;
    194 
    195 	/*
    196 	 * The user context
    197 	 */
    198 	sigframe.uc.luc_flags = 0;
    199 	sigframe.uc.luc_link = NULL;
    200 
    201 	/* This is used regardless of SA_ONSTACK in Linux */
    202 	sigframe.uc.luc_stack.ss_sp = p->p_sigctx.ps_sigstk.ss_sp;
    203 	sigframe.uc.luc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
    204 	sigframe.uc.luc_stack.ss_flags = 0;
    205 	if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
    206 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
    207 	if (p->p_sigctx.ps_sigstk.ss_flags & SS_DISABLE)
    208 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
    209 
    210 	sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
    211 	sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
    212 	sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
    213 	sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
    214 	sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
    215 	sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
    216 	sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
    217 	sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
    218 	sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
    219 	sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
    220 	sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
    221 	sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
    222 	sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
    223 	sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
    224 	sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
    225 	sigframe.uc.luc_mcontext.rip = tf->tf_rip;
    226 	sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
    227 	sigframe.uc.luc_mcontext.cs = tf->tf_cs;
    228 	sigframe.uc.luc_mcontext.gs = tf->tf_gs;
    229 	sigframe.uc.luc_mcontext.fs = tf->tf_fs;
    230 	sigframe.uc.luc_mcontext.err = tf->tf_err;
    231 	sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
    232 	native_to_linux_sigset(&lmask, mask);
    233 	sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
    234 	sigframe.uc.luc_mcontext.cr2 = (long)l->l_addr->u_pcb.pcb_onfault;
    235 	sigframe.uc.luc_mcontext.fpstate = fpsp;
    236 	native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
    237 
    238 	/*
    239 	 * the siginfo structure
    240 	 */
    241 	sigframe.info.lsi_signo = native_to_linux_signo[sig];
    242 	sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
    243 	sigframe.info.lsi_code = ksi->ksi_code;
    244 
    245 	/* XXX This is a rought conversion, taken from i386 code */
    246 	switch (sigframe.info.lsi_signo) {
    247 	case LINUX_SIGILL:
    248 	case LINUX_SIGFPE:
    249 	case LINUX_SIGSEGV:
    250 	case LINUX_SIGBUS:
    251 	case LINUX_SIGTRAP:
    252 		sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
    253 		break;
    254 	case LINUX_SIGCHLD:
    255 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
    256 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
    257 		sigframe.info._sifields._sigchld._status = ksi->ksi_status;
    258 		sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
    259 		sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
    260 		break;
    261 	case LINUX_SIGIO:
    262 		sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
    263 		sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
    264 		break;
    265 	default:
    266 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
    267 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
    268 		if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
    269 		    (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
    270 			sigframe.info._sifields._timer._sigval.sival_ptr =
    271 			     ksi->ksi_sigval.sival_ptr;
    272 		break;
    273 	}
    274 
    275 	if ((error = copyout(&sigframe, sp, sizeof(sigframe))) != 0) {
    276 		sigexit(l, SIGILL);
    277 		return;
    278 	}
    279 
    280 	linux_buildcontext(l, catcher, sp);
    281 	tf->tf_rdi = sigframe.info.lsi_signo;
    282 	tf->tf_rax = 0;
    283 	tf->tf_rsi = (long)&sfp->info;
    284 	tf->tf_rdx = (long)&sfp->uc;
    285 
    286 	/*
    287 	 * Remember we use signal stack
    288 	 */
    289 	if (onstack)
    290 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    291 	return;
    292 }
    293 
    294 int
    295 linux_sys_modify_ldt(l, v, retval)
    296         struct lwp *l;
    297         void *v;
    298         register_t *retval;
    299 {
    300 	return 0;
    301 }
    302 
    303 int
    304 linux_sys_iopl(l, v, retval)
    305         struct lwp *l;
    306         void *v;
    307         register_t *retval;
    308 {
    309 	return 0;
    310 }
    311 
    312 int
    313 linux_sys_ioperm(l, v, retval)
    314         struct lwp *l;
    315         void *v;
    316         register_t *retval;
    317 {
    318 	return 0;
    319 }
    320 
    321 dev_t
    322 linux_fakedev(dev, raw)
    323         dev_t dev;
    324 	int raw;
    325 {
    326 	return 0;
    327 }
    328 
    329 int
    330 linux_machdepioctl(p, v, retval)
    331         struct proc *p;
    332         void *v;
    333         register_t *retval;
    334 {
    335 	return 0;
    336 }
    337 
    338 int
    339 linux_sys_rt_sigreturn(l, v, retval)
    340         struct lwp *l;
    341         void *v;
    342         register_t *retval;
    343 {
    344 	struct linux_ucontext *luctx;
    345 	struct trapframe *tf = l->l_md.md_regs;
    346 	struct linux_sigcontext *lsigctx;
    347 	struct linux__fpstate fpstate;
    348 	struct linux_rt_sigframe frame, *fp;
    349 	ucontext_t uctx;
    350 	mcontext_t *mctx;
    351 	struct fxsave64 *fxsave;
    352 	int error;
    353 
    354 	fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
    355 	if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
    356 		sigexit(l, SIGILL);
    357 		return error;
    358 	}
    359 	luctx = &frame.uc;
    360 	lsigctx = &luctx->luc_mcontext;
    361 
    362 	bzero(&uctx, sizeof(uctx));
    363 	mctx = (mcontext_t *)&uctx.uc_mcontext;
    364 	fxsave = (struct fxsave64 *)&mctx->__fpregs;
    365 
    366 	/*
    367 	 * Set the flags. Linux always have CPU, stack and signal state,
    368 	 * FPU is optional. uc_flags is not used to tell what we have.
    369 	 */
    370 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
    371 	if (lsigctx->fpstate != NULL)
    372 		uctx.uc_flags |= _UC_FPU;
    373 	uctx.uc_link = NULL;
    374 
    375 	/*
    376 	 * Signal set
    377 	 */
    378 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
    379 
    380 	/*
    381 	 * CPU state
    382 	 */
    383 	mctx->__gregs[_REG_R8] = lsigctx->r8;
    384 	mctx->__gregs[_REG_R9] = lsigctx->r9;
    385 	mctx->__gregs[_REG_R10] = lsigctx->r10;
    386 	mctx->__gregs[_REG_R11] = lsigctx->r11;
    387 	mctx->__gregs[_REG_R12] = lsigctx->r12;
    388 	mctx->__gregs[_REG_R13] = lsigctx->r13;
    389 	mctx->__gregs[_REG_R14] = lsigctx->r14;
    390 	mctx->__gregs[_REG_R15] = lsigctx->r15;
    391 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
    392 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
    393 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
    394 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
    395 	mctx->__gregs[_REG_RAX] = tf->tf_rax;
    396 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
    397 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
    398 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
    399 	mctx->__gregs[_REG_RFL] = lsigctx->eflags;
    400 	mctx->__gregs[_REG_CS] = lsigctx->cs;
    401 	mctx->__gregs[_REG_GS] = lsigctx->gs;
    402 	mctx->__gregs[_REG_FS] = lsigctx->fs;
    403 	mctx->__gregs[_REG_ERR] = lsigctx->err;
    404 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
    405 	mctx->__gregs[_REG_ES] = tf->tf_es;
    406 	mctx->__gregs[_REG_DS] = tf->tf_ds;
    407 	mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
    408 	mctx->__gregs[_REG_SS] = tf->tf_ss;
    409 
    410 	/*
    411 	 * FPU state
    412 	 */
    413 	if (lsigctx->fpstate != NULL) {
    414 		error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
    415 		if (error != 0) {
    416 			sigexit(l, SIGILL);
    417 			return error;
    418 		}
    419 
    420 		fxsave->fx_fcw = fpstate.cwd;
    421 		fxsave->fx_fsw = fpstate.swd;
    422 		fxsave->fx_ftw = fpstate.twd;
    423 		fxsave->fx_fop = fpstate.fop;
    424 		fxsave->fx_rip = fpstate.rip;
    425 		fxsave->fx_rdp = fpstate.rdp;
    426 		fxsave->fx_mxcsr = fpstate.mxcsr;
    427 		fxsave->fx_mxcsr_mask = fpstate.mxcsr_mask;
    428 		memcpy(&fxsave->fx_st, &fpstate.st_space,
    429 		    sizeof(fxsave->fx_st));
    430 		memcpy(&fxsave->fx_xmm, &fpstate.xmm_space,
    431 		    sizeof(fxsave->fx_xmm));
    432 	}
    433 
    434 	/*
    435 	 * And the stack
    436 	 */
    437 	uctx.uc_stack.ss_flags = 0;
    438 	if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK);
    439 		uctx.uc_stack.ss_flags = SS_ONSTACK;
    440 
    441 	if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE);
    442 		uctx.uc_stack.ss_flags = SS_DISABLE;
    443 
    444 	uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
    445 	uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
    446 
    447 	/*
    448 	 * And let setucontext deal with that.
    449 	 */
    450 	return setucontext(l, &uctx);
    451 }
    452 
    453 int
    454 linux_sys_arch_prctl(l, v, retval)
    455 	struct lwp *l;
    456 	void *v;
    457 	register_t *retval;
    458 {
    459 	struct linux_sys_arch_prctl_args /* {
    460 		syscallarg(int) code;
    461 		syscallarg(unsigned long) addr;
    462 	} */ *uap = v;
    463 	struct pcb *pcb = &l->l_addr->u_pcb;
    464 	struct trapframe *tf = l->l_md.md_regs;
    465 	int error;
    466 	uint64_t taddr;
    467 
    468 	switch(SCARG(uap, code)) {
    469 	case LINUX_ARCH_SET_GS:
    470 		taddr = SCARG(uap, addr);
    471 		if (taddr >= VM_MAXUSER_ADDRESS)
    472 			return EINVAL;
    473 		pcb->pcb_gs = taddr;
    474 		pcb->pcb_flags |= PCB_GS64;
    475 		if (l == curlwp)
    476 			wrmsr(MSR_KERNELGSBASE, taddr);
    477 		break;
    478 
    479 	case LINUX_ARCH_GET_GS:
    480 		if (pcb->pcb_flags & PCB_GS64)
    481 			taddr = pcb->pcb_gs;
    482 		else {
    483 			error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
    484 			if (error != 0)
    485 				return error;
    486 		}
    487 		error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
    488 		if (error != 0)
    489 			return error;
    490 		break;
    491 
    492 	case LINUX_ARCH_SET_FS:
    493 		taddr = SCARG(uap, addr);
    494 		if (taddr >= VM_MAXUSER_ADDRESS)
    495 			return EINVAL;
    496 		pcb->pcb_fs = taddr;
    497 		pcb->pcb_flags |= PCB_FS64;
    498 		if (l == curlwp)
    499 			wrmsr(MSR_FSBASE, taddr);
    500 		break;
    501 
    502 	case LINUX_ARCH_GET_FS:
    503 		if (pcb->pcb_flags & PCB_FS64)
    504 			taddr = pcb->pcb_fs;
    505 		else {
    506 			error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
    507 			if (error != 0)
    508 				return error;
    509 		}
    510 		error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
    511 		if (error != 0)
    512 			return error;
    513 		break;
    514 
    515 	default:
    516 #ifdef DEBUG_LINUX
    517 		printf("linux_sys_arch_prctl: unexpected code %d\n",
    518 		    SCARG(uap, code));
    519 #endif
    520 		return EINVAL;
    521 	}
    522 
    523 	return 0;
    524 }
    525 
    526 const int linux_vsyscall_to_syscall[] = {
    527 	LINUX_SYS_gettimeofday,
    528 	LINUX_SYS_time,
    529 	LINUX_SYS_nosys,
    530 	LINUX_SYS_nosys,
    531 };
    532 
    533 int
    534 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    535 {
    536 	struct trapframe *tf = arg;
    537 	uint64_t retaddr;
    538 	int vsyscallnr;
    539 
    540 	/*
    541 	 * Check for a vsyscall. %rip must be the fault address,
    542 	 * and the address must be in the Linux vsyscall area.
    543 	 * Also, vsyscalls are only done at 1024-byte boundaries.
    544 	 */
    545 
    546 	if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
    547 		return 0;
    548 
    549 	if (trapaddr != tf->tf_rip)
    550 		return 0;
    551 
    552 	if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
    553 		return 0;
    554 
    555 	vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
    556 
    557 	if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
    558 		return 0;
    559 
    560 	/*
    561 	 * Get the return address from the top of the stack,
    562 	 * and fix up the return address.
    563 	 * This assumes the faulting instruction was callq *reg,
    564 	 * which is the only way that vsyscalls are ever entered.
    565 	 */
    566 	if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
    567 		return 0;
    568 	tf->tf_rip = retaddr;
    569 	tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
    570 	tf->tf_rsp += 8;	/* "pop" the return address */
    571 
    572 #if 0
    573 	printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
    574 	    (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
    575 	    vsyscallnr, (int)tf->tf_rax);
    576 #endif
    577 
    578 	(*l->l_proc->p_md.md_syscall)(tf);
    579 
    580 	return 1;
    581 }
    582 
    583 static void
    584 linux_buildcontext(struct lwp *l, void *catcher, void *f)
    585 {
    586 	struct trapframe *tf = l->l_md.md_regs;
    587 
    588 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
    589 	tf->tf_rip = (u_int64_t)catcher;
    590 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    591 	tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC);
    592 	tf->tf_rsp = (u_int64_t)f;
    593 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    594 }
    595