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