Home | History | Annotate | Line # | Download | only in mips
linux_machdep.c revision 1.1.2.2
      1 /*	$NetBSD: linux_machdep.c,v 1.1.2.2 2001/09/26 19:54:46 nathanw 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/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/signalvar.h>
     42 #include <sys/kernel.h>
     43 #include <sys/map.h>
     44 #include <sys/proc.h>
     45 #include <sys/user.h>
     46 #include <sys/buf.h>
     47 #include <sys/reboot.h>
     48 #include <sys/conf.h>
     49 #include <sys/exec.h>
     50 #include <sys/file.h>
     51 #include <sys/callout.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/msgbuf.h>
     55 #include <sys/mount.h>
     56 #include <sys/vnode.h>
     57 #include <sys/device.h>
     58 #include <sys/syscallargs.h>
     59 #include <sys/filedesc.h>
     60 #include <sys/exec_elf.h>
     61 #include <sys/disklabel.h>
     62 #include <sys/ioctl.h>
     63 #include <miscfs/specfs/specdev.h>
     64 
     65 #include <compat/linux/common/linux_types.h>
     66 #include <compat/linux/common/linux_signal.h>
     67 #include <compat/linux/common/linux_util.h>
     68 #include <compat/linux/common/linux_ioctl.h>
     69 #include <compat/linux/common/linux_hdio.h>
     70 #include <compat/linux/common/linux_exec.h>
     71 #include <compat/linux/common/linux_machdep.h>
     72 
     73 #include <compat/linux/linux_syscallargs.h>
     74 
     75 #include <machine/cpu.h>
     76 #include <machine/psl.h>
     77 #include <machine/reg.h>
     78 #include <machine/regnum.h>
     79 #include <machine/vmparam.h>
     80 #include <machine/locore.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(p, pack, stack)
    100 	struct proc *p;
    101 	struct exec_package *pack;
    102 	u_long stack;
    103 {
    104 	setregs(p, pack, stack);
    105 	return;
    106 }
    107 
    108 /*
    109  * Send an interrupt to process.
    110  *
    111  * Adapted from sys/arch/mips/mips/mips_machdep.c
    112  *
    113  * XXX Does not work well yet with RT signals
    114  *
    115  */
    116 
    117 void
    118 linux_sendsig(catcher, sig, mask, code)  /* XXX Check me */
    119 	sig_t catcher;
    120 	int sig;
    121 	sigset_t *mask;
    122 	u_long code;
    123 {
    124 	struct proc *p = curproc;
    125 	struct linux_sigframe *fp;
    126 	struct frame *f;
    127 	int i,onstack;
    128 	struct linux_sigframe sf;
    129 
    130 	printf("linux_sendsig()\n");
    131 	f = (struct frame *)p->p_md.md_regs;
    132 	printf("f = %p\n", f);
    133 
    134 	/*
    135 	 * Do we need to jump onto the signal stack?
    136 	 */
    137 	onstack =
    138 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    139 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    140 
    141 	/*
    142 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
    143 	 * not use it yet. XXX fix this.
    144 	 */
    145 	onstack=0;
    146 
    147 	/*
    148 	 * Allocate space for the signal handler context.
    149 	 */
    150 	if (onstack)
    151 		fp = (struct linux_sigframe *)
    152 		    ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
    153 		    + p->p_sigctx.ps_sigstk.ss_size);
    154 	else
    155 		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
    156 		fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[SP];
    157 
    158 	printf("fp = %p, sf = %p\n", fp, &sf);
    159 	/* Build stack frame for signal trampoline. */
    160 	memset(&sf, 0, sizeof sf);
    161 	sf.lsf_ass[0] = 0;			/* XXX */
    162 	sf.lsf_ass[1] = 0;
    163 	sf.lsf_code[0] = 0x24020000;	/* li	v0, __NR_sigreturn	*/
    164 	sf.lsf_code[0] = 0x0000000c;	/* syscall			*/
    165 	native_to_linux_sigset(mask, &sf.lsf_mask);
    166 	for (i=0; i<32; i++)
    167 		sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
    168 	sf.lsf_sc.lsc_mdhi = f->f_regs[MULHI];
    169 	sf.lsf_sc.lsc_mdlo = f->f_regs[MULLO];
    170 	sf.lsf_sc.lsc_pc = f->f_regs[PC];
    171 	sf.lsf_sc.lsc_status = 0;		/* XXX */
    172 	sf.lsf_sc.lsc_ownedfp = 0;		/* XXX */
    173 	sf.lsf_sc.lsc_fpc_csr = f->f_regs[SR];
    174 	sf.lsf_sc.lsc_fpc_eir = f->f_regs[RA];	/* XXX */
    175 	sf.lsf_sc.lsc_cause = f->f_regs[CAUSE];
    176 	sf.lsf_sc.lsc_badvaddr = f->f_regs[BADVADDR];	/* XXX */
    177 
    178 
    179 	/*
    180 	 * Save signal stack.  XXX broken
    181 	 */
    182 	/* kregs.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; */
    183 
    184 	/*
    185 	 * Install the sigframe onto the stack
    186 	 */
    187 	fp -= sizeof(struct linux_sigframe);
    188 	if (copyout(&sf, fp, sizeof(sf)) != 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 	/* Set up the registers to return to sigcode. */
    198 	f->f_regs[A0] = native_to_linux_sig[sig];
    199 	f->f_regs[A1] = 0;
    200 	f->f_regs[A2] = (int)&fp->lsf_sc;
    201 
    202 	f->f_regs[SP] = (int)fp;
    203 	f->f_regs[RA] = (int)fp->lsf_code;
    204 	f->f_regs[T9] = (int)catcher;
    205 	f->f_regs[PC] = (int)catcher;
    206 
    207 	/* Signal trampoline code is at base of user stack. */
    208 
    209 	/* Remember that we're now on the signal stack. */
    210 	if (onstack)
    211 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    212 
    213 	return;
    214 }
    215 
    216 /*
    217  * System call to cleanup state after a signal
    218  * has been taken.  Reset signal mask and
    219  * stack state from context left by sendsig (above).
    220  */
    221 int
    222 linux_sys_sigreturn(p, v, retval)
    223 	struct proc *p;
    224 	void *v;
    225 	register_t *retval;
    226 {
    227 	struct linux_sys_sigreturn_args /* {
    228 		syscallarg(struct linux_pt_regs) regs;
    229 	} */ *uap = v;
    230 	struct linux_pt_regs regs, kregs;
    231 	struct linux_sigframe *sf, ksf;
    232 	struct frame *f;
    233 	sigset_t mask;
    234 	int i, error;
    235 
    236 	printf("linux_sys_sigreturn()\n");
    237 	/*
    238 	 * The trampoline code hands us the context.
    239 	 * It is unsafe to keep track of it ourselves, in the event that a
    240 	 * program jumps out of a signal handler.
    241 	 */
    242 	regs = SCARG(uap, regs);
    243 
    244 	kregs = regs;
    245 	/* if ((error = copyin(regs, &kregs, sizeof(kregs))) != 0)
    246 		return (error); */
    247 
    248 	sf = (struct linux_sigframe *)kregs.lregs[29];
    249 	if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
    250 		return (error);
    251 
    252 	/* Restore the register context. */
    253 	f = (struct frame *)p->p_md.md_regs;
    254 	printf("sf = %p, f = %p\n", sf, f);
    255 	for (i=0; i<32; i++)
    256 		f->f_regs[i] = kregs.lregs[i];
    257 	f->f_regs[MULLO] = kregs.llo;
    258 	f->f_regs[MULHI] = kregs.lhi;
    259 	f->f_regs[PC] = kregs.lcp0_spc;
    260 	f->f_regs[BADVADDR] = kregs.lcp0_badvaddr;
    261 	f->f_regs[CAUSE] = kregs.lcp0_cause;
    262 
    263 	/* Restore signal stack. */
    264 	p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    265 
    266 	/* Restore signal mask. */
    267 	linux_to_native_sigset((linux_sigset_t *)&ksf.lsf_mask, &mask);
    268 	(void)sigprocmask1(p, SIG_SETMASK, &mask, 0);
    269 
    270 	return (EJUSTRETURN);
    271 }
    272 
    273 
    274 int
    275 linux_sys_rt_sigreturn(p, v, retval)
    276 	struct proc *p;
    277 	void *v;
    278 	register_t *retval;
    279 {
    280 	return 0;
    281 }
    282 
    283 
    284 #if 0
    285 int
    286 linux_sys_modify_ldt(p, v, retval)
    287 	struct proc *p;
    288 	void *v;
    289 	register_t *retval;
    290 {
    291 	/*
    292 	 * This syscall is not implemented in Linux/Mips: we should not
    293 	 * be here
    294 	 */
    295 #ifdef DEBUG_LINUX
    296 	printf("linux_sys_modify_ldt: should not be here.\n");
    297 #endif
    298   return 0;
    299 }
    300 #endif
    301 
    302 /*
    303  * major device numbers remapping
    304  */
    305 dev_t
    306 linux_fakedev(dev)
    307 	dev_t dev;
    308 {
    309   /* XXX write me */
    310   return dev;
    311 }
    312 
    313 /*
    314  * We come here in a last attempt to satisfy a Linux ioctl() call
    315  */
    316 int
    317 linux_machdepioctl(p, v, retval)
    318 	struct proc *p;
    319 	void *v;
    320 	register_t *retval;
    321 {
    322 	return 0;
    323 }
    324 
    325 /*
    326  * See above. If a root process tries to set access to an I/O port,
    327  * just let it have the whole range.
    328  */
    329 int
    330 linux_sys_ioperm(p, v, retval)
    331 	struct proc *p;
    332 	void *v;
    333 	register_t *retval;
    334 {
    335 	/*
    336 	 * This syscall is not implemented in Linux/Mips: we should not be here
    337 	 */
    338 #ifdef DEBUG_LINUX
    339 	printf("linux_sys_ioperm: should not be here.\n");
    340 #endif
    341 	return 0;
    342 }
    343 
    344 /*
    345  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    346  */
    347 int
    348 linux_sys_new_uname(p, v, retval)
    349 	struct proc *p;
    350 	void *v;
    351 	register_t *retval;
    352 {
    353 	return linux_sys_uname(p, v, retval);
    354 }
    355 
    356 /*
    357  * In Linux, cacheflush is icurrently mplemented
    358  * as a whole cache flush (arguments are ignored)
    359  * we emulate this broken beahior.
    360  */
    361 int
    362 linux_sys_cacheflush(p, v, retval)
    363 	struct proc *p;
    364 	void *v;
    365 	register_t *retval;
    366 {
    367 	MachFlushCache();
    368 	return 0;
    369 }
    370 
    371