2 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
4 * Copyright (C) 1996-2000 Russell King
5 * Copyright (C) 2012 ARM Ltd.
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.
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.
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/>.
20 #error "Only include this from assembly code"
23 #ifndef __ASM_ASSEMBLER_H
24 #define __ASM_ASSEMBLER_H
26 #include <asm/asm-offsets.h>
27 #include <asm/cpufeature.h>
29 #include <asm/pgtable-hwdef.h>
30 #include <asm/ptrace.h>
31 #include <asm/thread_info.h>
34 * Enable and disable interrupts.
44 .macro save_and_disable_irq
, flags
49 .macro restore_irq
, flags
54 * Enable and disable debug exceptions.
64 .macro disable_step_tsk
, flgs
, tmp
65 tbz
\flgs
, #TIF_SINGLESTEP, 9990f
69 isb
// Synchronise with enable_dbg
73 .macro enable_step_tsk
, flgs
, tmp
74 tbz
\flgs
, #TIF_SINGLESTEP, 9990f
83 * Enable both debug exceptions and interrupts. This is likely to be
84 * faster than two daifclr operations, since writes to this register
85 * are self-synchronising.
87 .macro enable_dbg_and_irq
92 * SMP data memory barrier
99 * Value prediction barrier
106 * Sanitise a 64-bit bounded index wrt speculation, returning zero if out
109 .macro mask_nospec64
, idx
, limit
, tmp
110 sub
\tmp
, \idx
, \limit
112 and \idx
, \idx
, \tmp
, asr
#63
126 * Emit an entry into the exception table
128 .macro _asm_extable
, from
, to
129 .pushsection __ex_table
, "a"
131 .long (\from
- .), (\to
- .)
135 #define USER(l, x...) \
137 _asm_extable 9999b, l
142 lr
.req x30
// link register
153 * Select code when configured for BE.
155 #ifdef CONFIG_CPU_BIG_ENDIAN
156 #define CPU_BE(code...) code
158 #define CPU_BE(code...)
162 * Select code when configured for LE.
164 #ifdef CONFIG_CPU_BIG_ENDIAN
165 #define CPU_LE(code...)
167 #define CPU_LE(code...) code
171 * Define a macro that constructs a 64-bit value by concatenating two
172 * 32-bit registers. Note that on big endian systems the order of the
173 * registers is swapped.
175 #ifndef CONFIG_CPU_BIG_ENDIAN
176 .macro regs_to_64
, rd
, lbits
, hbits
178 .macro regs_to_64
, rd
, hbits
, lbits
180 orr
\rd
, \lbits
, \hbits
, lsl
#32
184 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
185 * <symbol> is within the range +/- 4 GB of the PC when running
186 * in core kernel context. In module context, a movz/movk sequence
187 * is used, since modules may be loaded far away from the kernel
188 * when KASLR is in effect.
191 * @dst: destination register (64 bit wide)
192 * @sym: name of the symbol
194 .macro adr_l
, dst
, sym
197 add \dst
, \dst
, :lo12
:\sym
199 movz \dst
, #:abs_g3:\sym
200 movk \dst
, #:abs_g2_nc:\sym
201 movk \dst
, #:abs_g1_nc:\sym
202 movk \dst
, #:abs_g0_nc:\sym
207 * @dst: destination register (32 or 64 bit wide)
208 * @sym: name of the symbol
209 * @tmp: optional 64-bit scratch register to be used if <dst> is a
210 * 32-bit wide register, in which case it cannot be used to hold
213 .macro ldr_l
, dst
, sym
, tmp
=
217 ldr \dst
, [\dst
, :lo12
:\sym
]
220 ldr \dst
, [\tmp
, :lo12
:\sym
]
234 * @src: source register (32 or 64 bit wide)
235 * @sym: name of the symbol
236 * @tmp: mandatory 64-bit scratch register to calculate the address
237 * while <src> needs to be preserved.
239 .macro str_l
, src
, sym
, tmp
242 str \src
, [\tmp
, :lo12
:\sym
]
250 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
252 * @sym: The name of the per-cpu variable
253 * @tmp: scratch register
255 .macro adr_this_cpu
, dst
, sym
, tmp
258 add \dst
, \tmp
, #:lo12:\sym
267 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
268 * @sym: The name of the per-cpu variable
269 * @tmp: scratch register
271 .macro ldr_this_cpu dst
, sym
, tmp
274 ldr \dst
, [\dst
, \tmp
]
278 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
280 .macro vma_vm_mm
, rd
, rn
281 ldr
\rd
, [\rn
, #VMA_VM_MM]
285 * mmid - get context id from mm pointer (mm->context.id)
288 ldr
\rd
, [\rn
, #MM_CONTEXT_ID]
291 * read_ctr - read CTR_EL0. If the system has mismatched
292 * cache line sizes, provide the system wide safe value
293 * from arm64_ftr_reg_ctrel0.sys_val
296 alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
297 mrs
\reg
, ctr_el0
// read CTR
300 ldr_l
\reg
, arm64_ftr_reg_ctrel0
+ ARM64_FTR_SYSVAL
306 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
307 * from the CTR register.
309 .macro raw_dcache_line_size
, reg
, tmp
310 mrs
\tmp
, ctr_el0
// read CTR
311 ubfm
\tmp
, \tmp
, #16, #19 // cache line size encoding
312 mov
\reg
, #4 // bytes per word
313 lsl
\reg
, \reg
, \tmp
// actual cache line size
317 * dcache_line_size - get the safe D-cache line size across all CPUs
319 .macro dcache_line_size
, reg
, tmp
321 ubfm
\tmp
, \tmp
, #16, #19 // cache line size encoding
322 mov
\reg
, #4 // bytes per word
323 lsl
\reg
, \reg
, \tmp
// actual cache line size
327 * raw_icache_line_size - get the minimum I-cache line size on this CPU
328 * from the CTR register.
330 .macro raw_icache_line_size
, reg
, tmp
331 mrs
\tmp
, ctr_el0
// read CTR
332 and \tmp
, \tmp
, #0xf // cache line size encoding
333 mov
\reg
, #4 // bytes per word
334 lsl
\reg
, \reg
, \tmp
// actual cache line size
338 * icache_line_size - get the safe I-cache line size across all CPUs
340 .macro icache_line_size
, reg
, tmp
342 and \tmp
, \tmp
, #0xf // cache line size encoding
343 mov
\reg
, #4 // bytes per word
344 lsl
\reg
, \reg
, \tmp
// actual cache line size
348 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
350 .macro tcr_set_idmap_t0sz
, valreg
, tmpreg
351 #ifndef CONFIG_ARM64_VA_BITS_48
352 ldr_l
\tmpreg
, idmap_t0sz
353 bfi
\valreg
, \tmpreg
, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
358 * Macro to perform a data cache maintenance for the interval
359 * [kaddr, kaddr + size)
361 * op: operation passed to dc instruction
362 * domain: domain used in dsb instruciton
363 * kaddr: starting virtual address of the region
364 * size: size of the region
365 * Corrupts: kaddr, size, tmp1, tmp2
367 .macro dcache_by_line_op op
, domain
, kaddr
, size
, tmp1
, tmp2
368 dcache_line_size
\tmp
1, \tmp
2
369 add \size
, \kaddr
, \size
371 bic \kaddr
, \kaddr
, \tmp
2
373 .if (\op
== cvau
|| \op
== cvac
)
374 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
379 .elseif (\op
== cvap
)
380 alternative_if ARM64_HAS_DCPOP
381 sys
3, c7
, c12
, 1, \kaddr
// dc cvap
388 add \kaddr
, \kaddr
, \tmp
1
395 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
397 .macro reset_pmuserenr_el0
, tmpreg
398 mrs
\tmpreg
, id_aa64dfr0_el1
// Check ID_AA64DFR0_EL1 PMUVer
399 sbfx
\tmpreg
, \tmpreg
, #8, #4
400 cmp
\tmpreg
, #1 // Skip if no PMU present
402 msr pmuserenr_el0
, xzr
// Disable PMU access from EL0
407 * copy_page - copy src to dest using temp registers t1-t8
409 .macro copy_page dest
:req src
:req t1
:req t2
:req t3
:req t4
:req t5
:req t6
:req t7
:req t8
:req
410 9998: ldp
\t1, \t2, [\src
]
411 ldp
\t3, \t4, [\src
, #16]
412 ldp
\t5, \t6, [\src
, #32]
413 ldp
\t7, \t8, [\src
, #48]
415 stnp
\t1, \t2, [\dest
]
416 stnp
\t3, \t4, [\dest
, #16]
417 stnp
\t5, \t6, [\dest
, #32]
418 stnp
\t7, \t8, [\dest
, #48]
419 add \dest
, \dest
, #64
420 tst \src
, #(PAGE_SIZE - 1)
425 * Annotate a function as position independent, i.e., safe to be called before
426 * the kernel virtual mapping is activated.
428 #define ENDPIPROC(x) \
430 .type __pi_##x, %function; \
432 .size __pi_##x, . - x; \
436 * Annotate a function as being unsuitable for kprobes.
438 #ifdef CONFIG_KPROBES
439 #define NOKPROBE(x) \
440 .pushsection "_kprobe_blacklist", "aw"; \
447 * Emit a 64-bit absolute little endian symbol reference in a way that
448 * ensures that it will be resolved at build time, even when building a
449 * PIE binary. This requires cooperation from the linker script, which
450 * must emit the lo32/hi32 halves individually.
458 * mov_q - move an immediate constant into a 64-bit register using
459 * between 2 and 4 movz/movk instructions (depending on the
460 * magnitude and sign of the operand)
462 .macro mov_q
, reg
, val
463 .if (((\val
) >> 31) == 0 || ((\val
) >> 31) == 0x1ffffffff)
464 movz
\reg
, :abs_g1_s
:\val
466 .if (((\val
) >> 47) == 0 || ((\val
) >> 47) == 0x1ffff)
467 movz
\reg
, :abs_g2_s
:\val
469 movz
\reg
, :abs_g3
:\val
470 movk
\reg
, :abs_g2_nc
:\val
472 movk
\reg
, :abs_g1_nc
:\val
474 movk
\reg
, :abs_g0_nc
:\val
478 * Return the current thread_info.
480 .macro get_thread_info
, rd
485 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
486 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
488 .macro pre_disable_mmu_workaround
489 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
494 .macro pte_to_phys
, phys
, pte
495 and \phys
, \pte
, #(((1 << (48 - PAGE_SHIFT)) - 1) << PAGE_SHIFT)
498 #endif /* __ASM_ASSEMBLER_H */