Home | History | Annotate | Line # | Download | only in i386
linux_machdep.c revision 1.10
      1  1.10  christos /*	$NetBSD: linux_machdep.c,v 1.10 1995/06/24 20:36:28 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/buf.h>
     42   1.1      fvdl #include <sys/reboot.h>
     43   1.1      fvdl #include <sys/conf.h>
     44   1.1      fvdl #include <sys/file.h>
     45   1.1      fvdl #include <sys/callout.h>
     46   1.1      fvdl #include <sys/malloc.h>
     47   1.1      fvdl #include <sys/mbuf.h>
     48   1.1      fvdl #include <sys/msgbuf.h>
     49   1.1      fvdl #include <sys/mount.h>
     50   1.1      fvdl #include <sys/vnode.h>
     51   1.1      fvdl #include <sys/device.h>
     52   1.1      fvdl #include <sys/sysctl.h>
     53   1.1      fvdl #include <sys/syscallargs.h>
     54   1.7   mycroft 
     55   1.1      fvdl #include <compat/linux/linux_types.h>
     56   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     57   1.7   mycroft #include <compat/linux/linux_util.h>
     58   1.1      fvdl 
     59   1.1      fvdl #include <machine/cpu.h>
     60   1.1      fvdl #include <machine/cpufunc.h>
     61   1.1      fvdl #include <machine/psl.h>
     62   1.1      fvdl #include <machine/reg.h>
     63   1.7   mycroft #include <machine/segments.h>
     64   1.1      fvdl #include <machine/specialreg.h>
     65   1.7   mycroft #include <machine/sysarch.h>
     66   1.1      fvdl #include <machine/linux_machdep.h>
     67   1.1      fvdl 
     68   1.1      fvdl /*
     69   1.1      fvdl  * Deal with some i386-specific things in the Linux emulation code.
     70   1.1      fvdl  * This means just signals for now, will include stuff like
     71   1.1      fvdl  * I/O map permissions and V86 mode sometime.
     72   1.1      fvdl  */
     73   1.1      fvdl 
     74   1.1      fvdl /*
     75   1.1      fvdl  * Send an interrupt to process.
     76   1.1      fvdl  *
     77   1.1      fvdl  * Stack is set up to allow sigcode stored
     78   1.1      fvdl  * in u. to call routine, followed by kcall
     79   1.1      fvdl  * to sigreturn routine below.  After sigreturn
     80   1.1      fvdl  * resets the signal mask, the stack, and the
     81   1.1      fvdl  * frame pointer, it returns to the user
     82   1.1      fvdl  * specified pc, psl.
     83   1.1      fvdl  */
     84   1.1      fvdl 
     85   1.1      fvdl void
     86   1.1      fvdl linux_sendsig(catcher, sig, mask, code)
     87   1.1      fvdl 	sig_t catcher;
     88   1.1      fvdl 	int sig, mask;
     89   1.1      fvdl 	u_long code;
     90   1.1      fvdl {
     91   1.1      fvdl 	register struct proc *p = curproc;
     92   1.1      fvdl 	register struct trapframe *tf;
     93   1.1      fvdl 	struct linux_sigframe *fp, frame;
     94   1.1      fvdl 	struct sigacts *psp = p->p_sigacts;
     95   1.1      fvdl 	int oonstack;
     96   1.1      fvdl 	extern char linux_sigcode[], linux_esigcode[];
     97   1.1      fvdl 
     98   1.3   mycroft 	tf = p->p_md.md_regs;
     99   1.1      fvdl 	oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
    100   1.1      fvdl 
    101   1.1      fvdl 	/*
    102   1.1      fvdl 	 * Allocate space for the signal handler context.
    103   1.1      fvdl 	 */
    104   1.1      fvdl 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
    105   1.1      fvdl 	    (psp->ps_sigonstack & sigmask(sig))) {
    106   1.1      fvdl 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
    107   1.1      fvdl 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
    108   1.1      fvdl 		psp->ps_sigstk.ss_flags |= SA_ONSTACK;
    109   1.1      fvdl 	} else {
    110   1.1      fvdl 		fp = (struct linux_sigframe *)tf->tf_esp - 1;
    111   1.1      fvdl 	}
    112   1.1      fvdl 
    113   1.7   mycroft 	frame.sf_handler = catcher;
    114   1.7   mycroft 	frame.sf_sig = bsd_to_linux_sig(sig);
    115   1.1      fvdl 
    116   1.1      fvdl 	/*
    117   1.1      fvdl 	 * Build the signal context to be used by sigreturn.
    118   1.1      fvdl 	 */
    119   1.7   mycroft 	frame.sf_sc.sc_mask   = mask;
    120   1.4   mycroft #ifdef VM86
    121   1.4   mycroft 	if (tf->tf_eflags & PSL_VM) {
    122   1.7   mycroft 		frame.sf_sc.sc_gs = tf->tf_vm86_gs;
    123   1.7   mycroft 		frame.sf_sc.sc_fs = tf->tf_vm86_fs;
    124   1.7   mycroft 		frame.sf_sc.sc_es = tf->tf_vm86_es;
    125   1.7   mycroft 		frame.sf_sc.sc_ds = tf->tf_vm86_ds;
    126   1.4   mycroft 	} else
    127   1.4   mycroft #else
    128   1.4   mycroft 	{
    129   1.7   mycroft 		__asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
    130   1.7   mycroft 		__asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
    131   1.7   mycroft 		frame.sf_sc.sc_es = tf->tf_es;
    132   1.7   mycroft 		frame.sf_sc.sc_ds = tf->tf_ds;
    133   1.4   mycroft 	}
    134   1.4   mycroft #endif
    135   1.7   mycroft 	frame.sf_sc.sc_edi    = tf->tf_edi;
    136   1.7   mycroft 	frame.sf_sc.sc_esi    = tf->tf_esi;
    137   1.7   mycroft 	frame.sf_sc.sc_ebp    = tf->tf_ebp;
    138   1.7   mycroft 	frame.sf_sc.sc_ebx    = tf->tf_ebx;
    139   1.7   mycroft 	frame.sf_sc.sc_edx    = tf->tf_edx;
    140   1.7   mycroft 	frame.sf_sc.sc_ecx    = tf->tf_ecx;
    141   1.7   mycroft 	frame.sf_sc.sc_eax    = tf->tf_eax;
    142   1.7   mycroft 	frame.sf_sc.sc_eip    = tf->tf_eip;
    143   1.7   mycroft 	frame.sf_sc.sc_cs     = tf->tf_cs;
    144   1.7   mycroft 	frame.sf_sc.sc_eflags = tf->tf_eflags;
    145   1.7   mycroft 	frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
    146   1.7   mycroft 	frame.sf_sc.sc_ss     = tf->tf_ss;
    147   1.7   mycroft 	frame.sf_sc.sc_err    = tf->tf_err;
    148   1.7   mycroft 	frame.sf_sc.sc_trapno = tf->tf_trapno;
    149   1.1      fvdl 
    150   1.1      fvdl 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
    151   1.1      fvdl 		/*
    152   1.1      fvdl 		 * Process has trashed its stack; give it an illegal
    153   1.1      fvdl 		 * instruction to halt it in its tracks.
    154   1.1      fvdl 		 */
    155   1.1      fvdl 		sigexit(p, SIGILL);
    156   1.1      fvdl 		/* NOTREACHED */
    157   1.1      fvdl 	}
    158   1.1      fvdl 
    159   1.1      fvdl 	/*
    160   1.1      fvdl 	 * Build context to run handler in.
    161   1.1      fvdl 	 */
    162   1.1      fvdl 	tf->tf_esp = (int)fp;
    163   1.1      fvdl 	tf->tf_eip = (int)(((char *)PS_STRINGS) -
    164   1.1      fvdl 	     (linux_esigcode - linux_sigcode));
    165   1.3   mycroft #ifdef VM86
    166   1.1      fvdl 	tf->tf_eflags &= ~PSL_VM;
    167   1.3   mycroft #endif
    168   1.2  christos 	tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
    169   1.2  christos 	tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
    170   1.2  christos 	tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
    171   1.2  christos 	tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
    172   1.1      fvdl }
    173   1.1      fvdl 
    174   1.1      fvdl /*
    175   1.1      fvdl  * System call to cleanup state after a signal
    176   1.1      fvdl  * has been taken.  Reset signal mask and
    177   1.1      fvdl  * stack state from context left by sendsig (above).
    178   1.1      fvdl  * Return to previous pc and psl as specified by
    179   1.1      fvdl  * context left by sendsig. Check carefully to
    180   1.1      fvdl  * make sure that the user has not modified the
    181   1.1      fvdl  * psl to gain improper privileges or to cause
    182   1.1      fvdl  * a machine fault.
    183   1.1      fvdl  */
    184   1.1      fvdl int
    185   1.1      fvdl linux_sigreturn(p, uap, retval)
    186   1.1      fvdl 	struct proc *p;
    187   1.1      fvdl 	struct linux_sigreturn_args /* {
    188   1.1      fvdl 		syscallarg(struct linux_sigcontext *) scp;
    189   1.1      fvdl 	} */ *uap;
    190   1.1      fvdl 	register_t *retval;
    191   1.1      fvdl {
    192   1.1      fvdl 	struct linux_sigcontext *scp, context;
    193   1.1      fvdl 	register struct trapframe *tf;
    194   1.1      fvdl 
    195   1.3   mycroft 	tf = p->p_md.md_regs;
    196   1.1      fvdl 
    197   1.1      fvdl 	/*
    198   1.1      fvdl 	 * The trampoline code hands us the context.
    199   1.1      fvdl 	 * It is unsafe to keep track of it ourselves, in the event that a
    200   1.1      fvdl 	 * program jumps out of a signal handler.
    201   1.1      fvdl 	 */
    202   1.1      fvdl 	scp = SCARG(uap, scp);
    203   1.1      fvdl 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
    204   1.1      fvdl 		return (EFAULT);
    205   1.1      fvdl 
    206   1.1      fvdl 	/*
    207   1.1      fvdl 	 * Check for security violations.
    208   1.1      fvdl 	 */
    209   1.7   mycroft 	if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
    210   1.7   mycroft 	    ISPL(context.sc_cs) != SEL_UPL)
    211   1.1      fvdl 		return (EINVAL);
    212   1.1      fvdl 
    213   1.1      fvdl 	p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
    214   1.7   mycroft 	p->p_sigmask = context.sc_mask & ~sigcantmask;
    215   1.1      fvdl 
    216   1.1      fvdl 	/*
    217   1.1      fvdl 	 * Restore signal context.
    218   1.1      fvdl 	 */
    219   1.4   mycroft #ifdef VM86
    220   1.7   mycroft 	if (context.sc_eflags & PSL_VM) {
    221   1.7   mycroft 		tf->tf_vm86_gs = context.sc_gs;
    222   1.7   mycroft 		tf->tf_vm86_fs = context.sc_fs;
    223   1.7   mycroft 		tf->tf_vm86_es = context.sc_es;
    224   1.7   mycroft 		tf->tf_vm86_ds = context.sc_ds;
    225   1.4   mycroft 	} else
    226   1.4   mycroft #endif
    227   1.4   mycroft 	{
    228   1.4   mycroft 		/* %fs and %gs were restored by the trampoline. */
    229   1.7   mycroft 		tf->tf_es = context.sc_es;
    230   1.7   mycroft 		tf->tf_ds = context.sc_ds;
    231   1.4   mycroft 	}
    232   1.7   mycroft 	tf->tf_edi    = context.sc_edi;
    233   1.7   mycroft 	tf->tf_esi    = context.sc_esi;
    234   1.7   mycroft 	tf->tf_ebp    = context.sc_ebp;
    235   1.7   mycroft 	tf->tf_ebx    = context.sc_ebx;
    236   1.7   mycroft 	tf->tf_edx    = context.sc_edx;
    237   1.7   mycroft 	tf->tf_ecx    = context.sc_ecx;
    238   1.7   mycroft 	tf->tf_eax    = context.sc_eax;
    239   1.7   mycroft 	tf->tf_eip    = context.sc_eip;
    240   1.7   mycroft 	tf->tf_cs     = context.sc_cs;
    241   1.7   mycroft 	tf->tf_eflags = context.sc_eflags;
    242   1.7   mycroft 	tf->tf_esp    = context.sc_esp_at_signal;
    243   1.7   mycroft 	tf->tf_ss     = context.sc_ss;
    244   1.1      fvdl 
    245   1.1      fvdl 	return (EJUSTRETURN);
    246   1.6   mycroft }
    247   1.6   mycroft 
    248   1.7   mycroft #ifdef USER_LDT
    249   1.7   mycroft 
    250   1.7   mycroft int
    251   1.7   mycroft linux_read_ldt(p, uap, retval)
    252   1.7   mycroft 	struct proc *p;
    253   1.7   mycroft 	struct linux_modify_ldt_args /* {
    254   1.7   mycroft 		syscallarg(int) func;
    255   1.7   mycroft 		syscallarg(void *) ptr;
    256   1.7   mycroft 		syscallarg(size_t) bytecount;
    257   1.7   mycroft 	} */ *uap;
    258   1.7   mycroft 	register_t *retval;
    259   1.7   mycroft {
    260   1.7   mycroft 	struct i386_get_ldt_args gl;
    261   1.7   mycroft 	int error;
    262   1.7   mycroft 	caddr_t sg;
    263   1.7   mycroft 	char *parms;
    264   1.7   mycroft 
    265  1.10  christos 	sg = stackgap_init(p->p_emul);
    266   1.7   mycroft 
    267   1.7   mycroft 	gl.start = 0;
    268   1.7   mycroft 	gl.desc = SCARG(uap, ptr);
    269   1.7   mycroft 	gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
    270   1.7   mycroft 
    271   1.7   mycroft 	parms = stackgap_alloc(&sg, sizeof(gl));
    272   1.7   mycroft 
    273   1.7   mycroft 	if (error = copyout(&gl, parms, sizeof(gl)))
    274   1.7   mycroft 		return (error);
    275   1.7   mycroft 
    276   1.7   mycroft 	if (error = i386_get_ldt(p, parms, retval))
    277   1.7   mycroft 		return (error);
    278   1.7   mycroft 
    279   1.7   mycroft 	*retval *= sizeof(union descriptor);
    280   1.7   mycroft 	return (0);
    281   1.7   mycroft }
    282   1.7   mycroft 
    283   1.7   mycroft struct linux_ldt_info {
    284   1.7   mycroft 	u_int entry_number;
    285   1.7   mycroft 	u_long base_addr;
    286   1.7   mycroft 	u_int limit;
    287   1.7   mycroft 	u_int seg_32bit:1;
    288   1.7   mycroft 	u_int contents:2;
    289   1.7   mycroft 	u_int read_exec_only:1;
    290   1.7   mycroft 	u_int limit_in_pages:1;
    291   1.7   mycroft 	u_int seg_not_present:1;
    292   1.7   mycroft };
    293   1.7   mycroft 
    294   1.7   mycroft int
    295   1.7   mycroft linux_write_ldt(p, uap, retval)
    296   1.7   mycroft 	struct proc *p;
    297   1.7   mycroft 	struct linux_modify_ldt_args /* {
    298   1.7   mycroft 		syscallarg(int) func;
    299   1.7   mycroft 		syscallarg(void *) ptr;
    300   1.7   mycroft 		syscallarg(size_t) bytecount;
    301   1.7   mycroft 	} */ *uap;
    302   1.7   mycroft 	register_t *retval;
    303   1.7   mycroft {
    304   1.7   mycroft 	struct linux_ldt_info ldt_info;
    305   1.7   mycroft 	struct segment_descriptor sd;
    306   1.7   mycroft 	struct i386_set_ldt_args sl;
    307   1.7   mycroft 	int error;
    308   1.7   mycroft 	caddr_t sg;
    309   1.7   mycroft 	char *parms;
    310   1.7   mycroft 
    311   1.7   mycroft 	if (SCARG(uap, bytecount) != sizeof(ldt_info))
    312   1.7   mycroft 		return (EINVAL);
    313   1.7   mycroft 	if (error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info)))
    314   1.7   mycroft 		return error;
    315   1.7   mycroft 	if (ldt_info.contents == 3)
    316   1.7   mycroft 		return (EINVAL);
    317   1.7   mycroft 
    318  1.10  christos 	sg = stackgap_init(p->p_emul);
    319   1.7   mycroft 
    320   1.7   mycroft 	sd.sd_lobase = ldt_info.base_addr & 0xffffff;
    321   1.7   mycroft 	sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
    322   1.7   mycroft 	sd.sd_lolimit = ldt_info.limit & 0xffff;
    323   1.7   mycroft 	sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
    324   1.7   mycroft 	sd.sd_type =
    325   1.7   mycroft 	    16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
    326   1.7   mycroft 	sd.sd_dpl = SEL_UPL;
    327   1.7   mycroft 	sd.sd_p = !ldt_info.seg_not_present;
    328   1.7   mycroft 	sd.sd_def32 = ldt_info.seg_32bit;
    329   1.7   mycroft 	sd.sd_gran = ldt_info.limit_in_pages;
    330   1.7   mycroft 
    331   1.7   mycroft 	sl.start = ldt_info.entry_number;
    332   1.7   mycroft 	sl.desc = stackgap_alloc(&sg, sizeof(sd));
    333   1.7   mycroft 	sl.num = 1;
    334   1.7   mycroft 
    335   1.8   mycroft #if 0
    336   1.7   mycroft 	printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
    337   1.7   mycroft 	    ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
    338   1.8   mycroft #endif
    339   1.7   mycroft 
    340   1.7   mycroft 	parms = stackgap_alloc(&sg, sizeof(sl));
    341   1.7   mycroft 
    342   1.7   mycroft 	if (error = copyout(&sd, sl.desc, sizeof(sd)))
    343   1.7   mycroft 		return (error);
    344   1.7   mycroft 	if (error = copyout(&sl, parms, sizeof(sl)))
    345   1.7   mycroft 		return (error);
    346   1.7   mycroft 
    347   1.7   mycroft 	if (error = i386_set_ldt(p, parms, retval))
    348   1.7   mycroft 		return (error);
    349   1.7   mycroft 
    350   1.7   mycroft 	*retval = 0;
    351   1.7   mycroft 	return (0);
    352   1.7   mycroft }
    353   1.7   mycroft 
    354   1.7   mycroft #endif /* USER_LDT */
    355   1.7   mycroft 
    356   1.6   mycroft int
    357   1.6   mycroft linux_modify_ldt(p, uap, retval)
    358   1.6   mycroft 	struct proc *p;
    359   1.6   mycroft 	struct linux_modify_ldt_args /* {
    360   1.6   mycroft 		syscallarg(int) func;
    361   1.6   mycroft 		syscallarg(void *) ptr;
    362   1.6   mycroft 		syscallarg(size_t) bytecount;
    363   1.6   mycroft 	} */ *uap;
    364   1.6   mycroft 	register_t *retval;
    365   1.6   mycroft {
    366   1.6   mycroft 
    367   1.6   mycroft 	switch (SCARG(uap, func)) {
    368   1.7   mycroft #ifdef USER_LDT
    369   1.6   mycroft 	case 0:
    370   1.7   mycroft 		return (linux_read_ldt(p, uap, retval));
    371   1.7   mycroft 
    372   1.6   mycroft 	case 1:
    373   1.7   mycroft 		return (linux_write_ldt(p, uap, retval));
    374   1.7   mycroft #endif /* USER_LDT */
    375   1.7   mycroft 
    376   1.6   mycroft 	default:
    377   1.6   mycroft 		return (ENOSYS);
    378   1.6   mycroft 	}
    379   1.1      fvdl }
    380