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