assembler.h 11 KB

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