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