Home | History | Annotate | Line # | Download | only in amd64
linux_machdep.c revision 1.15.2.2
      1 /*	$NetBSD: linux_machdep.c,v 1.15.2.2 2007/05/30 18:20:35 riz 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.2 2007/05/30 18:20:35 riz 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.rax = tf->tf_rax;
    239 	sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
    240 	sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
    241 	sigframe.uc.luc_mcontext.rip = tf->tf_rip;
    242 	sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
    243 	sigframe.uc.luc_mcontext.cs = tf->tf_cs;
    244 	sigframe.uc.luc_mcontext.gs = tf->tf_gs;
    245 	sigframe.uc.luc_mcontext.fs = tf->tf_fs;
    246 	sigframe.uc.luc_mcontext.err = tf->tf_err;
    247 	sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
    248 	native_to_linux_sigset(&lmask, mask);
    249 	sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
    250 	sigframe.uc.luc_mcontext.cr2 = (long)l->l_addr->u_pcb.pcb_onfault;
    251 	sigframe.uc.luc_mcontext.fpstate = fpsp;
    252 	native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
    253 
    254 	/*
    255 	 * the siginfo structure
    256 	 */
    257 	sigframe.info.lsi_signo = native_to_linux_signo[sig];
    258 	sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
    259 	sigframe.info.lsi_code = ksi->ksi_code;
    260 
    261 	/* XXX This is a rought conversion, taken from i386 code */
    262 	switch (sigframe.info.lsi_signo) {
    263 	case LINUX_SIGILL:
    264 	case LINUX_SIGFPE:
    265 	case LINUX_SIGSEGV:
    266 	case LINUX_SIGBUS:
    267 	case LINUX_SIGTRAP:
    268 		sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
    269 		break;
    270 	case LINUX_SIGCHLD:
    271 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
    272 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
    273 		sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
    274 		sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
    275 
    276 		if (WCOREDUMP(ksi->ksi_status)) {
    277 			sigframe.info.lsi_code = LINUX_CLD_DUMPED;
    278 			sigframe.info._sifields._sigchld._status =
    279 			    _WSTATUS(ksi->ksi_status);
    280 		} else if (_WSTATUS(ksi->ksi_status)) {
    281 			sigframe.info.lsi_code = LINUX_CLD_KILLED;
    282 			sigframe.info._sifields._sigchld._status =
    283 			    _WSTATUS(ksi->ksi_status);
    284 		} else {
    285 			sigframe.info.lsi_code = LINUX_CLD_EXITED;
    286 			sigframe.info._sifields._sigchld._status =
    287 			    ((ksi->ksi_status & 0xff00U) >> 8);
    288 		}
    289 		break;
    290 	case LINUX_SIGIO:
    291 		sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
    292 		sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
    293 		break;
    294 	default:
    295 		sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
    296 		sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
    297 		if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
    298 		    (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
    299 			sigframe.info._sifields._timer._sigval.sival_ptr =
    300 			     ksi->ksi_sigval.sival_ptr;
    301 		break;
    302 	}
    303 
    304 	if ((error = copyout(&sigframe, sp, sizeof(sigframe))) != 0) {
    305 		sigexit(l, SIGILL);
    306 		return;
    307 	}
    308 
    309 	linux_buildcontext(l, catcher, sp);
    310 	tf->tf_rdi = sigframe.info.lsi_signo;
    311 	tf->tf_rax = 0;
    312 	tf->tf_rsi = (long)&sfp->info;
    313 	tf->tf_rdx = (long)&sfp->uc;
    314 
    315 	/*
    316 	 * Remember we use signal stack
    317 	 */
    318 	if (onstack)
    319 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    320 	return;
    321 }
    322 
    323 int
    324 linux_sys_modify_ldt(l, v, retval)
    325         struct lwp *l;
    326         void *v;
    327         register_t *retval;
    328 {
    329 	printf("linux_sys_modify_ldt\n");
    330 	return 0;
    331 }
    332 
    333 int
    334 linux_sys_iopl(l, v, retval)
    335         struct lwp *l;
    336         void *v;
    337         register_t *retval;
    338 {
    339 	return 0;
    340 }
    341 
    342 int
    343 linux_sys_ioperm(l, v, retval)
    344         struct lwp *l;
    345         void *v;
    346         register_t *retval;
    347 {
    348 	return 0;
    349 }
    350 
    351 dev_t
    352 linux_fakedev(dev, raw)
    353         dev_t dev;
    354 	int raw;
    355 {
    356 
    357        extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
    358        const struct cdevsw *cd = cdevsw_lookup(dev);
    359 
    360        if (raw) {
    361 #if (NWSDISPLAY > 0)
    362 	       extern const struct cdevsw wsdisplay_cdevsw;
    363 	       if (cd == &wsdisplay_cdevsw)
    364 		       return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
    365 #endif
    366        }
    367 
    368        if (cd == &ptc_cdevsw)
    369 	       return makedev(LINUX_PTC_MAJOR, minor(dev));
    370        if (cd == &pts_cdevsw)
    371 	       return makedev(LINUX_PTS_MAJOR, minor(dev));
    372 
    373 	return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
    374 	    | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
    375 	    | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
    376 }
    377 
    378 int
    379 linux_machdepioctl(l, v, retval)
    380         struct lwp *l;
    381         void *v;
    382         register_t *retval;
    383 {
    384 	return 0;
    385 }
    386 
    387 int
    388 linux_sys_rt_sigreturn(l, v, retval)
    389         struct lwp *l;
    390         void *v;
    391         register_t *retval;
    392 {
    393 	struct linux_ucontext *luctx;
    394 	struct trapframe *tf = l->l_md.md_regs;
    395 	struct linux_sigcontext *lsigctx;
    396 	struct linux__fpstate fpstate;
    397 	struct linux_rt_sigframe frame, *fp;
    398 	ucontext_t uctx;
    399 	mcontext_t *mctx;
    400 	struct fxsave64 *fxsave;
    401 	int error;
    402 
    403 	fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
    404 	if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
    405 		sigexit(l, SIGILL);
    406 		return error;
    407 	}
    408 	luctx = &frame.uc;
    409 	lsigctx = &luctx->luc_mcontext;
    410 
    411 	bzero(&uctx, sizeof(uctx));
    412 	mctx = (mcontext_t *)&uctx.uc_mcontext;
    413 	fxsave = (struct fxsave64 *)&mctx->__fpregs;
    414 
    415 	/*
    416 	 * Set the flags. Linux always have CPU, stack and signal state,
    417 	 * FPU is optional. uc_flags is not used to tell what we have.
    418 	 */
    419 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
    420 	if (lsigctx->fpstate != NULL)
    421 		uctx.uc_flags |= _UC_FPU;
    422 	uctx.uc_link = NULL;
    423 
    424 	/*
    425 	 * Signal set
    426 	 */
    427 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
    428 
    429 	/*
    430 	 * CPU state
    431 	 */
    432 	mctx->__gregs[_REG_R8] = lsigctx->r8;
    433 	mctx->__gregs[_REG_R9] = lsigctx->r9;
    434 	mctx->__gregs[_REG_R10] = lsigctx->r10;
    435 	mctx->__gregs[_REG_R11] = lsigctx->r11;
    436 	mctx->__gregs[_REG_R12] = lsigctx->r12;
    437 	mctx->__gregs[_REG_R13] = lsigctx->r13;
    438 	mctx->__gregs[_REG_R14] = lsigctx->r14;
    439 	mctx->__gregs[_REG_R15] = lsigctx->r15;
    440 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
    441 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
    442 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
    443 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
    444 	mctx->__gregs[_REG_RAX] = lsigctx->rax;
    445 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
    446 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
    447 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
    448 	mctx->__gregs[_REG_RFL] = lsigctx->eflags;
    449 	mctx->__gregs[_REG_CS] = lsigctx->cs;
    450 	mctx->__gregs[_REG_GS] = lsigctx->gs;
    451 	mctx->__gregs[_REG_FS] = lsigctx->fs;
    452 	mctx->__gregs[_REG_ERR] = lsigctx->err;
    453 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
    454 	mctx->__gregs[_REG_ES] = tf->tf_es;
    455 	mctx->__gregs[_REG_DS] = tf->tf_ds;
    456 	mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
    457 	mctx->__gregs[_REG_SS] = tf->tf_ss;
    458 
    459 	/*
    460 	 * FPU state
    461 	 */
    462 	if (lsigctx->fpstate != NULL) {
    463 		error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
    464 		if (error != 0) {
    465 			sigexit(l, SIGILL);
    466 			return error;
    467 		}
    468 
    469 		fxsave->fx_fcw = fpstate.cwd;
    470 		fxsave->fx_fsw = fpstate.swd;
    471 		fxsave->fx_ftw = fpstate.twd;
    472 		fxsave->fx_fop = fpstate.fop;
    473 		fxsave->fx_rip = fpstate.rip;
    474 		fxsave->fx_rdp = fpstate.rdp;
    475 		fxsave->fx_mxcsr = fpstate.mxcsr;
    476 		fxsave->fx_mxcsr_mask = fpstate.mxcsr_mask;
    477 		memcpy(&fxsave->fx_st, &fpstate.st_space,
    478 		    sizeof(fxsave->fx_st));
    479 		memcpy(&fxsave->fx_xmm, &fpstate.xmm_space,
    480 		    sizeof(fxsave->fx_xmm));
    481 	}
    482 
    483 	/*
    484 	 * And the stack
    485 	 */
    486 	uctx.uc_stack.ss_flags = 0;
    487 	if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK);
    488 		uctx.uc_stack.ss_flags = SS_ONSTACK;
    489 
    490 	if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE);
    491 		uctx.uc_stack.ss_flags = SS_DISABLE;
    492 
    493 	uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
    494 	uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
    495 
    496 	/*
    497 	 * And let setucontext deal with that.
    498 	 */
    499 	error = setucontext(l, &uctx);
    500 	if (error)
    501 		return error;
    502 
    503 	return EJUSTRETURN;
    504 }
    505 
    506 int
    507 linux_sys_arch_prctl(l, v, retval)
    508 	struct lwp *l;
    509 	void *v;
    510 	register_t *retval;
    511 {
    512 	struct linux_sys_arch_prctl_args /* {
    513 		syscallarg(int) code;
    514 		syscallarg(unsigned long) addr;
    515 	} */ *uap = v;
    516 	struct pcb *pcb = &l->l_addr->u_pcb;
    517 	struct trapframe *tf = l->l_md.md_regs;
    518 	int error;
    519 	uint64_t taddr;
    520 
    521 	switch(SCARG(uap, code)) {
    522 	case LINUX_ARCH_SET_GS:
    523 		taddr = SCARG(uap, addr);
    524 		if (taddr >= VM_MAXUSER_ADDRESS)
    525 			return EINVAL;
    526 		pcb->pcb_gs = taddr;
    527 		pcb->pcb_flags |= PCB_GS64;
    528 		if (l == curlwp)
    529 			wrmsr(MSR_KERNELGSBASE, taddr);
    530 		break;
    531 
    532 	case LINUX_ARCH_GET_GS:
    533 		if (pcb->pcb_flags & PCB_GS64)
    534 			taddr = pcb->pcb_gs;
    535 		else {
    536 			error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
    537 			if (error != 0)
    538 				return error;
    539 		}
    540 		error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
    541 		if (error != 0)
    542 			return error;
    543 		break;
    544 
    545 	case LINUX_ARCH_SET_FS:
    546 		taddr = SCARG(uap, addr);
    547 		if (taddr >= VM_MAXUSER_ADDRESS)
    548 			return EINVAL;
    549 		pcb->pcb_fs = taddr;
    550 		pcb->pcb_flags |= PCB_FS64;
    551 		if (l == curlwp)
    552 			wrmsr(MSR_FSBASE, taddr);
    553 		break;
    554 
    555 	case LINUX_ARCH_GET_FS:
    556 		if (pcb->pcb_flags & PCB_FS64)
    557 			taddr = pcb->pcb_fs;
    558 		else {
    559 			error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
    560 			if (error != 0)
    561 				return error;
    562 		}
    563 		error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
    564 		if (error != 0)
    565 			return error;
    566 		break;
    567 
    568 	default:
    569 #ifdef DEBUG_LINUX
    570 		printf("linux_sys_arch_prctl: unexpected code %d\n",
    571 		    SCARG(uap, code));
    572 #endif
    573 		return EINVAL;
    574 	}
    575 
    576 	return 0;
    577 }
    578 
    579 const int linux_vsyscall_to_syscall[] = {
    580 	LINUX_SYS_gettimeofday,
    581 	LINUX_SYS_time,
    582 	LINUX_SYS_nosys,
    583 	LINUX_SYS_nosys,
    584 };
    585 
    586 int
    587 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    588 {
    589 	struct trapframe *tf = arg;
    590 	uint64_t retaddr;
    591 	int vsyscallnr;
    592 
    593 	/*
    594 	 * Check for a vsyscall. %rip must be the fault address,
    595 	 * and the address must be in the Linux vsyscall area.
    596 	 * Also, vsyscalls are only done at 1024-byte boundaries.
    597 	 */
    598 
    599 	if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
    600 		return 0;
    601 
    602 	if (trapaddr != tf->tf_rip)
    603 		return 0;
    604 
    605 	if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
    606 		return 0;
    607 
    608 	vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
    609 
    610 	if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
    611 		return 0;
    612 
    613 	/*
    614 	 * Get the return address from the top of the stack,
    615 	 * and fix up the return address.
    616 	 * This assumes the faulting instruction was callq *reg,
    617 	 * which is the only way that vsyscalls are ever entered.
    618 	 */
    619 	if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
    620 		return 0;
    621 	tf->tf_rip = retaddr;
    622 	tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
    623 	tf->tf_rsp += 8;	/* "pop" the return address */
    624 
    625 #if 0
    626 	printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
    627 	    (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
    628 	    vsyscallnr, (int)tf->tf_rax);
    629 #endif
    630 
    631 	(*l->l_proc->p_md.md_syscall)(tf);
    632 
    633 	return 1;
    634 }
    635 
    636 static void
    637 linux_buildcontext(struct lwp *l, void *catcher, void *f)
    638 {
    639 	struct trapframe *tf = l->l_md.md_regs;
    640 
    641 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
    642 	tf->tf_rip = (u_int64_t)catcher;
    643 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    644 	tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC);
    645 	tf->tf_rsp = (u_int64_t)f;
    646 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    647 }
    648 
    649 void *
    650 linux_get_newtls(l)
    651 	struct lwp *l;
    652 {
    653 	struct trapframe *tf = l->l_md.md_regs;
    654 
    655 	return (void *)tf->tf_r8;
    656 }
    657 
    658 int
    659 linux_set_newtls(l, tls)
    660 	struct lwp *l;
    661 	void *tls;
    662 {
    663 	struct linux_sys_arch_prctl_args cup;
    664 	register_t retval;
    665 
    666 	SCARG(&cup, code) = LINUX_ARCH_SET_FS;
    667 	SCARG(&cup, addr) = (unsigned long)tls;
    668 
    669 	return linux_sys_arch_prctl(l, &cup, &retval);
    670 }
    671