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