xref: /linux/arch/arm64/kvm/hyp/nvhe/hyp-main.c (revision 06a130e42a5bfc84795464bff023bff4c16f58c5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 - Google Inc
4  * Author: Andrew Scull <ascull@google.com>
5  */
6 
7 #include <hyp/adjust_pc.h>
8 
9 #include <asm/pgtable-types.h>
10 #include <asm/kvm_asm.h>
11 #include <asm/kvm_emulate.h>
12 #include <asm/kvm_host.h>
13 #include <asm/kvm_hyp.h>
14 #include <asm/kvm_mmu.h>
15 
16 #include <nvhe/ffa.h>
17 #include <nvhe/mem_protect.h>
18 #include <nvhe/mm.h>
19 #include <nvhe/pkvm.h>
20 #include <nvhe/trap_handler.h>
21 
22 DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
23 
24 void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);
25 
26 static void __hyp_sve_save_guest(struct kvm_vcpu *vcpu)
27 {
28 	__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
29 	/*
30 	 * On saving/restoring guest sve state, always use the maximum VL for
31 	 * the guest. The layout of the data when saving the sve state depends
32 	 * on the VL, so use a consistent (i.e., the maximum) guest VL.
33 	 */
34 	sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2);
35 	__sve_save_state(vcpu_sve_pffr(vcpu), &vcpu->arch.ctxt.fp_regs.fpsr, true);
36 	write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
37 }
38 
39 static void __hyp_sve_restore_host(void)
40 {
41 	struct cpu_sve_state *sve_state = *host_data_ptr(sve_state);
42 
43 	/*
44 	 * On saving/restoring host sve state, always use the maximum VL for
45 	 * the host. The layout of the data when saving the sve state depends
46 	 * on the VL, so use a consistent (i.e., the maximum) host VL.
47 	 *
48 	 * Note that this constrains the PE to the maximum shared VL
49 	 * that was discovered, if we wish to use larger VLs this will
50 	 * need to be revisited.
51 	 */
52 	write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
53 	__sve_restore_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl),
54 			    &sve_state->fpsr,
55 			    true);
56 	write_sysreg_el1(sve_state->zcr_el1, SYS_ZCR);
57 }
58 
59 static void fpsimd_sve_flush(void)
60 {
61 	*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
62 }
63 
64 static void fpsimd_sve_sync(struct kvm_vcpu *vcpu)
65 {
66 	bool has_fpmr;
67 
68 	if (!guest_owns_fp_regs())
69 		return;
70 
71 	cpacr_clear_set(0, CPACR_ELx_FPEN | CPACR_ELx_ZEN);
72 	isb();
73 
74 	if (vcpu_has_sve(vcpu))
75 		__hyp_sve_save_guest(vcpu);
76 	else
77 		__fpsimd_save_state(&vcpu->arch.ctxt.fp_regs);
78 
79 	has_fpmr = kvm_has_fpmr(kern_hyp_va(vcpu->kvm));
80 	if (has_fpmr)
81 		__vcpu_sys_reg(vcpu, FPMR) = read_sysreg_s(SYS_FPMR);
82 
83 	if (system_supports_sve())
84 		__hyp_sve_restore_host();
85 	else
86 		__fpsimd_restore_state(*host_data_ptr(fpsimd_state));
87 
88 	if (has_fpmr)
89 		write_sysreg_s(*host_data_ptr(fpmr), SYS_FPMR);
90 
91 	*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
92 }
93 
94 static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
95 {
96 	struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
97 
98 	fpsimd_sve_flush();
99 
100 	hyp_vcpu->vcpu.arch.ctxt	= host_vcpu->arch.ctxt;
101 
102 	hyp_vcpu->vcpu.arch.sve_state	= kern_hyp_va(host_vcpu->arch.sve_state);
103 	/* Limit guest vector length to the maximum supported by the host.  */
104 	hyp_vcpu->vcpu.arch.sve_max_vl	= min(host_vcpu->arch.sve_max_vl, kvm_host_sve_max_vl);
105 
106 	hyp_vcpu->vcpu.arch.hw_mmu	= host_vcpu->arch.hw_mmu;
107 
108 	hyp_vcpu->vcpu.arch.hcr_el2	= host_vcpu->arch.hcr_el2;
109 	hyp_vcpu->vcpu.arch.mdcr_el2	= host_vcpu->arch.mdcr_el2;
110 
111 	hyp_vcpu->vcpu.arch.iflags	= host_vcpu->arch.iflags;
112 
113 	hyp_vcpu->vcpu.arch.debug_ptr	= kern_hyp_va(host_vcpu->arch.debug_ptr);
114 
115 	hyp_vcpu->vcpu.arch.vsesr_el2	= host_vcpu->arch.vsesr_el2;
116 
117 	hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
118 }
119 
120 static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
121 {
122 	struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
123 	struct vgic_v3_cpu_if *hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3;
124 	struct vgic_v3_cpu_if *host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3;
125 	unsigned int i;
126 
127 	fpsimd_sve_sync(&hyp_vcpu->vcpu);
128 
129 	host_vcpu->arch.ctxt		= hyp_vcpu->vcpu.arch.ctxt;
130 
131 	host_vcpu->arch.hcr_el2		= hyp_vcpu->vcpu.arch.hcr_el2;
132 
133 	host_vcpu->arch.fault		= hyp_vcpu->vcpu.arch.fault;
134 
135 	host_vcpu->arch.iflags		= hyp_vcpu->vcpu.arch.iflags;
136 
137 	host_cpu_if->vgic_hcr		= hyp_cpu_if->vgic_hcr;
138 	for (i = 0; i < hyp_cpu_if->used_lrs; ++i)
139 		host_cpu_if->vgic_lr[i] = hyp_cpu_if->vgic_lr[i];
140 }
141 
142 static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)
143 {
144 	DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 1);
145 	int ret;
146 
147 	host_vcpu = kern_hyp_va(host_vcpu);
148 
149 	if (unlikely(is_protected_kvm_enabled())) {
150 		struct pkvm_hyp_vcpu *hyp_vcpu;
151 		struct kvm *host_kvm;
152 
153 		/*
154 		 * KVM (and pKVM) doesn't support SME guests for now, and
155 		 * ensures that SME features aren't enabled in pstate when
156 		 * loading a vcpu. Therefore, if SME features enabled the host
157 		 * is misbehaving.
158 		 */
159 		if (unlikely(system_supports_sme() && read_sysreg_s(SYS_SVCR))) {
160 			ret = -EINVAL;
161 			goto out;
162 		}
163 
164 		host_kvm = kern_hyp_va(host_vcpu->kvm);
165 		hyp_vcpu = pkvm_load_hyp_vcpu(host_kvm->arch.pkvm.handle,
166 					      host_vcpu->vcpu_idx);
167 		if (!hyp_vcpu) {
168 			ret = -EINVAL;
169 			goto out;
170 		}
171 
172 		flush_hyp_vcpu(hyp_vcpu);
173 
174 		ret = __kvm_vcpu_run(&hyp_vcpu->vcpu);
175 
176 		sync_hyp_vcpu(hyp_vcpu);
177 		pkvm_put_hyp_vcpu(hyp_vcpu);
178 	} else {
179 		/* The host is fully trusted, run its vCPU directly. */
180 		ret = __kvm_vcpu_run(host_vcpu);
181 	}
182 
183 out:
184 	cpu_reg(host_ctxt, 1) =  ret;
185 }
186 
187 static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt)
188 {
189 	DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
190 
191 	__kvm_adjust_pc(kern_hyp_va(vcpu));
192 }
193 
194 static void handle___kvm_flush_vm_context(struct kvm_cpu_context *host_ctxt)
195 {
196 	__kvm_flush_vm_context();
197 }
198 
199 static void handle___kvm_tlb_flush_vmid_ipa(struct kvm_cpu_context *host_ctxt)
200 {
201 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
202 	DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2);
203 	DECLARE_REG(int, level, host_ctxt, 3);
204 
205 	__kvm_tlb_flush_vmid_ipa(kern_hyp_va(mmu), ipa, level);
206 }
207 
208 static void handle___kvm_tlb_flush_vmid_ipa_nsh(struct kvm_cpu_context *host_ctxt)
209 {
210 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
211 	DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2);
212 	DECLARE_REG(int, level, host_ctxt, 3);
213 
214 	__kvm_tlb_flush_vmid_ipa_nsh(kern_hyp_va(mmu), ipa, level);
215 }
216 
217 static void
218 handle___kvm_tlb_flush_vmid_range(struct kvm_cpu_context *host_ctxt)
219 {
220 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
221 	DECLARE_REG(phys_addr_t, start, host_ctxt, 2);
222 	DECLARE_REG(unsigned long, pages, host_ctxt, 3);
223 
224 	__kvm_tlb_flush_vmid_range(kern_hyp_va(mmu), start, pages);
225 }
226 
227 static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)
228 {
229 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
230 
231 	__kvm_tlb_flush_vmid(kern_hyp_va(mmu));
232 }
233 
234 static void handle___kvm_flush_cpu_context(struct kvm_cpu_context *host_ctxt)
235 {
236 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
237 
238 	__kvm_flush_cpu_context(kern_hyp_va(mmu));
239 }
240 
241 static void handle___kvm_timer_set_cntvoff(struct kvm_cpu_context *host_ctxt)
242 {
243 	__kvm_timer_set_cntvoff(cpu_reg(host_ctxt, 1));
244 }
245 
246 static void handle___kvm_enable_ssbs(struct kvm_cpu_context *host_ctxt)
247 {
248 	u64 tmp;
249 
250 	tmp = read_sysreg_el2(SYS_SCTLR);
251 	tmp |= SCTLR_ELx_DSSBS;
252 	write_sysreg_el2(tmp, SYS_SCTLR);
253 }
254 
255 static void handle___vgic_v3_get_gic_config(struct kvm_cpu_context *host_ctxt)
256 {
257 	cpu_reg(host_ctxt, 1) = __vgic_v3_get_gic_config();
258 }
259 
260 static void handle___vgic_v3_init_lrs(struct kvm_cpu_context *host_ctxt)
261 {
262 	__vgic_v3_init_lrs();
263 }
264 
265 static void handle___kvm_get_mdcr_el2(struct kvm_cpu_context *host_ctxt)
266 {
267 	cpu_reg(host_ctxt, 1) = __kvm_get_mdcr_el2();
268 }
269 
270 static void handle___vgic_v3_save_vmcr_aprs(struct kvm_cpu_context *host_ctxt)
271 {
272 	DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1);
273 
274 	__vgic_v3_save_vmcr_aprs(kern_hyp_va(cpu_if));
275 }
276 
277 static void handle___vgic_v3_restore_vmcr_aprs(struct kvm_cpu_context *host_ctxt)
278 {
279 	DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1);
280 
281 	__vgic_v3_restore_vmcr_aprs(kern_hyp_va(cpu_if));
282 }
283 
284 static void handle___pkvm_init(struct kvm_cpu_context *host_ctxt)
285 {
286 	DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
287 	DECLARE_REG(unsigned long, size, host_ctxt, 2);
288 	DECLARE_REG(unsigned long, nr_cpus, host_ctxt, 3);
289 	DECLARE_REG(unsigned long *, per_cpu_base, host_ctxt, 4);
290 	DECLARE_REG(u32, hyp_va_bits, host_ctxt, 5);
291 
292 	/*
293 	 * __pkvm_init() will return only if an error occurred, otherwise it
294 	 * will tail-call in __pkvm_init_finalise() which will have to deal
295 	 * with the host context directly.
296 	 */
297 	cpu_reg(host_ctxt, 1) = __pkvm_init(phys, size, nr_cpus, per_cpu_base,
298 					    hyp_va_bits);
299 }
300 
301 static void handle___pkvm_cpu_set_vector(struct kvm_cpu_context *host_ctxt)
302 {
303 	DECLARE_REG(enum arm64_hyp_spectre_vector, slot, host_ctxt, 1);
304 
305 	cpu_reg(host_ctxt, 1) = pkvm_cpu_set_vector(slot);
306 }
307 
308 static void handle___pkvm_host_share_hyp(struct kvm_cpu_context *host_ctxt)
309 {
310 	DECLARE_REG(u64, pfn, host_ctxt, 1);
311 
312 	cpu_reg(host_ctxt, 1) = __pkvm_host_share_hyp(pfn);
313 }
314 
315 static void handle___pkvm_host_unshare_hyp(struct kvm_cpu_context *host_ctxt)
316 {
317 	DECLARE_REG(u64, pfn, host_ctxt, 1);
318 
319 	cpu_reg(host_ctxt, 1) = __pkvm_host_unshare_hyp(pfn);
320 }
321 
322 static void handle___pkvm_create_private_mapping(struct kvm_cpu_context *host_ctxt)
323 {
324 	DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
325 	DECLARE_REG(size_t, size, host_ctxt, 2);
326 	DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3);
327 
328 	/*
329 	 * __pkvm_create_private_mapping() populates a pointer with the
330 	 * hypervisor start address of the allocation.
331 	 *
332 	 * However, handle___pkvm_create_private_mapping() hypercall crosses the
333 	 * EL1/EL2 boundary so the pointer would not be valid in this context.
334 	 *
335 	 * Instead pass the allocation address as the return value (or return
336 	 * ERR_PTR() on failure).
337 	 */
338 	unsigned long haddr;
339 	int err = __pkvm_create_private_mapping(phys, size, prot, &haddr);
340 
341 	if (err)
342 		haddr = (unsigned long)ERR_PTR(err);
343 
344 	cpu_reg(host_ctxt, 1) = haddr;
345 }
346 
347 static void handle___pkvm_prot_finalize(struct kvm_cpu_context *host_ctxt)
348 {
349 	cpu_reg(host_ctxt, 1) = __pkvm_prot_finalize();
350 }
351 
352 static void handle___pkvm_vcpu_init_traps(struct kvm_cpu_context *host_ctxt)
353 {
354 	DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
355 
356 	__pkvm_vcpu_init_traps(kern_hyp_va(vcpu));
357 }
358 
359 static void handle___pkvm_init_vm(struct kvm_cpu_context *host_ctxt)
360 {
361 	DECLARE_REG(struct kvm *, host_kvm, host_ctxt, 1);
362 	DECLARE_REG(unsigned long, vm_hva, host_ctxt, 2);
363 	DECLARE_REG(unsigned long, pgd_hva, host_ctxt, 3);
364 
365 	host_kvm = kern_hyp_va(host_kvm);
366 	cpu_reg(host_ctxt, 1) = __pkvm_init_vm(host_kvm, vm_hva, pgd_hva);
367 }
368 
369 static void handle___pkvm_init_vcpu(struct kvm_cpu_context *host_ctxt)
370 {
371 	DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
372 	DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 2);
373 	DECLARE_REG(unsigned long, vcpu_hva, host_ctxt, 3);
374 
375 	host_vcpu = kern_hyp_va(host_vcpu);
376 	cpu_reg(host_ctxt, 1) = __pkvm_init_vcpu(handle, host_vcpu, vcpu_hva);
377 }
378 
379 static void handle___pkvm_teardown_vm(struct kvm_cpu_context *host_ctxt)
380 {
381 	DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
382 
383 	cpu_reg(host_ctxt, 1) = __pkvm_teardown_vm(handle);
384 }
385 
386 typedef void (*hcall_t)(struct kvm_cpu_context *);
387 
388 #define HANDLE_FUNC(x)	[__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x
389 
390 static const hcall_t host_hcall[] = {
391 	/* ___kvm_hyp_init */
392 	HANDLE_FUNC(__kvm_get_mdcr_el2),
393 	HANDLE_FUNC(__pkvm_init),
394 	HANDLE_FUNC(__pkvm_create_private_mapping),
395 	HANDLE_FUNC(__pkvm_cpu_set_vector),
396 	HANDLE_FUNC(__kvm_enable_ssbs),
397 	HANDLE_FUNC(__vgic_v3_init_lrs),
398 	HANDLE_FUNC(__vgic_v3_get_gic_config),
399 	HANDLE_FUNC(__pkvm_prot_finalize),
400 
401 	HANDLE_FUNC(__pkvm_host_share_hyp),
402 	HANDLE_FUNC(__pkvm_host_unshare_hyp),
403 	HANDLE_FUNC(__kvm_adjust_pc),
404 	HANDLE_FUNC(__kvm_vcpu_run),
405 	HANDLE_FUNC(__kvm_flush_vm_context),
406 	HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa),
407 	HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa_nsh),
408 	HANDLE_FUNC(__kvm_tlb_flush_vmid),
409 	HANDLE_FUNC(__kvm_tlb_flush_vmid_range),
410 	HANDLE_FUNC(__kvm_flush_cpu_context),
411 	HANDLE_FUNC(__kvm_timer_set_cntvoff),
412 	HANDLE_FUNC(__vgic_v3_save_vmcr_aprs),
413 	HANDLE_FUNC(__vgic_v3_restore_vmcr_aprs),
414 	HANDLE_FUNC(__pkvm_vcpu_init_traps),
415 	HANDLE_FUNC(__pkvm_init_vm),
416 	HANDLE_FUNC(__pkvm_init_vcpu),
417 	HANDLE_FUNC(__pkvm_teardown_vm),
418 };
419 
420 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
421 {
422 	DECLARE_REG(unsigned long, id, host_ctxt, 0);
423 	unsigned long hcall_min = 0;
424 	hcall_t hfn;
425 
426 	/*
427 	 * If pKVM has been initialised then reject any calls to the
428 	 * early "privileged" hypercalls. Note that we cannot reject
429 	 * calls to __pkvm_prot_finalize for two reasons: (1) The static
430 	 * key used to determine initialisation must be toggled prior to
431 	 * finalisation and (2) finalisation is performed on a per-CPU
432 	 * basis. This is all fine, however, since __pkvm_prot_finalize
433 	 * returns -EPERM after the first call for a given CPU.
434 	 */
435 	if (static_branch_unlikely(&kvm_protected_mode_initialized))
436 		hcall_min = __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize;
437 
438 	id &= ~ARM_SMCCC_CALL_HINTS;
439 	id -= KVM_HOST_SMCCC_ID(0);
440 
441 	if (unlikely(id < hcall_min || id >= ARRAY_SIZE(host_hcall)))
442 		goto inval;
443 
444 	hfn = host_hcall[id];
445 	if (unlikely(!hfn))
446 		goto inval;
447 
448 	cpu_reg(host_ctxt, 0) = SMCCC_RET_SUCCESS;
449 	hfn(host_ctxt);
450 
451 	return;
452 inval:
453 	cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
454 }
455 
456 static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt)
457 {
458 	__kvm_hyp_host_forward_smc(host_ctxt);
459 }
460 
461 static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
462 {
463 	DECLARE_REG(u64, func_id, host_ctxt, 0);
464 	bool handled;
465 
466 	func_id &= ~ARM_SMCCC_CALL_HINTS;
467 
468 	handled = kvm_host_psci_handler(host_ctxt, func_id);
469 	if (!handled)
470 		handled = kvm_host_ffa_handler(host_ctxt, func_id);
471 	if (!handled)
472 		default_host_smc_handler(host_ctxt);
473 
474 	/* SMC was trapped, move ELR past the current PC. */
475 	kvm_skip_host_instr();
476 }
477 
478 void handle_trap(struct kvm_cpu_context *host_ctxt)
479 {
480 	u64 esr = read_sysreg_el2(SYS_ESR);
481 
482 	switch (ESR_ELx_EC(esr)) {
483 	case ESR_ELx_EC_HVC64:
484 		handle_host_hcall(host_ctxt);
485 		break;
486 	case ESR_ELx_EC_SMC64:
487 		handle_host_smc(host_ctxt);
488 		break;
489 	case ESR_ELx_EC_SVE:
490 		cpacr_clear_set(0, CPACR_ELx_ZEN);
491 		isb();
492 		sve_cond_update_zcr_vq(sve_vq_from_vl(kvm_host_sve_max_vl) - 1,
493 				       SYS_ZCR_EL2);
494 		break;
495 	case ESR_ELx_EC_IABT_LOW:
496 	case ESR_ELx_EC_DABT_LOW:
497 		handle_host_mem_abort(host_ctxt);
498 		break;
499 	default:
500 		BUG();
501 	}
502 }
503