assembler.h 12 KB

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