assembler.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
  3. *
  4. * Copyright (C) 1996-2000 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __ASSEMBLY__
  20. #error "Only include this from assembly code"
  21. #endif
  22. #ifndef __ASM_ASSEMBLER_H
  23. #define __ASM_ASSEMBLER_H
  24. #include <asm/asm-offsets.h>
  25. #include <asm/cpufeature.h>
  26. #include <asm/page.h>
  27. #include <asm/pgtable-hwdef.h>
  28. #include <asm/ptrace.h>
  29. #include <asm/thread_info.h>
  30. /*
  31. * Enable and disable interrupts.
  32. */
  33. .macro disable_irq
  34. msr daifset, #2
  35. .endm
  36. .macro enable_irq
  37. msr daifclr, #2
  38. .endm
  39. .macro save_and_disable_irq, flags
  40. mrs \flags, daif
  41. msr daifset, #2
  42. .endm
  43. .macro restore_irq, flags
  44. msr daif, \flags
  45. .endm
  46. /*
  47. * Enable and disable debug exceptions.
  48. */
  49. .macro disable_dbg
  50. msr daifset, #8
  51. .endm
  52. .macro enable_dbg
  53. msr daifclr, #8
  54. .endm
  55. .macro disable_step_tsk, flgs, tmp
  56. tbz \flgs, #TIF_SINGLESTEP, 9990f
  57. mrs \tmp, mdscr_el1
  58. bic \tmp, \tmp, #1
  59. msr mdscr_el1, \tmp
  60. isb // Synchronise with enable_dbg
  61. 9990:
  62. .endm
  63. .macro enable_step_tsk, flgs, tmp
  64. tbz \flgs, #TIF_SINGLESTEP, 9990f
  65. disable_dbg
  66. mrs \tmp, mdscr_el1
  67. orr \tmp, \tmp, #1
  68. msr mdscr_el1, \tmp
  69. 9990:
  70. .endm
  71. /*
  72. * Enable both debug exceptions and interrupts. This is likely to be
  73. * faster than two daifclr operations, since writes to this register
  74. * are self-synchronising.
  75. */
  76. .macro enable_dbg_and_irq
  77. msr daifclr, #(8 | 2)
  78. .endm
  79. /*
  80. * SMP data memory barrier
  81. */
  82. .macro smp_dmb, opt
  83. dmb \opt
  84. .endm
  85. /*
  86. * NOP sequence
  87. */
  88. .macro nops, num
  89. .rept \num
  90. nop
  91. .endr
  92. .endm
  93. /*
  94. * Emit an entry into the exception table
  95. */
  96. .macro _asm_extable, from, to
  97. .pushsection __ex_table, "a"
  98. .align 3
  99. .long (\from - .), (\to - .)
  100. .popsection
  101. .endm
  102. #define USER(l, x...) \
  103. 9999: x; \
  104. _asm_extable 9999b, l
  105. /*
  106. * Register aliases.
  107. */
  108. lr .req x30 // link register
  109. /*
  110. * Vector entry
  111. */
  112. .macro ventry label
  113. .align 7
  114. b \label
  115. .endm
  116. /*
  117. * Select code when configured for BE.
  118. */
  119. #ifdef CONFIG_CPU_BIG_ENDIAN
  120. #define CPU_BE(code...) code
  121. #else
  122. #define CPU_BE(code...)
  123. #endif
  124. /*
  125. * Select code when configured for LE.
  126. */
  127. #ifdef CONFIG_CPU_BIG_ENDIAN
  128. #define CPU_LE(code...)
  129. #else
  130. #define CPU_LE(code...) code
  131. #endif
  132. /*
  133. * Define a macro that constructs a 64-bit value by concatenating two
  134. * 32-bit registers. Note that on big endian systems the order of the
  135. * registers is swapped.
  136. */
  137. #ifndef CONFIG_CPU_BIG_ENDIAN
  138. .macro regs_to_64, rd, lbits, hbits
  139. #else
  140. .macro regs_to_64, rd, hbits, lbits
  141. #endif
  142. orr \rd, \lbits, \hbits, lsl #32
  143. .endm
  144. /*
  145. * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
  146. * <symbol> is within the range +/- 4 GB of the PC when running
  147. * in core kernel context. In module context, a movz/movk sequence
  148. * is used, since modules may be loaded far away from the kernel
  149. * when KASLR is in effect.
  150. */
  151. /*
  152. * @dst: destination register (64 bit wide)
  153. * @sym: name of the symbol
  154. */
  155. .macro adr_l, dst, sym
  156. #ifndef MODULE
  157. adrp \dst, \sym
  158. add \dst, \dst, :lo12:\sym
  159. #else
  160. movz \dst, #:abs_g3:\sym
  161. movk \dst, #:abs_g2_nc:\sym
  162. movk \dst, #:abs_g1_nc:\sym
  163. movk \dst, #:abs_g0_nc:\sym
  164. #endif
  165. .endm
  166. /*
  167. * @dst: destination register (32 or 64 bit wide)
  168. * @sym: name of the symbol
  169. * @tmp: optional 64-bit scratch register to be used if <dst> is a
  170. * 32-bit wide register, in which case it cannot be used to hold
  171. * the address
  172. */
  173. .macro ldr_l, dst, sym, tmp=
  174. #ifndef MODULE
  175. .ifb \tmp
  176. adrp \dst, \sym
  177. ldr \dst, [\dst, :lo12:\sym]
  178. .else
  179. adrp \tmp, \sym
  180. ldr \dst, [\tmp, :lo12:\sym]
  181. .endif
  182. #else
  183. .ifb \tmp
  184. adr_l \dst, \sym
  185. ldr \dst, [\dst]
  186. .else
  187. adr_l \tmp, \sym
  188. ldr \dst, [\tmp]
  189. .endif
  190. #endif
  191. .endm
  192. /*
  193. * @src: source register (32 or 64 bit wide)
  194. * @sym: name of the symbol
  195. * @tmp: mandatory 64-bit scratch register to calculate the address
  196. * while <src> needs to be preserved.
  197. */
  198. .macro str_l, src, sym, tmp
  199. #ifndef MODULE
  200. adrp \tmp, \sym
  201. str \src, [\tmp, :lo12:\sym]
  202. #else
  203. adr_l \tmp, \sym
  204. str \src, [\tmp]
  205. #endif
  206. .endm
  207. /*
  208. * @dst: Result of per_cpu(sym, smp_processor_id())
  209. * @sym: The name of the per-cpu variable
  210. * @tmp: scratch register
  211. */
  212. .macro adr_this_cpu, dst, sym, tmp
  213. adr_l \dst, \sym
  214. mrs \tmp, tpidr_el1
  215. add \dst, \dst, \tmp
  216. .endm
  217. /*
  218. * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
  219. * @sym: The name of the per-cpu variable
  220. * @tmp: scratch register
  221. */
  222. .macro ldr_this_cpu dst, sym, tmp
  223. adr_l \dst, \sym
  224. mrs \tmp, tpidr_el1
  225. ldr \dst, [\dst, \tmp]
  226. .endm
  227. /*
  228. * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
  229. */
  230. .macro vma_vm_mm, rd, rn
  231. ldr \rd, [\rn, #VMA_VM_MM]
  232. .endm
  233. /*
  234. * mmid - get context id from mm pointer (mm->context.id)
  235. */
  236. .macro mmid, rd, rn
  237. ldr \rd, [\rn, #MM_CONTEXT_ID]
  238. .endm
  239. /*
  240. * read_ctr - read CTR_EL0. If the system has mismatched
  241. * cache line sizes, provide the system wide safe value
  242. * from arm64_ftr_reg_ctrel0.sys_val
  243. */
  244. .macro read_ctr, reg
  245. alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
  246. mrs \reg, ctr_el0 // read CTR
  247. nop
  248. alternative_else
  249. ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
  250. alternative_endif
  251. .endm
  252. /*
  253. * raw_dcache_line_size - get the minimum D-cache line size on this CPU
  254. * from the CTR register.
  255. */
  256. .macro raw_dcache_line_size, reg, tmp
  257. mrs \tmp, ctr_el0 // read CTR
  258. ubfm \tmp, \tmp, #16, #19 // cache line size encoding
  259. mov \reg, #4 // bytes per word
  260. lsl \reg, \reg, \tmp // actual cache line size
  261. .endm
  262. /*
  263. * dcache_line_size - get the safe D-cache line size across all CPUs
  264. */
  265. .macro dcache_line_size, reg, tmp
  266. read_ctr \tmp
  267. ubfm \tmp, \tmp, #16, #19 // cache line size encoding
  268. mov \reg, #4 // bytes per word
  269. lsl \reg, \reg, \tmp // actual cache line size
  270. .endm
  271. /*
  272. * raw_icache_line_size - get the minimum I-cache line size on this CPU
  273. * from the CTR register.
  274. */
  275. .macro raw_icache_line_size, reg, tmp
  276. mrs \tmp, ctr_el0 // read CTR
  277. and \tmp, \tmp, #0xf // cache line size encoding
  278. mov \reg, #4 // bytes per word
  279. lsl \reg, \reg, \tmp // actual cache line size
  280. .endm
  281. /*
  282. * icache_line_size - get the safe I-cache line size across all CPUs
  283. */
  284. .macro icache_line_size, reg, tmp
  285. read_ctr \tmp
  286. and \tmp, \tmp, #0xf // cache line size encoding
  287. mov \reg, #4 // bytes per word
  288. lsl \reg, \reg, \tmp // actual cache line size
  289. .endm
  290. /*
  291. * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
  292. */
  293. .macro tcr_set_idmap_t0sz, valreg, tmpreg
  294. #ifndef CONFIG_ARM64_VA_BITS_48
  295. ldr_l \tmpreg, idmap_t0sz
  296. bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
  297. #endif
  298. .endm
  299. /*
  300. * Macro to perform a data cache maintenance for the interval
  301. * [kaddr, kaddr + size)
  302. *
  303. * op: operation passed to dc instruction
  304. * domain: domain used in dsb instruciton
  305. * kaddr: starting virtual address of the region
  306. * size: size of the region
  307. * Corrupts: kaddr, size, tmp1, tmp2
  308. */
  309. .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
  310. dcache_line_size \tmp1, \tmp2
  311. add \size, \kaddr, \size
  312. sub \tmp2, \tmp1, #1
  313. bic \kaddr, \kaddr, \tmp2
  314. 9998:
  315. .if (\op == cvau || \op == cvac)
  316. alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
  317. dc \op, \kaddr
  318. alternative_else
  319. dc civac, \kaddr
  320. alternative_endif
  321. .else
  322. dc \op, \kaddr
  323. .endif
  324. add \kaddr, \kaddr, \tmp1
  325. cmp \kaddr, \size
  326. b.lo 9998b
  327. dsb \domain
  328. .endm
  329. /*
  330. * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
  331. */
  332. .macro reset_pmuserenr_el0, tmpreg
  333. mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
  334. sbfx \tmpreg, \tmpreg, #8, #4
  335. cmp \tmpreg, #1 // Skip if no PMU present
  336. b.lt 9000f
  337. msr pmuserenr_el0, xzr // Disable PMU access from EL0
  338. 9000:
  339. .endm
  340. /*
  341. * copy_page - copy src to dest using temp registers t1-t8
  342. */
  343. .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
  344. 9998: ldp \t1, \t2, [\src]
  345. ldp \t3, \t4, [\src, #16]
  346. ldp \t5, \t6, [\src, #32]
  347. ldp \t7, \t8, [\src, #48]
  348. add \src, \src, #64
  349. stnp \t1, \t2, [\dest]
  350. stnp \t3, \t4, [\dest, #16]
  351. stnp \t5, \t6, [\dest, #32]
  352. stnp \t7, \t8, [\dest, #48]
  353. add \dest, \dest, #64
  354. tst \src, #(PAGE_SIZE - 1)
  355. b.ne 9998b
  356. .endm
  357. /*
  358. * Annotate a function as position independent, i.e., safe to be called before
  359. * the kernel virtual mapping is activated.
  360. */
  361. #define ENDPIPROC(x) \
  362. .globl __pi_##x; \
  363. .type __pi_##x, %function; \
  364. .set __pi_##x, x; \
  365. .size __pi_##x, . - x; \
  366. ENDPROC(x)
  367. /*
  368. * Emit a 64-bit absolute little endian symbol reference in a way that
  369. * ensures that it will be resolved at build time, even when building a
  370. * PIE binary. This requires cooperation from the linker script, which
  371. * must emit the lo32/hi32 halves individually.
  372. */
  373. .macro le64sym, sym
  374. .long \sym\()_lo32
  375. .long \sym\()_hi32
  376. .endm
  377. /*
  378. * mov_q - move an immediate constant into a 64-bit register using
  379. * between 2 and 4 movz/movk instructions (depending on the
  380. * magnitude and sign of the operand)
  381. */
  382. .macro mov_q, reg, val
  383. .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
  384. movz \reg, :abs_g1_s:\val
  385. .else
  386. .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
  387. movz \reg, :abs_g2_s:\val
  388. .else
  389. movz \reg, :abs_g3:\val
  390. movk \reg, :abs_g2_nc:\val
  391. .endif
  392. movk \reg, :abs_g1_nc:\val
  393. .endif
  394. movk \reg, :abs_g0_nc:\val
  395. .endm
  396. /*
  397. * Return the current thread_info.
  398. */
  399. .macro get_thread_info, rd
  400. mrs \rd, sp_el0
  401. .endm
  402. /*
  403. * Errata workaround post TTBR0_EL1 update.
  404. */
  405. .macro post_ttbr0_update_workaround
  406. #ifdef CONFIG_CAVIUM_ERRATUM_27456
  407. alternative_if ARM64_WORKAROUND_CAVIUM_27456
  408. ic iallu
  409. dsb nsh
  410. isb
  411. alternative_else_nop_endif
  412. #endif
  413. .endm
  414. #endif /* __ASM_ASSEMBLER_H */