assembler.h 11 KB

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