align.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * arch/xtensa/kernel/align.S
  3. *
  4. * Handle unalignment and load/store exceptions.
  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/asmmacro.h>
  22. #include <asm/processor.h>
  23. #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
  24. #define LOAD_EXCEPTION_HANDLER
  25. #endif
  26. #if XCHAL_UNALIGNED_STORE_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
  27. #define STORE_EXCEPTION_HANDLER
  28. #endif
  29. #if defined LOAD_EXCEPTION_HANDLER || defined STORE_EXCEPTION_HANDLER
  30. #define ANY_EXCEPTION_HANDLER
  31. #endif
  32. #if XCHAL_HAVE_WINDOWED && defined CONFIG_MMU
  33. #define UNALIGNED_USER_EXCEPTION
  34. #endif
  35. /* Big and little endian 16-bit values are located in
  36. * different halves of a register. HWORD_START helps to
  37. * abstract the notion of extracting a 16-bit value from a
  38. * register.
  39. * We also have to define new shifting instructions because
  40. * lsb and msb are on 'opposite' ends in a register for
  41. * different endian machines.
  42. *
  43. * Assume a memory region in ascending address:
  44. * 0 1 2 3|4 5 6 7
  45. *
  46. * When loading one word into a register, the content of that register is:
  47. * LE 3 2 1 0, 7 6 5 4
  48. * BE 0 1 2 3, 4 5 6 7
  49. *
  50. * Masking the bits of the higher/lower address means:
  51. * LE X X 0 0, 0 0 X X
  52. * BE 0 0 X X, X X 0 0
  53. *
  54. * Shifting to higher/lower addresses, means:
  55. * LE shift left / shift right
  56. * BE shift right / shift left
  57. *
  58. * Extracting 16 bits from a 32 bit reg. value to higher/lower address means:
  59. * LE mask 0 0 X X / shift left
  60. * BE shift left / mask 0 0 X X
  61. */
  62. #if XCHAL_HAVE_BE
  63. #define HWORD_START 16
  64. #define INSN_OP0 28
  65. #define INSN_T 24
  66. #define INSN_OP1 16
  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 __ssa8r r; ssa8b \r; .endm
  78. .macro __sh r, s; sll \r, \s; .endm
  79. .macro __sl r, s; srl \r, \s; .endm
  80. .macro __exth r, s; slli \r, \s, 16; .endm
  81. .macro __extl r, s; extui \r, \s, 0, 16; .endm
  82. #endif
  83. /*
  84. * xxxx xxxx = imm8 field
  85. * yyyy = imm4 field
  86. * ssss = s field
  87. * tttt = t field
  88. *
  89. * 16 0
  90. * -------------------
  91. * L32I.N yyyy ssss tttt 1000
  92. * S32I.N yyyy ssss tttt 1001
  93. *
  94. * 23 0
  95. * -----------------------------
  96. * L8UI xxxx xxxx 0000 ssss tttt 0010
  97. * L16UI xxxx xxxx 0001 ssss tttt 0010
  98. * L32I xxxx xxxx 0010 ssss tttt 0010
  99. * XXX 0011 ssss tttt 0010
  100. * XXX 0100 ssss tttt 0010
  101. * S16I xxxx xxxx 0101 ssss tttt 0010
  102. * S32I xxxx xxxx 0110 ssss tttt 0010
  103. * XXX 0111 ssss tttt 0010
  104. * XXX 1000 ssss tttt 0010
  105. * L16SI xxxx xxxx 1001 ssss tttt 0010
  106. * XXX 1010 0010
  107. * **L32AI xxxx xxxx 1011 ssss tttt 0010 unsupported
  108. * XXX 1100 0010
  109. * XXX 1101 0010
  110. * XXX 1110 0010
  111. * **S32RI xxxx xxxx 1111 ssss tttt 0010 unsupported
  112. * -----------------------------
  113. * ^ ^ ^
  114. * sub-opcode (NIBBLE_R) -+ | |
  115. * t field (NIBBLE_T) -----------+ |
  116. * major opcode (NIBBLE_OP0) --------------+
  117. */
  118. #define OP0_L32I_N 0x8 /* load immediate narrow */
  119. #define OP0_S32I_N 0x9 /* store immediate narrow */
  120. #define OP0_LSAI 0x2 /* load/store */
  121. #define OP1_SI_MASK 0x4 /* OP1 bit set for stores */
  122. #define OP1_SI_BIT 2 /* OP1 bit number for stores */
  123. #define OP1_L8UI 0x0
  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: a3
  138. * depc: a2, original value saved on stack (PT_DEPC)
  139. * excsave_1: dispatch table
  140. *
  141. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  142. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  143. */
  144. .literal_position
  145. #ifdef CONFIG_XTENSA_LOAD_STORE
  146. ENTRY(fast_load_store)
  147. call0 .Lsave_and_load_instruction
  148. /* Analyze the instruction (load or store?). */
  149. extui a0, a4, INSN_OP0, 4 # get insn.op0 nibble
  150. #if XCHAL_HAVE_DENSITY
  151. _beqi a0, OP0_L32I_N, 1f # L32I.N, jump
  152. #endif
  153. bnei a0, OP0_LSAI, .Linvalid_instruction
  154. /* 'store indicator bit' set, jump */
  155. bbsi.l a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction
  156. 1:
  157. movi a3, ~3
  158. and a3, a3, a8 # align memory address
  159. __ssa8 a8
  160. #ifdef CONFIG_MMU
  161. /* l32e can't be used here even when it's available. */
  162. /* TODO access_ok(a3) could be used here */
  163. j .Linvalid_instruction
  164. #endif
  165. l32i a5, a3, 0
  166. l32i a6, a3, 4
  167. __src_b a3, a5, a6 # a3 has the data word
  168. #if XCHAL_HAVE_DENSITY
  169. addi a7, a7, 2 # increment PC (assume 16-bit insn)
  170. _beqi a0, OP0_L32I_N, .Lload_w# l32i.n: jump
  171. addi a7, a7, 1
  172. #else
  173. addi a7, a7, 3
  174. #endif
  175. extui a5, a4, INSN_OP1, 4
  176. _beqi a5, OP1_L32I, .Lload_w
  177. bnei a5, OP1_L8UI, .Lload16
  178. extui a3, a3, 0, 8
  179. j .Lload_w
  180. ENDPROC(fast_load_store)
  181. #endif
  182. /*
  183. * Entry condition:
  184. *
  185. * a0: trashed, original value saved on stack (PT_AREG0)
  186. * a1: a1
  187. * a2: new stack pointer, original in DEPC
  188. * a3: a3
  189. * depc: a2, original value saved on stack (PT_DEPC)
  190. * excsave_1: dispatch table
  191. *
  192. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  193. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  194. */
  195. #ifdef ANY_EXCEPTION_HANDLER
  196. ENTRY(fast_unaligned)
  197. call0 .Lsave_and_load_instruction
  198. /* Analyze the instruction (load or store?). */
  199. extui a5, a4, INSN_OP0, 4 # get insn.op0 nibble
  200. #if XCHAL_HAVE_DENSITY
  201. _beqi a5, OP0_L32I_N, .Lload # L32I.N, jump
  202. addi a6, a5, -OP0_S32I_N
  203. _beqz a6, .Lstore # S32I.N, do a store
  204. #endif
  205. /* 'store indicator bit' not set, jump */
  206. _bbci.l a4, OP1_SI_BIT + INSN_OP1, .Lload
  207. #ifdef STORE_EXCEPTION_HANDLER
  208. /* Store: Jump to table entry to get the value in the source register.*/
  209. .Lstore:movi a5, .Lstore_table # table
  210. extui a6, a4, INSN_T, 4 # get source register
  211. addx8 a5, a6, a5
  212. jx a5 # jump into table
  213. #endif
  214. #ifdef LOAD_EXCEPTION_HANDLER
  215. /* Load: Load memory address. */
  216. .Lload: movi a3, ~3
  217. and a3, a3, a8 # align memory address
  218. __ssa8 a8
  219. #ifdef UNALIGNED_USER_EXCEPTION
  220. addi a3, a3, 8
  221. l32e a5, a3, -8
  222. l32e a6, a3, -4
  223. #else
  224. l32i a5, a3, 0
  225. l32i a6, a3, 4
  226. #endif
  227. __src_b a3, a5, a6 # a3 has the data word
  228. #if XCHAL_HAVE_DENSITY
  229. addi a7, a7, 2 # increment PC (assume 16-bit insn)
  230. extui a5, a4, INSN_OP0, 4
  231. _beqi a5, OP0_L32I_N, .Lload_w# l32i.n: jump
  232. addi a7, a7, 1
  233. #else
  234. addi a7, a7, 3
  235. #endif
  236. extui a5, a4, INSN_OP1, 4
  237. _beqi a5, OP1_L32I, .Lload_w # l32i: jump
  238. #endif
  239. #ifdef LOAD_EXCEPTION_HANDLER
  240. .Lload16:
  241. extui a3, a3, 0, 16 # extract lower 16 bits
  242. _beqi a5, OP1_L16UI, .Lload_w
  243. addi a5, a5, -OP1_L16SI
  244. _bnez a5, .Linvalid_instruction
  245. /* sign extend value */
  246. #if XCHAL_HAVE_SEXT
  247. sext a3, a3, 15
  248. #else
  249. slli a3, a3, 16
  250. srai a3, a3, 16
  251. #endif
  252. /* Set target register. */
  253. .Lload_w:
  254. extui a4, a4, INSN_T, 4 # extract target register
  255. movi a5, .Lload_table
  256. addx8 a4, a4, a5
  257. jx a4 # jump to entry for target register
  258. .align 8
  259. .Lload_table:
  260. s32i a3, a2, PT_AREG0; _j .Lexit; .align 8
  261. mov a1, a3; _j .Lexit; .align 8 # fishy??
  262. s32i a3, a2, PT_AREG2; _j .Lexit; .align 8
  263. s32i a3, a2, PT_AREG3; _j .Lexit; .align 8
  264. s32i a3, a2, PT_AREG4; _j .Lexit; .align 8
  265. s32i a3, a2, PT_AREG5; _j .Lexit; .align 8
  266. s32i a3, a2, PT_AREG6; _j .Lexit; .align 8
  267. s32i a3, a2, PT_AREG7; _j .Lexit; .align 8
  268. s32i a3, a2, PT_AREG8; _j .Lexit; .align 8
  269. mov a9, a3 ; _j .Lexit; .align 8
  270. mov a10, a3 ; _j .Lexit; .align 8
  271. mov a11, a3 ; _j .Lexit; .align 8
  272. mov a12, a3 ; _j .Lexit; .align 8
  273. mov a13, a3 ; _j .Lexit; .align 8
  274. mov a14, a3 ; _j .Lexit; .align 8
  275. mov a15, a3 ; _j .Lexit; .align 8
  276. #endif
  277. #ifdef STORE_EXCEPTION_HANDLER
  278. .Lstore_table:
  279. l32i a3, a2, PT_AREG0; _j .Lstore_w; .align 8
  280. mov a3, a1; _j .Lstore_w; .align 8 # fishy??
  281. l32i a3, a2, PT_AREG2; _j .Lstore_w; .align 8
  282. l32i a3, a2, PT_AREG3; _j .Lstore_w; .align 8
  283. l32i a3, a2, PT_AREG4; _j .Lstore_w; .align 8
  284. l32i a3, a2, PT_AREG5; _j .Lstore_w; .align 8
  285. l32i a3, a2, PT_AREG6; _j .Lstore_w; .align 8
  286. l32i a3, a2, PT_AREG7; _j .Lstore_w; .align 8
  287. l32i a3, a2, PT_AREG8; _j .Lstore_w; .align 8
  288. mov a3, a9 ; _j .Lstore_w; .align 8
  289. mov a3, a10 ; _j .Lstore_w; .align 8
  290. mov a3, a11 ; _j .Lstore_w; .align 8
  291. mov a3, a12 ; _j .Lstore_w; .align 8
  292. mov a3, a13 ; _j .Lstore_w; .align 8
  293. mov a3, a14 ; _j .Lstore_w; .align 8
  294. mov a3, a15 ; _j .Lstore_w; .align 8
  295. #endif
  296. /* We cannot handle this exception. */
  297. .extern _kernel_exception
  298. .Linvalid_instruction:
  299. movi a4, 0
  300. rsr a3, excsave1
  301. s32i a4, a3, EXC_TABLE_FIXUP
  302. /* Restore a4...a8 and SAR, set SP, and jump to default exception. */
  303. l32i a0, a2, PT_SAR
  304. l32i a8, a2, PT_AREG8
  305. l32i a7, a2, PT_AREG7
  306. l32i a6, a2, PT_AREG6
  307. l32i a5, a2, PT_AREG5
  308. l32i a4, a2, PT_AREG4
  309. wsr a0, sar
  310. mov a1, a2
  311. rsr a0, ps
  312. bbsi.l a0, PS_UM_BIT, 2f # jump if user mode
  313. movi a0, _kernel_exception
  314. jx a0
  315. 2: movi a0, _user_exception
  316. jx a0
  317. #ifdef STORE_EXCEPTION_HANDLER
  318. # a7: instruction pointer, a4: instruction, a3: value
  319. .Lstore_w:
  320. movi a6, 0 # mask: ffffffff:00000000
  321. #if XCHAL_HAVE_DENSITY
  322. addi a7, a7, 2 # incr. PC,assume 16-bit instruction
  323. extui a5, a4, INSN_OP0, 4 # extract OP0
  324. addi a5, a5, -OP0_S32I_N
  325. _beqz a5, 1f # s32i.n: jump
  326. addi a7, a7, 1 # increment PC, 32-bit instruction
  327. #else
  328. addi a7, a7, 3 # increment PC, 32-bit instruction
  329. #endif
  330. extui a5, a4, INSN_OP1, 4 # extract OP1
  331. _beqi a5, OP1_S32I, 1f # jump if 32 bit store
  332. _bnei a5, OP1_S16I, .Linvalid_instruction
  333. movi a5, -1
  334. __extl a3, a3 # get 16-bit value
  335. __exth a6, a5 # get 16-bit mask ffffffff:ffff0000
  336. /* Get memory address */
  337. 1:
  338. movi a4, ~3
  339. and a4, a4, a8 # align memory address
  340. /* Insert value into memory */
  341. movi a5, -1 # mask: ffffffff:XXXX0000
  342. #ifdef UNALIGNED_USER_EXCEPTION
  343. addi a4, a4, 8
  344. #endif
  345. __ssa8r a8
  346. __src_b a8, a5, a6 # lo-mask F..F0..0 (BE) 0..0F..F (LE)
  347. __src_b a6, a6, a5 # hi-mask 0..0F..F (BE) F..F0..0 (LE)
  348. #ifdef UNALIGNED_USER_EXCEPTION
  349. l32e a5, a4, -8
  350. #else
  351. l32i a5, a4, 0 # load lower address word
  352. #endif
  353. and a5, a5, a8 # mask
  354. __sh a8, a3 # shift value
  355. or a5, a5, a8 # or with original value
  356. #ifdef UNALIGNED_USER_EXCEPTION
  357. s32e a5, a4, -8
  358. l32e a8, a4, -4
  359. #else
  360. s32i a5, a4, 0 # store
  361. l32i a8, a4, 4 # same for upper address word
  362. #endif
  363. __sl a5, a3
  364. and a6, a8, a6
  365. or a6, a6, a5
  366. #ifdef UNALIGNED_USER_EXCEPTION
  367. s32e a6, a4, -4
  368. #else
  369. s32i a6, a4, 4
  370. #endif
  371. #endif
  372. .Lexit:
  373. #if XCHAL_HAVE_LOOPS
  374. rsr a4, lend # check if we reached LEND
  375. bne a7, a4, 1f
  376. rsr a4, lcount # and LCOUNT != 0
  377. beqz a4, 1f
  378. addi a4, a4, -1 # decrement LCOUNT and set
  379. rsr a7, lbeg # set PC to LBEGIN
  380. wsr a4, lcount
  381. #endif
  382. 1: wsr a7, epc1 # skip emulated instruction
  383. /* Update icount if we're single-stepping in userspace. */
  384. rsr a4, icountlevel
  385. beqz a4, 1f
  386. bgeui a4, LOCKLEVEL + 1, 1f
  387. rsr a4, icount
  388. addi a4, a4, 1
  389. wsr a4, icount
  390. 1:
  391. movi a4, 0
  392. rsr a3, excsave1
  393. s32i a4, a3, EXC_TABLE_FIXUP
  394. /* Restore working register */
  395. l32i a0, a2, PT_SAR
  396. l32i a8, a2, PT_AREG8
  397. l32i a7, a2, PT_AREG7
  398. l32i a6, a2, PT_AREG6
  399. l32i a5, a2, PT_AREG5
  400. l32i a4, a2, PT_AREG4
  401. l32i a3, a2, PT_AREG3
  402. /* restore SAR and return */
  403. wsr a0, sar
  404. l32i a0, a2, PT_AREG0
  405. l32i a2, a2, PT_AREG2
  406. rfe
  407. .align 4
  408. .Lsave_and_load_instruction:
  409. /* Save some working register */
  410. s32i a3, a2, PT_AREG3
  411. s32i a4, a2, PT_AREG4
  412. s32i a5, a2, PT_AREG5
  413. s32i a6, a2, PT_AREG6
  414. s32i a7, a2, PT_AREG7
  415. s32i a8, a2, PT_AREG8
  416. rsr a4, depc
  417. s32i a4, a2, PT_AREG2
  418. rsr a5, sar
  419. s32i a5, a2, PT_SAR
  420. rsr a3, excsave1
  421. movi a4, fast_unaligned_fixup
  422. s32i a4, a3, EXC_TABLE_FIXUP
  423. rsr a8, excvaddr # load unaligned memory address
  424. /* Now, identify one of the following load/store instructions.
  425. *
  426. * The only possible danger of a double exception on the
  427. * following l32i instructions is kernel code in vmalloc
  428. * memory. The processor was just executing at the EPC_1
  429. * address, and indeed, already fetched the instruction. That
  430. * guarantees a TLB mapping, which hasn't been replaced by
  431. * this unaligned exception handler that uses only static TLB
  432. * mappings. However, high-level interrupt handlers might
  433. * modify TLB entries, so for the generic case, we register a
  434. * TABLE_FIXUP handler here, too.
  435. */
  436. /* a3...a6 saved on stack, a2 = SP */
  437. /* Extract the instruction that caused the unaligned access. */
  438. rsr a7, epc1 # load exception address
  439. movi a3, ~3
  440. and a3, a3, a7 # mask lower bits
  441. l32i a4, a3, 0 # load 2 words
  442. l32i a5, a3, 4
  443. __ssa8 a7
  444. __src_b a4, a4, a5 # a4 has the instruction
  445. ret
  446. ENDPROC(fast_unaligned)
  447. ENTRY(fast_unaligned_fixup)
  448. l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
  449. wsr a3, excsave1
  450. l32i a8, a2, PT_AREG8
  451. l32i a7, a2, PT_AREG7
  452. l32i a6, a2, PT_AREG6
  453. l32i a5, a2, PT_AREG5
  454. l32i a4, a2, PT_SAR
  455. l32i a0, a2, PT_AREG2
  456. wsr a4, sar
  457. wsr a0, depc # restore depc and a0
  458. l32i a4, a2, PT_AREG4
  459. rsr a0, exccause
  460. s32i a0, a2, PT_DEPC # mark as a regular exception
  461. rsr a0, ps
  462. bbsi.l a0, PS_UM_BIT, 1f # jump if user mode
  463. rsr a0, exccause
  464. addx4 a0, a0, a3 # find entry in table
  465. l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler
  466. l32i a3, a2, PT_AREG3
  467. jx a0
  468. 1:
  469. rsr a0, exccause
  470. addx4 a0, a0, a3 # find entry in table
  471. l32i a0, a0, EXC_TABLE_FAST_USER # load handler
  472. l32i a3, a2, PT_AREG3
  473. jx a0
  474. ENDPROC(fast_unaligned_fixup)
  475. #endif