branch.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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/mips-r2-to-r6-emul.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/uaccess.h>
  22. /*
  23. * Calculate and return exception PC in case of branch delay slot
  24. * for microMIPS and MIPS16e. It does not clear the ISA mode bit.
  25. */
  26. int __isa_exception_epc(struct pt_regs *regs)
  27. {
  28. unsigned short inst;
  29. long epc = regs->cp0_epc;
  30. /* Calculate exception PC in branch delay slot. */
  31. if (__get_user(inst, (u16 __user *) msk_isa16_mode(epc))) {
  32. /* This should never happen because delay slot was checked. */
  33. force_sig(SIGSEGV, current);
  34. return epc;
  35. }
  36. if (cpu_has_mips16) {
  37. if (((union mips16e_instruction)inst).ri.opcode
  38. == MIPS16e_jal_op)
  39. epc += 4;
  40. else
  41. epc += 2;
  42. } else if (mm_insn_16bit(inst))
  43. epc += 2;
  44. else
  45. epc += 4;
  46. return epc;
  47. }
  48. /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
  49. static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
  50. int __mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
  51. unsigned long *contpc)
  52. {
  53. union mips_instruction insn = (union mips_instruction)dec_insn.insn;
  54. int bc_false = 0;
  55. unsigned int fcr31;
  56. unsigned int bit;
  57. if (!cpu_has_mmips)
  58. return 0;
  59. switch (insn.mm_i_format.opcode) {
  60. case mm_pool32a_op:
  61. if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
  62. mm_pool32axf_op) {
  63. switch (insn.mm_i_format.simmediate >>
  64. MM_POOL32A_MINOR_SHIFT) {
  65. case mm_jalr_op:
  66. case mm_jalrhb_op:
  67. case mm_jalrs_op:
  68. case mm_jalrshb_op:
  69. if (insn.mm_i_format.rt != 0) /* Not mm_jr */
  70. regs->regs[insn.mm_i_format.rt] =
  71. regs->cp0_epc +
  72. dec_insn.pc_inc +
  73. dec_insn.next_pc_inc;
  74. *contpc = regs->regs[insn.mm_i_format.rs];
  75. return 1;
  76. }
  77. }
  78. break;
  79. case mm_pool32i_op:
  80. switch (insn.mm_i_format.rt) {
  81. case mm_bltzals_op:
  82. case mm_bltzal_op:
  83. regs->regs[31] = regs->cp0_epc +
  84. dec_insn.pc_inc +
  85. dec_insn.next_pc_inc;
  86. /* Fall through */
  87. case mm_bltz_op:
  88. if ((long)regs->regs[insn.mm_i_format.rs] < 0)
  89. *contpc = regs->cp0_epc +
  90. dec_insn.pc_inc +
  91. (insn.mm_i_format.simmediate << 1);
  92. else
  93. *contpc = regs->cp0_epc +
  94. dec_insn.pc_inc +
  95. dec_insn.next_pc_inc;
  96. return 1;
  97. case mm_bgezals_op:
  98. case mm_bgezal_op:
  99. regs->regs[31] = regs->cp0_epc +
  100. dec_insn.pc_inc +
  101. dec_insn.next_pc_inc;
  102. /* Fall through */
  103. case mm_bgez_op:
  104. if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
  105. *contpc = regs->cp0_epc +
  106. dec_insn.pc_inc +
  107. (insn.mm_i_format.simmediate << 1);
  108. else
  109. *contpc = regs->cp0_epc +
  110. dec_insn.pc_inc +
  111. dec_insn.next_pc_inc;
  112. return 1;
  113. case mm_blez_op:
  114. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  115. *contpc = regs->cp0_epc +
  116. dec_insn.pc_inc +
  117. (insn.mm_i_format.simmediate << 1);
  118. else
  119. *contpc = regs->cp0_epc +
  120. dec_insn.pc_inc +
  121. dec_insn.next_pc_inc;
  122. return 1;
  123. case mm_bgtz_op:
  124. if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
  125. *contpc = regs->cp0_epc +
  126. dec_insn.pc_inc +
  127. (insn.mm_i_format.simmediate << 1);
  128. else
  129. *contpc = regs->cp0_epc +
  130. dec_insn.pc_inc +
  131. dec_insn.next_pc_inc;
  132. return 1;
  133. case mm_bc2f_op:
  134. case mm_bc1f_op:
  135. bc_false = 1;
  136. /* Fall through */
  137. case mm_bc2t_op:
  138. case mm_bc1t_op:
  139. preempt_disable();
  140. if (is_fpu_owner())
  141. fcr31 = read_32bit_cp1_register(CP1_STATUS);
  142. else
  143. fcr31 = current->thread.fpu.fcr31;
  144. preempt_enable();
  145. if (bc_false)
  146. fcr31 = ~fcr31;
  147. bit = (insn.mm_i_format.rs >> 2);
  148. bit += (bit != 0);
  149. bit += 23;
  150. if (fcr31 & (1 << bit))
  151. *contpc = regs->cp0_epc +
  152. dec_insn.pc_inc +
  153. (insn.mm_i_format.simmediate << 1);
  154. else
  155. *contpc = regs->cp0_epc +
  156. dec_insn.pc_inc + dec_insn.next_pc_inc;
  157. return 1;
  158. }
  159. break;
  160. case mm_pool16c_op:
  161. switch (insn.mm_i_format.rt) {
  162. case mm_jalr16_op:
  163. case mm_jalrs16_op:
  164. regs->regs[31] = regs->cp0_epc +
  165. dec_insn.pc_inc + dec_insn.next_pc_inc;
  166. /* Fall through */
  167. case mm_jr16_op:
  168. *contpc = regs->regs[insn.mm_i_format.rs];
  169. return 1;
  170. }
  171. break;
  172. case mm_beqz16_op:
  173. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
  174. *contpc = regs->cp0_epc +
  175. dec_insn.pc_inc +
  176. (insn.mm_b1_format.simmediate << 1);
  177. else
  178. *contpc = regs->cp0_epc +
  179. dec_insn.pc_inc + dec_insn.next_pc_inc;
  180. return 1;
  181. case mm_bnez16_op:
  182. if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
  183. *contpc = regs->cp0_epc +
  184. dec_insn.pc_inc +
  185. (insn.mm_b1_format.simmediate << 1);
  186. else
  187. *contpc = regs->cp0_epc +
  188. dec_insn.pc_inc + dec_insn.next_pc_inc;
  189. return 1;
  190. case mm_b16_op:
  191. *contpc = regs->cp0_epc + dec_insn.pc_inc +
  192. (insn.mm_b0_format.simmediate << 1);
  193. return 1;
  194. case mm_beq32_op:
  195. if (regs->regs[insn.mm_i_format.rs] ==
  196. regs->regs[insn.mm_i_format.rt])
  197. *contpc = regs->cp0_epc +
  198. dec_insn.pc_inc +
  199. (insn.mm_i_format.simmediate << 1);
  200. else
  201. *contpc = regs->cp0_epc +
  202. dec_insn.pc_inc +
  203. dec_insn.next_pc_inc;
  204. return 1;
  205. case mm_bne32_op:
  206. if (regs->regs[insn.mm_i_format.rs] !=
  207. regs->regs[insn.mm_i_format.rt])
  208. *contpc = regs->cp0_epc +
  209. dec_insn.pc_inc +
  210. (insn.mm_i_format.simmediate << 1);
  211. else
  212. *contpc = regs->cp0_epc +
  213. dec_insn.pc_inc + dec_insn.next_pc_inc;
  214. return 1;
  215. case mm_jalx32_op:
  216. regs->regs[31] = regs->cp0_epc +
  217. dec_insn.pc_inc + dec_insn.next_pc_inc;
  218. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  219. *contpc >>= 28;
  220. *contpc <<= 28;
  221. *contpc |= (insn.j_format.target << 2);
  222. return 1;
  223. case mm_jals32_op:
  224. case mm_jal32_op:
  225. regs->regs[31] = regs->cp0_epc +
  226. dec_insn.pc_inc + dec_insn.next_pc_inc;
  227. /* Fall through */
  228. case mm_j32_op:
  229. *contpc = regs->cp0_epc + dec_insn.pc_inc;
  230. *contpc >>= 27;
  231. *contpc <<= 27;
  232. *contpc |= (insn.j_format.target << 1);
  233. set_isa16_mode(*contpc);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. /*
  239. * Compute return address and emulate branch in microMIPS mode after an
  240. * exception only. It does not handle compact branches/jumps and cannot
  241. * be used in interrupt context. (Compact branches/jumps do not cause
  242. * exceptions.)
  243. */
  244. int __microMIPS_compute_return_epc(struct pt_regs *regs)
  245. {
  246. u16 __user *pc16;
  247. u16 halfword;
  248. unsigned int word;
  249. unsigned long contpc;
  250. struct mm_decoded_insn mminsn = { 0 };
  251. mminsn.micro_mips_mode = 1;
  252. /* This load never faults. */
  253. pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
  254. __get_user(halfword, pc16);
  255. pc16++;
  256. contpc = regs->cp0_epc + 2;
  257. word = ((unsigned int)halfword << 16);
  258. mminsn.pc_inc = 2;
  259. if (!mm_insn_16bit(halfword)) {
  260. __get_user(halfword, pc16);
  261. pc16++;
  262. contpc = regs->cp0_epc + 4;
  263. mminsn.pc_inc = 4;
  264. word |= halfword;
  265. }
  266. mminsn.insn = word;
  267. if (get_user(halfword, pc16))
  268. goto sigsegv;
  269. mminsn.next_pc_inc = 2;
  270. word = ((unsigned int)halfword << 16);
  271. if (!mm_insn_16bit(halfword)) {
  272. pc16++;
  273. if (get_user(halfword, pc16))
  274. goto sigsegv;
  275. mminsn.next_pc_inc = 4;
  276. word |= halfword;
  277. }
  278. mminsn.next_insn = word;
  279. mm_isBranchInstr(regs, mminsn, &contpc);
  280. regs->cp0_epc = contpc;
  281. return 0;
  282. sigsegv:
  283. force_sig(SIGSEGV, current);
  284. return -EFAULT;
  285. }
  286. /*
  287. * Compute return address and emulate branch in MIPS16e mode after an
  288. * exception only. It does not handle compact branches/jumps and cannot
  289. * be used in interrupt context. (Compact branches/jumps do not cause
  290. * exceptions.)
  291. */
  292. int __MIPS16e_compute_return_epc(struct pt_regs *regs)
  293. {
  294. u16 __user *addr;
  295. union mips16e_instruction inst;
  296. u16 inst2;
  297. u32 fullinst;
  298. long epc;
  299. epc = regs->cp0_epc;
  300. /* Read the instruction. */
  301. addr = (u16 __user *)msk_isa16_mode(epc);
  302. if (__get_user(inst.full, addr)) {
  303. force_sig(SIGSEGV, current);
  304. return -EFAULT;
  305. }
  306. switch (inst.ri.opcode) {
  307. case MIPS16e_extend_op:
  308. regs->cp0_epc += 4;
  309. return 0;
  310. /*
  311. * JAL and JALX in MIPS16e mode
  312. */
  313. case MIPS16e_jal_op:
  314. addr += 1;
  315. if (__get_user(inst2, addr)) {
  316. force_sig(SIGSEGV, current);
  317. return -EFAULT;
  318. }
  319. fullinst = ((unsigned)inst.full << 16) | inst2;
  320. regs->regs[31] = epc + 6;
  321. epc += 4;
  322. epc >>= 28;
  323. epc <<= 28;
  324. /*
  325. * JAL:5 X:1 TARGET[20-16]:5 TARGET[25:21]:5 TARGET[15:0]:16
  326. *
  327. * ......TARGET[15:0].................TARGET[20:16]...........
  328. * ......TARGET[25:21]
  329. */
  330. epc |=
  331. ((fullinst & 0xffff) << 2) | ((fullinst & 0x3e00000) >> 3) |
  332. ((fullinst & 0x1f0000) << 7);
  333. if (!inst.jal.x)
  334. set_isa16_mode(epc); /* Set ISA mode bit. */
  335. regs->cp0_epc = epc;
  336. return 0;
  337. /*
  338. * J(AL)R(C)
  339. */
  340. case MIPS16e_rr_op:
  341. if (inst.rr.func == MIPS16e_jr_func) {
  342. if (inst.rr.ra)
  343. regs->cp0_epc = regs->regs[31];
  344. else
  345. regs->cp0_epc =
  346. regs->regs[reg16to32[inst.rr.rx]];
  347. if (inst.rr.l) {
  348. if (inst.rr.nd)
  349. regs->regs[31] = epc + 2;
  350. else
  351. regs->regs[31] = epc + 4;
  352. }
  353. return 0;
  354. }
  355. break;
  356. }
  357. /*
  358. * All other cases have no branch delay slot and are 16-bits.
  359. * Branches do not cause an exception.
  360. */
  361. regs->cp0_epc += 2;
  362. return 0;
  363. }
  364. /**
  365. * __compute_return_epc_for_insn - Computes the return address and do emulate
  366. * branch simulation, if required.
  367. *
  368. * @regs: Pointer to pt_regs
  369. * @insn: branch instruction to decode
  370. * @returns: -EFAULT on error and forces SIGBUS, and on success
  371. * returns 0 or BRANCH_LIKELY_TAKEN as appropriate after
  372. * evaluating the branch.
  373. *
  374. * MIPS R6 Compact branches and forbidden slots:
  375. * Compact branches do not throw exceptions because they do
  376. * not have delay slots. The forbidden slot instruction ($PC+4)
  377. * is only executed if the branch was not taken. Otherwise the
  378. * forbidden slot is skipped entirely. This means that the
  379. * only possible reason to be here because of a MIPS R6 compact
  380. * branch instruction is that the forbidden slot has thrown one.
  381. * In that case the branch was not taken, so the EPC can be safely
  382. * set to EPC + 8.
  383. */
  384. int __compute_return_epc_for_insn(struct pt_regs *regs,
  385. union mips_instruction insn)
  386. {
  387. unsigned int bit, fcr31, dspcontrol, reg;
  388. long epc = regs->cp0_epc;
  389. int ret = 0;
  390. switch (insn.i_format.opcode) {
  391. /*
  392. * jr and jalr are in r_format format.
  393. */
  394. case spec_op:
  395. switch (insn.r_format.func) {
  396. case jalr_op:
  397. regs->regs[insn.r_format.rd] = epc + 8;
  398. /* Fall through */
  399. case jr_op:
  400. if (NO_R6EMU && insn.r_format.func == jr_op)
  401. goto sigill_r6;
  402. regs->cp0_epc = regs->regs[insn.r_format.rs];
  403. break;
  404. }
  405. break;
  406. /*
  407. * This group contains:
  408. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  409. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  410. */
  411. case bcond_op:
  412. switch (insn.i_format.rt) {
  413. case bltzl_op:
  414. if (NO_R6EMU)
  415. goto sigill_r6;
  416. case bltz_op:
  417. if ((long)regs->regs[insn.i_format.rs] < 0) {
  418. epc = epc + 4 + (insn.i_format.simmediate << 2);
  419. if (insn.i_format.rt == bltzl_op)
  420. ret = BRANCH_LIKELY_TAKEN;
  421. } else
  422. epc += 8;
  423. regs->cp0_epc = epc;
  424. break;
  425. case bgezl_op:
  426. if (NO_R6EMU)
  427. goto sigill_r6;
  428. case bgez_op:
  429. if ((long)regs->regs[insn.i_format.rs] >= 0) {
  430. epc = epc + 4 + (insn.i_format.simmediate << 2);
  431. if (insn.i_format.rt == bgezl_op)
  432. ret = BRANCH_LIKELY_TAKEN;
  433. } else
  434. epc += 8;
  435. regs->cp0_epc = epc;
  436. break;
  437. case bltzal_op:
  438. case bltzall_op:
  439. if (NO_R6EMU && (insn.i_format.rs ||
  440. insn.i_format.rt == bltzall_op)) {
  441. ret = -SIGILL;
  442. break;
  443. }
  444. regs->regs[31] = epc + 8;
  445. /*
  446. * OK we are here either because we hit a NAL
  447. * instruction or because we are emulating an
  448. * old bltzal{,l} one. Lets figure out what the
  449. * case really is.
  450. */
  451. if (!insn.i_format.rs) {
  452. /*
  453. * NAL or BLTZAL with rs == 0
  454. * Doesn't matter if we are R6 or not. The
  455. * result is the same
  456. */
  457. regs->cp0_epc += 4 +
  458. (insn.i_format.simmediate << 2);
  459. break;
  460. }
  461. /* Now do the real thing for non-R6 BLTZAL{,L} */
  462. if ((long)regs->regs[insn.i_format.rs] < 0) {
  463. epc = epc + 4 + (insn.i_format.simmediate << 2);
  464. if (insn.i_format.rt == bltzall_op)
  465. ret = BRANCH_LIKELY_TAKEN;
  466. } else
  467. epc += 8;
  468. regs->cp0_epc = epc;
  469. break;
  470. case bgezal_op:
  471. case bgezall_op:
  472. if (NO_R6EMU && (insn.i_format.rs ||
  473. insn.i_format.rt == bgezall_op)) {
  474. ret = -SIGILL;
  475. break;
  476. }
  477. regs->regs[31] = epc + 8;
  478. /*
  479. * OK we are here either because we hit a BAL
  480. * instruction or because we are emulating an
  481. * old bgezal{,l} one. Lets figure out what the
  482. * case really is.
  483. */
  484. if (!insn.i_format.rs) {
  485. /*
  486. * BAL or BGEZAL with rs == 0
  487. * Doesn't matter if we are R6 or not. The
  488. * result is the same
  489. */
  490. regs->cp0_epc += 4 +
  491. (insn.i_format.simmediate << 2);
  492. break;
  493. }
  494. /* Now do the real thing for non-R6 BGEZAL{,L} */
  495. if ((long)regs->regs[insn.i_format.rs] >= 0) {
  496. epc = epc + 4 + (insn.i_format.simmediate << 2);
  497. if (insn.i_format.rt == bgezall_op)
  498. ret = BRANCH_LIKELY_TAKEN;
  499. } else
  500. epc += 8;
  501. regs->cp0_epc = epc;
  502. break;
  503. case bposge32_op:
  504. if (!cpu_has_dsp)
  505. goto sigill_dsp;
  506. dspcontrol = rddsp(0x01);
  507. if (dspcontrol >= 32) {
  508. epc = epc + 4 + (insn.i_format.simmediate << 2);
  509. } else
  510. epc += 8;
  511. regs->cp0_epc = epc;
  512. break;
  513. }
  514. break;
  515. /*
  516. * These are unconditional and in j_format.
  517. */
  518. case jal_op:
  519. regs->regs[31] = regs->cp0_epc + 8;
  520. case j_op:
  521. epc += 4;
  522. epc >>= 28;
  523. epc <<= 28;
  524. epc |= (insn.j_format.target << 2);
  525. regs->cp0_epc = epc;
  526. if (insn.i_format.opcode == jalx_op)
  527. set_isa16_mode(regs->cp0_epc);
  528. break;
  529. /*
  530. * These are conditional and in i_format.
  531. */
  532. case beql_op:
  533. if (NO_R6EMU)
  534. goto sigill_r6;
  535. case beq_op:
  536. if (regs->regs[insn.i_format.rs] ==
  537. regs->regs[insn.i_format.rt]) {
  538. epc = epc + 4 + (insn.i_format.simmediate << 2);
  539. if (insn.i_format.opcode == beql_op)
  540. ret = BRANCH_LIKELY_TAKEN;
  541. } else
  542. epc += 8;
  543. regs->cp0_epc = epc;
  544. break;
  545. case bnel_op:
  546. if (NO_R6EMU)
  547. goto sigill_r6;
  548. case bne_op:
  549. if (regs->regs[insn.i_format.rs] !=
  550. regs->regs[insn.i_format.rt]) {
  551. epc = epc + 4 + (insn.i_format.simmediate << 2);
  552. if (insn.i_format.opcode == bnel_op)
  553. ret = BRANCH_LIKELY_TAKEN;
  554. } else
  555. epc += 8;
  556. regs->cp0_epc = epc;
  557. break;
  558. case blezl_op: /* not really i_format */
  559. if (NO_R6EMU)
  560. goto sigill_r6;
  561. case blez_op:
  562. /*
  563. * Compact branches for R6 for the
  564. * blez and blezl opcodes.
  565. * BLEZ | rs = 0 | rt != 0 == BLEZALC
  566. * BLEZ | rs = rt != 0 == BGEZALC
  567. * BLEZ | rs != 0 | rt != 0 == BGEUC
  568. * BLEZL | rs = 0 | rt != 0 == BLEZC
  569. * BLEZL | rs = rt != 0 == BGEZC
  570. * BLEZL | rs != 0 | rt != 0 == BGEC
  571. *
  572. * For real BLEZ{,L}, rt is always 0.
  573. */
  574. if (cpu_has_mips_r6 && insn.i_format.rt) {
  575. if ((insn.i_format.opcode == blez_op) &&
  576. ((!insn.i_format.rs && insn.i_format.rt) ||
  577. (insn.i_format.rs == insn.i_format.rt)))
  578. regs->regs[31] = epc + 4;
  579. regs->cp0_epc += 8;
  580. break;
  581. }
  582. /* rt field assumed to be zero */
  583. if ((long)regs->regs[insn.i_format.rs] <= 0) {
  584. epc = epc + 4 + (insn.i_format.simmediate << 2);
  585. if (insn.i_format.opcode == blezl_op)
  586. ret = BRANCH_LIKELY_TAKEN;
  587. } else
  588. epc += 8;
  589. regs->cp0_epc = epc;
  590. break;
  591. case bgtzl_op:
  592. if (NO_R6EMU)
  593. goto sigill_r6;
  594. case bgtz_op:
  595. /*
  596. * Compact branches for R6 for the
  597. * bgtz and bgtzl opcodes.
  598. * BGTZ | rs = 0 | rt != 0 == BGTZALC
  599. * BGTZ | rs = rt != 0 == BLTZALC
  600. * BGTZ | rs != 0 | rt != 0 == BLTUC
  601. * BGTZL | rs = 0 | rt != 0 == BGTZC
  602. * BGTZL | rs = rt != 0 == BLTZC
  603. * BGTZL | rs != 0 | rt != 0 == BLTC
  604. *
  605. * *ZALC varint for BGTZ &&& rt != 0
  606. * For real GTZ{,L}, rt is always 0.
  607. */
  608. if (cpu_has_mips_r6 && insn.i_format.rt) {
  609. if ((insn.i_format.opcode == blez_op) &&
  610. ((!insn.i_format.rs && insn.i_format.rt) ||
  611. (insn.i_format.rs == insn.i_format.rt)))
  612. regs->regs[31] = epc + 4;
  613. regs->cp0_epc += 8;
  614. break;
  615. }
  616. /* rt field assumed to be zero */
  617. if ((long)regs->regs[insn.i_format.rs] > 0) {
  618. epc = epc + 4 + (insn.i_format.simmediate << 2);
  619. if (insn.i_format.opcode == bgtzl_op)
  620. ret = BRANCH_LIKELY_TAKEN;
  621. } else
  622. epc += 8;
  623. regs->cp0_epc = epc;
  624. break;
  625. /*
  626. * And now the FPA/cp1 branch instructions.
  627. */
  628. case cop1_op:
  629. if (cpu_has_mips_r6 &&
  630. ((insn.i_format.rs == bc1eqz_op) ||
  631. (insn.i_format.rs == bc1nez_op))) {
  632. if (!used_math()) { /* First time FPU user */
  633. ret = init_fpu();
  634. if (ret && NO_R6EMU) {
  635. ret = -ret;
  636. break;
  637. }
  638. ret = 0;
  639. set_used_math();
  640. }
  641. lose_fpu(1); /* Save FPU state for the emulator. */
  642. reg = insn.i_format.rt;
  643. bit = 0;
  644. switch (insn.i_format.rs) {
  645. case bc1eqz_op:
  646. /* Test bit 0 */
  647. if (get_fpr32(&current->thread.fpu.fpr[reg], 0)
  648. & 0x1)
  649. bit = 1;
  650. break;
  651. case bc1nez_op:
  652. /* Test bit 0 */
  653. if (!(get_fpr32(&current->thread.fpu.fpr[reg], 0)
  654. & 0x1))
  655. bit = 1;
  656. break;
  657. }
  658. own_fpu(1);
  659. if (bit)
  660. epc = epc + 4 +
  661. (insn.i_format.simmediate << 2);
  662. else
  663. epc += 8;
  664. regs->cp0_epc = epc;
  665. break;
  666. } else {
  667. preempt_disable();
  668. if (is_fpu_owner())
  669. fcr31 = read_32bit_cp1_register(CP1_STATUS);
  670. else
  671. fcr31 = current->thread.fpu.fcr31;
  672. preempt_enable();
  673. bit = (insn.i_format.rt >> 2);
  674. bit += (bit != 0);
  675. bit += 23;
  676. switch (insn.i_format.rt & 3) {
  677. case 0: /* bc1f */
  678. case 2: /* bc1fl */
  679. if (~fcr31 & (1 << bit)) {
  680. epc = epc + 4 +
  681. (insn.i_format.simmediate << 2);
  682. if (insn.i_format.rt == 2)
  683. ret = BRANCH_LIKELY_TAKEN;
  684. } else
  685. epc += 8;
  686. regs->cp0_epc = epc;
  687. break;
  688. case 1: /* bc1t */
  689. case 3: /* bc1tl */
  690. if (fcr31 & (1 << bit)) {
  691. epc = epc + 4 +
  692. (insn.i_format.simmediate << 2);
  693. if (insn.i_format.rt == 3)
  694. ret = BRANCH_LIKELY_TAKEN;
  695. } else
  696. epc += 8;
  697. regs->cp0_epc = epc;
  698. break;
  699. }
  700. break;
  701. }
  702. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  703. case lwc2_op: /* This is bbit0 on Octeon */
  704. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  705. == 0)
  706. epc = epc + 4 + (insn.i_format.simmediate << 2);
  707. else
  708. epc += 8;
  709. regs->cp0_epc = epc;
  710. break;
  711. case ldc2_op: /* This is bbit032 on Octeon */
  712. if ((regs->regs[insn.i_format.rs] &
  713. (1ull<<(insn.i_format.rt+32))) == 0)
  714. epc = epc + 4 + (insn.i_format.simmediate << 2);
  715. else
  716. epc += 8;
  717. regs->cp0_epc = epc;
  718. break;
  719. case swc2_op: /* This is bbit1 on Octeon */
  720. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  721. epc = epc + 4 + (insn.i_format.simmediate << 2);
  722. else
  723. epc += 8;
  724. regs->cp0_epc = epc;
  725. break;
  726. case sdc2_op: /* This is bbit132 on Octeon */
  727. if (regs->regs[insn.i_format.rs] &
  728. (1ull<<(insn.i_format.rt+32)))
  729. epc = epc + 4 + (insn.i_format.simmediate << 2);
  730. else
  731. epc += 8;
  732. regs->cp0_epc = epc;
  733. break;
  734. #else
  735. case bc6_op:
  736. /* Only valid for MIPS R6 */
  737. if (!cpu_has_mips_r6) {
  738. ret = -SIGILL;
  739. break;
  740. }
  741. regs->cp0_epc += 8;
  742. break;
  743. case balc6_op:
  744. if (!cpu_has_mips_r6) {
  745. ret = -SIGILL;
  746. break;
  747. }
  748. /* Compact branch: BALC */
  749. regs->regs[31] = epc + 4;
  750. epc += 4 + (insn.i_format.simmediate << 2);
  751. regs->cp0_epc = epc;
  752. break;
  753. case beqzcjic_op:
  754. if (!cpu_has_mips_r6) {
  755. ret = -SIGILL;
  756. break;
  757. }
  758. /* Compact branch: BEQZC || JIC */
  759. regs->cp0_epc += 8;
  760. break;
  761. case bnezcjialc_op:
  762. if (!cpu_has_mips_r6) {
  763. ret = -SIGILL;
  764. break;
  765. }
  766. /* Compact branch: BNEZC || JIALC */
  767. if (insn.i_format.rs)
  768. regs->regs[31] = epc + 4;
  769. regs->cp0_epc += 8;
  770. break;
  771. #endif
  772. case cbcond0_op:
  773. case cbcond1_op:
  774. /* Only valid for MIPS R6 */
  775. if (!cpu_has_mips_r6) {
  776. ret = -SIGILL;
  777. break;
  778. }
  779. /*
  780. * Compact branches:
  781. * bovc, beqc, beqzalc, bnvc, bnec, bnezlac
  782. */
  783. if (insn.i_format.rt && !insn.i_format.rs)
  784. regs->regs[31] = epc + 4;
  785. regs->cp0_epc += 8;
  786. break;
  787. }
  788. return ret;
  789. sigill_dsp:
  790. printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm);
  791. force_sig(SIGBUS, current);
  792. return -EFAULT;
  793. sigill_r6:
  794. pr_info("%s: R2 branch but r2-to-r6 emulator is not preset - sending SIGILL.\n",
  795. current->comm);
  796. force_sig(SIGILL, current);
  797. return -EFAULT;
  798. }
  799. EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn);
  800. int __compute_return_epc(struct pt_regs *regs)
  801. {
  802. unsigned int __user *addr;
  803. long epc;
  804. union mips_instruction insn;
  805. epc = regs->cp0_epc;
  806. if (epc & 3)
  807. goto unaligned;
  808. /*
  809. * Read the instruction
  810. */
  811. addr = (unsigned int __user *) epc;
  812. if (__get_user(insn.word, addr)) {
  813. force_sig(SIGSEGV, current);
  814. return -EFAULT;
  815. }
  816. return __compute_return_epc_for_insn(regs, insn);
  817. unaligned:
  818. printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
  819. force_sig(SIGBUS, current);
  820. return -EFAULT;
  821. }