xref: /openbmc/linux/arch/arm64/kvm/hyp/nvhe/hyp-main.c (revision e8069f5a)
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 flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
27 {
28 	struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
29 
30 	hyp_vcpu->vcpu.arch.ctxt	= host_vcpu->arch.ctxt;
31 
32 	hyp_vcpu->vcpu.arch.sve_state	= kern_hyp_va(host_vcpu->arch.sve_state);
33 	hyp_vcpu->vcpu.arch.sve_max_vl	= host_vcpu->arch.sve_max_vl;
34 
35 	hyp_vcpu->vcpu.arch.hw_mmu	= host_vcpu->arch.hw_mmu;
36 
37 	hyp_vcpu->vcpu.arch.hcr_el2	= host_vcpu->arch.hcr_el2;
38 	hyp_vcpu->vcpu.arch.mdcr_el2	= host_vcpu->arch.mdcr_el2;
39 	hyp_vcpu->vcpu.arch.cptr_el2	= host_vcpu->arch.cptr_el2;
40 
41 	hyp_vcpu->vcpu.arch.iflags	= host_vcpu->arch.iflags;
42 	hyp_vcpu->vcpu.arch.fp_state	= host_vcpu->arch.fp_state;
43 
44 	hyp_vcpu->vcpu.arch.debug_ptr	= kern_hyp_va(host_vcpu->arch.debug_ptr);
45 	hyp_vcpu->vcpu.arch.host_fpsimd_state = host_vcpu->arch.host_fpsimd_state;
46 
47 	hyp_vcpu->vcpu.arch.vsesr_el2	= host_vcpu->arch.vsesr_el2;
48 
49 	hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
50 }
51 
52 static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
53 {
54 	struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
55 	struct vgic_v3_cpu_if *hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3;
56 	struct vgic_v3_cpu_if *host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3;
57 	unsigned int i;
58 
59 	host_vcpu->arch.ctxt		= hyp_vcpu->vcpu.arch.ctxt;
60 
61 	host_vcpu->arch.hcr_el2		= hyp_vcpu->vcpu.arch.hcr_el2;
62 	host_vcpu->arch.cptr_el2	= hyp_vcpu->vcpu.arch.cptr_el2;
63 
64 	host_vcpu->arch.fault		= hyp_vcpu->vcpu.arch.fault;
65 
66 	host_vcpu->arch.iflags		= hyp_vcpu->vcpu.arch.iflags;
67 	host_vcpu->arch.fp_state	= hyp_vcpu->vcpu.arch.fp_state;
68 
69 	host_cpu_if->vgic_hcr		= hyp_cpu_if->vgic_hcr;
70 	for (i = 0; i < hyp_cpu_if->used_lrs; ++i)
71 		host_cpu_if->vgic_lr[i] = hyp_cpu_if->vgic_lr[i];
72 }
73 
74 static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)
75 {
76 	DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 1);
77 	int ret;
78 
79 	host_vcpu = kern_hyp_va(host_vcpu);
80 
81 	if (unlikely(is_protected_kvm_enabled())) {
82 		struct pkvm_hyp_vcpu *hyp_vcpu;
83 		struct kvm *host_kvm;
84 
85 		host_kvm = kern_hyp_va(host_vcpu->kvm);
86 		hyp_vcpu = pkvm_load_hyp_vcpu(host_kvm->arch.pkvm.handle,
87 					      host_vcpu->vcpu_idx);
88 		if (!hyp_vcpu) {
89 			ret = -EINVAL;
90 			goto out;
91 		}
92 
93 		flush_hyp_vcpu(hyp_vcpu);
94 
95 		ret = __kvm_vcpu_run(&hyp_vcpu->vcpu);
96 
97 		sync_hyp_vcpu(hyp_vcpu);
98 		pkvm_put_hyp_vcpu(hyp_vcpu);
99 	} else {
100 		/* The host is fully trusted, run its vCPU directly. */
101 		ret = __kvm_vcpu_run(host_vcpu);
102 	}
103 
104 out:
105 	cpu_reg(host_ctxt, 1) =  ret;
106 }
107 
108 static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt)
109 {
110 	DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
111 
112 	__kvm_adjust_pc(kern_hyp_va(vcpu));
113 }
114 
115 static void handle___kvm_flush_vm_context(struct kvm_cpu_context *host_ctxt)
116 {
117 	__kvm_flush_vm_context();
118 }
119 
120 static void handle___kvm_tlb_flush_vmid_ipa(struct kvm_cpu_context *host_ctxt)
121 {
122 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
123 	DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2);
124 	DECLARE_REG(int, level, host_ctxt, 3);
125 
126 	__kvm_tlb_flush_vmid_ipa(kern_hyp_va(mmu), ipa, level);
127 }
128 
129 static void handle___kvm_tlb_flush_vmid_ipa_nsh(struct kvm_cpu_context *host_ctxt)
130 {
131 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
132 	DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2);
133 	DECLARE_REG(int, level, host_ctxt, 3);
134 
135 	__kvm_tlb_flush_vmid_ipa_nsh(kern_hyp_va(mmu), ipa, level);
136 }
137 
138 static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)
139 {
140 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
141 
142 	__kvm_tlb_flush_vmid(kern_hyp_va(mmu));
143 }
144 
145 static void handle___kvm_flush_cpu_context(struct kvm_cpu_context *host_ctxt)
146 {
147 	DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
148 
149 	__kvm_flush_cpu_context(kern_hyp_va(mmu));
150 }
151 
152 static void handle___kvm_timer_set_cntvoff(struct kvm_cpu_context *host_ctxt)
153 {
154 	__kvm_timer_set_cntvoff(cpu_reg(host_ctxt, 1));
155 }
156 
157 static void handle___kvm_enable_ssbs(struct kvm_cpu_context *host_ctxt)
158 {
159 	u64 tmp;
160 
161 	tmp = read_sysreg_el2(SYS_SCTLR);
162 	tmp |= SCTLR_ELx_DSSBS;
163 	write_sysreg_el2(tmp, SYS_SCTLR);
164 }
165 
166 static void handle___vgic_v3_get_gic_config(struct kvm_cpu_context *host_ctxt)
167 {
168 	cpu_reg(host_ctxt, 1) = __vgic_v3_get_gic_config();
169 }
170 
171 static void handle___vgic_v3_read_vmcr(struct kvm_cpu_context *host_ctxt)
172 {
173 	cpu_reg(host_ctxt, 1) = __vgic_v3_read_vmcr();
174 }
175 
176 static void handle___vgic_v3_write_vmcr(struct kvm_cpu_context *host_ctxt)
177 {
178 	__vgic_v3_write_vmcr(cpu_reg(host_ctxt, 1));
179 }
180 
181 static void handle___vgic_v3_init_lrs(struct kvm_cpu_context *host_ctxt)
182 {
183 	__vgic_v3_init_lrs();
184 }
185 
186 static void handle___kvm_get_mdcr_el2(struct kvm_cpu_context *host_ctxt)
187 {
188 	cpu_reg(host_ctxt, 1) = __kvm_get_mdcr_el2();
189 }
190 
191 static void handle___vgic_v3_save_aprs(struct kvm_cpu_context *host_ctxt)
192 {
193 	DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1);
194 
195 	__vgic_v3_save_aprs(kern_hyp_va(cpu_if));
196 }
197 
198 static void handle___vgic_v3_restore_aprs(struct kvm_cpu_context *host_ctxt)
199 {
200 	DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1);
201 
202 	__vgic_v3_restore_aprs(kern_hyp_va(cpu_if));
203 }
204 
205 static void handle___pkvm_init(struct kvm_cpu_context *host_ctxt)
206 {
207 	DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
208 	DECLARE_REG(unsigned long, size, host_ctxt, 2);
209 	DECLARE_REG(unsigned long, nr_cpus, host_ctxt, 3);
210 	DECLARE_REG(unsigned long *, per_cpu_base, host_ctxt, 4);
211 	DECLARE_REG(u32, hyp_va_bits, host_ctxt, 5);
212 
213 	/*
214 	 * __pkvm_init() will return only if an error occurred, otherwise it
215 	 * will tail-call in __pkvm_init_finalise() which will have to deal
216 	 * with the host context directly.
217 	 */
218 	cpu_reg(host_ctxt, 1) = __pkvm_init(phys, size, nr_cpus, per_cpu_base,
219 					    hyp_va_bits);
220 }
221 
222 static void handle___pkvm_cpu_set_vector(struct kvm_cpu_context *host_ctxt)
223 {
224 	DECLARE_REG(enum arm64_hyp_spectre_vector, slot, host_ctxt, 1);
225 
226 	cpu_reg(host_ctxt, 1) = pkvm_cpu_set_vector(slot);
227 }
228 
229 static void handle___pkvm_host_share_hyp(struct kvm_cpu_context *host_ctxt)
230 {
231 	DECLARE_REG(u64, pfn, host_ctxt, 1);
232 
233 	cpu_reg(host_ctxt, 1) = __pkvm_host_share_hyp(pfn);
234 }
235 
236 static void handle___pkvm_host_unshare_hyp(struct kvm_cpu_context *host_ctxt)
237 {
238 	DECLARE_REG(u64, pfn, host_ctxt, 1);
239 
240 	cpu_reg(host_ctxt, 1) = __pkvm_host_unshare_hyp(pfn);
241 }
242 
243 static void handle___pkvm_create_private_mapping(struct kvm_cpu_context *host_ctxt)
244 {
245 	DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
246 	DECLARE_REG(size_t, size, host_ctxt, 2);
247 	DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3);
248 
249 	/*
250 	 * __pkvm_create_private_mapping() populates a pointer with the
251 	 * hypervisor start address of the allocation.
252 	 *
253 	 * However, handle___pkvm_create_private_mapping() hypercall crosses the
254 	 * EL1/EL2 boundary so the pointer would not be valid in this context.
255 	 *
256 	 * Instead pass the allocation address as the return value (or return
257 	 * ERR_PTR() on failure).
258 	 */
259 	unsigned long haddr;
260 	int err = __pkvm_create_private_mapping(phys, size, prot, &haddr);
261 
262 	if (err)
263 		haddr = (unsigned long)ERR_PTR(err);
264 
265 	cpu_reg(host_ctxt, 1) = haddr;
266 }
267 
268 static void handle___pkvm_prot_finalize(struct kvm_cpu_context *host_ctxt)
269 {
270 	cpu_reg(host_ctxt, 1) = __pkvm_prot_finalize();
271 }
272 
273 static void handle___pkvm_vcpu_init_traps(struct kvm_cpu_context *host_ctxt)
274 {
275 	DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
276 
277 	__pkvm_vcpu_init_traps(kern_hyp_va(vcpu));
278 }
279 
280 static void handle___pkvm_init_vm(struct kvm_cpu_context *host_ctxt)
281 {
282 	DECLARE_REG(struct kvm *, host_kvm, host_ctxt, 1);
283 	DECLARE_REG(unsigned long, vm_hva, host_ctxt, 2);
284 	DECLARE_REG(unsigned long, pgd_hva, host_ctxt, 3);
285 
286 	host_kvm = kern_hyp_va(host_kvm);
287 	cpu_reg(host_ctxt, 1) = __pkvm_init_vm(host_kvm, vm_hva, pgd_hva);
288 }
289 
290 static void handle___pkvm_init_vcpu(struct kvm_cpu_context *host_ctxt)
291 {
292 	DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
293 	DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 2);
294 	DECLARE_REG(unsigned long, vcpu_hva, host_ctxt, 3);
295 
296 	host_vcpu = kern_hyp_va(host_vcpu);
297 	cpu_reg(host_ctxt, 1) = __pkvm_init_vcpu(handle, host_vcpu, vcpu_hva);
298 }
299 
300 static void handle___pkvm_teardown_vm(struct kvm_cpu_context *host_ctxt)
301 {
302 	DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
303 
304 	cpu_reg(host_ctxt, 1) = __pkvm_teardown_vm(handle);
305 }
306 
307 typedef void (*hcall_t)(struct kvm_cpu_context *);
308 
309 #define HANDLE_FUNC(x)	[__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x
310 
311 static const hcall_t host_hcall[] = {
312 	/* ___kvm_hyp_init */
313 	HANDLE_FUNC(__kvm_get_mdcr_el2),
314 	HANDLE_FUNC(__pkvm_init),
315 	HANDLE_FUNC(__pkvm_create_private_mapping),
316 	HANDLE_FUNC(__pkvm_cpu_set_vector),
317 	HANDLE_FUNC(__kvm_enable_ssbs),
318 	HANDLE_FUNC(__vgic_v3_init_lrs),
319 	HANDLE_FUNC(__vgic_v3_get_gic_config),
320 	HANDLE_FUNC(__pkvm_prot_finalize),
321 
322 	HANDLE_FUNC(__pkvm_host_share_hyp),
323 	HANDLE_FUNC(__pkvm_host_unshare_hyp),
324 	HANDLE_FUNC(__kvm_adjust_pc),
325 	HANDLE_FUNC(__kvm_vcpu_run),
326 	HANDLE_FUNC(__kvm_flush_vm_context),
327 	HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa),
328 	HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa_nsh),
329 	HANDLE_FUNC(__kvm_tlb_flush_vmid),
330 	HANDLE_FUNC(__kvm_flush_cpu_context),
331 	HANDLE_FUNC(__kvm_timer_set_cntvoff),
332 	HANDLE_FUNC(__vgic_v3_read_vmcr),
333 	HANDLE_FUNC(__vgic_v3_write_vmcr),
334 	HANDLE_FUNC(__vgic_v3_save_aprs),
335 	HANDLE_FUNC(__vgic_v3_restore_aprs),
336 	HANDLE_FUNC(__pkvm_vcpu_init_traps),
337 	HANDLE_FUNC(__pkvm_init_vm),
338 	HANDLE_FUNC(__pkvm_init_vcpu),
339 	HANDLE_FUNC(__pkvm_teardown_vm),
340 };
341 
342 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
343 {
344 	DECLARE_REG(unsigned long, id, host_ctxt, 0);
345 	unsigned long hcall_min = 0;
346 	hcall_t hfn;
347 
348 	/*
349 	 * If pKVM has been initialised then reject any calls to the
350 	 * early "privileged" hypercalls. Note that we cannot reject
351 	 * calls to __pkvm_prot_finalize for two reasons: (1) The static
352 	 * key used to determine initialisation must be toggled prior to
353 	 * finalisation and (2) finalisation is performed on a per-CPU
354 	 * basis. This is all fine, however, since __pkvm_prot_finalize
355 	 * returns -EPERM after the first call for a given CPU.
356 	 */
357 	if (static_branch_unlikely(&kvm_protected_mode_initialized))
358 		hcall_min = __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize;
359 
360 	id -= KVM_HOST_SMCCC_ID(0);
361 
362 	if (unlikely(id < hcall_min || id >= ARRAY_SIZE(host_hcall)))
363 		goto inval;
364 
365 	hfn = host_hcall[id];
366 	if (unlikely(!hfn))
367 		goto inval;
368 
369 	cpu_reg(host_ctxt, 0) = SMCCC_RET_SUCCESS;
370 	hfn(host_ctxt);
371 
372 	return;
373 inval:
374 	cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
375 }
376 
377 static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt)
378 {
379 	__kvm_hyp_host_forward_smc(host_ctxt);
380 }
381 
382 static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
383 {
384 	bool handled;
385 
386 	handled = kvm_host_psci_handler(host_ctxt);
387 	if (!handled)
388 		handled = kvm_host_ffa_handler(host_ctxt);
389 	if (!handled)
390 		default_host_smc_handler(host_ctxt);
391 
392 	/* SMC was trapped, move ELR past the current PC. */
393 	kvm_skip_host_instr();
394 }
395 
396 void handle_trap(struct kvm_cpu_context *host_ctxt)
397 {
398 	u64 esr = read_sysreg_el2(SYS_ESR);
399 
400 	switch (ESR_ELx_EC(esr)) {
401 	case ESR_ELx_EC_HVC64:
402 		handle_host_hcall(host_ctxt);
403 		break;
404 	case ESR_ELx_EC_SMC64:
405 		handle_host_smc(host_ctxt);
406 		break;
407 	case ESR_ELx_EC_SVE:
408 		if (has_hvhe())
409 			sysreg_clear_set(cpacr_el1, 0, (CPACR_EL1_ZEN_EL1EN |
410 							CPACR_EL1_ZEN_EL0EN));
411 		else
412 			sysreg_clear_set(cptr_el2, CPTR_EL2_TZ, 0);
413 		isb();
414 		sve_cond_update_zcr_vq(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
415 		break;
416 	case ESR_ELx_EC_IABT_LOW:
417 	case ESR_ELx_EC_DABT_LOW:
418 		handle_host_mem_abort(host_ctxt);
419 		break;
420 	default:
421 		BUG();
422 	}
423 }
424