Home | History | Annotate | Line # | Download | only in i386
linux_machdep.c revision 1.6
      1  1.6   mycroft /*	$NetBSD: linux_machdep.c,v 1.6 1995/05/06 18:17:15 mycroft 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.3   mycroft 	tf = 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.4   mycroft #ifdef VM86
    118  1.4   mycroft 	if (tf->tf_eflags & PSL_VM) {
    119  1.4   mycroft 		frame.ls_sc.lsc_gs = tf->tf_vm86_gs;
    120  1.4   mycroft 		frame.ls_sc.lsc_fs = tf->tf_vm86_fs;
    121  1.4   mycroft 		frame.ls_sc.lsc_es = tf->tf_vm86_es;
    122  1.4   mycroft 		frame.ls_sc.lsc_ds = tf->tf_vm86_ds;
    123  1.4   mycroft 	} else
    124  1.4   mycroft #else
    125  1.4   mycroft 	{
    126  1.5   mycroft 		__asm("movl %%gs,%w0" : "=r" (frame.ls_sc.lsc_gs));
    127  1.5   mycroft 		__asm("movl %%fs,%w0" : "=r" (frame.ls_sc.lsc_fs));
    128  1.4   mycroft 		frame.ls_sc.lsc_es = tf->tf_es;
    129  1.4   mycroft 		frame.ls_sc.lsc_ds = tf->tf_ds;
    130  1.4   mycroft 	}
    131  1.4   mycroft #endif
    132  1.1      fvdl 	frame.ls_sc.lsc_edi    = tf->tf_edi;
    133  1.1      fvdl 	frame.ls_sc.lsc_esi    = tf->tf_esi;
    134  1.1      fvdl 	frame.ls_sc.lsc_ebp    = tf->tf_ebp;
    135  1.1      fvdl 	frame.ls_sc.lsc_ebx    = tf->tf_ebx;
    136  1.1      fvdl 	frame.ls_sc.lsc_edx    = tf->tf_edx;
    137  1.1      fvdl 	frame.ls_sc.lsc_ecx    = tf->tf_ecx;
    138  1.1      fvdl 	frame.ls_sc.lsc_eax    = tf->tf_eax;
    139  1.1      fvdl 	frame.ls_sc.lsc_eip    = tf->tf_eip;
    140  1.1      fvdl 	frame.ls_sc.lsc_cs     = tf->tf_cs;
    141  1.1      fvdl 	frame.ls_sc.lsc_eflags = tf->tf_eflags;
    142  1.1      fvdl 	frame.ls_sc.lsc_esp    = tf->tf_esp;
    143  1.1      fvdl 	frame.ls_sc.lsc_ss     = tf->tf_ss;
    144  1.1      fvdl 	frame.ls_sc.lsc_err    = tf->tf_err;
    145  1.1      fvdl 	frame.ls_sc.lsc_trapno = tf->tf_trapno;
    146  1.1      fvdl 
    147  1.1      fvdl 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
    148  1.1      fvdl 		/*
    149  1.1      fvdl 		 * Process has trashed its stack; give it an illegal
    150  1.1      fvdl 		 * instruction to halt it in its tracks.
    151  1.1      fvdl 		 */
    152  1.1      fvdl 		sigexit(p, SIGILL);
    153  1.1      fvdl 		/* NOTREACHED */
    154  1.1      fvdl 	}
    155  1.1      fvdl 
    156  1.1      fvdl 	/*
    157  1.1      fvdl 	 * Build context to run handler in.
    158  1.1      fvdl 	 */
    159  1.1      fvdl 	tf->tf_esp = (int)fp;
    160  1.1      fvdl 	tf->tf_eip = (int)(((char *)PS_STRINGS) -
    161  1.1      fvdl 	     (linux_esigcode - linux_sigcode));
    162  1.3   mycroft #ifdef VM86
    163  1.1      fvdl 	tf->tf_eflags &= ~PSL_VM;
    164  1.3   mycroft #endif
    165  1.2  christos 	tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
    166  1.2  christos 	tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
    167  1.2  christos 	tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
    168  1.2  christos 	tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
    169  1.1      fvdl }
    170  1.1      fvdl 
    171  1.1      fvdl /*
    172  1.1      fvdl  * System call to cleanup state after a signal
    173  1.1      fvdl  * has been taken.  Reset signal mask and
    174  1.1      fvdl  * stack state from context left by sendsig (above).
    175  1.1      fvdl  * Return to previous pc and psl as specified by
    176  1.1      fvdl  * context left by sendsig. Check carefully to
    177  1.1      fvdl  * make sure that the user has not modified the
    178  1.1      fvdl  * psl to gain improper privileges or to cause
    179  1.1      fvdl  * a machine fault.
    180  1.1      fvdl  */
    181  1.1      fvdl int
    182  1.1      fvdl linux_sigreturn(p, uap, retval)
    183  1.1      fvdl 	struct proc *p;
    184  1.1      fvdl 	struct linux_sigreturn_args /* {
    185  1.1      fvdl 		syscallarg(struct linux_sigcontext *) scp;
    186  1.1      fvdl 	} */ *uap;
    187  1.1      fvdl 	register_t *retval;
    188  1.1      fvdl {
    189  1.1      fvdl 	struct linux_sigcontext *scp, context;
    190  1.1      fvdl 	register struct trapframe *tf;
    191  1.1      fvdl 
    192  1.3   mycroft 	tf = p->p_md.md_regs;
    193  1.1      fvdl 
    194  1.1      fvdl 	/*
    195  1.1      fvdl 	 * The trampoline code hands us the context.
    196  1.1      fvdl 	 * It is unsafe to keep track of it ourselves, in the event that a
    197  1.1      fvdl 	 * program jumps out of a signal handler.
    198  1.1      fvdl 	 */
    199  1.1      fvdl 	scp = SCARG(uap, scp);
    200  1.1      fvdl 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
    201  1.1      fvdl 		return (EFAULT);
    202  1.1      fvdl 
    203  1.1      fvdl 	/*
    204  1.1      fvdl 	 * Check for security violations.
    205  1.1      fvdl 	 */
    206  1.1      fvdl 	if (((context.lsc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
    207  1.1      fvdl 	    ISPL(context.lsc_cs) != SEL_UPL)
    208  1.1      fvdl 		return (EINVAL);
    209  1.1      fvdl 
    210  1.1      fvdl 	p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
    211  1.1      fvdl 	p->p_sigmask = context.lsc_mask & ~sigcantmask;
    212  1.1      fvdl 
    213  1.1      fvdl 	/*
    214  1.1      fvdl 	 * Restore signal context.
    215  1.1      fvdl 	 */
    216  1.4   mycroft #ifdef VM86
    217  1.4   mycroft 	if (context.lsc_eflags & PSL_VM) {
    218  1.4   mycroft 		tf->tf_vm86_gs = context.lsc_gs;
    219  1.4   mycroft 		tf->tf_vm86_fs = context.lsc_fs;
    220  1.4   mycroft 		tf->tf_vm86_es = context.lsc_es;
    221  1.4   mycroft 		tf->tf_vm86_ds = context.lsc_ds;
    222  1.4   mycroft 	} else
    223  1.4   mycroft #endif
    224  1.4   mycroft 	{
    225  1.4   mycroft 		/* %fs and %gs were restored by the trampoline. */
    226  1.4   mycroft 		tf->tf_es = context.lsc_es;
    227  1.4   mycroft 		tf->tf_ds = context.lsc_ds;
    228  1.4   mycroft 	}
    229  1.1      fvdl 	tf->tf_edi    = context.lsc_edi;
    230  1.1      fvdl 	tf->tf_esi    = context.lsc_esi;
    231  1.1      fvdl 	tf->tf_ebp    = context.lsc_ebp;
    232  1.1      fvdl 	tf->tf_ebx    = context.lsc_ebx;
    233  1.1      fvdl 	tf->tf_edx    = context.lsc_edx;
    234  1.1      fvdl 	tf->tf_ecx    = context.lsc_ecx;
    235  1.1      fvdl 	tf->tf_eax    = context.lsc_eax;
    236  1.1      fvdl 	tf->tf_eip    = context.lsc_eip;
    237  1.1      fvdl 	tf->tf_cs     = context.lsc_cs;
    238  1.1      fvdl 	tf->tf_eflags = context.lsc_eflags;
    239  1.1      fvdl 	tf->tf_esp    = context.lsc_esp;
    240  1.1      fvdl 	tf->tf_ss     = context.lsc_ss;
    241  1.1      fvdl 
    242  1.1      fvdl 	return (EJUSTRETURN);
    243  1.6   mycroft }
    244  1.6   mycroft 
    245  1.6   mycroft int
    246  1.6   mycroft linux_modify_ldt(p, uap, retval)
    247  1.6   mycroft 	struct proc *p;
    248  1.6   mycroft 	struct linux_modify_ldt_args /* {
    249  1.6   mycroft 		syscallarg(int) func;
    250  1.6   mycroft 		syscallarg(void *) ptr;
    251  1.6   mycroft 		syscallarg(size_t) bytecount;
    252  1.6   mycroft 	} */ *uap;
    253  1.6   mycroft 	register_t *retval;
    254  1.6   mycroft {
    255  1.6   mycroft 
    256  1.6   mycroft 	switch (SCARG(uap, func)) {
    257  1.6   mycroft 	case 0:
    258  1.6   mycroft 		/* read */
    259  1.6   mycroft 	case 1:
    260  1.6   mycroft 		/* write */
    261  1.6   mycroft 	default:
    262  1.6   mycroft 		return (ENOSYS);
    263  1.6   mycroft 	}
    264  1.1      fvdl }
    265