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