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