branch.c 21 KB

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