Home | History | Annotate | Line # | Download | only in mips
linux_machdep.c revision 1.10
      1 /*	$NetBSD: linux_machdep.c,v 1.10 2002/02/15 16:48:01 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Emmanuel Dreyfus.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.10 2002/02/15 16:48:01 christos Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/signalvar.h>
     45 #include <sys/kernel.h>
     46 #include <sys/map.h>
     47 #include <sys/proc.h>
     48 #include <sys/user.h>
     49 #include <sys/buf.h>
     50 #include <sys/reboot.h>
     51 #include <sys/conf.h>
     52 #include <sys/exec.h>
     53 #include <sys/file.h>
     54 #include <sys/callout.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/msgbuf.h>
     58 #include <sys/mount.h>
     59 #include <sys/vnode.h>
     60 #include <sys/device.h>
     61 #include <sys/syscallargs.h>
     62 #include <sys/filedesc.h>
     63 #include <sys/exec_elf.h>
     64 #include <sys/disklabel.h>
     65 #include <sys/ioctl.h>
     66 #include <sys/sysctl.h>
     67 #include <miscfs/specfs/specdev.h>
     68 
     69 #include <compat/linux/common/linux_types.h>
     70 #include <compat/linux/common/linux_signal.h>
     71 #include <compat/linux/common/linux_util.h>
     72 #include <compat/linux/common/linux_ioctl.h>
     73 #include <compat/linux/common/linux_hdio.h>
     74 #include <compat/linux/common/linux_exec.h>
     75 #include <compat/linux/common/linux_machdep.h>
     76 
     77 #include <compat/linux/linux_syscallargs.h>
     78 
     79 #include <machine/cpu.h>
     80 #include <machine/psl.h>
     81 #include <machine/reg.h>
     82 #include <machine/regnum.h>
     83 #include <machine/vmparam.h>
     84 #include <machine/locore.h>
     85 
     86 #include <mips/cache.h>
     87 
     88 /*
     89  * To see whether wscons is configured (for virtual console ioctl calls).
     90  */
     91 #if defined(_KERNEL_OPT)
     92 #include "wsdisplay.h"
     93 #endif
     94 #if (NWSDISPLAY > 0)
     95 #include <dev/wscons/wsconsio.h>
     96 #include <dev/wscons/wsdisplay_usl_io.h>
     97 #endif
     98 
     99 /*
    100  * Set set up registers on exec.
    101  * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
    102  * entry uses NetBSD's native setregs instead of linux_setregs
    103  */
    104 void
    105 linux_setregs(p, pack, stack)
    106 	struct proc *p;
    107 	struct exec_package *pack;
    108 	u_long stack;
    109 {
    110 	setregs(p, pack, stack);
    111 	return;
    112 }
    113 
    114 /*
    115  * Send an interrupt to process.
    116  *
    117  * Adapted from sys/arch/mips/mips/mips_machdep.c
    118  *
    119  * XXX Does not work well yet with RT signals
    120  *
    121  */
    122 
    123 void
    124 linux_sendsig(catcher, sig, mask, code)  /* XXX Check me */
    125 	sig_t catcher;
    126 	int sig;
    127 	sigset_t *mask;
    128 	u_long code;
    129 {
    130 	struct proc *p = curproc;
    131 	struct linux_sigframe *fp;
    132 	struct frame *f;
    133 	int i,onstack;
    134 	struct linux_sigframe sf;
    135 
    136 #ifdef DEBUG_LINUX
    137 	printf("linux_sendsig()\n");
    138 #endif /* DEBUG_LINUX */
    139 	f = (struct frame *)p->p_md.md_regs;
    140 
    141 	/*
    142 	 * Do we need to jump onto the signal stack?
    143 	 */
    144 	onstack =
    145 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    146 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    147 
    148 	/*
    149 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
    150 	 * not use it yet. XXX fix this.
    151 	 */
    152 	onstack=0;
    153 
    154 	/*
    155 	 * Allocate space for the signal handler context.
    156 	 */
    157 	if (onstack)
    158 		fp = (struct linux_sigframe *)
    159 		    ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
    160 		    + p->p_sigctx.ps_sigstk.ss_size);
    161 	else
    162 		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
    163 		fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[SP];
    164 
    165 	/*
    166 	 * Build stack frame for signal trampoline.
    167 	 */
    168 	memset(&sf, 0, sizeof sf);
    169 
    170 	/*
    171 	 * This is the signal trampoline used by Linux, we don't use it,
    172 	 * but we set it up in case an application expects it to be there
    173 	 */
    174 	sf.lsf_code[0] = 0x24020000;	/* li	v0, __NR_sigreturn	*/
    175 	sf.lsf_code[1] = 0x0000000c;	/* syscall			*/
    176 
    177 	native_to_linux_sigset(mask, &sf.lsf_mask);
    178 	for (i=0; i<32; i++) {
    179 		sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
    180 	}
    181 	sf.lsf_sc.lsc_mdhi = f->f_regs[MULHI];
    182 	sf.lsf_sc.lsc_mdlo = f->f_regs[MULLO];
    183 	sf.lsf_sc.lsc_pc = f->f_regs[PC];
    184 	sf.lsf_sc.lsc_status = f->f_regs[SR];	/* XXX */
    185 	sf.lsf_sc.lsc_cause = f->f_regs[CAUSE];
    186 	sf.lsf_sc.lsc_badvaddr = f->f_regs[BADVADDR];	/* XXX */
    187 
    188 	/*
    189 	 * Save signal stack.  XXX broken
    190 	 */
    191 	/* kregs.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; */
    192 
    193 	/*
    194 	 * Install the sigframe onto the stack
    195 	 */
    196 	fp -= sizeof(struct linux_sigframe);
    197 	if (copyout(&sf, fp, sizeof(sf)) != 0) {
    198 		/*
    199 		 * Process has trashed its stack; give it an illegal
    200 		 * instruction to halt it in its tracks.
    201 		 */
    202 #ifdef DEBUG_LINUX
    203 		printf("linux_sendsig: stack trashed\n");
    204 #endif /* DEBUG_LINUX */
    205 		sigexit(p, SIGILL);
    206 		/* NOTREACHED */
    207 	}
    208 
    209 	/* Set up the registers to return to sigcode. */
    210 	f->f_regs[A0] = native_to_linux_sig[sig];
    211 	f->f_regs[A1] = 0;
    212 	f->f_regs[A2] = (unsigned long)&fp->lsf_sc;
    213 
    214 #ifdef DEBUG_LINUX
    215 	printf("sigcontext is at %p\n", &fp->lsf_sc);
    216 #endif /* DEBUG_LINUX */
    217 
    218 	f->f_regs[SP] = (unsigned long)fp;
    219 	/* Signal trampoline code is at base of user stack. */
    220 	f->f_regs[RA] = (unsigned long)p->p_sigctx.ps_sigcode;
    221 	f->f_regs[T9] = (unsigned long)catcher;
    222 	f->f_regs[PC] = (unsigned long)catcher;
    223 
    224 	/* Remember that we're now on the signal stack. */
    225 	if (onstack)
    226 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    227 
    228 	return;
    229 }
    230 
    231 /*
    232  * System call to cleanup state after a signal
    233  * has been taken.  Reset signal mask and
    234  * stack state from context left by sendsig (above).
    235  */
    236 int
    237 linux_sys_sigreturn(p, v, retval)
    238 	struct proc *p;
    239 	void *v;
    240 	register_t *retval;
    241 {
    242 	struct linux_sys_sigreturn_args /* {
    243 		syscallarg(struct linux_sigframe *) sf;
    244 	} */ *uap = v;
    245 	struct linux_sigframe *sf, ksf;
    246 	struct frame *f;
    247 	sigset_t mask;
    248 	int i, error;
    249 
    250 #ifdef DEBUG_LINUX
    251 	printf("linux_sys_sigreturn()\n");
    252 #endif /* DEBUG_LINUX */
    253 
    254 	/*
    255 	 * The trampoline code hands us the context.
    256 	 * It is unsafe to keep track of it ourselves, in the event that a
    257 	 * program jumps out of a signal handler.
    258 	 */
    259 	sf = SCARG(uap, sf);
    260 
    261 	if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
    262 		return (error);
    263 
    264 	/* Restore the register context. */
    265 	f = (struct frame *)p->p_md.md_regs;
    266 	for (i=0; i<32; i++)
    267 		f->f_regs[i] = ksf.lsf_sc.lsc_regs[i];
    268 	f->f_regs[MULLO] = ksf.lsf_sc.lsc_mdlo;
    269 	f->f_regs[MULHI] = ksf.lsf_sc.lsc_mdhi;
    270 	f->f_regs[PC] = ksf.lsf_sc.lsc_pc;
    271 	f->f_regs[BADVADDR] = ksf.lsf_sc.lsc_badvaddr;
    272 	f->f_regs[CAUSE] = ksf.lsf_sc.lsc_cause;
    273 	f->f_regs[SR] = ksf.lsf_sc.lsc_status;
    274 
    275 	/* Restore signal stack. */
    276 	p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    277 
    278 	/* Restore signal mask. */
    279 	linux_to_native_sigset((linux_sigset_t *)&ksf.lsf_mask, &mask);
    280 	(void)sigprocmask1(p, SIG_SETMASK, &mask, 0);
    281 
    282 	return (EJUSTRETURN);
    283 }
    284 
    285 
    286 int
    287 linux_sys_rt_sigreturn(p, v, retval)
    288 	struct proc *p;
    289 	void *v;
    290 	register_t *retval;
    291 {
    292 	return 0;
    293 }
    294 
    295 
    296 #if 0
    297 int
    298 linux_sys_modify_ldt(p, v, retval)
    299 	struct proc *p;
    300 	void *v;
    301 	register_t *retval;
    302 {
    303 	/*
    304 	 * This syscall is not implemented in Linux/Mips: we should not
    305 	 * be here
    306 	 */
    307 #ifdef DEBUG_LINUX
    308 	printf("linux_sys_modify_ldt: should not be here.\n");
    309 #endif /* DEBUG_LINUX */
    310   return 0;
    311 }
    312 #endif
    313 
    314 /*
    315  * major device numbers remapping
    316  */
    317 dev_t
    318 linux_fakedev(dev, raw)
    319 	dev_t dev;
    320 	int raw;
    321 {
    322   /* XXX write me */
    323   return dev;
    324 }
    325 
    326 /*
    327  * We come here in a last attempt to satisfy a Linux ioctl() call
    328  */
    329 int
    330 linux_machdepioctl(p, v, retval)
    331 	struct proc *p;
    332 	void *v;
    333 	register_t *retval;
    334 {
    335 	return 0;
    336 }
    337 
    338 /*
    339  * See above. If a root process tries to set access to an I/O port,
    340  * just let it have the whole range.
    341  */
    342 int
    343 linux_sys_ioperm(p, v, retval)
    344 	struct proc *p;
    345 	void *v;
    346 	register_t *retval;
    347 {
    348 	/*
    349 	 * This syscall is not implemented in Linux/Mips: we should not be here
    350 	 */
    351 #ifdef DEBUG_LINUX
    352 	printf("linux_sys_ioperm: should not be here.\n");
    353 #endif /* DEBUG_LINUX */
    354 	return 0;
    355 }
    356 
    357 /*
    358  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    359  */
    360 int
    361 linux_sys_new_uname(p, v, retval)
    362 	struct proc *p;
    363 	void *v;
    364 	register_t *retval;
    365 {
    366 /*
    367  * Use this if you want to try Linux emulation with a glibc-2.2
    368  * or higher. Note that signals will not work
    369  */
    370 #if 0
    371         struct linux_sys_uname_args /* {
    372                 syscallarg(struct linux_utsname *) up;
    373         } */ *uap = v;
    374         struct linux_utsname luts;
    375 
    376         strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    377         strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    378         strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
    379         strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    380         strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    381         strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    382 
    383         return copyout(&luts, SCARG(uap, up), sizeof(luts));
    384 #else
    385 	return linux_sys_uname(p, v, retval);
    386 #endif
    387 }
    388 
    389 /*
    390  * In Linux, cacheflush is icurrently implemented
    391  * as a whole cache flush (arguments are ignored)
    392  * we emulate this broken beahior.
    393  */
    394 int
    395 linux_sys_cacheflush(p, v, retval)
    396 	struct proc *p;
    397 	void *v;
    398 	register_t *retval;
    399 {
    400 	mips_icache_sync_all();
    401 	mips_dcache_wbinv_all();
    402 	return 0;
    403 }
    404 
    405 /*
    406  * This system call is depecated in Linux, but
    407  * some binaries and some libraries use it.
    408  */
    409 int
    410 linux_sys_sysmips(p, v, retval)
    411 	struct proc *p;
    412 	void *v;
    413 	register_t *retval;
    414 {
    415 	struct linux_sys_sysmips_args {
    416 		syscallarg(int) cmd;
    417 		syscallarg(int) arg1;
    418 		syscallarg(int) arg2;
    419 		syscallarg(int) arg3;
    420 	} *uap = v;
    421 	int error;
    422 
    423 	switch (SCARG(uap, cmd)) {
    424 	case LINUX_SETNAME: {
    425 		char nodename [LINUX___NEW_UTS_LEN + 1];
    426 		int name;
    427 		size_t len;
    428 
    429 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    430 			return error;
    431 		if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
    432 		    LINUX___NEW_UTS_LEN, &len)) != 0)
    433 			return error;
    434 
    435 		name = KERN_HOSTNAME;
    436 		return (kern_sysctl(&name, 1, 0, 0, nodename, len, p));
    437 
    438 		break;
    439 	}
    440 	case LINUX_MIPS_ATOMIC_SET: {
    441 		void *addr;
    442 		int s;
    443 
    444 		addr = (void *)SCARG(uap, arg1);
    445 
    446 		if ((uvm_useracc((caddr_t)addr, sizeof(int),
    447 		    B_READ | B_WRITE)) != 1)
    448 			return EFAULT;
    449 
    450 		s = splhigh();
    451 		/*
    452 		 * No error testing here. This is bad, but Linux does
    453 		 * it like this. The source aknowledge "This is broken"
    454 		 * in a comment...
    455 		 */
    456 		*retval = (register_t)fubyte(addr);
    457 		error = subyte(addr, SCARG(uap, arg2));
    458 		splx(s);
    459 
    460 		return 0;
    461 		break;
    462 	}
    463 	case LINUX_MIPS_FIXADE:		/* XXX not implemented */
    464 		break;
    465 	case LINUX_FLUSH_CACHE:
    466 		mips_icache_sync_all();
    467 		mips_dcache_wbinv_all();
    468 		break;
    469 	case LINUX_MIPS_RDNVRAM:
    470 		return EIO;
    471 		break;
    472 	default:
    473 		return EINVAL;
    474 		break;
    475 	}
    476 #ifdef DEBUG_LINUX
    477 	printf("linux_sys_sysmips(): unimplemented command %d\n",
    478 	    SCARG(uap,cmd));
    479 #endif /* DEBUG_LINUX */
    480 	return 0;
    481 }
    482