Merge remote-tracking branch 'arm64/for-next/vhe-only' into kvmarm-master/next
[linux-2.6-block.git] / arch / arm64 / include / asm / assembler.h
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
12 #ifndef __ASM_ASSEMBLER_H
13 #define __ASM_ASSEMBLER_H
14
15 #include <asm-generic/export.h>
16
17 #include <asm/asm-offsets.h>
18 #include <asm/asm-bug.h>
19 #include <asm/cpufeature.h>
20 #include <asm/cputype.h>
21 #include <asm/debug-monitors.h>
22 #include <asm/page.h>
23 #include <asm/pgtable-hwdef.h>
24 #include <asm/ptrace.h>
25 #include <asm/thread_info.h>
26
27         .macro save_and_disable_daif, flags
28         mrs     \flags, daif
29         msr     daifset, #0xf
30         .endm
31
32         .macro disable_daif
33         msr     daifset, #0xf
34         .endm
35
36         .macro enable_daif
37         msr     daifclr, #0xf
38         .endm
39
40         .macro  restore_daif, flags:req
41         msr     daif, \flags
42         .endm
43
44         /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
45         .macro enable_da_f
46         msr     daifclr, #(8 | 4 | 1)
47         .endm
48
49 /*
50  * Save/restore interrupts.
51  */
52         .macro  save_and_disable_irq, flags
53         mrs     \flags, daif
54         msr     daifset, #2
55         .endm
56
57         .macro  restore_irq, flags
58         msr     daif, \flags
59         .endm
60
61         .macro  enable_dbg
62         msr     daifclr, #8
63         .endm
64
65         .macro  disable_step_tsk, flgs, tmp
66         tbz     \flgs, #TIF_SINGLESTEP, 9990f
67         mrs     \tmp, mdscr_el1
68         bic     \tmp, \tmp, #DBG_MDSCR_SS
69         msr     mdscr_el1, \tmp
70         isb     // Synchronise with enable_dbg
71 9990:
72         .endm
73
74         /* call with daif masked */
75         .macro  enable_step_tsk, flgs, tmp
76         tbz     \flgs, #TIF_SINGLESTEP, 9990f
77         mrs     \tmp, mdscr_el1
78         orr     \tmp, \tmp, #DBG_MDSCR_SS
79         msr     mdscr_el1, \tmp
80 9990:
81         .endm
82
83 /*
84  * RAS Error Synchronization barrier
85  */
86         .macro  esb
87 #ifdef CONFIG_ARM64_RAS_EXTN
88         hint    #16
89 #else
90         nop
91 #endif
92         .endm
93
94 /*
95  * Value prediction barrier
96  */
97         .macro  csdb
98         hint    #20
99         .endm
100
101 /*
102  * Speculation barrier
103  */
104         .macro  sb
105 alternative_if_not ARM64_HAS_SB
106         dsb     nsh
107         isb
108 alternative_else
109         SB_BARRIER_INSN
110         nop
111 alternative_endif
112         .endm
113
114 /*
115  * NOP sequence
116  */
117         .macro  nops, num
118         .rept   \num
119         nop
120         .endr
121         .endm
122
123 /*
124  * Emit an entry into the exception table
125  */
126         .macro          _asm_extable, from, to
127         .pushsection    __ex_table, "a"
128         .align          3
129         .long           (\from - .), (\to - .)
130         .popsection
131         .endm
132
133 #define USER(l, x...)                           \
134 9999:   x;                                      \
135         _asm_extable    9999b, l
136
137 /*
138  * Register aliases.
139  */
140 lr      .req    x30             // link register
141
142 /*
143  * Vector entry
144  */
145          .macro ventry  label
146         .align  7
147         b       \label
148         .endm
149
150 /*
151  * Select code when configured for BE.
152  */
153 #ifdef CONFIG_CPU_BIG_ENDIAN
154 #define CPU_BE(code...) code
155 #else
156 #define CPU_BE(code...)
157 #endif
158
159 /*
160  * Select code when configured for LE.
161  */
162 #ifdef CONFIG_CPU_BIG_ENDIAN
163 #define CPU_LE(code...)
164 #else
165 #define CPU_LE(code...) code
166 #endif
167
168 /*
169  * Define a macro that constructs a 64-bit value by concatenating two
170  * 32-bit registers. Note that on big endian systems the order of the
171  * registers is swapped.
172  */
173 #ifndef CONFIG_CPU_BIG_ENDIAN
174         .macro  regs_to_64, rd, lbits, hbits
175 #else
176         .macro  regs_to_64, rd, hbits, lbits
177 #endif
178         orr     \rd, \lbits, \hbits, lsl #32
179         .endm
180
181 /*
182  * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
183  * <symbol> is within the range +/- 4 GB of the PC.
184  */
185         /*
186          * @dst: destination register (64 bit wide)
187          * @sym: name of the symbol
188          */
189         .macro  adr_l, dst, sym
190         adrp    \dst, \sym
191         add     \dst, \dst, :lo12:\sym
192         .endm
193
194         /*
195          * @dst: destination register (32 or 64 bit wide)
196          * @sym: name of the symbol
197          * @tmp: optional 64-bit scratch register to be used if <dst> is a
198          *       32-bit wide register, in which case it cannot be used to hold
199          *       the address
200          */
201         .macro  ldr_l, dst, sym, tmp=
202         .ifb    \tmp
203         adrp    \dst, \sym
204         ldr     \dst, [\dst, :lo12:\sym]
205         .else
206         adrp    \tmp, \sym
207         ldr     \dst, [\tmp, :lo12:\sym]
208         .endif
209         .endm
210
211         /*
212          * @src: source register (32 or 64 bit wide)
213          * @sym: name of the symbol
214          * @tmp: mandatory 64-bit scratch register to calculate the address
215          *       while <src> needs to be preserved.
216          */
217         .macro  str_l, src, sym, tmp
218         adrp    \tmp, \sym
219         str     \src, [\tmp, :lo12:\sym]
220         .endm
221
222         /*
223          * @dst: destination register
224          */
225 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
226         .macro  this_cpu_offset, dst
227         mrs     \dst, tpidr_el2
228         .endm
229 #else
230         .macro  this_cpu_offset, dst
231 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
232         mrs     \dst, tpidr_el1
233 alternative_else
234         mrs     \dst, tpidr_el2
235 alternative_endif
236         .endm
237 #endif
238
239         /*
240          * @dst: Result of per_cpu(sym, smp_processor_id()) (can be SP)
241          * @sym: The name of the per-cpu variable
242          * @tmp: scratch register
243          */
244         .macro adr_this_cpu, dst, sym, tmp
245         adrp    \tmp, \sym
246         add     \dst, \tmp, #:lo12:\sym
247         this_cpu_offset \tmp
248         add     \dst, \dst, \tmp
249         .endm
250
251         /*
252          * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
253          * @sym: The name of the per-cpu variable
254          * @tmp: scratch register
255          */
256         .macro ldr_this_cpu dst, sym, tmp
257         adr_l   \dst, \sym
258         this_cpu_offset \tmp
259         ldr     \dst, [\dst, \tmp]
260         .endm
261
262 /*
263  * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
264  */
265         .macro  vma_vm_mm, rd, rn
266         ldr     \rd, [\rn, #VMA_VM_MM]
267         .endm
268
269 /*
270  * read_ctr - read CTR_EL0. If the system has mismatched register fields,
271  * provide the system wide safe value from arm64_ftr_reg_ctrel0.sys_val
272  */
273         .macro  read_ctr, reg
274 #ifndef __KVM_NVHE_HYPERVISOR__
275 alternative_if_not ARM64_MISMATCHED_CACHE_TYPE
276         mrs     \reg, ctr_el0                   // read CTR
277         nop
278 alternative_else
279         ldr_l   \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
280 alternative_endif
281 #else
282 alternative_if_not ARM64_KVM_PROTECTED_MODE
283         ASM_BUG()
284 alternative_else_nop_endif
285 alternative_cb kvm_compute_final_ctr_el0
286         movz    \reg, #0
287         movk    \reg, #0, lsl #16
288         movk    \reg, #0, lsl #32
289         movk    \reg, #0, lsl #48
290 alternative_cb_end
291 #endif
292         .endm
293
294
295 /*
296  * raw_dcache_line_size - get the minimum D-cache line size on this CPU
297  * from the CTR register.
298  */
299         .macro  raw_dcache_line_size, reg, tmp
300         mrs     \tmp, ctr_el0                   // read CTR
301         ubfm    \tmp, \tmp, #16, #19            // cache line size encoding
302         mov     \reg, #4                        // bytes per word
303         lsl     \reg, \reg, \tmp                // actual cache line size
304         .endm
305
306 /*
307  * dcache_line_size - get the safe D-cache line size across all CPUs
308  */
309         .macro  dcache_line_size, reg, tmp
310         read_ctr        \tmp
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
314         .endm
315
316 /*
317  * raw_icache_line_size - get the minimum I-cache line size on this CPU
318  * from the CTR register.
319  */
320         .macro  raw_icache_line_size, reg, tmp
321         mrs     \tmp, ctr_el0                   // read CTR
322         and     \tmp, \tmp, #0xf                // cache line size encoding
323         mov     \reg, #4                        // bytes per word
324         lsl     \reg, \reg, \tmp                // actual cache line size
325         .endm
326
327 /*
328  * icache_line_size - get the safe I-cache line size across all CPUs
329  */
330         .macro  icache_line_size, reg, tmp
331         read_ctr        \tmp
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
335         .endm
336
337 /*
338  * tcr_set_t0sz - update TCR.T0SZ so that we can load the ID map
339  */
340         .macro  tcr_set_t0sz, valreg, t0sz
341         bfi     \valreg, \t0sz, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
342         .endm
343
344 /*
345  * tcr_set_t1sz - update TCR.T1SZ
346  */
347         .macro  tcr_set_t1sz, valreg, t1sz
348         bfi     \valreg, \t1sz, #TCR_T1SZ_OFFSET, #TCR_TxSZ_WIDTH
349         .endm
350
351 /*
352  * tcr_compute_pa_size - set TCR.(I)PS to the highest supported
353  * ID_AA64MMFR0_EL1.PARange value
354  *
355  *      tcr:            register with the TCR_ELx value to be updated
356  *      pos:            IPS or PS bitfield position
357  *      tmp{0,1}:       temporary registers
358  */
359         .macro  tcr_compute_pa_size, tcr, pos, tmp0, tmp1
360         mrs     \tmp0, ID_AA64MMFR0_EL1
361         // Narrow PARange to fit the PS field in TCR_ELx
362         ubfx    \tmp0, \tmp0, #ID_AA64MMFR0_PARANGE_SHIFT, #3
363         mov     \tmp1, #ID_AA64MMFR0_PARANGE_MAX
364         cmp     \tmp0, \tmp1
365         csel    \tmp0, \tmp1, \tmp0, hi
366         bfi     \tcr, \tmp0, \pos, #3
367         .endm
368
369 /*
370  * Macro to perform a data cache maintenance for the interval
371  * [kaddr, kaddr + size)
372  *
373  *      op:             operation passed to dc instruction
374  *      domain:         domain used in dsb instruciton
375  *      kaddr:          starting virtual address of the region
376  *      size:           size of the region
377  *      Corrupts:       kaddr, size, tmp1, tmp2
378  */
379         .macro __dcache_op_workaround_clean_cache, op, kaddr
380 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
381         dc      \op, \kaddr
382 alternative_else
383         dc      civac, \kaddr
384 alternative_endif
385         .endm
386
387         .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
388         dcache_line_size \tmp1, \tmp2
389         add     \size, \kaddr, \size
390         sub     \tmp2, \tmp1, #1
391         bic     \kaddr, \kaddr, \tmp2
392 9998:
393         .ifc    \op, cvau
394         __dcache_op_workaround_clean_cache \op, \kaddr
395         .else
396         .ifc    \op, cvac
397         __dcache_op_workaround_clean_cache \op, \kaddr
398         .else
399         .ifc    \op, cvap
400         sys     3, c7, c12, 1, \kaddr   // dc cvap
401         .else
402         .ifc    \op, cvadp
403         sys     3, c7, c13, 1, \kaddr   // dc cvadp
404         .else
405         dc      \op, \kaddr
406         .endif
407         .endif
408         .endif
409         .endif
410         add     \kaddr, \kaddr, \tmp1
411         cmp     \kaddr, \size
412         b.lo    9998b
413         dsb     \domain
414         .endm
415
416 /*
417  * Macro to perform an instruction cache maintenance for the interval
418  * [start, end)
419  *
420  *      start, end:     virtual addresses describing the region
421  *      label:          A label to branch to on user fault.
422  *      Corrupts:       tmp1, tmp2
423  */
424         .macro invalidate_icache_by_line start, end, tmp1, tmp2, label
425         icache_line_size \tmp1, \tmp2
426         sub     \tmp2, \tmp1, #1
427         bic     \tmp2, \start, \tmp2
428 9997:
429 USER(\label, ic ivau, \tmp2)                    // invalidate I line PoU
430         add     \tmp2, \tmp2, \tmp1
431         cmp     \tmp2, \end
432         b.lo    9997b
433         dsb     ish
434         isb
435         .endm
436
437 /*
438  * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
439  */
440         .macro  reset_pmuserenr_el0, tmpreg
441         mrs     \tmpreg, id_aa64dfr0_el1
442         sbfx    \tmpreg, \tmpreg, #ID_AA64DFR0_PMUVER_SHIFT, #4
443         cmp     \tmpreg, #1                     // Skip if no PMU present
444         b.lt    9000f
445         msr     pmuserenr_el0, xzr              // Disable PMU access from EL0
446 9000:
447         .endm
448
449 /*
450  * reset_amuserenr_el0 - reset AMUSERENR_EL0 if AMUv1 present
451  */
452         .macro  reset_amuserenr_el0, tmpreg
453         mrs     \tmpreg, id_aa64pfr0_el1        // Check ID_AA64PFR0_EL1
454         ubfx    \tmpreg, \tmpreg, #ID_AA64PFR0_AMU_SHIFT, #4
455         cbz     \tmpreg, .Lskip_\@              // Skip if no AMU present
456         msr_s   SYS_AMUSERENR_EL0, xzr          // Disable AMU access from EL0
457 .Lskip_\@:
458         .endm
459 /*
460  * copy_page - copy src to dest using temp registers t1-t8
461  */
462         .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
463 9998:   ldp     \t1, \t2, [\src]
464         ldp     \t3, \t4, [\src, #16]
465         ldp     \t5, \t6, [\src, #32]
466         ldp     \t7, \t8, [\src, #48]
467         add     \src, \src, #64
468         stnp    \t1, \t2, [\dest]
469         stnp    \t3, \t4, [\dest, #16]
470         stnp    \t5, \t6, [\dest, #32]
471         stnp    \t7, \t8, [\dest, #48]
472         add     \dest, \dest, #64
473         tst     \src, #(PAGE_SIZE - 1)
474         b.ne    9998b
475         .endm
476
477 /*
478  * Annotate a function as being unsuitable for kprobes.
479  */
480 #ifdef CONFIG_KPROBES
481 #define NOKPROBE(x)                             \
482         .pushsection "_kprobe_blacklist", "aw"; \
483         .quad   x;                              \
484         .popsection;
485 #else
486 #define NOKPROBE(x)
487 #endif
488
489 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
490 #define EXPORT_SYMBOL_NOKASAN(name)
491 #else
492 #define EXPORT_SYMBOL_NOKASAN(name)     EXPORT_SYMBOL(name)
493 #endif
494
495         /*
496          * Emit a 64-bit absolute little endian symbol reference in a way that
497          * ensures that it will be resolved at build time, even when building a
498          * PIE binary. This requires cooperation from the linker script, which
499          * must emit the lo32/hi32 halves individually.
500          */
501         .macro  le64sym, sym
502         .long   \sym\()_lo32
503         .long   \sym\()_hi32
504         .endm
505
506         /*
507          * mov_q - move an immediate constant into a 64-bit register using
508          *         between 2 and 4 movz/movk instructions (depending on the
509          *         magnitude and sign of the operand)
510          */
511         .macro  mov_q, reg, val
512         .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
513         movz    \reg, :abs_g1_s:\val
514         .else
515         .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
516         movz    \reg, :abs_g2_s:\val
517         .else
518         movz    \reg, :abs_g3:\val
519         movk    \reg, :abs_g2_nc:\val
520         .endif
521         movk    \reg, :abs_g1_nc:\val
522         .endif
523         movk    \reg, :abs_g0_nc:\val
524         .endm
525
526 /*
527  * Return the current task_struct.
528  */
529         .macro  get_current_task, rd
530         mrs     \rd, sp_el0
531         .endm
532
533 /*
534  * Offset ttbr1 to allow for 48-bit kernel VAs set with 52-bit PTRS_PER_PGD.
535  * orr is used as it can cover the immediate value (and is idempotent).
536  * In future this may be nop'ed out when dealing with 52-bit kernel VAs.
537  *      ttbr: Value of ttbr to set, modified.
538  */
539         .macro  offset_ttbr1, ttbr, tmp
540 #ifdef CONFIG_ARM64_VA_BITS_52
541         mrs_s   \tmp, SYS_ID_AA64MMFR2_EL1
542         and     \tmp, \tmp, #(0xf << ID_AA64MMFR2_LVA_SHIFT)
543         cbnz    \tmp, .Lskipoffs_\@
544         orr     \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
545 .Lskipoffs_\@ :
546 #endif
547         .endm
548
549 /*
550  * Perform the reverse of offset_ttbr1.
551  * bic is used as it can cover the immediate value and, in future, won't need
552  * to be nop'ed out when dealing with 52-bit kernel VAs.
553  */
554         .macro  restore_ttbr1, ttbr
555 #ifdef CONFIG_ARM64_VA_BITS_52
556         bic     \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET
557 #endif
558         .endm
559
560 /*
561  * Arrange a physical address in a TTBR register, taking care of 52-bit
562  * addresses.
563  *
564  *      phys:   physical address, preserved
565  *      ttbr:   returns the TTBR value
566  */
567         .macro  phys_to_ttbr, ttbr, phys
568 #ifdef CONFIG_ARM64_PA_BITS_52
569         orr     \ttbr, \phys, \phys, lsr #46
570         and     \ttbr, \ttbr, #TTBR_BADDR_MASK_52
571 #else
572         mov     \ttbr, \phys
573 #endif
574         .endm
575
576         .macro  phys_to_pte, pte, phys
577 #ifdef CONFIG_ARM64_PA_BITS_52
578         /*
579          * We assume \phys is 64K aligned and this is guaranteed by only
580          * supporting this configuration with 64K pages.
581          */
582         orr     \pte, \phys, \phys, lsr #36
583         and     \pte, \pte, #PTE_ADDR_MASK
584 #else
585         mov     \pte, \phys
586 #endif
587         .endm
588
589         .macro  pte_to_phys, phys, pte
590 #ifdef CONFIG_ARM64_PA_BITS_52
591         ubfiz   \phys, \pte, #(48 - 16 - 12), #16
592         bfxil   \phys, \pte, #16, #32
593         lsl     \phys, \phys, #16
594 #else
595         and     \phys, \pte, #PTE_ADDR_MASK
596 #endif
597         .endm
598
599 /*
600  * tcr_clear_errata_bits - Clear TCR bits that trigger an errata on this CPU.
601  */
602         .macro  tcr_clear_errata_bits, tcr, tmp1, tmp2
603 #ifdef CONFIG_FUJITSU_ERRATUM_010001
604         mrs     \tmp1, midr_el1
605
606         mov_q   \tmp2, MIDR_FUJITSU_ERRATUM_010001_MASK
607         and     \tmp1, \tmp1, \tmp2
608         mov_q   \tmp2, MIDR_FUJITSU_ERRATUM_010001
609         cmp     \tmp1, \tmp2
610         b.ne    10f
611
612         mov_q   \tmp2, TCR_CLEAR_FUJITSU_ERRATUM_010001
613         bic     \tcr, \tcr, \tmp2
614 10:
615 #endif /* CONFIG_FUJITSU_ERRATUM_010001 */
616         .endm
617
618 /**
619  * Errata workaround prior to disable MMU. Insert an ISB immediately prior
620  * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
621  */
622         .macro pre_disable_mmu_workaround
623 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
624         isb
625 #endif
626         .endm
627
628         /*
629          * frame_push - Push @regcount callee saved registers to the stack,
630          *              starting at x19, as well as x29/x30, and set x29 to
631          *              the new value of sp. Add @extra bytes of stack space
632          *              for locals.
633          */
634         .macro          frame_push, regcount:req, extra
635         __frame         st, \regcount, \extra
636         .endm
637
638         /*
639          * frame_pop  - Pop the callee saved registers from the stack that were
640          *              pushed in the most recent call to frame_push, as well
641          *              as x29/x30 and any extra stack space that may have been
642          *              allocated.
643          */
644         .macro          frame_pop
645         __frame         ld
646         .endm
647
648         .macro          __frame_regs, reg1, reg2, op, num
649         .if             .Lframe_regcount == \num
650         \op\()r         \reg1, [sp, #(\num + 1) * 8]
651         .elseif         .Lframe_regcount > \num
652         \op\()p         \reg1, \reg2, [sp, #(\num + 1) * 8]
653         .endif
654         .endm
655
656         .macro          __frame, op, regcount, extra=0
657         .ifc            \op, st
658         .if             (\regcount) < 0 || (\regcount) > 10
659         .error          "regcount should be in the range [0 ... 10]"
660         .endif
661         .if             ((\extra) % 16) != 0
662         .error          "extra should be a multiple of 16 bytes"
663         .endif
664         .ifdef          .Lframe_regcount
665         .if             .Lframe_regcount != -1
666         .error          "frame_push/frame_pop may not be nested"
667         .endif
668         .endif
669         .set            .Lframe_regcount, \regcount
670         .set            .Lframe_extra, \extra
671         .set            .Lframe_local_offset, ((\regcount + 3) / 2) * 16
672         stp             x29, x30, [sp, #-.Lframe_local_offset - .Lframe_extra]!
673         mov             x29, sp
674         .endif
675
676         __frame_regs    x19, x20, \op, 1
677         __frame_regs    x21, x22, \op, 3
678         __frame_regs    x23, x24, \op, 5
679         __frame_regs    x25, x26, \op, 7
680         __frame_regs    x27, x28, \op, 9
681
682         .ifc            \op, ld
683         .if             .Lframe_regcount == -1
684         .error          "frame_push/frame_pop may not be nested"
685         .endif
686         ldp             x29, x30, [sp], #.Lframe_local_offset + .Lframe_extra
687         .set            .Lframe_regcount, -1
688         .endif
689         .endm
690
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
708 .macro set_sctlr_el1, reg
709         set_sctlr sctlr_el1, \reg
710 .endm
711
712 .macro set_sctlr_el2, reg
713         set_sctlr sctlr_el2, \reg
714 .endm
715
716 /*
717  * Check whether to yield to another runnable task from kernel mode NEON code
718  * (which runs with preemption disabled).
719  *
720  * if_will_cond_yield_neon
721  *        // pre-yield patchup code
722  * do_cond_yield_neon
723  *        // post-yield patchup code
724  * endif_yield_neon    <label>
725  *
726  * where <label> is optional, and marks the point where execution will resume
727  * after a yield has been performed. If omitted, execution resumes right after
728  * the endif_yield_neon invocation. Note that the entire sequence, including
729  * the provided patchup code, will be omitted from the image if
730  * CONFIG_PREEMPTION is not defined.
731  *
732  * As a convenience, in the case where no patchup code is required, the above
733  * sequence may be abbreviated to
734  *
735  * cond_yield_neon <label>
736  *
737  * Note that the patchup code does not support assembler directives that change
738  * the output section, any use of such directives is undefined.
739  *
740  * The yield itself consists of the following:
741  * - Check whether the preempt count is exactly 1 and a reschedule is also
742  *   needed. If so, calling of preempt_enable() in kernel_neon_end() will
743  *   trigger a reschedule. If it is not the case, yielding is pointless.
744  * - Disable and re-enable kernel mode NEON, and branch to the yield fixup
745  *   code.
746  *
747  * This macro sequence may clobber all CPU state that is not guaranteed by the
748  * AAPCS to be preserved across an ordinary function call.
749  */
750
751         .macro          cond_yield_neon, lbl
752         if_will_cond_yield_neon
753         do_cond_yield_neon
754         endif_yield_neon        \lbl
755         .endm
756
757         .macro          if_will_cond_yield_neon
758 #ifdef CONFIG_PREEMPTION
759         get_current_task        x0
760         ldr             x0, [x0, #TSK_TI_PREEMPT]
761         sub             x0, x0, #PREEMPT_DISABLE_OFFSET
762         cbz             x0, .Lyield_\@
763         /* fall through to endif_yield_neon */
764         .subsection     1
765 .Lyield_\@ :
766 #else
767         .section        ".discard.cond_yield_neon", "ax"
768 #endif
769         .endm
770
771         .macro          do_cond_yield_neon
772         bl              kernel_neon_end
773         bl              kernel_neon_begin
774         .endm
775
776         .macro          endif_yield_neon, lbl
777         .ifnb           \lbl
778         b               \lbl
779         .else
780         b               .Lyield_out_\@
781         .endif
782         .previous
783 .Lyield_out_\@ :
784         .endm
785
786         /*
787          * Check whether preempt-disabled code should yield as soon as it
788          * is able. This is the case if re-enabling preemption a single
789          * time results in a preempt count of zero, and the TIF_NEED_RESCHED
790          * flag is set. (Note that the latter is stored negated in the
791          * top word of the thread_info::preempt_count field)
792          */
793         .macro          cond_yield, lbl:req, tmp:req
794 #ifdef CONFIG_PREEMPTION
795         get_current_task \tmp
796         ldr             \tmp, [\tmp, #TSK_TI_PREEMPT]
797         sub             \tmp, \tmp, #PREEMPT_DISABLE_OFFSET
798         cbz             \tmp, \lbl
799 #endif
800         .endm
801
802 /*
803  * This macro emits a program property note section identifying
804  * architecture features which require special handling, mainly for
805  * use in assembly files included in the VDSO.
806  */
807
808 #define NT_GNU_PROPERTY_TYPE_0  5
809 #define GNU_PROPERTY_AARCH64_FEATURE_1_AND      0xc0000000
810
811 #define GNU_PROPERTY_AARCH64_FEATURE_1_BTI      (1U << 0)
812 #define GNU_PROPERTY_AARCH64_FEATURE_1_PAC      (1U << 1)
813
814 #ifdef CONFIG_ARM64_BTI_KERNEL
815 #define GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT          \
816                 ((GNU_PROPERTY_AARCH64_FEATURE_1_BTI |  \
817                   GNU_PROPERTY_AARCH64_FEATURE_1_PAC))
818 #endif
819
820 #ifdef GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
821 .macro emit_aarch64_feature_1_and, feat=GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
822         .pushsection .note.gnu.property, "a"
823         .align  3
824         .long   2f - 1f
825         .long   6f - 3f
826         .long   NT_GNU_PROPERTY_TYPE_0
827 1:      .string "GNU"
828 2:
829         .align  3
830 3:      .long   GNU_PROPERTY_AARCH64_FEATURE_1_AND
831         .long   5f - 4f
832 4:
833         /*
834          * This is described with an array of char in the Linux API
835          * spec but the text and all other usage (including binutils,
836          * clang and GCC) treat this as a 32 bit value so no swizzling
837          * is required for big endian.
838          */
839         .long   \feat
840 5:
841         .align  3
842 6:
843         .popsection
844 .endm
845
846 #else
847 .macro emit_aarch64_feature_1_and, feat=0
848 .endm
849
850 #endif /* GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT */
851
852 #endif  /* __ASM_ASSEMBLER_H */