dsemul.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)
  30. {
  31. struct emuframe __user *fr;
  32. int err;
  33. if ((get_isa16_mode(regs->cp0_epc) && ((ir >> 16) == MM_NOP16)) ||
  34. (ir == 0)) {
  35. /* NOP is easy */
  36. regs->cp0_epc = cpc;
  37. clear_delay_slot(regs);
  38. return 0;
  39. }
  40. pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc);
  41. /*
  42. * The strategy is to push the instruction onto the user stack
  43. * and put a trap after it which we can catch and jump to
  44. * the required address any alternative apart from full
  45. * instruction emulation!!.
  46. *
  47. * Algorithmics used a system call instruction, and
  48. * borrowed that vector. MIPS/Linux version is a bit
  49. * more heavyweight in the interests of portability and
  50. * multiprocessor support. For Linux we generate a
  51. * an unaligned access and force an address error exception.
  52. *
  53. * For embedded systems (stand-alone) we prefer to use a
  54. * non-existing CP1 instruction. This prevents us from emulating
  55. * branches, but gives us a cleaner interface to the exception
  56. * handler (single entry point).
  57. */
  58. /* Ensure that the two instructions are in the same cache line */
  59. fr = (struct emuframe __user *)
  60. ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);
  61. /* Verify that the stack pointer is not competely insane */
  62. if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))
  63. return SIGBUS;
  64. if (get_isa16_mode(regs->cp0_epc)) {
  65. err = __put_user(ir >> 16, (u16 __user *)(&fr->emul));
  66. err |= __put_user(ir & 0xffff, (u16 __user *)((long)(&fr->emul) + 2));
  67. err |= __put_user(BREAK_MATH >> 16, (u16 __user *)(&fr->badinst));
  68. err |= __put_user(BREAK_MATH & 0xffff, (u16 __user *)((long)(&fr->badinst) + 2));
  69. } else {
  70. err = __put_user(ir, &fr->emul);
  71. err |= __put_user((mips_instruction)BREAK_MATH, &fr->badinst);
  72. }
  73. err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
  74. err |= __put_user(cpc, &fr->epc);
  75. if (unlikely(err)) {
  76. MIPS_FPU_EMU_INC_STATS(errors);
  77. return SIGBUS;
  78. }
  79. regs->cp0_epc = ((unsigned long) &fr->emul) |
  80. get_isa16_mode(regs->cp0_epc);
  81. flush_cache_sigtramp((unsigned long)&fr->emul);
  82. return 0;
  83. }
  84. int do_dsemulret(struct pt_regs *xcp)
  85. {
  86. struct emuframe __user *fr;
  87. unsigned long epc;
  88. u32 insn, cookie;
  89. int err = 0;
  90. u16 instr[2];
  91. fr = (struct emuframe __user *)
  92. (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction));
  93. /*
  94. * If we can't even access the area, something is very wrong, but we'll
  95. * leave that to the default handling
  96. */
  97. if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
  98. return 0;
  99. /*
  100. * Do some sanity checking on the stackframe:
  101. *
  102. * - Is the instruction pointed to by the EPC an BREAK_MATH?
  103. * - Is the following memory word the BD_COOKIE?
  104. */
  105. if (get_isa16_mode(xcp->cp0_epc)) {
  106. err = __get_user(instr[0], (u16 __user *)(&fr->badinst));
  107. err |= __get_user(instr[1], (u16 __user *)((long)(&fr->badinst) + 2));
  108. insn = (instr[0] << 16) | instr[1];
  109. } else {
  110. err = __get_user(insn, &fr->badinst);
  111. }
  112. err |= __get_user(cookie, &fr->cookie);
  113. if (unlikely(err || (insn != BREAK_MATH) || (cookie != BD_COOKIE))) {
  114. MIPS_FPU_EMU_INC_STATS(errors);
  115. return 0;
  116. }
  117. /*
  118. * At this point, we are satisfied that it's a BD emulation trap. Yes,
  119. * a user might have deliberately put two malformed and useless
  120. * instructions in a row in his program, in which case he's in for a
  121. * nasty surprise - the next instruction will be treated as a
  122. * continuation address! Alas, this seems to be the only way that we
  123. * can handle signals, recursion, and longjmps() in the context of
  124. * emulating the branch delay instruction.
  125. */
  126. pr_debug("dsemulret\n");
  127. if (__get_user(epc, &fr->epc)) { /* Saved EPC */
  128. /* This is not a good situation to be in */
  129. force_sig(SIGBUS, current);
  130. return 0;
  131. }
  132. /* Set EPC to return to post-branch instruction */
  133. xcp->cp0_epc = epc;
  134. MIPS_FPU_EMU_INC_STATS(ds_emul);
  135. return 1;
  136. }