assembler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * arch/arm/include/asm/assembler.h
  3. *
  4. * Copyright (C) 1996-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This file contains arm architecture specific defines
  11. * for the different processors.
  12. *
  13. * Do not include any C declarations in this file - it is included by
  14. * assembler source.
  15. */
  16. #ifndef __ASM_ASSEMBLER_H__
  17. #define __ASM_ASSEMBLER_H__
  18. #ifndef __ASSEMBLY__
  19. #error "Only include this from assembly code"
  20. #endif
  21. #include <asm/ptrace.h>
  22. #include <asm/domain.h>
  23. #include <asm/opcodes-virt.h>
  24. #include <asm/asm-offsets.h>
  25. #include <asm/page.h>
  26. #include <asm/thread_info.h>
  27. #define IOMEM(x) (x)
  28. /*
  29. * Endian independent macros for shifting bytes within registers.
  30. */
  31. #ifndef __ARMEB__
  32. #define lspull lsr
  33. #define lspush lsl
  34. #define get_byte_0 lsl #0
  35. #define get_byte_1 lsr #8
  36. #define get_byte_2 lsr #16
  37. #define get_byte_3 lsr #24
  38. #define put_byte_0 lsl #0
  39. #define put_byte_1 lsl #8
  40. #define put_byte_2 lsl #16
  41. #define put_byte_3 lsl #24
  42. #else
  43. #define lspull lsl
  44. #define lspush lsr
  45. #define get_byte_0 lsr #24
  46. #define get_byte_1 lsr #16
  47. #define get_byte_2 lsr #8
  48. #define get_byte_3 lsl #0
  49. #define put_byte_0 lsl #24
  50. #define put_byte_1 lsl #16
  51. #define put_byte_2 lsl #8
  52. #define put_byte_3 lsl #0
  53. #endif
  54. /* Select code for any configuration running in BE8 mode */
  55. #ifdef CONFIG_CPU_ENDIAN_BE8
  56. #define ARM_BE8(code...) code
  57. #else
  58. #define ARM_BE8(code...)
  59. #endif
  60. /*
  61. * Data preload for architectures that support it
  62. */
  63. #if __LINUX_ARM_ARCH__ >= 5
  64. #define PLD(code...) code
  65. #else
  66. #define PLD(code...)
  67. #endif
  68. /*
  69. * This can be used to enable code to cacheline align the destination
  70. * pointer when bulk writing to memory. Experiments on StrongARM and
  71. * XScale didn't show this a worthwhile thing to do when the cache is not
  72. * set to write-allocate (this would need further testing on XScale when WA
  73. * is used).
  74. *
  75. * On Feroceon there is much to gain however, regardless of cache mode.
  76. */
  77. #ifdef CONFIG_CPU_FEROCEON
  78. #define CALGN(code...) code
  79. #else
  80. #define CALGN(code...)
  81. #endif
  82. /*
  83. * Enable and disable interrupts
  84. */
  85. #if __LINUX_ARM_ARCH__ >= 6
  86. .macro disable_irq_notrace
  87. cpsid i
  88. .endm
  89. .macro enable_irq_notrace
  90. cpsie i
  91. .endm
  92. #else
  93. .macro disable_irq_notrace
  94. msr cpsr_c, #PSR_I_BIT | SVC_MODE
  95. .endm
  96. .macro enable_irq_notrace
  97. msr cpsr_c, #SVC_MODE
  98. .endm
  99. #endif
  100. .macro asm_trace_hardirqs_off, save=1
  101. #if defined(CONFIG_TRACE_IRQFLAGS)
  102. .if \save
  103. stmdb sp!, {r0-r3, ip, lr}
  104. .endif
  105. bl trace_hardirqs_off
  106. .if \save
  107. ldmia sp!, {r0-r3, ip, lr}
  108. .endif
  109. #endif
  110. .endm
  111. .macro asm_trace_hardirqs_on, cond=al, save=1
  112. #if defined(CONFIG_TRACE_IRQFLAGS)
  113. /*
  114. * actually the registers should be pushed and pop'd conditionally, but
  115. * after bl the flags are certainly clobbered
  116. */
  117. .if \save
  118. stmdb sp!, {r0-r3, ip, lr}
  119. .endif
  120. bl\cond trace_hardirqs_on
  121. .if \save
  122. ldmia sp!, {r0-r3, ip, lr}
  123. .endif
  124. #endif
  125. .endm
  126. .macro disable_irq, save=1
  127. disable_irq_notrace
  128. asm_trace_hardirqs_off \save
  129. .endm
  130. .macro enable_irq
  131. asm_trace_hardirqs_on
  132. enable_irq_notrace
  133. .endm
  134. /*
  135. * Save the current IRQ state and disable IRQs. Note that this macro
  136. * assumes FIQs are enabled, and that the processor is in SVC mode.
  137. */
  138. .macro save_and_disable_irqs, oldcpsr
  139. #ifdef CONFIG_CPU_V7M
  140. mrs \oldcpsr, primask
  141. #else
  142. mrs \oldcpsr, cpsr
  143. #endif
  144. disable_irq
  145. .endm
  146. .macro save_and_disable_irqs_notrace, oldcpsr
  147. mrs \oldcpsr, cpsr
  148. disable_irq_notrace
  149. .endm
  150. /*
  151. * Restore interrupt state previously stored in a register. We don't
  152. * guarantee that this will preserve the flags.
  153. */
  154. .macro restore_irqs_notrace, oldcpsr
  155. #ifdef CONFIG_CPU_V7M
  156. msr primask, \oldcpsr
  157. #else
  158. msr cpsr_c, \oldcpsr
  159. #endif
  160. .endm
  161. .macro restore_irqs, oldcpsr
  162. tst \oldcpsr, #PSR_I_BIT
  163. asm_trace_hardirqs_on cond=eq
  164. restore_irqs_notrace \oldcpsr
  165. .endm
  166. /*
  167. * Assembly version of "adr rd, BSYM(sym)". This should only be used to
  168. * reference local symbols in the same assembly file which are to be
  169. * resolved by the assembler. Other usage is undefined.
  170. */
  171. .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
  172. .macro badr\c, rd, sym
  173. #ifdef CONFIG_THUMB2_KERNEL
  174. adr\c \rd, \sym + 1
  175. #else
  176. adr\c \rd, \sym
  177. #endif
  178. .endm
  179. .endr
  180. /*
  181. * Get current thread_info.
  182. */
  183. .macro get_thread_info, rd
  184. ARM( mov \rd, sp, lsr #THREAD_SIZE_ORDER + PAGE_SHIFT )
  185. THUMB( mov \rd, sp )
  186. THUMB( lsr \rd, \rd, #THREAD_SIZE_ORDER + PAGE_SHIFT )
  187. mov \rd, \rd, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT
  188. .endm
  189. /*
  190. * Increment/decrement the preempt count.
  191. */
  192. #ifdef CONFIG_PREEMPT_COUNT
  193. .macro inc_preempt_count, ti, tmp
  194. ldr \tmp, [\ti, #TI_PREEMPT] @ get preempt count
  195. add \tmp, \tmp, #1 @ increment it
  196. str \tmp, [\ti, #TI_PREEMPT]
  197. .endm
  198. .macro dec_preempt_count, ti, tmp
  199. ldr \tmp, [\ti, #TI_PREEMPT] @ get preempt count
  200. sub \tmp, \tmp, #1 @ decrement it
  201. str \tmp, [\ti, #TI_PREEMPT]
  202. .endm
  203. .macro dec_preempt_count_ti, ti, tmp
  204. get_thread_info \ti
  205. dec_preempt_count \ti, \tmp
  206. .endm
  207. #else
  208. .macro inc_preempt_count, ti, tmp
  209. .endm
  210. .macro dec_preempt_count, ti, tmp
  211. .endm
  212. .macro dec_preempt_count_ti, ti, tmp
  213. .endm
  214. #endif
  215. #define USER(x...) \
  216. 9999: x; \
  217. .pushsection __ex_table,"a"; \
  218. .align 3; \
  219. .long 9999b,9001f; \
  220. .popsection
  221. #ifdef CONFIG_SMP
  222. #define ALT_SMP(instr...) \
  223. 9998: instr
  224. /*
  225. * Note: if you get assembler errors from ALT_UP() when building with
  226. * CONFIG_THUMB2_KERNEL, you almost certainly need to use
  227. * ALT_SMP( W(instr) ... )
  228. */
  229. #define ALT_UP(instr...) \
  230. .pushsection ".alt.smp.init", "a" ;\
  231. .long 9998b ;\
  232. 9997: instr ;\
  233. .if . - 9997b == 2 ;\
  234. nop ;\
  235. .endif ;\
  236. .if . - 9997b != 4 ;\
  237. .error "ALT_UP() content must assemble to exactly 4 bytes";\
  238. .endif ;\
  239. .popsection
  240. #define ALT_UP_B(label) \
  241. .equ up_b_offset, label - 9998b ;\
  242. .pushsection ".alt.smp.init", "a" ;\
  243. .long 9998b ;\
  244. W(b) . + up_b_offset ;\
  245. .popsection
  246. #else
  247. #define ALT_SMP(instr...)
  248. #define ALT_UP(instr...) instr
  249. #define ALT_UP_B(label) b label
  250. #endif
  251. /*
  252. * Instruction barrier
  253. */
  254. .macro instr_sync
  255. #if __LINUX_ARM_ARCH__ >= 7
  256. isb
  257. #elif __LINUX_ARM_ARCH__ == 6
  258. mcr p15, 0, r0, c7, c5, 4
  259. #endif
  260. .endm
  261. /*
  262. * SMP data memory barrier
  263. */
  264. .macro smp_dmb mode
  265. #ifdef CONFIG_SMP
  266. #if __LINUX_ARM_ARCH__ >= 7
  267. .ifeqs "\mode","arm"
  268. ALT_SMP(dmb ish)
  269. .else
  270. ALT_SMP(W(dmb) ish)
  271. .endif
  272. #elif __LINUX_ARM_ARCH__ == 6
  273. ALT_SMP(mcr p15, 0, r0, c7, c10, 5) @ dmb
  274. #else
  275. #error Incompatible SMP platform
  276. #endif
  277. .ifeqs "\mode","arm"
  278. ALT_UP(nop)
  279. .else
  280. ALT_UP(W(nop))
  281. .endif
  282. #endif
  283. .endm
  284. #if defined(CONFIG_CPU_V7M)
  285. /*
  286. * setmode is used to assert to be in svc mode during boot. For v7-M
  287. * this is done in __v7m_setup, so setmode can be empty here.
  288. */
  289. .macro setmode, mode, reg
  290. .endm
  291. #elif defined(CONFIG_THUMB2_KERNEL)
  292. .macro setmode, mode, reg
  293. mov \reg, #\mode
  294. msr cpsr_c, \reg
  295. .endm
  296. #else
  297. .macro setmode, mode, reg
  298. msr cpsr_c, #\mode
  299. .endm
  300. #endif
  301. /*
  302. * Helper macro to enter SVC mode cleanly and mask interrupts. reg is
  303. * a scratch register for the macro to overwrite.
  304. *
  305. * This macro is intended for forcing the CPU into SVC mode at boot time.
  306. * you cannot return to the original mode.
  307. */
  308. .macro safe_svcmode_maskall reg:req
  309. #if __LINUX_ARM_ARCH__ >= 6 && !defined(CONFIG_CPU_V7M)
  310. mrs \reg , cpsr
  311. eor \reg, \reg, #HYP_MODE
  312. tst \reg, #MODE_MASK
  313. bic \reg , \reg , #MODE_MASK
  314. orr \reg , \reg , #PSR_I_BIT | PSR_F_BIT | SVC_MODE
  315. THUMB( orr \reg , \reg , #PSR_T_BIT )
  316. bne 1f
  317. orr \reg, \reg, #PSR_A_BIT
  318. badr lr, 2f
  319. msr spsr_cxsf, \reg
  320. __MSR_ELR_HYP(14)
  321. __ERET
  322. 1: msr cpsr_c, \reg
  323. 2:
  324. #else
  325. /*
  326. * workaround for possibly broken pre-v6 hardware
  327. * (akita, Sharp Zaurus C-1000, PXA270-based)
  328. */
  329. setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, \reg
  330. #endif
  331. .endm
  332. /*
  333. * STRT/LDRT access macros with ARM and Thumb-2 variants
  334. */
  335. #ifdef CONFIG_THUMB2_KERNEL
  336. .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
  337. 9999:
  338. .if \inc == 1
  339. \instr\cond\()b\()\t\().w \reg, [\ptr, #\off]
  340. .elseif \inc == 4
  341. \instr\cond\()\t\().w \reg, [\ptr, #\off]
  342. .else
  343. .error "Unsupported inc macro argument"
  344. .endif
  345. .pushsection __ex_table,"a"
  346. .align 3
  347. .long 9999b, \abort
  348. .popsection
  349. .endm
  350. .macro usracc, instr, reg, ptr, inc, cond, rept, abort
  351. @ explicit IT instruction needed because of the label
  352. @ introduced by the USER macro
  353. .ifnc \cond,al
  354. .if \rept == 1
  355. itt \cond
  356. .elseif \rept == 2
  357. ittt \cond
  358. .else
  359. .error "Unsupported rept macro argument"
  360. .endif
  361. .endif
  362. @ Slightly optimised to avoid incrementing the pointer twice
  363. usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
  364. .if \rept == 2
  365. usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
  366. .endif
  367. add\cond \ptr, #\rept * \inc
  368. .endm
  369. #else /* !CONFIG_THUMB2_KERNEL */
  370. .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
  371. .rept \rept
  372. 9999:
  373. .if \inc == 1
  374. \instr\cond\()b\()\t \reg, [\ptr], #\inc
  375. .elseif \inc == 4
  376. \instr\cond\()\t \reg, [\ptr], #\inc
  377. .else
  378. .error "Unsupported inc macro argument"
  379. .endif
  380. .pushsection __ex_table,"a"
  381. .align 3
  382. .long 9999b, \abort
  383. .popsection
  384. .endr
  385. .endm
  386. #endif /* CONFIG_THUMB2_KERNEL */
  387. .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  388. usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
  389. .endm
  390. .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  391. usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
  392. .endm
  393. /* Utility macro for declaring string literals */
  394. .macro string name:req, string
  395. .type \name , #object
  396. \name:
  397. .asciz "\string"
  398. .size \name , . - \name
  399. .endm
  400. .macro check_uaccess, addr:req, size:req, limit:req, tmp:req, bad:req
  401. #ifndef CONFIG_CPU_USE_DOMAINS
  402. adds \tmp, \addr, #\size - 1
  403. sbcccs \tmp, \tmp, \limit
  404. bcs \bad
  405. #endif
  406. .endm
  407. .macro uaccess_disable, tmp, isb=1
  408. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  409. /*
  410. * Whenever we re-enter userspace, the domains should always be
  411. * set appropriately.
  412. */
  413. mov \tmp, #DACR_UACCESS_DISABLE
  414. mcr p15, 0, \tmp, c3, c0, 0 @ Set domain register
  415. .if \isb
  416. instr_sync
  417. .endif
  418. #endif
  419. .endm
  420. .macro uaccess_enable, tmp, isb=1
  421. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  422. /*
  423. * Whenever we re-enter userspace, the domains should always be
  424. * set appropriately.
  425. */
  426. mov \tmp, #DACR_UACCESS_ENABLE
  427. mcr p15, 0, \tmp, c3, c0, 0
  428. .if \isb
  429. instr_sync
  430. .endif
  431. #endif
  432. .endm
  433. .macro uaccess_save, tmp
  434. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  435. mrc p15, 0, \tmp, c3, c0, 0
  436. str \tmp, [sp, #S_FRAME_SIZE]
  437. #endif
  438. .endm
  439. .macro uaccess_restore
  440. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  441. ldr r0, [sp, #S_FRAME_SIZE]
  442. mcr p15, 0, r0, c3, c0, 0
  443. #endif
  444. .endm
  445. .macro uaccess_save_and_disable, tmp
  446. uaccess_save \tmp
  447. uaccess_disable \tmp
  448. .endm
  449. .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
  450. .macro ret\c, reg
  451. #if __LINUX_ARM_ARCH__ < 6
  452. mov\c pc, \reg
  453. #else
  454. .ifeqs "\reg", "lr"
  455. bx\c \reg
  456. .else
  457. mov\c pc, \reg
  458. .endif
  459. #endif
  460. .endm
  461. .endr
  462. .macro ret.w, reg
  463. ret \reg
  464. #ifdef CONFIG_THUMB2_KERNEL
  465. nop
  466. #endif
  467. .endm
  468. #endif /* __ASM_ASSEMBLER_H__ */