Home | History | Annotate | Line # | Download | only in i386
linux_machdep.c revision 1.1
      1 /*	$NetBSD: linux_machdep.c,v 1.1 1995/04/07 22:29:34 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Frank van der Linden
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project
     18  *      by Frank van der Linden
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/signalvar.h>
     37 #include <sys/kernel.h>
     38 #include <sys/map.h>
     39 #include <sys/proc.h>
     40 #include <sys/user.h>
     41 #include <sys/exec.h>
     42 #include <sys/buf.h>
     43 #include <sys/reboot.h>
     44 #include <sys/conf.h>
     45 #include <sys/file.h>
     46 #include <sys/callout.h>
     47 #include <sys/malloc.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/msgbuf.h>
     50 #include <sys/mount.h>
     51 #include <sys/vnode.h>
     52 #include <sys/device.h>
     53 #include <sys/sysctl.h>
     54 #include <sys/syscallargs.h>
     55 #include <compat/linux/linux_types.h>
     56 #include <compat/linux/linux_syscallargs.h>
     57 
     58 #include <machine/cpu.h>
     59 #include <machine/cpufunc.h>
     60 #include <machine/psl.h>
     61 #include <machine/reg.h>
     62 #include <machine/specialreg.h>
     63 #include <machine/linux_machdep.h>
     64 
     65 /*
     66  * Deal with some i386-specific things in the Linux emulation code.
     67  * This means just signals for now, will include stuff like
     68  * I/O map permissions and V86 mode sometime.
     69  */
     70 
     71 extern int _udatasel, _ucodesel;
     72 
     73 /*
     74  * Send an interrupt to process.
     75  *
     76  * Stack is set up to allow sigcode stored
     77  * in u. to call routine, followed by kcall
     78  * to sigreturn routine below.  After sigreturn
     79  * resets the signal mask, the stack, and the
     80  * frame pointer, it returns to the user
     81  * specified pc, psl.
     82  */
     83 
     84 void
     85 linux_sendsig(catcher, sig, mask, code)
     86 	sig_t catcher;
     87 	int sig, mask;
     88 	u_long code;
     89 {
     90 	register struct proc *p = curproc;
     91 	register struct trapframe *tf;
     92 	struct linux_sigframe *fp, frame;
     93 	struct sigacts *psp = p->p_sigacts;
     94 	int oonstack;
     95 	extern char linux_sigcode[], linux_esigcode[];
     96 
     97 	tf = (struct trapframe *)p->p_md.md_regs;
     98 	oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
     99 
    100 	/*
    101 	 * Allocate space for the signal handler context.
    102 	 */
    103 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
    104 	    (psp->ps_sigonstack & sigmask(sig))) {
    105 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
    106 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
    107 		psp->ps_sigstk.ss_flags |= SA_ONSTACK;
    108 	} else {
    109 		fp = (struct linux_sigframe *)tf->tf_esp - 1;
    110 	}
    111 
    112 	frame.ls_handler = catcher;
    113 	frame.ls_sig = bsd_to_linux_sig(sig);
    114 
    115 	/*
    116 	 * Build the signal context to be used by sigreturn.
    117 	 */
    118 	frame.ls_sc.lsc_mask   = mask;
    119 	frame.ls_sc.lsc_es     = tf->tf_es;
    120 	frame.ls_sc.lsc_fs     = _udatasel;
    121 	frame.ls_sc.lsc_gs     = _udatasel;
    122 	frame.ls_sc.lsc_ds     = tf->tf_ds;
    123 	frame.ls_sc.lsc_edi    = tf->tf_edi;
    124 	frame.ls_sc.lsc_esi    = tf->tf_esi;
    125 	frame.ls_sc.lsc_ebp    = tf->tf_ebp;
    126 	frame.ls_sc.lsc_ebx    = tf->tf_ebx;
    127 	frame.ls_sc.lsc_edx    = tf->tf_edx;
    128 	frame.ls_sc.lsc_ecx    = tf->tf_ecx;
    129 	frame.ls_sc.lsc_eax    = tf->tf_eax;
    130 	frame.ls_sc.lsc_eip    = tf->tf_eip;
    131 	frame.ls_sc.lsc_cs     = tf->tf_cs;
    132 	frame.ls_sc.lsc_eflags = tf->tf_eflags;
    133 	frame.ls_sc.lsc_esp    = tf->tf_esp;
    134 	frame.ls_sc.lsc_ss     = tf->tf_ss;
    135 	frame.ls_sc.lsc_err    = tf->tf_err;
    136 	frame.ls_sc.lsc_trapno = tf->tf_trapno;
    137 
    138 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
    139 		/*
    140 		 * Process has trashed its stack; give it an illegal
    141 		 * instruction to halt it in its tracks.
    142 		 */
    143 		sigexit(p, SIGILL);
    144 		/* NOTREACHED */
    145 	}
    146 
    147 	/*
    148 	 * Build context to run handler in.
    149 	 */
    150 	tf->tf_esp = (int)fp;
    151 	tf->tf_eip = (int)(((char *)PS_STRINGS) -
    152 	     (linux_esigcode - linux_sigcode));
    153 	tf->tf_eflags &= ~PSL_VM;
    154 	tf->tf_cs = _ucodesel;
    155 	tf->tf_ds = _udatasel;
    156 	tf->tf_es = _udatasel;
    157 	tf->tf_ss = _udatasel;
    158 }
    159 
    160 /*
    161  * System call to cleanup state after a signal
    162  * has been taken.  Reset signal mask and
    163  * stack state from context left by sendsig (above).
    164  * Return to previous pc and psl as specified by
    165  * context left by sendsig. Check carefully to
    166  * make sure that the user has not modified the
    167  * psl to gain improper privileges or to cause
    168  * a machine fault.
    169  */
    170 int
    171 linux_sigreturn(p, uap, retval)
    172 	struct proc *p;
    173 	struct linux_sigreturn_args /* {
    174 		syscallarg(struct linux_sigcontext *) scp;
    175 	} */ *uap;
    176 	register_t *retval;
    177 {
    178 	struct linux_sigcontext *scp, context;
    179 	register struct trapframe *tf;
    180 
    181 	tf = (struct trapframe *)p->p_md.md_regs;
    182 
    183 	/*
    184 	 * The trampoline code hands us the context.
    185 	 * It is unsafe to keep track of it ourselves, in the event that a
    186 	 * program jumps out of a signal handler.
    187 	 */
    188 	scp = SCARG(uap, scp);
    189 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
    190 		return (EFAULT);
    191 
    192 	/*
    193 	 * Check for security violations.
    194 	 */
    195 	if (((context.lsc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
    196 	    ISPL(context.lsc_cs) != SEL_UPL)
    197 		return (EINVAL);
    198 
    199 	p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
    200 	p->p_sigmask = context.lsc_mask & ~sigcantmask;
    201 
    202 	/*
    203 	 * Restore signal context.
    204 	 */
    205 	tf->tf_es     = context.lsc_es;
    206 	tf->tf_ds     = context.lsc_ds;
    207 	tf->tf_edi    = context.lsc_edi;
    208 	tf->tf_esi    = context.lsc_esi;
    209 	tf->tf_ebp    = context.lsc_ebp;
    210 	tf->tf_ebx    = context.lsc_ebx;
    211 	tf->tf_edx    = context.lsc_edx;
    212 	tf->tf_ecx    = context.lsc_ecx;
    213 	tf->tf_eax    = context.lsc_eax;
    214 	tf->tf_eip    = context.lsc_eip;
    215 	tf->tf_cs     = context.lsc_cs;
    216 	tf->tf_eflags = context.lsc_eflags;
    217 	tf->tf_esp    = context.lsc_esp;
    218 	tf->tf_ss     = context.lsc_ss;
    219 
    220 	return (EJUSTRETURN);
    221 }
    222