GNU Linux-libre 4.9.337-gnu1
[releases.git] / arch / arm64 / include / asm / assembler.h
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
23 #ifndef __ASM_ASSEMBLER_H
24 #define __ASM_ASSEMBLER_H
25
26 #include <asm/asm-offsets.h>
27 #include <asm/cpufeature.h>
28 #include <asm/cputype.h>
29 #include <asm/page.h>
30 #include <asm/pgtable-hwdef.h>
31 #include <asm/ptrace.h>
32 #include <asm/thread_info.h>
33
34 /*
35  * Enable and disable interrupts.
36  */
37         .macro  disable_irq
38         msr     daifset, #2
39         .endm
40
41         .macro  enable_irq
42         msr     daifclr, #2
43         .endm
44
45 /*
46  * Enable and disable debug exceptions.
47  */
48         .macro  disable_dbg
49         msr     daifset, #8
50         .endm
51
52         .macro  enable_dbg
53         msr     daifclr, #8
54         .endm
55
56         .macro  disable_step_tsk, flgs, tmp
57         tbz     \flgs, #TIF_SINGLESTEP, 9990f
58         mrs     \tmp, mdscr_el1
59         bic     \tmp, \tmp, #1
60         msr     mdscr_el1, \tmp
61         isb     // Synchronise with enable_dbg
62 9990:
63         .endm
64
65         .macro  enable_step_tsk, flgs, tmp
66         tbz     \flgs, #TIF_SINGLESTEP, 9990f
67         disable_dbg
68         mrs     \tmp, mdscr_el1
69         orr     \tmp, \tmp, #1
70         msr     mdscr_el1, \tmp
71 9990:
72         .endm
73
74 /*
75  * Enable both debug exceptions and interrupts. This is likely to be
76  * faster than two daifclr operations, since writes to this register
77  * are self-synchronising.
78  */
79         .macro  enable_dbg_and_irq
80         msr     daifclr, #(8 | 2)
81         .endm
82
83 /*
84  * SMP data memory barrier
85  */
86         .macro  smp_dmb, opt
87         dmb     \opt
88         .endm
89
90 /*
91  * Value prediction barrier
92  */
93         .macro  csdb
94         hint    #20
95         .endm
96
97 /*
98  * Clear Branch History instruction
99  */
100         .macro clearbhb
101         hint    #22
102         .endm
103
104 /*
105  * Sanitise a 64-bit bounded index wrt speculation, returning zero if out
106  * of bounds.
107  */
108         .macro  mask_nospec64, idx, limit, tmp
109         sub     \tmp, \idx, \limit
110         bic     \tmp, \tmp, \idx
111         and     \idx, \idx, \tmp, asr #63
112         csdb
113         .endm
114
115 /*
116  * NOP sequence
117  */
118         .macro  nops, num
119         .rept   \num
120         nop
121         .endr
122         .endm
123
124 /*
125  * Emit an entry into the exception table
126  */
127         .macro          _asm_extable, from, to
128         .pushsection    __ex_table, "a"
129         .align          3
130         .long           (\from - .), (\to - .)
131         .popsection
132         .endm
133
134 #define USER(l, x...)                           \
135 9999:   x;                                      \
136         _asm_extable    9999b, l
137
138 /*
139  * Register aliases.
140  */
141 lr      .req    x30             // link register
142
143 /*
144  * Vector entry
145  */
146          .macro ventry  label
147         .align  7
148         b       \label
149         .endm
150
151 /*
152  * Select code when configured for BE.
153  */
154 #ifdef CONFIG_CPU_BIG_ENDIAN
155 #define CPU_BE(code...) code
156 #else
157 #define CPU_BE(code...)
158 #endif
159
160 /*
161  * Select code when configured for LE.
162  */
163 #ifdef CONFIG_CPU_BIG_ENDIAN
164 #define CPU_LE(code...)
165 #else
166 #define CPU_LE(code...) code
167 #endif
168
169 /*
170  * Define a macro that constructs a 64-bit value by concatenating two
171  * 32-bit registers. Note that on big endian systems the order of the
172  * registers is swapped.
173  */
174 #ifndef CONFIG_CPU_BIG_ENDIAN
175         .macro  regs_to_64, rd, lbits, hbits
176 #else
177         .macro  regs_to_64, rd, hbits, lbits
178 #endif
179         orr     \rd, \lbits, \hbits, lsl #32
180         .endm
181
182 /*
183  * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
184  * <symbol> is within the range +/- 4 GB of the PC when running
185  * in core kernel context. In module context, a movz/movk sequence
186  * is used, since modules may be loaded far away from the kernel
187  * when KASLR is in effect.
188  */
189         /*
190          * @dst: destination register (64 bit wide)
191          * @sym: name of the symbol
192          */
193         .macro  adr_l, dst, sym
194 #ifndef MODULE
195         adrp    \dst, \sym
196         add     \dst, \dst, :lo12:\sym
197 #else
198         movz    \dst, #:abs_g3:\sym
199         movk    \dst, #:abs_g2_nc:\sym
200         movk    \dst, #:abs_g1_nc:\sym
201         movk    \dst, #:abs_g0_nc:\sym
202 #endif
203         .endm
204
205         /*
206          * @dst: destination register (32 or 64 bit wide)
207          * @sym: name of the symbol
208          * @tmp: optional 64-bit scratch register to be used if <dst> is a
209          *       32-bit wide register, in which case it cannot be used to hold
210          *       the address
211          */
212         .macro  ldr_l, dst, sym, tmp=
213 #ifndef MODULE
214         .ifb    \tmp
215         adrp    \dst, \sym
216         ldr     \dst, [\dst, :lo12:\sym]
217         .else
218         adrp    \tmp, \sym
219         ldr     \dst, [\tmp, :lo12:\sym]
220         .endif
221 #else
222         .ifb    \tmp
223         adr_l   \dst, \sym
224         ldr     \dst, [\dst]
225         .else
226         adr_l   \tmp, \sym
227         ldr     \dst, [\tmp]
228         .endif
229 #endif
230         .endm
231
232         /*
233          * @src: source register (32 or 64 bit wide)
234          * @sym: name of the symbol
235          * @tmp: mandatory 64-bit scratch register to calculate the address
236          *       while <src> needs to be preserved.
237          */
238         .macro  str_l, src, sym, tmp
239 #ifndef MODULE
240         adrp    \tmp, \sym
241         str     \src, [\tmp, :lo12:\sym]
242 #else
243         adr_l   \tmp, \sym
244         str     \src, [\tmp]
245 #endif
246         .endm
247
248         /*
249          * @dst: Result of per_cpu(sym, smp_processor_id())
250          * @sym: The name of the per-cpu variable
251          * @tmp: scratch register
252          */
253         .macro adr_this_cpu, dst, sym, tmp
254         adr_l   \dst, \sym
255 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
256         mrs     \tmp, tpidr_el1
257 alternative_else
258         mrs     \tmp, tpidr_el2
259 alternative_endif
260         add     \dst, \dst, \tmp
261         .endm
262
263         /*
264          * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
265          * @sym: The name of the per-cpu variable
266          * @tmp: scratch register
267          */
268         .macro ldr_this_cpu dst, sym, tmp
269         adr_l   \dst, \sym
270 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
271         mrs     \tmp, tpidr_el1
272 alternative_else
273         mrs     \tmp, tpidr_el2
274 alternative_endif
275         ldr     \dst, [\dst, \tmp]
276         .endm
277
278 /*
279  * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
280  */
281         .macro  vma_vm_mm, rd, rn
282         ldr     \rd, [\rn, #VMA_VM_MM]
283         .endm
284
285 /*
286  * mmid - get context id from mm pointer (mm->context.id)
287  */
288         .macro  mmid, rd, rn
289         ldr     \rd, [\rn, #MM_CONTEXT_ID]
290         .endm
291 /*
292  * read_ctr - read CTR_EL0. If the system has mismatched
293  * cache line sizes, provide the system wide safe value
294  * from arm64_ftr_reg_ctrel0.sys_val
295  */
296         .macro  read_ctr, reg
297 alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
298         mrs     \reg, ctr_el0                   // read CTR
299         nop
300 alternative_else
301         ldr_l   \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
302 alternative_endif
303         .endm
304
305
306 /*
307  * raw_dcache_line_size - get the minimum D-cache line size on this CPU
308  * from the CTR register.
309  */
310         .macro  raw_dcache_line_size, reg, tmp
311         mrs     \tmp, ctr_el0                   // read CTR
312         ubfm    \tmp, \tmp, #16, #19            // cache line size encoding
313         mov     \reg, #4                        // bytes per word
314         lsl     \reg, \reg, \tmp                // actual cache line size
315         .endm
316
317 /*
318  * dcache_line_size - get the safe D-cache line size across all CPUs
319  */
320         .macro  dcache_line_size, reg, tmp
321         read_ctr        \tmp
322         ubfm            \tmp, \tmp, #16, #19    // 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  * raw_icache_line_size - get the minimum I-cache line size on this CPU
329  * from the CTR register.
330  */
331         .macro  raw_icache_line_size, reg, tmp
332         mrs     \tmp, ctr_el0                   // read CTR
333         and     \tmp, \tmp, #0xf                // cache line size encoding
334         mov     \reg, #4                        // bytes per word
335         lsl     \reg, \reg, \tmp                // actual cache line size
336         .endm
337
338 /*
339  * icache_line_size - get the safe I-cache line size across all CPUs
340  */
341         .macro  icache_line_size, reg, tmp
342         read_ctr        \tmp
343         and             \tmp, \tmp, #0xf        // cache line size encoding
344         mov             \reg, #4                // bytes per word
345         lsl             \reg, \reg, \tmp        // actual cache line size
346         .endm
347
348 /*
349  * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
350  */
351         .macro  tcr_set_idmap_t0sz, valreg, tmpreg
352 #ifndef CONFIG_ARM64_VA_BITS_48
353         ldr_l   \tmpreg, idmap_t0sz
354         bfi     \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
355 #endif
356         .endm
357
358 /*
359  * Macro to perform a data cache maintenance for the interval
360  * [kaddr, kaddr + size)
361  *
362  *      op:             operation passed to dc instruction
363  *      domain:         domain used in dsb instruciton
364  *      kaddr:          starting virtual address of the region
365  *      size:           size of the region
366  *      Corrupts:       kaddr, size, tmp1, tmp2
367  */
368         .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
369         dcache_line_size \tmp1, \tmp2
370         add     \size, \kaddr, \size
371         sub     \tmp2, \tmp1, #1
372         bic     \kaddr, \kaddr, \tmp2
373 9998:
374         .if     (\op == cvau || \op == cvac)
375 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
376         dc      \op, \kaddr
377 alternative_else
378         dc      civac, \kaddr
379 alternative_endif
380         .else
381         dc      \op, \kaddr
382         .endif
383         add     \kaddr, \kaddr, \tmp1
384         cmp     \kaddr, \size
385         b.lo    9998b
386         dsb     \domain
387         .endm
388
389 /*
390  * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
391  */
392         .macro  reset_pmuserenr_el0, tmpreg
393         mrs     \tmpreg, id_aa64dfr0_el1        // Check ID_AA64DFR0_EL1 PMUVer
394         sbfx    \tmpreg, \tmpreg, #8, #4
395         cmp     \tmpreg, #1                     // Skip if no PMU present
396         b.lt    9000f
397         msr     pmuserenr_el0, xzr              // Disable PMU access from EL0
398 9000:
399         .endm
400
401 /*
402  * copy_page - copy src to dest using temp registers t1-t8
403  */
404         .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
405 9998:   ldp     \t1, \t2, [\src]
406         ldp     \t3, \t4, [\src, #16]
407         ldp     \t5, \t6, [\src, #32]
408         ldp     \t7, \t8, [\src, #48]
409         add     \src, \src, #64
410         stnp    \t1, \t2, [\dest]
411         stnp    \t3, \t4, [\dest, #16]
412         stnp    \t5, \t6, [\dest, #32]
413         stnp    \t7, \t8, [\dest, #48]
414         add     \dest, \dest, #64
415         tst     \src, #(PAGE_SIZE - 1)
416         b.ne    9998b
417         .endm
418
419 /*
420  * Annotate a function as position independent, i.e., safe to be called before
421  * the kernel virtual mapping is activated.
422  */
423 #define ENDPIPROC(x)                    \
424         .globl  __pi_##x;               \
425         .type   __pi_##x, %function;    \
426         .set    __pi_##x, x;            \
427         .size   __pi_##x, . - x;        \
428         ENDPROC(x)
429
430         /*
431          * Emit a 64-bit absolute little endian symbol reference in a way that
432          * ensures that it will be resolved at build time, even when building a
433          * PIE binary. This requires cooperation from the linker script, which
434          * must emit the lo32/hi32 halves individually.
435          */
436         .macro  le64sym, sym
437         .long   \sym\()_lo32
438         .long   \sym\()_hi32
439         .endm
440
441         /*
442          * mov_q - move an immediate constant into a 64-bit register using
443          *         between 2 and 4 movz/movk instructions (depending on the
444          *         magnitude and sign of the operand)
445          */
446         .macro  mov_q, reg, val
447         .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
448         movz    \reg, :abs_g1_s:\val
449         .else
450         .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
451         movz    \reg, :abs_g2_s:\val
452         .else
453         movz    \reg, :abs_g3:\val
454         movk    \reg, :abs_g2_nc:\val
455         .endif
456         movk    \reg, :abs_g1_nc:\val
457         .endif
458         movk    \reg, :abs_g0_nc:\val
459         .endm
460
461         .macro  pte_to_phys, phys, pte
462         and     \phys, \pte, #(((1 << (48 - PAGE_SHIFT)) - 1) << PAGE_SHIFT)
463         .endm
464
465 /*
466  * Check the MIDR_EL1 of the current CPU for a given model and a range of
467  * variant/revision. See asm/cputype.h for the macros used below.
468  *
469  *      model:          MIDR_CPU_MODEL of CPU
470  *      rv_min:         Minimum of MIDR_CPU_VAR_REV()
471  *      rv_max:         Maximum of MIDR_CPU_VAR_REV()
472  *      res:            Result register.
473  *      tmp1, tmp2, tmp3: Temporary registers
474  *
475  * Corrupts: res, tmp1, tmp2, tmp3
476  * Returns:  0, if the CPU id doesn't match. Non-zero otherwise
477  */
478         .macro  cpu_midr_match model, rv_min, rv_max, res, tmp1, tmp2, tmp3
479         mrs             \res, midr_el1
480         mov_q           \tmp1, (MIDR_REVISION_MASK | MIDR_VARIANT_MASK)
481         mov_q           \tmp2, MIDR_CPU_MODEL_MASK
482         and             \tmp3, \res, \tmp2      // Extract model
483         and             \tmp1, \res, \tmp1      // rev & variant
484         mov_q           \tmp2, \model
485         cmp             \tmp3, \tmp2
486         cset            \res, eq
487         cbz             \res, .Ldone\@          // Model matches ?
488
489         .if (\rv_min != 0)                      // Skip min check if rv_min == 0
490         mov_q           \tmp3, \rv_min
491         cmp             \tmp1, \tmp3
492         cset            \res, ge
493         .endif                                  // \rv_min != 0
494         /* Skip rv_max check if rv_min == rv_max && rv_min != 0 */
495         .if ((\rv_min != \rv_max) || \rv_min == 0)
496         mov_q           \tmp2, \rv_max
497         cmp             \tmp1, \tmp2
498         cset            \tmp2, le
499         and             \res, \res, \tmp2
500         .endif
501 .Ldone\@:
502         .endm
503
504         .macro __mitigate_spectre_bhb_loop      tmp
505 #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
506 alternative_cb  spectre_bhb_patch_loop_iter
507         mov     \tmp, #32               // Patched to correct the immediate
508 alternative_cb_end
509 .Lspectre_bhb_loop\@:
510         b       . + 4
511         subs    \tmp, \tmp, #1
512         b.ne    .Lspectre_bhb_loop\@
513         dsb     nsh
514         isb
515 #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
516         .endm
517
518         /* Save/restores x0-x3 to the stack */
519         .macro __mitigate_spectre_bhb_fw
520 #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
521         stp     x0, x1, [sp, #-16]!
522         stp     x2, x3, [sp, #-16]!
523         mov     w0, #ARM_SMCCC_ARCH_WORKAROUND_3
524 alternative_cb  arm64_update_smccc_conduit
525         nop                                     // Patched to SMC/HVC #0
526 alternative_cb_end
527         ldp     x2, x3, [sp], #16
528         ldp     x0, x1, [sp], #16
529 #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
530         .endm
531 #endif  /* __ASM_ASSEMBLER_H */