insnemu.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2003-2013 Altera Corporation
  4. * All rights reserved.
  5. */
  6. #include <linux/linkage.h>
  7. #include <asm/entry.h>
  8. .set noat
  9. .set nobreak
  10. /*
  11. * Explicitly allow the use of r1 (the assembler temporary register)
  12. * within this code. This register is normally reserved for the use of
  13. * the compiler.
  14. */
  15. ENTRY(instruction_trap)
  16. ldw r1, PT_R1(sp) // Restore registers
  17. ldw r2, PT_R2(sp)
  18. ldw r3, PT_R3(sp)
  19. ldw r4, PT_R4(sp)
  20. ldw r5, PT_R5(sp)
  21. ldw r6, PT_R6(sp)
  22. ldw r7, PT_R7(sp)
  23. ldw r8, PT_R8(sp)
  24. ldw r9, PT_R9(sp)
  25. ldw r10, PT_R10(sp)
  26. ldw r11, PT_R11(sp)
  27. ldw r12, PT_R12(sp)
  28. ldw r13, PT_R13(sp)
  29. ldw r14, PT_R14(sp)
  30. ldw r15, PT_R15(sp)
  31. ldw ra, PT_RA(sp)
  32. ldw fp, PT_FP(sp)
  33. ldw gp, PT_GP(sp)
  34. ldw et, PT_ESTATUS(sp)
  35. wrctl estatus, et
  36. ldw ea, PT_EA(sp)
  37. ldw et, PT_SP(sp) /* backup sp in et */
  38. addi sp, sp, PT_REGS_SIZE
  39. /* INSTRUCTION EMULATION
  40. * ---------------------
  41. *
  42. * Nios II processors generate exceptions for unimplemented instructions.
  43. * The routines below emulate these instructions. Depending on the
  44. * processor core, the only instructions that might need to be emulated
  45. * are div, divu, mul, muli, mulxss, mulxsu, and mulxuu.
  46. *
  47. * The emulations match the instructions, except for the following
  48. * limitations:
  49. *
  50. * 1) The emulation routines do not emulate the use of the exception
  51. * temporary register (et) as a source operand because the exception
  52. * handler already has modified it.
  53. *
  54. * 2) The routines do not emulate the use of the stack pointer (sp) or
  55. * the exception return address register (ea) as a destination because
  56. * modifying these registers crashes the exception handler or the
  57. * interrupted routine.
  58. *
  59. * Detailed Design
  60. * ---------------
  61. *
  62. * The emulation routines expect the contents of integer registers r0-r31
  63. * to be on the stack at addresses sp, 4(sp), 8(sp), ... 124(sp). The
  64. * routines retrieve source operands from the stack and modify the
  65. * destination register's value on the stack prior to the end of the
  66. * exception handler. Then all registers except the destination register
  67. * are restored to their previous values.
  68. *
  69. * The instruction that causes the exception is found at address -4(ea).
  70. * The instruction's OP and OPX fields identify the operation to be
  71. * performed.
  72. *
  73. * One instruction, muli, is an I-type instruction that is identified by
  74. * an OP field of 0x24.
  75. *
  76. * muli AAAAA,BBBBB,IIIIIIIIIIIIIIII,-0x24-
  77. * 27 22 6 0 <-- LSB of field
  78. *
  79. * The remaining emulated instructions are R-type and have an OP field
  80. * of 0x3a. Their OPX fields identify them.
  81. *
  82. * R-type AAAAA,BBBBB,CCCCC,XXXXXX,NNNNN,-0x3a-
  83. * 27 22 17 11 6 0 <-- LSB of field
  84. *
  85. *
  86. * Opcode Encoding. muli is identified by its OP value. Then OPX & 0x02
  87. * is used to differentiate between the division opcodes and the
  88. * remaining multiplication opcodes.
  89. *
  90. * Instruction OP OPX OPX & 0x02
  91. * ----------- ---- ---- ----------
  92. * muli 0x24
  93. * divu 0x3a 0x24 0
  94. * div 0x3a 0x25 0
  95. * mul 0x3a 0x27 != 0
  96. * mulxuu 0x3a 0x07 != 0
  97. * mulxsu 0x3a 0x17 != 0
  98. * mulxss 0x3a 0x1f != 0
  99. */
  100. /*
  101. * Save everything on the stack to make it easy for the emulation
  102. * routines to retrieve the source register operands.
  103. */
  104. addi sp, sp, -128
  105. stw zero, 0(sp) /* Save zero on stack to avoid special case for r0. */
  106. stw r1, 4(sp)
  107. stw r2, 8(sp)
  108. stw r3, 12(sp)
  109. stw r4, 16(sp)
  110. stw r5, 20(sp)
  111. stw r6, 24(sp)
  112. stw r7, 28(sp)
  113. stw r8, 32(sp)
  114. stw r9, 36(sp)
  115. stw r10, 40(sp)
  116. stw r11, 44(sp)
  117. stw r12, 48(sp)
  118. stw r13, 52(sp)
  119. stw r14, 56(sp)
  120. stw r15, 60(sp)
  121. stw r16, 64(sp)
  122. stw r17, 68(sp)
  123. stw r18, 72(sp)
  124. stw r19, 76(sp)
  125. stw r20, 80(sp)
  126. stw r21, 84(sp)
  127. stw r22, 88(sp)
  128. stw r23, 92(sp)
  129. /* Don't bother to save et. It's already been changed. */
  130. rdctl r5, estatus
  131. stw r5, 100(sp)
  132. stw gp, 104(sp)
  133. stw et, 108(sp) /* et contains previous sp value. */
  134. stw fp, 112(sp)
  135. stw ea, 116(sp)
  136. stw ra, 120(sp)
  137. /*
  138. * Split the instruction into its fields. We need 4*A, 4*B, and 4*C as
  139. * offsets to the stack pointer for access to the stored register values.
  140. */
  141. ldw r2,-4(ea) /* r2 = AAAAA,BBBBB,IIIIIIIIIIIIIIII,PPPPPP */
  142. roli r3, r2, 7 /* r3 = BBB,IIIIIIIIIIIIIIII,PPPPPP,AAAAA,BB */
  143. roli r4, r3, 3 /* r4 = IIIIIIIIIIIIIIII,PPPPPP,AAAAA,BBBBB */
  144. roli r5, r4, 2 /* r5 = IIIIIIIIIIIIII,PPPPPP,AAAAA,BBBBB,II */
  145. srai r4, r4, 16 /* r4 = (sign-extended) IMM16 */
  146. roli r6, r5, 5 /* r6 = XXXX,NNNNN,PPPPPP,AAAAA,BBBBB,CCCCC,XX */
  147. andi r2, r2, 0x3f /* r2 = 00000000000000000000000000,PPPPPP */
  148. andi r3, r3, 0x7c /* r3 = 0000000000000000000000000,AAAAA,00 */
  149. andi r5, r5, 0x7c /* r5 = 0000000000000000000000000,BBBBB,00 */
  150. andi r6, r6, 0x7c /* r6 = 0000000000000000000000000,CCCCC,00 */
  151. /* Now
  152. * r2 = OP
  153. * r3 = 4*A
  154. * r4 = IMM16 (sign extended)
  155. * r5 = 4*B
  156. * r6 = 4*C
  157. */
  158. /*
  159. * Get the operands.
  160. *
  161. * It is necessary to check for muli because it uses an I-type
  162. * instruction format, while the other instructions are have an R-type
  163. * format.
  164. *
  165. * Prepare for either multiplication or division loop.
  166. * They both loop 32 times.
  167. */
  168. movi r14, 32
  169. add r3, r3, sp /* r3 = address of A-operand. */
  170. ldw r3, 0(r3) /* r3 = A-operand. */
  171. movi r7, 0x24 /* muli opcode (I-type instruction format) */
  172. beq r2, r7, mul_immed /* muli doesn't use the B register as a source */
  173. add r5, r5, sp /* r5 = address of B-operand. */
  174. ldw r5, 0(r5) /* r5 = B-operand. */
  175. /* r4 = SSSSSSSSSSSSSSSS,-----IMM16------ */
  176. /* IMM16 not needed, align OPX portion */
  177. /* r4 = SSSSSSSSSSSSSSSS,CCCCC,-OPX--,00000 */
  178. srli r4, r4, 5 /* r4 = 00000,SSSSSSSSSSSSSSSS,CCCCC,-OPX-- */
  179. andi r4, r4, 0x3f /* r4 = 00000000000000000000000000,-OPX-- */
  180. /* Now
  181. * r2 = OP
  182. * r3 = src1
  183. * r5 = src2
  184. * r4 = OPX (no longer can be muli)
  185. * r6 = 4*C
  186. */
  187. /*
  188. * Multiply or Divide?
  189. */
  190. andi r7, r4, 0x02 /* For R-type multiply instructions,
  191. OPX & 0x02 != 0 */
  192. bne r7, zero, multiply
  193. /* DIVISION
  194. *
  195. * Divide an unsigned dividend by an unsigned divisor using
  196. * a shift-and-subtract algorithm. The example below shows
  197. * 43 div 7 = 6 for 8-bit integers. This classic algorithm uses a
  198. * single register to store both the dividend and the quotient,
  199. * allowing both values to be shifted with a single instruction.
  200. *
  201. * remainder dividend:quotient
  202. * --------- -----------------
  203. * initialize 00000000 00101011:
  204. * shift 00000000 0101011:_
  205. * remainder >= divisor? no 00000000 0101011:0
  206. * shift 00000000 101011:0_
  207. * remainder >= divisor? no 00000000 101011:00
  208. * shift 00000001 01011:00_
  209. * remainder >= divisor? no 00000001 01011:000
  210. * shift 00000010 1011:000_
  211. * remainder >= divisor? no 00000010 1011:0000
  212. * shift 00000101 011:0000_
  213. * remainder >= divisor? no 00000101 011:00000
  214. * shift 00001010 11:00000_
  215. * remainder >= divisor? yes 00001010 11:000001
  216. * remainder -= divisor - 00000111
  217. * ----------
  218. * 00000011 11:000001
  219. * shift 00000111 1:000001_
  220. * remainder >= divisor? yes 00000111 1:0000011
  221. * remainder -= divisor - 00000111
  222. * ----------
  223. * 00000000 1:0000011
  224. * shift 00000001 :0000011_
  225. * remainder >= divisor? no 00000001 :00000110
  226. *
  227. * The quotient is 00000110.
  228. */
  229. divide:
  230. /*
  231. * Prepare for division by assuming the result
  232. * is unsigned, and storing its "sign" as 0.
  233. */
  234. movi r17, 0
  235. /* Which division opcode? */
  236. xori r7, r4, 0x25 /* OPX of div */
  237. bne r7, zero, unsigned_division
  238. /*
  239. * OPX is div. Determine and store the sign of the quotient.
  240. * Then take the absolute value of both operands.
  241. */
  242. xor r17, r3, r5 /* MSB contains sign of quotient */
  243. bge r3,zero,dividend_is_nonnegative
  244. sub r3, zero, r3 /* -r3 */
  245. dividend_is_nonnegative:
  246. bge r5, zero, divisor_is_nonnegative
  247. sub r5, zero, r5 /* -r5 */
  248. divisor_is_nonnegative:
  249. unsigned_division:
  250. /* Initialize the unsigned-division loop. */
  251. movi r13, 0 /* remainder = 0 */
  252. /* Now
  253. * r3 = dividend : quotient
  254. * r4 = 0x25 for div, 0x24 for divu
  255. * r5 = divisor
  256. * r13 = remainder
  257. * r14 = loop counter (already initialized to 32)
  258. * r17 = MSB contains sign of quotient
  259. */
  260. /*
  261. * for (count = 32; count > 0; --count)
  262. * {
  263. */
  264. divide_loop:
  265. /*
  266. * Division:
  267. *
  268. * (remainder:dividend:quotient) <<= 1;
  269. */
  270. slli r13, r13, 1
  271. cmplt r7, r3, zero /* r7 = MSB of r3 */
  272. or r13, r13, r7
  273. slli r3, r3, 1
  274. /*
  275. * if (remainder >= divisor)
  276. * {
  277. * set LSB of quotient
  278. * remainder -= divisor;
  279. * }
  280. */
  281. bltu r13, r5, div_skip
  282. ori r3, r3, 1
  283. sub r13, r13, r5
  284. div_skip:
  285. /*
  286. * }
  287. */
  288. subi r14, r14, 1
  289. bne r14, zero, divide_loop
  290. /* Now
  291. * r3 = quotient
  292. * r4 = 0x25 for div, 0x24 for divu
  293. * r6 = 4*C
  294. * r17 = MSB contains sign of quotient
  295. */
  296. /*
  297. * Conditionally negate signed quotient. If quotient is unsigned,
  298. * the sign already is initialized to 0.
  299. */
  300. bge r17, zero, quotient_is_nonnegative
  301. sub r3, zero, r3 /* -r3 */
  302. quotient_is_nonnegative:
  303. /*
  304. * Final quotient is in r3.
  305. */
  306. add r6, r6, sp
  307. stw r3, 0(r6) /* write quotient to stack */
  308. br restore_registers
  309. /* MULTIPLICATION
  310. *
  311. * A "product" is the number that one gets by summing a "multiplicand"
  312. * several times. The "multiplier" specifies the number of copies of the
  313. * multiplicand that are summed.
  314. *
  315. * Actual multiplication algorithms don't use repeated addition, however.
  316. * Shift-and-add algorithms get the same answer as repeated addition, and
  317. * they are faster. To compute the lower half of a product (pppp below)
  318. * one shifts the product left before adding in each of the partial
  319. * products (a * mmmm) through (d * mmmm).
  320. *
  321. * To compute the upper half of a product (PPPP below), one adds in the
  322. * partial products (d * mmmm) through (a * mmmm), each time following
  323. * the add by a right shift of the product.
  324. *
  325. * mmmm
  326. * * abcd
  327. * ------
  328. * #### = d * mmmm
  329. * #### = c * mmmm
  330. * #### = b * mmmm
  331. * #### = a * mmmm
  332. * --------
  333. * PPPPpppp
  334. *
  335. * The example above shows 4 partial products. Computing actual Nios II
  336. * products requires 32 partials.
  337. *
  338. * It is possible to compute the result of mulxsu from the result of
  339. * mulxuu because the only difference between the results of these two
  340. * opcodes is the value of the partial product associated with the sign
  341. * bit of rA.
  342. *
  343. * mulxsu = mulxuu - (rA < 0) ? rB : 0;
  344. *
  345. * It is possible to compute the result of mulxss from the result of
  346. * mulxsu because the only difference between the results of these two
  347. * opcodes is the value of the partial product associated with the sign
  348. * bit of rB.
  349. *
  350. * mulxss = mulxsu - (rB < 0) ? rA : 0;
  351. *
  352. */
  353. mul_immed:
  354. /* Opcode is muli. Change it into mul for remainder of algorithm. */
  355. mov r6, r5 /* Field B is dest register, not field C. */
  356. mov r5, r4 /* Field IMM16 is src2, not field B. */
  357. movi r4, 0x27 /* OPX of mul is 0x27 */
  358. multiply:
  359. /* Initialize the multiplication loop. */
  360. movi r9, 0 /* mul_product = 0 */
  361. movi r10, 0 /* mulxuu_product = 0 */
  362. mov r11, r5 /* save original multiplier for mulxsu and mulxss */
  363. mov r12, r5 /* mulxuu_multiplier (will be shifted) */
  364. movi r16, 1 /* used to create "rori B,A,1" from "ror B,A,r16" */
  365. /* Now
  366. * r3 = multiplicand
  367. * r5 = mul_multiplier
  368. * r6 = 4 * dest_register (used later as offset to sp)
  369. * r7 = temp
  370. * r9 = mul_product
  371. * r10 = mulxuu_product
  372. * r11 = original multiplier
  373. * r12 = mulxuu_multiplier
  374. * r14 = loop counter (already initialized)
  375. * r16 = 1
  376. */
  377. /*
  378. * for (count = 32; count > 0; --count)
  379. * {
  380. */
  381. multiply_loop:
  382. /*
  383. * mul_product <<= 1;
  384. * lsb = multiplier & 1;
  385. */
  386. slli r9, r9, 1
  387. andi r7, r12, 1
  388. /*
  389. * if (lsb == 1)
  390. * {
  391. * mulxuu_product += multiplicand;
  392. * }
  393. */
  394. beq r7, zero, mulx_skip
  395. add r10, r10, r3
  396. cmpltu r7, r10, r3 /* Save the carry from the MSB of mulxuu_product. */
  397. ror r7, r7, r16 /* r7 = 0x80000000 on carry, or else 0x00000000 */
  398. mulx_skip:
  399. /*
  400. * if (MSB of mul_multiplier == 1)
  401. * {
  402. * mul_product += multiplicand;
  403. * }
  404. */
  405. bge r5, zero, mul_skip
  406. add r9, r9, r3
  407. mul_skip:
  408. /*
  409. * mulxuu_product >>= 1; logical shift
  410. * mul_multiplier <<= 1; done with MSB
  411. * mulx_multiplier >>= 1; done with LSB
  412. */
  413. srli r10, r10, 1
  414. or r10, r10, r7 /* OR in the saved carry bit. */
  415. slli r5, r5, 1
  416. srli r12, r12, 1
  417. /*
  418. * }
  419. */
  420. subi r14, r14, 1
  421. bne r14, zero, multiply_loop
  422. /*
  423. * Multiply emulation loop done.
  424. */
  425. /* Now
  426. * r3 = multiplicand
  427. * r4 = OPX
  428. * r6 = 4 * dest_register (used later as offset to sp)
  429. * r7 = temp
  430. * r9 = mul_product
  431. * r10 = mulxuu_product
  432. * r11 = original multiplier
  433. */
  434. /* Calculate address for result from 4 * dest_register */
  435. add r6, r6, sp
  436. /*
  437. * Select/compute the result based on OPX.
  438. */
  439. /* OPX == mul? Then store. */
  440. xori r7, r4, 0x27
  441. beq r7, zero, store_product
  442. /* It's one of the mulx.. opcodes. Move over the result. */
  443. mov r9, r10
  444. /* OPX == mulxuu? Then store. */
  445. xori r7, r4, 0x07
  446. beq r7, zero, store_product
  447. /* Compute mulxsu
  448. *
  449. * mulxsu = mulxuu - (rA < 0) ? rB : 0;
  450. */
  451. bge r3, zero, mulxsu_skip
  452. sub r9, r9, r11
  453. mulxsu_skip:
  454. /* OPX == mulxsu? Then store. */
  455. xori r7, r4, 0x17
  456. beq r7, zero, store_product
  457. /* Compute mulxss
  458. *
  459. * mulxss = mulxsu - (rB < 0) ? rA : 0;
  460. */
  461. bge r11,zero,mulxss_skip
  462. sub r9, r9, r3
  463. mulxss_skip:
  464. /* At this point, assume that OPX is mulxss, so store*/
  465. store_product:
  466. stw r9, 0(r6)
  467. restore_registers:
  468. /* No need to restore r0. */
  469. ldw r5, 100(sp)
  470. wrctl estatus, r5
  471. ldw r1, 4(sp)
  472. ldw r2, 8(sp)
  473. ldw r3, 12(sp)
  474. ldw r4, 16(sp)
  475. ldw r5, 20(sp)
  476. ldw r6, 24(sp)
  477. ldw r7, 28(sp)
  478. ldw r8, 32(sp)
  479. ldw r9, 36(sp)
  480. ldw r10, 40(sp)
  481. ldw r11, 44(sp)
  482. ldw r12, 48(sp)
  483. ldw r13, 52(sp)
  484. ldw r14, 56(sp)
  485. ldw r15, 60(sp)
  486. ldw r16, 64(sp)
  487. ldw r17, 68(sp)
  488. ldw r18, 72(sp)
  489. ldw r19, 76(sp)
  490. ldw r20, 80(sp)
  491. ldw r21, 84(sp)
  492. ldw r22, 88(sp)
  493. ldw r23, 92(sp)
  494. /* Does not need to restore et */
  495. ldw gp, 104(sp)
  496. ldw fp, 112(sp)
  497. ldw ea, 116(sp)
  498. ldw ra, 120(sp)
  499. ldw sp, 108(sp) /* last restore sp */
  500. eret
  501. .set at
  502. .set break