branch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 <linux/module.h>
  13. #include <asm/branch.h>
  14. #include <asm/cpu.h>
  15. #include <asm/cpu-features.h>
  16. #include <asm/fpu.h>
  17. #include <asm/fpu_emulator.h>
  18. #include <asm/inst.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/uaccess.h>
  21. /*
  22. * Calculate and return exception PC in case of branch delay slot
  23. * for microMIPS and MIPS16e. It does not clear the ISA mode bit.
  24. */
  25. int __isa_exception_epc(struct pt_regs *regs)
  26. {
  27. unsigned short inst;
  28. long epc = regs->cp0_epc;
  29. /* Calculate exception PC in branch delay slot. */
  30. if (__get_user(inst, (u16 __user *) msk_isa16_mode(epc))) {
  31. /* This should never happen because delay slot was checked. */
  32. force_sig(SIGSEGV, current);
  33. return epc;
  34. }
  35. if (cpu_has_mips16) {
  36. if (((union mips16e_instruction)inst).ri.opcode
  37. == MIPS16e_jal_op)
  38. epc += 4;
  39. else
  40. epc += 2;
  41. } else if (mm_insn_16bit(inst))
  42. epc += 2;
  43. else
  44. epc += 4;
  45. return epc;
  46. }
  47. /*
  48. * Compute return address and emulate branch in microMIPS mode after an
  49. * exception only. It does not handle compact branches/jumps and cannot
  50. * be used in interrupt context. (Compact branches/jumps do not cause
  51. * exceptions.)
  52. */
  53. int __microMIPS_compute_return_epc(struct pt_regs *regs)
  54. {
  55. u16 __user *pc16;
  56. u16 halfword;
  57. unsigned int word;
  58. unsigned long contpc;
  59. struct mm_decoded_insn mminsn = { 0 };
  60. mminsn.micro_mips_mode = 1;
  61. /* This load never faults. */
  62. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  63. __get_user(halfword, pc16);
  64. pc16++;
  65. contpc = regs->cp0_epc + 2;
  66. word = ((unsigned int)halfword << 16);
  67. mminsn.pc_inc = 2;
  68. if (!mm_insn_16bit(halfword)) {
  69. __get_user(halfword, pc16);
  70. pc16++;
  71. contpc = regs->cp0_epc + 4;
  72. mminsn.pc_inc = 4;
  73. word |= halfword;
  74. }
  75. mminsn.insn = word;
  76. if (get_user(halfword, pc16))
  77. goto sigsegv;
  78. mminsn.next_pc_inc = 2;
  79. word = ((unsigned int)halfword << 16);
  80. if (!mm_insn_16bit(halfword)) {
  81. pc16++;
  82. if (get_user(halfword, pc16))
  83. goto sigsegv;
  84. mminsn.next_pc_inc = 4;
  85. word |= halfword;
  86. }
  87. mminsn.next_insn = word;
  88. mm_isBranchInstr(regs, mminsn, &contpc);
  89. regs->cp0_epc = contpc;
  90. return 0;
  91. sigsegv:
  92. force_sig(SIGSEGV, current);
  93. return -EFAULT;
  94. }
  95. /*
  96. * Compute return address and emulate branch in MIPS16e mode after an
  97. * exception only. It does not handle compact branches/jumps and cannot
  98. * be used in interrupt context. (Compact branches/jumps do not cause
  99. * exceptions.)
  100. */
  101. int __MIPS16e_compute_return_epc(struct pt_regs *regs)
  102. {
  103. u16 __user *addr;
  104. union mips16e_instruction inst;
  105. u16 inst2;
  106. u32 fullinst;
  107. long epc;
  108. epc = regs->cp0_epc;
  109. /* Read the instruction. */
  110. addr = (u16 __user *)msk_isa16_mode(epc);
  111. if (__get_user(inst.full, addr)) {
  112. force_sig(SIGSEGV, current);
  113. return -EFAULT;
  114. }
  115. switch (inst.ri.opcode) {
  116. case MIPS16e_extend_op:
  117. regs->cp0_epc += 4;
  118. return 0;
  119. /*
  120. * JAL and JALX in MIPS16e mode
  121. */
  122. case MIPS16e_jal_op:
  123. addr += 1;
  124. if (__get_user(inst2, addr)) {
  125. force_sig(SIGSEGV, current);
  126. return -EFAULT;
  127. }
  128. fullinst = ((unsigned)inst.full << 16) | inst2;
  129. regs->regs[31] = epc + 6;
  130. epc += 4;
  131. epc >>= 28;
  132. epc <<= 28;
  133. /*
  134. * JAL:5 X:1 TARGET[20-16]:5 TARGET[25:21]:5 TARGET[15:0]:16
  135. *
  136. * ......TARGET[15:0].................TARGET[20:16]...........
  137. * ......TARGET[25:21]
  138. */
  139. epc |=
  140. ((fullinst & 0xffff) << 2) | ((fullinst & 0x3e00000) >> 3) |
  141. ((fullinst & 0x1f0000) << 7);
  142. if (!inst.jal.x)
  143. set_isa16_mode(epc); /* Set ISA mode bit. */
  144. regs->cp0_epc = epc;
  145. return 0;
  146. /*
  147. * J(AL)R(C)
  148. */
  149. case MIPS16e_rr_op:
  150. if (inst.rr.func == MIPS16e_jr_func) {
  151. if (inst.rr.ra)
  152. regs->cp0_epc = regs->regs[31];
  153. else
  154. regs->cp0_epc =
  155. regs->regs[reg16to32[inst.rr.rx]];
  156. if (inst.rr.l) {
  157. if (inst.rr.nd)
  158. regs->regs[31] = epc + 2;
  159. else
  160. regs->regs[31] = epc + 4;
  161. }
  162. return 0;
  163. }
  164. break;
  165. }
  166. /*
  167. * All other cases have no branch delay slot and are 16-bits.
  168. * Branches do not cause an exception.
  169. */
  170. regs->cp0_epc += 2;
  171. return 0;
  172. }
  173. /**
  174. * __compute_return_epc_for_insn - Computes the return address and do emulate
  175. * branch simulation, if required.
  176. *
  177. * @regs: Pointer to pt_regs
  178. * @insn: branch instruction to decode
  179. * @returns: -EFAULT on error and forces SIGBUS, and on success
  180. * returns 0 or BRANCH_LIKELY_TAKEN as appropriate after
  181. * evaluating the branch.
  182. */
  183. int __compute_return_epc_for_insn(struct pt_regs *regs,
  184. union mips_instruction insn)
  185. {
  186. unsigned int bit, fcr31, dspcontrol;
  187. long epc = regs->cp0_epc;
  188. int ret = 0;
  189. switch (insn.i_format.opcode) {
  190. /*
  191. * jr and jalr are in r_format format.
  192. */
  193. case spec_op:
  194. switch (insn.r_format.func) {
  195. case jalr_op:
  196. regs->regs[insn.r_format.rd] = epc + 8;
  197. /* Fall through */
  198. case jr_op:
  199. regs->cp0_epc = regs->regs[insn.r_format.rs];
  200. break;
  201. }
  202. break;
  203. /*
  204. * This group contains:
  205. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  206. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  207. */
  208. case bcond_op:
  209. switch (insn.i_format.rt) {
  210. case bltz_op:
  211. case bltzl_op:
  212. if ((long)regs->regs[insn.i_format.rs] < 0) {
  213. epc = epc + 4 + (insn.i_format.simmediate << 2);
  214. if (insn.i_format.rt == bltzl_op)
  215. ret = BRANCH_LIKELY_TAKEN;
  216. } else
  217. epc += 8;
  218. regs->cp0_epc = epc;
  219. break;
  220. case bgez_op:
  221. case bgezl_op:
  222. if ((long)regs->regs[insn.i_format.rs] >= 0) {
  223. epc = epc + 4 + (insn.i_format.simmediate << 2);
  224. if (insn.i_format.rt == bgezl_op)
  225. ret = BRANCH_LIKELY_TAKEN;
  226. } else
  227. epc += 8;
  228. regs->cp0_epc = epc;
  229. break;
  230. case bltzal_op:
  231. case bltzall_op:
  232. regs->regs[31] = epc + 8;
  233. if ((long)regs->regs[insn.i_format.rs] < 0) {
  234. epc = epc + 4 + (insn.i_format.simmediate << 2);
  235. if (insn.i_format.rt == bltzall_op)
  236. ret = BRANCH_LIKELY_TAKEN;
  237. } else
  238. epc += 8;
  239. regs->cp0_epc = epc;
  240. break;
  241. case bgezal_op:
  242. case bgezall_op:
  243. regs->regs[31] = epc + 8;
  244. if ((long)regs->regs[insn.i_format.rs] >= 0) {
  245. epc = epc + 4 + (insn.i_format.simmediate << 2);
  246. if (insn.i_format.rt == bgezall_op)
  247. ret = BRANCH_LIKELY_TAKEN;
  248. } else
  249. epc += 8;
  250. regs->cp0_epc = epc;
  251. break;
  252. case bposge32_op:
  253. if (!cpu_has_dsp)
  254. goto sigill;
  255. dspcontrol = rddsp(0x01);
  256. if (dspcontrol >= 32) {
  257. epc = epc + 4 + (insn.i_format.simmediate << 2);
  258. } else
  259. epc += 8;
  260. regs->cp0_epc = epc;
  261. break;
  262. }
  263. break;
  264. /*
  265. * These are unconditional and in j_format.
  266. */
  267. case jal_op:
  268. regs->regs[31] = regs->cp0_epc + 8;
  269. case j_op:
  270. epc += 4;
  271. epc >>= 28;
  272. epc <<= 28;
  273. epc |= (insn.j_format.target << 2);
  274. regs->cp0_epc = epc;
  275. if (insn.i_format.opcode == jalx_op)
  276. set_isa16_mode(regs->cp0_epc);
  277. break;
  278. /*
  279. * These are conditional and in i_format.
  280. */
  281. case beq_op:
  282. case beql_op:
  283. if (regs->regs[insn.i_format.rs] ==
  284. regs->regs[insn.i_format.rt]) {
  285. epc = epc + 4 + (insn.i_format.simmediate << 2);
  286. if (insn.i_format.rt == beql_op)
  287. ret = BRANCH_LIKELY_TAKEN;
  288. } else
  289. epc += 8;
  290. regs->cp0_epc = epc;
  291. break;
  292. case bne_op:
  293. case bnel_op:
  294. if (regs->regs[insn.i_format.rs] !=
  295. regs->regs[insn.i_format.rt]) {
  296. epc = epc + 4 + (insn.i_format.simmediate << 2);
  297. if (insn.i_format.rt == bnel_op)
  298. ret = BRANCH_LIKELY_TAKEN;
  299. } else
  300. epc += 8;
  301. regs->cp0_epc = epc;
  302. break;
  303. case blez_op: /* not really i_format */
  304. case blezl_op:
  305. /* rt field assumed to be zero */
  306. if ((long)regs->regs[insn.i_format.rs] <= 0) {
  307. epc = epc + 4 + (insn.i_format.simmediate << 2);
  308. if (insn.i_format.rt == bnel_op)
  309. ret = BRANCH_LIKELY_TAKEN;
  310. } else
  311. epc += 8;
  312. regs->cp0_epc = epc;
  313. break;
  314. case bgtz_op:
  315. case bgtzl_op:
  316. /* rt field assumed to be zero */
  317. if ((long)regs->regs[insn.i_format.rs] > 0) {
  318. epc = epc + 4 + (insn.i_format.simmediate << 2);
  319. if (insn.i_format.rt == bnel_op)
  320. ret = BRANCH_LIKELY_TAKEN;
  321. } else
  322. epc += 8;
  323. regs->cp0_epc = epc;
  324. break;
  325. /*
  326. * And now the FPA/cp1 branch instructions.
  327. */
  328. case cop1_op:
  329. preempt_disable();
  330. if (is_fpu_owner())
  331. asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
  332. else
  333. fcr31 = current->thread.fpu.fcr31;
  334. preempt_enable();
  335. bit = (insn.i_format.rt >> 2);
  336. bit += (bit != 0);
  337. bit += 23;
  338. switch (insn.i_format.rt & 3) {
  339. case 0: /* bc1f */
  340. case 2: /* bc1fl */
  341. if (~fcr31 & (1 << bit)) {
  342. epc = epc + 4 + (insn.i_format.simmediate << 2);
  343. if (insn.i_format.rt == 2)
  344. ret = BRANCH_LIKELY_TAKEN;
  345. } else
  346. epc += 8;
  347. regs->cp0_epc = epc;
  348. break;
  349. case 1: /* bc1t */
  350. case 3: /* bc1tl */
  351. if (fcr31 & (1 << bit)) {
  352. epc = epc + 4 + (insn.i_format.simmediate << 2);
  353. if (insn.i_format.rt == 3)
  354. ret = BRANCH_LIKELY_TAKEN;
  355. } else
  356. epc += 8;
  357. regs->cp0_epc = epc;
  358. break;
  359. }
  360. break;
  361. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  362. case lwc2_op: /* This is bbit0 on Octeon */
  363. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  364. == 0)
  365. epc = epc + 4 + (insn.i_format.simmediate << 2);
  366. else
  367. epc += 8;
  368. regs->cp0_epc = epc;
  369. break;
  370. case ldc2_op: /* This is bbit032 on Octeon */
  371. if ((regs->regs[insn.i_format.rs] &
  372. (1ull<<(insn.i_format.rt+32))) == 0)
  373. epc = epc + 4 + (insn.i_format.simmediate << 2);
  374. else
  375. epc += 8;
  376. regs->cp0_epc = epc;
  377. break;
  378. case swc2_op: /* This is bbit1 on Octeon */
  379. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  380. epc = epc + 4 + (insn.i_format.simmediate << 2);
  381. else
  382. epc += 8;
  383. regs->cp0_epc = epc;
  384. break;
  385. case sdc2_op: /* This is bbit132 on Octeon */
  386. if (regs->regs[insn.i_format.rs] &
  387. (1ull<<(insn.i_format.rt+32)))
  388. epc = epc + 4 + (insn.i_format.simmediate << 2);
  389. else
  390. epc += 8;
  391. regs->cp0_epc = epc;
  392. break;
  393. #endif
  394. }
  395. return ret;
  396. sigill:
  397. printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm);
  398. force_sig(SIGBUS, current);
  399. return -EFAULT;
  400. }
  401. EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn);
  402. int __compute_return_epc(struct pt_regs *regs)
  403. {
  404. unsigned int __user *addr;
  405. long epc;
  406. union mips_instruction insn;
  407. epc = regs->cp0_epc;
  408. if (epc & 3)
  409. goto unaligned;
  410. /*
  411. * Read the instruction
  412. */
  413. addr = (unsigned int __user *) epc;
  414. if (__get_user(insn.word, addr)) {
  415. force_sig(SIGSEGV, current);
  416. return -EFAULT;
  417. }
  418. return __compute_return_epc_for_insn(regs, insn);
  419. unaligned:
  420. printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
  421. force_sig(SIGBUS, current);
  422. return -EFAULT;
  423. }