dsemul.c 4.5 KB

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