Home | History | Annotate | Line # | Download | only in amd64
linux32_machdep.c revision 1.42
      1  1.42      maxv /*	$NetBSD: linux32_machdep.c,v 1.42 2017/10/19 10:01:09 maxv Exp $ */
      2   1.1      manu 
      3   1.1      manu /*-
      4   1.1      manu  * Copyright (c) 2006 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 #include <sys/cdefs.h>
     34  1.42      maxv __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.42 2017/10/19 10:01:09 maxv Exp $");
     35   1.1      manu 
     36   1.1      manu #include <sys/param.h>
     37   1.1      manu #include <sys/proc.h>
     38   1.1      manu #include <sys/exec.h>
     39   1.1      manu 
     40  1.24       chs #include <machine/vmparam.h>
     41  1.24       chs #include <machine/cpufunc.h>
     42   1.1      manu #include <machine/netbsd32_machdep.h>
     43   1.1      manu 
     44  1.36       dsl #include <x86/fpu.h>
     45  1.36       dsl 
     46   1.1      manu #include <compat/netbsd32/netbsd32.h>
     47   1.1      manu #include <compat/netbsd32/netbsd32_syscallargs.h>
     48   1.1      manu 
     49  1.24       chs #include <compat/linux/common/linux_types.h>
     50  1.24       chs #include <compat/linux/common/linux_emuldata.h>
     51   1.1      manu #include <compat/linux/common/linux_signal.h>
     52   1.1      manu #include <compat/linux/common/linux_errno.h>
     53  1.24       chs #include <compat/linux/common/linux_exec.h>
     54  1.25      jmmv #include <compat/linux/common/linux_ipc.h>
     55  1.25      jmmv #include <compat/linux/common/linux_sem.h>
     56  1.24       chs #include <compat/linux/linux_syscallargs.h>
     57   1.1      manu 
     58   1.1      manu #include <compat/linux32/common/linux32_types.h>
     59   1.1      manu #include <compat/linux32/common/linux32_errno.h>
     60   1.1      manu #include <compat/linux32/common/linux32_machdep.h>
     61   1.1      manu #include <compat/linux32/common/linux32_signal.h>
     62   1.1      manu #include <compat/linux32/common/linux32_exec.h>
     63  1.25      jmmv #include <compat/linux32/common/linux32_ipc.h>
     64  1.25      jmmv #include <compat/linux32/common/linux32_sem.h>
     65   1.1      manu #include <compat/linux32/linux32_syscallargs.h>
     66   1.1      manu 
     67  1.24       chs #ifdef DEBUG_LINUX
     68  1.24       chs #define DPRINTF(a) uprintf a
     69  1.24       chs #else
     70  1.24       chs #define DPRINTF(a)
     71  1.24       chs #endif
     72   1.1      manu 
     73  1.24       chs extern char linux32_sigcode[];
     74  1.24       chs extern char linux32_rt_sigcode[];
     75  1.24       chs extern char linux32_esigcode[];
     76   1.1      manu 
     77  1.14       dsl static void linux32_save_ucontext(struct lwp *, struct trapframe *,
     78  1.14       dsl     const sigset_t *, struct sigaltstack *, struct linux32_ucontext *);
     79  1.14       dsl static void linux32_save_sigcontext(struct lwp *, struct trapframe *,
     80  1.14       dsl     const sigset_t *, struct linux32_sigcontext *);
     81  1.14       dsl static void linux32_rt_sendsig(const ksiginfo_t *, const sigset_t *);
     82  1.14       dsl static void linux32_old_sendsig(const ksiginfo_t *, const sigset_t *);
     83  1.14       dsl static int linux32_restore_sigcontext(struct lwp *,
     84  1.14       dsl     struct linux32_sigcontext *, register_t *);
     85   1.1      manu 
     86   1.1      manu void
     87   1.1      manu linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
     88   1.1      manu {
     89   1.1      manu 	if (SIGACTION(curproc, ksi->ksi_signo).sa_flags & SA_SIGINFO)
     90   1.1      manu 		linux32_rt_sendsig(ksi, mask);
     91   1.1      manu 	else
     92   1.1      manu 		linux32_old_sendsig(ksi, mask);
     93   1.1      manu 	return;
     94   1.1      manu }
     95   1.1      manu 
     96   1.1      manu void
     97   1.1      manu linux32_old_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
     98   1.1      manu {
     99   1.1      manu 	struct lwp *l = curlwp;
    100   1.1      manu 	struct proc *p = l->l_proc;
    101   1.1      manu 	struct trapframe *tf;
    102   1.1      manu 	struct linux32_sigframe *fp, frame;
    103   1.4        ad 	int onstack, error;
    104   1.1      manu 	int sig = ksi->ksi_signo;
    105   1.1      manu 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    106   1.5        ad 	struct sigaltstack *sas = &l->l_sigstk;
    107   1.1      manu 
    108   1.1      manu 	tf = l->l_md.md_regs;
    109   1.1      manu 	/* Do we need to jump onto the signal stack? */
    110   1.1      manu 	onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    111   1.1      manu 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    112   1.1      manu 
    113   1.1      manu 
    114   1.1      manu 	/* Allocate space for the signal handler context. */
    115   1.1      manu 	if (onstack)
    116   1.8  christos 		fp = (struct linux32_sigframe *)((char *)sas->ss_sp +
    117   1.1      manu 		    sas->ss_size);
    118   1.1      manu 	else
    119   1.1      manu 		fp = (struct linux32_sigframe *)tf->tf_rsp;
    120   1.1      manu 	fp--;
    121   1.1      manu 
    122  1.26  christos 	DPRINTF(("old: onstack = %d, fp = %p sig = %d rip = 0x%lx\n",
    123  1.26  christos 	    onstack, fp, sig, tf->tf_rip));
    124  1.24       chs 
    125   1.1      manu 	/* Build stack frame for signal trampoline. */
    126   1.9       dsl 	NETBSD32PTR32(frame.sf_handler, catcher);
    127   1.1      manu 	frame.sf_sig = native_to_linux32_signo[sig];
    128   1.1      manu 
    129   1.1      manu 	linux32_save_sigcontext(l, tf, mask, &frame.sf_sc);
    130   1.1      manu 
    131   1.4        ad 	sendsig_reset(l, sig);
    132  1.17        ad 	mutex_exit(p->p_lock);
    133   1.4        ad 	error = copyout(&frame, fp, sizeof(frame));
    134  1.17        ad 	mutex_enter(p->p_lock);
    135   1.4        ad 
    136   1.4        ad 	if (error != 0) {
    137   1.1      manu 		/*
    138   1.1      manu 		 * Process has trashed its stack; give it an illegal
    139   1.1      manu 		 * instruction to halt it in its tracks.
    140   1.1      manu 		 */
    141   1.1      manu 		sigexit(l, SIGILL);
    142   1.1      manu 		/* NOTREACHED */
    143   1.1      manu 	}
    144   1.1      manu 
    145   1.1      manu 	/*
    146   1.1      manu 	 * Build context to run handler in.
    147   1.1      manu 	 */
    148   1.1      manu 	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    149   1.1      manu 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    150   1.1      manu 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    151   1.1      manu 	tf->tf_rip = ((long)p->p_sigctx.ps_sigcode) & 0xffffffff;
    152   1.1      manu 	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
    153  1.18  christos 	tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff;
    154   1.1      manu 	tf->tf_rsp = (long)fp & 0xffffffff;
    155   1.1      manu 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    156   1.1      manu 
    157   1.1      manu 	/* Remember that we're now on the signal stack. */
    158   1.1      manu 	if (onstack)
    159   1.1      manu 		sas->ss_flags |= SS_ONSTACK;
    160   1.1      manu 
    161   1.1      manu 	return;
    162   1.1      manu }
    163   1.1      manu 
    164   1.1      manu void
    165   1.1      manu linux32_rt_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
    166   1.1      manu {
    167   1.1      manu 	struct lwp *l = curlwp;
    168   1.1      manu 	struct proc *p = l->l_proc;
    169   1.1      manu 	struct trapframe *tf;
    170   1.1      manu 	struct linux32_rt_sigframe *fp, frame;
    171   1.4        ad 	int onstack, error;
    172   1.1      manu 	linux32_siginfo_t *lsi;
    173   1.5        ad 	int sig = ksi->ksi_signo;
    174   1.1      manu 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    175   1.5        ad 	struct sigaltstack *sas = &l->l_sigstk;
    176   1.1      manu 
    177   1.1      manu 	tf = l->l_md.md_regs;
    178   1.1      manu 	/* Do we need to jump onto the signal stack? */
    179   1.1      manu 	onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    180   1.1      manu 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    181   1.1      manu 
    182   1.1      manu 
    183   1.1      manu 	/* Allocate space for the signal handler context. */
    184   1.1      manu 	if (onstack)
    185   1.8  christos 		fp = (struct linux32_rt_sigframe *)((char *)sas->ss_sp +
    186   1.1      manu 		    sas->ss_size);
    187   1.1      manu 	else
    188   1.1      manu 		fp = (struct linux32_rt_sigframe *)tf->tf_rsp;
    189   1.1      manu 	fp--;
    190   1.1      manu 
    191   1.1      manu 	/* Build stack frame for signal trampoline. */
    192   1.9       dsl 	NETBSD32PTR32(frame.sf_handler, catcher);
    193   1.1      manu 	frame.sf_sig = native_to_linux32_signo[sig];
    194   1.9       dsl 	NETBSD32PTR32(frame.sf_sip, &fp->sf_si);
    195   1.9       dsl 	NETBSD32PTR32(frame.sf_ucp, &fp->sf_uc);
    196   1.1      manu 
    197  1.26  christos 	DPRINTF(("rt: onstack = %d, fp = %p sig = %d rip = 0x%lx\n",
    198  1.26  christos 	    onstack, fp, sig, tf->tf_rip));
    199  1.24       chs 
    200   1.1      manu 	lsi = &frame.sf_si;
    201   1.1      manu 	(void)memset(lsi, 0, sizeof(frame.sf_si));
    202   1.1      manu 	lsi->lsi_errno = native_to_linux32_errno[ksi->ksi_errno];
    203  1.20     njoly 	lsi->lsi_code = native_to_linux_si_code(ksi->ksi_code);
    204  1.19     njoly 	lsi->lsi_signo = frame.sf_sig;
    205   1.1      manu 	switch (lsi->lsi_signo) {
    206   1.1      manu 	case LINUX32_SIGILL:
    207   1.1      manu 	case LINUX32_SIGFPE:
    208   1.1      manu 	case LINUX32_SIGSEGV:
    209   1.1      manu 	case LINUX32_SIGBUS:
    210   1.1      manu 	case LINUX32_SIGTRAP:
    211   1.9       dsl 		NETBSD32PTR32(lsi->lsi_addr, ksi->ksi_addr);
    212   1.1      manu 		break;
    213   1.1      manu 	case LINUX32_SIGCHLD:
    214   1.1      manu 		lsi->lsi_uid = ksi->ksi_uid;
    215   1.1      manu 		lsi->lsi_pid = ksi->ksi_pid;
    216   1.1      manu 		lsi->lsi_utime = ksi->ksi_utime;
    217   1.1      manu 		lsi->lsi_stime = ksi->ksi_stime;
    218  1.22     njoly 		lsi->lsi_status = native_to_linux_si_status(ksi->ksi_code,
    219  1.22     njoly 		    ksi->ksi_status);
    220   1.1      manu 		break;
    221   1.1      manu 	case LINUX32_SIGIO:
    222   1.1      manu 		lsi->lsi_band = ksi->ksi_band;
    223   1.1      manu 		lsi->lsi_fd = ksi->ksi_fd;
    224   1.1      manu 		break;
    225   1.1      manu 	default:
    226   1.1      manu 		lsi->lsi_uid = ksi->ksi_uid;
    227   1.1      manu 		lsi->lsi_pid = ksi->ksi_pid;
    228   1.1      manu 		if (lsi->lsi_signo == LINUX32_SIGALRM ||
    229   1.1      manu 		    lsi->lsi_signo >= LINUX32_SIGRTMIN)
    230   1.9       dsl 			NETBSD32PTR32(lsi->lsi_value.sival_ptr,
    231  1.11  christos 			     ksi->ksi_value.sival_ptr);
    232   1.1      manu 		break;
    233   1.1      manu 	}
    234   1.1      manu 
    235   1.1      manu 	/* Save register context. */
    236  1.13  christos 	linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc);
    237   1.4        ad 	sendsig_reset(l, sig);
    238  1.17        ad 	mutex_exit(p->p_lock);
    239   1.4        ad 	error = copyout(&frame, fp, sizeof(frame));
    240  1.17        ad 	mutex_enter(p->p_lock);
    241   1.1      manu 
    242   1.4        ad 	if (error != 0) {
    243   1.1      manu 		/*
    244   1.1      manu 		 * Process has trashed its stack; give it an illegal
    245   1.1      manu 		 * instruction to halt it in its tracks.
    246   1.1      manu 		 */
    247   1.1      manu 		sigexit(l, SIGILL);
    248   1.1      manu 		/* NOTREACHED */
    249   1.1      manu 	}
    250   1.1      manu 
    251   1.1      manu 	/*
    252   1.1      manu 	 * Build context to run handler in.
    253   1.1      manu 	 */
    254   1.1      manu 	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    255   1.1      manu 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    256   1.1      manu 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    257   1.1      manu 	tf->tf_rip = (((long)p->p_sigctx.ps_sigcode) +
    258   1.1      manu 	    (linux32_rt_sigcode - linux32_sigcode)) & 0xffffffff;
    259   1.1      manu 	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
    260  1.18  christos 	tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff;
    261   1.1      manu 	tf->tf_rsp = (long)fp & 0xffffffff;
    262   1.1      manu 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
    263   1.1      manu 
    264   1.1      manu 	/* Remember that we're now on the signal stack. */
    265   1.1      manu 	if (onstack)
    266   1.1      manu 		sas->ss_flags |= SS_ONSTACK;
    267   1.1      manu 
    268   1.1      manu 	return;
    269   1.1      manu }
    270   1.1      manu 
    271   1.1      manu void
    272   1.1      manu linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
    273   1.1      manu {
    274  1.23     rmind 	struct pcb *pcb = lwp_getpcb(l);
    275   1.1      manu 	struct trapframe *tf;
    276   1.1      manu 	struct proc *p = l->l_proc;
    277   1.1      manu 
    278  1.38      maxv #if defined(USER_LDT)
    279  1.37      maxv 	pmap_ldt_cleanup(l);
    280   1.1      manu #endif
    281   1.1      manu 
    282   1.3  christos 	netbsd32_adjust_limits(p);
    283   1.1      manu 
    284  1.35       dsl 	fpu_save_area_clear(l, __Linux_NPXCW__);
    285  1.35       dsl 
    286  1.40      maxv 	l->l_md.md_flags = MDL_COMPAT32;	/* Forces iret not sysret */
    287  1.24       chs 	pcb->pcb_flags = PCB_COMPAT32;
    288   1.1      manu 
    289   1.6     pavel 	p->p_flag |= PK_32;
    290   1.1      manu 
    291   1.1      manu 	tf = l->l_md.md_regs;
    292   1.1      manu 	tf->tf_rax = 0;
    293  1.29     joerg 	tf->tf_rbx = (u_int32_t)p->p_psstrp;
    294   1.1      manu 	tf->tf_rcx = pack->ep_entry & 0xffffffff;
    295   1.1      manu 	tf->tf_rdx = 0;
    296   1.1      manu 	tf->tf_rsi = 0;
    297   1.1      manu 	tf->tf_rdi = 0;
    298   1.1      manu 	tf->tf_rbp = 0;
    299   1.1      manu 	tf->tf_rsp = stack & 0xffffffff;
    300   1.1      manu 	tf->tf_r8 = 0;
    301   1.1      manu 	tf->tf_r9 = 0;
    302   1.1      manu 	tf->tf_r10 = 0;
    303   1.1      manu 	tf->tf_r11 = 0;
    304   1.1      manu 	tf->tf_r12 = 0;
    305   1.1      manu 	tf->tf_r13 = 0;
    306   1.1      manu 	tf->tf_r14 = 0;
    307   1.1      manu 	tf->tf_r15 = 0;
    308   1.1      manu 	tf->tf_rip = pack->ep_entry & 0xffffffff;
    309   1.1      manu 	tf->tf_rflags = PSL_USERSET;
    310  1.24       chs 	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL);
    311  1.24       chs 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL);
    312  1.24       chs 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL);
    313  1.24       chs 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL);
    314  1.41      maxv 	cpu_segregs32_zero(l);
    315  1.24       chs 	cpu_fsgs_reload(l, GSEL(GUDATA32_SEL, SEL_UPL), GSEL(GUDATA32_SEL, SEL_UPL));
    316   1.1      manu }
    317   1.1      manu 
    318   1.1      manu static void
    319  1.23     rmind linux32_save_ucontext(struct lwp *l, struct trapframe *tf,
    320  1.23     rmind     const sigset_t *mask, struct sigaltstack *sas, struct linux32_ucontext *uc)
    321   1.1      manu {
    322  1.23     rmind 
    323   1.1      manu 	uc->uc_flags = 0;
    324   1.9       dsl 	NETBSD32PTR32(uc->uc_link, NULL);
    325   1.1      manu 	native_to_linux32_sigaltstack(&uc->uc_stack, sas);
    326   1.1      manu 	linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext);
    327   1.1      manu 	native_to_linux32_sigset(&uc->uc_sigmask, mask);
    328   1.1      manu 	(void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem));
    329   1.1      manu }
    330   1.1      manu 
    331   1.1      manu static void
    332  1.21    cegger linux32_save_sigcontext(struct lwp *l, struct trapframe *tf,
    333  1.21    cegger 			const sigset_t *mask, struct linux32_sigcontext *sc)
    334   1.1      manu {
    335  1.23     rmind 	struct pcb *pcb = lwp_getpcb(l);
    336  1.23     rmind 
    337   1.1      manu 	/* Save register context. */
    338  1.42      maxv 	sc->sc_gs = tf->tf_gs & 0xFFFF;
    339  1.42      maxv 	sc->sc_fs = tf->tf_fs & 0xFFFF;
    340  1.42      maxv 	sc->sc_es = tf->tf_es & 0xFFFF;
    341  1.42      maxv 	sc->sc_ds = tf->tf_ds & 0xFFFF;
    342   1.1      manu 	sc->sc_eflags = tf->tf_rflags;
    343   1.1      manu 	sc->sc_edi = tf->tf_rdi;
    344   1.1      manu 	sc->sc_esi = tf->tf_rsi;
    345   1.1      manu 	sc->sc_esp = tf->tf_rsp;
    346   1.1      manu 	sc->sc_ebp = tf->tf_rbp;
    347   1.1      manu 	sc->sc_ebx = tf->tf_rbx;
    348   1.1      manu 	sc->sc_edx = tf->tf_rdx;
    349   1.1      manu 	sc->sc_ecx = tf->tf_rcx;
    350   1.1      manu 	sc->sc_eax = tf->tf_rax;
    351   1.1      manu 	sc->sc_eip = tf->tf_rip;
    352  1.42      maxv 	sc->sc_cs = tf->tf_cs & 0xFFFF;
    353   1.1      manu 	sc->sc_esp_at_signal = tf->tf_rsp;
    354  1.42      maxv 	sc->sc_ss = tf->tf_ss & 0xFFFF;
    355   1.1      manu 	sc->sc_err = tf->tf_err;
    356   1.1      manu 	sc->sc_trapno = tf->tf_trapno;
    357  1.23     rmind 	sc->sc_cr2 = pcb->pcb_cr2;
    358   1.9       dsl 	NETBSD32PTR32(sc->sc_387, NULL);
    359   1.1      manu 
    360   1.1      manu 	/* Save signal stack. */
    361   1.1      manu 	/* Linux doesn't save the onstack flag in sigframe */
    362   1.1      manu 
    363   1.1      manu 	/* Save signal mask. */
    364   1.1      manu 	native_to_linux32_old_sigset(&sc->sc_mask, mask);
    365   1.1      manu }
    366   1.1      manu 
    367   1.1      manu int
    368  1.23     rmind linux32_sys_sigreturn(struct lwp *l,
    369  1.23     rmind     const struct linux32_sys_sigreturn_args *uap, register_t *retval)
    370   1.1      manu {
    371  1.16       dsl 	/* {
    372   1.1      manu 		syscallarg(linux32_sigcontextp_t) scp;
    373  1.16       dsl 	} */
    374   1.1      manu 	struct linux32_sigcontext ctx;
    375   1.1      manu 	int error;
    376   1.1      manu 
    377  1.10       dsl 	if ((error = copyin(SCARG_P32(uap, scp), &ctx, sizeof(ctx))) != 0)
    378   1.1      manu 		return error;
    379   1.1      manu 
    380   1.1      manu 	return linux32_restore_sigcontext(l, &ctx, retval);
    381   1.1      manu }
    382   1.1      manu 
    383   1.1      manu int
    384  1.23     rmind linux32_sys_rt_sigreturn(struct lwp *l,
    385  1.23     rmind     const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval)
    386   1.1      manu {
    387  1.16       dsl 	/* {
    388   1.1      manu 		syscallarg(linux32_ucontextp_t) ucp;
    389  1.16       dsl 	} */
    390   1.1      manu 	struct linux32_ucontext ctx;
    391   1.1      manu 	int error;
    392   1.1      manu 
    393  1.10       dsl 	if ((error = copyin(SCARG_P32(uap, ucp), &ctx, sizeof(ctx))) != 0)
    394   1.1      manu 		return error;
    395   1.1      manu 
    396   1.1      manu 	return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval);
    397   1.1      manu }
    398   1.1      manu 
    399   1.1      manu static int
    400  1.21    cegger linux32_restore_sigcontext(struct lwp *l, struct linux32_sigcontext *scp,
    401  1.21    cegger 			register_t *retval)
    402   1.1      manu {
    403   1.1      manu 	struct trapframe *tf;
    404   1.1      manu 	struct proc *p = l->l_proc;
    405   1.5        ad 	struct sigaltstack *sas = &l->l_sigstk;
    406  1.24       chs 	struct pcb *pcb;
    407   1.1      manu 	sigset_t mask;
    408   1.1      manu 	ssize_t ss_gap;
    409  1.24       chs 	register_t fssel, gssel;
    410   1.1      manu 
    411   1.1      manu 	/* Restore register context. */
    412   1.1      manu 	tf = l->l_md.md_regs;
    413  1.24       chs 	pcb = lwp_getpcb(l);
    414  1.24       chs 	DPRINTF(("sigreturn enter rsp=0x%lx rip=0x%lx\n", tf->tf_rsp,
    415  1.24       chs 		 tf->tf_rip));
    416   1.1      manu 
    417   1.1      manu 	/*
    418  1.24       chs 	 * Check for security violations.
    419   1.1      manu 	 */
    420  1.39      maxv 	if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0)
    421  1.39      maxv 		return EINVAL;
    422  1.39      maxv 	if (!VALID_USER_CSEL32(scp->sc_cs))
    423   1.1      manu 		return EINVAL;
    424   1.1      manu 
    425  1.24       chs 	if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs) &&
    426  1.28       chs 	    !(VALID_USER_FSEL32(scp->sc_fs) && pcb->pcb_fs != 0))
    427   1.1      manu 		return EINVAL;
    428   1.1      manu 
    429  1.24       chs 	if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs) &&
    430  1.28       chs 	    !(VALID_USER_GSEL32(scp->sc_gs) && pcb->pcb_gs != 0))
    431   1.1      manu 		return EINVAL;
    432   1.1      manu 
    433   1.1      manu 	if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es))
    434   1.1      manu 		return EINVAL;
    435   1.1      manu 
    436  1.24       chs 	if (!VALID_USER_DSEL32(scp->sc_ds) ||
    437   1.1      manu 	    !VALID_USER_DSEL32(scp->sc_ss))
    438   1.1      manu 		return EINVAL;
    439   1.1      manu 
    440   1.1      manu 	if (scp->sc_eip >= VM_MAXUSER_ADDRESS32)
    441   1.1      manu 		return EINVAL;
    442   1.1      manu 
    443  1.24       chs 	gssel = (register_t)scp->sc_gs & 0xffff;
    444  1.24       chs 	fssel = (register_t)scp->sc_fs & 0xffff;
    445  1.24       chs 	cpu_fsgs_reload(l, fssel, gssel);
    446  1.24       chs 	tf->tf_es = (register_t)scp->sc_es & 0xffff;
    447  1.24       chs 	tf->tf_ds = (register_t)scp->sc_ds & 0xffff;
    448   1.1      manu 	tf->tf_rflags &= ~PSL_USER;
    449   1.1      manu 	tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER);
    450   1.1      manu 	tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff;
    451   1.1      manu 	tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff;
    452   1.1      manu 	tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff;
    453   1.1      manu 	tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff;
    454   1.1      manu 	tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff;
    455   1.1      manu 	tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff;
    456   1.1      manu 	tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff;
    457   1.1      manu 	tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff;
    458  1.24       chs 	tf->tf_cs = (register_t)scp->sc_cs & 0xffff;
    459   1.1      manu 	tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff;
    460  1.24       chs 	tf->tf_ss = (register_t)scp->sc_ss & 0xffff;
    461   1.1      manu 
    462  1.17        ad 	mutex_enter(p->p_lock);
    463   1.5        ad 
    464   1.1      manu 	/* Restore signal stack. */
    465   1.1      manu 	ss_gap = (ssize_t)
    466   1.9       dsl 	    ((char *)NETBSD32IPTR64(scp->sc_esp_at_signal)
    467   1.8  christos 	     - (char *)sas->ss_sp);
    468   1.1      manu 	if (ss_gap >= 0 && ss_gap < sas->ss_size)
    469   1.1      manu 		sas->ss_flags |= SS_ONSTACK;
    470   1.1      manu 	else
    471   1.1      manu 		sas->ss_flags &= ~SS_ONSTACK;
    472   1.1      manu 
    473   1.1      manu 	/* Restore signal mask. */
    474   1.1      manu 	linux32_old_to_native_sigset(&mask, &scp->sc_mask);
    475   1.5        ad 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
    476   1.5        ad 
    477  1.17        ad 	mutex_exit(p->p_lock);
    478   1.5        ad 
    479  1.24       chs 	DPRINTF(("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n",
    480  1.24       chs 	    tf->tf_rip, tf->tf_rsp, tf->tf_rflags));
    481   1.1      manu 	return EJUSTRETURN;
    482   1.1      manu }
    483  1.24       chs 
    484  1.24       chs int
    485  1.24       chs linux32_sys_set_thread_area(struct lwp *l,
    486  1.24       chs     const struct linux32_sys_set_thread_area_args *uap, register_t *retval)
    487  1.24       chs {
    488  1.24       chs 	/* {
    489  1.24       chs 		syscallarg(linux32_user_descp_t) desc;
    490  1.24       chs 	} */
    491  1.24       chs 
    492  1.24       chs 	return linux_lwp_setprivate(l, SCARG_P32(uap, desc));
    493  1.24       chs }
    494  1.24       chs 
    495  1.24       chs int
    496  1.24       chs linux32_sys_get_thread_area(struct lwp *l,
    497  1.24       chs     const struct linux32_sys_get_thread_area_args *uap, register_t *retval)
    498  1.24       chs {
    499  1.24       chs 	/* {
    500  1.24       chs 		syscallarg(linux32_user_descp_t) desc;
    501  1.24       chs 	} */
    502  1.24       chs 
    503  1.24       chs 	/* glibc doesn't actually call this. */
    504  1.24       chs 	return ENOSYS;
    505  1.24       chs }
    506  1.27       chs 
    507  1.27       chs int
    508  1.27       chs linux32_sys_modify_ldt(struct lwp *l, const struct linux32_sys_modify_ldt_args *uap, register_t *retval)
    509  1.27       chs {
    510  1.27       chs 	/* {
    511  1.27       chs 		syscallarg(int) func;
    512  1.27       chs 		syscallarg(netbsd32_charp) ptr;
    513  1.27       chs 		syscallarg(netbsd32_size_t) bytecount;
    514  1.27       chs 	} */
    515  1.27       chs 	struct linux_sys_modify_ldt_args ua;
    516  1.27       chs 
    517  1.27       chs 	NETBSD32TO64_UAP(func);
    518  1.27       chs 	NETBSD32TOP_UAP(ptr, void *);
    519  1.27       chs 	NETBSD32TOX_UAP(bytecount, size_t);
    520  1.27       chs 	return linux_sys_modify_ldt(l, &ua, retval);
    521  1.27       chs }
    522