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