Home | History | Annotate | Line # | Download | only in i386
linux_machdep.c revision 1.1
      1  1.1  fvdl /*	$NetBSD: linux_machdep.c,v 1.1 1995/04/07 22:29:34 fvdl 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 extern int _udatasel, _ucodesel;
     72  1.1  fvdl 
     73  1.1  fvdl /*
     74  1.1  fvdl  * Send an interrupt to process.
     75  1.1  fvdl  *
     76  1.1  fvdl  * Stack is set up to allow sigcode stored
     77  1.1  fvdl  * in u. to call routine, followed by kcall
     78  1.1  fvdl  * to sigreturn routine below.  After sigreturn
     79  1.1  fvdl  * resets the signal mask, the stack, and the
     80  1.1  fvdl  * frame pointer, it returns to the user
     81  1.1  fvdl  * specified pc, psl.
     82  1.1  fvdl  */
     83  1.1  fvdl 
     84  1.1  fvdl void
     85  1.1  fvdl linux_sendsig(catcher, sig, mask, code)
     86  1.1  fvdl 	sig_t catcher;
     87  1.1  fvdl 	int sig, mask;
     88  1.1  fvdl 	u_long code;
     89  1.1  fvdl {
     90  1.1  fvdl 	register struct proc *p = curproc;
     91  1.1  fvdl 	register struct trapframe *tf;
     92  1.1  fvdl 	struct linux_sigframe *fp, frame;
     93  1.1  fvdl 	struct sigacts *psp = p->p_sigacts;
     94  1.1  fvdl 	int oonstack;
     95  1.1  fvdl 	extern char linux_sigcode[], linux_esigcode[];
     96  1.1  fvdl 
     97  1.1  fvdl 	tf = (struct trapframe *)p->p_md.md_regs;
     98  1.1  fvdl 	oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
     99  1.1  fvdl 
    100  1.1  fvdl 	/*
    101  1.1  fvdl 	 * Allocate space for the signal handler context.
    102  1.1  fvdl 	 */
    103  1.1  fvdl 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
    104  1.1  fvdl 	    (psp->ps_sigonstack & sigmask(sig))) {
    105  1.1  fvdl 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
    106  1.1  fvdl 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
    107  1.1  fvdl 		psp->ps_sigstk.ss_flags |= SA_ONSTACK;
    108  1.1  fvdl 	} else {
    109  1.1  fvdl 		fp = (struct linux_sigframe *)tf->tf_esp - 1;
    110  1.1  fvdl 	}
    111  1.1  fvdl 
    112  1.1  fvdl 	frame.ls_handler = catcher;
    113  1.1  fvdl 	frame.ls_sig = bsd_to_linux_sig(sig);
    114  1.1  fvdl 
    115  1.1  fvdl 	/*
    116  1.1  fvdl 	 * Build the signal context to be used by sigreturn.
    117  1.1  fvdl 	 */
    118  1.1  fvdl 	frame.ls_sc.lsc_mask   = mask;
    119  1.1  fvdl 	frame.ls_sc.lsc_es     = tf->tf_es;
    120  1.1  fvdl 	frame.ls_sc.lsc_fs     = _udatasel;
    121  1.1  fvdl 	frame.ls_sc.lsc_gs     = _udatasel;
    122  1.1  fvdl 	frame.ls_sc.lsc_ds     = tf->tf_ds;
    123  1.1  fvdl 	frame.ls_sc.lsc_edi    = tf->tf_edi;
    124  1.1  fvdl 	frame.ls_sc.lsc_esi    = tf->tf_esi;
    125  1.1  fvdl 	frame.ls_sc.lsc_ebp    = tf->tf_ebp;
    126  1.1  fvdl 	frame.ls_sc.lsc_ebx    = tf->tf_ebx;
    127  1.1  fvdl 	frame.ls_sc.lsc_edx    = tf->tf_edx;
    128  1.1  fvdl 	frame.ls_sc.lsc_ecx    = tf->tf_ecx;
    129  1.1  fvdl 	frame.ls_sc.lsc_eax    = tf->tf_eax;
    130  1.1  fvdl 	frame.ls_sc.lsc_eip    = tf->tf_eip;
    131  1.1  fvdl 	frame.ls_sc.lsc_cs     = tf->tf_cs;
    132  1.1  fvdl 	frame.ls_sc.lsc_eflags = tf->tf_eflags;
    133  1.1  fvdl 	frame.ls_sc.lsc_esp    = tf->tf_esp;
    134  1.1  fvdl 	frame.ls_sc.lsc_ss     = tf->tf_ss;
    135  1.1  fvdl 	frame.ls_sc.lsc_err    = tf->tf_err;
    136  1.1  fvdl 	frame.ls_sc.lsc_trapno = tf->tf_trapno;
    137  1.1  fvdl 
    138  1.1  fvdl 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
    139  1.1  fvdl 		/*
    140  1.1  fvdl 		 * Process has trashed its stack; give it an illegal
    141  1.1  fvdl 		 * instruction to halt it in its tracks.
    142  1.1  fvdl 		 */
    143  1.1  fvdl 		sigexit(p, SIGILL);
    144  1.1  fvdl 		/* NOTREACHED */
    145  1.1  fvdl 	}
    146  1.1  fvdl 
    147  1.1  fvdl 	/*
    148  1.1  fvdl 	 * Build context to run handler in.
    149  1.1  fvdl 	 */
    150  1.1  fvdl 	tf->tf_esp = (int)fp;
    151  1.1  fvdl 	tf->tf_eip = (int)(((char *)PS_STRINGS) -
    152  1.1  fvdl 	     (linux_esigcode - linux_sigcode));
    153  1.1  fvdl 	tf->tf_eflags &= ~PSL_VM;
    154  1.1  fvdl 	tf->tf_cs = _ucodesel;
    155  1.1  fvdl 	tf->tf_ds = _udatasel;
    156  1.1  fvdl 	tf->tf_es = _udatasel;
    157  1.1  fvdl 	tf->tf_ss = _udatasel;
    158  1.1  fvdl }
    159  1.1  fvdl 
    160  1.1  fvdl /*
    161  1.1  fvdl  * System call to cleanup state after a signal
    162  1.1  fvdl  * has been taken.  Reset signal mask and
    163  1.1  fvdl  * stack state from context left by sendsig (above).
    164  1.1  fvdl  * Return to previous pc and psl as specified by
    165  1.1  fvdl  * context left by sendsig. Check carefully to
    166  1.1  fvdl  * make sure that the user has not modified the
    167  1.1  fvdl  * psl to gain improper privileges or to cause
    168  1.1  fvdl  * a machine fault.
    169  1.1  fvdl  */
    170  1.1  fvdl int
    171  1.1  fvdl linux_sigreturn(p, uap, retval)
    172  1.1  fvdl 	struct proc *p;
    173  1.1  fvdl 	struct linux_sigreturn_args /* {
    174  1.1  fvdl 		syscallarg(struct linux_sigcontext *) scp;
    175  1.1  fvdl 	} */ *uap;
    176  1.1  fvdl 	register_t *retval;
    177  1.1  fvdl {
    178  1.1  fvdl 	struct linux_sigcontext *scp, context;
    179  1.1  fvdl 	register struct trapframe *tf;
    180  1.1  fvdl 
    181  1.1  fvdl 	tf = (struct trapframe *)p->p_md.md_regs;
    182  1.1  fvdl 
    183  1.1  fvdl 	/*
    184  1.1  fvdl 	 * The trampoline code hands us the context.
    185  1.1  fvdl 	 * It is unsafe to keep track of it ourselves, in the event that a
    186  1.1  fvdl 	 * program jumps out of a signal handler.
    187  1.1  fvdl 	 */
    188  1.1  fvdl 	scp = SCARG(uap, scp);
    189  1.1  fvdl 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
    190  1.1  fvdl 		return (EFAULT);
    191  1.1  fvdl 
    192  1.1  fvdl 	/*
    193  1.1  fvdl 	 * Check for security violations.
    194  1.1  fvdl 	 */
    195  1.1  fvdl 	if (((context.lsc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
    196  1.1  fvdl 	    ISPL(context.lsc_cs) != SEL_UPL)
    197  1.1  fvdl 		return (EINVAL);
    198  1.1  fvdl 
    199  1.1  fvdl 	p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
    200  1.1  fvdl 	p->p_sigmask = context.lsc_mask & ~sigcantmask;
    201  1.1  fvdl 
    202  1.1  fvdl 	/*
    203  1.1  fvdl 	 * Restore signal context.
    204  1.1  fvdl 	 */
    205  1.1  fvdl 	tf->tf_es     = context.lsc_es;
    206  1.1  fvdl 	tf->tf_ds     = context.lsc_ds;
    207  1.1  fvdl 	tf->tf_edi    = context.lsc_edi;
    208  1.1  fvdl 	tf->tf_esi    = context.lsc_esi;
    209  1.1  fvdl 	tf->tf_ebp    = context.lsc_ebp;
    210  1.1  fvdl 	tf->tf_ebx    = context.lsc_ebx;
    211  1.1  fvdl 	tf->tf_edx    = context.lsc_edx;
    212  1.1  fvdl 	tf->tf_ecx    = context.lsc_ecx;
    213  1.1  fvdl 	tf->tf_eax    = context.lsc_eax;
    214  1.1  fvdl 	tf->tf_eip    = context.lsc_eip;
    215  1.1  fvdl 	tf->tf_cs     = context.lsc_cs;
    216  1.1  fvdl 	tf->tf_eflags = context.lsc_eflags;
    217  1.1  fvdl 	tf->tf_esp    = context.lsc_esp;
    218  1.1  fvdl 	tf->tf_ss     = context.lsc_ss;
    219  1.1  fvdl 
    220  1.1  fvdl 	return (EJUSTRETURN);
    221  1.1  fvdl }
    222