Home | History | Annotate | Line # | Download | only in amd64
linux_machdep.c revision 1.47
      1  1.47       dsl /*	$NetBSD: linux_machdep.c,v 1.47 2014/02/15 10:11:15 dsl Exp $ */
      2   1.1      manu 
      3   1.1      manu /*-
      4   1.1      manu  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
      5   1.1      manu  *
      6   1.1      manu  * Redistribution and use in source and binary forms, with or without
      7   1.1      manu  * modification, are permitted provided that the following conditions
      8   1.1      manu  * are met:
      9   1.1      manu  * 1. Redistributions of source code must retain the above copyright
     10   1.1      manu  *    notice, this list of conditions and the following disclaimer.
     11   1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      manu  *    documentation and/or other materials provided with the distribution.
     14   1.1      manu  * 3. All advertising materials mentioning features or use of this software
     15   1.1      manu  *    must display the following acknowledgement:
     16   1.1      manu  *	This product includes software developed by Emmanuel Dreyfus
     17   1.1      manu  * 4. The name of the author may not be used to endorse or promote
     18   1.1      manu  *    products derived from this software without specific prior written
     19   1.1      manu  *    permission.
     20   1.1      manu  *
     21   1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22   1.1      manu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23   1.1      manu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1      manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25   1.1      manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1      manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1      manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1      manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1      manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1      manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1      manu  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1      manu  */
     33   1.1      manu 
     34   1.1      manu #include <sys/cdefs.h>
     35   1.1      manu 
     36  1.47       dsl __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.47 2014/02/15 10:11:15 dsl Exp $");
     37   1.1      manu 
     38   1.1      manu #include <sys/param.h>
     39   1.1      manu #include <sys/types.h>
     40   1.1      manu #include <sys/systm.h>
     41   1.1      manu #include <sys/signal.h>
     42   1.1      manu #include <sys/exec.h>
     43   1.1      manu #include <sys/proc.h>
     44   1.1      manu #include <sys/ptrace.h> /* for process_read_fpregs() */
     45   1.1      manu #include <sys/ucontext.h>
     46  1.21      manu #include <sys/conf.h>
     47   1.1      manu 
     48   1.1      manu #include <machine/reg.h>
     49   1.1      manu #include <machine/pcb.h>
     50   1.1      manu #include <machine/mcontext.h>
     51   1.2      fvdl #include <machine/specialreg.h>
     52   1.2      fvdl #include <machine/vmparam.h>
     53  1.30  jmcneill #include <machine/cpufunc.h>
     54   1.1      manu 
     55  1.21      manu /*
     56  1.21      manu  * To see whether wscons is configured (for virtual console ioctl calls).
     57  1.21      manu  */
     58  1.21      manu #if defined(_KERNEL_OPT)
     59  1.21      manu #include "wsdisplay.h"
     60  1.21      manu #endif
     61  1.21      manu #if (NWSDISPLAY > 0)
     62  1.21      manu #include <dev/wscons/wsconsio.h>
     63  1.21      manu #include <dev/wscons/wsdisplay_usl_io.h>
     64  1.21      manu #endif
     65  1.21      manu 
     66  1.21      manu 
     67   1.1      manu #include <compat/linux/common/linux_signal.h>
     68   1.1      manu #include <compat/linux/common/linux_errno.h>
     69   1.1      manu #include <compat/linux/common/linux_exec.h>
     70   1.1      manu #include <compat/linux/common/linux_ioctl.h>
     71   1.1      manu #include <compat/linux/common/linux_prctl.h>
     72   1.1      manu #include <compat/linux/common/linux_machdep.h>
     73  1.25     njoly #include <compat/linux/common/linux_ipc.h>
     74  1.25     njoly #include <compat/linux/common/linux_sem.h>
     75   1.4      fvdl #include <compat/linux/linux_syscall.h>
     76   1.1      manu #include <compat/linux/linux_syscallargs.h>
     77   1.1      manu 
     78   1.5      fvdl static void linux_buildcontext(struct lwp *, void *, void *);
     79   1.5      fvdl 
     80   1.1      manu void
     81  1.38       chs linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
     82   1.1      manu {
     83  1.37     rmind 	struct pcb *pcb = lwp_getpcb(l);
     84   1.1      manu 	struct trapframe *tf;
     85   1.1      manu 
     86  1.47       dsl 	fpu_save_area_clear(l, __NetBSD_NPXCW__);
     87   1.1      manu 	pcb->pcb_flags = 0;
     88   1.1      manu 
     89  1.18     pavel 	l->l_proc->p_flag &= ~PK_32;
     90   1.1      manu 
     91   1.1      manu 	tf = l->l_md.md_regs;
     92   1.1      manu 	tf->tf_rax = 0;
     93   1.1      manu 	tf->tf_rbx = 0;
     94   1.1      manu 	tf->tf_rcx = epp->ep_entry;
     95   1.1      manu 	tf->tf_rdx = 0;
     96   1.1      manu 	tf->tf_rsi = 0;
     97   1.1      manu 	tf->tf_rdi = 0;
     98   1.1      manu 	tf->tf_rbp = 0;
     99   1.1      manu 	tf->tf_rsp = stack;
    100   1.1      manu 	tf->tf_r8 = 0;
    101   1.1      manu 	tf->tf_r9 = 0;
    102   1.1      manu 	tf->tf_r10 = 0;
    103   1.1      manu 	tf->tf_r11 = 0;
    104   1.1      manu 	tf->tf_r12 = 0;
    105   1.1      manu 	tf->tf_r13 = 0;
    106   1.1      manu 	tf->tf_r14 = 0;
    107   1.1      manu 	tf->tf_r15 = 0;
    108   1.1      manu 	tf->tf_rip = epp->ep_entry;
    109   1.4      fvdl 	tf->tf_rflags = PSL_USERSET;
    110   1.4      fvdl 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    111   1.4      fvdl 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    112  1.42       chs 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
    113   1.1      manu 	tf->tf_es = 0;
    114  1.38       chs 	cpu_fsgs_zero(l);
    115   1.1      manu 
    116   1.1      manu 	return;
    117   1.1      manu }
    118   1.1      manu 
    119  1.27       dsl void
    120  1.26       dsl linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
    121   1.1      manu {
    122   1.1      manu 	struct lwp *l = curlwp;
    123   1.1      manu 	struct proc *p = l->l_proc;
    124  1.37     rmind 	struct pcb *pcb = lwp_getpcb(l);
    125   1.1      manu 	struct sigacts *ps = p->p_sigacts;
    126  1.16        ad 	int onstack, error;
    127   1.1      manu 	int sig = ksi->ksi_signo;
    128   1.1      manu 	struct linux_rt_sigframe *sfp, sigframe;
    129  1.45       dsl 	struct linux__fpstate *fpsp;
    130   1.1      manu 	struct fpreg fpregs;
    131   1.1      manu 	struct trapframe *tf = l->l_md.md_regs;
    132   1.1      manu 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    133   1.1      manu 	linux_sigset_t lmask;
    134   1.1      manu 	char *sp;
    135   1.4      fvdl 
    136   1.1      manu 	/* Do we need to jump onto the signal stack? */
    137   1.1      manu 	onstack =
    138  1.17        ad 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    139   1.1      manu 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    140   1.1      manu 
    141   1.1      manu 	/* Allocate space for the signal handler context. */
    142   1.1      manu 	if (onstack)
    143  1.20  christos 		sp = ((char *)l->l_sigstk.ss_sp +
    144  1.17        ad 		    l->l_sigstk.ss_size);
    145   1.1      manu 	else
    146  1.20  christos 		sp = (char *)tf->tf_rsp - 128;
    147   1.1      manu 
    148  1.47       dsl 	/* Save FPU state */
    149  1.47       dsl 	sp = (char *) (((long)sp - sizeof (*fpsp)) & ~0xfUL);
    150  1.47       dsl 	fpsp = (struct linux__fpstate *)sp;
    151   1.1      manu 
    152   1.1      manu 	/*
    153   1.1      manu 	 * Populate the rt_sigframe
    154   1.1      manu 	 */
    155   1.1      manu 	sp = (char *)
    156   1.1      manu 	    ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
    157   1.1      manu 	sfp = (struct linux_rt_sigframe *)sp;
    158   1.1      manu 
    159  1.35    cegger 	memset(&sigframe, 0, sizeof(sigframe));
    160   1.3      manu 	if (ps->sa_sigdesc[sig].sd_vers != 0)
    161   1.7      manu 		sigframe.pretcode =
    162   1.7      manu 		    (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
    163   1.3      manu 	else
    164   1.3      manu 		sigframe.pretcode = NULL;
    165   1.1      manu 
    166   1.1      manu 	/*
    167   1.1      manu 	 * The user context
    168   1.1      manu 	 */
    169   1.1      manu 	sigframe.uc.luc_flags = 0;
    170   1.1      manu 	sigframe.uc.luc_link = NULL;
    171   1.1      manu 
    172   1.1      manu 	/* This is used regardless of SA_ONSTACK in Linux */
    173  1.17        ad 	sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
    174  1.17        ad 	sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
    175   1.1      manu 	sigframe.uc.luc_stack.ss_flags = 0;
    176  1.17        ad 	if (l->l_sigstk.ss_flags & SS_ONSTACK)
    177   1.1      manu 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
    178  1.17        ad 	if (l->l_sigstk.ss_flags & SS_DISABLE)
    179   1.1      manu 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
    180   1.1      manu 
    181   1.1      manu 	sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
    182   1.1      manu 	sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
    183   1.1      manu 	sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
    184   1.1      manu 	sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
    185   1.1      manu 	sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
    186   1.1      manu 	sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
    187   1.1      manu 	sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
    188   1.1      manu 	sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
    189   1.1      manu 	sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
    190   1.1      manu 	sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
    191   1.1      manu 	sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
    192   1.1      manu 	sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
    193   1.1      manu 	sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
    194  1.23     njoly 	sigframe.uc.luc_mcontext.rax = tf->tf_rax;
    195   1.1      manu 	sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
    196   1.1      manu 	sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
    197   1.6      fvdl 	sigframe.uc.luc_mcontext.rip = tf->tf_rip;
    198   1.1      manu 	sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
    199   1.1      manu 	sigframe.uc.luc_mcontext.cs = tf->tf_cs;
    200   1.1      manu 	sigframe.uc.luc_mcontext.gs = tf->tf_gs;
    201   1.1      manu 	sigframe.uc.luc_mcontext.fs = tf->tf_fs;
    202   1.1      manu 	sigframe.uc.luc_mcontext.err = tf->tf_err;
    203   1.1      manu 	sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
    204   1.1      manu 	native_to_linux_sigset(&lmask, mask);
    205   1.1      manu 	sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
    206  1.37     rmind 	sigframe.uc.luc_mcontext.cr2 = (long)pcb->pcb_onfault;
    207   1.1      manu 	sigframe.uc.luc_mcontext.fpstate = fpsp;
    208   1.1      manu 	native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
    209  1.39  christos 	native_to_linux_siginfo(&sigframe.info, &ksi->ksi_info);
    210  1.16        ad 	sendsig_reset(l, sig);
    211  1.29        ad 	mutex_exit(p->p_lock);
    212  1.17        ad 	error = 0;
    213  1.17        ad 
    214  1.17        ad 	/*
    215  1.17        ad 	 * Save FPU state, if any
    216  1.17        ad 	 */
    217  1.17        ad 	if (fpsp != NULL) {
    218  1.44       dsl 		size_t fp_size = sizeof fpregs;
    219  1.45       dsl 		/* The netbsd and linux structures both match the fxsave data */
    220  1.44       dsl 		(void)process_read_fpregs(l, &fpregs, &fp_size);
    221  1.45       dsl 		error = copyout(&fpregs, fpsp, sizeof(*fpsp));
    222  1.17        ad 	}
    223  1.17        ad 
    224  1.17        ad 	if (error == 0)
    225  1.17        ad 		error = copyout(&sigframe, sp, sizeof(sigframe));
    226  1.17        ad 
    227  1.29        ad 	mutex_enter(p->p_lock);
    228  1.16        ad 
    229  1.16        ad 	if (error != 0) {
    230   1.1      manu 		sigexit(l, SIGILL);
    231   1.1      manu 		return;
    232   1.1      manu 	}
    233   1.1      manu 
    234   1.6      fvdl 	linux_buildcontext(l, catcher, sp);
    235   1.1      manu 	tf->tf_rdi = sigframe.info.lsi_signo;
    236   1.1      manu 	tf->tf_rax = 0;
    237   1.1      manu 	tf->tf_rsi = (long)&sfp->info;
    238   1.1      manu 	tf->tf_rdx = (long)&sfp->uc;
    239   1.1      manu 
    240   1.1      manu 	/*
    241   1.1      manu 	 * Remember we use signal stack
    242   1.1      manu 	 */
    243   1.1      manu 	if (onstack)
    244  1.17        ad 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    245   1.1      manu 	return;
    246   1.1      manu }
    247   1.1      manu 
    248  1.27       dsl int
    249  1.27       dsl linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
    250  1.27       dsl {
    251  1.14      manu 	printf("linux_sys_modify_ldt\n");
    252   1.1      manu 	return 0;
    253   1.1      manu }
    254   1.1      manu 
    255  1.27       dsl int
    256  1.27       dsl linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
    257  1.27       dsl {
    258   1.1      manu 	return 0;
    259   1.1      manu }
    260   1.1      manu 
    261  1.27       dsl int
    262  1.27       dsl linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
    263  1.27       dsl {
    264   1.1      manu 	return 0;
    265   1.1      manu }
    266   1.1      manu 
    267   1.1      manu dev_t
    268  1.26       dsl linux_fakedev(dev_t dev, int raw)
    269   1.1      manu {
    270  1.21      manu 
    271  1.21      manu        extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
    272  1.21      manu        const struct cdevsw *cd = cdevsw_lookup(dev);
    273  1.21      manu 
    274  1.21      manu        if (raw) {
    275  1.21      manu #if (NWSDISPLAY > 0)
    276  1.21      manu 	       extern const struct cdevsw wsdisplay_cdevsw;
    277  1.21      manu 	       if (cd == &wsdisplay_cdevsw)
    278  1.21      manu 		       return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
    279  1.21      manu #endif
    280  1.21      manu        }
    281  1.21      manu 
    282  1.21      manu        if (cd == &ptc_cdevsw)
    283  1.21      manu 	       return makedev(LINUX_PTC_MAJOR, minor(dev));
    284  1.21      manu        if (cd == &pts_cdevsw)
    285  1.21      manu 	       return makedev(LINUX_PTS_MAJOR, minor(dev));
    286  1.21      manu 
    287  1.15      manu 	return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
    288  1.15      manu 	    | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
    289  1.15      manu 	    | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
    290   1.1      manu }
    291   1.1      manu 
    292  1.27       dsl int
    293  1.27       dsl linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
    294  1.27       dsl {
    295   1.1      manu 	return 0;
    296   1.1      manu }
    297   1.1      manu 
    298   1.1      manu int
    299  1.27       dsl linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
    300  1.27       dsl {
    301   1.6      fvdl 	struct linux_ucontext *luctx;
    302   1.1      manu 	struct trapframe *tf = l->l_md.md_regs;
    303   1.1      manu 	struct linux_sigcontext *lsigctx;
    304   1.6      fvdl 	struct linux_rt_sigframe frame, *fp;
    305   1.1      manu 	ucontext_t uctx;
    306   1.1      manu 	mcontext_t *mctx;
    307  1.45       dsl 	struct fxsave *fxarea;
    308   1.1      manu 	int error;
    309   1.1      manu 
    310   1.6      fvdl 	fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
    311   1.6      fvdl 	if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
    312  1.29        ad 		mutex_enter(l->l_proc->p_lock);
    313   1.1      manu 		sigexit(l, SIGILL);
    314   1.1      manu 		return error;
    315   1.1      manu 	}
    316   1.6      fvdl 	luctx = &frame.uc;
    317   1.6      fvdl 	lsigctx = &luctx->luc_mcontext;
    318   1.1      manu 
    319  1.35    cegger 	memset(&uctx, 0, sizeof(uctx));
    320   1.1      manu 	mctx = (mcontext_t *)&uctx.uc_mcontext;
    321  1.45       dsl 	fxarea = (struct fxsave *)&mctx->__fpregs;
    322   1.1      manu 
    323   1.1      manu 	/*
    324   1.1      manu 	 * Set the flags. Linux always have CPU, stack and signal state,
    325   1.1      manu 	 * FPU is optional. uc_flags is not used to tell what we have.
    326   1.1      manu 	 */
    327   1.1      manu 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
    328   1.1      manu 	if (lsigctx->fpstate != NULL)
    329   1.1      manu 		uctx.uc_flags |= _UC_FPU;
    330   1.1      manu 	uctx.uc_link = NULL;
    331   1.1      manu 
    332   1.1      manu 	/*
    333   1.1      manu 	 * Signal set
    334   1.1      manu 	 */
    335   1.6      fvdl 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
    336   1.1      manu 
    337   1.1      manu 	/*
    338   1.1      manu 	 * CPU state
    339   1.1      manu 	 */
    340   1.1      manu 	mctx->__gregs[_REG_R8] = lsigctx->r8;
    341   1.1      manu 	mctx->__gregs[_REG_R9] = lsigctx->r9;
    342   1.1      manu 	mctx->__gregs[_REG_R10] = lsigctx->r10;
    343   1.1      manu 	mctx->__gregs[_REG_R11] = lsigctx->r11;
    344   1.1      manu 	mctx->__gregs[_REG_R12] = lsigctx->r12;
    345   1.1      manu 	mctx->__gregs[_REG_R13] = lsigctx->r13;
    346   1.1      manu 	mctx->__gregs[_REG_R14] = lsigctx->r14;
    347   1.1      manu 	mctx->__gregs[_REG_R15] = lsigctx->r15;
    348   1.1      manu 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
    349   1.1      manu 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
    350   1.1      manu 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
    351   1.1      manu 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
    352  1.23     njoly 	mctx->__gregs[_REG_RAX] = lsigctx->rax;
    353   1.1      manu 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
    354   1.1      manu 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
    355   1.1      manu 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
    356  1.28       dsl 	mctx->__gregs[_REG_RFLAGS] = lsigctx->eflags;
    357   1.1      manu 	mctx->__gregs[_REG_CS] = lsigctx->cs;
    358   1.1      manu 	mctx->__gregs[_REG_GS] = lsigctx->gs;
    359   1.1      manu 	mctx->__gregs[_REG_FS] = lsigctx->fs;
    360   1.1      manu 	mctx->__gregs[_REG_ERR] = lsigctx->err;
    361   1.1      manu 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
    362   1.1      manu 	mctx->__gregs[_REG_ES] = tf->tf_es;
    363   1.1      manu 	mctx->__gregs[_REG_DS] = tf->tf_ds;
    364  1.28       dsl 	mctx->__gregs[_REG_RSP] = lsigctx->rsp; /* XXX */
    365   1.1      manu 	mctx->__gregs[_REG_SS] = tf->tf_ss;
    366   1.1      manu 
    367   1.1      manu 	/*
    368   1.1      manu 	 * FPU state
    369   1.1      manu 	 */
    370   1.1      manu 	if (lsigctx->fpstate != NULL) {
    371  1.45       dsl 		/* Both structures match the fxstate data */
    372  1.45       dsl 		error = copyin(lsigctx->fpstate, fxarea, sizeof(*fxarea));
    373   1.1      manu 		if (error != 0) {
    374  1.29        ad 			mutex_enter(l->l_proc->p_lock);
    375   1.1      manu 			sigexit(l, SIGILL);
    376   1.1      manu 			return error;
    377   1.1      manu 		}
    378   1.1      manu 	}
    379   1.1      manu 
    380   1.1      manu 	/*
    381   1.1      manu 	 * And the stack
    382   1.1      manu 	 */
    383   1.1      manu 	uctx.uc_stack.ss_flags = 0;
    384  1.31  jmcneill 	if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
    385  1.31  jmcneill 		uctx.uc_stack.ss_flags |= SS_ONSTACK;
    386   1.1      manu 
    387  1.31  jmcneill 	if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
    388  1.31  jmcneill 		uctx.uc_stack.ss_flags |= SS_DISABLE;
    389   1.1      manu 
    390   1.6      fvdl 	uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
    391   1.6      fvdl 	uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
    392   1.1      manu 
    393   1.1      manu 	/*
    394   1.1      manu 	 * And let setucontext deal with that.
    395   1.1      manu 	 */
    396  1.29        ad 	mutex_enter(l->l_proc->p_lock);
    397  1.16        ad 	error = setucontext(l, &uctx);
    398  1.29        ad 	mutex_exit(l->l_proc->p_lock);
    399  1.23     njoly 	if (error)
    400  1.23     njoly 		return error;
    401  1.16        ad 
    402  1.23     njoly 	return EJUSTRETURN;
    403   1.1      manu }
    404   1.1      manu 
    405   1.1      manu int
    406  1.37     rmind linux_sys_arch_prctl(struct lwp *l,
    407  1.37     rmind     const struct linux_sys_arch_prctl_args *uap, register_t *retval)
    408   1.1      manu {
    409  1.27       dsl 	/* {
    410   1.1      manu 		syscallarg(int) code;
    411   1.1      manu 		syscallarg(unsigned long) addr;
    412  1.27       dsl 	} */
    413  1.38       chs 	void *addr = (void *)SCARG(uap, addr);
    414   1.1      manu 
    415   1.1      manu 	switch(SCARG(uap, code)) {
    416   1.1      manu 	case LINUX_ARCH_SET_GS:
    417  1.38       chs 		return x86_set_sdbase(addr, 'g', l, true);
    418   1.1      manu 
    419   1.1      manu 	case LINUX_ARCH_GET_GS:
    420  1.38       chs 		return x86_get_sdbase(addr, 'g');
    421   1.1      manu 
    422   1.1      manu 	case LINUX_ARCH_SET_FS:
    423  1.38       chs 		return x86_set_sdbase(addr, 'f', l, true);
    424   1.1      manu 
    425   1.1      manu 	case LINUX_ARCH_GET_FS:
    426  1.38       chs 		return x86_get_sdbase(addr, 'f');
    427   1.1      manu 
    428   1.1      manu 	default:
    429   1.1      manu #ifdef DEBUG_LINUX
    430   1.1      manu 		printf("linux_sys_arch_prctl: unexpected code %d\n",
    431   1.1      manu 		    SCARG(uap, code));
    432   1.1      manu #endif
    433   1.1      manu 		return EINVAL;
    434   1.1      manu 	}
    435  1.38       chs 	/* NOTREACHED */
    436   1.1      manu }
    437   1.4      fvdl 
    438   1.4      fvdl const int linux_vsyscall_to_syscall[] = {
    439   1.4      fvdl 	LINUX_SYS_gettimeofday,
    440   1.4      fvdl 	LINUX_SYS_time,
    441  1.34  christos 	LINUX_SYS_nosys,	/* nosys */
    442  1.34  christos 	LINUX_SYS_nosys,	/* nosys */
    443   1.4      fvdl };
    444   1.4      fvdl 
    445   1.4      fvdl int
    446   1.4      fvdl linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    447   1.4      fvdl {
    448   1.4      fvdl 	struct trapframe *tf = arg;
    449   1.4      fvdl 	uint64_t retaddr;
    450   1.4      fvdl 	int vsyscallnr;
    451   1.4      fvdl 
    452   1.4      fvdl 	/*
    453   1.4      fvdl 	 * Check for a vsyscall. %rip must be the fault address,
    454   1.4      fvdl 	 * and the address must be in the Linux vsyscall area.
    455   1.4      fvdl 	 * Also, vsyscalls are only done at 1024-byte boundaries.
    456   1.4      fvdl 	 */
    457   1.4      fvdl 
    458   1.4      fvdl 	if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
    459   1.4      fvdl 		return 0;
    460   1.4      fvdl 
    461   1.4      fvdl 	if (trapaddr != tf->tf_rip)
    462   1.4      fvdl 		return 0;
    463   1.4      fvdl 
    464   1.4      fvdl 	if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
    465   1.4      fvdl 		return 0;
    466   1.4      fvdl 
    467   1.4      fvdl 	vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
    468   1.4      fvdl 
    469   1.4      fvdl 	if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
    470   1.4      fvdl 		return 0;
    471   1.4      fvdl 
    472   1.4      fvdl 	/*
    473   1.4      fvdl 	 * Get the return address from the top of the stack,
    474   1.4      fvdl 	 * and fix up the return address.
    475   1.4      fvdl 	 * This assumes the faulting instruction was callq *reg,
    476   1.4      fvdl 	 * which is the only way that vsyscalls are ever entered.
    477   1.4      fvdl 	 */
    478   1.4      fvdl 	if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
    479   1.4      fvdl 		return 0;
    480   1.4      fvdl 	tf->tf_rip = retaddr;
    481   1.4      fvdl 	tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
    482   1.4      fvdl 	tf->tf_rsp += 8;	/* "pop" the return address */
    483   1.4      fvdl 
    484   1.4      fvdl #if 0
    485   1.4      fvdl 	printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
    486   1.4      fvdl 	    (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
    487   1.4      fvdl 	    vsyscallnr, (int)tf->tf_rax);
    488   1.4      fvdl #endif
    489   1.4      fvdl 
    490   1.4      fvdl 	(*l->l_proc->p_md.md_syscall)(tf);
    491   1.4      fvdl 
    492   1.4      fvdl 	return 1;
    493   1.4      fvdl }
    494   1.5      fvdl 
    495   1.5      fvdl static void
    496   1.5      fvdl linux_buildcontext(struct lwp *l, void *catcher, void *f)
    497   1.5      fvdl {
    498   1.5      fvdl 	struct trapframe *tf = l->l_md.md_regs;
    499   1.5      fvdl 
    500   1.5      fvdl 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
    501   1.5      fvdl 	tf->tf_rip = (u_int64_t)catcher;
    502   1.5      fvdl 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    503  1.32  christos 	tf->tf_rflags &= ~PSL_CLEARSIG;
    504   1.5      fvdl 	tf->tf_rsp = (u_int64_t)f;
    505   1.5      fvdl 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    506   1.5      fvdl }
    507