assembler.h 21 KB

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