xref: /linux/arch/arm64/include/asm/kvm_asm.h (revision 64dd3b6a79f0907d36de481b0f15fab323a53e5a)
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 #include <asm/sysreg.h>
14 
15 #define ARM_EXIT_WITH_SERROR_BIT  31
16 #define ARM_EXCEPTION_CODE(x)	  ((x) & ~(1U << ARM_EXIT_WITH_SERROR_BIT))
17 #define ARM_EXCEPTION_IS_TRAP(x)  (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP)
18 #define ARM_SERROR_PENDING(x)	  !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT))
19 
20 #define ARM_EXCEPTION_IRQ	  0
21 #define ARM_EXCEPTION_EL1_SERROR  1
22 #define ARM_EXCEPTION_TRAP	  2
23 #define ARM_EXCEPTION_IL	  3
24 /* The hyp-stub will return this for any kvm_call_hyp() call */
25 #define ARM_EXCEPTION_HYP_GONE	  HVC_STUB_ERR
26 
27 #define kvm_arm_exception_type					\
28 	{ARM_EXCEPTION_IRQ,		"IRQ"		},	\
29 	{ARM_EXCEPTION_EL1_SERROR, 	"SERROR"	},	\
30 	{ARM_EXCEPTION_TRAP, 		"TRAP"		},	\
31 	{ARM_EXCEPTION_HYP_GONE,	"HYP_GONE"	}
32 
33 /*
34  * Size of the HYP vectors preamble. kvm_patch_vector_branch() generates code
35  * that jumps over this.
36  */
37 #define KVM_VECTOR_PREAMBLE	(2 * AARCH64_INSN_SIZE)
38 
39 #define KVM_HOST_SMCCC_ID(id)						\
40 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
41 			   ARM_SMCCC_SMC_64,				\
42 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
43 			   (id))
44 
45 #define KVM_HOST_SMCCC_FUNC(name) KVM_HOST_SMCCC_ID(__KVM_HOST_SMCCC_FUNC_##name)
46 
47 #define __KVM_HOST_SMCCC_FUNC___kvm_hyp_init			0
48 
49 #ifndef __ASSEMBLY__
50 
51 #include <linux/mm.h>
52 
53 enum __kvm_host_smccc_func {
54 	/* Hypercalls available only prior to pKVM finalisation */
55 	/* __KVM_HOST_SMCCC_FUNC___kvm_hyp_init */
56 	__KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 = __KVM_HOST_SMCCC_FUNC___kvm_hyp_init + 1,
57 	__KVM_HOST_SMCCC_FUNC___pkvm_init,
58 	__KVM_HOST_SMCCC_FUNC___pkvm_create_private_mapping,
59 	__KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector,
60 	__KVM_HOST_SMCCC_FUNC___kvm_enable_ssbs,
61 	__KVM_HOST_SMCCC_FUNC___vgic_v3_init_lrs,
62 	__KVM_HOST_SMCCC_FUNC___vgic_v3_get_gic_config,
63 	__KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize,
64 
65 	/* Hypercalls available after pKVM finalisation */
66 	__KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp,
67 	__KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp,
68 	__KVM_HOST_SMCCC_FUNC___kvm_adjust_pc,
69 	__KVM_HOST_SMCCC_FUNC___kvm_vcpu_run,
70 	__KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
71 	__KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_ipa,
72 	__KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_ipa_nsh,
73 	__KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid,
74 	__KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_range,
75 	__KVM_HOST_SMCCC_FUNC___kvm_flush_cpu_context,
76 	__KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntvoff,
77 	__KVM_HOST_SMCCC_FUNC___vgic_v3_save_vmcr_aprs,
78 	__KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs,
79 	__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_init_traps,
80 	__KVM_HOST_SMCCC_FUNC___pkvm_init_vm,
81 	__KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu,
82 	__KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm,
83 };
84 
85 #define DECLARE_KVM_VHE_SYM(sym)	extern char sym[]
86 #define DECLARE_KVM_NVHE_SYM(sym)	extern char kvm_nvhe_sym(sym)[]
87 
88 /*
89  * Define a pair of symbols sharing the same name but one defined in
90  * VHE and the other in nVHE hyp implementations.
91  */
92 #define DECLARE_KVM_HYP_SYM(sym)		\
93 	DECLARE_KVM_VHE_SYM(sym);		\
94 	DECLARE_KVM_NVHE_SYM(sym)
95 
96 #define DECLARE_KVM_VHE_PER_CPU(type, sym)	\
97 	DECLARE_PER_CPU(type, sym)
98 #define DECLARE_KVM_NVHE_PER_CPU(type, sym)	\
99 	DECLARE_PER_CPU(type, kvm_nvhe_sym(sym))
100 
101 #define DECLARE_KVM_HYP_PER_CPU(type, sym)	\
102 	DECLARE_KVM_VHE_PER_CPU(type, sym);	\
103 	DECLARE_KVM_NVHE_PER_CPU(type, sym)
104 
105 /*
106  * Compute pointer to a symbol defined in nVHE percpu region.
107  * Returns NULL if percpu memory has not been allocated yet.
108  */
109 #define this_cpu_ptr_nvhe_sym(sym)	per_cpu_ptr_nvhe_sym(sym, smp_processor_id())
110 #define per_cpu_ptr_nvhe_sym(sym, cpu)						\
111 	({									\
112 		unsigned long base, off;					\
113 		base = kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];		\
114 		off = (unsigned long)&CHOOSE_NVHE_SYM(sym) -			\
115 		      (unsigned long)&CHOOSE_NVHE_SYM(__per_cpu_start);		\
116 		base ? (typeof(CHOOSE_NVHE_SYM(sym))*)(base + off) : NULL;	\
117 	})
118 
119 #if defined(__KVM_NVHE_HYPERVISOR__)
120 
121 #define CHOOSE_NVHE_SYM(sym)	sym
122 #define CHOOSE_HYP_SYM(sym)	CHOOSE_NVHE_SYM(sym)
123 
124 /* The nVHE hypervisor shouldn't even try to access VHE symbols */
125 extern void *__nvhe_undefined_symbol;
126 #define CHOOSE_VHE_SYM(sym)		__nvhe_undefined_symbol
127 #define this_cpu_ptr_hyp_sym(sym)	(&__nvhe_undefined_symbol)
128 #define per_cpu_ptr_hyp_sym(sym, cpu)	(&__nvhe_undefined_symbol)
129 
130 #elif defined(__KVM_VHE_HYPERVISOR__)
131 
132 #define CHOOSE_VHE_SYM(sym)	sym
133 #define CHOOSE_HYP_SYM(sym)	CHOOSE_VHE_SYM(sym)
134 
135 /* The VHE hypervisor shouldn't even try to access nVHE symbols */
136 extern void *__vhe_undefined_symbol;
137 #define CHOOSE_NVHE_SYM(sym)		__vhe_undefined_symbol
138 #define this_cpu_ptr_hyp_sym(sym)	(&__vhe_undefined_symbol)
139 #define per_cpu_ptr_hyp_sym(sym, cpu)	(&__vhe_undefined_symbol)
140 
141 #else
142 
143 /*
144  * BIG FAT WARNINGS:
145  *
146  * - Don't be tempted to change the following is_kernel_in_hyp_mode()
147  *   to has_vhe(). has_vhe() is implemented as a *final* capability,
148  *   while this is used early at boot time, when the capabilities are
149  *   not final yet....
150  *
151  * - Don't let the nVHE hypervisor have access to this, as it will
152  *   pick the *wrong* symbol (yes, it runs at EL2...).
153  */
154 #define CHOOSE_HYP_SYM(sym)		(is_kernel_in_hyp_mode()	\
155 					   ? CHOOSE_VHE_SYM(sym)	\
156 					   : CHOOSE_NVHE_SYM(sym))
157 
158 #define this_cpu_ptr_hyp_sym(sym)	(is_kernel_in_hyp_mode()	\
159 					   ? this_cpu_ptr(&sym)		\
160 					   : this_cpu_ptr_nvhe_sym(sym))
161 
162 #define per_cpu_ptr_hyp_sym(sym, cpu)	(is_kernel_in_hyp_mode()	\
163 					   ? per_cpu_ptr(&sym, cpu)	\
164 					   : per_cpu_ptr_nvhe_sym(sym, cpu))
165 
166 #define CHOOSE_VHE_SYM(sym)	sym
167 #define CHOOSE_NVHE_SYM(sym)	kvm_nvhe_sym(sym)
168 
169 #endif
170 
171 struct kvm_nvhe_init_params {
172 	unsigned long mair_el2;
173 	unsigned long tcr_el2;
174 	unsigned long tpidr_el2;
175 	unsigned long stack_hyp_va;
176 	unsigned long stack_pa;
177 	phys_addr_t pgd_pa;
178 	unsigned long hcr_el2;
179 	unsigned long vttbr;
180 	unsigned long vtcr;
181 };
182 
183 /*
184  * Used by the host in EL1 to dump the nVHE hypervisor backtrace on
185  * hyp_panic() in non-protected mode.
186  *
187  * @stack_base:                 hyp VA of the hyp_stack base.
188  * @overflow_stack_base:        hyp VA of the hyp_overflow_stack base.
189  * @fp:                         hyp FP where the backtrace begins.
190  * @pc:                         hyp PC where the backtrace begins.
191  */
192 struct kvm_nvhe_stacktrace_info {
193 	unsigned long stack_base;
194 	unsigned long overflow_stack_base;
195 	unsigned long fp;
196 	unsigned long pc;
197 };
198 
199 /* Translate a kernel address @ptr into its equivalent linear mapping */
200 #define kvm_ksym_ref(ptr)						\
201 	({								\
202 		void *val = (ptr);					\
203 		if (!is_kernel_in_hyp_mode())				\
204 			val = lm_alias((ptr));				\
205 		val;							\
206 	 })
207 #define kvm_ksym_ref_nvhe(sym)	kvm_ksym_ref(kvm_nvhe_sym(sym))
208 
209 struct kvm;
210 struct kvm_vcpu;
211 struct kvm_s2_mmu;
212 
213 DECLARE_KVM_NVHE_SYM(__kvm_hyp_init);
214 DECLARE_KVM_HYP_SYM(__kvm_hyp_vector);
215 #define __kvm_hyp_init		CHOOSE_NVHE_SYM(__kvm_hyp_init)
216 #define __kvm_hyp_vector	CHOOSE_HYP_SYM(__kvm_hyp_vector)
217 
218 extern unsigned long kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[];
219 DECLARE_KVM_NVHE_SYM(__per_cpu_start);
220 DECLARE_KVM_NVHE_SYM(__per_cpu_end);
221 
222 DECLARE_KVM_HYP_SYM(__bp_harden_hyp_vecs);
223 #define __bp_harden_hyp_vecs	CHOOSE_HYP_SYM(__bp_harden_hyp_vecs)
224 
225 extern void __kvm_flush_vm_context(void);
226 extern void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu);
227 extern void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa,
228 				     int level);
229 extern void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,
230 					 phys_addr_t ipa,
231 					 int level);
232 extern void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
233 					phys_addr_t start, unsigned long pages);
234 extern void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu);
235 
236 extern int __kvm_tlbi_s1e2(struct kvm_s2_mmu *mmu, u64 va, u64 sys_encoding);
237 
238 extern void __kvm_timer_set_cntvoff(u64 cntvoff);
239 extern void __kvm_at_s1e01(struct kvm_vcpu *vcpu, u32 op, u64 vaddr);
240 extern void __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr);
241 extern void __kvm_at_s12(struct kvm_vcpu *vcpu, u32 op, u64 vaddr);
242 
243 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
244 
245 extern void __kvm_adjust_pc(struct kvm_vcpu *vcpu);
246 
247 extern u64 __vgic_v3_get_gic_config(void);
248 extern void __vgic_v3_init_lrs(void);
249 
250 extern u64 __kvm_get_mdcr_el2(void);
251 
252 #define __KVM_EXTABLE(from, to)						\
253 	"	.pushsection	__kvm_ex_table, \"a\"\n"		\
254 	"	.align		3\n"					\
255 	"	.long		(" #from " - .), (" #to " - .)\n"	\
256 	"	.popsection\n"
257 
258 
259 #define __kvm_at(at_op, addr)						\
260 ( { 									\
261 	int __kvm_at_err = 0;						\
262 	u64 spsr, elr;							\
263 	asm volatile(							\
264 	"	mrs	%1, spsr_el2\n"					\
265 	"	mrs	%2, elr_el2\n"					\
266 	"1:	" __msr_s(at_op, "%3") "\n"				\
267 	"	isb\n"							\
268 	"	b	9f\n"						\
269 	"2:	msr	spsr_el2, %1\n"					\
270 	"	msr	elr_el2, %2\n"					\
271 	"	mov	%w0, %4\n"					\
272 	"9:\n"								\
273 	__KVM_EXTABLE(1b, 2b)						\
274 	: "+r" (__kvm_at_err), "=&r" (spsr), "=&r" (elr)		\
275 	: "r" (addr), "i" (-EFAULT));					\
276 	__kvm_at_err;							\
277 } )
278 
279 void __noreturn hyp_panic(void);
280 asmlinkage void kvm_unexpected_el2_exception(void);
281 asmlinkage void __noreturn hyp_panic(void);
282 asmlinkage void __noreturn hyp_panic_bad_stack(void);
283 asmlinkage void kvm_unexpected_el2_exception(void);
284 struct kvm_cpu_context;
285 void handle_trap(struct kvm_cpu_context *host_ctxt);
286 asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on);
287 void __noreturn __pkvm_init_finalise(void);
288 void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc);
289 void kvm_patch_vector_branch(struct alt_instr *alt,
290 	__le32 *origptr, __le32 *updptr, int nr_inst);
291 void kvm_get_kimage_voffset(struct alt_instr *alt,
292 	__le32 *origptr, __le32 *updptr, int nr_inst);
293 void kvm_compute_final_ctr_el0(struct alt_instr *alt,
294 	__le32 *origptr, __le32 *updptr, int nr_inst);
295 void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, u64 elr_virt,
296 	u64 elr_phys, u64 par, uintptr_t vcpu, u64 far, u64 hpfar);
297 
298 #else /* __ASSEMBLY__ */
299 
300 .macro get_host_ctxt reg, tmp
301 	adr_this_cpu \reg, kvm_host_data, \tmp
302 	add	\reg, \reg, #HOST_DATA_CONTEXT
303 .endm
304 
305 .macro get_vcpu_ptr vcpu, ctxt
306 	get_host_ctxt \ctxt, \vcpu
307 	ldr	\vcpu, [\ctxt, #HOST_CONTEXT_VCPU]
308 .endm
309 
310 .macro get_loaded_vcpu vcpu, ctxt
311 	adr_this_cpu \ctxt, kvm_hyp_ctxt, \vcpu
312 	ldr	\vcpu, [\ctxt, #HOST_CONTEXT_VCPU]
313 .endm
314 
315 .macro set_loaded_vcpu vcpu, ctxt, tmp
316 	adr_this_cpu \ctxt, kvm_hyp_ctxt, \tmp
317 	str	\vcpu, [\ctxt, #HOST_CONTEXT_VCPU]
318 .endm
319 
320 /*
321  * KVM extable for unexpected exceptions.
322  * Create a struct kvm_exception_table_entry output to a section that can be
323  * mapped by EL2. The table is not sorted.
324  *
325  * The caller must ensure:
326  * x18 has the hypervisor value to allow any Shadow-Call-Stack instrumented
327  * code to write to it, and that SPSR_EL2 and ELR_EL2 are restored by the fixup.
328  */
329 .macro	_kvm_extable, from, to
330 	.pushsection	__kvm_ex_table, "a"
331 	.align		3
332 	.long		(\from - .), (\to - .)
333 	.popsection
334 .endm
335 
336 #define CPU_XREG_OFFSET(x)	(CPU_USER_PT_REGS + 8*x)
337 #define CPU_LR_OFFSET		CPU_XREG_OFFSET(30)
338 #define CPU_SP_EL0_OFFSET	(CPU_LR_OFFSET + 8)
339 
340 /*
341  * We treat x18 as callee-saved as the host may use it as a platform
342  * register (e.g. for shadow call stack).
343  */
344 .macro save_callee_saved_regs ctxt
345 	str	x18,      [\ctxt, #CPU_XREG_OFFSET(18)]
346 	stp	x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
347 	stp	x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
348 	stp	x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
349 	stp	x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
350 	stp	x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
351 	stp	x29, lr,  [\ctxt, #CPU_XREG_OFFSET(29)]
352 .endm
353 
354 .macro restore_callee_saved_regs ctxt
355 	// We require \ctxt is not x18-x28
356 	ldr	x18,      [\ctxt, #CPU_XREG_OFFSET(18)]
357 	ldp	x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
358 	ldp	x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
359 	ldp	x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
360 	ldp	x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
361 	ldp	x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
362 	ldp	x29, lr,  [\ctxt, #CPU_XREG_OFFSET(29)]
363 .endm
364 
365 .macro save_sp_el0 ctxt, tmp
366 	mrs	\tmp,	sp_el0
367 	str	\tmp,	[\ctxt, #CPU_SP_EL0_OFFSET]
368 .endm
369 
370 .macro restore_sp_el0 ctxt, tmp
371 	ldr	\tmp,	  [\ctxt, #CPU_SP_EL0_OFFSET]
372 	msr	sp_el0, \tmp
373 .endm
374 
375 #endif
376 
377 #endif /* __ARM_KVM_ASM_H__ */
378