Home | History | Annotate | Line # | Download | only in amd64
linux_machdep.c revision 1.15
      1 /*	$NetBSD: linux_machdep.c,v 1.15 2006/09/20 09:54:55 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.15 2006/09/20 09:54:55 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 	printf("linux_sys_modify_ldt\n");
    316 	return 0;
    317 }
    318 
    319 int
    320 linux_sys_iopl(l, v, retval)
    321         struct lwp *l;
    322         void *v;
    323         register_t *retval;
    324 {
    325 	return 0;
    326 }
    327 
    328 int
    329 linux_sys_ioperm(l, v, retval)
    330         struct lwp *l;
    331         void *v;
    332         register_t *retval;
    333 {
    334 	return 0;
    335 }
    336 
    337 dev_t
    338 linux_fakedev(dev, raw)
    339         dev_t dev;
    340 	int raw;
    341 {
    342 	return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
    343 	    | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
    344 	    | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
    345 }
    346 
    347 int
    348 linux_machdepioctl(l, v, retval)
    349         struct lwp *l;
    350         void *v;
    351         register_t *retval;
    352 {
    353 	return 0;
    354 }
    355 
    356 int
    357 linux_sys_rt_sigreturn(l, v, retval)
    358         struct lwp *l;
    359         void *v;
    360         register_t *retval;
    361 {
    362 	struct linux_ucontext *luctx;
    363 	struct trapframe *tf = l->l_md.md_regs;
    364 	struct linux_sigcontext *lsigctx;
    365 	struct linux__fpstate fpstate;
    366 	struct linux_rt_sigframe frame, *fp;
    367 	ucontext_t uctx;
    368 	mcontext_t *mctx;
    369 	struct fxsave64 *fxsave;
    370 	int error;
    371 
    372 	fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
    373 	if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
    374 		sigexit(l, SIGILL);
    375 		return error;
    376 	}
    377 	luctx = &frame.uc;
    378 	lsigctx = &luctx->luc_mcontext;
    379 
    380 	bzero(&uctx, sizeof(uctx));
    381 	mctx = (mcontext_t *)&uctx.uc_mcontext;
    382 	fxsave = (struct fxsave64 *)&mctx->__fpregs;
    383 
    384 	/*
    385 	 * Set the flags. Linux always have CPU, stack and signal state,
    386 	 * FPU is optional. uc_flags is not used to tell what we have.
    387 	 */
    388 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
    389 	if (lsigctx->fpstate != NULL)
    390 		uctx.uc_flags |= _UC_FPU;
    391 	uctx.uc_link = NULL;
    392 
    393 	/*
    394 	 * Signal set
    395 	 */
    396 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
    397 
    398 	/*
    399 	 * CPU state
    400 	 */
    401 	mctx->__gregs[_REG_R8] = lsigctx->r8;
    402 	mctx->__gregs[_REG_R9] = lsigctx->r9;
    403 	mctx->__gregs[_REG_R10] = lsigctx->r10;
    404 	mctx->__gregs[_REG_R11] = lsigctx->r11;
    405 	mctx->__gregs[_REG_R12] = lsigctx->r12;
    406 	mctx->__gregs[_REG_R13] = lsigctx->r13;
    407 	mctx->__gregs[_REG_R14] = lsigctx->r14;
    408 	mctx->__gregs[_REG_R15] = lsigctx->r15;
    409 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
    410 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
    411 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
    412 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
    413 	mctx->__gregs[_REG_RAX] = tf->tf_rax;
    414 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
    415 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
    416 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
    417 	mctx->__gregs[_REG_RFL] = lsigctx->eflags;
    418 	mctx->__gregs[_REG_CS] = lsigctx->cs;
    419 	mctx->__gregs[_REG_GS] = lsigctx->gs;
    420 	mctx->__gregs[_REG_FS] = lsigctx->fs;
    421 	mctx->__gregs[_REG_ERR] = lsigctx->err;
    422 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
    423 	mctx->__gregs[_REG_ES] = tf->tf_es;
    424 	mctx->__gregs[_REG_DS] = tf->tf_ds;
    425 	mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
    426 	mctx->__gregs[_REG_SS] = tf->tf_ss;
    427 
    428 	/*
    429 	 * FPU state
    430 	 */
    431 	if (lsigctx->fpstate != NULL) {
    432 		error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
    433 		if (error != 0) {
    434 			sigexit(l, SIGILL);
    435 			return error;
    436 		}
    437 
    438 		fxsave->fx_fcw = fpstate.cwd;
    439 		fxsave->fx_fsw = fpstate.swd;
    440 		fxsave->fx_ftw = fpstate.twd;
    441 		fxsave->fx_fop = fpstate.fop;
    442 		fxsave->fx_rip = fpstate.rip;
    443 		fxsave->fx_rdp = fpstate.rdp;
    444 		fxsave->fx_mxcsr = fpstate.mxcsr;
    445 		fxsave->fx_mxcsr_mask = fpstate.mxcsr_mask;
    446 		memcpy(&fxsave->fx_st, &fpstate.st_space,
    447 		    sizeof(fxsave->fx_st));
    448 		memcpy(&fxsave->fx_xmm, &fpstate.xmm_space,
    449 		    sizeof(fxsave->fx_xmm));
    450 	}
    451 
    452 	/*
    453 	 * And the stack
    454 	 */
    455 	uctx.uc_stack.ss_flags = 0;
    456 	if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK);
    457 		uctx.uc_stack.ss_flags = SS_ONSTACK;
    458 
    459 	if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE);
    460 		uctx.uc_stack.ss_flags = SS_DISABLE;
    461 
    462 	uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
    463 	uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
    464 
    465 	/*
    466 	 * And let setucontext deal with that.
    467 	 */
    468 	return setucontext(l, &uctx);
    469 }
    470 
    471 int
    472 linux_sys_arch_prctl(l, v, retval)
    473 	struct lwp *l;
    474 	void *v;
    475 	register_t *retval;
    476 {
    477 	struct linux_sys_arch_prctl_args /* {
    478 		syscallarg(int) code;
    479 		syscallarg(unsigned long) addr;
    480 	} */ *uap = v;
    481 	struct pcb *pcb = &l->l_addr->u_pcb;
    482 	struct trapframe *tf = l->l_md.md_regs;
    483 	int error;
    484 	uint64_t taddr;
    485 
    486 	switch(SCARG(uap, code)) {
    487 	case LINUX_ARCH_SET_GS:
    488 		taddr = SCARG(uap, addr);
    489 		if (taddr >= VM_MAXUSER_ADDRESS)
    490 			return EINVAL;
    491 		pcb->pcb_gs = taddr;
    492 		pcb->pcb_flags |= PCB_GS64;
    493 		if (l == curlwp)
    494 			wrmsr(MSR_KERNELGSBASE, taddr);
    495 		break;
    496 
    497 	case LINUX_ARCH_GET_GS:
    498 		if (pcb->pcb_flags & PCB_GS64)
    499 			taddr = pcb->pcb_gs;
    500 		else {
    501 			error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
    502 			if (error != 0)
    503 				return error;
    504 		}
    505 		error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
    506 		if (error != 0)
    507 			return error;
    508 		break;
    509 
    510 	case LINUX_ARCH_SET_FS:
    511 		taddr = SCARG(uap, addr);
    512 		if (taddr >= VM_MAXUSER_ADDRESS)
    513 			return EINVAL;
    514 		pcb->pcb_fs = taddr;
    515 		pcb->pcb_flags |= PCB_FS64;
    516 		if (l == curlwp)
    517 			wrmsr(MSR_FSBASE, taddr);
    518 		break;
    519 
    520 	case LINUX_ARCH_GET_FS:
    521 		if (pcb->pcb_flags & PCB_FS64)
    522 			taddr = pcb->pcb_fs;
    523 		else {
    524 			error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
    525 			if (error != 0)
    526 				return error;
    527 		}
    528 		error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
    529 		if (error != 0)
    530 			return error;
    531 		break;
    532 
    533 	default:
    534 #ifdef DEBUG_LINUX
    535 		printf("linux_sys_arch_prctl: unexpected code %d\n",
    536 		    SCARG(uap, code));
    537 #endif
    538 		return EINVAL;
    539 	}
    540 
    541 	return 0;
    542 }
    543 
    544 const int linux_vsyscall_to_syscall[] = {
    545 	LINUX_SYS_gettimeofday,
    546 	LINUX_SYS_time,
    547 	LINUX_SYS_nosys,
    548 	LINUX_SYS_nosys,
    549 };
    550 
    551 int
    552 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    553 {
    554 	struct trapframe *tf = arg;
    555 	uint64_t retaddr;
    556 	int vsyscallnr;
    557 
    558 	/*
    559 	 * Check for a vsyscall. %rip must be the fault address,
    560 	 * and the address must be in the Linux vsyscall area.
    561 	 * Also, vsyscalls are only done at 1024-byte boundaries.
    562 	 */
    563 
    564 	if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
    565 		return 0;
    566 
    567 	if (trapaddr != tf->tf_rip)
    568 		return 0;
    569 
    570 	if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
    571 		return 0;
    572 
    573 	vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
    574 
    575 	if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
    576 		return 0;
    577 
    578 	/*
    579 	 * Get the return address from the top of the stack,
    580 	 * and fix up the return address.
    581 	 * This assumes the faulting instruction was callq *reg,
    582 	 * which is the only way that vsyscalls are ever entered.
    583 	 */
    584 	if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
    585 		return 0;
    586 	tf->tf_rip = retaddr;
    587 	tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
    588 	tf->tf_rsp += 8;	/* "pop" the return address */
    589 
    590 #if 0
    591 	printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
    592 	    (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
    593 	    vsyscallnr, (int)tf->tf_rax);
    594 #endif
    595 
    596 	(*l->l_proc->p_md.md_syscall)(tf);
    597 
    598 	return 1;
    599 }
    600 
    601 static void
    602 linux_buildcontext(struct lwp *l, void *catcher, void *f)
    603 {
    604 	struct trapframe *tf = l->l_md.md_regs;
    605 
    606 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
    607 	tf->tf_rip = (u_int64_t)catcher;
    608 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    609 	tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC);
    610 	tf->tf_rsp = (u_int64_t)f;
    611 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    612 }
    613 
    614 void *
    615 linux_get_newtls(l)
    616 	struct lwp *l;
    617 {
    618 	struct trapframe *tf = l->l_md.md_regs;
    619 
    620 	return (void *)tf->tf_r8;
    621 }
    622 
    623 int
    624 linux_set_newtls(l, tls)
    625 	struct lwp *l;
    626 	void *tls;
    627 {
    628 	struct linux_sys_arch_prctl_args cup;
    629 	register_t retval;
    630 
    631 	SCARG(&cup, code) = LINUX_ARCH_SET_FS;
    632 	SCARG(&cup, addr) = (unsigned long)tls;
    633 
    634 	return linux_sys_arch_prctl(l, &cup, &retval);
    635 }
    636