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