11da177e4SLinus Torvalds #include <asm/branch.h> 21da177e4SLinus Torvalds #include <asm/cacheflush.h> 31da177e4SLinus Torvalds #include <asm/fpu_emulator.h> 4cd8ee345SRalf Baechle #include <asm/inst.h> 5cd8ee345SRalf Baechle #include <asm/mipsregs.h> 6cd8ee345SRalf Baechle #include <asm/uaccess.h> 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include "ieee754.h" 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds /* 111da177e4SLinus Torvalds * Emulate the arbritrary instruction ir at xcp->cp0_epc. Required when 121da177e4SLinus Torvalds * we have to emulate the instruction in a COP1 branch delay slot. Do 131da177e4SLinus Torvalds * not change cp0_epc due to the instruction 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * According to the spec: 1625985edcSLucas De Marchi * 1) it shouldn't be a branch :-) 171da177e4SLinus Torvalds * 2) it can be a COP instruction :-( 181da177e4SLinus Torvalds * 3) if we are tring to run a protected memory space we must take 191da177e4SLinus Torvalds * special care on memory access instructions :-( 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds /* 231da177e4SLinus Torvalds * "Trampoline" return routine to catch exception following 241da177e4SLinus Torvalds * execution of delay-slot instruction execution. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds struct emuframe { 281da177e4SLinus Torvalds mips_instruction emul; 291da177e4SLinus Torvalds mips_instruction badinst; 301da177e4SLinus Torvalds mips_instruction cookie; 31333d1f67SRalf Baechle unsigned long epc; 321da177e4SLinus Torvalds }; 331da177e4SLinus Torvalds 34e4553573SMaciej W. Rozycki /* 35e4553573SMaciej W. Rozycki * Set up an emulation frame for instruction IR, from a delay slot of 36e4553573SMaciej W. Rozycki * a branch jumping to CPC. Return 0 if successful, -1 if no emulation 37e4553573SMaciej W. Rozycki * required, otherwise a signal number causing a frame setup failure. 38e4553573SMaciej W. Rozycki */ 39333d1f67SRalf Baechle int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc) 401da177e4SLinus Torvalds { 41733b8bc1SMaciej W. Rozycki mips_instruction break_math; 425e0373b8SAtsushi Nemoto struct emuframe __user *fr; 431da177e4SLinus Torvalds int err; 441da177e4SLinus Torvalds 45102cedc3SLeonid Yegoshin /* NOP is easy */ 46*69a1e6cbSMaciej W. Rozycki if (ir == 0) 47e4553573SMaciej W. Rozycki return -1; 481da177e4SLinus Torvalds 49*69a1e6cbSMaciej W. Rozycki /* microMIPS instructions */ 50*69a1e6cbSMaciej W. Rozycki if (get_isa16_mode(regs->cp0_epc)) { 51*69a1e6cbSMaciej W. Rozycki union mips_instruction insn = { .word = ir }; 52*69a1e6cbSMaciej W. Rozycki 53*69a1e6cbSMaciej W. Rozycki /* NOP16 aka MOVE16 $0, $0 */ 54*69a1e6cbSMaciej W. Rozycki if ((ir >> 16) == MM_NOP16) 55*69a1e6cbSMaciej W. Rozycki return -1; 56*69a1e6cbSMaciej W. Rozycki 57*69a1e6cbSMaciej W. Rozycki /* ADDIUPC */ 58*69a1e6cbSMaciej W. Rozycki if (insn.mm_a_format.opcode == mm_addiupc_op) { 59*69a1e6cbSMaciej W. Rozycki unsigned int rs; 60*69a1e6cbSMaciej W. Rozycki s32 v; 61*69a1e6cbSMaciej W. Rozycki 62*69a1e6cbSMaciej W. Rozycki rs = (((insn.mm_a_format.rs + 0x1e) & 0xf) + 2); 63*69a1e6cbSMaciej W. Rozycki v = regs->cp0_epc & ~3; 64*69a1e6cbSMaciej W. Rozycki v += insn.mm_a_format.simmediate << 2; 65*69a1e6cbSMaciej W. Rozycki regs->regs[rs] = (long)v; 66*69a1e6cbSMaciej W. Rozycki return -1; 67*69a1e6cbSMaciej W. Rozycki } 68*69a1e6cbSMaciej W. Rozycki } 69*69a1e6cbSMaciej W. Rozycki 7092df0f8bSRalf Baechle pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc); 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* 731da177e4SLinus Torvalds * The strategy is to push the instruction onto the user stack 741da177e4SLinus Torvalds * and put a trap after it which we can catch and jump to 751da177e4SLinus Torvalds * the required address any alternative apart from full 761da177e4SLinus Torvalds * instruction emulation!!. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * Algorithmics used a system call instruction, and 791da177e4SLinus Torvalds * borrowed that vector. MIPS/Linux version is a bit 801da177e4SLinus Torvalds * more heavyweight in the interests of portability and 811da177e4SLinus Torvalds * multiprocessor support. For Linux we generate a 821da177e4SLinus Torvalds * an unaligned access and force an address error exception. 831da177e4SLinus Torvalds * 841da177e4SLinus Torvalds * For embedded systems (stand-alone) we prefer to use a 851da177e4SLinus Torvalds * non-existing CP1 instruction. This prevents us from emulating 861da177e4SLinus Torvalds * branches, but gives us a cleaner interface to the exception 871da177e4SLinus Torvalds * handler (single entry point). 881da177e4SLinus Torvalds */ 89733b8bc1SMaciej W. Rozycki break_math = BREAK_MATH(get_isa16_mode(regs->cp0_epc)); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* Ensure that the two instructions are in the same cache line */ 925e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 935e0373b8SAtsushi Nemoto ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /* Verify that the stack pointer is not competely insane */ 961da177e4SLinus Torvalds if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe)))) 971da177e4SLinus Torvalds return SIGBUS; 981da177e4SLinus Torvalds 99102cedc3SLeonid Yegoshin if (get_isa16_mode(regs->cp0_epc)) { 100a87265cfSMaciej W. Rozycki err = __put_user(ir >> 16, 101a87265cfSMaciej W. Rozycki (u16 __user *)(&fr->emul)); 102a87265cfSMaciej W. Rozycki err |= __put_user(ir & 0xffff, 103a87265cfSMaciej W. Rozycki (u16 __user *)((long)(&fr->emul) + 2)); 104733b8bc1SMaciej W. Rozycki err |= __put_user(break_math >> 16, 105a87265cfSMaciej W. Rozycki (u16 __user *)(&fr->badinst)); 106733b8bc1SMaciej W. Rozycki err |= __put_user(break_math & 0xffff, 107a87265cfSMaciej W. Rozycki (u16 __user *)((long)(&fr->badinst) + 2)); 108102cedc3SLeonid Yegoshin } else { 1091da177e4SLinus Torvalds err = __put_user(ir, &fr->emul); 110733b8bc1SMaciej W. Rozycki err |= __put_user(break_math, &fr->badinst); 111102cedc3SLeonid Yegoshin } 112102cedc3SLeonid Yegoshin 1131da177e4SLinus Torvalds err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie); 1141da177e4SLinus Torvalds err |= __put_user(cpc, &fr->epc); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (unlikely(err)) { 117b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1181da177e4SLinus Torvalds return SIGBUS; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 121102cedc3SLeonid Yegoshin regs->cp0_epc = ((unsigned long) &fr->emul) | 122102cedc3SLeonid Yegoshin get_isa16_mode(regs->cp0_epc); 1231da177e4SLinus Torvalds 1247737b20bSMaciej W. Rozycki flush_cache_sigtramp((unsigned long)&fr->emul); 1251da177e4SLinus Torvalds 1269ab4471cSMaciej W. Rozycki return 0; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds int do_dsemulret(struct pt_regs *xcp) 1301da177e4SLinus Torvalds { 1315e0373b8SAtsushi Nemoto struct emuframe __user *fr; 132333d1f67SRalf Baechle unsigned long epc; 1331da177e4SLinus Torvalds u32 insn, cookie; 1341da177e4SLinus Torvalds int err = 0; 135102cedc3SLeonid Yegoshin u16 instr[2]; 1361da177e4SLinus Torvalds 1375e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 138102cedc3SLeonid Yegoshin (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction)); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* 1411da177e4SLinus Torvalds * If we can't even access the area, something is very wrong, but we'll 1421da177e4SLinus Torvalds * leave that to the default handling 1431da177e4SLinus Torvalds */ 1441da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe))) 1451da177e4SLinus Torvalds return 0; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Do some sanity checking on the stackframe: 1491da177e4SLinus Torvalds * 150ba3049edSRalf Baechle * - Is the instruction pointed to by the EPC an BREAK_MATH? 1511da177e4SLinus Torvalds * - Is the following memory word the BD_COOKIE? 1521da177e4SLinus Torvalds */ 153102cedc3SLeonid Yegoshin if (get_isa16_mode(xcp->cp0_epc)) { 154a87265cfSMaciej W. Rozycki err = __get_user(instr[0], 155a87265cfSMaciej W. Rozycki (u16 __user *)(&fr->badinst)); 156a87265cfSMaciej W. Rozycki err |= __get_user(instr[1], 157a87265cfSMaciej W. Rozycki (u16 __user *)((long)(&fr->badinst) + 2)); 158102cedc3SLeonid Yegoshin insn = (instr[0] << 16) | instr[1]; 159102cedc3SLeonid Yegoshin } else { 1601da177e4SLinus Torvalds err = __get_user(insn, &fr->badinst); 161102cedc3SLeonid Yegoshin } 1621da177e4SLinus Torvalds err |= __get_user(cookie, &fr->cookie); 1631da177e4SLinus Torvalds 164733b8bc1SMaciej W. Rozycki if (unlikely(err || insn != BREAK_MATH(get_isa16_mode(xcp->cp0_epc)) || 165733b8bc1SMaciej W. Rozycki cookie != BD_COOKIE)) { 166b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1671da177e4SLinus Torvalds return 0; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * At this point, we are satisfied that it's a BD emulation trap. Yes, 1721da177e4SLinus Torvalds * a user might have deliberately put two malformed and useless 1731da177e4SLinus Torvalds * instructions in a row in his program, in which case he's in for a 1741da177e4SLinus Torvalds * nasty surprise - the next instruction will be treated as a 1751da177e4SLinus Torvalds * continuation address! Alas, this seems to be the only way that we 1761da177e4SLinus Torvalds * can handle signals, recursion, and longjmps() in the context of 1771da177e4SLinus Torvalds * emulating the branch delay instruction. 1781da177e4SLinus Torvalds */ 1791da177e4SLinus Torvalds 18092df0f8bSRalf Baechle pr_debug("dsemulret\n"); 18192df0f8bSRalf Baechle 1821da177e4SLinus Torvalds if (__get_user(epc, &fr->epc)) { /* Saved EPC */ 1831da177e4SLinus Torvalds /* This is not a good situation to be in */ 1841da177e4SLinus Torvalds force_sig(SIGBUS, current); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds return 0; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* Set EPC to return to post-branch instruction */ 1901da177e4SLinus Torvalds xcp->cp0_epc = epc; 1912707cd29SDavid Daney MIPS_FPU_EMU_INC_STATS(ds_emul); 1921da177e4SLinus Torvalds return 1; 1931da177e4SLinus Torvalds } 194