assembler.h 11 KB

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