assembler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. .macro bug, msg, line
  469. #ifdef CONFIG_THUMB2_KERNEL
  470. 1: .inst 0xde02
  471. #else
  472. 1: .inst 0xe7f001f2
  473. #endif
  474. #ifdef CONFIG_DEBUG_BUGVERBOSE
  475. .pushsection .rodata.str, "aMS", %progbits, 1
  476. 2: .asciz "\msg"
  477. .popsection
  478. .pushsection __bug_table, "aw"
  479. .align 2
  480. .word 1b, 2b
  481. .hword \line
  482. .popsection
  483. #endif
  484. .endm
  485. #ifdef CONFIG_KPROBES
  486. #define _ASM_NOKPROBE(entry) \
  487. .pushsection "_kprobe_blacklist", "aw" ; \
  488. .balign 4 ; \
  489. .long entry; \
  490. .popsection
  491. #else
  492. #define _ASM_NOKPROBE(entry)
  493. #endif
  494. #endif /* __ASM_ASSEMBLER_H__ */