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