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