assembler.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_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. bne 1f
  239. orr \reg, \reg, #PSR_A_BIT
  240. adr lr, BSYM(2f)
  241. msr spsr_cxsf, \reg
  242. __MSR_ELR_HYP(14)
  243. __ERET
  244. 1: msr cpsr_c, \reg
  245. 2:
  246. .endm
  247. /*
  248. * STRT/LDRT access macros with ARM and Thumb-2 variants
  249. */
  250. #ifdef CONFIG_THUMB2_KERNEL
  251. .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
  252. 9999:
  253. .if \inc == 1
  254. \instr\cond\()b\()\t\().w \reg, [\ptr, #\off]
  255. .elseif \inc == 4
  256. \instr\cond\()\t\().w \reg, [\ptr, #\off]
  257. .else
  258. .error "Unsupported inc macro argument"
  259. .endif
  260. .pushsection __ex_table,"a"
  261. .align 3
  262. .long 9999b, \abort
  263. .popsection
  264. .endm
  265. .macro usracc, instr, reg, ptr, inc, cond, rept, abort
  266. @ explicit IT instruction needed because of the label
  267. @ introduced by the USER macro
  268. .ifnc \cond,al
  269. .if \rept == 1
  270. itt \cond
  271. .elseif \rept == 2
  272. ittt \cond
  273. .else
  274. .error "Unsupported rept macro argument"
  275. .endif
  276. .endif
  277. @ Slightly optimised to avoid incrementing the pointer twice
  278. usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
  279. .if \rept == 2
  280. usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
  281. .endif
  282. add\cond \ptr, #\rept * \inc
  283. .endm
  284. #else /* !CONFIG_THUMB2_KERNEL */
  285. .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
  286. .rept \rept
  287. 9999:
  288. .if \inc == 1
  289. \instr\cond\()b\()\t \reg, [\ptr], #\inc
  290. .elseif \inc == 4
  291. \instr\cond\()\t \reg, [\ptr], #\inc
  292. .else
  293. .error "Unsupported inc macro argument"
  294. .endif
  295. .pushsection __ex_table,"a"
  296. .align 3
  297. .long 9999b, \abort
  298. .popsection
  299. .endr
  300. .endm
  301. #endif /* CONFIG_THUMB2_KERNEL */
  302. .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  303. usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
  304. .endm
  305. .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  306. usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
  307. .endm
  308. /* Utility macro for declaring string literals */
  309. .macro string name:req, string
  310. .type \name , #object
  311. \name:
  312. .asciz "\string"
  313. .size \name , . - \name
  314. .endm
  315. .macro check_uaccess, addr:req, size:req, limit:req, tmp:req, bad:req
  316. #ifndef CONFIG_CPU_USE_DOMAINS
  317. adds \tmp, \addr, #\size - 1
  318. sbcccs \tmp, \tmp, \limit
  319. bcs \bad
  320. #endif
  321. .endm
  322. #endif /* __ASM_ASSEMBLER_H__ */