]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm64/include/asm/assembler.h
arm64: kpti: Add ->enable callback to remap swapper using nG mappings
[mirror_ubuntu-artful-kernel.git] / arch / arm64 / include / asm / assembler.h
CommitLineData
0be7320a 1/*
7b7293ae 2 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
0be7320a
CM
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
f3e39273
MZ
23#ifndef __ASM_ASSEMBLER_H
24#define __ASM_ASSEMBLER_H
25
7b7293ae 26#include <asm/asm-offsets.h>
823066d9 27#include <asm/cpufeature.h>
5003dbde 28#include <asm/page.h>
7b7293ae 29#include <asm/pgtable-hwdef.h>
0be7320a 30#include <asm/ptrace.h>
2a283070 31#include <asm/thread_info.h>
0be7320a 32
0be7320a
CM
33/*
34 * 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
4b65a5db
CM
44 .macro save_and_disable_irq, flags
45 mrs \flags, daif
46 msr daifset, #2
47 .endm
48
49 .macro restore_irq, flags
50 msr daif, \flags
51 .endm
52
0be7320a
CM
53/*
54 * Enable and disable debug exceptions.
55 */
56 .macro disable_dbg
57 msr daifset, #8
58 .endm
59
60 .macro enable_dbg
61 msr daifclr, #8
62 .endm
63
2a283070
WD
64 .macro disable_step_tsk, flgs, tmp
65 tbz \flgs, #TIF_SINGLESTEP, 9990f
0be7320a
CM
66 mrs \tmp, mdscr_el1
67 bic \tmp, \tmp, #1
68 msr mdscr_el1, \tmp
2a283070
WD
69 isb // Synchronise with enable_dbg
709990:
0be7320a
CM
71 .endm
72
2a283070
WD
73 .macro enable_step_tsk, flgs, tmp
74 tbz \flgs, #TIF_SINGLESTEP, 9990f
75 disable_dbg
0be7320a
CM
76 mrs \tmp, mdscr_el1
77 orr \tmp, \tmp, #1
78 msr mdscr_el1, \tmp
2a283070 799990:
0be7320a
CM
80 .endm
81
2a283070
WD
82/*
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.
86 */
87 .macro enable_dbg_and_irq
88 msr daifclr, #(8 | 2)
0be7320a
CM
89 .endm
90
91/*
92 * SMP data memory barrier
93 */
94 .macro smp_dmb, opt
0be7320a 95 dmb \opt
0be7320a
CM
96 .endm
97
f99a250c
WD
98/*
99 * NOP sequence
100 */
101 .macro nops, num
102 .rept \num
103 nop
104 .endr
105 .endm
106
6c94f27a
AB
107/*
108 * Emit an entry into the exception table
109 */
110 .macro _asm_extable, from, to
111 .pushsection __ex_table, "a"
112 .align 3
113 .long (\from - .), (\to - .)
114 .popsection
115 .endm
116
0be7320a
CM
117#define USER(l, x...) \
1189999: x; \
6c94f27a 119 _asm_extable 9999b, l
0be7320a
CM
120
121/*
122 * Register aliases.
123 */
124lr .req x30 // link register
dc637f1f
MZ
125
126/*
127 * Vector entry
128 */
129 .macro ventry label
130 .align 7
131 b \label
132 .endm
e68bedaa
ML
133
134/*
135 * Select code when configured for BE.
136 */
137#ifdef CONFIG_CPU_BIG_ENDIAN
138#define CPU_BE(code...) code
139#else
140#define CPU_BE(code...)
141#endif
142
143/*
144 * Select code when configured for LE.
145 */
146#ifdef CONFIG_CPU_BIG_ENDIAN
147#define CPU_LE(code...)
148#else
149#define CPU_LE(code...) code
150#endif
151
55b89540
ML
152/*
153 * Define a macro that constructs a 64-bit value by concatenating two
154 * 32-bit registers. Note that on big endian systems the order of the
155 * registers is swapped.
156 */
157#ifndef CONFIG_CPU_BIG_ENDIAN
158 .macro regs_to_64, rd, lbits, hbits
159#else
160 .macro regs_to_64, rd, hbits, lbits
161#endif
162 orr \rd, \lbits, \hbits, lsl #32
163 .endm
f3e39273 164
b784a5d9
AB
165/*
166 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
41c066f2
AB
167 * <symbol> is within the range +/- 4 GB of the PC when running
168 * in core kernel context. In module context, a movz/movk sequence
169 * is used, since modules may be loaded far away from the kernel
170 * when KASLR is in effect.
b784a5d9
AB
171 */
172 /*
173 * @dst: destination register (64 bit wide)
174 * @sym: name of the symbol
b784a5d9 175 */
41c066f2
AB
176 .macro adr_l, dst, sym
177#ifndef MODULE
b784a5d9
AB
178 adrp \dst, \sym
179 add \dst, \dst, :lo12:\sym
41c066f2
AB
180#else
181 movz \dst, #:abs_g3:\sym
182 movk \dst, #:abs_g2_nc:\sym
183 movk \dst, #:abs_g1_nc:\sym
184 movk \dst, #:abs_g0_nc:\sym
185#endif
b784a5d9
AB
186 .endm
187
188 /*
189 * @dst: destination register (32 or 64 bit wide)
190 * @sym: name of the symbol
191 * @tmp: optional 64-bit scratch register to be used if <dst> is a
192 * 32-bit wide register, in which case it cannot be used to hold
193 * the address
194 */
195 .macro ldr_l, dst, sym, tmp=
41c066f2 196#ifndef MODULE
b784a5d9
AB
197 .ifb \tmp
198 adrp \dst, \sym
199 ldr \dst, [\dst, :lo12:\sym]
200 .else
201 adrp \tmp, \sym
202 ldr \dst, [\tmp, :lo12:\sym]
203 .endif
41c066f2
AB
204#else
205 .ifb \tmp
206 adr_l \dst, \sym
207 ldr \dst, [\dst]
208 .else
209 adr_l \tmp, \sym
210 ldr \dst, [\tmp]
211 .endif
212#endif
b784a5d9
AB
213 .endm
214
215 /*
216 * @src: source register (32 or 64 bit wide)
217 * @sym: name of the symbol
218 * @tmp: mandatory 64-bit scratch register to calculate the address
219 * while <src> needs to be preserved.
220 */
221 .macro str_l, src, sym, tmp
41c066f2 222#ifndef MODULE
b784a5d9
AB
223 adrp \tmp, \sym
224 str \src, [\tmp, :lo12:\sym]
41c066f2
AB
225#else
226 adr_l \tmp, \sym
227 str \src, [\tmp]
228#endif
b784a5d9
AB
229 .endm
230
aa4d5d3c 231 /*
6de835e2
AB
232 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
233 * non-module code
aa4d5d3c 234 * @sym: The name of the per-cpu variable
aa4d5d3c
JM
235 * @tmp: scratch register
236 */
1b7e2296 237 .macro adr_this_cpu, dst, sym, tmp
6de835e2
AB
238#ifndef MODULE
239 adrp \tmp, \sym
240 add \dst, \tmp, #:lo12:\sym
241#else
1b7e2296 242 adr_l \dst, \sym
6de835e2 243#endif
aa4d5d3c 244 mrs \tmp, tpidr_el1
1b7e2296
MR
245 add \dst, \dst, \tmp
246 .endm
247
248 /*
249 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
250 * @sym: The name of the per-cpu variable
251 * @tmp: scratch register
252 */
253 .macro ldr_this_cpu dst, sym, tmp
254 adr_l \dst, \sym
255 mrs \tmp, tpidr_el1
256 ldr \dst, [\dst, \tmp]
aa4d5d3c
JM
257 .endm
258
7b7293ae
GL
259/*
260 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
261 */
262 .macro vma_vm_mm, rd, rn
263 ldr \rd, [\rn, #VMA_VM_MM]
264 .endm
265
266/*
267 * mmid - get context id from mm pointer (mm->context.id)
268 */
269 .macro mmid, rd, rn
270 ldr \rd, [\rn, #MM_CONTEXT_ID]
271 .endm
116c81f4
SP
272/*
273 * read_ctr - read CTR_EL0. If the system has mismatched
274 * cache line sizes, provide the system wide safe value
275 * from arm64_ftr_reg_ctrel0.sys_val
276 */
277 .macro read_ctr, reg
278alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
279 mrs \reg, ctr_el0 // read CTR
280 nop
281alternative_else
282 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
283alternative_endif
284 .endm
285
7b7293ae
GL
286
287/*
072f0a63
SP
288 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
289 * from the CTR register.
7b7293ae 290 */
072f0a63 291 .macro raw_dcache_line_size, reg, tmp
7b7293ae
GL
292 mrs \tmp, ctr_el0 // read CTR
293 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
294 mov \reg, #4 // bytes per word
295 lsl \reg, \reg, \tmp // actual cache line size
296 .endm
297
298/*
072f0a63 299 * dcache_line_size - get the safe D-cache line size across all CPUs
7b7293ae 300 */
072f0a63 301 .macro dcache_line_size, reg, tmp
116c81f4
SP
302 read_ctr \tmp
303 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
304 mov \reg, #4 // bytes per word
305 lsl \reg, \reg, \tmp // actual cache line size
072f0a63
SP
306 .endm
307
308/*
309 * raw_icache_line_size - get the minimum I-cache line size on this CPU
310 * from the CTR register.
311 */
312 .macro raw_icache_line_size, reg, tmp
7b7293ae
GL
313 mrs \tmp, ctr_el0 // read CTR
314 and \tmp, \tmp, #0xf // cache line size encoding
315 mov \reg, #4 // bytes per word
316 lsl \reg, \reg, \tmp // actual cache line size
317 .endm
318
072f0a63
SP
319/*
320 * icache_line_size - get the safe I-cache line size across all CPUs
321 */
322 .macro icache_line_size, reg, tmp
116c81f4
SP
323 read_ctr \tmp
324 and \tmp, \tmp, #0xf // cache line size encoding
325 mov \reg, #4 // bytes per word
326 lsl \reg, \reg, \tmp // actual cache line size
072f0a63
SP
327 .endm
328
7b7293ae
GL
329/*
330 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
331 */
332 .macro tcr_set_idmap_t0sz, valreg, tmpreg
333#ifndef CONFIG_ARM64_VA_BITS_48
334 ldr_l \tmpreg, idmap_t0sz
335 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
336#endif
337 .endm
338
339/*
340 * Macro to perform a data cache maintenance for the interval
341 * [kaddr, kaddr + size)
342 *
343 * op: operation passed to dc instruction
344 * domain: domain used in dsb instruciton
345 * kaddr: starting virtual address of the region
346 * size: size of the region
347 * Corrupts: kaddr, size, tmp1, tmp2
348 */
349 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
350 dcache_line_size \tmp1, \tmp2
351 add \size, \kaddr, \size
352 sub \tmp2, \tmp1, #1
353 bic \kaddr, \kaddr, \tmp2
823066d9
AP
3549998:
355 .if (\op == cvau || \op == cvac)
356alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
357 dc \op, \kaddr
358alternative_else
359 dc civac, \kaddr
3c55f98c
RM
360alternative_endif
361 .elseif (\op == cvap)
362alternative_if ARM64_HAS_DCPOP
363 sys 3, c7, c12, 1, \kaddr // dc cvap
364alternative_else
365 dc cvac, \kaddr
823066d9
AP
366alternative_endif
367 .else
368 dc \op, \kaddr
369 .endif
7b7293ae
GL
370 add \kaddr, \kaddr, \tmp1
371 cmp \kaddr, \size
372 b.lo 9998b
373 dsb \domain
374 .endm
375
376/*
377 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
378 */
379 .macro reset_pmuserenr_el0, tmpreg
380 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
381 sbfx \tmpreg, \tmpreg, #8, #4
382 cmp \tmpreg, #1 // Skip if no PMU present
383 b.lt 9000f
384 msr pmuserenr_el0, xzr // Disable PMU access from EL0
3859000:
386 .endm
387
5003dbde
GL
388/*
389 * copy_page - copy src to dest using temp registers t1-t8
390 */
391 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
3929998: ldp \t1, \t2, [\src]
393 ldp \t3, \t4, [\src, #16]
394 ldp \t5, \t6, [\src, #32]
395 ldp \t7, \t8, [\src, #48]
396 add \src, \src, #64
397 stnp \t1, \t2, [\dest]
398 stnp \t3, \t4, [\dest, #16]
399 stnp \t5, \t6, [\dest, #32]
400 stnp \t7, \t8, [\dest, #48]
401 add \dest, \dest, #64
402 tst \src, #(PAGE_SIZE - 1)
403 b.ne 9998b
404 .endm
405
20791846
AB
406/*
407 * Annotate a function as position independent, i.e., safe to be called before
408 * the kernel virtual mapping is activated.
409 */
410#define ENDPIPROC(x) \
411 .globl __pi_##x; \
412 .type __pi_##x, %function; \
413 .set __pi_##x, x; \
414 .size __pi_##x, . - x; \
415 ENDPROC(x)
416
4f7a82d7
MR
417/*
418 * Annotate a function as being unsuitable for kprobes.
419 */
420#ifdef CONFIG_KPROBES
421#define NOKPROBE(x) \
422 .pushsection "_kprobe_blacklist", "aw"; \
423 .quad x; \
424 .popsection;
425#else
426#define NOKPROBE(x)
427#endif
6ad1fe5d
AB
428 /*
429 * Emit a 64-bit absolute little endian symbol reference in a way that
430 * ensures that it will be resolved at build time, even when building a
431 * PIE binary. This requires cooperation from the linker script, which
432 * must emit the lo32/hi32 halves individually.
433 */
434 .macro le64sym, sym
435 .long \sym\()_lo32
436 .long \sym\()_hi32
437 .endm
438
30b5ba5c
AB
439 /*
440 * mov_q - move an immediate constant into a 64-bit register using
441 * between 2 and 4 movz/movk instructions (depending on the
442 * magnitude and sign of the operand)
443 */
444 .macro mov_q, reg, val
445 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
446 movz \reg, :abs_g1_s:\val
447 .else
448 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
449 movz \reg, :abs_g2_s:\val
450 .else
451 movz \reg, :abs_g3:\val
452 movk \reg, :abs_g2_nc:\val
453 .endif
454 movk \reg, :abs_g1_nc:\val
455 .endif
456 movk \reg, :abs_g0_nc:\val
457 .endm
458
4b65a5db
CM
459/*
460 * Return the current thread_info.
461 */
462 .macro get_thread_info, rd
463 mrs \rd, sp_el0
464 .endm
465
38fd94b0 466/*
9388fb4e 467 * Errata workaround post TTBRx_EL1 update.
f33bcf03 468 */
9388fb4e 469 .macro post_ttbr_update_workaround
f33bcf03
CM
470#ifdef CONFIG_CAVIUM_ERRATUM_27456
471alternative_if ARM64_WORKAROUND_CAVIUM_27456
472 ic iallu
473 dsb nsh
474 isb
475alternative_else_nop_endif
476#endif
477 .endm
5635c980
SD
478/**
479 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
480 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
481 */
482 .macro pre_disable_mmu_workaround
483#ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
484 isb
485#endif
486 .endm
f33bcf03 487
950c683a
WD
488 .macro pte_to_phys, phys, pte
489 and \phys, \pte, #(((1 << (48 - PAGE_SHIFT)) - 1) << PAGE_SHIFT)
490 .endm
491
f3e39273 492#endif /* __ASM_ASSEMBLER_H */