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