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