#include <linux/compiler.h>1#include <linux/mm.h>2#include <linux/signal.h>3#include <linux/smp.h>45#include <asm/asm.h>6#include <asm/bootinfo.h>7#include <asm/byteorder.h>8#include <asm/cpu.h>9#include <asm/inst.h>10#include <asm/processor.h>11#include <asm/uaccess.h>12#include <asm/branch.h>13#include <asm/mipsregs.h>14#include <asm/system.h>15#include <asm/cacheflush.h>1617#include <asm/fpu_emulator.h>1819#include "ieee754.h"2021/* Strap kernel emulator for full MIPS IV emulation */2223#ifdef __mips24#undef __mips25#endif26#define __mips 42728/*29* Emulate the arbritrary instruction ir at xcp->cp0_epc. Required when30* we have to emulate the instruction in a COP1 branch delay slot. Do31* not change cp0_epc due to the instruction32*33* According to the spec:34* 1) it shouldn't be a branch :-)35* 2) it can be a COP instruction :-(36* 3) if we are tring to run a protected memory space we must take37* special care on memory access instructions :-(38*/3940/*41* "Trampoline" return routine to catch exception following42* execution of delay-slot instruction execution.43*/4445struct emuframe {46mips_instruction emul;47mips_instruction badinst;48mips_instruction cookie;49unsigned long epc;50};5152int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)53{54extern asmlinkage void handle_dsemulret(void);55struct emuframe __user *fr;56int err;5758if (ir == 0) { /* a nop is easy */59regs->cp0_epc = cpc;60regs->cp0_cause &= ~CAUSEF_BD;61return 0;62}63#ifdef DSEMUL_TRACE64printk("dsemul %lx %lx\n", regs->cp0_epc, cpc);6566#endif6768/*69* The strategy is to push the instruction onto the user stack70* and put a trap after it which we can catch and jump to71* the required address any alternative apart from full72* instruction emulation!!.73*74* Algorithmics used a system call instruction, and75* borrowed that vector. MIPS/Linux version is a bit76* more heavyweight in the interests of portability and77* multiprocessor support. For Linux we generate a78* an unaligned access and force an address error exception.79*80* For embedded systems (stand-alone) we prefer to use a81* non-existing CP1 instruction. This prevents us from emulating82* branches, but gives us a cleaner interface to the exception83* handler (single entry point).84*/8586/* Ensure that the two instructions are in the same cache line */87fr = (struct emuframe __user *)88((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);8990/* Verify that the stack pointer is not competely insane */91if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))92return SIGBUS;9394err = __put_user(ir, &fr->emul);95err |= __put_user((mips_instruction)BREAK_MATH, &fr->badinst);96err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);97err |= __put_user(cpc, &fr->epc);9899if (unlikely(err)) {100MIPS_FPU_EMU_INC_STATS(errors);101return SIGBUS;102}103104regs->cp0_epc = (unsigned long) &fr->emul;105106flush_cache_sigtramp((unsigned long)&fr->badinst);107108return SIGILL; /* force out of emulation loop */109}110111int do_dsemulret(struct pt_regs *xcp)112{113struct emuframe __user *fr;114unsigned long epc;115u32 insn, cookie;116int err = 0;117118fr = (struct emuframe __user *)119(xcp->cp0_epc - sizeof(mips_instruction));120121/*122* If we can't even access the area, something is very wrong, but we'll123* leave that to the default handling124*/125if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))126return 0;127128/*129* Do some sanity checking on the stackframe:130*131* - Is the instruction pointed to by the EPC an BREAK_MATH?132* - Is the following memory word the BD_COOKIE?133*/134err = __get_user(insn, &fr->badinst);135err |= __get_user(cookie, &fr->cookie);136137if (unlikely(err || (insn != BREAK_MATH) || (cookie != BD_COOKIE))) {138MIPS_FPU_EMU_INC_STATS(errors);139return 0;140}141142/*143* At this point, we are satisfied that it's a BD emulation trap. Yes,144* a user might have deliberately put two malformed and useless145* instructions in a row in his program, in which case he's in for a146* nasty surprise - the next instruction will be treated as a147* continuation address! Alas, this seems to be the only way that we148* can handle signals, recursion, and longjmps() in the context of149* emulating the branch delay instruction.150*/151152#ifdef DSEMUL_TRACE153printk("dsemulret\n");154#endif155if (__get_user(epc, &fr->epc)) { /* Saved EPC */156/* This is not a good situation to be in */157force_sig(SIGBUS, current);158159return 0;160}161162/* Set EPC to return to post-branch instruction */163xcp->cp0_epc = epc;164165return 1;166}167168169