dsemul.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 (ir == 0) { /* a nop is easy */
  49. regs->cp0_epc = cpc;
  50. regs->cp0_cause &= ~CAUSEF_BD;
  51. return 0;
  52. }
  53. #ifdef DSEMUL_TRACE
  54. printk("dsemul %lx %lx\n", regs->cp0_epc, cpc);
  55. #endif
  56. /*
  57. * The strategy is to push the instruction onto the user stack
  58. * and put a trap after it which we can catch and jump to
  59. * the required address any alternative apart from full
  60. * instruction emulation!!.
  61. *
  62. * Algorithmics used a system call instruction, and
  63. * borrowed that vector. MIPS/Linux version is a bit
  64. * more heavyweight in the interests of portability and
  65. * multiprocessor support. For Linux we generate a
  66. * an unaligned access and force an address error exception.
  67. *
  68. * For embedded systems (stand-alone) we prefer to use a
  69. * non-existing CP1 instruction. This prevents us from emulating
  70. * branches, but gives us a cleaner interface to the exception
  71. * handler (single entry point).
  72. */
  73. /* Ensure that the two instructions are in the same cache line */
  74. fr = (struct emuframe __user *)
  75. ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);
  76. /* Verify that the stack pointer is not competely insane */
  77. if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))
  78. return SIGBUS;
  79. err = __put_user(ir, &fr->emul);
  80. err |= __put_user((mips_instruction)BREAK_MATH, &fr->badinst);
  81. err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
  82. err |= __put_user(cpc, &fr->epc);
  83. if (unlikely(err)) {
  84. MIPS_FPU_EMU_INC_STATS(errors);
  85. return SIGBUS;
  86. }
  87. regs->cp0_epc = (unsigned long) &fr->emul;
  88. flush_cache_sigtramp((unsigned long)&fr->badinst);
  89. return SIGILL; /* force out of emulation loop */
  90. }
  91. int do_dsemulret(struct pt_regs *xcp)
  92. {
  93. struct emuframe __user *fr;
  94. unsigned long epc;
  95. u32 insn, cookie;
  96. int err = 0;
  97. fr = (struct emuframe __user *)
  98. (xcp->cp0_epc - sizeof(mips_instruction));
  99. /*
  100. * If we can't even access the area, something is very wrong, but we'll
  101. * leave that to the default handling
  102. */
  103. if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
  104. return 0;
  105. /*
  106. * Do some sanity checking on the stackframe:
  107. *
  108. * - Is the instruction pointed to by the EPC an BREAK_MATH?
  109. * - Is the following memory word the BD_COOKIE?
  110. */
  111. err = __get_user(insn, &fr->badinst);
  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. #ifdef DSEMUL_TRACE
  127. printk("dsemulret\n");
  128. #endif
  129. if (__get_user(epc, &fr->epc)) { /* Saved EPC */
  130. /* This is not a good situation to be in */
  131. force_sig(SIGBUS, current);
  132. return 0;
  133. }
  134. /* Set EPC to return to post-branch instruction */
  135. xcp->cp0_epc = epc;
  136. return 1;
  137. }