assembler.h 7.8 KB

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