dsemul.c 4.5 KB

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