dsemul.c 4.7 KB

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