align.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * arch/xtensa/kernel/align.S
  3. *
  4. * Handle unalignment exceptions in kernel space.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file "COPYING" in the main directory of
  8. * this archive for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica, Inc.
  11. * Copyright (C) 2014 Cadence Design Systems Inc.
  12. *
  13. * Rewritten by Chris Zankel <chris@zankel.net>
  14. *
  15. * Based on work from Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  16. * and Marc Gauthier <marc@tensilica.com, marc@alimni.uwaterloo.ca>
  17. */
  18. #include <linux/linkage.h>
  19. #include <asm/current.h>
  20. #include <asm/asm-offsets.h>
  21. #include <asm/processor.h>
  22. #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
  23. /* First-level exception handler for unaligned exceptions.
  24. *
  25. * Note: This handler works only for kernel exceptions. Unaligned user
  26. * access should get a seg fault.
  27. */
  28. /* Big and little endian 16-bit values are located in
  29. * different halves of a register. HWORD_START helps to
  30. * abstract the notion of extracting a 16-bit value from a
  31. * register.
  32. * We also have to define new shifting instructions because
  33. * lsb and msb are on 'opposite' ends in a register for
  34. * different endian machines.
  35. *
  36. * Assume a memory region in ascending address:
  37. * 0 1 2 3|4 5 6 7
  38. *
  39. * When loading one word into a register, the content of that register is:
  40. * LE 3 2 1 0, 7 6 5 4
  41. * BE 0 1 2 3, 4 5 6 7
  42. *
  43. * Masking the bits of the higher/lower address means:
  44. * LE X X 0 0, 0 0 X X
  45. * BE 0 0 X X, X X 0 0
  46. *
  47. * Shifting to higher/lower addresses, means:
  48. * LE shift left / shift right
  49. * BE shift right / shift left
  50. *
  51. * Extracting 16 bits from a 32 bit reg. value to higher/lower address means:
  52. * LE mask 0 0 X X / shift left
  53. * BE shift left / mask 0 0 X X
  54. */
  55. #define UNALIGNED_USER_EXCEPTION
  56. #if XCHAL_HAVE_BE
  57. #define HWORD_START 16
  58. #define INSN_OP0 28
  59. #define INSN_T 24
  60. #define INSN_OP1 16
  61. .macro __src_b r, w0, w1; src \r, \w0, \w1; .endm
  62. .macro __ssa8 r; ssa8b \r; .endm
  63. .macro __ssa8r r; ssa8l \r; .endm
  64. .macro __sh r, s; srl \r, \s; .endm
  65. .macro __sl r, s; sll \r, \s; .endm
  66. .macro __exth r, s; extui \r, \s, 0, 16; .endm
  67. .macro __extl r, s; slli \r, \s, 16; .endm
  68. #else
  69. #define HWORD_START 0
  70. #define INSN_OP0 0
  71. #define INSN_T 4
  72. #define INSN_OP1 12
  73. .macro __src_b r, w0, w1; src \r, \w1, \w0; .endm
  74. .macro __ssa8 r; ssa8l \r; .endm
  75. .macro __ssa8r r; ssa8b \r; .endm
  76. .macro __sh r, s; sll \r, \s; .endm
  77. .macro __sl r, s; srl \r, \s; .endm
  78. .macro __exth r, s; slli \r, \s, 16; .endm
  79. .macro __extl r, s; extui \r, \s, 0, 16; .endm
  80. #endif
  81. /*
  82. * xxxx xxxx = imm8 field
  83. * yyyy = imm4 field
  84. * ssss = s field
  85. * tttt = t field
  86. *
  87. * 16 0
  88. * -------------------
  89. * L32I.N yyyy ssss tttt 1000
  90. * S32I.N yyyy ssss tttt 1001
  91. *
  92. * 23 0
  93. * -----------------------------
  94. * res 0000 0010
  95. * L16UI xxxx xxxx 0001 ssss tttt 0010
  96. * L32I xxxx xxxx 0010 ssss tttt 0010
  97. * XXX 0011 ssss tttt 0010
  98. * XXX 0100 ssss tttt 0010
  99. * S16I xxxx xxxx 0101 ssss tttt 0010
  100. * S32I xxxx xxxx 0110 ssss tttt 0010
  101. * XXX 0111 ssss tttt 0010
  102. * XXX 1000 ssss tttt 0010
  103. * L16SI xxxx xxxx 1001 ssss tttt 0010
  104. * XXX 1010 0010
  105. * **L32AI xxxx xxxx 1011 ssss tttt 0010 unsupported
  106. * XXX 1100 0010
  107. * XXX 1101 0010
  108. * XXX 1110 0010
  109. * **S32RI xxxx xxxx 1111 ssss tttt 0010 unsupported
  110. * -----------------------------
  111. * ^ ^ ^
  112. * sub-opcode (NIBBLE_R) -+ | |
  113. * t field (NIBBLE_T) -----------+ |
  114. * major opcode (NIBBLE_OP0) --------------+
  115. */
  116. #define OP0_L32I_N 0x8 /* load immediate narrow */
  117. #define OP0_S32I_N 0x9 /* store immediate narrow */
  118. #define OP1_SI_MASK 0x4 /* OP1 bit set for stores */
  119. #define OP1_SI_BIT 2 /* OP1 bit number for stores */
  120. #define OP1_L32I 0x2
  121. #define OP1_L16UI 0x1
  122. #define OP1_L16SI 0x9
  123. #define OP1_L32AI 0xb
  124. #define OP1_S32I 0x6
  125. #define OP1_S16I 0x5
  126. #define OP1_S32RI 0xf
  127. /*
  128. * Entry condition:
  129. *
  130. * a0: trashed, original value saved on stack (PT_AREG0)
  131. * a1: a1
  132. * a2: new stack pointer, original in DEPC
  133. * a3: a3
  134. * depc: a2, original value saved on stack (PT_DEPC)
  135. * excsave_1: dispatch table
  136. *
  137. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  138. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  139. */
  140. ENTRY(fast_unaligned)
  141. /* Note: We don't expect the address to be aligned on a word
  142. * boundary. After all, the processor generated that exception
  143. * and it would be a hardware fault.
  144. */
  145. /* Save some working register */
  146. s32i a4, a2, PT_AREG4
  147. s32i a5, a2, PT_AREG5
  148. s32i a6, a2, PT_AREG6
  149. s32i a7, a2, PT_AREG7
  150. s32i a8, a2, PT_AREG8
  151. rsr a0, depc
  152. s32i a0, a2, PT_AREG2
  153. s32i a3, a2, PT_AREG3
  154. rsr a3, excsave1
  155. movi a4, fast_unaligned_fixup
  156. s32i a4, a3, EXC_TABLE_FIXUP
  157. /* Keep value of SAR in a0 */
  158. rsr a0, sar
  159. rsr a8, excvaddr # load unaligned memory address
  160. /* Now, identify one of the following load/store instructions.
  161. *
  162. * The only possible danger of a double exception on the
  163. * following l32i instructions is kernel code in vmalloc
  164. * memory. The processor was just executing at the EPC_1
  165. * address, and indeed, already fetched the instruction. That
  166. * guarantees a TLB mapping, which hasn't been replaced by
  167. * this unaligned exception handler that uses only static TLB
  168. * mappings. However, high-level interrupt handlers might
  169. * modify TLB entries, so for the generic case, we register a
  170. * TABLE_FIXUP handler here, too.
  171. */
  172. /* a3...a6 saved on stack, a2 = SP */
  173. /* Extract the instruction that caused the unaligned access. */
  174. rsr a7, epc1 # load exception address
  175. movi a3, ~3
  176. and a3, a3, a7 # mask lower bits
  177. l32i a4, a3, 0 # load 2 words
  178. l32i a5, a3, 4
  179. __ssa8 a7
  180. __src_b a4, a4, a5 # a4 has the instruction
  181. /* Analyze the instruction (load or store?). */
  182. extui a5, a4, INSN_OP0, 4 # get insn.op0 nibble
  183. #if XCHAL_HAVE_DENSITY
  184. _beqi a5, OP0_L32I_N, .Lload # L32I.N, jump
  185. addi a6, a5, -OP0_S32I_N
  186. _beqz a6, .Lstore # S32I.N, do a store
  187. #endif
  188. /* 'store indicator bit' not set, jump */
  189. _bbci.l a4, OP1_SI_BIT + INSN_OP1, .Lload
  190. /* Store: Jump to table entry to get the value in the source register.*/
  191. .Lstore:movi a5, .Lstore_table # table
  192. extui a6, a4, INSN_T, 4 # get source register
  193. addx8 a5, a6, a5
  194. jx a5 # jump into table
  195. /* Load: Load memory address. */
  196. .Lload: movi a3, ~3
  197. and a3, a3, a8 # align memory address
  198. __ssa8 a8
  199. #ifdef UNALIGNED_USER_EXCEPTION
  200. addi a3, a3, 8
  201. l32e a5, a3, -8
  202. l32e a6, a3, -4
  203. #else
  204. l32i a5, a3, 0
  205. l32i a6, a3, 4
  206. #endif
  207. __src_b a3, a5, a6 # a3 has the data word
  208. #if XCHAL_HAVE_DENSITY
  209. addi a7, a7, 2 # increment PC (assume 16-bit insn)
  210. extui a5, a4, INSN_OP0, 4
  211. _beqi a5, OP0_L32I_N, 1f # l32i.n: jump
  212. addi a7, a7, 1
  213. #else
  214. addi a7, a7, 3
  215. #endif
  216. extui a5, a4, INSN_OP1, 4
  217. _beqi a5, OP1_L32I, 1f # l32i: jump
  218. extui a3, a3, 0, 16 # extract lower 16 bits
  219. _beqi a5, OP1_L16UI, 1f
  220. addi a5, a5, -OP1_L16SI
  221. _bnez a5, .Linvalid_instruction_load
  222. /* sign extend value */
  223. slli a3, a3, 16
  224. srai a3, a3, 16
  225. /* Set target register. */
  226. 1:
  227. extui a4, a4, INSN_T, 4 # extract target register
  228. movi a5, .Lload_table
  229. addx8 a4, a4, a5
  230. jx a4 # jump to entry for target register
  231. .align 8
  232. .Lload_table:
  233. s32i a3, a2, PT_AREG0; _j .Lexit; .align 8
  234. mov a1, a3; _j .Lexit; .align 8 # fishy??
  235. s32i a3, a2, PT_AREG2; _j .Lexit; .align 8
  236. s32i a3, a2, PT_AREG3; _j .Lexit; .align 8
  237. s32i a3, a2, PT_AREG4; _j .Lexit; .align 8
  238. s32i a3, a2, PT_AREG5; _j .Lexit; .align 8
  239. s32i a3, a2, PT_AREG6; _j .Lexit; .align 8
  240. s32i a3, a2, PT_AREG7; _j .Lexit; .align 8
  241. s32i a3, a2, PT_AREG8; _j .Lexit; .align 8
  242. mov a9, a3 ; _j .Lexit; .align 8
  243. mov a10, a3 ; _j .Lexit; .align 8
  244. mov a11, a3 ; _j .Lexit; .align 8
  245. mov a12, a3 ; _j .Lexit; .align 8
  246. mov a13, a3 ; _j .Lexit; .align 8
  247. mov a14, a3 ; _j .Lexit; .align 8
  248. mov a15, a3 ; _j .Lexit; .align 8
  249. .Lstore_table:
  250. l32i a3, a2, PT_AREG0; _j 1f; .align 8
  251. mov a3, a1; _j 1f; .align 8 # fishy??
  252. l32i a3, a2, PT_AREG2; _j 1f; .align 8
  253. l32i a3, a2, PT_AREG3; _j 1f; .align 8
  254. l32i a3, a2, PT_AREG4; _j 1f; .align 8
  255. l32i a3, a2, PT_AREG5; _j 1f; .align 8
  256. l32i a3, a2, PT_AREG6; _j 1f; .align 8
  257. l32i a3, a2, PT_AREG7; _j 1f; .align 8
  258. l32i a3, a2, PT_AREG8; _j 1f; .align 8
  259. mov a3, a9 ; _j 1f; .align 8
  260. mov a3, a10 ; _j 1f; .align 8
  261. mov a3, a11 ; _j 1f; .align 8
  262. mov a3, a12 ; _j 1f; .align 8
  263. mov a3, a13 ; _j 1f; .align 8
  264. mov a3, a14 ; _j 1f; .align 8
  265. mov a3, a15 ; _j 1f; .align 8
  266. /* We cannot handle this exception. */
  267. .extern _kernel_exception
  268. .Linvalid_instruction_load:
  269. .Linvalid_instruction_store:
  270. movi a4, 0
  271. rsr a3, excsave1
  272. s32i a4, a3, EXC_TABLE_FIXUP
  273. /* Restore a4...a8 and SAR, set SP, and jump to default exception. */
  274. l32i a8, a2, PT_AREG8
  275. l32i a7, a2, PT_AREG7
  276. l32i a6, a2, PT_AREG6
  277. l32i a5, a2, PT_AREG5
  278. l32i a4, a2, PT_AREG4
  279. wsr a0, sar
  280. mov a1, a2
  281. rsr a0, ps
  282. bbsi.l a0, PS_UM_BIT, 2f # jump if user mode
  283. movi a0, _kernel_exception
  284. jx a0
  285. 2: movi a0, _user_exception
  286. jx a0
  287. 1: # a7: instruction pointer, a4: instruction, a3: value
  288. movi a6, 0 # mask: ffffffff:00000000
  289. #if XCHAL_HAVE_DENSITY
  290. addi a7, a7, 2 # incr. PC,assume 16-bit instruction
  291. extui a5, a4, INSN_OP0, 4 # extract OP0
  292. addi a5, a5, -OP0_S32I_N
  293. _beqz a5, 1f # s32i.n: jump
  294. addi a7, a7, 1 # increment PC, 32-bit instruction
  295. #else
  296. addi a7, a7, 3 # increment PC, 32-bit instruction
  297. #endif
  298. extui a5, a4, INSN_OP1, 4 # extract OP1
  299. _beqi a5, OP1_S32I, 1f # jump if 32 bit store
  300. _bnei a5, OP1_S16I, .Linvalid_instruction_store
  301. movi a5, -1
  302. __extl a3, a3 # get 16-bit value
  303. __exth a6, a5 # get 16-bit mask ffffffff:ffff0000
  304. /* Get memory address */
  305. 1:
  306. movi a4, ~3
  307. and a4, a4, a8 # align memory address
  308. /* Insert value into memory */
  309. movi a5, -1 # mask: ffffffff:XXXX0000
  310. #ifdef UNALIGNED_USER_EXCEPTION
  311. addi a4, a4, 8
  312. #endif
  313. __ssa8r a8
  314. __src_b a8, a5, a6 # lo-mask F..F0..0 (BE) 0..0F..F (LE)
  315. __src_b a6, a6, a5 # hi-mask 0..0F..F (BE) F..F0..0 (LE)
  316. #ifdef UNALIGNED_USER_EXCEPTION
  317. l32e a5, a4, -8
  318. #else
  319. l32i a5, a4, 0 # load lower address word
  320. #endif
  321. and a5, a5, a8 # mask
  322. __sh a8, a3 # shift value
  323. or a5, a5, a8 # or with original value
  324. #ifdef UNALIGNED_USER_EXCEPTION
  325. s32e a5, a4, -8
  326. l32e a8, a4, -4
  327. #else
  328. s32i a5, a4, 0 # store
  329. l32i a8, a4, 4 # same for upper address word
  330. #endif
  331. __sl a5, a3
  332. and a6, a8, a6
  333. or a6, a6, a5
  334. #ifdef UNALIGNED_USER_EXCEPTION
  335. s32e a6, a4, -4
  336. #else
  337. s32i a6, a4, 4
  338. #endif
  339. .Lexit:
  340. #if XCHAL_HAVE_LOOPS
  341. rsr a4, lend # check if we reached LEND
  342. bne a7, a4, 1f
  343. rsr a4, lcount # and LCOUNT != 0
  344. beqz a4, 1f
  345. addi a4, a4, -1 # decrement LCOUNT and set
  346. rsr a7, lbeg # set PC to LBEGIN
  347. wsr a4, lcount
  348. #endif
  349. 1: wsr a7, epc1 # skip emulated instruction
  350. /* Update icount if we're single-stepping in userspace. */
  351. rsr a4, icountlevel
  352. beqz a4, 1f
  353. bgeui a4, LOCKLEVEL + 1, 1f
  354. rsr a4, icount
  355. addi a4, a4, 1
  356. wsr a4, icount
  357. 1:
  358. movi a4, 0
  359. rsr a3, excsave1
  360. s32i a4, a3, EXC_TABLE_FIXUP
  361. /* Restore working register */
  362. l32i a8, a2, PT_AREG8
  363. l32i a7, a2, PT_AREG7
  364. l32i a6, a2, PT_AREG6
  365. l32i a5, a2, PT_AREG5
  366. l32i a4, a2, PT_AREG4
  367. l32i a3, a2, PT_AREG3
  368. /* restore SAR and return */
  369. wsr a0, sar
  370. l32i a0, a2, PT_AREG0
  371. l32i a2, a2, PT_AREG2
  372. rfe
  373. ENDPROC(fast_unaligned)
  374. ENTRY(fast_unaligned_fixup)
  375. l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
  376. wsr a3, excsave1
  377. l32i a8, a2, PT_AREG8
  378. l32i a7, a2, PT_AREG7
  379. l32i a6, a2, PT_AREG6
  380. l32i a5, a2, PT_AREG5
  381. l32i a4, a2, PT_AREG4
  382. l32i a0, a2, PT_AREG2
  383. xsr a0, depc # restore depc and a0
  384. wsr a0, sar
  385. rsr a0, exccause
  386. s32i a0, a2, PT_DEPC # mark as a regular exception
  387. rsr a0, ps
  388. bbsi.l a0, PS_UM_BIT, 1f # jump if user mode
  389. rsr a0, exccause
  390. addx4 a0, a0, a3 # find entry in table
  391. l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler
  392. l32i a3, a2, PT_AREG3
  393. jx a0
  394. 1:
  395. rsr a0, exccause
  396. addx4 a0, a0, a3 # find entry in table
  397. l32i a0, a0, EXC_TABLE_FAST_USER # load handler
  398. l32i a3, a2, PT_AREG3
  399. jx a0
  400. ENDPROC(fast_unaligned_fixup)
  401. #endif /* XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION */