assembler.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #define IOMEM(x) (x)
  25. /*
  26. * Endian independent macros for shifting bytes within registers.
  27. */
  28. #ifndef __ARMEB__
  29. #define pull lsr
  30. #define push lsl
  31. #define get_byte_0 lsl #0
  32. #define get_byte_1 lsr #8
  33. #define get_byte_2 lsr #16
  34. #define get_byte_3 lsr #24
  35. #define put_byte_0 lsl #0
  36. #define put_byte_1 lsl #8
  37. #define put_byte_2 lsl #16
  38. #define put_byte_3 lsl #24
  39. #else
  40. #define pull lsl
  41. #define push lsr
  42. #define get_byte_0 lsr #24
  43. #define get_byte_1 lsr #16
  44. #define get_byte_2 lsr #8
  45. #define get_byte_3 lsl #0
  46. #define put_byte_0 lsl #24
  47. #define put_byte_1 lsl #16
  48. #define put_byte_2 lsl #8
  49. #define put_byte_3 lsl #0
  50. #endif
  51. /*
  52. * Data preload for architectures that support it
  53. */
  54. #if __LINUX_ARM_ARCH__ >= 5
  55. #define PLD(code...) code
  56. #else
  57. #define PLD(code...)
  58. #endif
  59. /*
  60. * This can be used to enable code to cacheline align the destination
  61. * pointer when bulk writing to memory. Experiments on StrongARM and
  62. * XScale didn't show this a worthwhile thing to do when the cache is not
  63. * set to write-allocate (this would need further testing on XScale when WA
  64. * is used).
  65. *
  66. * On Feroceon there is much to gain however, regardless of cache mode.
  67. */
  68. #ifdef CONFIG_CPU_FEROCEON
  69. #define CALGN(code...) code
  70. #else
  71. #define CALGN(code...)
  72. #endif
  73. /*
  74. * Enable and disable interrupts
  75. */
  76. #if __LINUX_ARM_ARCH__ >= 6
  77. .macro disable_irq_notrace
  78. cpsid i
  79. .endm
  80. .macro enable_irq_notrace
  81. cpsie i
  82. .endm
  83. #else
  84. .macro disable_irq_notrace
  85. msr cpsr_c, #PSR_I_BIT | SVC_MODE
  86. .endm
  87. .macro enable_irq_notrace
  88. msr cpsr_c, #SVC_MODE
  89. .endm
  90. #endif
  91. .macro asm_trace_hardirqs_off
  92. #if defined(CONFIG_TRACE_IRQFLAGS)
  93. stmdb sp!, {r0-r3, ip, lr}
  94. bl trace_hardirqs_off
  95. ldmia sp!, {r0-r3, ip, lr}
  96. #endif
  97. .endm
  98. .macro asm_trace_hardirqs_on_cond, cond
  99. #if defined(CONFIG_TRACE_IRQFLAGS)
  100. /*
  101. * actually the registers should be pushed and pop'd conditionally, but
  102. * after bl the flags are certainly clobbered
  103. */
  104. stmdb sp!, {r0-r3, ip, lr}
  105. bl\cond trace_hardirqs_on
  106. ldmia sp!, {r0-r3, ip, lr}
  107. #endif
  108. .endm
  109. .macro asm_trace_hardirqs_on
  110. asm_trace_hardirqs_on_cond al
  111. .endm
  112. .macro disable_irq
  113. disable_irq_notrace
  114. asm_trace_hardirqs_off
  115. .endm
  116. .macro enable_irq
  117. asm_trace_hardirqs_on
  118. enable_irq_notrace
  119. .endm
  120. /*
  121. * Save the current IRQ state and disable IRQs. Note that this macro
  122. * assumes FIQs are enabled, and that the processor is in SVC mode.
  123. */
  124. .macro save_and_disable_irqs, oldcpsr
  125. mrs \oldcpsr, cpsr
  126. disable_irq
  127. .endm
  128. .macro save_and_disable_irqs_notrace, oldcpsr
  129. mrs \oldcpsr, cpsr
  130. disable_irq_notrace
  131. .endm
  132. /*
  133. * Restore interrupt state previously stored in a register. We don't
  134. * guarantee that this will preserve the flags.
  135. */
  136. .macro restore_irqs_notrace, oldcpsr
  137. msr cpsr_c, \oldcpsr
  138. .endm
  139. .macro restore_irqs, oldcpsr
  140. tst \oldcpsr, #PSR_I_BIT
  141. asm_trace_hardirqs_on_cond eq
  142. restore_irqs_notrace \oldcpsr
  143. .endm
  144. #define USER(x...) \
  145. 9999: x; \
  146. .pushsection __ex_table,"a"; \
  147. .align 3; \
  148. .long 9999b,9001f; \
  149. .popsection
  150. #ifdef CONFIG_SMP
  151. #define ALT_SMP(instr...) \
  152. 9998: instr
  153. /*
  154. * Note: if you get assembler errors from ALT_UP() when building with
  155. * CONFIG_THUMB2_KERNEL, you almost certainly need to use
  156. * ALT_SMP( W(instr) ... )
  157. */
  158. #define ALT_UP(instr...) \
  159. .pushsection ".alt.smp.init", "a" ;\
  160. .long 9998b ;\
  161. 9997: instr ;\
  162. .if . - 9997b != 4 ;\
  163. .error "ALT_UP() content must assemble to exactly 4 bytes";\
  164. .endif ;\
  165. .popsection
  166. #define ALT_UP_B(label) \
  167. .equ up_b_offset, label - 9998b ;\
  168. .pushsection ".alt.smp.init", "a" ;\
  169. .long 9998b ;\
  170. W(b) . + up_b_offset ;\
  171. .popsection
  172. #else
  173. #define ALT_SMP(instr...)
  174. #define ALT_UP(instr...) instr
  175. #define ALT_UP_B(label) b label
  176. #endif
  177. /*
  178. * Instruction barrier
  179. */
  180. .macro instr_sync
  181. #if __LINUX_ARM_ARCH__ >= 7
  182. isb
  183. #elif __LINUX_ARM_ARCH__ == 6
  184. mcr p15, 0, r0, c7, c5, 4
  185. #endif
  186. .endm
  187. /*
  188. * SMP data memory barrier
  189. */
  190. .macro smp_dmb mode
  191. #ifdef CONFIG_SMP
  192. #if __LINUX_ARM_ARCH__ >= 7
  193. .ifeqs "\mode","arm"
  194. ALT_SMP(dmb)
  195. .else
  196. ALT_SMP(W(dmb))
  197. .endif
  198. #elif __LINUX_ARM_ARCH__ == 6
  199. ALT_SMP(mcr p15, 0, r0, c7, c10, 5) @ dmb
  200. #else
  201. #error Incompatible SMP platform
  202. #endif
  203. .ifeqs "\mode","arm"
  204. ALT_UP(nop)
  205. .else
  206. ALT_UP(W(nop))
  207. .endif
  208. #endif
  209. .endm
  210. #ifdef CONFIG_THUMB2_KERNEL
  211. .macro setmode, mode, reg
  212. mov \reg, #\mode
  213. msr cpsr_c, \reg
  214. .endm
  215. #else
  216. .macro setmode, mode, reg
  217. msr cpsr_c, #\mode
  218. .endm
  219. #endif
  220. /*
  221. * Helper macro to enter SVC mode cleanly and mask interrupts. reg is
  222. * a scratch register for the macro to overwrite.
  223. *
  224. * This macro is intended for forcing the CPU into SVC mode at boot time.
  225. * you cannot return to the original mode.
  226. *
  227. * Beware, it also clobers LR.
  228. */
  229. .macro safe_svcmode_maskall reg:req
  230. mrs \reg , cpsr
  231. mov lr , \reg
  232. and lr , lr , #MODE_MASK
  233. cmp lr , #HYP_MODE
  234. orr \reg , \reg , #PSR_A_BIT | PSR_I_BIT | PSR_F_BIT
  235. bic \reg , \reg , #MODE_MASK
  236. orr \reg , \reg , #SVC_MODE
  237. THUMB( orr \reg , \reg , #PSR_T_BIT )
  238. msr spsr_cxsf, \reg
  239. adr lr, BSYM(2f)
  240. bne 1f
  241. __MSR_ELR_HYP(14)
  242. __ERET
  243. 1: movs pc, lr
  244. 2:
  245. .endm
  246. /*
  247. * STRT/LDRT access macros with ARM and Thumb-2 variants
  248. */
  249. #ifdef CONFIG_THUMB2_KERNEL
  250. .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
  251. 9999:
  252. .if \inc == 1
  253. \instr\cond\()b\()\t\().w \reg, [\ptr, #\off]
  254. .elseif \inc == 4
  255. \instr\cond\()\t\().w \reg, [\ptr, #\off]
  256. .else
  257. .error "Unsupported inc macro argument"
  258. .endif
  259. .pushsection __ex_table,"a"
  260. .align 3
  261. .long 9999b, \abort
  262. .popsection
  263. .endm
  264. .macro usracc, instr, reg, ptr, inc, cond, rept, abort
  265. @ explicit IT instruction needed because of the label
  266. @ introduced by the USER macro
  267. .ifnc \cond,al
  268. .if \rept == 1
  269. itt \cond
  270. .elseif \rept == 2
  271. ittt \cond
  272. .else
  273. .error "Unsupported rept macro argument"
  274. .endif
  275. .endif
  276. @ Slightly optimised to avoid incrementing the pointer twice
  277. usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
  278. .if \rept == 2
  279. usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
  280. .endif
  281. add\cond \ptr, #\rept * \inc
  282. .endm
  283. #else /* !CONFIG_THUMB2_KERNEL */
  284. .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
  285. .rept \rept
  286. 9999:
  287. .if \inc == 1
  288. \instr\cond\()b\()\t \reg, [\ptr], #\inc
  289. .elseif \inc == 4
  290. \instr\cond\()\t \reg, [\ptr], #\inc
  291. .else
  292. .error "Unsupported inc macro argument"
  293. .endif
  294. .pushsection __ex_table,"a"
  295. .align 3
  296. .long 9999b, \abort
  297. .popsection
  298. .endr
  299. .endm
  300. #endif /* CONFIG_THUMB2_KERNEL */
  301. .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  302. usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
  303. .endm
  304. .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  305. usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
  306. .endm
  307. /* Utility macro for declaring string literals */
  308. .macro string name:req, string
  309. .type \name , #object
  310. \name:
  311. .asciz "\string"
  312. .size \name , . - \name
  313. .endm
  314. #endif /* __ASM_ASSEMBLER_H__ */