Merge tag '5.15-rc-cifs-part2' of git://git.samba.org/sfrench/cifs-2.6
[linux-2.6-microblaze.git] / arch / arm64 / include / asm / kvm_asm.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012,2013 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6
7 #ifndef __ARM_KVM_ASM_H__
8 #define __ARM_KVM_ASM_H__
9
10 #include <asm/hyp_image.h>
11 #include <asm/insn.h>
12 #include <asm/virt.h>
13
14 #define ARM_EXIT_WITH_SERROR_BIT  31
15 #define ARM_EXCEPTION_CODE(x)     ((x) & ~(1U << ARM_EXIT_WITH_SERROR_BIT))
16 #define ARM_EXCEPTION_IS_TRAP(x)  (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP)
17 #define ARM_SERROR_PENDING(x)     !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT))
18
19 #define ARM_EXCEPTION_IRQ         0
20 #define ARM_EXCEPTION_EL1_SERROR  1
21 #define ARM_EXCEPTION_TRAP        2
22 #define ARM_EXCEPTION_IL          3
23 /* The hyp-stub will return this for any kvm_call_hyp() call */
24 #define ARM_EXCEPTION_HYP_GONE    HVC_STUB_ERR
25
26 #define kvm_arm_exception_type                                  \
27         {ARM_EXCEPTION_IRQ,             "IRQ"           },      \
28         {ARM_EXCEPTION_EL1_SERROR,      "SERROR"        },      \
29         {ARM_EXCEPTION_TRAP,            "TRAP"          },      \
30         {ARM_EXCEPTION_HYP_GONE,        "HYP_GONE"      }
31
32 /*
33  * Size of the HYP vectors preamble. kvm_patch_vector_branch() generates code
34  * that jumps over this.
35  */
36 #define KVM_VECTOR_PREAMBLE     (2 * AARCH64_INSN_SIZE)
37
38 #define KVM_HOST_SMCCC_ID(id)                                           \
39         ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,                         \
40                            ARM_SMCCC_SMC_64,                            \
41                            ARM_SMCCC_OWNER_VENDOR_HYP,                  \
42                            (id))
43
44 #define KVM_HOST_SMCCC_FUNC(name) KVM_HOST_SMCCC_ID(__KVM_HOST_SMCCC_FUNC_##name)
45
46 #define __KVM_HOST_SMCCC_FUNC___kvm_hyp_init                    0
47 #define __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run                    1
48 #define __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context            2
49 #define __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_ipa          3
50 #define __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid              4
51 #define __KVM_HOST_SMCCC_FUNC___kvm_flush_cpu_context           5
52 #define __KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntvoff           6
53 #define __KVM_HOST_SMCCC_FUNC___kvm_enable_ssbs                 7
54 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_get_gic_config          8
55 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_read_vmcr               9
56 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_write_vmcr              10
57 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_init_lrs                11
58 #define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2                12
59 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs               13
60 #define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs            14
61 #define __KVM_HOST_SMCCC_FUNC___pkvm_init                       15
62 #define __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp             16
63 #define __KVM_HOST_SMCCC_FUNC___pkvm_create_private_mapping     17
64 #define __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector             18
65 #define __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize              19
66 #define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc                   20
67
68 #ifndef __ASSEMBLY__
69
70 #include <linux/mm.h>
71
72 #define DECLARE_KVM_VHE_SYM(sym)        extern char sym[]
73 #define DECLARE_KVM_NVHE_SYM(sym)       extern char kvm_nvhe_sym(sym)[]
74
75 /*
76  * Define a pair of symbols sharing the same name but one defined in
77  * VHE and the other in nVHE hyp implementations.
78  */
79 #define DECLARE_KVM_HYP_SYM(sym)                \
80         DECLARE_KVM_VHE_SYM(sym);               \
81         DECLARE_KVM_NVHE_SYM(sym)
82
83 #define DECLARE_KVM_VHE_PER_CPU(type, sym)      \
84         DECLARE_PER_CPU(type, sym)
85 #define DECLARE_KVM_NVHE_PER_CPU(type, sym)     \
86         DECLARE_PER_CPU(type, kvm_nvhe_sym(sym))
87
88 #define DECLARE_KVM_HYP_PER_CPU(type, sym)      \
89         DECLARE_KVM_VHE_PER_CPU(type, sym);     \
90         DECLARE_KVM_NVHE_PER_CPU(type, sym)
91
92 /*
93  * Compute pointer to a symbol defined in nVHE percpu region.
94  * Returns NULL if percpu memory has not been allocated yet.
95  */
96 #define this_cpu_ptr_nvhe_sym(sym)      per_cpu_ptr_nvhe_sym(sym, smp_processor_id())
97 #define per_cpu_ptr_nvhe_sym(sym, cpu)                                          \
98         ({                                                                      \
99                 unsigned long base, off;                                        \
100                 base = kvm_arm_hyp_percpu_base[cpu];                            \
101                 off = (unsigned long)&CHOOSE_NVHE_SYM(sym) -                    \
102                       (unsigned long)&CHOOSE_NVHE_SYM(__per_cpu_start);         \
103                 base ? (typeof(CHOOSE_NVHE_SYM(sym))*)(base + off) : NULL;      \
104         })
105
106 #if defined(__KVM_NVHE_HYPERVISOR__)
107
108 #define CHOOSE_NVHE_SYM(sym)    sym
109 #define CHOOSE_HYP_SYM(sym)     CHOOSE_NVHE_SYM(sym)
110
111 /* The nVHE hypervisor shouldn't even try to access VHE symbols */
112 extern void *__nvhe_undefined_symbol;
113 #define CHOOSE_VHE_SYM(sym)             __nvhe_undefined_symbol
114 #define this_cpu_ptr_hyp_sym(sym)       (&__nvhe_undefined_symbol)
115 #define per_cpu_ptr_hyp_sym(sym, cpu)   (&__nvhe_undefined_symbol)
116
117 #elif defined(__KVM_VHE_HYPERVISOR__)
118
119 #define CHOOSE_VHE_SYM(sym)     sym
120 #define CHOOSE_HYP_SYM(sym)     CHOOSE_VHE_SYM(sym)
121
122 /* The VHE hypervisor shouldn't even try to access nVHE symbols */
123 extern void *__vhe_undefined_symbol;
124 #define CHOOSE_NVHE_SYM(sym)            __vhe_undefined_symbol
125 #define this_cpu_ptr_hyp_sym(sym)       (&__vhe_undefined_symbol)
126 #define per_cpu_ptr_hyp_sym(sym, cpu)   (&__vhe_undefined_symbol)
127
128 #else
129
130 /*
131  * BIG FAT WARNINGS:
132  *
133  * - Don't be tempted to change the following is_kernel_in_hyp_mode()
134  *   to has_vhe(). has_vhe() is implemented as a *final* capability,
135  *   while this is used early at boot time, when the capabilities are
136  *   not final yet....
137  *
138  * - Don't let the nVHE hypervisor have access to this, as it will
139  *   pick the *wrong* symbol (yes, it runs at EL2...).
140  */
141 #define CHOOSE_HYP_SYM(sym)             (is_kernel_in_hyp_mode()        \
142                                            ? CHOOSE_VHE_SYM(sym)        \
143                                            : CHOOSE_NVHE_SYM(sym))
144
145 #define this_cpu_ptr_hyp_sym(sym)       (is_kernel_in_hyp_mode()        \
146                                            ? this_cpu_ptr(&sym)         \
147                                            : this_cpu_ptr_nvhe_sym(sym))
148
149 #define per_cpu_ptr_hyp_sym(sym, cpu)   (is_kernel_in_hyp_mode()        \
150                                            ? per_cpu_ptr(&sym, cpu)     \
151                                            : per_cpu_ptr_nvhe_sym(sym, cpu))
152
153 #define CHOOSE_VHE_SYM(sym)     sym
154 #define CHOOSE_NVHE_SYM(sym)    kvm_nvhe_sym(sym)
155
156 #endif
157
158 struct kvm_nvhe_init_params {
159         unsigned long mair_el2;
160         unsigned long tcr_el2;
161         unsigned long tpidr_el2;
162         unsigned long stack_hyp_va;
163         phys_addr_t pgd_pa;
164         unsigned long hcr_el2;
165         unsigned long vttbr;
166         unsigned long vtcr;
167 };
168
169 /* Translate a kernel address @ptr into its equivalent linear mapping */
170 #define kvm_ksym_ref(ptr)                                               \
171         ({                                                              \
172                 void *val = (ptr);                                      \
173                 if (!is_kernel_in_hyp_mode())                           \
174                         val = lm_alias((ptr));                          \
175                 val;                                                    \
176          })
177 #define kvm_ksym_ref_nvhe(sym)  kvm_ksym_ref(kvm_nvhe_sym(sym))
178
179 struct kvm;
180 struct kvm_vcpu;
181 struct kvm_s2_mmu;
182
183 DECLARE_KVM_NVHE_SYM(__kvm_hyp_init);
184 DECLARE_KVM_HYP_SYM(__kvm_hyp_vector);
185 #define __kvm_hyp_init          CHOOSE_NVHE_SYM(__kvm_hyp_init)
186 #define __kvm_hyp_vector        CHOOSE_HYP_SYM(__kvm_hyp_vector)
187
188 extern unsigned long kvm_arm_hyp_percpu_base[NR_CPUS];
189 DECLARE_KVM_NVHE_SYM(__per_cpu_start);
190 DECLARE_KVM_NVHE_SYM(__per_cpu_end);
191
192 DECLARE_KVM_HYP_SYM(__bp_harden_hyp_vecs);
193 #define __bp_harden_hyp_vecs    CHOOSE_HYP_SYM(__bp_harden_hyp_vecs)
194
195 extern void __kvm_flush_vm_context(void);
196 extern void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu);
197 extern void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa,
198                                      int level);
199 extern void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu);
200
201 extern void __kvm_timer_set_cntvoff(u64 cntvoff);
202
203 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
204
205 extern void __kvm_adjust_pc(struct kvm_vcpu *vcpu);
206
207 extern u64 __vgic_v3_get_gic_config(void);
208 extern u64 __vgic_v3_read_vmcr(void);
209 extern void __vgic_v3_write_vmcr(u32 vmcr);
210 extern void __vgic_v3_init_lrs(void);
211
212 extern u64 __kvm_get_mdcr_el2(void);
213
214 #define __KVM_EXTABLE(from, to)                                         \
215         "       .pushsection    __kvm_ex_table, \"a\"\n"                \
216         "       .align          3\n"                                    \
217         "       .long           (" #from " - .), (" #to " - .)\n"       \
218         "       .popsection\n"
219
220
221 #define __kvm_at(at_op, addr)                                           \
222 ( {                                                                     \
223         int __kvm_at_err = 0;                                           \
224         u64 spsr, elr;                                                  \
225         asm volatile(                                                   \
226         "       mrs     %1, spsr_el2\n"                                 \
227         "       mrs     %2, elr_el2\n"                                  \
228         "1:     at      "at_op", %3\n"                                  \
229         "       isb\n"                                                  \
230         "       b       9f\n"                                           \
231         "2:     msr     spsr_el2, %1\n"                                 \
232         "       msr     elr_el2, %2\n"                                  \
233         "       mov     %w0, %4\n"                                      \
234         "9:\n"                                                          \
235         __KVM_EXTABLE(1b, 2b)                                           \
236         : "+r" (__kvm_at_err), "=&r" (spsr), "=&r" (elr)                \
237         : "r" (addr), "i" (-EFAULT));                                   \
238         __kvm_at_err;                                                   \
239 } )
240
241
242 #else /* __ASSEMBLY__ */
243
244 .macro get_host_ctxt reg, tmp
245         adr_this_cpu \reg, kvm_host_data, \tmp
246         add     \reg, \reg, #HOST_DATA_CONTEXT
247 .endm
248
249 .macro get_vcpu_ptr vcpu, ctxt
250         get_host_ctxt \ctxt, \vcpu
251         ldr     \vcpu, [\ctxt, #HOST_CONTEXT_VCPU]
252 .endm
253
254 .macro get_loaded_vcpu vcpu, ctxt
255         adr_this_cpu \ctxt, kvm_hyp_ctxt, \vcpu
256         ldr     \vcpu, [\ctxt, #HOST_CONTEXT_VCPU]
257 .endm
258
259 .macro set_loaded_vcpu vcpu, ctxt, tmp
260         adr_this_cpu \ctxt, kvm_hyp_ctxt, \tmp
261         str     \vcpu, [\ctxt, #HOST_CONTEXT_VCPU]
262 .endm
263
264 /*
265  * KVM extable for unexpected exceptions.
266  * In the same format _asm_extable, but output to a different section so that
267  * it can be mapped to EL2. The KVM version is not sorted. The caller must
268  * ensure:
269  * x18 has the hypervisor value to allow any Shadow-Call-Stack instrumented
270  * code to write to it, and that SPSR_EL2 and ELR_EL2 are restored by the fixup.
271  */
272 .macro  _kvm_extable, from, to
273         .pushsection    __kvm_ex_table, "a"
274         .align          3
275         .long           (\from - .), (\to - .)
276         .popsection
277 .endm
278
279 #define CPU_XREG_OFFSET(x)      (CPU_USER_PT_REGS + 8*x)
280 #define CPU_LR_OFFSET           CPU_XREG_OFFSET(30)
281 #define CPU_SP_EL0_OFFSET       (CPU_LR_OFFSET + 8)
282
283 /*
284  * We treat x18 as callee-saved as the host may use it as a platform
285  * register (e.g. for shadow call stack).
286  */
287 .macro save_callee_saved_regs ctxt
288         str     x18,      [\ctxt, #CPU_XREG_OFFSET(18)]
289         stp     x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
290         stp     x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
291         stp     x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
292         stp     x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
293         stp     x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
294         stp     x29, lr,  [\ctxt, #CPU_XREG_OFFSET(29)]
295 .endm
296
297 .macro restore_callee_saved_regs ctxt
298         // We require \ctxt is not x18-x28
299         ldr     x18,      [\ctxt, #CPU_XREG_OFFSET(18)]
300         ldp     x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
301         ldp     x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
302         ldp     x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
303         ldp     x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
304         ldp     x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
305         ldp     x29, lr,  [\ctxt, #CPU_XREG_OFFSET(29)]
306 .endm
307
308 .macro save_sp_el0 ctxt, tmp
309         mrs     \tmp,   sp_el0
310         str     \tmp,   [\ctxt, #CPU_SP_EL0_OFFSET]
311 .endm
312
313 .macro restore_sp_el0 ctxt, tmp
314         ldr     \tmp,     [\ctxt, #CPU_SP_EL0_OFFSET]
315         msr     sp_el0, \tmp
316 .endm
317
318 #endif
319
320 #endif /* __ARM_KVM_ASM_H__ */