Home | History | Annotate | Line # | Download | only in i386
linux_machdep.c revision 1.31
      1  1.31   thorpej /*	$NetBSD: linux_machdep.c,v 1.31 1996/10/12 02:20:37 thorpej 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.31   thorpej #include <sys/exec.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.13      fvdl #include <sys/filedesc.h>
     56   1.7   mycroft 
     57   1.1      fvdl #include <compat/linux/linux_types.h>
     58  1.11   mycroft #include <compat/linux/linux_signal.h>
     59   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     60   1.7   mycroft #include <compat/linux/linux_util.h>
     61  1.29  christos #include <compat/linux/linux_ioctl.h>
     62   1.1      fvdl 
     63   1.1      fvdl #include <machine/cpu.h>
     64   1.1      fvdl #include <machine/cpufunc.h>
     65   1.1      fvdl #include <machine/psl.h>
     66   1.1      fvdl #include <machine/reg.h>
     67   1.7   mycroft #include <machine/segments.h>
     68   1.1      fvdl #include <machine/specialreg.h>
     69   1.7   mycroft #include <machine/sysarch.h>
     70  1.26   mycroft #include <machine/vm86.h>
     71   1.1      fvdl #include <machine/linux_machdep.h>
     72   1.1      fvdl 
     73   1.1      fvdl /*
     74  1.13      fvdl  * To see whether pcvt is configured (for virtual console ioctl calls).
     75  1.13      fvdl  */
     76  1.13      fvdl #include "vt.h"
     77  1.13      fvdl #if NVT > 0
     78  1.13      fvdl #include <arch/i386/isa/pcvt/pcvt_ioctl.h>
     79  1.13      fvdl #endif
     80  1.13      fvdl 
     81  1.29  christos #ifdef USER_LDT
     82  1.29  christos #include <machine/cpu.h>
     83  1.29  christos int linux_read_ldt __P((struct proc *, struct linux_sys_modify_ldt_args *,
     84  1.29  christos     register_t *));
     85  1.29  christos int linux_write_ldt __P((struct proc *, struct linux_sys_modify_ldt_args *,
     86  1.29  christos     register_t *));
     87  1.29  christos #endif
     88  1.29  christos 
     89  1.13      fvdl /*
     90   1.1      fvdl  * Deal with some i386-specific things in the Linux emulation code.
     91   1.1      fvdl  * This means just signals for now, will include stuff like
     92   1.1      fvdl  * I/O map permissions and V86 mode sometime.
     93   1.1      fvdl  */
     94   1.1      fvdl 
     95   1.1      fvdl /*
     96   1.1      fvdl  * Send an interrupt to process.
     97   1.1      fvdl  *
     98   1.1      fvdl  * Stack is set up to allow sigcode stored
     99   1.1      fvdl  * in u. to call routine, followed by kcall
    100   1.1      fvdl  * to sigreturn routine below.  After sigreturn
    101   1.1      fvdl  * resets the signal mask, the stack, and the
    102   1.1      fvdl  * frame pointer, it returns to the user
    103   1.1      fvdl  * specified pc, psl.
    104   1.1      fvdl  */
    105   1.1      fvdl 
    106   1.1      fvdl void
    107   1.1      fvdl linux_sendsig(catcher, sig, mask, code)
    108   1.1      fvdl 	sig_t catcher;
    109   1.1      fvdl 	int sig, mask;
    110   1.1      fvdl 	u_long code;
    111   1.1      fvdl {
    112   1.1      fvdl 	register struct proc *p = curproc;
    113   1.1      fvdl 	register struct trapframe *tf;
    114   1.1      fvdl 	struct linux_sigframe *fp, frame;
    115   1.1      fvdl 	struct sigacts *psp = p->p_sigacts;
    116   1.1      fvdl 	int oonstack;
    117   1.1      fvdl 	extern char linux_sigcode[], linux_esigcode[];
    118   1.1      fvdl 
    119   1.3   mycroft 	tf = p->p_md.md_regs;
    120  1.16   mycroft 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
    121   1.1      fvdl 
    122   1.1      fvdl 	/*
    123   1.1      fvdl 	 * Allocate space for the signal handler context.
    124   1.1      fvdl 	 */
    125   1.1      fvdl 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
    126   1.1      fvdl 	    (psp->ps_sigonstack & sigmask(sig))) {
    127  1.24       jtc 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp +
    128   1.1      fvdl 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
    129  1.16   mycroft 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
    130   1.1      fvdl 	} else {
    131   1.1      fvdl 		fp = (struct linux_sigframe *)tf->tf_esp - 1;
    132   1.1      fvdl 	}
    133   1.1      fvdl 
    134   1.7   mycroft 	frame.sf_handler = catcher;
    135  1.12   mycroft 	frame.sf_sig = bsd_to_linux_sig[sig];
    136   1.1      fvdl 
    137   1.1      fvdl 	/*
    138   1.1      fvdl 	 * Build the signal context to be used by sigreturn.
    139   1.1      fvdl 	 */
    140   1.7   mycroft 	frame.sf_sc.sc_mask   = mask;
    141   1.4   mycroft #ifdef VM86
    142   1.4   mycroft 	if (tf->tf_eflags & PSL_VM) {
    143   1.7   mycroft 		frame.sf_sc.sc_gs = tf->tf_vm86_gs;
    144   1.7   mycroft 		frame.sf_sc.sc_fs = tf->tf_vm86_fs;
    145   1.7   mycroft 		frame.sf_sc.sc_es = tf->tf_vm86_es;
    146   1.7   mycroft 		frame.sf_sc.sc_ds = tf->tf_vm86_ds;
    147  1.26   mycroft 		frame.sf_sc.sc_eflags = get_vflags(p);
    148   1.4   mycroft 	} else
    149  1.18      fvdl #endif
    150   1.4   mycroft 	{
    151   1.7   mycroft 		__asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
    152   1.7   mycroft 		__asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
    153   1.7   mycroft 		frame.sf_sc.sc_es = tf->tf_es;
    154   1.7   mycroft 		frame.sf_sc.sc_ds = tf->tf_ds;
    155  1.26   mycroft 		frame.sf_sc.sc_eflags = tf->tf_eflags;
    156   1.4   mycroft 	}
    157  1.26   mycroft 	frame.sf_sc.sc_edi = tf->tf_edi;
    158  1.26   mycroft 	frame.sf_sc.sc_esi = tf->tf_esi;
    159  1.26   mycroft 	frame.sf_sc.sc_ebp = tf->tf_ebp;
    160  1.26   mycroft 	frame.sf_sc.sc_ebx = tf->tf_ebx;
    161  1.26   mycroft 	frame.sf_sc.sc_edx = tf->tf_edx;
    162  1.26   mycroft 	frame.sf_sc.sc_ecx = tf->tf_ecx;
    163  1.26   mycroft 	frame.sf_sc.sc_eax = tf->tf_eax;
    164  1.26   mycroft 	frame.sf_sc.sc_eip = tf->tf_eip;
    165  1.26   mycroft 	frame.sf_sc.sc_cs = tf->tf_cs;
    166   1.7   mycroft 	frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
    167  1.26   mycroft 	frame.sf_sc.sc_ss = tf->tf_ss;
    168  1.26   mycroft 	frame.sf_sc.sc_err = tf->tf_err;
    169   1.7   mycroft 	frame.sf_sc.sc_trapno = tf->tf_trapno;
    170   1.1      fvdl 
    171   1.1      fvdl 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
    172   1.1      fvdl 		/*
    173   1.1      fvdl 		 * Process has trashed its stack; give it an illegal
    174   1.1      fvdl 		 * instruction to halt it in its tracks.
    175   1.1      fvdl 		 */
    176   1.1      fvdl 		sigexit(p, SIGILL);
    177   1.1      fvdl 		/* NOTREACHED */
    178   1.1      fvdl 	}
    179   1.1      fvdl 
    180   1.1      fvdl 	/*
    181   1.1      fvdl 	 * Build context to run handler in.
    182   1.1      fvdl 	 */
    183  1.26   mycroft 	tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
    184  1.26   mycroft 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
    185   1.1      fvdl 	tf->tf_eip = (int)(((char *)PS_STRINGS) -
    186   1.1      fvdl 	     (linux_esigcode - linux_sigcode));
    187  1.23   mycroft 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
    188  1.28   mycroft 	tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
    189  1.26   mycroft 	tf->tf_esp = (int)fp;
    190  1.23   mycroft 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
    191   1.1      fvdl }
    192   1.1      fvdl 
    193   1.1      fvdl /*
    194   1.1      fvdl  * System call to cleanup state after a signal
    195   1.1      fvdl  * has been taken.  Reset signal mask and
    196   1.1      fvdl  * stack state from context left by sendsig (above).
    197   1.1      fvdl  * Return to previous pc and psl as specified by
    198   1.1      fvdl  * context left by sendsig. Check carefully to
    199   1.1      fvdl  * make sure that the user has not modified the
    200   1.1      fvdl  * psl to gain improper privileges or to cause
    201   1.1      fvdl  * a machine fault.
    202   1.1      fvdl  */
    203   1.1      fvdl int
    204  1.20   mycroft linux_sys_sigreturn(p, v, retval)
    205   1.1      fvdl 	struct proc *p;
    206  1.19   thorpej 	void *v;
    207  1.19   thorpej 	register_t *retval;
    208  1.19   thorpej {
    209  1.20   mycroft 	struct linux_sys_sigreturn_args /* {
    210   1.1      fvdl 		syscallarg(struct linux_sigcontext *) scp;
    211  1.19   thorpej 	} */ *uap = v;
    212   1.1      fvdl 	struct linux_sigcontext *scp, context;
    213   1.1      fvdl 	register struct trapframe *tf;
    214   1.1      fvdl 
    215   1.3   mycroft 	tf = p->p_md.md_regs;
    216   1.1      fvdl 
    217   1.1      fvdl 	/*
    218   1.1      fvdl 	 * The trampoline code hands us the context.
    219   1.1      fvdl 	 * It is unsafe to keep track of it ourselves, in the event that a
    220   1.1      fvdl 	 * program jumps out of a signal handler.
    221   1.1      fvdl 	 */
    222   1.1      fvdl 	scp = SCARG(uap, scp);
    223   1.1      fvdl 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
    224   1.1      fvdl 		return (EFAULT);
    225   1.1      fvdl 
    226   1.1      fvdl 	/*
    227   1.1      fvdl 	 * Restore signal context.
    228   1.1      fvdl 	 */
    229   1.4   mycroft #ifdef VM86
    230   1.7   mycroft 	if (context.sc_eflags & PSL_VM) {
    231   1.7   mycroft 		tf->tf_vm86_gs = context.sc_gs;
    232   1.7   mycroft 		tf->tf_vm86_fs = context.sc_fs;
    233   1.7   mycroft 		tf->tf_vm86_es = context.sc_es;
    234   1.7   mycroft 		tf->tf_vm86_ds = context.sc_ds;
    235  1.26   mycroft 		set_vflags(p, context.sc_eflags);
    236   1.4   mycroft 	} else
    237   1.4   mycroft #endif
    238   1.4   mycroft 	{
    239  1.26   mycroft 		/*
    240  1.26   mycroft 		 * Check for security violations.  If we're returning to
    241  1.26   mycroft 		 * protected mode, the CPU will validate the segment registers
    242  1.26   mycroft 		 * automatically and generate a trap on violations.  We handle
    243  1.26   mycroft 		 * the trap, rather than doing all of the checking here.
    244  1.26   mycroft 		 */
    245  1.26   mycroft 		if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
    246  1.26   mycroft 		    !USERMODE(context.sc_cs, context.sc_eflags))
    247  1.26   mycroft 			return (EINVAL);
    248  1.26   mycroft 
    249   1.4   mycroft 		/* %fs and %gs were restored by the trampoline. */
    250   1.7   mycroft 		tf->tf_es = context.sc_es;
    251   1.7   mycroft 		tf->tf_ds = context.sc_ds;
    252  1.26   mycroft 		tf->tf_eflags = context.sc_eflags;
    253   1.4   mycroft 	}
    254  1.26   mycroft 	tf->tf_edi = context.sc_edi;
    255  1.26   mycroft 	tf->tf_esi = context.sc_esi;
    256  1.26   mycroft 	tf->tf_ebp = context.sc_ebp;
    257  1.26   mycroft 	tf->tf_ebx = context.sc_ebx;
    258  1.26   mycroft 	tf->tf_edx = context.sc_edx;
    259  1.26   mycroft 	tf->tf_ecx = context.sc_ecx;
    260  1.26   mycroft 	tf->tf_eax = context.sc_eax;
    261  1.26   mycroft 	tf->tf_eip = context.sc_eip;
    262  1.26   mycroft 	tf->tf_cs = context.sc_cs;
    263  1.26   mycroft 	tf->tf_esp = context.sc_esp_at_signal;
    264  1.26   mycroft 	tf->tf_ss = context.sc_ss;
    265  1.26   mycroft 
    266  1.26   mycroft 	p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
    267  1.26   mycroft 	p->p_sigmask = context.sc_mask & ~sigcantmask;
    268   1.1      fvdl 
    269   1.1      fvdl 	return (EJUSTRETURN);
    270   1.6   mycroft }
    271   1.6   mycroft 
    272   1.7   mycroft #ifdef USER_LDT
    273   1.7   mycroft 
    274   1.7   mycroft int
    275   1.7   mycroft linux_read_ldt(p, uap, retval)
    276   1.7   mycroft 	struct proc *p;
    277  1.20   mycroft 	struct linux_sys_modify_ldt_args /* {
    278   1.7   mycroft 		syscallarg(int) func;
    279   1.7   mycroft 		syscallarg(void *) ptr;
    280   1.7   mycroft 		syscallarg(size_t) bytecount;
    281   1.7   mycroft 	} */ *uap;
    282   1.7   mycroft 	register_t *retval;
    283   1.7   mycroft {
    284   1.7   mycroft 	struct i386_get_ldt_args gl;
    285   1.7   mycroft 	int error;
    286   1.7   mycroft 	caddr_t sg;
    287   1.7   mycroft 	char *parms;
    288   1.7   mycroft 
    289  1.10  christos 	sg = stackgap_init(p->p_emul);
    290   1.7   mycroft 
    291   1.7   mycroft 	gl.start = 0;
    292   1.7   mycroft 	gl.desc = SCARG(uap, ptr);
    293   1.7   mycroft 	gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
    294   1.7   mycroft 
    295   1.7   mycroft 	parms = stackgap_alloc(&sg, sizeof(gl));
    296   1.7   mycroft 
    297  1.29  christos 	if ((error = copyout(&gl, parms, sizeof(gl))) != 0)
    298   1.7   mycroft 		return (error);
    299   1.7   mycroft 
    300  1.29  christos 	if ((error = i386_get_ldt(p, parms, retval)) != 0)
    301   1.7   mycroft 		return (error);
    302   1.7   mycroft 
    303   1.7   mycroft 	*retval *= sizeof(union descriptor);
    304   1.7   mycroft 	return (0);
    305   1.7   mycroft }
    306   1.7   mycroft 
    307   1.7   mycroft struct linux_ldt_info {
    308   1.7   mycroft 	u_int entry_number;
    309   1.7   mycroft 	u_long base_addr;
    310   1.7   mycroft 	u_int limit;
    311   1.7   mycroft 	u_int seg_32bit:1;
    312   1.7   mycroft 	u_int contents:2;
    313   1.7   mycroft 	u_int read_exec_only:1;
    314   1.7   mycroft 	u_int limit_in_pages:1;
    315   1.7   mycroft 	u_int seg_not_present:1;
    316   1.7   mycroft };
    317   1.7   mycroft 
    318   1.7   mycroft int
    319   1.7   mycroft linux_write_ldt(p, uap, retval)
    320   1.7   mycroft 	struct proc *p;
    321  1.20   mycroft 	struct linux_sys_modify_ldt_args /* {
    322   1.7   mycroft 		syscallarg(int) func;
    323   1.7   mycroft 		syscallarg(void *) ptr;
    324   1.7   mycroft 		syscallarg(size_t) bytecount;
    325   1.7   mycroft 	} */ *uap;
    326   1.7   mycroft 	register_t *retval;
    327   1.7   mycroft {
    328   1.7   mycroft 	struct linux_ldt_info ldt_info;
    329   1.7   mycroft 	struct segment_descriptor sd;
    330   1.7   mycroft 	struct i386_set_ldt_args sl;
    331   1.7   mycroft 	int error;
    332   1.7   mycroft 	caddr_t sg;
    333   1.7   mycroft 	char *parms;
    334   1.7   mycroft 
    335   1.7   mycroft 	if (SCARG(uap, bytecount) != sizeof(ldt_info))
    336   1.7   mycroft 		return (EINVAL);
    337  1.29  christos 	if ((error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info))) != 0)
    338   1.7   mycroft 		return error;
    339   1.7   mycroft 	if (ldt_info.contents == 3)
    340   1.7   mycroft 		return (EINVAL);
    341   1.7   mycroft 
    342  1.10  christos 	sg = stackgap_init(p->p_emul);
    343   1.7   mycroft 
    344   1.7   mycroft 	sd.sd_lobase = ldt_info.base_addr & 0xffffff;
    345   1.7   mycroft 	sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
    346   1.7   mycroft 	sd.sd_lolimit = ldt_info.limit & 0xffff;
    347   1.7   mycroft 	sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
    348   1.7   mycroft 	sd.sd_type =
    349   1.7   mycroft 	    16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
    350   1.7   mycroft 	sd.sd_dpl = SEL_UPL;
    351   1.7   mycroft 	sd.sd_p = !ldt_info.seg_not_present;
    352   1.7   mycroft 	sd.sd_def32 = ldt_info.seg_32bit;
    353   1.7   mycroft 	sd.sd_gran = ldt_info.limit_in_pages;
    354   1.7   mycroft 
    355   1.7   mycroft 	sl.start = ldt_info.entry_number;
    356   1.7   mycroft 	sl.desc = stackgap_alloc(&sg, sizeof(sd));
    357   1.7   mycroft 	sl.num = 1;
    358   1.7   mycroft 
    359   1.8   mycroft #if 0
    360  1.30  christos 	kprintf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
    361   1.7   mycroft 	    ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
    362   1.8   mycroft #endif
    363   1.7   mycroft 
    364   1.7   mycroft 	parms = stackgap_alloc(&sg, sizeof(sl));
    365   1.7   mycroft 
    366  1.29  christos 	if ((error = copyout(&sd, sl.desc, sizeof(sd))) != 0)
    367   1.7   mycroft 		return (error);
    368  1.29  christos 	if ((error = copyout(&sl, parms, sizeof(sl))) != 0)
    369   1.7   mycroft 		return (error);
    370   1.7   mycroft 
    371  1.29  christos 	if ((error = i386_set_ldt(p, parms, retval)) != 0)
    372   1.7   mycroft 		return (error);
    373   1.7   mycroft 
    374   1.7   mycroft 	*retval = 0;
    375   1.7   mycroft 	return (0);
    376   1.7   mycroft }
    377   1.7   mycroft 
    378   1.7   mycroft #endif /* USER_LDT */
    379   1.7   mycroft 
    380   1.6   mycroft int
    381  1.20   mycroft linux_sys_modify_ldt(p, v, retval)
    382   1.6   mycroft 	struct proc *p;
    383  1.19   thorpej 	void *v;
    384  1.19   thorpej 	register_t *retval;
    385  1.19   thorpej {
    386  1.20   mycroft 	struct linux_sys_modify_ldt_args /* {
    387   1.6   mycroft 		syscallarg(int) func;
    388   1.6   mycroft 		syscallarg(void *) ptr;
    389   1.6   mycroft 		syscallarg(size_t) bytecount;
    390  1.19   thorpej 	} */ *uap = v;
    391   1.6   mycroft 
    392   1.6   mycroft 	switch (SCARG(uap, func)) {
    393   1.7   mycroft #ifdef USER_LDT
    394   1.6   mycroft 	case 0:
    395   1.7   mycroft 		return (linux_read_ldt(p, uap, retval));
    396   1.7   mycroft 
    397   1.6   mycroft 	case 1:
    398   1.7   mycroft 		return (linux_write_ldt(p, uap, retval));
    399   1.7   mycroft #endif /* USER_LDT */
    400   1.7   mycroft 
    401   1.6   mycroft 	default:
    402   1.6   mycroft 		return (ENOSYS);
    403   1.6   mycroft 	}
    404  1.13      fvdl }
    405  1.13      fvdl 
    406  1.13      fvdl /*
    407  1.13      fvdl  * XXX Pathetic hack to make svgalib work. This will fake the major
    408  1.13      fvdl  * device number of an opened VT so that svgalib likes it. grmbl.
    409  1.13      fvdl  * Should probably do it 'wrong the right way' and use a mapping
    410  1.13      fvdl  * array for all major device numbers, and map linux_mknod too.
    411  1.13      fvdl  */
    412  1.13      fvdl dev_t
    413  1.13      fvdl linux_fakedev(dev)
    414  1.13      fvdl 	dev_t dev;
    415  1.13      fvdl {
    416  1.20   mycroft 
    417  1.13      fvdl 	if (major(dev) == NETBSD_CONS_MAJOR)
    418  1.17      fvdl 		return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
    419  1.13      fvdl 	return dev;
    420  1.13      fvdl }
    421  1.13      fvdl 
    422  1.13      fvdl /*
    423  1.13      fvdl  * We come here in a last attempt to satisfy a Linux ioctl() call
    424  1.13      fvdl  */
    425  1.13      fvdl int
    426  1.19   thorpej linux_machdepioctl(p, v, retval)
    427  1.13      fvdl 	struct proc *p;
    428  1.19   thorpej 	void *v;
    429  1.19   thorpej 	register_t *retval;
    430  1.19   thorpej {
    431  1.20   mycroft 	struct linux_sys_ioctl_args /* {
    432  1.13      fvdl 		syscallarg(int) fd;
    433  1.13      fvdl 		syscallarg(u_long) com;
    434  1.13      fvdl 		syscallarg(caddr_t) data;
    435  1.19   thorpej 	} */ *uap = v;
    436  1.29  christos 	struct sys_ioctl_args bia;
    437  1.15      fvdl 	u_long com;
    438  1.15      fvdl #if NVT > 0
    439  1.29  christos 	int error;
    440  1.13      fvdl 	struct vt_mode lvt;
    441  1.13      fvdl 	caddr_t bvtp, sg;
    442  1.15      fvdl #endif
    443  1.13      fvdl 
    444  1.13      fvdl 	SCARG(&bia, fd) = SCARG(uap, fd);
    445  1.13      fvdl 	SCARG(&bia, data) = SCARG(uap, data);
    446  1.13      fvdl 	com = SCARG(uap, com);
    447  1.13      fvdl 
    448  1.13      fvdl 	switch (com) {
    449  1.13      fvdl #if NVT > 0
    450  1.13      fvdl 	case LINUX_KDGKBMODE:
    451  1.21      fvdl 		com = KDGKBMODE;
    452  1.21      fvdl 		break;
    453  1.13      fvdl 	case LINUX_KDSKBMODE:
    454  1.13      fvdl 		com = KDSKBMODE;
    455  1.13      fvdl 		if ((unsigned)SCARG(uap, data) == LINUX_K_MEDIUMRAW)
    456  1.13      fvdl 			SCARG(&bia, data) = (caddr_t)K_RAW;
    457  1.13      fvdl 		break;
    458  1.13      fvdl 	case LINUX_KDMKTONE:
    459  1.13      fvdl 		com = KDMKTONE;
    460  1.13      fvdl 		break;
    461  1.13      fvdl 	case LINUX_KDSETMODE:
    462  1.13      fvdl 		com = KDSETMODE;
    463  1.13      fvdl 		break;
    464  1.13      fvdl 	case LINUX_KDENABIO:
    465  1.13      fvdl 		com = KDENABIO;
    466  1.13      fvdl 		break;
    467  1.13      fvdl 	case LINUX_KDDISABIO:
    468  1.13      fvdl 		com = KDDISABIO;
    469  1.13      fvdl 		break;
    470  1.13      fvdl 	case LINUX_KDGETLED:
    471  1.13      fvdl 		com = KDGETLED;
    472  1.13      fvdl 		break;
    473  1.13      fvdl 	case LINUX_KDSETLED:
    474  1.13      fvdl 		com = KDSETLED;
    475  1.13      fvdl 		break;
    476  1.13      fvdl 	case LINUX_VT_OPENQRY:
    477  1.13      fvdl 		com = VT_OPENQRY;
    478  1.13      fvdl 		break;
    479  1.13      fvdl 	case LINUX_VT_GETMODE:
    480  1.13      fvdl 		SCARG(&bia, com) = VT_GETMODE;
    481  1.20   mycroft 		if ((error = sys_ioctl(p, &bia, retval)))
    482  1.13      fvdl 			return error;
    483  1.13      fvdl 		if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
    484  1.13      fvdl 		    sizeof (struct vt_mode))))
    485  1.13      fvdl 			return error;
    486  1.13      fvdl 		lvt.relsig = bsd_to_linux_sig[lvt.relsig];
    487  1.13      fvdl 		lvt.acqsig = bsd_to_linux_sig[lvt.acqsig];
    488  1.13      fvdl 		lvt.frsig = bsd_to_linux_sig[lvt.frsig];
    489  1.13      fvdl 		return copyout((caddr_t)&lvt, SCARG(uap, data),
    490  1.13      fvdl 		    sizeof (struct vt_mode));
    491  1.13      fvdl 	case LINUX_VT_SETMODE:
    492  1.13      fvdl 		com = VT_SETMODE;
    493  1.13      fvdl 		if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
    494  1.13      fvdl 		    sizeof (struct vt_mode))))
    495  1.13      fvdl 			return error;
    496  1.13      fvdl 		lvt.relsig = linux_to_bsd_sig[lvt.relsig];
    497  1.13      fvdl 		lvt.acqsig = linux_to_bsd_sig[lvt.acqsig];
    498  1.13      fvdl 		lvt.frsig = linux_to_bsd_sig[lvt.frsig];
    499  1.13      fvdl 		sg = stackgap_init(p->p_emul);
    500  1.13      fvdl 		bvtp = stackgap_alloc(&sg, sizeof (struct vt_mode));
    501  1.13      fvdl 		if ((error = copyout(&lvt, bvtp, sizeof (struct vt_mode))))
    502  1.13      fvdl 			return error;
    503  1.13      fvdl 		SCARG(&bia, data) = bvtp;
    504  1.13      fvdl 		break;
    505  1.13      fvdl 	case LINUX_VT_RELDISP:
    506  1.13      fvdl 		com = VT_RELDISP;
    507  1.13      fvdl 		break;
    508  1.13      fvdl 	case LINUX_VT_ACTIVATE:
    509  1.13      fvdl 		com = VT_ACTIVATE;
    510  1.13      fvdl 		break;
    511  1.13      fvdl 	case LINUX_VT_WAITACTIVE:
    512  1.13      fvdl 		com = VT_WAITACTIVE;
    513  1.13      fvdl 		break;
    514  1.13      fvdl #endif
    515  1.13      fvdl 	default:
    516  1.30  christos 		kprintf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    517  1.13      fvdl 		return EINVAL;
    518  1.13      fvdl 	}
    519  1.13      fvdl 	SCARG(&bia, com) = com;
    520  1.20   mycroft 	return sys_ioctl(p, &bia, retval);
    521  1.13      fvdl }
    522  1.13      fvdl 
    523  1.13      fvdl /*
    524  1.13      fvdl  * Set I/O permissions for a process. Just set the maximum level
    525  1.13      fvdl  * right away (ignoring the argument), otherwise we would have
    526  1.13      fvdl  * to rely on I/O permission maps, which are not implemented.
    527  1.13      fvdl  */
    528  1.13      fvdl int
    529  1.20   mycroft linux_sys_iopl(p, v, retval)
    530  1.13      fvdl 	struct proc *p;
    531  1.19   thorpej 	void *v;
    532  1.19   thorpej 	register_t *retval;
    533  1.19   thorpej {
    534  1.29  christos #if 0
    535  1.20   mycroft 	struct linux_sys_iopl_args /* {
    536  1.13      fvdl 		syscallarg(int) level;
    537  1.19   thorpej 	} */ *uap = v;
    538  1.29  christos #endif
    539  1.13      fvdl 	struct trapframe *fp = p->p_md.md_regs;
    540  1.13      fvdl 
    541  1.13      fvdl 	if (suser(p->p_ucred, &p->p_acflag) != 0)
    542  1.13      fvdl 		return EPERM;
    543  1.13      fvdl 	fp->tf_eflags |= PSL_IOPL;
    544  1.13      fvdl 	*retval = 0;
    545  1.13      fvdl 	return 0;
    546  1.13      fvdl }
    547  1.13      fvdl 
    548  1.13      fvdl /*
    549  1.13      fvdl  * See above. If a root process tries to set access to an I/O port,
    550  1.13      fvdl  * just let it have the whole range.
    551  1.13      fvdl  */
    552  1.13      fvdl int
    553  1.20   mycroft linux_sys_ioperm(p, v, retval)
    554  1.13      fvdl 	struct proc *p;
    555  1.19   thorpej 	void *v;
    556  1.19   thorpej 	register_t *retval;
    557  1.19   thorpej {
    558  1.20   mycroft 	struct linux_sys_ioperm_args /* {
    559  1.13      fvdl 		syscallarg(unsigned int) lo;
    560  1.13      fvdl 		syscallarg(unsigned int) hi;
    561  1.13      fvdl 		syscallarg(int) val;
    562  1.19   thorpej 	} */ *uap = v;
    563  1.13      fvdl 	struct trapframe *fp = p->p_md.md_regs;
    564  1.13      fvdl 
    565  1.13      fvdl 	if (suser(p->p_ucred, &p->p_acflag) != 0)
    566  1.13      fvdl 		return EPERM;
    567  1.13      fvdl 	if (SCARG(uap, val))
    568  1.13      fvdl 		fp->tf_eflags |= PSL_IOPL;
    569  1.13      fvdl 	*retval = 0;
    570  1.13      fvdl 	return 0;
    571   1.1      fvdl }
    572