branch.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 __user *addr;
  25. unsigned int bit, fcr31, dspcontrol;
  26. long epc;
  27. union mips_instruction insn;
  28. epc = regs->cp0_epc;
  29. if (epc & 3)
  30. goto unaligned;
  31. /*
  32. * Read the instruction
  33. */
  34. addr = (unsigned int __user *) epc;
  35. if (__get_user(insn.word, addr)) {
  36. force_sig(SIGSEGV, current);
  37. return -EFAULT;
  38. }
  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.fcr31;
  166. preempt_enable();
  167. bit = (insn.i_format.rt >> 2);
  168. bit += (bit != 0);
  169. bit += 23;
  170. switch (insn.i_format.rt & 3) {
  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. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  190. case lwc2_op: /* This is bbit0 on Octeon */
  191. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  192. == 0)
  193. epc = epc + 4 + (insn.i_format.simmediate << 2);
  194. else
  195. epc += 8;
  196. regs->cp0_epc = epc;
  197. break;
  198. case ldc2_op: /* This is bbit032 on Octeon */
  199. if ((regs->regs[insn.i_format.rs] &
  200. (1ull<<(insn.i_format.rt+32))) == 0)
  201. epc = epc + 4 + (insn.i_format.simmediate << 2);
  202. else
  203. epc += 8;
  204. regs->cp0_epc = epc;
  205. break;
  206. case swc2_op: /* This is bbit1 on Octeon */
  207. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  208. epc = epc + 4 + (insn.i_format.simmediate << 2);
  209. else
  210. epc += 8;
  211. regs->cp0_epc = epc;
  212. break;
  213. case sdc2_op: /* This is bbit132 on Octeon */
  214. if (regs->regs[insn.i_format.rs] &
  215. (1ull<<(insn.i_format.rt+32)))
  216. epc = epc + 4 + (insn.i_format.simmediate << 2);
  217. else
  218. epc += 8;
  219. regs->cp0_epc = epc;
  220. break;
  221. #endif
  222. }
  223. return 0;
  224. unaligned:
  225. printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
  226. force_sig(SIGBUS, current);
  227. return -EFAULT;
  228. sigill:
  229. printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm);
  230. force_sig(SIGBUS, current);
  231. return -EFAULT;
  232. }