blob: f09a5ae48a44a007f6e1f4af713fab836fb7c6f5 [file] [log] [blame]
Catalin Marinas0be73202012-03-05 11:49:26 +00001/*
Geoff Levand7b7293a2016-04-27 17:47:00 +01002 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
Catalin Marinas0be73202012-03-05 11:49:26 +00003 *
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
Marc Zyngierf3e39272015-02-20 13:53:13 +000023#ifndef __ASM_ASSEMBLER_H
24#define __ASM_ASSEMBLER_H
25
Geoff Levand7b7293a2016-04-27 17:47:00 +010026#include <asm/asm-offsets.h>
Andre Przywara823066d2016-06-28 18:07:29 +010027#include <asm/cpufeature.h>
Geoff Levand5003dbd2016-04-27 17:47:10 +010028#include <asm/page.h>
Geoff Levand7b7293a2016-04-27 17:47:00 +010029#include <asm/pgtable-hwdef.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000030#include <asm/ptrace.h>
Will Deacon2a283072014-04-29 19:04:06 +010031#include <asm/thread_info.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000032
33/*
Catalin Marinas0be73202012-03-05 11:49:26 +000034 * Enable and disable interrupts.
35 */
36 .macro disable_irq
37 msr daifset, #2
38 .endm
39
40 .macro enable_irq
41 msr daifclr, #2
42 .endm
43
44/*
Catalin Marinas0be73202012-03-05 11:49:26 +000045 * Enable and disable debug exceptions.
46 */
47 .macro disable_dbg
48 msr daifset, #8
49 .endm
50
51 .macro enable_dbg
52 msr daifclr, #8
53 .endm
54
Will Deacon2a283072014-04-29 19:04:06 +010055 .macro disable_step_tsk, flgs, tmp
56 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000057 mrs \tmp, mdscr_el1
58 bic \tmp, \tmp, #1
59 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010060 isb // Synchronise with enable_dbg
619990:
Catalin Marinas0be73202012-03-05 11:49:26 +000062 .endm
63
Will Deacon2a283072014-04-29 19:04:06 +010064 .macro enable_step_tsk, flgs, tmp
65 tbz \flgs, #TIF_SINGLESTEP, 9990f
66 disable_dbg
Catalin Marinas0be73202012-03-05 11:49:26 +000067 mrs \tmp, mdscr_el1
68 orr \tmp, \tmp, #1
69 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100709990:
Catalin Marinas0be73202012-03-05 11:49:26 +000071 .endm
72
Will Deacon2a283072014-04-29 19:04:06 +010073/*
74 * Enable both debug exceptions and interrupts. This is likely to be
75 * faster than two daifclr operations, since writes to this register
76 * are self-synchronising.
77 */
78 .macro enable_dbg_and_irq
79 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +000080 .endm
81
82/*
83 * SMP data memory barrier
84 */
85 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +000086 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +000087 .endm
88
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010089/*
90 * Emit an entry into the exception table
91 */
92 .macro _asm_extable, from, to
93 .pushsection __ex_table, "a"
94 .align 3
95 .long (\from - .), (\to - .)
96 .popsection
97 .endm
98
Catalin Marinas0be73202012-03-05 11:49:26 +000099#define USER(l, x...) \
1009999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100101 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000102
103/*
104 * Register aliases.
105 */
106lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100107
108/*
109 * Vector entry
110 */
111 .macro ventry label
112 .align 7
113 b \label
114 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100115
116/*
117 * Select code when configured for BE.
118 */
119#ifdef CONFIG_CPU_BIG_ENDIAN
120#define CPU_BE(code...) code
121#else
122#define CPU_BE(code...)
123#endif
124
125/*
126 * Select code when configured for LE.
127 */
128#ifdef CONFIG_CPU_BIG_ENDIAN
129#define CPU_LE(code...)
130#else
131#define CPU_LE(code...) code
132#endif
133
Matthew Leach55b89542013-10-11 14:52:13 +0100134/*
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
Marc Zyngierf3e39272015-02-20 13:53:13 +0000146
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100147/*
148 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
149 * <symbol> is within the range +/- 4 GB of the PC.
150 */
151 /*
152 * @dst: destination register (64 bit wide)
153 * @sym: name of the symbol
154 * @tmp: optional scratch register to be used if <dst> == sp, which
155 * is not allowed in an adrp instruction
156 */
157 .macro adr_l, dst, sym, tmp=
158 .ifb \tmp
159 adrp \dst, \sym
160 add \dst, \dst, :lo12:\sym
161 .else
162 adrp \tmp, \sym
163 add \dst, \tmp, :lo12:\sym
164 .endif
165 .endm
166
167 /*
168 * @dst: destination register (32 or 64 bit wide)
169 * @sym: name of the symbol
170 * @tmp: optional 64-bit scratch register to be used if <dst> is a
171 * 32-bit wide register, in which case it cannot be used to hold
172 * the address
173 */
174 .macro ldr_l, dst, sym, tmp=
175 .ifb \tmp
176 adrp \dst, \sym
177 ldr \dst, [\dst, :lo12:\sym]
178 .else
179 adrp \tmp, \sym
180 ldr \dst, [\tmp, :lo12:\sym]
181 .endif
182 .endm
183
184 /*
185 * @src: source register (32 or 64 bit wide)
186 * @sym: name of the symbol
187 * @tmp: mandatory 64-bit scratch register to calculate the address
188 * while <src> needs to be preserved.
189 */
190 .macro str_l, src, sym, tmp
191 adrp \tmp, \sym
192 str \src, [\tmp, :lo12:\sym]
193 .endm
194
James Morseaa4d5d32015-12-10 10:22:39 +0000195 /*
196 * @sym: The name of the per-cpu variable
197 * @reg: Result of per_cpu(sym, smp_processor_id())
198 * @tmp: scratch register
199 */
200 .macro this_cpu_ptr, sym, reg, tmp
201 adr_l \reg, \sym
202 mrs \tmp, tpidr_el1
203 add \reg, \reg, \tmp
204 .endm
205
Ard Biesheuvel20791842015-10-08 20:02:03 +0100206/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100207 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
208 */
209 .macro vma_vm_mm, rd, rn
210 ldr \rd, [\rn, #VMA_VM_MM]
211 .endm
212
213/*
214 * mmid - get context id from mm pointer (mm->context.id)
215 */
216 .macro mmid, rd, rn
217 ldr \rd, [\rn, #MM_CONTEXT_ID]
218 .endm
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100219/*
220 * read_ctr - read CTR_EL0. If the system has mismatched
221 * cache line sizes, provide the system wide safe value
222 * from arm64_ftr_reg_ctrel0.sys_val
223 */
224 .macro read_ctr, reg
225alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
226 mrs \reg, ctr_el0 // read CTR
227 nop
228alternative_else
229 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
230alternative_endif
231 .endm
232
Geoff Levand7b7293a2016-04-27 17:47:00 +0100233
234/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100235 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
236 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100237 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100238 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100239 mrs \tmp, ctr_el0 // read CTR
240 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
241 mov \reg, #4 // bytes per word
242 lsl \reg, \reg, \tmp // actual cache line size
243 .endm
244
245/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100246 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100247 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100248 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100249 read_ctr \tmp
250 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
251 mov \reg, #4 // bytes per word
252 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100253 .endm
254
255/*
256 * raw_icache_line_size - get the minimum I-cache line size on this CPU
257 * from the CTR register.
258 */
259 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100260 mrs \tmp, ctr_el0 // read CTR
261 and \tmp, \tmp, #0xf // cache line size encoding
262 mov \reg, #4 // bytes per word
263 lsl \reg, \reg, \tmp // actual cache line size
264 .endm
265
266/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100267 * icache_line_size - get the safe I-cache line size across all CPUs
268 */
269 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100270 read_ctr \tmp
271 and \tmp, \tmp, #0xf // cache line size encoding
272 mov \reg, #4 // bytes per word
273 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100274 .endm
275
276/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100277 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
278 */
279 .macro tcr_set_idmap_t0sz, valreg, tmpreg
280#ifndef CONFIG_ARM64_VA_BITS_48
281 ldr_l \tmpreg, idmap_t0sz
282 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
283#endif
284 .endm
285
286/*
287 * Macro to perform a data cache maintenance for the interval
288 * [kaddr, kaddr + size)
289 *
290 * op: operation passed to dc instruction
291 * domain: domain used in dsb instruciton
292 * kaddr: starting virtual address of the region
293 * size: size of the region
294 * Corrupts: kaddr, size, tmp1, tmp2
295 */
296 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
297 dcache_line_size \tmp1, \tmp2
298 add \size, \kaddr, \size
299 sub \tmp2, \tmp1, #1
300 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003019998:
302 .if (\op == cvau || \op == cvac)
303alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
304 dc \op, \kaddr
305alternative_else
306 dc civac, \kaddr
307alternative_endif
308 .else
309 dc \op, \kaddr
310 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100311 add \kaddr, \kaddr, \tmp1
312 cmp \kaddr, \size
313 b.lo 9998b
314 dsb \domain
315 .endm
316
317/*
318 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
319 */
320 .macro reset_pmuserenr_el0, tmpreg
321 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
322 sbfx \tmpreg, \tmpreg, #8, #4
323 cmp \tmpreg, #1 // Skip if no PMU present
324 b.lt 9000f
325 msr pmuserenr_el0, xzr // Disable PMU access from EL0
3269000:
327 .endm
328
329/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100330 * copy_page - copy src to dest using temp registers t1-t8
331 */
332 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
3339998: ldp \t1, \t2, [\src]
334 ldp \t3, \t4, [\src, #16]
335 ldp \t5, \t6, [\src, #32]
336 ldp \t7, \t8, [\src, #48]
337 add \src, \src, #64
338 stnp \t1, \t2, [\dest]
339 stnp \t3, \t4, [\dest, #16]
340 stnp \t5, \t6, [\dest, #32]
341 stnp \t7, \t8, [\dest, #48]
342 add \dest, \dest, #64
343 tst \src, #(PAGE_SIZE - 1)
344 b.ne 9998b
345 .endm
346
347/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100348 * Annotate a function as position independent, i.e., safe to be called before
349 * the kernel virtual mapping is activated.
350 */
351#define ENDPIPROC(x) \
352 .globl __pi_##x; \
353 .type __pi_##x, %function; \
354 .set __pi_##x, x; \
355 .size __pi_##x, . - x; \
356 ENDPROC(x)
357
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100358 /*
359 * Emit a 64-bit absolute little endian symbol reference in a way that
360 * ensures that it will be resolved at build time, even when building a
361 * PIE binary. This requires cooperation from the linker script, which
362 * must emit the lo32/hi32 halves individually.
363 */
364 .macro le64sym, sym
365 .long \sym\()_lo32
366 .long \sym\()_hi32
367 .endm
368
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200369 /*
370 * mov_q - move an immediate constant into a 64-bit register using
371 * between 2 and 4 movz/movk instructions (depending on the
372 * magnitude and sign of the operand)
373 */
374 .macro mov_q, reg, val
375 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
376 movz \reg, :abs_g1_s:\val
377 .else
378 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
379 movz \reg, :abs_g2_s:\val
380 .else
381 movz \reg, :abs_g3:\val
382 movk \reg, :abs_g2_nc:\val
383 .endif
384 movk \reg, :abs_g1_nc:\val
385 .endif
386 movk \reg, :abs_g0_nc:\val
387 .endm
388
Marc Zyngierf3e39272015-02-20 13:53:13 +0000389#endif /* __ASM_ASSEMBLER_H */