branch.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 SIGILL, 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_r2r6;
  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_r2r6;
  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_r2r6;
  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. goto sigill_r2r6;
  443. regs->regs[31] = epc + 8;
  444. /*
  445. * OK we are here either because we hit a NAL
  446. * instruction or because we are emulating an
  447. * old bltzal{,l} one. Lets figure out what the
  448. * case really is.
  449. */
  450. if (!insn.i_format.rs) {
  451. /*
  452. * NAL or BLTZAL with rs == 0
  453. * Doesn't matter if we are R6 or not. The
  454. * result is the same
  455. */
  456. regs->cp0_epc += 4 +
  457. (insn.i_format.simmediate << 2);
  458. break;
  459. }
  460. /* Now do the real thing for non-R6 BLTZAL{,L} */
  461. if ((long)regs->regs[insn.i_format.rs] < 0) {
  462. epc = epc + 4 + (insn.i_format.simmediate << 2);
  463. if (insn.i_format.rt == bltzall_op)
  464. ret = BRANCH_LIKELY_TAKEN;
  465. } else
  466. epc += 8;
  467. regs->cp0_epc = epc;
  468. break;
  469. case bgezal_op:
  470. case bgezall_op:
  471. if (NO_R6EMU && (insn.i_format.rs ||
  472. insn.i_format.rt == bgezall_op))
  473. goto sigill_r2r6;
  474. regs->regs[31] = epc + 8;
  475. /*
  476. * OK we are here either because we hit a BAL
  477. * instruction or because we are emulating an
  478. * old bgezal{,l} one. Lets figure out what the
  479. * case really is.
  480. */
  481. if (!insn.i_format.rs) {
  482. /*
  483. * BAL or BGEZAL with rs == 0
  484. * Doesn't matter if we are R6 or not. The
  485. * result is the same
  486. */
  487. regs->cp0_epc += 4 +
  488. (insn.i_format.simmediate << 2);
  489. break;
  490. }
  491. /* Now do the real thing for non-R6 BGEZAL{,L} */
  492. if ((long)regs->regs[insn.i_format.rs] >= 0) {
  493. epc = epc + 4 + (insn.i_format.simmediate << 2);
  494. if (insn.i_format.rt == bgezall_op)
  495. ret = BRANCH_LIKELY_TAKEN;
  496. } else
  497. epc += 8;
  498. regs->cp0_epc = epc;
  499. break;
  500. case bposge32_op:
  501. if (!cpu_has_dsp)
  502. goto sigill_dsp;
  503. dspcontrol = rddsp(0x01);
  504. if (dspcontrol >= 32) {
  505. epc = epc + 4 + (insn.i_format.simmediate << 2);
  506. } else
  507. epc += 8;
  508. regs->cp0_epc = epc;
  509. break;
  510. }
  511. break;
  512. /*
  513. * These are unconditional and in j_format.
  514. */
  515. case jalx_op:
  516. case jal_op:
  517. regs->regs[31] = regs->cp0_epc + 8;
  518. case j_op:
  519. epc += 4;
  520. epc >>= 28;
  521. epc <<= 28;
  522. epc |= (insn.j_format.target << 2);
  523. regs->cp0_epc = epc;
  524. if (insn.i_format.opcode == jalx_op)
  525. set_isa16_mode(regs->cp0_epc);
  526. break;
  527. /*
  528. * These are conditional and in i_format.
  529. */
  530. case beql_op:
  531. if (NO_R6EMU)
  532. goto sigill_r2r6;
  533. case beq_op:
  534. if (regs->regs[insn.i_format.rs] ==
  535. regs->regs[insn.i_format.rt]) {
  536. epc = epc + 4 + (insn.i_format.simmediate << 2);
  537. if (insn.i_format.opcode == beql_op)
  538. ret = BRANCH_LIKELY_TAKEN;
  539. } else
  540. epc += 8;
  541. regs->cp0_epc = epc;
  542. break;
  543. case bnel_op:
  544. if (NO_R6EMU)
  545. goto sigill_r2r6;
  546. case bne_op:
  547. if (regs->regs[insn.i_format.rs] !=
  548. regs->regs[insn.i_format.rt]) {
  549. epc = epc + 4 + (insn.i_format.simmediate << 2);
  550. if (insn.i_format.opcode == bnel_op)
  551. ret = BRANCH_LIKELY_TAKEN;
  552. } else
  553. epc += 8;
  554. regs->cp0_epc = epc;
  555. break;
  556. case blezl_op: /* not really i_format */
  557. if (!insn.i_format.rt && NO_R6EMU)
  558. goto sigill_r2r6;
  559. case blez_op:
  560. /*
  561. * Compact branches for R6 for the
  562. * blez and blezl opcodes.
  563. * BLEZ | rs = 0 | rt != 0 == BLEZALC
  564. * BLEZ | rs = rt != 0 == BGEZALC
  565. * BLEZ | rs != 0 | rt != 0 == BGEUC
  566. * BLEZL | rs = 0 | rt != 0 == BLEZC
  567. * BLEZL | rs = rt != 0 == BGEZC
  568. * BLEZL | rs != 0 | rt != 0 == BGEC
  569. *
  570. * For real BLEZ{,L}, rt is always 0.
  571. */
  572. if (cpu_has_mips_r6 && insn.i_format.rt) {
  573. if ((insn.i_format.opcode == blez_op) &&
  574. ((!insn.i_format.rs && insn.i_format.rt) ||
  575. (insn.i_format.rs == insn.i_format.rt)))
  576. regs->regs[31] = epc + 4;
  577. regs->cp0_epc += 8;
  578. break;
  579. }
  580. /* rt field assumed to be zero */
  581. if ((long)regs->regs[insn.i_format.rs] <= 0) {
  582. epc = epc + 4 + (insn.i_format.simmediate << 2);
  583. if (insn.i_format.opcode == blezl_op)
  584. ret = BRANCH_LIKELY_TAKEN;
  585. } else
  586. epc += 8;
  587. regs->cp0_epc = epc;
  588. break;
  589. case bgtzl_op:
  590. if (!insn.i_format.rt && NO_R6EMU)
  591. goto sigill_r2r6;
  592. case bgtz_op:
  593. /*
  594. * Compact branches for R6 for the
  595. * bgtz and bgtzl opcodes.
  596. * BGTZ | rs = 0 | rt != 0 == BGTZALC
  597. * BGTZ | rs = rt != 0 == BLTZALC
  598. * BGTZ | rs != 0 | rt != 0 == BLTUC
  599. * BGTZL | rs = 0 | rt != 0 == BGTZC
  600. * BGTZL | rs = rt != 0 == BLTZC
  601. * BGTZL | rs != 0 | rt != 0 == BLTC
  602. *
  603. * *ZALC varint for BGTZ &&& rt != 0
  604. * For real GTZ{,L}, rt is always 0.
  605. */
  606. if (cpu_has_mips_r6 && insn.i_format.rt) {
  607. if ((insn.i_format.opcode == blez_op) &&
  608. ((!insn.i_format.rs && insn.i_format.rt) ||
  609. (insn.i_format.rs == insn.i_format.rt)))
  610. regs->regs[31] = epc + 4;
  611. regs->cp0_epc += 8;
  612. break;
  613. }
  614. /* rt field assumed to be zero */
  615. if ((long)regs->regs[insn.i_format.rs] > 0) {
  616. epc = epc + 4 + (insn.i_format.simmediate << 2);
  617. if (insn.i_format.opcode == bgtzl_op)
  618. ret = BRANCH_LIKELY_TAKEN;
  619. } else
  620. epc += 8;
  621. regs->cp0_epc = epc;
  622. break;
  623. /*
  624. * And now the FPA/cp1 branch instructions.
  625. */
  626. case cop1_op:
  627. if (cpu_has_mips_r6 &&
  628. ((insn.i_format.rs == bc1eqz_op) ||
  629. (insn.i_format.rs == bc1nez_op))) {
  630. if (!used_math()) { /* First time FPU user */
  631. ret = init_fpu();
  632. if (ret && NO_R6EMU) {
  633. ret = -ret;
  634. break;
  635. }
  636. ret = 0;
  637. set_used_math();
  638. }
  639. lose_fpu(1); /* Save FPU state for the emulator. */
  640. reg = insn.i_format.rt;
  641. bit = 0;
  642. switch (insn.i_format.rs) {
  643. case bc1eqz_op:
  644. /* Test bit 0 */
  645. if (get_fpr32(&current->thread.fpu.fpr[reg], 0)
  646. & 0x1)
  647. bit = 1;
  648. break;
  649. case bc1nez_op:
  650. /* Test bit 0 */
  651. if (!(get_fpr32(&current->thread.fpu.fpr[reg], 0)
  652. & 0x1))
  653. bit = 1;
  654. break;
  655. }
  656. own_fpu(1);
  657. if (bit)
  658. epc = epc + 4 +
  659. (insn.i_format.simmediate << 2);
  660. else
  661. epc += 8;
  662. regs->cp0_epc = epc;
  663. break;
  664. } else {
  665. preempt_disable();
  666. if (is_fpu_owner())
  667. fcr31 = read_32bit_cp1_register(CP1_STATUS);
  668. else
  669. fcr31 = current->thread.fpu.fcr31;
  670. preempt_enable();
  671. bit = (insn.i_format.rt >> 2);
  672. bit += (bit != 0);
  673. bit += 23;
  674. switch (insn.i_format.rt & 3) {
  675. case 0: /* bc1f */
  676. case 2: /* bc1fl */
  677. if (~fcr31 & (1 << bit)) {
  678. epc = epc + 4 +
  679. (insn.i_format.simmediate << 2);
  680. if (insn.i_format.rt == 2)
  681. ret = BRANCH_LIKELY_TAKEN;
  682. } else
  683. epc += 8;
  684. regs->cp0_epc = epc;
  685. break;
  686. case 1: /* bc1t */
  687. case 3: /* bc1tl */
  688. if (fcr31 & (1 << bit)) {
  689. epc = epc + 4 +
  690. (insn.i_format.simmediate << 2);
  691. if (insn.i_format.rt == 3)
  692. ret = BRANCH_LIKELY_TAKEN;
  693. } else
  694. epc += 8;
  695. regs->cp0_epc = epc;
  696. break;
  697. }
  698. break;
  699. }
  700. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  701. case lwc2_op: /* This is bbit0 on Octeon */
  702. if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  703. == 0)
  704. epc = epc + 4 + (insn.i_format.simmediate << 2);
  705. else
  706. epc += 8;
  707. regs->cp0_epc = epc;
  708. break;
  709. case ldc2_op: /* This is bbit032 on Octeon */
  710. if ((regs->regs[insn.i_format.rs] &
  711. (1ull<<(insn.i_format.rt+32))) == 0)
  712. epc = epc + 4 + (insn.i_format.simmediate << 2);
  713. else
  714. epc += 8;
  715. regs->cp0_epc = epc;
  716. break;
  717. case swc2_op: /* This is bbit1 on Octeon */
  718. if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
  719. epc = epc + 4 + (insn.i_format.simmediate << 2);
  720. else
  721. epc += 8;
  722. regs->cp0_epc = epc;
  723. break;
  724. case sdc2_op: /* This is bbit132 on Octeon */
  725. if (regs->regs[insn.i_format.rs] &
  726. (1ull<<(insn.i_format.rt+32)))
  727. epc = epc + 4 + (insn.i_format.simmediate << 2);
  728. else
  729. epc += 8;
  730. regs->cp0_epc = epc;
  731. break;
  732. #else
  733. case bc6_op:
  734. /* Only valid for MIPS R6 */
  735. if (!cpu_has_mips_r6) {
  736. ret = -SIGILL;
  737. break;
  738. }
  739. regs->cp0_epc += 8;
  740. break;
  741. case balc6_op:
  742. if (!cpu_has_mips_r6) {
  743. ret = -SIGILL;
  744. break;
  745. }
  746. /* Compact branch: BALC */
  747. regs->regs[31] = epc + 4;
  748. epc += 4 + (insn.i_format.simmediate << 2);
  749. regs->cp0_epc = epc;
  750. break;
  751. case beqzcjic_op:
  752. if (!cpu_has_mips_r6) {
  753. ret = -SIGILL;
  754. break;
  755. }
  756. /* Compact branch: BEQZC || JIC */
  757. regs->cp0_epc += 8;
  758. break;
  759. case bnezcjialc_op:
  760. if (!cpu_has_mips_r6) {
  761. ret = -SIGILL;
  762. break;
  763. }
  764. /* Compact branch: BNEZC || JIALC */
  765. if (!insn.i_format.rs) {
  766. /* JIALC: set $31/ra */
  767. regs->regs[31] = epc + 4;
  768. }
  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. pr_info("%s: DSP branch but not DSP ASE - sending SIGILL.\n",
  791. current->comm);
  792. force_sig(SIGILL, current);
  793. return -EFAULT;
  794. sigill_r2r6:
  795. pr_info("%s: R2 branch but r2-to-r6 emulator is not present - 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. }