align.S 11 KB

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