dsemul.c 5.1 KB

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