assembler.h 23 KB

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