Home | History | Annotate | Line # | Download | only in mips
linux_machdep.c revision 1.27.4.2
      1 /*	$NetBSD: linux_machdep.c,v 1.27.4.2 2007/01/27 01:40:59 ad 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.27.4.2 2007/01/27 01:40:59 ad 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/proc.h>
     47 #include <sys/user.h>
     48 #include <sys/buf.h>
     49 #include <sys/reboot.h>
     50 #include <sys/conf.h>
     51 #include <sys/exec.h>
     52 #include <sys/file.h>
     53 #include <sys/callout.h>
     54 #include <sys/malloc.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/msgbuf.h>
     57 #include <sys/mount.h>
     58 #include <sys/vnode.h>
     59 #include <sys/device.h>
     60 #include <sys/sa.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 <sys/kauth.h>
     68 #include <miscfs/specfs/specdev.h>
     69 
     70 #include <compat/linux/common/linux_types.h>
     71 #include <compat/linux/common/linux_signal.h>
     72 #include <compat/linux/common/linux_util.h>
     73 #include <compat/linux/common/linux_ioctl.h>
     74 #include <compat/linux/common/linux_hdio.h>
     75 #include <compat/linux/common/linux_exec.h>
     76 #include <compat/linux/common/linux_machdep.h>
     77 
     78 #include <compat/linux/linux_syscallargs.h>
     79 
     80 #include <machine/cpu.h>
     81 #include <machine/psl.h>
     82 #include <machine/reg.h>
     83 #include <machine/regnum.h>
     84 #include <machine/vmparam.h>
     85 #include <machine/locore.h>
     86 
     87 #include <mips/cache.h>
     88 
     89 /*
     90  * To see whether wscons is configured (for virtual console ioctl calls).
     91  */
     92 #if defined(_KERNEL_OPT)
     93 #include "wsdisplay.h"
     94 #endif
     95 #if (NWSDISPLAY > 0)
     96 #include <dev/wscons/wsconsio.h>
     97 #include <dev/wscons/wsdisplay_usl_io.h>
     98 #endif
     99 
    100 /*
    101  * Set set up registers on exec.
    102  * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
    103  * entry uses NetBSD's native setregs instead of linux_setregs
    104  */
    105 void
    106 linux_setregs(l, pack, stack)
    107 	struct lwp *l;
    108 	struct exec_package *pack;
    109 	u_long stack;
    110 {
    111 	setregs(l, pack, stack);
    112 	return;
    113 }
    114 
    115 /*
    116  * Send an interrupt to process.
    117  *
    118  * Adapted from sys/arch/mips/mips/mips_machdep.c
    119  *
    120  * XXX Does not work well yet with RT signals
    121  *
    122  */
    123 
    124 void
    125 linux_sendsig(ksi, mask)
    126 	const ksiginfo_t *ksi;
    127 	const sigset_t *mask;
    128 {
    129 	const int sig = ksi->ksi_signo;
    130 	struct lwp *l = curlwp;
    131 	struct proc *p = l->l_proc;
    132 	struct linux_sigframe *fp;
    133 	struct frame *f;
    134 	int i, onstack, error;
    135 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    136 	struct linux_sigframe sf;
    137 
    138 #ifdef DEBUG_LINUX
    139 	printf("linux_sendsig()\n");
    140 #endif /* DEBUG_LINUX */
    141 	f = (struct frame *)l->l_md.md_regs;
    142 
    143 	/*
    144 	 * Do we need to jump onto the signal stack?
    145 	 */
    146 	onstack =
    147 	    (l->l_sigstk->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    148 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    149 
    150 	/*
    151 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
    152 	 * not use it yet. XXX fix this.
    153 	 */
    154 	onstack=0;
    155 
    156 	/*
    157 	 * Allocate space for the signal handler context.
    158 	 */
    159 	if (onstack)
    160 		fp = (struct linux_sigframe *)
    161 		    ((caddr_t)l->l_sigstk->ss_sp
    162 		    + l->l_sigstk->ss_size);
    163 	else
    164 		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
    165 		fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[_R_SP];
    166 
    167 	/*
    168 	 * Build stack frame for signal trampoline.
    169 	 */
    170 	memset(&sf, 0, sizeof sf);
    171 
    172 	/*
    173 	 * This is the signal trampoline used by Linux, we don't use it,
    174 	 * but we set it up in case an application expects it to be there
    175 	 */
    176 	sf.lsf_code[0] = 0x24020000;	/* li	v0, __NR_sigreturn	*/
    177 	sf.lsf_code[1] = 0x0000000c;	/* syscall			*/
    178 
    179 	native_to_linux_sigset(&sf.lsf_mask, mask);
    180 	for (i=0; i<32; i++) {
    181 		sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
    182 	}
    183 	sf.lsf_sc.lsc_mdhi = f->f_regs[_R_MULHI];
    184 	sf.lsf_sc.lsc_mdlo = f->f_regs[_R_MULLO];
    185 	sf.lsf_sc.lsc_pc = f->f_regs[_R_PC];
    186 	sf.lsf_sc.lsc_status = f->f_regs[_R_SR];
    187 	sf.lsf_sc.lsc_cause = f->f_regs[_R_CAUSE];
    188 	sf.lsf_sc.lsc_badvaddr = f->f_regs[_R_BADVADDR];
    189 	sendsig_reset(l, sig);
    190 
    191 	/*
    192 	 * Save signal stack.  XXX broken
    193 	 */
    194 	/* kregs.sc_onstack = l->l_sigstk->ss_flags & SS_ONSTACK; */
    195 
    196 	/*
    197 	 * Install the sigframe onto the stack
    198 	 */
    199 	fp -= sizeof(struct linux_sigframe);
    200 	mutex_exit(&p->p_smutex);
    201 	error = copyout(&sf, fp, sizeof(sf);
    202 	mutex_enter(&p->p_smutex);
    203 
    204 	if (error != 0) {
    205 		/*
    206 		 * Process has trashed its stack; give it an illegal
    207 		 * instruction to halt it in its tracks.
    208 		 */
    209 #ifdef DEBUG_LINUX
    210 		printf("linux_sendsig: stack trashed\n");
    211 #endif /* DEBUG_LINUX */
    212 		sigexit(l, SIGILL);
    213 		/* NOTREACHED */
    214 	}
    215 
    216 	/* Set up the registers to return to sigcode. */
    217 	f->f_regs[_R_A0] = native_to_linux_signo[sig];
    218 	f->f_regs[_R_A1] = 0;
    219 	f->f_regs[_R_A2] = (unsigned long)&fp->lsf_sc;
    220 
    221 #ifdef DEBUG_LINUX
    222 	printf("sigcontext is at %p\n", &fp->lsf_sc);
    223 #endif /* DEBUG_LINUX */
    224 
    225 	f->f_regs[_R_SP] = (unsigned long)fp;
    226 	/* Signal trampoline code is at base of user stack. */
    227 	f->f_regs[_R_RA] = (unsigned long)p->p_sigctx.ps_sigcode;
    228 	f->f_regs[_R_T9] = (unsigned long)catcher;
    229 	f->f_regs[_R_PC] = (unsigned long)catcher;
    230 
    231 	/* Remember that we're now on the signal stack. */
    232 	if (onstack)
    233 		l->l_sigstk->ss_flags |= SS_ONSTACK;
    234 
    235 	return;
    236 }
    237 
    238 /*
    239  * System call to cleanup state after a signal
    240  * has been taken.  Reset signal mask and
    241  * stack state from context left by sendsig (above).
    242  */
    243 int
    244 linux_sys_sigreturn(l, v, retval)
    245 	struct lwp *l;
    246 	void *v;
    247 	register_t *retval;
    248 {
    249 	struct linux_sys_sigreturn_args /* {
    250 		syscallarg(struct linux_sigframe *) sf;
    251 	} */ *uap = v;
    252 	struct proc *p = l->l_proc;
    253 	struct linux_sigframe *sf, ksf;
    254 	struct frame *f;
    255 	sigset_t mask;
    256 	int i, error;
    257 
    258 #ifdef DEBUG_LINUX
    259 	printf("linux_sys_sigreturn()\n");
    260 #endif /* DEBUG_LINUX */
    261 
    262 	/*
    263 	 * The trampoline code hands us the context.
    264 	 * It is unsafe to keep track of it ourselves, in the event that a
    265 	 * program jumps out of a signal handler.
    266 	 */
    267 	sf = SCARG(uap, sf);
    268 
    269 	if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
    270 		return (error);
    271 
    272 	/* Restore the register context. */
    273 	f = (struct frame *)l->l_md.md_regs;
    274 	for (i=0; i<32; i++)
    275 		f->f_regs[i] = ksf.lsf_sc.lsc_regs[i];
    276 	f->f_regs[_R_MULLO] = ksf.lsf_sc.lsc_mdlo;
    277 	f->f_regs[_R_MULHI] = ksf.lsf_sc.lsc_mdhi;
    278 	f->f_regs[_R_PC] = ksf.lsf_sc.lsc_pc;
    279 	f->f_regs[_R_BADVADDR] = ksf.lsf_sc.lsc_badvaddr;
    280 	f->f_regs[_R_CAUSE] = ksf.lsf_sc.lsc_cause;
    281 
    282 	mutex_enter(&p->p_smutex);
    283 
    284 	/* Restore signal stack. */
    285 	l->l_sigstk->ss_flags &= ~SS_ONSTACK;
    286 
    287 	/* Restore signal mask. */
    288 	linux_to_native_sigset(&mask, (linux_sigset_t *)&ksf.lsf_mask);
    289 	(void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
    290 
    291 	mutex_exit(&p->p_smutex);
    292 
    293 	return (EJUSTRETURN);
    294 }
    295 
    296 
    297 int
    298 linux_sys_rt_sigreturn(l, v, retval)
    299 	struct lwp *l;
    300 	void *v;
    301 	register_t *retval;
    302 {
    303 	return (ENOSYS);
    304 }
    305 
    306 
    307 #if 0
    308 int
    309 linux_sys_modify_ldt(l, v, retval)
    310 	struct lwp *l;
    311 	void *v;
    312 	register_t *retval;
    313 {
    314 	/*
    315 	 * This syscall is not implemented in Linux/Mips: we should not
    316 	 * be here
    317 	 */
    318 #ifdef DEBUG_LINUX
    319 	printf("linux_sys_modify_ldt: should not be here.\n");
    320 #endif /* DEBUG_LINUX */
    321   return 0;
    322 }
    323 #endif
    324 
    325 /*
    326  * major device numbers remapping
    327  */
    328 dev_t
    329 linux_fakedev(dev, raw)
    330 	dev_t dev;
    331 	int raw;
    332 {
    333 	/* XXX write me */
    334 	return dev;
    335 }
    336 
    337 /*
    338  * We come here in a last attempt to satisfy a Linux ioctl() call
    339  */
    340 int
    341 linux_machdepioctl(p, v, retval)
    342 	struct proc *p;
    343 	void *v;
    344 	register_t *retval;
    345 {
    346 	return 0;
    347 }
    348 
    349 /*
    350  * See above. If a root process tries to set access to an I/O port,
    351  * just let it have the whole range.
    352  */
    353 int
    354 linux_sys_ioperm(l, v, retval)
    355 	struct lwp *l;
    356 	void *v;
    357 	register_t *retval;
    358 {
    359 	/*
    360 	 * This syscall is not implemented in Linux/Mips: we should not be here
    361 	 */
    362 #ifdef DEBUG_LINUX
    363 	printf("linux_sys_ioperm: should not be here.\n");
    364 #endif /* DEBUG_LINUX */
    365 	return 0;
    366 }
    367 
    368 /*
    369  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    370  */
    371 int
    372 linux_sys_new_uname(l, v, retval)
    373 	struct lwp *l;
    374 	void *v;
    375 	register_t *retval;
    376 {
    377 /*
    378  * Use this if you want to try Linux emulation with a glibc-2.2
    379  * or higher. Note that signals will not work
    380  */
    381 #if 0
    382         struct linux_sys_uname_args /* {
    383                 syscallarg(struct linux_utsname *) up;
    384         } */ *uap = v;
    385         struct linux_utsname luts;
    386 
    387         strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    388         strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    389         strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
    390         strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    391         strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    392         strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    393 
    394         return copyout(&luts, SCARG(uap, up), sizeof(luts));
    395 #else
    396 	return linux_sys_uname(l, v, retval);
    397 #endif
    398 }
    399 
    400 /*
    401  * In Linux, cacheflush is currently implemented
    402  * as a whole cache flush (arguments are ignored)
    403  * we emulate this broken beahior.
    404  */
    405 int
    406 linux_sys_cacheflush(l, v, retval)
    407 	struct lwp *l;
    408 	void *v;
    409 	register_t *retval;
    410 {
    411 	mips_icache_sync_all();
    412 	mips_dcache_wbinv_all();
    413 	return 0;
    414 }
    415 
    416 /*
    417  * This system call is depecated in Linux, but
    418  * some binaries and some libraries use it.
    419  */
    420 int
    421 linux_sys_sysmips(l, v, retval)
    422 	struct lwp *l;
    423 	void *v;
    424 	register_t *retval;
    425 {
    426 	struct linux_sys_sysmips_args {
    427 		syscallarg(int) cmd;
    428 		syscallarg(int) arg1;
    429 		syscallarg(int) arg2;
    430 		syscallarg(int) arg3;
    431 	} *uap = v;
    432 	int error;
    433 
    434 	switch (SCARG(uap, cmd)) {
    435 	case LINUX_SETNAME: {
    436 		char nodename [LINUX___NEW_UTS_LEN + 1];
    437 		int name[2];
    438 		size_t len;
    439 
    440 		if ((error = kauth_authorize_generic(l->l_cred,
    441 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
    442 			return error;
    443 		if ((error = copyinstr((char *)SCARG(uap, arg1), nodename,
    444 		    LINUX___NEW_UTS_LEN, &len)) != 0)
    445 			return error;
    446 
    447 		name[0] = CTL_KERN;
    448 		name[1] = KERN_HOSTNAME;
    449 		return (old_sysctl(&name[0], 2, 0, 0, nodename, len, NULL));
    450 
    451 		break;
    452 	}
    453 	case LINUX_MIPS_ATOMIC_SET: {
    454 		void *addr;
    455 		int s;
    456 		u_int8_t value = 0;
    457 
    458 		addr = (void *)SCARG(uap, arg1);
    459 
    460 		s = splhigh();
    461 		/*
    462 		 * No error testing here. This is bad, but Linux does
    463 		 * it like this. The source aknowledge "This is broken"
    464 		 * in a comment...
    465 		 */
    466 		(void) copyin(addr, &value, 1);
    467 		*retval = value;
    468 		value = (u_int8_t) SCARG(uap, arg2);
    469 		error = copyout(&value, addr, 1);
    470 		splx(s);
    471 
    472 		return 0;
    473 		break;
    474 	}
    475 	case LINUX_MIPS_FIXADE:		/* XXX not implemented */
    476 		break;
    477 	case LINUX_FLUSH_CACHE:
    478 		mips_icache_sync_all();
    479 		mips_dcache_wbinv_all();
    480 		break;
    481 	case LINUX_MIPS_RDNVRAM:
    482 		return EIO;
    483 		break;
    484 	default:
    485 		return EINVAL;
    486 		break;
    487 	}
    488 #ifdef DEBUG_LINUX
    489 	printf("linux_sys_sysmips(): unimplemented command %d\n",
    490 	    SCARG(uap,cmd));
    491 #endif /* DEBUG_LINUX */
    492 	return 0;
    493 }
    494 
    495 int
    496 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    497 {
    498 	return 0;
    499 }
    500