assembler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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/page.h>
  28. #include <asm/pgtable-hwdef.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/thread_info.h>
  31. .macro save_and_disable_daif, flags
  32. mrs \flags, daif
  33. msr daifset, #0xf
  34. .endm
  35. .macro disable_daif
  36. msr daifset, #0xf
  37. .endm
  38. .macro enable_daif
  39. msr daifclr, #0xf
  40. .endm
  41. .macro restore_daif, flags:req
  42. msr daif, \flags
  43. .endm
  44. /* Only on aarch64 pstate, PSR_D_BIT is different for aarch32 */
  45. .macro inherit_daif, pstate:req, tmp:req
  46. and \tmp, \pstate, #(PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
  47. msr daif, \tmp
  48. .endm
  49. /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
  50. .macro enable_da_f
  51. msr daifclr, #(8 | 4 | 1)
  52. .endm
  53. /*
  54. * Enable and disable interrupts.
  55. */
  56. .macro disable_irq
  57. msr daifset, #2
  58. .endm
  59. .macro enable_irq
  60. msr daifclr, #2
  61. .endm
  62. .macro save_and_disable_irq, flags
  63. mrs \flags, daif
  64. msr daifset, #2
  65. .endm
  66. .macro restore_irq, flags
  67. msr daif, \flags
  68. .endm
  69. .macro enable_dbg
  70. msr daifclr, #8
  71. .endm
  72. .macro disable_step_tsk, flgs, tmp
  73. tbz \flgs, #TIF_SINGLESTEP, 9990f
  74. mrs \tmp, mdscr_el1
  75. bic \tmp, \tmp, #DBG_MDSCR_SS
  76. msr mdscr_el1, \tmp
  77. isb // Synchronise with enable_dbg
  78. 9990:
  79. .endm
  80. /* call with daif masked */
  81. .macro enable_step_tsk, flgs, tmp
  82. tbz \flgs, #TIF_SINGLESTEP, 9990f
  83. mrs \tmp, mdscr_el1
  84. orr \tmp, \tmp, #DBG_MDSCR_SS
  85. msr mdscr_el1, \tmp
  86. 9990:
  87. .endm
  88. /*
  89. * SMP data memory barrier
  90. */
  91. .macro smp_dmb, opt
  92. dmb \opt
  93. .endm
  94. /*
  95. * RAS Error Synchronization barrier
  96. */
  97. .macro esb
  98. hint #16
  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. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  236. mrs \tmp, tpidr_el1
  237. alternative_else
  238. mrs \tmp, tpidr_el2
  239. alternative_endif
  240. add \dst, \dst, \tmp
  241. .endm
  242. /*
  243. * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
  244. * @sym: The name of the per-cpu variable
  245. * @tmp: scratch register
  246. */
  247. .macro ldr_this_cpu dst, sym, tmp
  248. adr_l \dst, \sym
  249. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  250. mrs \tmp, tpidr_el1
  251. alternative_else
  252. mrs \tmp, tpidr_el2
  253. alternative_endif
  254. ldr \dst, [\dst, \tmp]
  255. .endm
  256. /*
  257. * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
  258. */
  259. .macro vma_vm_mm, rd, rn
  260. ldr \rd, [\rn, #VMA_VM_MM]
  261. .endm
  262. /*
  263. * mmid - get context id from mm pointer (mm->context.id)
  264. */
  265. .macro mmid, rd, rn
  266. ldr \rd, [\rn, #MM_CONTEXT_ID]
  267. .endm
  268. /*
  269. * read_ctr - read CTR_EL0. If the system has mismatched
  270. * cache line sizes, provide the system wide safe value
  271. * from arm64_ftr_reg_ctrel0.sys_val
  272. */
  273. .macro read_ctr, reg
  274. alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
  275. mrs \reg, ctr_el0 // read CTR
  276. nop
  277. alternative_else
  278. ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
  279. alternative_endif
  280. .endm
  281. /*
  282. * raw_dcache_line_size - get the minimum D-cache line size on this CPU
  283. * from the CTR register.
  284. */
  285. .macro raw_dcache_line_size, reg, tmp
  286. mrs \tmp, ctr_el0 // read CTR
  287. ubfm \tmp, \tmp, #16, #19 // cache line size encoding
  288. mov \reg, #4 // bytes per word
  289. lsl \reg, \reg, \tmp // actual cache line size
  290. .endm
  291. /*
  292. * dcache_line_size - get the safe D-cache line size across all CPUs
  293. */
  294. .macro dcache_line_size, reg, tmp
  295. read_ctr \tmp
  296. ubfm \tmp, \tmp, #16, #19 // cache line size encoding
  297. mov \reg, #4 // bytes per word
  298. lsl \reg, \reg, \tmp // actual cache line size
  299. .endm
  300. /*
  301. * raw_icache_line_size - get the minimum I-cache line size on this CPU
  302. * from the CTR register.
  303. */
  304. .macro raw_icache_line_size, reg, tmp
  305. mrs \tmp, ctr_el0 // read CTR
  306. and \tmp, \tmp, #0xf // cache line size encoding
  307. mov \reg, #4 // bytes per word
  308. lsl \reg, \reg, \tmp // actual cache line size
  309. .endm
  310. /*
  311. * icache_line_size - get the safe I-cache line size across all CPUs
  312. */
  313. .macro icache_line_size, reg, tmp
  314. read_ctr \tmp
  315. and \tmp, \tmp, #0xf // cache line size encoding
  316. mov \reg, #4 // bytes per word
  317. lsl \reg, \reg, \tmp // actual cache line size
  318. .endm
  319. /*
  320. * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
  321. */
  322. .macro tcr_set_idmap_t0sz, valreg, tmpreg
  323. ldr_l \tmpreg, idmap_t0sz
  324. bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
  325. .endm
  326. /*
  327. * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
  328. * ID_AA64MMFR0_EL1.PARange value
  329. *
  330. * tcr: register with the TCR_ELx value to be updated
  331. * pos: IPS or PS bitfield position
  332. * tmp{0,1}: temporary registers
  333. */
  334. .macro tcr_compute_pa_size, tcr, pos, tmp0, tmp1
  335. mrs \tmp0, ID_AA64MMFR0_EL1
  336. // Narrow PARange to fit the PS field in TCR_ELx
  337. ubfx \tmp0, \tmp0, #ID_AA64MMFR0_PARANGE_SHIFT, #3
  338. mov \tmp1, #ID_AA64MMFR0_PARANGE_MAX
  339. cmp \tmp0, \tmp1
  340. csel \tmp0, \tmp1, \tmp0, hi
  341. bfi \tcr, \tmp0, \pos, #3
  342. .endm
  343. /*
  344. * Macro to perform a data cache maintenance for the interval
  345. * [kaddr, kaddr + size)
  346. *
  347. * op: operation passed to dc instruction
  348. * domain: domain used in dsb instruciton
  349. * kaddr: starting virtual address of the region
  350. * size: size of the region
  351. * Corrupts: kaddr, size, tmp1, tmp2
  352. */
  353. .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
  354. dcache_line_size \tmp1, \tmp2
  355. add \size, \kaddr, \size
  356. sub \tmp2, \tmp1, #1
  357. bic \kaddr, \kaddr, \tmp2
  358. 9998:
  359. .if (\op == cvau || \op == cvac)
  360. alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
  361. dc \op, \kaddr
  362. alternative_else
  363. dc civac, \kaddr
  364. alternative_endif
  365. .elseif (\op == cvap)
  366. alternative_if ARM64_HAS_DCPOP
  367. sys 3, c7, c12, 1, \kaddr // dc cvap
  368. alternative_else
  369. dc cvac, \kaddr
  370. alternative_endif
  371. .else
  372. dc \op, \kaddr
  373. .endif
  374. add \kaddr, \kaddr, \tmp1
  375. cmp \kaddr, \size
  376. b.lo 9998b
  377. dsb \domain
  378. .endm
  379. /*
  380. * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
  381. */
  382. .macro reset_pmuserenr_el0, tmpreg
  383. mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
  384. sbfx \tmpreg, \tmpreg, #8, #4
  385. cmp \tmpreg, #1 // Skip if no PMU present
  386. b.lt 9000f
  387. msr pmuserenr_el0, xzr // Disable PMU access from EL0
  388. 9000:
  389. .endm
  390. /*
  391. * copy_page - copy src to dest using temp registers t1-t8
  392. */
  393. .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
  394. 9998: ldp \t1, \t2, [\src]
  395. ldp \t3, \t4, [\src, #16]
  396. ldp \t5, \t6, [\src, #32]
  397. ldp \t7, \t8, [\src, #48]
  398. add \src, \src, #64
  399. stnp \t1, \t2, [\dest]
  400. stnp \t3, \t4, [\dest, #16]
  401. stnp \t5, \t6, [\dest, #32]
  402. stnp \t7, \t8, [\dest, #48]
  403. add \dest, \dest, #64
  404. tst \src, #(PAGE_SIZE - 1)
  405. b.ne 9998b
  406. .endm
  407. /*
  408. * Annotate a function as position independent, i.e., safe to be called before
  409. * the kernel virtual mapping is activated.
  410. */
  411. #define ENDPIPROC(x) \
  412. .globl __pi_##x; \
  413. .type __pi_##x, %function; \
  414. .set __pi_##x, x; \
  415. .size __pi_##x, . - x; \
  416. ENDPROC(x)
  417. /*
  418. * Annotate a function as being unsuitable for kprobes.
  419. */
  420. #ifdef CONFIG_KPROBES
  421. #define NOKPROBE(x) \
  422. .pushsection "_kprobe_blacklist", "aw"; \
  423. .quad x; \
  424. .popsection;
  425. #else
  426. #define NOKPROBE(x)
  427. #endif
  428. /*
  429. * Emit a 64-bit absolute little endian symbol reference in a way that
  430. * ensures that it will be resolved at build time, even when building a
  431. * PIE binary. This requires cooperation from the linker script, which
  432. * must emit the lo32/hi32 halves individually.
  433. */
  434. .macro le64sym, sym
  435. .long \sym\()_lo32
  436. .long \sym\()_hi32
  437. .endm
  438. /*
  439. * mov_q - move an immediate constant into a 64-bit register using
  440. * between 2 and 4 movz/movk instructions (depending on the
  441. * magnitude and sign of the operand)
  442. */
  443. .macro mov_q, reg, val
  444. .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
  445. movz \reg, :abs_g1_s:\val
  446. .else
  447. .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
  448. movz \reg, :abs_g2_s:\val
  449. .else
  450. movz \reg, :abs_g3:\val
  451. movk \reg, :abs_g2_nc:\val
  452. .endif
  453. movk \reg, :abs_g1_nc:\val
  454. .endif
  455. movk \reg, :abs_g0_nc:\val
  456. .endm
  457. /*
  458. * Return the current thread_info.
  459. */
  460. .macro get_thread_info, rd
  461. mrs \rd, sp_el0
  462. .endm
  463. /*
  464. * Arrange a physical address in a TTBR register, taking care of 52-bit
  465. * addresses.
  466. *
  467. * phys: physical address, preserved
  468. * ttbr: returns the TTBR value
  469. */
  470. .macro phys_to_ttbr, phys, ttbr
  471. #ifdef CONFIG_ARM64_PA_BITS_52
  472. orr \ttbr, \phys, \phys, lsr #46
  473. and \ttbr, \ttbr, #TTBR_BADDR_MASK_52
  474. #else
  475. mov \ttbr, \phys
  476. #endif
  477. .endm
  478. .macro pte_to_phys, phys, pte
  479. #ifdef CONFIG_ARM64_PA_BITS_52
  480. ubfiz \phys, \pte, #(48 - 16 - 12), #16
  481. bfxil \phys, \pte, #16, #32
  482. lsl \phys, \phys, #16
  483. #else
  484. and \phys, \pte, #PTE_ADDR_MASK
  485. #endif
  486. .endm
  487. /**
  488. * Errata workaround prior to disable MMU. Insert an ISB immediately prior
  489. * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
  490. */
  491. .macro pre_disable_mmu_workaround
  492. #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
  493. isb
  494. #endif
  495. .endm
  496. #endif /* __ASM_ASSEMBLER_H */