PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/arm/include/asm/assembler.h

https://github.com/tklauser/linux-nios2
C Header | 519 lines | 373 code | 51 blank | 95 comment | 12 complexity | bdb72adf022c30b68a464066aacb6b52 MD5 | raw file
  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. #ifdef CONFIG_CPU_V7M
  148. mrs \oldcpsr, primask
  149. #else
  150. mrs \oldcpsr, cpsr
  151. #endif
  152. disable_irq_notrace
  153. .endm
  154. /*
  155. * Restore interrupt state previously stored in a register. We don't
  156. * guarantee that this will preserve the flags.
  157. */
  158. .macro restore_irqs_notrace, oldcpsr
  159. #ifdef CONFIG_CPU_V7M
  160. msr primask, \oldcpsr
  161. #else
  162. msr cpsr_c, \oldcpsr
  163. #endif
  164. .endm
  165. .macro restore_irqs, oldcpsr
  166. tst \oldcpsr, #PSR_I_BIT
  167. asm_trace_hardirqs_on cond=eq
  168. restore_irqs_notrace \oldcpsr
  169. .endm
  170. /*
  171. * Assembly version of "adr rd, BSYM(sym)". This should only be used to
  172. * reference local symbols in the same assembly file which are to be
  173. * resolved by the assembler. Other usage is undefined.
  174. */
  175. .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
  176. .macro badr\c, rd, sym
  177. #ifdef CONFIG_THUMB2_KERNEL
  178. adr\c \rd, \sym + 1
  179. #else
  180. adr\c \rd, \sym
  181. #endif
  182. .endm
  183. .endr
  184. /*
  185. * Get current thread_info.
  186. */
  187. .macro get_thread_info, rd
  188. ARM( mov \rd, sp, lsr #THREAD_SIZE_ORDER + PAGE_SHIFT )
  189. THUMB( mov \rd, sp )
  190. THUMB( lsr \rd, \rd, #THREAD_SIZE_ORDER + PAGE_SHIFT )
  191. mov \rd, \rd, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT
  192. .endm
  193. /*
  194. * Increment/decrement the preempt count.
  195. */
  196. #ifdef CONFIG_PREEMPT_COUNT
  197. .macro inc_preempt_count, ti, tmp
  198. ldr \tmp, [\ti, #TI_PREEMPT] @ get preempt count
  199. add \tmp, \tmp, #1 @ increment it
  200. str \tmp, [\ti, #TI_PREEMPT]
  201. .endm
  202. .macro dec_preempt_count, ti, tmp
  203. ldr \tmp, [\ti, #TI_PREEMPT] @ get preempt count
  204. sub \tmp, \tmp, #1 @ decrement it
  205. str \tmp, [\ti, #TI_PREEMPT]
  206. .endm
  207. .macro dec_preempt_count_ti, ti, tmp
  208. get_thread_info \ti
  209. dec_preempt_count \ti, \tmp
  210. .endm
  211. #else
  212. .macro inc_preempt_count, ti, tmp
  213. .endm
  214. .macro dec_preempt_count, ti, tmp
  215. .endm
  216. .macro dec_preempt_count_ti, ti, tmp
  217. .endm
  218. #endif
  219. #define USER(x...) \
  220. 9999: x; \
  221. .pushsection __ex_table,"a"; \
  222. .align 3; \
  223. .long 9999b,9001f; \
  224. .popsection
  225. #ifdef CONFIG_SMP
  226. #define ALT_SMP(instr...) \
  227. 9998: instr
  228. /*
  229. * Note: if you get assembler errors from ALT_UP() when building with
  230. * CONFIG_THUMB2_KERNEL, you almost certainly need to use
  231. * ALT_SMP( W(instr) ... )
  232. */
  233. #define ALT_UP(instr...) \
  234. .pushsection ".alt.smp.init", "a" ;\
  235. .long 9998b ;\
  236. 9997: instr ;\
  237. .if . - 9997b == 2 ;\
  238. nop ;\
  239. .endif ;\
  240. .if . - 9997b != 4 ;\
  241. .error "ALT_UP() content must assemble to exactly 4 bytes";\
  242. .endif ;\
  243. .popsection
  244. #define ALT_UP_B(label) \
  245. .equ up_b_offset, label - 9998b ;\
  246. .pushsection ".alt.smp.init", "a" ;\
  247. .long 9998b ;\
  248. W(b) . + up_b_offset ;\
  249. .popsection
  250. #else
  251. #define ALT_SMP(instr...)
  252. #define ALT_UP(instr...) instr
  253. #define ALT_UP_B(label) b label
  254. #endif
  255. /*
  256. * Instruction barrier
  257. */
  258. .macro instr_sync
  259. #if __LINUX_ARM_ARCH__ >= 7
  260. isb
  261. #elif __LINUX_ARM_ARCH__ == 6
  262. mcr p15, 0, r0, c7, c5, 4
  263. #endif
  264. .endm
  265. /*
  266. * SMP data memory barrier
  267. */
  268. .macro smp_dmb mode
  269. #ifdef CONFIG_SMP
  270. #if __LINUX_ARM_ARCH__ >= 7
  271. .ifeqs "\mode","arm"
  272. ALT_SMP(dmb ish)
  273. .else
  274. ALT_SMP(W(dmb) ish)
  275. .endif
  276. #elif __LINUX_ARM_ARCH__ == 6
  277. ALT_SMP(mcr p15, 0, r0, c7, c10, 5) @ dmb
  278. #else
  279. #error Incompatible SMP platform
  280. #endif
  281. .ifeqs "\mode","arm"
  282. ALT_UP(nop)
  283. .else
  284. ALT_UP(W(nop))
  285. .endif
  286. #endif
  287. .endm
  288. #if defined(CONFIG_CPU_V7M)
  289. /*
  290. * setmode is used to assert to be in svc mode during boot. For v7-M
  291. * this is done in __v7m_setup, so setmode can be empty here.
  292. */
  293. .macro setmode, mode, reg
  294. .endm
  295. #elif defined(CONFIG_THUMB2_KERNEL)
  296. .macro setmode, mode, reg
  297. mov \reg, #\mode
  298. msr cpsr_c, \reg
  299. .endm
  300. #else
  301. .macro setmode, mode, reg
  302. msr cpsr_c, #\mode
  303. .endm
  304. #endif
  305. /*
  306. * Helper macro to enter SVC mode cleanly and mask interrupts. reg is
  307. * a scratch register for the macro to overwrite.
  308. *
  309. * This macro is intended for forcing the CPU into SVC mode at boot time.
  310. * you cannot return to the original mode.
  311. */
  312. .macro safe_svcmode_maskall reg:req
  313. #if __LINUX_ARM_ARCH__ >= 6 && !defined(CONFIG_CPU_V7M)
  314. mrs \reg , cpsr
  315. eor \reg, \reg, #HYP_MODE
  316. tst \reg, #MODE_MASK
  317. bic \reg , \reg , #MODE_MASK
  318. orr \reg , \reg , #PSR_I_BIT | PSR_F_BIT | SVC_MODE
  319. THUMB( orr \reg , \reg , #PSR_T_BIT )
  320. bne 1f
  321. orr \reg, \reg, #PSR_A_BIT
  322. badr lr, 2f
  323. msr spsr_cxsf, \reg
  324. __MSR_ELR_HYP(14)
  325. __ERET
  326. 1: msr cpsr_c, \reg
  327. 2:
  328. #else
  329. /*
  330. * workaround for possibly broken pre-v6 hardware
  331. * (akita, Sharp Zaurus C-1000, PXA270-based)
  332. */
  333. setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, \reg
  334. #endif
  335. .endm
  336. /*
  337. * STRT/LDRT access macros with ARM and Thumb-2 variants
  338. */
  339. #ifdef CONFIG_THUMB2_KERNEL
  340. .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
  341. 9999:
  342. .if \inc == 1
  343. \instr\cond\()b\()\t\().w \reg, [\ptr, #\off]
  344. .elseif \inc == 4
  345. \instr\cond\()\t\().w \reg, [\ptr, #\off]
  346. .else
  347. .error "Unsupported inc macro argument"
  348. .endif
  349. .pushsection __ex_table,"a"
  350. .align 3
  351. .long 9999b, \abort
  352. .popsection
  353. .endm
  354. .macro usracc, instr, reg, ptr, inc, cond, rept, abort
  355. @ explicit IT instruction needed because of the label
  356. @ introduced by the USER macro
  357. .ifnc \cond,al
  358. .if \rept == 1
  359. itt \cond
  360. .elseif \rept == 2
  361. ittt \cond
  362. .else
  363. .error "Unsupported rept macro argument"
  364. .endif
  365. .endif
  366. @ Slightly optimised to avoid incrementing the pointer twice
  367. usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
  368. .if \rept == 2
  369. usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
  370. .endif
  371. add\cond \ptr, #\rept * \inc
  372. .endm
  373. #else /* !CONFIG_THUMB2_KERNEL */
  374. .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
  375. .rept \rept
  376. 9999:
  377. .if \inc == 1
  378. \instr\cond\()b\()\t \reg, [\ptr], #\inc
  379. .elseif \inc == 4
  380. \instr\cond\()\t \reg, [\ptr], #\inc
  381. .else
  382. .error "Unsupported inc macro argument"
  383. .endif
  384. .pushsection __ex_table,"a"
  385. .align 3
  386. .long 9999b, \abort
  387. .popsection
  388. .endr
  389. .endm
  390. #endif /* CONFIG_THUMB2_KERNEL */
  391. .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  392. usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
  393. .endm
  394. .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
  395. usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
  396. .endm
  397. /* Utility macro for declaring string literals */
  398. .macro string name:req, string
  399. .type \name , #object
  400. \name:
  401. .asciz "\string"
  402. .size \name , . - \name
  403. .endm
  404. .macro check_uaccess, addr:req, size:req, limit:req, tmp:req, bad:req
  405. #ifndef CONFIG_CPU_USE_DOMAINS
  406. adds \tmp, \addr, #\size - 1
  407. sbcccs \tmp, \tmp, \limit
  408. bcs \bad
  409. #endif
  410. .endm
  411. .macro uaccess_disable, tmp, isb=1
  412. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  413. /*
  414. * Whenever we re-enter userspace, the domains should always be
  415. * set appropriately.
  416. */
  417. mov \tmp, #DACR_UACCESS_DISABLE
  418. mcr p15, 0, \tmp, c3, c0, 0 @ Set domain register
  419. .if \isb
  420. instr_sync
  421. .endif
  422. #endif
  423. .endm
  424. .macro uaccess_enable, tmp, isb=1
  425. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  426. /*
  427. * Whenever we re-enter userspace, the domains should always be
  428. * set appropriately.
  429. */
  430. mov \tmp, #DACR_UACCESS_ENABLE
  431. mcr p15, 0, \tmp, c3, c0, 0
  432. .if \isb
  433. instr_sync
  434. .endif
  435. #endif
  436. .endm
  437. .macro uaccess_save, tmp
  438. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  439. mrc p15, 0, \tmp, c3, c0, 0
  440. str \tmp, [sp, #SVC_DACR]
  441. #endif
  442. .endm
  443. .macro uaccess_restore
  444. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  445. ldr r0, [sp, #SVC_DACR]
  446. mcr p15, 0, r0, c3, c0, 0
  447. #endif
  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__ */