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