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