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