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 { 415e0373b8SAtsushi Nemoto struct emuframe __user *fr; 421da177e4SLinus Torvalds int err; 431da177e4SLinus Torvalds 44102cedc3SLeonid Yegoshin /* NOP is easy */ 45e4553573SMaciej W. Rozycki if ((get_isa16_mode(regs->cp0_epc) && ((ir >> 16) == MM_NOP16)) || 46e4553573SMaciej W. Rozycki (ir == 0)) 47e4553573SMaciej W. Rozycki return -1; 481da177e4SLinus Torvalds 4992df0f8bSRalf Baechle pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc); 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds /* 521da177e4SLinus Torvalds * The strategy is to push the instruction onto the user stack 531da177e4SLinus Torvalds * and put a trap after it which we can catch and jump to 541da177e4SLinus Torvalds * the required address any alternative apart from full 551da177e4SLinus Torvalds * instruction emulation!!. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * Algorithmics used a system call instruction, and 581da177e4SLinus Torvalds * borrowed that vector. MIPS/Linux version is a bit 591da177e4SLinus Torvalds * more heavyweight in the interests of portability and 601da177e4SLinus Torvalds * multiprocessor support. For Linux we generate a 611da177e4SLinus Torvalds * an unaligned access and force an address error exception. 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * For embedded systems (stand-alone) we prefer to use a 641da177e4SLinus Torvalds * non-existing CP1 instruction. This prevents us from emulating 651da177e4SLinus Torvalds * branches, but gives us a cleaner interface to the exception 661da177e4SLinus Torvalds * handler (single entry point). 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* Ensure that the two instructions are in the same cache line */ 705e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 715e0373b8SAtsushi Nemoto ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* Verify that the stack pointer is not competely insane */ 741da177e4SLinus Torvalds if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe)))) 751da177e4SLinus Torvalds return SIGBUS; 761da177e4SLinus Torvalds 77102cedc3SLeonid Yegoshin if (get_isa16_mode(regs->cp0_epc)) { 78*a87265cfSMaciej W. Rozycki err = __put_user(ir >> 16, 79*a87265cfSMaciej W. Rozycki (u16 __user *)(&fr->emul)); 80*a87265cfSMaciej W. Rozycki err |= __put_user(ir & 0xffff, 81*a87265cfSMaciej W. Rozycki (u16 __user *)((long)(&fr->emul) + 2)); 82*a87265cfSMaciej W. Rozycki err |= __put_user(BREAK_MATH >> 16, 83*a87265cfSMaciej W. Rozycki (u16 __user *)(&fr->badinst)); 84*a87265cfSMaciej W. Rozycki err |= __put_user(BREAK_MATH & 0xffff, 85*a87265cfSMaciej W. Rozycki (u16 __user *)((long)(&fr->badinst) + 2)); 86102cedc3SLeonid Yegoshin } else { 871da177e4SLinus Torvalds err = __put_user(ir, &fr->emul); 88ba3049edSRalf Baechle err |= __put_user((mips_instruction)BREAK_MATH, &fr->badinst); 89102cedc3SLeonid Yegoshin } 90102cedc3SLeonid Yegoshin 911da177e4SLinus Torvalds err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie); 921da177e4SLinus Torvalds err |= __put_user(cpc, &fr->epc); 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds if (unlikely(err)) { 95b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 961da177e4SLinus Torvalds return SIGBUS; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 99102cedc3SLeonid Yegoshin regs->cp0_epc = ((unsigned long) &fr->emul) | 100102cedc3SLeonid Yegoshin get_isa16_mode(regs->cp0_epc); 1011da177e4SLinus Torvalds 1027737b20bSMaciej W. Rozycki flush_cache_sigtramp((unsigned long)&fr->emul); 1031da177e4SLinus Torvalds 1049ab4471cSMaciej W. Rozycki return 0; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds int do_dsemulret(struct pt_regs *xcp) 1081da177e4SLinus Torvalds { 1095e0373b8SAtsushi Nemoto struct emuframe __user *fr; 110333d1f67SRalf Baechle unsigned long epc; 1111da177e4SLinus Torvalds u32 insn, cookie; 1121da177e4SLinus Torvalds int err = 0; 113102cedc3SLeonid Yegoshin u16 instr[2]; 1141da177e4SLinus Torvalds 1155e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 116102cedc3SLeonid Yegoshin (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction)); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 1191da177e4SLinus Torvalds * If we can't even access the area, something is very wrong, but we'll 1201da177e4SLinus Torvalds * leave that to the default handling 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe))) 1231da177e4SLinus Torvalds return 0; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Do some sanity checking on the stackframe: 1271da177e4SLinus Torvalds * 128ba3049edSRalf Baechle * - Is the instruction pointed to by the EPC an BREAK_MATH? 1291da177e4SLinus Torvalds * - Is the following memory word the BD_COOKIE? 1301da177e4SLinus Torvalds */ 131102cedc3SLeonid Yegoshin if (get_isa16_mode(xcp->cp0_epc)) { 132*a87265cfSMaciej W. Rozycki err = __get_user(instr[0], 133*a87265cfSMaciej W. Rozycki (u16 __user *)(&fr->badinst)); 134*a87265cfSMaciej W. Rozycki err |= __get_user(instr[1], 135*a87265cfSMaciej W. Rozycki (u16 __user *)((long)(&fr->badinst) + 2)); 136102cedc3SLeonid Yegoshin insn = (instr[0] << 16) | instr[1]; 137102cedc3SLeonid Yegoshin } else { 1381da177e4SLinus Torvalds err = __get_user(insn, &fr->badinst); 139102cedc3SLeonid Yegoshin } 1401da177e4SLinus Torvalds err |= __get_user(cookie, &fr->cookie); 1411da177e4SLinus Torvalds 142ba3049edSRalf Baechle if (unlikely(err || (insn != BREAK_MATH) || (cookie != BD_COOKIE))) { 143b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1441da177e4SLinus Torvalds return 0; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * At this point, we are satisfied that it's a BD emulation trap. Yes, 1491da177e4SLinus Torvalds * a user might have deliberately put two malformed and useless 1501da177e4SLinus Torvalds * instructions in a row in his program, in which case he's in for a 1511da177e4SLinus Torvalds * nasty surprise - the next instruction will be treated as a 1521da177e4SLinus Torvalds * continuation address! Alas, this seems to be the only way that we 1531da177e4SLinus Torvalds * can handle signals, recursion, and longjmps() in the context of 1541da177e4SLinus Torvalds * emulating the branch delay instruction. 1551da177e4SLinus Torvalds */ 1561da177e4SLinus Torvalds 15792df0f8bSRalf Baechle pr_debug("dsemulret\n"); 15892df0f8bSRalf Baechle 1591da177e4SLinus Torvalds if (__get_user(epc, &fr->epc)) { /* Saved EPC */ 1601da177e4SLinus Torvalds /* This is not a good situation to be in */ 1611da177e4SLinus Torvalds force_sig(SIGBUS, current); 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds return 0; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* Set EPC to return to post-branch instruction */ 1671da177e4SLinus Torvalds xcp->cp0_epc = epc; 1682707cd29SDavid Daney MIPS_FPU_EMU_INC_STATS(ds_emul); 1691da177e4SLinus Torvalds return 1; 1701da177e4SLinus Torvalds } 171