branch.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996, 97, 2000, 2001 by Ralf Baechle
  7. * Copyright (C) 2001 MIPS Technologies, Inc.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/signal.h>
  12. #include <asm/branch.h>
  13. #include <asm/cpu.h>
  14. #include <asm/cpu-features.h>
  15. #include <asm/fpu.h>
  16. #include <asm/inst.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/uaccess.h>
  19. /*
  20. * Compute the return address and do emulate branch simulation, if required.
  21. */
  22. int __compute_return_epc(struct pt_regs *regs)
  23. {
  24. unsigned int *addr, bit, fcr31, dspcontrol;
  25. long epc;
  26. union mips_instruction insn;
  27. epc = regs->cp0_epc;
  28. if (epc & 3)
  29. goto unaligned;
  30. /*
  31. * Read the instruction
  32. */
  33. addr = (unsigned int *) epc;
  34. if (__get_user(insn.word, addr)) {
  35. force_sig(SIGSEGV, current);
  36. return -EFAULT;
  37. }
  38. regs->regs[0] = 0;
  39. switch (insn.i_format.opcode) {
  40. /*
  41. * jr and jalr are in r_format format.
  42. */
  43. case spec_op:
  44. switch (insn.r_format.func) {
  45. case jalr_op:
  46. regs->regs[insn.r_format.rd] = epc + 8;
  47. /* Fall through */
  48. case jr_op:
  49. regs->cp0_epc = regs->regs[insn.r_format.rs];
  50. break;
  51. }
  52. break;
  53. /*
  54. * This group contains:
  55. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  56. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  57. */
  58. case bcond_op:
  59. switch (insn.i_format.rt) {
  60. case bltz_op:
  61. case bltzl_op:
  62. if ((long)regs->regs[insn.i_format.rs] < 0)
  63. epc = epc + 4 + (insn.i_format.simmediate << 2);
  64. else
  65. epc += 8;
  66. regs->cp0_epc = epc;
  67. break;
  68. case bgez_op:
  69. case bgezl_op:
  70. if ((long)regs->regs[insn.i_format.rs] >= 0)
  71. epc = epc + 4 + (insn.i_format.simmediate << 2);
  72. else
  73. epc += 8;
  74. regs->cp0_epc = epc;
  75. break;
  76. case bltzal_op:
  77. case bltzall_op:
  78. regs->regs[31] = epc + 8;
  79. if ((long)regs->regs[insn.i_format.rs] < 0)
  80. epc = epc + 4 + (insn.i_format.simmediate << 2);
  81. else
  82. epc += 8;
  83. regs->cp0_epc = epc;
  84. break;
  85. case bgezal_op:
  86. case bgezall_op:
  87. regs->regs[31] = epc + 8;
  88. if ((long)regs->regs[insn.i_format.rs] >= 0)
  89. epc = epc + 4 + (insn.i_format.simmediate << 2);
  90. else
  91. epc += 8;
  92. regs->cp0_epc = epc;
  93. break;
  94. case bposge32_op:
  95. if (!cpu_has_dsp)
  96. goto sigill;
  97. dspcontrol = rddsp(0x01);
  98. if (dspcontrol >= 32) {
  99. epc = epc + 4 + (insn.i_format.simmediate << 2);
  100. } else
  101. epc += 8;
  102. regs->cp0_epc = epc;
  103. break;
  104. }
  105. break;
  106. /*
  107. * These are unconditional and in j_format.
  108. */
  109. case jal_op:
  110. regs->regs[31] = regs->cp0_epc + 8;
  111. case j_op:
  112. epc += 4;
  113. epc >>= 28;
  114. epc <<= 28;
  115. epc |= (insn.j_format.target << 2);
  116. regs->cp0_epc = epc;
  117. break;
  118. /*
  119. * These are conditional and in i_format.
  120. */
  121. case beq_op:
  122. case beql_op:
  123. if (regs->regs[insn.i_format.rs] ==
  124. regs->regs[insn.i_format.rt])
  125. epc = epc + 4 + (insn.i_format.simmediate << 2);
  126. else
  127. epc += 8;
  128. regs->cp0_epc = epc;
  129. break;
  130. case bne_op:
  131. case bnel_op:
  132. if (regs->regs[insn.i_format.rs] !=
  133. regs->regs[insn.i_format.rt])
  134. epc = epc + 4 + (insn.i_format.simmediate << 2);
  135. else
  136. epc += 8;
  137. regs->cp0_epc = epc;
  138. break;
  139. case blez_op: /* not really i_format */
  140. case blezl_op:
  141. /* rt field assumed to be zero */
  142. if ((long)regs->regs[insn.i_format.rs] <= 0)
  143. epc = epc + 4 + (insn.i_format.simmediate << 2);
  144. else
  145. epc += 8;
  146. regs->cp0_epc = epc;
  147. break;
  148. case bgtz_op:
  149. case bgtzl_op:
  150. /* rt field assumed to be zero */
  151. if ((long)regs->regs[insn.i_format.rs] > 0)
  152. epc = epc + 4 + (insn.i_format.simmediate << 2);
  153. else
  154. epc += 8;
  155. regs->cp0_epc = epc;
  156. break;
  157. /*
  158. * And now the FPA/cp1 branch instructions.
  159. */
  160. case cop1_op:
  161. preempt_disable();
  162. if (is_fpu_owner())
  163. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  164. else
  165. fcr31 = current->thread.fpu.hard.fcr31;
  166. preempt_enable();
  167. bit = (insn.i_format.rt >> 2);
  168. bit += (bit != 0);
  169. bit += 23;
  170. switch (insn.i_format.rt) {
  171. case 0: /* bc1f */
  172. case 2: /* bc1fl */
  173. if (~fcr31 & (1 << bit))
  174. epc = epc + 4 + (insn.i_format.simmediate << 2);
  175. else
  176. epc += 8;
  177. regs->cp0_epc = epc;
  178. break;
  179. case 1: /* bc1t */
  180. case 3: /* bc1tl */
  181. if (fcr31 & (1 << bit))
  182. epc = epc + 4 + (insn.i_format.simmediate << 2);
  183. else
  184. epc += 8;
  185. regs->cp0_epc = epc;
  186. break;
  187. }
  188. break;
  189. }
  190. return 0;
  191. unaligned:
  192. printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
  193. force_sig(SIGBUS, current);
  194. return -EFAULT;
  195. sigill:
  196. printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm);
  197. force_sig(SIGBUS, current);
  198. return -EFAULT;
  199. }