dsemul.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <asm/branch.h>
  2. #include <asm/cacheflush.h>
  3. #include <asm/fpu_emulator.h>
  4. #include <asm/inst.h>
  5. #include <asm/mipsregs.h>
  6. #include <asm/uaccess.h>
  7. #include "ieee754.h"
  8. /*
  9. * Emulate the arbritrary instruction ir at xcp->cp0_epc. Required when
  10. * we have to emulate the instruction in a COP1 branch delay slot. Do
  11. * not change cp0_epc due to the instruction
  12. *
  13. * According to the spec:
  14. * 1) it shouldn't be a branch :-)
  15. * 2) it can be a COP instruction :-(
  16. * 3) if we are tring to run a protected memory space we must take
  17. * special care on memory access instructions :-(
  18. */
  19. /*
  20. * "Trampoline" return routine to catch exception following
  21. * execution of delay-slot instruction execution.
  22. */
  23. struct emuframe {
  24. mips_instruction emul;
  25. mips_instruction badinst;
  26. mips_instruction cookie;
  27. unsigned long epc;
  28. };
  29. /*
  30. * Set up an emulation frame for instruction IR, from a delay slot of
  31. * a branch jumping to CPC. Return 0 if successful, -1 if no emulation
  32. * required, otherwise a signal number causing a frame setup failure.
  33. */
  34. int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)
  35. {
  36. int isa16 = get_isa16_mode(regs->cp0_epc);
  37. mips_instruction break_math;
  38. struct emuframe __user *fr;
  39. int err;
  40. /* NOP is easy */
  41. if (ir == 0)
  42. return -1;
  43. /* microMIPS instructions */
  44. if (isa16) {
  45. union mips_instruction insn = { .word = ir };
  46. /* NOP16 aka MOVE16 $0, $0 */
  47. if ((ir >> 16) == MM_NOP16)
  48. return -1;
  49. /* ADDIUPC */
  50. if (insn.mm_a_format.opcode == mm_addiupc_op) {
  51. unsigned int rs;
  52. s32 v;
  53. rs = (((insn.mm_a_format.rs + 0x1e) & 0xf) + 2);
  54. v = regs->cp0_epc & ~3;
  55. v += insn.mm_a_format.simmediate << 2;
  56. regs->regs[rs] = (long)v;
  57. return -1;
  58. }
  59. }
  60. pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc);
  61. /*
  62. * The strategy is to push the instruction onto the user stack
  63. * and put a trap after it which we can catch and jump to
  64. * the required address any alternative apart from full
  65. * instruction emulation!!.
  66. *
  67. * Algorithmics used a system call instruction, and
  68. * borrowed that vector. MIPS/Linux version is a bit
  69. * more heavyweight in the interests of portability and
  70. * multiprocessor support. For Linux we use a BREAK 514
  71. * instruction causing a breakpoint exception.
  72. */
  73. break_math = BREAK_MATH(isa16);
  74. /* Ensure that the two instructions are in the same cache line */
  75. fr = (struct emuframe __user *)
  76. ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);
  77. /* Verify that the stack pointer is not competely insane */
  78. if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))
  79. return SIGBUS;
  80. if (isa16) {
  81. err = __put_user(ir >> 16,
  82. (u16 __user *)(&fr->emul));
  83. err |= __put_user(ir & 0xffff,
  84. (u16 __user *)((long)(&fr->emul) + 2));
  85. err |= __put_user(break_math >> 16,
  86. (u16 __user *)(&fr->badinst));
  87. err |= __put_user(break_math & 0xffff,
  88. (u16 __user *)((long)(&fr->badinst) + 2));
  89. } else {
  90. err = __put_user(ir, &fr->emul);
  91. err |= __put_user(break_math, &fr->badinst);
  92. }
  93. err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
  94. err |= __put_user(cpc, &fr->epc);
  95. if (unlikely(err)) {
  96. MIPS_FPU_EMU_INC_STATS(errors);
  97. return SIGBUS;
  98. }
  99. regs->cp0_epc = (unsigned long)&fr->emul | isa16;
  100. flush_cache_sigtramp((unsigned long)&fr->emul);
  101. return 0;
  102. }
  103. int do_dsemulret(struct pt_regs *xcp)
  104. {
  105. int isa16 = get_isa16_mode(xcp->cp0_epc);
  106. struct emuframe __user *fr;
  107. unsigned long epc;
  108. u32 insn, cookie;
  109. int err = 0;
  110. u16 instr[2];
  111. fr = (struct emuframe __user *)
  112. (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction));
  113. /*
  114. * If we can't even access the area, something is very wrong, but we'll
  115. * leave that to the default handling
  116. */
  117. if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
  118. return 0;
  119. /*
  120. * Do some sanity checking on the stackframe:
  121. *
  122. * - Is the instruction pointed to by the EPC an BREAK_MATH?
  123. * - Is the following memory word the BD_COOKIE?
  124. */
  125. if (isa16) {
  126. err = __get_user(instr[0],
  127. (u16 __user *)(&fr->badinst));
  128. err |= __get_user(instr[1],
  129. (u16 __user *)((long)(&fr->badinst) + 2));
  130. insn = (instr[0] << 16) | instr[1];
  131. } else {
  132. err = __get_user(insn, &fr->badinst);
  133. }
  134. err |= __get_user(cookie, &fr->cookie);
  135. if (unlikely(err ||
  136. insn != BREAK_MATH(isa16) || cookie != BD_COOKIE)) {
  137. MIPS_FPU_EMU_INC_STATS(errors);
  138. return 0;
  139. }
  140. /*
  141. * At this point, we are satisfied that it's a BD emulation trap. Yes,
  142. * a user might have deliberately put two malformed and useless
  143. * instructions in a row in his program, in which case he's in for a
  144. * nasty surprise - the next instruction will be treated as a
  145. * continuation address! Alas, this seems to be the only way that we
  146. * can handle signals, recursion, and longjmps() in the context of
  147. * emulating the branch delay instruction.
  148. */
  149. pr_debug("dsemulret\n");
  150. if (__get_user(epc, &fr->epc)) { /* Saved EPC */
  151. /* This is not a good situation to be in */
  152. force_sig(SIGBUS, current);
  153. return 0;
  154. }
  155. /* Set EPC to return to post-branch instruction */
  156. xcp->cp0_epc = epc;
  157. MIPS_FPU_EMU_INC_STATS(ds_emul);
  158. return 1;
  159. }