11da177e4SLinus Torvalds #include <asm/branch.h> 21da177e4SLinus Torvalds #include <asm/cacheflush.h> 31da177e4SLinus Torvalds #include <asm/fpu_emulator.h> 4*cd8ee345SRalf Baechle #include <asm/inst.h> 5*cd8ee345SRalf Baechle #include <asm/mipsregs.h> 6*cd8ee345SRalf Baechle #include <asm/uaccess.h> 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include "ieee754.h" 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds /* Strap kernel emulator for full MIPS IV emulation */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #ifdef __mips 131da177e4SLinus Torvalds #undef __mips 141da177e4SLinus Torvalds #endif 151da177e4SLinus Torvalds #define __mips 4 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds /* 181da177e4SLinus Torvalds * Emulate the arbritrary instruction ir at xcp->cp0_epc. Required when 191da177e4SLinus Torvalds * we have to emulate the instruction in a COP1 branch delay slot. Do 201da177e4SLinus Torvalds * not change cp0_epc due to the instruction 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * According to the spec: 2325985edcSLucas De Marchi * 1) it shouldn't be a branch :-) 241da177e4SLinus Torvalds * 2) it can be a COP instruction :-( 251da177e4SLinus Torvalds * 3) if we are tring to run a protected memory space we must take 261da177e4SLinus Torvalds * special care on memory access instructions :-( 271da177e4SLinus Torvalds */ 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * "Trampoline" return routine to catch exception following 311da177e4SLinus Torvalds * execution of delay-slot instruction execution. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds struct emuframe { 351da177e4SLinus Torvalds mips_instruction emul; 361da177e4SLinus Torvalds mips_instruction badinst; 371da177e4SLinus Torvalds mips_instruction cookie; 38333d1f67SRalf Baechle unsigned long epc; 391da177e4SLinus Torvalds }; 401da177e4SLinus Torvalds 41333d1f67SRalf Baechle int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc) 421da177e4SLinus Torvalds { 431da177e4SLinus Torvalds extern asmlinkage void handle_dsemulret(void); 445e0373b8SAtsushi Nemoto struct emuframe __user *fr; 451da177e4SLinus Torvalds int err; 461da177e4SLinus Torvalds 47102cedc3SLeonid Yegoshin if ((get_isa16_mode(regs->cp0_epc) && ((ir >> 16) == MM_NOP16)) || 48102cedc3SLeonid Yegoshin (ir == 0)) { 49102cedc3SLeonid Yegoshin /* NOP is easy */ 501da177e4SLinus Torvalds regs->cp0_epc = cpc; 51e7e9cae5SRalf Baechle clear_delay_slot(regs); 521da177e4SLinus Torvalds return 0; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds #ifdef DSEMUL_TRACE 551da177e4SLinus Torvalds printk("dsemul %lx %lx\n", regs->cp0_epc, cpc); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds #endif 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * The strategy is to push the instruction onto the user stack 611da177e4SLinus Torvalds * and put a trap after it which we can catch and jump to 621da177e4SLinus Torvalds * the required address any alternative apart from full 631da177e4SLinus Torvalds * instruction emulation!!. 641da177e4SLinus Torvalds * 651da177e4SLinus Torvalds * Algorithmics used a system call instruction, and 661da177e4SLinus Torvalds * borrowed that vector. MIPS/Linux version is a bit 671da177e4SLinus Torvalds * more heavyweight in the interests of portability and 681da177e4SLinus Torvalds * multiprocessor support. For Linux we generate a 691da177e4SLinus Torvalds * an unaligned access and force an address error exception. 701da177e4SLinus Torvalds * 711da177e4SLinus Torvalds * For embedded systems (stand-alone) we prefer to use a 721da177e4SLinus Torvalds * non-existing CP1 instruction. This prevents us from emulating 731da177e4SLinus Torvalds * branches, but gives us a cleaner interface to the exception 741da177e4SLinus Torvalds * handler (single entry point). 751da177e4SLinus Torvalds */ 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* Ensure that the two instructions are in the same cache line */ 785e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 795e0373b8SAtsushi Nemoto ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* Verify that the stack pointer is not competely insane */ 821da177e4SLinus Torvalds if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe)))) 831da177e4SLinus Torvalds return SIGBUS; 841da177e4SLinus Torvalds 85102cedc3SLeonid Yegoshin if (get_isa16_mode(regs->cp0_epc)) { 86102cedc3SLeonid Yegoshin err = __put_user(ir >> 16, (u16 __user *)(&fr->emul)); 87102cedc3SLeonid Yegoshin err |= __put_user(ir & 0xffff, (u16 __user *)((long)(&fr->emul) + 2)); 88102cedc3SLeonid Yegoshin err |= __put_user(BREAK_MATH >> 16, (u16 __user *)(&fr->badinst)); 89102cedc3SLeonid Yegoshin err |= __put_user(BREAK_MATH & 0xffff, (u16 __user *)((long)(&fr->badinst) + 2)); 90102cedc3SLeonid Yegoshin } else { 911da177e4SLinus Torvalds err = __put_user(ir, &fr->emul); 92ba3049edSRalf Baechle err |= __put_user((mips_instruction)BREAK_MATH, &fr->badinst); 93102cedc3SLeonid Yegoshin } 94102cedc3SLeonid Yegoshin 951da177e4SLinus Torvalds err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie); 961da177e4SLinus Torvalds err |= __put_user(cpc, &fr->epc); 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds if (unlikely(err)) { 99b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1001da177e4SLinus Torvalds return SIGBUS; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 103102cedc3SLeonid Yegoshin regs->cp0_epc = ((unsigned long) &fr->emul) | 104102cedc3SLeonid Yegoshin get_isa16_mode(regs->cp0_epc); 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds flush_cache_sigtramp((unsigned long)&fr->badinst); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds return SIGILL; /* force out of emulation loop */ 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds int do_dsemulret(struct pt_regs *xcp) 1121da177e4SLinus Torvalds { 1135e0373b8SAtsushi Nemoto struct emuframe __user *fr; 114333d1f67SRalf Baechle unsigned long epc; 1151da177e4SLinus Torvalds u32 insn, cookie; 1161da177e4SLinus Torvalds int err = 0; 117102cedc3SLeonid Yegoshin u16 instr[2]; 1181da177e4SLinus Torvalds 1195e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 120102cedc3SLeonid Yegoshin (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction)); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* 1231da177e4SLinus Torvalds * If we can't even access the area, something is very wrong, but we'll 1241da177e4SLinus Torvalds * leave that to the default handling 1251da177e4SLinus Torvalds */ 1261da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe))) 1271da177e4SLinus Torvalds return 0; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * Do some sanity checking on the stackframe: 1311da177e4SLinus Torvalds * 132ba3049edSRalf Baechle * - Is the instruction pointed to by the EPC an BREAK_MATH? 1331da177e4SLinus Torvalds * - Is the following memory word the BD_COOKIE? 1341da177e4SLinus Torvalds */ 135102cedc3SLeonid Yegoshin if (get_isa16_mode(xcp->cp0_epc)) { 136102cedc3SLeonid Yegoshin err = __get_user(instr[0], (u16 __user *)(&fr->badinst)); 137102cedc3SLeonid Yegoshin err |= __get_user(instr[1], (u16 __user *)((long)(&fr->badinst) + 2)); 138102cedc3SLeonid Yegoshin insn = (instr[0] << 16) | instr[1]; 139102cedc3SLeonid Yegoshin } else { 1401da177e4SLinus Torvalds err = __get_user(insn, &fr->badinst); 141102cedc3SLeonid Yegoshin } 1421da177e4SLinus Torvalds err |= __get_user(cookie, &fr->cookie); 1431da177e4SLinus Torvalds 144ba3049edSRalf Baechle if (unlikely(err || (insn != BREAK_MATH) || (cookie != BD_COOKIE))) { 145b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1461da177e4SLinus Torvalds return 0; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * At this point, we are satisfied that it's a BD emulation trap. Yes, 1511da177e4SLinus Torvalds * a user might have deliberately put two malformed and useless 1521da177e4SLinus Torvalds * instructions in a row in his program, in which case he's in for a 1531da177e4SLinus Torvalds * nasty surprise - the next instruction will be treated as a 1541da177e4SLinus Torvalds * continuation address! Alas, this seems to be the only way that we 1551da177e4SLinus Torvalds * can handle signals, recursion, and longjmps() in the context of 1561da177e4SLinus Torvalds * emulating the branch delay instruction. 1571da177e4SLinus Torvalds */ 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds #ifdef DSEMUL_TRACE 1601da177e4SLinus Torvalds printk("dsemulret\n"); 1611da177e4SLinus Torvalds #endif 1621da177e4SLinus Torvalds if (__get_user(epc, &fr->epc)) { /* Saved EPC */ 1631da177e4SLinus Torvalds /* This is not a good situation to be in */ 1641da177e4SLinus Torvalds force_sig(SIGBUS, current); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds return 0; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Set EPC to return to post-branch instruction */ 1701da177e4SLinus Torvalds xcp->cp0_epc = epc; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds return 1; 1731da177e4SLinus Torvalds } 174