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