dsemul.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/system.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/fpu_emulator.h>
  17. #include "ieee754.h"
  18. #include "dsemul.h"
  19. /* Strap kernel emulator for full MIPS IV emulation */
  20. #ifdef __mips
  21. #undef __mips
  22. #endif
  23. #define __mips 4
  24. /*
  25. * Emulate the arbritrary instruction ir at xcp->cp0_epc. Required when
  26. * we have to emulate the instruction in a COP1 branch delay slot. Do
  27. * not change cp0_epc due to the instruction
  28. *
  29. * According to the spec:
  30. * 1) it shouldnt be a branch :-)
  31. * 2) it can be a COP instruction :-(
  32. * 3) if we are tring to run a protected memory space we must take
  33. * special care on memory access instructions :-(
  34. */
  35. /*
  36. * "Trampoline" return routine to catch exception following
  37. * execution of delay-slot instruction execution.
  38. */
  39. struct emuframe {
  40. mips_instruction emul;
  41. mips_instruction badinst;
  42. mips_instruction cookie;
  43. unsigned long epc;
  44. };
  45. int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)
  46. {
  47. extern asmlinkage void handle_dsemulret(void);
  48. struct emuframe __user *fr;
  49. int err;
  50. if (ir == 0) { /* a 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. err = __put_user(ir, &fr->emul);
  82. err |= __put_user((mips_instruction)BADINST, &fr->badinst);
  83. err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
  84. err |= __put_user(cpc, &fr->epc);
  85. if (unlikely(err)) {
  86. fpuemustats.errors++;
  87. return SIGBUS;
  88. }
  89. regs->cp0_epc = (unsigned long) &fr->emul;
  90. flush_cache_sigtramp((unsigned long)&fr->badinst);
  91. return SIGILL; /* force out of emulation loop */
  92. }
  93. int do_dsemulret(struct pt_regs *xcp)
  94. {
  95. struct emuframe __user *fr;
  96. unsigned long epc;
  97. u32 insn, cookie;
  98. int err = 0;
  99. fr = (struct emuframe __user *)
  100. (xcp->cp0_epc - sizeof(mips_instruction));
  101. /*
  102. * If we can't even access the area, something is very wrong, but we'll
  103. * leave that to the default handling
  104. */
  105. if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
  106. return 0;
  107. /*
  108. * Do some sanity checking on the stackframe:
  109. *
  110. * - Is the instruction pointed to by the EPC an BADINST?
  111. * - Is the following memory word the BD_COOKIE?
  112. */
  113. err = __get_user(insn, &fr->badinst);
  114. err |= __get_user(cookie, &fr->cookie);
  115. if (unlikely(err || (insn != BADINST) || (cookie != BD_COOKIE))) {
  116. fpuemustats.errors++;
  117. return 0;
  118. }
  119. /*
  120. * At this point, we are satisfied that it's a BD emulation trap. Yes,
  121. * a user might have deliberately put two malformed and useless
  122. * instructions in a row in his program, in which case he's in for a
  123. * nasty surprise - the next instruction will be treated as a
  124. * continuation address! Alas, this seems to be the only way that we
  125. * can handle signals, recursion, and longjmps() in the context of
  126. * emulating the branch delay instruction.
  127. */
  128. #ifdef DSEMUL_TRACE
  129. printk("dsemulret\n");
  130. #endif
  131. if (__get_user(epc, &fr->epc)) { /* Saved EPC */
  132. /* This is not a good situation to be in */
  133. force_sig(SIGBUS, current);
  134. return 0;
  135. }
  136. /* Set EPC to return to post-branch instruction */
  137. xcp->cp0_epc = epc;
  138. return 1;
  139. }