align.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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: dispatch table
  133. * depc: a2, original value saved on stack (PT_DEPC)
  134. * excsave_1: a3
  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. xsr a3, EXCSAVE_1
  152. s32i a0, a2, PT_AREG2
  153. s32i a3, a2, PT_AREG3
  154. /* Keep value of SAR in a0 */
  155. rsr a0, SAR
  156. rsr a8, EXCVADDR # load unaligned memory address
  157. /* Now, identify one of the following load/store instructions.
  158. *
  159. * The only possible danger of a double exception on the
  160. * following l32i instructions is kernel code in vmalloc
  161. * memory. The processor was just executing at the EPC_1
  162. * address, and indeed, already fetched the instruction. That
  163. * guarantees a TLB mapping, which hasn't been replaced by
  164. * this unaligned exception handler that uses only static TLB
  165. * mappings. However, high-level interrupt handlers might
  166. * modify TLB entries, so for the generic case, we register a
  167. * TABLE_FIXUP handler here, too.
  168. */
  169. /* a3...a6 saved on stack, a2 = SP */
  170. /* Extract the instruction that caused the unaligned access. */
  171. rsr a7, EPC_1 # load exception address
  172. movi a3, ~3
  173. and a3, a3, a7 # mask lower bits
  174. l32i a4, a3, 0 # load 2 words
  175. l32i a5, a3, 4
  176. __ssa8 a7
  177. __src_b a4, a4, a5 # a4 has the instruction
  178. /* Analyze the instruction (load or store?). */
  179. extui a5, a4, INSN_OP0, 4 # get insn.op0 nibble
  180. #if XCHAL_HAVE_DENSITY
  181. _beqi a5, OP0_L32I_N, .Lload # L32I.N, jump
  182. addi a6, a5, -OP0_S32I_N
  183. _beqz a6, .Lstore # S32I.N, do a store
  184. #endif
  185. /* 'store indicator bit' not set, jump */
  186. _bbci.l a4, OP1_SI_BIT + INSN_OP1, .Lload
  187. /* Store: Jump to table entry to get the value in the source register.*/
  188. .Lstore:movi a5, .Lstore_table # table
  189. extui a6, a4, INSN_T, 4 # get source register
  190. addx8 a5, a6, a5
  191. jx a5 # jump into table
  192. /* Invalid instruction, CRITICAL! */
  193. .Linvalid_instruction_load:
  194. j .Linvalid_instruction
  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. #if XCHAL_HAVE_LOOPS
  228. rsr a5, LEND # check if we reached LEND
  229. bne a7, a5, 1f
  230. rsr a5, LCOUNT # and LCOUNT != 0
  231. beqz a5, 1f
  232. addi a5, a5, -1 # decrement LCOUNT and set
  233. rsr a7, LBEG # set PC to LBEGIN
  234. wsr a5, LCOUNT
  235. #endif
  236. 1: wsr a7, EPC_1 # skip load instruction
  237. extui a4, a4, INSN_T, 4 # extract target register
  238. movi a5, .Lload_table
  239. addx8 a4, a4, a5
  240. jx a4 # jump to entry for target register
  241. .align 8
  242. .Lload_table:
  243. s32i a3, a2, PT_AREG0; _j .Lexit; .align 8
  244. mov a1, a3; _j .Lexit; .align 8 # fishy??
  245. s32i a3, a2, PT_AREG2; _j .Lexit; .align 8
  246. s32i a3, a2, PT_AREG3; _j .Lexit; .align 8
  247. s32i a3, a2, PT_AREG4; _j .Lexit; .align 8
  248. s32i a3, a2, PT_AREG5; _j .Lexit; .align 8
  249. s32i a3, a2, PT_AREG6; _j .Lexit; .align 8
  250. s32i a3, a2, PT_AREG7; _j .Lexit; .align 8
  251. s32i a3, a2, PT_AREG8; _j .Lexit; .align 8
  252. mov a9, a3 ; _j .Lexit; .align 8
  253. mov a10, a3 ; _j .Lexit; .align 8
  254. mov a11, a3 ; _j .Lexit; .align 8
  255. mov a12, a3 ; _j .Lexit; .align 8
  256. mov a13, a3 ; _j .Lexit; .align 8
  257. mov a14, a3 ; _j .Lexit; .align 8
  258. mov a15, a3 ; _j .Lexit; .align 8
  259. .Lstore_table:
  260. l32i a3, a2, PT_AREG0; _j 1f; .align 8
  261. mov a3, a1; _j 1f; .align 8 # fishy??
  262. l32i a3, a2, PT_AREG2; _j 1f; .align 8
  263. l32i a3, a2, PT_AREG3; _j 1f; .align 8
  264. l32i a3, a2, PT_AREG4; _j 1f; .align 8
  265. l32i a3, a2, PT_AREG5; _j 1f; .align 8
  266. l32i a3, a2, PT_AREG6; _j 1f; .align 8
  267. l32i a3, a2, PT_AREG7; _j 1f; .align 8
  268. l32i a3, a2, PT_AREG8; _j 1f; .align 8
  269. mov a3, a9 ; _j 1f; .align 8
  270. mov a3, a10 ; _j 1f; .align 8
  271. mov a3, a11 ; _j 1f; .align 8
  272. mov a3, a12 ; _j 1f; .align 8
  273. mov a3, a13 ; _j 1f; .align 8
  274. mov a3, a14 ; _j 1f; .align 8
  275. mov a3, a15 ; _j 1f; .align 8
  276. 1: # a7: instruction pointer, a4: instruction, a3: value
  277. movi a6, 0 # mask: ffffffff:00000000
  278. #if XCHAL_HAVE_DENSITY
  279. addi a7, a7, 2 # incr. PC,assume 16-bit instruction
  280. extui a5, a4, INSN_OP0, 4 # extract OP0
  281. addi a5, a5, -OP0_S32I_N
  282. _beqz a5, 1f # s32i.n: jump
  283. addi a7, a7, 1 # increment PC, 32-bit instruction
  284. #else
  285. addi a7, a7, 3 # increment PC, 32-bit instruction
  286. #endif
  287. extui a5, a4, INSN_OP1, 4 # extract OP1
  288. _beqi a5, OP1_S32I, 1f # jump if 32 bit store
  289. _bnei a5, OP1_S16I, .Linvalid_instruction_store
  290. movi a5, -1
  291. __extl a3, a3 # get 16-bit value
  292. __exth a6, a5 # get 16-bit mask ffffffff:ffff0000
  293. /* Get memory address */
  294. 1:
  295. #if XCHAL_HAVE_LOOPS
  296. rsr a4, LEND # check if we reached LEND
  297. bne a7, a4, 1f
  298. rsr a4, LCOUNT # and LCOUNT != 0
  299. beqz a4, 1f
  300. addi a4, a4, -1 # decrement LCOUNT and set
  301. rsr a7, LBEG # set PC to LBEGIN
  302. wsr a4, LCOUNT
  303. #endif
  304. 1: wsr a7, EPC_1 # skip store instruction
  305. movi a4, ~3
  306. and a4, a4, a8 # align memory address
  307. /* Insert value into memory */
  308. movi a5, -1 # mask: ffffffff:XXXX0000
  309. #ifdef UNALIGNED_USER_EXCEPTION
  310. addi a4, a4, 8
  311. #endif
  312. __ssa8r a8
  313. __src_b a7, a5, a6 # lo-mask F..F0..0 (BE) 0..0F..F (LE)
  314. __src_b a6, a6, a5 # hi-mask 0..0F..F (BE) F..F0..0 (LE)
  315. #ifdef UNALIGNED_USER_EXCEPTION
  316. l32e a5, a4, -8
  317. #else
  318. l32i a5, a4, 0 # load lower address word
  319. #endif
  320. and a5, a5, a7 # mask
  321. __sh a7, a3 # shift value
  322. or a5, a5, a7 # or with original value
  323. #ifdef UNALIGNED_USER_EXCEPTION
  324. s32e a5, a4, -8
  325. l32e a7, a4, -4
  326. #else
  327. s32i a5, a4, 0 # store
  328. l32i a7, a4, 4 # same for upper address word
  329. #endif
  330. __sl a5, a3
  331. and a6, a7, a6
  332. or a6, a6, a5
  333. #ifdef UNALIGNED_USER_EXCEPTION
  334. s32e a6, a4, -4
  335. #else
  336. s32i a6, a4, 4
  337. #endif
  338. /* Done. restore stack and return */
  339. .Lexit:
  340. movi a4, 0
  341. rsr a3, EXCSAVE_1
  342. s32i a4, a3, EXC_TABLE_FIXUP
  343. /* Restore working register */
  344. l32i a8, a2, PT_AREG8
  345. l32i a7, a2, PT_AREG7
  346. l32i a6, a2, PT_AREG6
  347. l32i a5, a2, PT_AREG5
  348. l32i a4, a2, PT_AREG4
  349. l32i a3, a2, PT_AREG3
  350. /* restore SAR and return */
  351. wsr a0, SAR
  352. l32i a0, a2, PT_AREG0
  353. l32i a2, a2, PT_AREG2
  354. rfe
  355. /* We cannot handle this exception. */
  356. .extern _kernel_exception
  357. .Linvalid_instruction_store:
  358. .Linvalid_instruction:
  359. /* Restore a4...a8 and SAR, set SP, and jump to default exception. */
  360. l32i a8, a2, PT_AREG8
  361. l32i a7, a2, PT_AREG7
  362. l32i a6, a2, PT_AREG6
  363. l32i a5, a2, PT_AREG5
  364. l32i a4, a2, PT_AREG4
  365. wsr a0, SAR
  366. mov a1, a2
  367. rsr a0, PS
  368. bbsi.l a2, PS_UM_BIT, 1f # jump if user mode
  369. movi a0, _kernel_exception
  370. jx a0
  371. 1: movi a0, _user_exception
  372. jx a0
  373. #endif /* XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION */