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