align.S 11 KB

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