assembler.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
  4. *
  5. * Copyright (C) 1996-2000 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #ifndef __ASSEMBLY__
  9. #error "Only include this from assembly code"
  10. #endif
  11. #ifndef __ASM_ASSEMBLER_H
  12. #define __ASM_ASSEMBLER_H
  13. #include <linux/export.h>
  14. #include <asm/alternative.h>
  15. #include <asm/asm-bug.h>
  16. #include <asm/asm-extable.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/cputype.h>
  20. #include <asm/debug-monitors.h>
  21. #include <asm/page.h>
  22. #include <asm/pgtable-hwdef.h>
  23. #include <asm/ptrace.h>
  24. #include <asm/thread_info.h>
  25. /*
  26. * Provide a wxN alias for each wN register so what we can paste a xN
  27. * reference after a 'w' to obtain the 32-bit version.
  28. */
  29. .irp n,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
  30. wx\n .req w\n
  31. .endr
  32. .macro disable_daif
  33. msr daifset, #0xf
  34. .endm
  35. /*
  36. * Save/restore interrupts.
  37. */
  38. .macro save_and_disable_daif, flags
  39. mrs \flags, daif
  40. msr daifset, #0xf
  41. .endm
  42. .macro save_and_disable_irq, flags
  43. mrs \flags, daif
  44. msr daifset, #3
  45. .endm
  46. .macro restore_irq, flags
  47. msr daif, \flags
  48. .endm
  49. .macro disable_step_tsk, flgs, tmp
  50. tbz \flgs, #TIF_SINGLESTEP, 9990f
  51. mrs \tmp, mdscr_el1
  52. bic \tmp, \tmp, #DBG_MDSCR_SS
  53. msr mdscr_el1, \tmp
  54. isb // Take effect before a subsequent clear of DAIF.D
  55. 9990:
  56. .endm
  57. /* call with daif masked */
  58. .macro enable_step_tsk, flgs, tmp
  59. tbz \flgs, #TIF_SINGLESTEP, 9990f
  60. mrs \tmp, mdscr_el1
  61. orr \tmp, \tmp, #DBG_MDSCR_SS
  62. msr mdscr_el1, \tmp
  63. 9990:
  64. .endm
  65. /*
  66. * RAS Error Synchronization barrier
  67. */
  68. .macro esb
  69. #ifdef CONFIG_ARM64_RAS_EXTN
  70. hint #16
  71. #else
  72. nop
  73. #endif
  74. .endm
  75. /*
  76. * Value prediction barrier
  77. */
  78. .macro csdb
  79. hint #20
  80. .endm
  81. /*
  82. * Clear Branch History instruction
  83. */
  84. .macro clearbhb
  85. hint #22
  86. .endm
  87. /*
  88. * Speculation barrier
  89. */
  90. .macro sb
  91. alternative_if_not ARM64_HAS_SB
  92. dsb nsh
  93. isb
  94. alternative_else
  95. SB_BARRIER_INSN
  96. nop
  97. alternative_endif
  98. .endm
  99. /*
  100. * NOP sequence
  101. */
  102. .macro nops, num
  103. .rept \num
  104. nop
  105. .endr
  106. .endm
  107. /*
  108. * Register aliases.
  109. */
  110. lr .req x30 // link register
  111. /*
  112. * Vector entry
  113. */
  114. .macro ventry label
  115. .align 7
  116. b \label
  117. .endm
  118. /*
  119. * Select code when configured for BE.
  120. */
  121. #ifdef CONFIG_CPU_BIG_ENDIAN
  122. #define CPU_BE(code...) code
  123. #else
  124. #define CPU_BE(code...)
  125. #endif
  126. /*
  127. * Select code when configured for LE.
  128. */
  129. #ifdef CONFIG_CPU_BIG_ENDIAN
  130. #define CPU_LE(code...)
  131. #else
  132. #define CPU_LE(code...) code
  133. #endif
  134. /*
  135. * Define a macro that constructs a 64-bit value by concatenating two
  136. * 32-bit registers. Note that on big endian systems the order of the
  137. * registers is swapped.
  138. */
  139. #ifndef CONFIG_CPU_BIG_ENDIAN
  140. .macro regs_to_64, rd, lbits, hbits
  141. #else
  142. .macro regs_to_64, rd, hbits, lbits
  143. #endif
  144. orr \rd, \lbits, \hbits, lsl #32
  145. .endm
  146. /*
  147. * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
  148. * <symbol> is within the range +/- 4 GB of the PC.
  149. */
  150. /*
  151. * @dst: destination register (64 bit wide)
  152. * @sym: name of the symbol
  153. */
  154. .macro adr_l, dst, sym
  155. adrp \dst, \sym
  156. add \dst, \dst, :lo12:\sym
  157. .endm
  158. /*
  159. * @dst: destination register (32 or 64 bit wide)
  160. * @sym: name of the symbol
  161. * @tmp: optional 64-bit scratch register to be used if <dst> is a
  162. * 32-bit wide register, in which case it cannot be used to hold
  163. * the address
  164. */
  165. .macro ldr_l, dst, sym, tmp=
  166. .ifb \tmp
  167. adrp \dst, \sym
  168. ldr \dst, [\dst, :lo12:\sym]
  169. .else
  170. adrp \tmp, \sym
  171. ldr \dst, [\tmp, :lo12:\sym]
  172. .endif
  173. .endm
  174. /*
  175. * @src: source register (32 or 64 bit wide)
  176. * @sym: name of the symbol
  177. * @tmp: mandatory 64-bit scratch register to calculate the address
  178. * while <src> needs to be preserved.
  179. */
  180. .macro str_l, src, sym, tmp
  181. adrp \tmp, \sym
  182. str \src, [\tmp, :lo12:\sym]
  183. .endm
  184. /*
  185. * @dst: destination register
  186. */
  187. #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
  188. .macro get_this_cpu_offset, dst
  189. mrs \dst, tpidr_el2
  190. .endm
  191. #else
  192. .macro get_this_cpu_offset, dst
  193. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  194. mrs \dst, tpidr_el1
  195. alternative_else
  196. mrs \dst, tpidr_el2
  197. alternative_endif
  198. .endm
  199. .macro set_this_cpu_offset, src
  200. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  201. msr tpidr_el1, \src
  202. alternative_else
  203. msr tpidr_el2, \src
  204. alternative_endif
  205. .endm
  206. #endif
  207. /*
  208. * @dst: Result of per_cpu(sym, smp_processor_id()) (can be SP)
  209. * @sym: The name of the per-cpu variable
  210. * @tmp: scratch register
  211. */
  212. .macro adr_this_cpu, dst, sym, tmp
  213. adrp \tmp, \sym
  214. add \dst, \tmp, #:lo12:\sym
  215. get_this_cpu_offset \tmp
  216. add \dst, \dst, \tmp
  217. .endm
  218. /*
  219. * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
  220. * @sym: The name of the per-cpu variable
  221. * @tmp: scratch register
  222. */
  223. .macro ldr_this_cpu dst, sym, tmp
  224. adr_l \dst, \sym
  225. get_this_cpu_offset \tmp
  226. ldr \dst, [\dst, \tmp]
  227. .endm
  228. /*
  229. * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
  230. */
  231. .macro vma_vm_mm, rd, rn
  232. ldr \rd, [\rn, #VMA_VM_MM]
  233. .endm
  234. /*
  235. * read_ctr - read CTR_EL0. If the system has mismatched register fields,
  236. * provide the system wide safe value from arm64_ftr_reg_ctrel0.sys_val
  237. */
  238. .macro read_ctr, reg
  239. #ifndef __KVM_NVHE_HYPERVISOR__
  240. alternative_if_not ARM64_MISMATCHED_CACHE_TYPE
  241. mrs \reg, ctr_el0 // read CTR
  242. nop
  243. alternative_else
  244. ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
  245. alternative_endif
  246. #else
  247. alternative_if_not ARM64_KVM_PROTECTED_MODE
  248. ASM_BUG()
  249. alternative_else_nop_endif
  250. alternative_cb ARM64_ALWAYS_SYSTEM, kvm_compute_final_ctr_el0
  251. movz \reg, #0
  252. movk \reg, #0, lsl #16
  253. movk \reg, #0, lsl #32
  254. movk \reg, #0, lsl #48
  255. alternative_cb_end
  256. #endif
  257. .endm
  258. /*
  259. * raw_dcache_line_size - get the minimum D-cache line size on this CPU
  260. * from the CTR register.
  261. */
  262. .macro raw_dcache_line_size, reg, tmp
  263. mrs \tmp, ctr_el0 // read CTR
  264. ubfm \tmp, \tmp, #16, #19 // cache line size encoding
  265. mov \reg, #4 // bytes per word
  266. lsl \reg, \reg, \tmp // actual cache line size
  267. .endm
  268. /*
  269. * dcache_line_size - get the safe D-cache line size across all CPUs
  270. */
  271. .macro dcache_line_size, reg, tmp
  272. read_ctr \tmp
  273. ubfm \tmp, \tmp, #16, #19 // cache line size encoding
  274. mov \reg, #4 // bytes per word
  275. lsl \reg, \reg, \tmp // actual cache line size
  276. .endm
  277. /*
  278. * raw_icache_line_size - get the minimum I-cache line size on this CPU
  279. * from the CTR register.
  280. */
  281. .macro raw_icache_line_size, reg, tmp
  282. mrs \tmp, ctr_el0 // read CTR
  283. and \tmp, \tmp, #0xf // cache line size encoding
  284. mov \reg, #4 // bytes per word
  285. lsl \reg, \reg, \tmp // actual cache line size
  286. .endm
  287. /*
  288. * icache_line_size - get the safe I-cache line size across all CPUs
  289. */
  290. .macro icache_line_size, reg, tmp
  291. read_ctr \tmp
  292. and \tmp, \tmp, #0xf // cache line size encoding
  293. mov \reg, #4 // bytes per word
  294. lsl \reg, \reg, \tmp // actual cache line size
  295. .endm
  296. /*
  297. * tcr_set_t0sz - update TCR.T0SZ so that we can load the ID map
  298. */
  299. .macro tcr_set_t0sz, valreg, t0sz
  300. bfi \valreg, \t0sz, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
  301. .endm
  302. /*
  303. * tcr_set_t1sz - update TCR.T1SZ
  304. */
  305. .macro tcr_set_t1sz, valreg, t1sz
  306. bfi \valreg, \t1sz, #TCR_T1SZ_OFFSET, #TCR_TxSZ_WIDTH
  307. .endm
  308. /*
  309. * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
  310. * ID_AA64MMFR0_EL1.PARange value
  311. *
  312. * tcr: register with the TCR_ELx value to be updated
  313. * pos: IPS or PS bitfield position
  314. * tmp{0,1}: temporary registers
  315. */
  316. .macro tcr_compute_pa_size, tcr, pos, tmp0, tmp1
  317. mrs \tmp0, ID_AA64MMFR0_EL1
  318. // Narrow PARange to fit the PS field in TCR_ELx
  319. ubfx \tmp0, \tmp0, #ID_AA64MMFR0_EL1_PARANGE_SHIFT, #3
  320. mov \tmp1, #ID_AA64MMFR0_EL1_PARANGE_MAX
  321. #ifdef CONFIG_ARM64_LPA2
  322. alternative_if_not ARM64_HAS_VA52
  323. mov \tmp1, #ID_AA64MMFR0_EL1_PARANGE_48
  324. alternative_else_nop_endif
  325. #endif
  326. cmp \tmp0, \tmp1
  327. csel \tmp0, \tmp1, \tmp0, hi
  328. bfi \tcr, \tmp0, \pos, #3
  329. .endm
  330. .macro __dcache_op_workaround_clean_cache, op, addr
  331. alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
  332. dc \op, \addr
  333. alternative_else
  334. dc civac, \addr
  335. alternative_endif
  336. .endm
  337. /*
  338. * Macro to perform a data cache maintenance for the interval
  339. * [start, end) with dcache line size explicitly provided.
  340. *
  341. * op: operation passed to dc instruction
  342. * domain: domain used in dsb instruciton
  343. * start: starting virtual address of the region
  344. * end: end virtual address of the region
  345. * linesz: dcache line size
  346. * fixup: optional label to branch to on user fault
  347. * Corrupts: start, end, tmp
  348. */
  349. .macro dcache_by_myline_op op, domain, start, end, linesz, tmp, fixup
  350. sub \tmp, \linesz, #1
  351. bic \start, \start, \tmp
  352. .Ldcache_op\@:
  353. .ifc \op, cvau
  354. __dcache_op_workaround_clean_cache \op, \start
  355. .else
  356. .ifc \op, cvac
  357. __dcache_op_workaround_clean_cache \op, \start
  358. .else
  359. .ifc \op, cvap
  360. sys 3, c7, c12, 1, \start // dc cvap
  361. .else
  362. .ifc \op, cvadp
  363. sys 3, c7, c13, 1, \start // dc cvadp
  364. .else
  365. dc \op, \start
  366. .endif
  367. .endif
  368. .endif
  369. .endif
  370. add \start, \start, \linesz
  371. cmp \start, \end
  372. b.lo .Ldcache_op\@
  373. dsb \domain
  374. _cond_uaccess_extable .Ldcache_op\@, \fixup
  375. .endm
  376. /*
  377. * Macro to perform a data cache maintenance for the interval
  378. * [start, end)
  379. *
  380. * op: operation passed to dc instruction
  381. * domain: domain used in dsb instruciton
  382. * start: starting virtual address of the region
  383. * end: end virtual address of the region
  384. * fixup: optional label to branch to on user fault
  385. * Corrupts: start, end, tmp1, tmp2
  386. */
  387. .macro dcache_by_line_op op, domain, start, end, tmp1, tmp2, fixup
  388. dcache_line_size \tmp1, \tmp2
  389. dcache_by_myline_op \op, \domain, \start, \end, \tmp1, \tmp2, \fixup
  390. .endm
  391. /*
  392. * Macro to perform an instruction cache maintenance for the interval
  393. * [start, end)
  394. *
  395. * start, end: virtual addresses describing the region
  396. * fixup: optional label to branch to on user fault
  397. * Corrupts: tmp1, tmp2
  398. */
  399. .macro invalidate_icache_by_line start, end, tmp1, tmp2, fixup
  400. icache_line_size \tmp1, \tmp2
  401. sub \tmp2, \tmp1, #1
  402. bic \tmp2, \start, \tmp2
  403. .Licache_op\@:
  404. ic ivau, \tmp2 // invalidate I line PoU
  405. add \tmp2, \tmp2, \tmp1
  406. cmp \tmp2, \end
  407. b.lo .Licache_op\@
  408. dsb ish
  409. isb
  410. _cond_uaccess_extable .Licache_op\@, \fixup
  411. .endm
  412. /*
  413. * load_ttbr1 - install @pgtbl as a TTBR1 page table
  414. * pgtbl preserved
  415. * tmp1/tmp2 clobbered, either may overlap with pgtbl
  416. */
  417. .macro load_ttbr1, pgtbl, tmp1, tmp2
  418. phys_to_ttbr \tmp1, \pgtbl
  419. offset_ttbr1 \tmp1, \tmp2
  420. msr ttbr1_el1, \tmp1
  421. isb
  422. .endm
  423. /*
  424. * To prevent the possibility of old and new partial table walks being visible
  425. * in the tlb, switch the ttbr to a zero page when we invalidate the old
  426. * records. D4.7.1 'General TLB maintenance requirements' in ARM DDI 0487A.i
  427. * Even switching to our copied tables will cause a changed output address at
  428. * each stage of the walk.
  429. */
  430. .macro break_before_make_ttbr_switch zero_page, page_table, tmp, tmp2
  431. phys_to_ttbr \tmp, \zero_page
  432. msr ttbr1_el1, \tmp
  433. isb
  434. tlbi vmalle1
  435. dsb nsh
  436. load_ttbr1 \page_table, \tmp, \tmp2
  437. .endm
  438. /*
  439. * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
  440. */
  441. .macro reset_pmuserenr_el0, tmpreg
  442. mrs \tmpreg, id_aa64dfr0_el1
  443. ubfx \tmpreg, \tmpreg, #ID_AA64DFR0_EL1_PMUVer_SHIFT, #4
  444. cmp \tmpreg, #ID_AA64DFR0_EL1_PMUVer_NI
  445. ccmp \tmpreg, #ID_AA64DFR0_EL1_PMUVer_IMP_DEF, #4, ne
  446. b.eq 9000f // Skip if no PMU present or IMP_DEF
  447. msr pmuserenr_el0, xzr // Disable PMU access from EL0
  448. 9000:
  449. .endm
  450. /*
  451. * reset_amuserenr_el0 - reset AMUSERENR_EL0 if AMUv1 present
  452. */
  453. .macro reset_amuserenr_el0, tmpreg
  454. mrs \tmpreg, id_aa64pfr0_el1 // Check ID_AA64PFR0_EL1
  455. ubfx \tmpreg, \tmpreg, #ID_AA64PFR0_EL1_AMU_SHIFT, #4
  456. cbz \tmpreg, .Lskip_\@ // Skip if no AMU present
  457. msr_s SYS_AMUSERENR_EL0, xzr // Disable AMU access from EL0
  458. .Lskip_\@:
  459. .endm
  460. /*
  461. * copy_page - copy src to dest using temp registers t1-t8
  462. */
  463. .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
  464. 9998: ldp \t1, \t2, [\src]
  465. ldp \t3, \t4, [\src, #16]
  466. ldp \t5, \t6, [\src, #32]
  467. ldp \t7, \t8, [\src, #48]
  468. add \src, \src, #64
  469. stnp \t1, \t2, [\dest]
  470. stnp \t3, \t4, [\dest, #16]
  471. stnp \t5, \t6, [\dest, #32]
  472. stnp \t7, \t8, [\dest, #48]
  473. add \dest, \dest, #64
  474. tst \src, #(PAGE_SIZE - 1)
  475. b.ne 9998b
  476. .endm
  477. /*
  478. * Annotate a function as being unsuitable for kprobes.
  479. */
  480. #ifdef CONFIG_KPROBES
  481. #define NOKPROBE(x) \
  482. .pushsection "_kprobe_blacklist", "aw"; \
  483. .quad x; \
  484. .popsection;
  485. #else
  486. #define NOKPROBE(x)
  487. #endif
  488. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  489. #define EXPORT_SYMBOL_NOKASAN(name)
  490. #else
  491. #define EXPORT_SYMBOL_NOKASAN(name) EXPORT_SYMBOL(name)
  492. #endif
  493. /*
  494. * Emit a 64-bit absolute little endian symbol reference in a way that
  495. * ensures that it will be resolved at build time, even when building a
  496. * PIE binary. This requires cooperation from the linker script, which
  497. * must emit the lo32/hi32 halves individually.
  498. */
  499. .macro le64sym, sym
  500. .long \sym\()_lo32
  501. .long \sym\()_hi32
  502. .endm
  503. /*
  504. * mov_q - move an immediate constant into a 64-bit register using
  505. * between 2 and 4 movz/movk instructions (depending on the
  506. * magnitude and sign of the operand)
  507. */
  508. .macro mov_q, reg, val
  509. .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
  510. movz \reg, :abs_g1_s:\val
  511. .else
  512. .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
  513. movz \reg, :abs_g2_s:\val
  514. .else
  515. movz \reg, :abs_g3:\val
  516. movk \reg, :abs_g2_nc:\val
  517. .endif
  518. movk \reg, :abs_g1_nc:\val
  519. .endif
  520. movk \reg, :abs_g0_nc:\val
  521. .endm
  522. /*
  523. * Return the current task_struct.
  524. */
  525. .macro get_current_task, rd
  526. mrs \rd, sp_el0
  527. .endm
  528. /*
  529. * If the kernel is built for 52-bit virtual addressing but the hardware only
  530. * supports 48 bits, we cannot program the pgdir address into TTBR1 directly,
  531. * but we have to add an offset so that the TTBR1 address corresponds with the
  532. * pgdir entry that covers the lowest 48-bit addressable VA.
  533. *
  534. * Note that this trick is only used for LVA/64k pages - LPA2/4k pages uses an
  535. * additional paging level, and on LPA2/16k pages, we would end up with a root
  536. * level table with only 2 entries, which is suboptimal in terms of TLB
  537. * utilization, so there we fall back to 47 bits of translation if LPA2 is not
  538. * supported.
  539. *
  540. * orr is used as it can cover the immediate value (and is idempotent).
  541. * ttbr: Value of ttbr to set, modified.
  542. */
  543. .macro offset_ttbr1, ttbr, tmp
  544. #if defined(CONFIG_ARM64_VA_BITS_52) && !defined(CONFIG_ARM64_LPA2)
  545. mrs \tmp, tcr_el1
  546. and \tmp, \tmp, #TCR_T1SZ_MASK
  547. cmp \tmp, #TCR_T1SZ(VA_BITS_MIN)
  548. orr \tmp, \ttbr, #TTBR1_BADDR_4852_OFFSET
  549. csel \ttbr, \tmp, \ttbr, eq
  550. #endif
  551. .endm
  552. /*
  553. * Arrange a physical address in a TTBR register, taking care of 52-bit
  554. * addresses.
  555. *
  556. * phys: physical address, preserved
  557. * ttbr: returns the TTBR value
  558. */
  559. .macro phys_to_ttbr, ttbr, phys
  560. #ifdef CONFIG_ARM64_PA_BITS_52
  561. orr \ttbr, \phys, \phys, lsr #46
  562. and \ttbr, \ttbr, #TTBR_BADDR_MASK_52
  563. #else
  564. mov \ttbr, \phys
  565. #endif
  566. .endm
  567. .macro phys_to_pte, pte, phys
  568. #ifdef CONFIG_ARM64_PA_BITS_52
  569. orr \pte, \phys, \phys, lsr #PTE_ADDR_HIGH_SHIFT
  570. and \pte, \pte, #PHYS_TO_PTE_ADDR_MASK
  571. #else
  572. mov \pte, \phys
  573. #endif
  574. .endm
  575. /*
  576. * tcr_clear_errata_bits - Clear TCR bits that trigger an errata on this CPU.
  577. */
  578. .macro tcr_clear_errata_bits, tcr, tmp1, tmp2
  579. #ifdef CONFIG_FUJITSU_ERRATUM_010001
  580. mrs \tmp1, midr_el1
  581. mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001_MASK
  582. and \tmp1, \tmp1, \tmp2
  583. mov_q \tmp2, MIDR_FUJITSU_ERRATUM_010001
  584. cmp \tmp1, \tmp2
  585. b.ne 10f
  586. mov_q \tmp2, TCR_CLEAR_FUJITSU_ERRATUM_010001
  587. bic \tcr, \tcr, \tmp2
  588. 10:
  589. #endif /* CONFIG_FUJITSU_ERRATUM_010001 */
  590. .endm
  591. /**
  592. * Errata workaround prior to disable MMU. Insert an ISB immediately prior
  593. * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
  594. */
  595. .macro pre_disable_mmu_workaround
  596. #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
  597. isb
  598. #endif
  599. .endm
  600. /*
  601. * frame_push - Push @regcount callee saved registers to the stack,
  602. * starting at x19, as well as x29/x30, and set x29 to
  603. * the new value of sp. Add @extra bytes of stack space
  604. * for locals.
  605. */
  606. .macro frame_push, regcount:req, extra
  607. __frame st, \regcount, \extra
  608. .endm
  609. /*
  610. * frame_pop - Pop the callee saved registers from the stack that were
  611. * pushed in the most recent call to frame_push, as well
  612. * as x29/x30 and any extra stack space that may have been
  613. * allocated.
  614. */
  615. .macro frame_pop
  616. __frame ld
  617. .endm
  618. .macro __frame_regs, reg1, reg2, op, num
  619. .if .Lframe_regcount == \num
  620. \op\()r \reg1, [sp, #(\num + 1) * 8]
  621. .elseif .Lframe_regcount > \num
  622. \op\()p \reg1, \reg2, [sp, #(\num + 1) * 8]
  623. .endif
  624. .endm
  625. .macro __frame, op, regcount, extra=0
  626. .ifc \op, st
  627. .if (\regcount) < 0 || (\regcount) > 10
  628. .error "regcount should be in the range [0 ... 10]"
  629. .endif
  630. .if ((\extra) % 16) != 0
  631. .error "extra should be a multiple of 16 bytes"
  632. .endif
  633. .ifdef .Lframe_regcount
  634. .if .Lframe_regcount != -1
  635. .error "frame_push/frame_pop may not be nested"
  636. .endif
  637. .endif
  638. .set .Lframe_regcount, \regcount
  639. .set .Lframe_extra, \extra
  640. .set .Lframe_local_offset, ((\regcount + 3) / 2) * 16
  641. stp x29, x30, [sp, #-.Lframe_local_offset - .Lframe_extra]!
  642. mov x29, sp
  643. .endif
  644. __frame_regs x19, x20, \op, 1
  645. __frame_regs x21, x22, \op, 3
  646. __frame_regs x23, x24, \op, 5
  647. __frame_regs x25, x26, \op, 7
  648. __frame_regs x27, x28, \op, 9
  649. .ifc \op, ld
  650. .if .Lframe_regcount == -1
  651. .error "frame_push/frame_pop may not be nested"
  652. .endif
  653. ldp x29, x30, [sp], #.Lframe_local_offset + .Lframe_extra
  654. .set .Lframe_regcount, -1
  655. .endif
  656. .endm
  657. /*
  658. * Set SCTLR_ELx to the @reg value, and invalidate the local icache
  659. * in the process. This is called when setting the MMU on.
  660. */
  661. .macro set_sctlr, sreg, reg
  662. msr \sreg, \reg
  663. isb
  664. /*
  665. * Invalidate the local I-cache so that any instructions fetched
  666. * speculatively from the PoC are discarded, since they may have
  667. * been dynamically patched at the PoU.
  668. */
  669. ic iallu
  670. dsb nsh
  671. isb
  672. .endm
  673. .macro set_sctlr_el1, reg
  674. set_sctlr sctlr_el1, \reg
  675. .endm
  676. .macro set_sctlr_el2, reg
  677. set_sctlr sctlr_el2, \reg
  678. .endm
  679. /*
  680. * Check whether asm code should yield as soon as it is able. This is
  681. * the case if we are currently running in task context, and the
  682. * TIF_NEED_RESCHED flag is set. (Note that the TIF_NEED_RESCHED flag
  683. * is stored negated in the top word of the thread_info::preempt_count
  684. * field)
  685. */
  686. .macro cond_yield, lbl:req, tmp:req, tmp2
  687. #ifdef CONFIG_PREEMPT_VOLUNTARY
  688. get_current_task \tmp
  689. ldr \tmp, [\tmp, #TSK_TI_PREEMPT]
  690. /*
  691. * If we are serving a softirq, there is no point in yielding: the
  692. * softirq will not be preempted no matter what we do, so we should
  693. * run to completion as quickly as we can. The preempt_count field will
  694. * have BIT(SOFTIRQ_SHIFT) set in this case, so the zero check will
  695. * catch this case too.
  696. */
  697. cbz \tmp, \lbl
  698. #endif
  699. .endm
  700. /*
  701. * Branch Target Identifier (BTI)
  702. */
  703. .macro bti, targets
  704. .equ .L__bti_targets_c, 34
  705. .equ .L__bti_targets_j, 36
  706. .equ .L__bti_targets_jc,38
  707. hint #.L__bti_targets_\targets
  708. .endm
  709. /*
  710. * This macro emits a program property note section identifying
  711. * architecture features which require special handling, mainly for
  712. * use in assembly files included in the VDSO.
  713. */
  714. #define NT_GNU_PROPERTY_TYPE_0 5
  715. #define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
  716. #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0)
  717. #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1U << 1)
  718. #ifdef CONFIG_ARM64_BTI_KERNEL
  719. #define GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT \
  720. ((GNU_PROPERTY_AARCH64_FEATURE_1_BTI | \
  721. GNU_PROPERTY_AARCH64_FEATURE_1_PAC))
  722. #endif
  723. #ifdef GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
  724. .macro emit_aarch64_feature_1_and, feat=GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
  725. .pushsection .note.gnu.property, "a"
  726. .align 3
  727. .long 2f - 1f
  728. .long 6f - 3f
  729. .long NT_GNU_PROPERTY_TYPE_0
  730. 1: .string "GNU"
  731. 2:
  732. .align 3
  733. 3: .long GNU_PROPERTY_AARCH64_FEATURE_1_AND
  734. .long 5f - 4f
  735. 4:
  736. /*
  737. * This is described with an array of char in the Linux API
  738. * spec but the text and all other usage (including binutils,
  739. * clang and GCC) treat this as a 32 bit value so no swizzling
  740. * is required for big endian.
  741. */
  742. .long \feat
  743. 5:
  744. .align 3
  745. 6:
  746. .popsection
  747. .endm
  748. #else
  749. .macro emit_aarch64_feature_1_and, feat=0
  750. .endm
  751. #endif /* GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT */
  752. .macro __mitigate_spectre_bhb_loop tmp
  753. #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
  754. alternative_cb ARM64_ALWAYS_SYSTEM, spectre_bhb_patch_loop_iter
  755. mov \tmp, #32 // Patched to correct the immediate
  756. alternative_cb_end
  757. .Lspectre_bhb_loop\@:
  758. b . + 4
  759. subs \tmp, \tmp, #1
  760. b.ne .Lspectre_bhb_loop\@
  761. sb
  762. #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
  763. .endm
  764. .macro mitigate_spectre_bhb_loop tmp
  765. #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
  766. alternative_cb ARM64_ALWAYS_SYSTEM, spectre_bhb_patch_loop_mitigation_enable
  767. b .L_spectre_bhb_loop_done\@ // Patched to NOP
  768. alternative_cb_end
  769. __mitigate_spectre_bhb_loop \tmp
  770. .L_spectre_bhb_loop_done\@:
  771. #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
  772. .endm
  773. /* Save/restores x0-x3 to the stack */
  774. .macro __mitigate_spectre_bhb_fw
  775. #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
  776. stp x0, x1, [sp, #-16]!
  777. stp x2, x3, [sp, #-16]!
  778. mov w0, #ARM_SMCCC_ARCH_WORKAROUND_3
  779. alternative_cb ARM64_ALWAYS_SYSTEM, smccc_patch_fw_mitigation_conduit
  780. nop // Patched to SMC/HVC #0
  781. alternative_cb_end
  782. ldp x2, x3, [sp], #16
  783. ldp x0, x1, [sp], #16
  784. #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
  785. .endm
  786. .macro mitigate_spectre_bhb_clear_insn
  787. #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
  788. alternative_cb ARM64_ALWAYS_SYSTEM, spectre_bhb_patch_clearbhb
  789. /* Patched to NOP when not supported */
  790. clearbhb
  791. isb
  792. alternative_cb_end
  793. #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
  794. .endm
  795. #endif /* __ASM_ASSEMBLER_H */