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 34*e4553573SMaciej W. Rozycki /* 35*e4553573SMaciej W. Rozycki * Set up an emulation frame for instruction IR, from a delay slot of 36*e4553573SMaciej W. Rozycki * a branch jumping to CPC. Return 0 if successful, -1 if no emulation 37*e4553573SMaciej W. Rozycki * required, otherwise a signal number causing a frame setup failure. 38*e4553573SMaciej 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 */ 45*e4553573SMaciej W. Rozycki if ((get_isa16_mode(regs->cp0_epc) && ((ir >> 16) == MM_NOP16)) || 46*e4553573SMaciej W. Rozycki (ir == 0)) 47*e4553573SMaciej 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)) { 78102cedc3SLeonid Yegoshin err = __put_user(ir >> 16, (u16 __user *)(&fr->emul)); 79102cedc3SLeonid Yegoshin err |= __put_user(ir & 0xffff, (u16 __user *)((long)(&fr->emul) + 2)); 80102cedc3SLeonid Yegoshin err |= __put_user(BREAK_MATH >> 16, (u16 __user *)(&fr->badinst)); 81102cedc3SLeonid Yegoshin err |= __put_user(BREAK_MATH & 0xffff, (u16 __user *)((long)(&fr->badinst) + 2)); 82102cedc3SLeonid Yegoshin } else { 831da177e4SLinus Torvalds err = __put_user(ir, &fr->emul); 84ba3049edSRalf Baechle err |= __put_user((mips_instruction)BREAK_MATH, &fr->badinst); 85102cedc3SLeonid Yegoshin } 86102cedc3SLeonid Yegoshin 871da177e4SLinus Torvalds err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie); 881da177e4SLinus Torvalds err |= __put_user(cpc, &fr->epc); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds if (unlikely(err)) { 91b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 921da177e4SLinus Torvalds return SIGBUS; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 95102cedc3SLeonid Yegoshin regs->cp0_epc = ((unsigned long) &fr->emul) | 96102cedc3SLeonid Yegoshin get_isa16_mode(regs->cp0_epc); 971da177e4SLinus Torvalds 987737b20bSMaciej W. Rozycki flush_cache_sigtramp((unsigned long)&fr->emul); 991da177e4SLinus Torvalds 1009ab4471cSMaciej W. Rozycki return 0; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds int do_dsemulret(struct pt_regs *xcp) 1041da177e4SLinus Torvalds { 1055e0373b8SAtsushi Nemoto struct emuframe __user *fr; 106333d1f67SRalf Baechle unsigned long epc; 1071da177e4SLinus Torvalds u32 insn, cookie; 1081da177e4SLinus Torvalds int err = 0; 109102cedc3SLeonid Yegoshin u16 instr[2]; 1101da177e4SLinus Torvalds 1115e0373b8SAtsushi Nemoto fr = (struct emuframe __user *) 112102cedc3SLeonid Yegoshin (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction)); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* 1151da177e4SLinus Torvalds * If we can't even access the area, something is very wrong, but we'll 1161da177e4SLinus Torvalds * leave that to the default handling 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe))) 1191da177e4SLinus Torvalds return 0; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * Do some sanity checking on the stackframe: 1231da177e4SLinus Torvalds * 124ba3049edSRalf Baechle * - Is the instruction pointed to by the EPC an BREAK_MATH? 1251da177e4SLinus Torvalds * - Is the following memory word the BD_COOKIE? 1261da177e4SLinus Torvalds */ 127102cedc3SLeonid Yegoshin if (get_isa16_mode(xcp->cp0_epc)) { 128102cedc3SLeonid Yegoshin err = __get_user(instr[0], (u16 __user *)(&fr->badinst)); 129102cedc3SLeonid Yegoshin err |= __get_user(instr[1], (u16 __user *)((long)(&fr->badinst) + 2)); 130102cedc3SLeonid Yegoshin insn = (instr[0] << 16) | instr[1]; 131102cedc3SLeonid Yegoshin } else { 1321da177e4SLinus Torvalds err = __get_user(insn, &fr->badinst); 133102cedc3SLeonid Yegoshin } 1341da177e4SLinus Torvalds err |= __get_user(cookie, &fr->cookie); 1351da177e4SLinus Torvalds 136ba3049edSRalf Baechle if (unlikely(err || (insn != BREAK_MATH) || (cookie != BD_COOKIE))) { 137b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1381da177e4SLinus Torvalds return 0; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * At this point, we are satisfied that it's a BD emulation trap. Yes, 1431da177e4SLinus Torvalds * a user might have deliberately put two malformed and useless 1441da177e4SLinus Torvalds * instructions in a row in his program, in which case he's in for a 1451da177e4SLinus Torvalds * nasty surprise - the next instruction will be treated as a 1461da177e4SLinus Torvalds * continuation address! Alas, this seems to be the only way that we 1471da177e4SLinus Torvalds * can handle signals, recursion, and longjmps() in the context of 1481da177e4SLinus Torvalds * emulating the branch delay instruction. 1491da177e4SLinus Torvalds */ 1501da177e4SLinus Torvalds 15192df0f8bSRalf Baechle pr_debug("dsemulret\n"); 15292df0f8bSRalf Baechle 1531da177e4SLinus Torvalds if (__get_user(epc, &fr->epc)) { /* Saved EPC */ 1541da177e4SLinus Torvalds /* This is not a good situation to be in */ 1551da177e4SLinus Torvalds force_sig(SIGBUS, current); 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds return 0; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* Set EPC to return to post-branch instruction */ 1611da177e4SLinus Torvalds xcp->cp0_epc = epc; 1622707cd29SDavid Daney MIPS_FPU_EMU_INC_STATS(ds_emul); 1631da177e4SLinus Torvalds return 1; 1641da177e4SLinus Torvalds } 165