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