• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5  */
6 
7 #include <linux/bug.h>
8 #include <linux/cpu_pm.h>
9 #include <linux/entry-kvm.h>
10 #include <linux/errno.h>
11 #include <linux/err.h>
12 #include <linux/kvm_host.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/vmalloc.h>
16 #include <linux/fs.h>
17 #include <linux/mman.h>
18 #include <linux/sched.h>
19 #include <linux/kmemleak.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_irqfd.h>
22 #include <linux/irqbypass.h>
23 #include <linux/sched/stat.h>
24 #include <linux/psci.h>
25 #include <trace/events/kvm.h>
26 
27 #define CREATE_TRACE_POINTS
28 #include "trace_arm.h"
29 
30 #include <linux/uaccess.h>
31 #include <asm/ptrace.h>
32 #include <asm/mman.h>
33 #include <asm/tlbflush.h>
34 #include <asm/cacheflush.h>
35 #include <asm/cpufeature.h>
36 #include <asm/virt.h>
37 #include <asm/kvm_arm.h>
38 #include <asm/kvm_asm.h>
39 #include <asm/kvm_mmu.h>
40 #include <asm/kvm_pkvm.h>
41 #include <asm/kvm_emulate.h>
42 #include <asm/sections.h>
43 
44 #include <kvm/arm_hypercalls.h>
45 #include <kvm/arm_pmu.h>
46 #include <kvm/arm_psci.h>
47 
48 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
49 DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
50 
51 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
52 
53 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
54 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
55 
56 /* The VMID used in the VTTBR */
57 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
58 static u32 kvm_next_vmid;
59 static DEFINE_SPINLOCK(kvm_vmid_lock);
60 
61 static bool vgic_present;
62 
63 static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
64 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
65 
kvm_arch_vcpu_should_kick(struct kvm_vcpu * vcpu)66 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
67 {
68 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
69 }
70 
kvm_arch_hardware_setup(void * opaque)71 int kvm_arch_hardware_setup(void *opaque)
72 {
73 	return 0;
74 }
75 
kvm_arch_check_processor_compat(void * opaque)76 int kvm_arch_check_processor_compat(void *opaque)
77 {
78 	return 0;
79 }
80 
kvm_vm_ioctl_enable_cap(struct kvm * kvm,struct kvm_enable_cap * cap)81 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
82 			    struct kvm_enable_cap *cap)
83 {
84 	int r;
85 
86 	/* Capabilities with flags */
87 	switch (cap->cap) {
88 	case KVM_CAP_ARM_PROTECTED_VM:
89 		return kvm_arm_vm_ioctl_pkvm(kvm, cap);
90 	default:
91 		if (cap->flags)
92 			return -EINVAL;
93 	}
94 
95 	/* Capabilities without flags */
96 	switch (cap->cap) {
97 	case KVM_CAP_ARM_NISV_TO_USER:
98 		if (kvm_vm_is_protected(kvm)) {
99 			r = -EINVAL;
100 		} else {
101 			r = 0;
102 			set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
103 				&kvm->arch.flags);
104 		}
105 		break;
106 	case KVM_CAP_ARM_MTE:
107 		mutex_lock(&kvm->lock);
108 		if (!system_supports_mte() || kvm->created_vcpus) {
109 			r = -EINVAL;
110 		} else {
111 			r = 0;
112 			set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
113 		}
114 		mutex_unlock(&kvm->lock);
115 		break;
116 	default:
117 		r = -EINVAL;
118 		break;
119 	}
120 
121 	return r;
122 }
123 
kvm_arm_default_max_vcpus(void)124 static int kvm_arm_default_max_vcpus(void)
125 {
126 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
127 }
128 
set_default_spectre(struct kvm * kvm)129 static void set_default_spectre(struct kvm *kvm)
130 {
131 	/*
132 	 * The default is to expose CSV2 == 1 if the HW isn't affected.
133 	 * Although this is a per-CPU feature, we make it global because
134 	 * asymmetric systems are just a nuisance.
135 	 *
136 	 * Userspace can override this as long as it doesn't promise
137 	 * the impossible.
138 	 */
139 	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
140 		kvm->arch.pfr0_csv2 = 1;
141 	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
142 		kvm->arch.pfr0_csv3 = 1;
143 }
144 
145 /**
146  * kvm_arch_init_vm - initializes a VM data structure
147  * @kvm:	pointer to the KVM struct
148  */
kvm_arch_init_vm(struct kvm * kvm,unsigned long type)149 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
150 {
151 	int ret;
152 
153 	if (type & ~KVM_VM_TYPE_MASK)
154 		return -EINVAL;
155 
156 	ret = kvm_share_hyp(kvm, kvm + 1);
157 	if (ret)
158 		return ret;
159 
160 	ret = kvm_init_pvm(kvm, type);
161 	if (ret)
162 		return ret;
163 
164 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
165 	if (ret)
166 		return ret;
167 
168 	kvm_vgic_early_init(kvm);
169 
170 	/* The maximum number of VCPUs is limited by the host's GIC model */
171 	kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
172 
173 	set_default_spectre(kvm);
174 
175 	return ret;
176 }
177 
kvm_arch_vcpu_fault(struct kvm_vcpu * vcpu,struct vm_fault * vmf)178 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
179 {
180 	return VM_FAULT_SIGBUS;
181 }
182 
183 /**
184  * kvm_arch_destroy_vm - destroy the VM data structure
185  * @kvm:	pointer to the KVM struct
186  */
kvm_arch_destroy_vm(struct kvm * kvm)187 void kvm_arch_destroy_vm(struct kvm *kvm)
188 {
189 	int i;
190 
191 	bitmap_free(kvm->arch.pmu_filter);
192 
193 	kvm_vgic_destroy(kvm);
194 
195 	if (is_protected_kvm_enabled())
196 		kvm_shadow_destroy(kvm);
197 
198 	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
199 		if (kvm->vcpus[i]) {
200 			kvm_vcpu_destroy(kvm->vcpus[i]);
201 			kvm->vcpus[i] = NULL;
202 		}
203 	}
204 	atomic_set(&kvm->online_vcpus, 0);
205 
206 	kvm_unshare_hyp(kvm, kvm + 1);
207 }
208 
kvm_check_extension(struct kvm * kvm,long ext)209 static int kvm_check_extension(struct kvm *kvm, long ext)
210 {
211 	int r;
212 
213 	switch (ext) {
214 	case KVM_CAP_IRQCHIP:
215 		r = vgic_present;
216 		break;
217 	case KVM_CAP_IOEVENTFD:
218 	case KVM_CAP_DEVICE_CTRL:
219 	case KVM_CAP_USER_MEMORY:
220 	case KVM_CAP_SYNC_MMU:
221 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
222 	case KVM_CAP_ONE_REG:
223 	case KVM_CAP_ARM_PSCI:
224 	case KVM_CAP_ARM_PSCI_0_2:
225 	case KVM_CAP_READONLY_MEM:
226 	case KVM_CAP_MP_STATE:
227 	case KVM_CAP_IMMEDIATE_EXIT:
228 	case KVM_CAP_VCPU_EVENTS:
229 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
230 	case KVM_CAP_ARM_INJECT_EXT_DABT:
231 	case KVM_CAP_SET_GUEST_DEBUG:
232 	case KVM_CAP_VCPU_ATTRIBUTES:
233 	case KVM_CAP_PTP_KVM:
234 		r = 1;
235 		break;
236 	case KVM_CAP_ARM_NISV_TO_USER:
237 		r = !kvm || !kvm_vm_is_protected(kvm);
238 		break;
239 	case KVM_CAP_SET_GUEST_DEBUG2:
240 		return KVM_GUESTDBG_VALID_MASK;
241 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
242 		r = 1;
243 		break;
244 	case KVM_CAP_NR_VCPUS:
245 		/*
246 		 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
247 		 * architectures, as it does not always bound it to
248 		 * KVM_CAP_MAX_VCPUS. It should not matter much because
249 		 * this is just an advisory value.
250 		 */
251 		r = min_t(unsigned int, num_online_cpus(),
252 			  kvm_arm_default_max_vcpus());
253 		break;
254 	case KVM_CAP_MAX_VCPUS:
255 	case KVM_CAP_MAX_VCPU_ID:
256 		if (kvm)
257 			r = kvm->arch.max_vcpus;
258 		else
259 			r = kvm_arm_default_max_vcpus();
260 		break;
261 	case KVM_CAP_MSI_DEVID:
262 		if (!kvm)
263 			r = -EINVAL;
264 		else
265 			r = kvm->arch.vgic.msis_require_devid;
266 		break;
267 	case KVM_CAP_ARM_USER_IRQ:
268 		/*
269 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
270 		 * (bump this number if adding more devices)
271 		 */
272 		r = 1;
273 		break;
274 	case KVM_CAP_ARM_MTE:
275 		r = system_supports_mte();
276 		break;
277 	case KVM_CAP_STEAL_TIME:
278 		r = kvm_arm_pvtime_supported();
279 		break;
280 	case KVM_CAP_ARM_EL1_32BIT:
281 		r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
282 		break;
283 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
284 		r = get_num_brps();
285 		break;
286 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
287 		r = get_num_wrps();
288 		break;
289 	case KVM_CAP_ARM_PMU_V3:
290 		r = kvm_arm_support_pmu_v3();
291 		break;
292 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
293 		r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
294 		break;
295 	case KVM_CAP_ARM_VM_IPA_SIZE:
296 		r = get_kvm_ipa_limit();
297 		break;
298 	case KVM_CAP_ARM_SVE:
299 		r = system_supports_sve() && !is_protected_kvm_enabled();
300 		break;
301 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
302 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
303 		r = system_has_full_ptr_auth();
304 		break;
305 	default:
306 		r = 0;
307 	}
308 
309 	return r;
310 }
311 
312 /*
313  * Checks whether the exctension specified in ext is supported for protected
314  * vms. The capabilities supported by kvm in general are passed in kvm_cap.
315  */
pkvm_check_extension(struct kvm * kvm,long ext,int kvm_cap)316 static int pkvm_check_extension(struct kvm *kvm, long ext, int kvm_cap)
317 {
318 	int r;
319 
320 	switch (ext) {
321 	case KVM_CAP_IRQCHIP:
322 	case KVM_CAP_ARM_PSCI:
323 	case KVM_CAP_ARM_PSCI_0_2:
324 	case KVM_CAP_NR_VCPUS:
325 	case KVM_CAP_MAX_VCPUS:
326 	case KVM_CAP_MAX_VCPU_ID:
327 	case KVM_CAP_MSI_DEVID:
328 	case KVM_CAP_ARM_VM_IPA_SIZE:
329 		r = kvm_cap;
330 		break;
331 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
332 		r = min(kvm_cap, pkvm_get_max_brps());
333 		break;
334 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
335 		r = min(kvm_cap, pkvm_get_max_wrps());
336 		break;
337 	case KVM_CAP_ARM_PMU_V3:
338 		r = kvm_cap && FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_PMUVER),
339 					 PVM_ID_AA64DFR0_ALLOW);
340 		break;
341 	case KVM_CAP_ARM_SVE:
342 		r = kvm_cap && FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_SVE),
343 					 PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
344 		break;
345 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
346 		r = kvm_cap &&
347 		    FIELD_GET(ARM64_FEATURE_MASK(ID_AA64ISAR1_API),
348 			      PVM_ID_AA64ISAR1_ALLOW) &&
349 		    FIELD_GET(ARM64_FEATURE_MASK(ID_AA64ISAR1_APA),
350 			      PVM_ID_AA64ISAR1_ALLOW);
351 		break;
352 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
353 		r = kvm_cap &&
354 		    FIELD_GET(ARM64_FEATURE_MASK(ID_AA64ISAR1_GPI),
355 			      PVM_ID_AA64ISAR1_ALLOW) &&
356 		    FIELD_GET(ARM64_FEATURE_MASK(ID_AA64ISAR1_GPA),
357 			      PVM_ID_AA64ISAR1_ALLOW);
358 		break;
359 	case KVM_CAP_ARM_PROTECTED_VM:
360 		r = 1;
361 		break;
362 	default:
363 		r = 0;
364 		break;
365 	}
366 
367 	return r;
368 }
369 
kvm_vm_ioctl_check_extension(struct kvm * kvm,long ext)370 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
371 {
372 	int r = kvm_check_extension(kvm, ext);
373 
374 	if (unlikely(kvm && kvm_vm_is_protected(kvm)))
375 		r = pkvm_check_extension(kvm, ext, r);
376 
377 	return r;
378 }
379 
kvm_arch_dev_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)380 long kvm_arch_dev_ioctl(struct file *filp,
381 			unsigned int ioctl, unsigned long arg)
382 {
383 	return -EINVAL;
384 }
385 
kvm_arch_alloc_vm(void)386 struct kvm *kvm_arch_alloc_vm(void)
387 {
388 	size_t sz = sizeof(struct kvm);
389 
390 	if (!has_vhe())
391 		return kzalloc(sz, GFP_KERNEL_ACCOUNT);
392 
393 	return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
394 }
395 
kvm_arch_free_vm(struct kvm * kvm)396 void kvm_arch_free_vm(struct kvm *kvm)
397 {
398 	if (!has_vhe())
399 		kfree(kvm);
400 	else
401 		vfree(kvm);
402 }
403 
kvm_arch_vcpu_precreate(struct kvm * kvm,unsigned int id)404 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
405 {
406 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
407 		return -EBUSY;
408 
409 	if (id >= kvm->arch.max_vcpus)
410 		return -EINVAL;
411 
412 	return 0;
413 }
414 
kvm_arch_vcpu_create(struct kvm_vcpu * vcpu)415 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
416 {
417 	int err;
418 
419 	/* Force users to call KVM_ARM_VCPU_INIT */
420 	vcpu->arch.target = -1;
421 	bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
422 
423 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
424 
425 	/* Set up the timer */
426 	kvm_timer_vcpu_init(vcpu);
427 
428 	kvm_pmu_vcpu_init(vcpu);
429 
430 	kvm_arm_reset_debug_ptr(vcpu);
431 
432 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
433 
434 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
435 
436 	err = kvm_vgic_vcpu_init(vcpu);
437 	if (err)
438 		return err;
439 
440 	return kvm_share_hyp(vcpu, vcpu + 1);
441 }
442 
kvm_arch_vcpu_postcreate(struct kvm_vcpu * vcpu)443 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
444 {
445 }
446 
kvm_arch_vcpu_destroy(struct kvm_vcpu * vcpu)447 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
448 {
449 	if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
450 		static_branch_dec(&userspace_irqchip_in_use);
451 
452 	if (is_protected_kvm_enabled())
453 		free_hyp_memcache(&vcpu->arch.pkvm_memcache);
454 	else
455 		kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
456 	kvm_timer_vcpu_terminate(vcpu);
457 	kvm_pmu_vcpu_destroy(vcpu);
458 	kvm_vgic_vcpu_destroy(vcpu);
459 	kvm_arm_vcpu_destroy(vcpu);
460 }
461 
kvm_cpu_has_pending_timer(struct kvm_vcpu * vcpu)462 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
463 {
464 	return kvm_timer_is_pending(vcpu);
465 }
466 
kvm_arch_vcpu_blocking(struct kvm_vcpu * vcpu)467 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
468 {
469 	/*
470 	 * If we're about to block (most likely because we've just hit a
471 	 * WFI), we need to sync back the state of the GIC CPU interface
472 	 * so that we have the latest PMR and group enables. This ensures
473 	 * that kvm_arch_vcpu_runnable has up-to-date data to decide
474 	 * whether we have pending interrupts.
475 	 *
476 	 * For the same reason, we want to tell GICv4 that we need
477 	 * doorbells to be signalled, should an interrupt become pending.
478 	 */
479 	preempt_disable();
480 	kvm_vgic_put(vcpu, true);
481 	preempt_enable();
482 }
483 
kvm_arch_vcpu_unblocking(struct kvm_vcpu * vcpu)484 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
485 {
486 	preempt_disable();
487 	kvm_vgic_load(vcpu);
488 	preempt_enable();
489 }
490 
kvm_arch_vcpu_load(struct kvm_vcpu * vcpu,int cpu)491 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
492 {
493 	struct kvm_s2_mmu *mmu;
494 	int *last_ran;
495 
496 	if (is_protected_kvm_enabled())
497 		goto nommu;
498 
499 	mmu = vcpu->arch.hw_mmu;
500 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
501 
502 	/*
503 	 * We guarantee that both TLBs and I-cache are private to each
504 	 * vcpu. If detecting that a vcpu from the same VM has
505 	 * previously run on the same physical CPU, call into the
506 	 * hypervisor code to nuke the relevant contexts.
507 	 *
508 	 * We might get preempted before the vCPU actually runs, but
509 	 * over-invalidation doesn't affect correctness.
510 	 */
511 	if (*last_ran != vcpu->vcpu_id) {
512 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
513 		*last_ran = vcpu->vcpu_id;
514 	}
515 
516 nommu:
517 	vcpu->cpu = cpu;
518 
519 	kvm_vgic_load(vcpu);
520 	kvm_timer_vcpu_load(vcpu);
521 	if (has_vhe())
522 		kvm_vcpu_load_sysregs_vhe(vcpu);
523 	kvm_arch_vcpu_load_fp(vcpu);
524 	kvm_vcpu_pmu_restore_guest(vcpu);
525 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
526 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
527 
528 	if (single_task_running())
529 		vcpu_clear_wfx_traps(vcpu);
530 	else
531 		vcpu_set_wfx_traps(vcpu);
532 
533 	if (vcpu_has_ptrauth(vcpu))
534 		vcpu_ptrauth_disable(vcpu);
535 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
536 
537 	if (is_protected_kvm_enabled()) {
538 		kvm_call_hyp_nvhe(__pkvm_vcpu_load,
539 				  vcpu->kvm->arch.pkvm.shadow_handle,
540 				  vcpu->vcpu_idx, vcpu->arch.hcr_el2);
541 		kvm_call_hyp(__vgic_v3_restore_vmcr_aprs,
542 			     &vcpu->arch.vgic_cpu.vgic_v3);
543 	}
544 }
545 
kvm_arch_vcpu_put(struct kvm_vcpu * vcpu)546 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
547 {
548 	if (is_protected_kvm_enabled()) {
549 		kvm_call_hyp(__vgic_v3_save_vmcr_aprs,
550 			     &vcpu->arch.vgic_cpu.vgic_v3);
551 		kvm_call_hyp_nvhe(__pkvm_vcpu_put);
552 
553 		/* __pkvm_vcpu_put implies a sync of the state */
554 		if (!kvm_vm_is_protected(vcpu->kvm))
555 			vcpu->arch.flags |= KVM_ARM64_PKVM_STATE_DIRTY;
556 	}
557 
558 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
559 	kvm_arch_vcpu_put_fp(vcpu);
560 	if (has_vhe())
561 		kvm_vcpu_put_sysregs_vhe(vcpu);
562 	kvm_timer_vcpu_put(vcpu);
563 	kvm_vgic_put(vcpu, false);
564 	kvm_vcpu_pmu_restore_host(vcpu);
565 
566 	vcpu->cpu = -1;
567 }
568 
vcpu_power_off(struct kvm_vcpu * vcpu)569 static void vcpu_power_off(struct kvm_vcpu *vcpu)
570 {
571 	vcpu->arch.power_off = true;
572 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
573 	kvm_vcpu_kick(vcpu);
574 }
575 
kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)576 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
577 				    struct kvm_mp_state *mp_state)
578 {
579 	if (vcpu->arch.power_off)
580 		mp_state->mp_state = KVM_MP_STATE_STOPPED;
581 	else
582 		mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
583 
584 	return 0;
585 }
586 
kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)587 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
588 				    struct kvm_mp_state *mp_state)
589 {
590 	int ret = 0;
591 
592 	switch (mp_state->mp_state) {
593 	case KVM_MP_STATE_RUNNABLE:
594 		vcpu->arch.power_off = false;
595 		break;
596 	case KVM_MP_STATE_STOPPED:
597 		vcpu_power_off(vcpu);
598 		break;
599 	default:
600 		ret = -EINVAL;
601 	}
602 
603 	return ret;
604 }
605 
606 /**
607  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
608  * @v:		The VCPU pointer
609  *
610  * If the guest CPU is not waiting for interrupts or an interrupt line is
611  * asserted, the CPU is by definition runnable.
612  */
kvm_arch_vcpu_runnable(struct kvm_vcpu * v)613 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
614 {
615 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
616 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
617 		&& !v->arch.power_off && !v->arch.pause);
618 }
619 
kvm_arch_vcpu_in_kernel(struct kvm_vcpu * vcpu)620 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
621 {
622 	return vcpu_mode_priv(vcpu);
623 }
624 
625 /* Just ensure a guest exit from a particular CPU */
exit_vm_noop(void * info)626 static void exit_vm_noop(void *info)
627 {
628 }
629 
force_vm_exit(const cpumask_t * mask)630 void force_vm_exit(const cpumask_t *mask)
631 {
632 	preempt_disable();
633 	smp_call_function_many(mask, exit_vm_noop, NULL, true);
634 	preempt_enable();
635 }
636 
637 /**
638  * need_new_vmid_gen - check that the VMID is still valid
639  * @vmid: The VMID to check
640  *
641  * return true if there is a new generation of VMIDs being used
642  *
643  * The hardware supports a limited set of values with the value zero reserved
644  * for the host, so we check if an assigned value belongs to a previous
645  * generation, which requires us to assign a new value. If we're the first to
646  * use a VMID for the new generation, we must flush necessary caches and TLBs
647  * on all CPUs.
648  */
need_new_vmid_gen(struct kvm_vmid * vmid)649 static bool need_new_vmid_gen(struct kvm_vmid *vmid)
650 {
651 	u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
652 	smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
653 	return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
654 }
655 
656 /**
657  * update_vmid - Update the vmid with a valid VMID for the current generation
658  * @vmid: The stage-2 VMID information struct
659  */
update_vmid(struct kvm_vmid * vmid)660 static void update_vmid(struct kvm_vmid *vmid)
661 {
662 	if (!need_new_vmid_gen(vmid))
663 		return;
664 
665 	spin_lock(&kvm_vmid_lock);
666 
667 	/*
668 	 * We need to re-check the vmid_gen here to ensure that if another vcpu
669 	 * already allocated a valid vmid for this vm, then this vcpu should
670 	 * use the same vmid.
671 	 */
672 	if (!need_new_vmid_gen(vmid)) {
673 		spin_unlock(&kvm_vmid_lock);
674 		return;
675 	}
676 
677 	/* First user of a new VMID generation? */
678 	if (unlikely(kvm_next_vmid == 0)) {
679 		atomic64_inc(&kvm_vmid_gen);
680 		kvm_next_vmid = 1;
681 
682 		/*
683 		 * On SMP we know no other CPUs can use this CPU's or each
684 		 * other's VMID after force_vm_exit returns since the
685 		 * kvm_vmid_lock blocks them from reentry to the guest.
686 		 */
687 		force_vm_exit(cpu_all_mask);
688 		/*
689 		 * Now broadcast TLB + ICACHE invalidation over the inner
690 		 * shareable domain to make sure all data structures are
691 		 * clean.
692 		 */
693 		kvm_call_hyp(__kvm_flush_vm_context);
694 	}
695 
696 	WRITE_ONCE(vmid->vmid, kvm_next_vmid);
697 	kvm_next_vmid++;
698 	kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
699 
700 	smp_wmb();
701 	WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
702 
703 	spin_unlock(&kvm_vmid_lock);
704 }
705 
kvm_vcpu_initialized(struct kvm_vcpu * vcpu)706 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
707 {
708 	return vcpu->arch.target >= 0;
709 }
710 
711 /*
712  * Handle both the initialisation that is being done when the vcpu is
713  * run for the first time, as well as the updates that must be
714  * performed each time we get a new thread dealing with this vcpu.
715  */
kvm_arch_vcpu_run_pid_change(struct kvm_vcpu * vcpu)716 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
717 {
718 	struct kvm *kvm = vcpu->kvm;
719 	int ret;
720 
721 	if (!kvm_vcpu_initialized(vcpu))
722 		return -ENOEXEC;
723 
724 	if (!kvm_arm_vcpu_is_finalized(vcpu))
725 		return -EPERM;
726 
727 	ret = kvm_arch_vcpu_run_map_fp(vcpu);
728 	if (ret)
729 		return ret;
730 
731 	if (likely(vcpu_has_run_once(vcpu)))
732 		return 0;
733 
734 	kvm_arm_vcpu_init_debug(vcpu);
735 
736 	if (likely(irqchip_in_kernel(kvm))) {
737 		/*
738 		 * Map the VGIC hardware resources before running a vcpu the
739 		 * first time on this VM.
740 		 */
741 		ret = kvm_vgic_map_resources(kvm);
742 		if (ret)
743 			return ret;
744 	}
745 
746 	ret = kvm_timer_enable(vcpu);
747 	if (ret)
748 		return ret;
749 
750 	ret = kvm_arm_pmu_v3_enable(vcpu);
751 	if (ret)
752 		return ret;
753 
754 	if (!irqchip_in_kernel(kvm)) {
755 		/*
756 		 * Tell the rest of the code that there are userspace irqchip
757 		 * VMs in the wild.
758 		 */
759 		static_branch_inc(&userspace_irqchip_in_use);
760 	}
761 
762 	if (is_protected_kvm_enabled()) {
763 		/* Start with the vcpu in a dirty state */
764 		if (!kvm_vm_is_protected(vcpu->kvm))
765 			vcpu->arch.flags |= KVM_ARM64_PKVM_STATE_DIRTY;
766 		ret = create_el2_shadow(kvm);
767 	}
768 
769 	return ret;
770 }
771 
kvm_arch_intc_initialized(struct kvm * kvm)772 bool kvm_arch_intc_initialized(struct kvm *kvm)
773 {
774 	return vgic_initialized(kvm);
775 }
776 
kvm_arm_halt_guest(struct kvm * kvm)777 void kvm_arm_halt_guest(struct kvm *kvm)
778 {
779 	int i;
780 	struct kvm_vcpu *vcpu;
781 
782 	kvm_for_each_vcpu(i, vcpu, kvm)
783 		vcpu->arch.pause = true;
784 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
785 }
786 
kvm_arm_resume_guest(struct kvm * kvm)787 void kvm_arm_resume_guest(struct kvm *kvm)
788 {
789 	int i;
790 	struct kvm_vcpu *vcpu;
791 
792 	kvm_for_each_vcpu(i, vcpu, kvm) {
793 		vcpu->arch.pause = false;
794 		rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu));
795 	}
796 }
797 
vcpu_req_sleep(struct kvm_vcpu * vcpu)798 static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
799 {
800 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
801 
802 	rcuwait_wait_event(wait,
803 			   (!vcpu->arch.power_off) &&(!vcpu->arch.pause),
804 			   TASK_INTERRUPTIBLE);
805 
806 	if (vcpu->arch.power_off || vcpu->arch.pause) {
807 		/* Awaken to handle a signal, request we sleep again later. */
808 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
809 	}
810 
811 	/*
812 	 * Make sure we will observe a potential reset request if we've
813 	 * observed a change to the power state. Pairs with the smp_wmb() in
814 	 * kvm_psci_vcpu_on().
815 	 */
816 	smp_rmb();
817 }
818 
check_vcpu_requests(struct kvm_vcpu * vcpu)819 static void check_vcpu_requests(struct kvm_vcpu *vcpu)
820 {
821 	if (kvm_request_pending(vcpu)) {
822 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
823 			vcpu_req_sleep(vcpu);
824 
825 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
826 			kvm_reset_vcpu(vcpu);
827 
828 		/*
829 		 * Clear IRQ_PENDING requests that were made to guarantee
830 		 * that a VCPU sees new virtual interrupts.
831 		 */
832 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
833 
834 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
835 			kvm_update_stolen_time(vcpu);
836 
837 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
838 			/* The distributor enable bits were changed */
839 			preempt_disable();
840 			vgic_v4_put(vcpu, false);
841 			vgic_v4_load(vcpu);
842 			preempt_enable();
843 		}
844 
845 		if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
846 			kvm_pmu_handle_pmcr(vcpu,
847 					    __vcpu_sys_reg(vcpu, PMCR_EL0));
848 	}
849 }
850 
vcpu_mode_is_bad_32bit(struct kvm_vcpu * vcpu)851 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
852 {
853 	if (likely(!vcpu_mode_is_32bit(vcpu)))
854 		return false;
855 
856 	return !kvm_supports_32bit_el0();
857 }
858 
859 /**
860  * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
861  * @vcpu:	The VCPU pointer
862  * @ret:	Pointer to write optional return code
863  *
864  * Returns: true if the VCPU needs to return to a preemptible + interruptible
865  *	    and skip guest entry.
866  *
867  * This function disambiguates between two different types of exits: exits to a
868  * preemptible + interruptible kernel context and exits to userspace. For an
869  * exit to userspace, this function will write the return code to ret and return
870  * true. For an exit to preemptible + interruptible kernel context (i.e. check
871  * for pending work and re-enter), return true without writing to ret.
872  */
kvm_vcpu_exit_request(struct kvm_vcpu * vcpu,int * ret)873 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
874 {
875 	struct kvm_run *run = vcpu->run;
876 
877 	/*
878 	 * If we're using a userspace irqchip, then check if we need
879 	 * to tell a userspace irqchip about timer or PMU level
880 	 * changes and if so, exit to userspace (the actual level
881 	 * state gets updated in kvm_timer_update_run and
882 	 * kvm_pmu_update_run below).
883 	 */
884 	if (static_branch_unlikely(&userspace_irqchip_in_use)) {
885 		if (kvm_timer_should_notify_user(vcpu) ||
886 		    kvm_pmu_should_notify_user(vcpu)) {
887 			*ret = -EINTR;
888 			run->exit_reason = KVM_EXIT_INTR;
889 			return true;
890 		}
891 	}
892 
893 	return kvm_request_pending(vcpu) ||
894 			need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) ||
895 			xfer_to_guest_mode_work_pending();
896 }
897 
898 /*
899  * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
900  * the vCPU is running.
901  *
902  * This must be noinstr as instrumentation may make use of RCU, and this is not
903  * safe during the EQS.
904  */
kvm_arm_vcpu_enter_exit(struct kvm_vcpu * vcpu)905 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
906 {
907 	int ret;
908 
909 	guest_state_enter_irqoff();
910 	ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
911 	guest_state_exit_irqoff();
912 
913 	return ret;
914 }
915 
916 /**
917  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
918  * @vcpu:	The VCPU pointer
919  *
920  * This function is called through the VCPU_RUN ioctl called from user space. It
921  * will execute VM code in a loop until the time slice for the process is used
922  * or some emulation is needed from user space in which case the function will
923  * return with return value 0 and with the kvm_run structure filled in with the
924  * required data for the requested emulation.
925  */
kvm_arch_vcpu_ioctl_run(struct kvm_vcpu * vcpu)926 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
927 {
928 	struct kvm_run *run = vcpu->run;
929 	int ret;
930 
931 	if (run->exit_reason == KVM_EXIT_MMIO) {
932 		ret = kvm_handle_mmio_return(vcpu);
933 		if (ret)
934 			return ret;
935 	}
936 
937 	vcpu_load(vcpu);
938 
939 	if (run->immediate_exit) {
940 		ret = -EINTR;
941 		goto out;
942 	}
943 
944 	kvm_sigset_activate(vcpu);
945 
946 	ret = 1;
947 	run->exit_reason = KVM_EXIT_UNKNOWN;
948 	while (ret > 0) {
949 		/*
950 		 * Check conditions before entering the guest
951 		 */
952 		ret = xfer_to_guest_mode_handle_work(vcpu);
953 		if (!ret)
954 			ret = 1;
955 
956 		update_vmid(&vcpu->arch.hw_mmu->vmid);
957 
958 		check_vcpu_requests(vcpu);
959 
960 		/*
961 		 * Preparing the interrupts to be injected also
962 		 * involves poking the GIC, which must be done in a
963 		 * non-preemptible context.
964 		 */
965 		preempt_disable();
966 
967 		kvm_pmu_flush_hwstate(vcpu);
968 
969 		local_irq_disable();
970 
971 		kvm_vgic_flush_hwstate(vcpu);
972 
973 		/*
974 		 * Ensure we set mode to IN_GUEST_MODE after we disable
975 		 * interrupts and before the final VCPU requests check.
976 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
977 		 * Documentation/virt/kvm/vcpu-requests.rst
978 		 */
979 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
980 
981 		if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
982 			vcpu->mode = OUTSIDE_GUEST_MODE;
983 			isb(); /* Ensure work in x_flush_hwstate is committed */
984 			kvm_pmu_sync_hwstate(vcpu);
985 			if (static_branch_unlikely(&userspace_irqchip_in_use))
986 				kvm_timer_sync_user(vcpu);
987 			kvm_vgic_sync_hwstate(vcpu);
988 			local_irq_enable();
989 			preempt_enable();
990 			continue;
991 		}
992 
993 		kvm_arm_setup_debug(vcpu);
994 		kvm_arch_vcpu_ctxflush_fp(vcpu);
995 
996 		/**************************************************************
997 		 * Enter the guest
998 		 */
999 		trace_kvm_entry(*vcpu_pc(vcpu));
1000 		guest_timing_enter_irqoff();
1001 
1002 		ret = kvm_arm_vcpu_enter_exit(vcpu);
1003 
1004 		vcpu->mode = OUTSIDE_GUEST_MODE;
1005 		vcpu->stat.exits++;
1006 		/*
1007 		 * Back from guest
1008 		 *************************************************************/
1009 
1010 		kvm_arm_clear_debug(vcpu);
1011 
1012 		/*
1013 		 * We must sync the PMU state before the vgic state so
1014 		 * that the vgic can properly sample the updated state of the
1015 		 * interrupt line.
1016 		 */
1017 		kvm_pmu_sync_hwstate(vcpu);
1018 
1019 		/*
1020 		 * Sync the vgic state before syncing the timer state because
1021 		 * the timer code needs to know if the virtual timer
1022 		 * interrupts are active.
1023 		 */
1024 		kvm_vgic_sync_hwstate(vcpu);
1025 
1026 		/*
1027 		 * Sync the timer hardware state before enabling interrupts as
1028 		 * we don't want vtimer interrupts to race with syncing the
1029 		 * timer virtual interrupt state.
1030 		 */
1031 		if (static_branch_unlikely(&userspace_irqchip_in_use))
1032 			kvm_timer_sync_user(vcpu);
1033 
1034 		kvm_arch_vcpu_ctxsync_fp(vcpu);
1035 
1036 		/*
1037 		 * We must ensure that any pending interrupts are taken before
1038 		 * we exit guest timing so that timer ticks are accounted as
1039 		 * guest time. Transiently unmask interrupts so that any
1040 		 * pending interrupts are taken.
1041 		 *
1042 		 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
1043 		 * context synchronization event) is necessary to ensure that
1044 		 * pending interrupts are taken.
1045 		 */
1046 		local_irq_enable();
1047 		isb();
1048 		local_irq_disable();
1049 
1050 		guest_timing_exit_irqoff();
1051 
1052 		local_irq_enable();
1053 
1054 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1055 
1056 		/* Exit types that need handling before we can be preempted */
1057 		handle_exit_early(vcpu, ret);
1058 
1059 		preempt_enable();
1060 
1061 		/*
1062 		 * The ARMv8 architecture doesn't give the hypervisor
1063 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
1064 		 * if implemented by the CPU. If we spot the guest in such
1065 		 * state and that we decided it wasn't supposed to do so (like
1066 		 * with the asymmetric AArch32 case), return to userspace with
1067 		 * a fatal error.
1068 		 */
1069 		if (vcpu_mode_is_bad_32bit(vcpu)) {
1070 			/*
1071 			 * As we have caught the guest red-handed, decide that
1072 			 * it isn't fit for purpose anymore by making the vcpu
1073 			 * invalid. The VMM can try and fix it by issuing  a
1074 			 * KVM_ARM_VCPU_INIT if it really wants to.
1075 			 */
1076 			vcpu->arch.target = -1;
1077 			ret = ARM_EXCEPTION_IL;
1078 		}
1079 
1080 		ret = handle_exit(vcpu, ret);
1081 	}
1082 
1083 	/* Tell userspace about in-kernel device output levels */
1084 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
1085 		kvm_timer_update_run(vcpu);
1086 		kvm_pmu_update_run(vcpu);
1087 	}
1088 
1089 	kvm_sigset_deactivate(vcpu);
1090 
1091 out:
1092 	/*
1093 	 * In the unlikely event that we are returning to userspace
1094 	 * with pending exceptions or PC adjustment, commit these
1095 	 * adjustments in order to give userspace a consistent view of
1096 	 * the vcpu state. Note that this relies on __kvm_adjust_pc()
1097 	 * being preempt-safe on VHE.
1098 	 */
1099 	if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION |
1100 					 KVM_ARM64_INCREMENT_PC)))
1101 		kvm_call_hyp(__kvm_adjust_pc, vcpu);
1102 
1103 	vcpu_put(vcpu);
1104 	return ret;
1105 }
1106 
vcpu_interrupt_line(struct kvm_vcpu * vcpu,int number,bool level)1107 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
1108 {
1109 	int bit_index;
1110 	bool set;
1111 	unsigned long *hcr;
1112 
1113 	if (number == KVM_ARM_IRQ_CPU_IRQ)
1114 		bit_index = __ffs(HCR_VI);
1115 	else /* KVM_ARM_IRQ_CPU_FIQ */
1116 		bit_index = __ffs(HCR_VF);
1117 
1118 	hcr = vcpu_hcr(vcpu);
1119 	if (level)
1120 		set = test_and_set_bit(bit_index, hcr);
1121 	else
1122 		set = test_and_clear_bit(bit_index, hcr);
1123 
1124 	/*
1125 	 * If we didn't change anything, no need to wake up or kick other CPUs
1126 	 */
1127 	if (set == level)
1128 		return 0;
1129 
1130 	/*
1131 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
1132 	 * trigger a world-switch round on the running physical CPU to set the
1133 	 * virtual IRQ/FIQ fields in the HCR appropriately.
1134 	 */
1135 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
1136 	kvm_vcpu_kick(vcpu);
1137 
1138 	return 0;
1139 }
1140 
kvm_vm_ioctl_irq_line(struct kvm * kvm,struct kvm_irq_level * irq_level,bool line_status)1141 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
1142 			  bool line_status)
1143 {
1144 	u32 irq = irq_level->irq;
1145 	unsigned int irq_type, vcpu_idx, irq_num;
1146 	int nrcpus = atomic_read(&kvm->online_vcpus);
1147 	struct kvm_vcpu *vcpu = NULL;
1148 	bool level = irq_level->level;
1149 
1150 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
1151 	vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
1152 	vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
1153 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
1154 
1155 	trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
1156 
1157 	switch (irq_type) {
1158 	case KVM_ARM_IRQ_TYPE_CPU:
1159 		if (irqchip_in_kernel(kvm))
1160 			return -ENXIO;
1161 
1162 		if (vcpu_idx >= nrcpus)
1163 			return -EINVAL;
1164 
1165 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1166 		if (!vcpu)
1167 			return -EINVAL;
1168 
1169 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
1170 			return -EINVAL;
1171 
1172 		return vcpu_interrupt_line(vcpu, irq_num, level);
1173 	case KVM_ARM_IRQ_TYPE_PPI:
1174 		if (!irqchip_in_kernel(kvm))
1175 			return -ENXIO;
1176 
1177 		if (vcpu_idx >= nrcpus)
1178 			return -EINVAL;
1179 
1180 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1181 		if (!vcpu)
1182 			return -EINVAL;
1183 
1184 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
1185 			return -EINVAL;
1186 
1187 		return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
1188 	case KVM_ARM_IRQ_TYPE_SPI:
1189 		if (!irqchip_in_kernel(kvm))
1190 			return -ENXIO;
1191 
1192 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
1193 			return -EINVAL;
1194 
1195 		return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
1196 	}
1197 
1198 	return -EINVAL;
1199 }
1200 
kvm_vcpu_set_target(struct kvm_vcpu * vcpu,const struct kvm_vcpu_init * init)1201 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1202 			       const struct kvm_vcpu_init *init)
1203 {
1204 	unsigned int i, ret;
1205 	u32 phys_target = kvm_target_cpu();
1206 
1207 	if (init->target != phys_target)
1208 		return -EINVAL;
1209 
1210 	/*
1211 	 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
1212 	 * use the same target.
1213 	 */
1214 	if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
1215 		return -EINVAL;
1216 
1217 	/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
1218 	for (i = 0; i < sizeof(init->features) * 8; i++) {
1219 		bool set = (init->features[i / 32] & (1 << (i % 32)));
1220 
1221 		if (set && i >= KVM_VCPU_MAX_FEATURES)
1222 			return -ENOENT;
1223 
1224 		/*
1225 		 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
1226 		 * use the same feature set.
1227 		 */
1228 		if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
1229 		    test_bit(i, vcpu->arch.features) != set)
1230 			return -EINVAL;
1231 
1232 		if (set)
1233 			set_bit(i, vcpu->arch.features);
1234 	}
1235 
1236 	vcpu->arch.target = phys_target;
1237 
1238 	/* Now we know what it is, we can reset it. */
1239 	ret = kvm_reset_vcpu(vcpu);
1240 	if (ret) {
1241 		vcpu->arch.target = -1;
1242 		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1243 	}
1244 
1245 	return ret;
1246 }
1247 
kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu * vcpu,struct kvm_vcpu_init * init)1248 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1249 					 struct kvm_vcpu_init *init)
1250 {
1251 	int ret;
1252 
1253 	ret = kvm_vcpu_set_target(vcpu, init);
1254 	if (ret)
1255 		return ret;
1256 
1257 	/*
1258 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
1259 	 * guest MMU is turned off and flush the caches as needed.
1260 	 *
1261 	 * S2FWB enforces all memory accesses to RAM being cacheable,
1262 	 * ensuring that the data side is always coherent. We still
1263 	 * need to invalidate the I-cache though, as FWB does *not*
1264 	 * imply CTR_EL0.DIC.
1265 	 */
1266 	if (vcpu_has_run_once(vcpu)) {
1267 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
1268 			stage2_unmap_vm(vcpu->kvm);
1269 		else
1270 			icache_inval_all_pou();
1271 	}
1272 
1273 	vcpu_reset_hcr(vcpu);
1274 	vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
1275 
1276 	/*
1277 	 * Handle the "start in power-off" case.
1278 	 */
1279 	if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
1280 		vcpu_power_off(vcpu);
1281 	else
1282 		vcpu->arch.power_off = false;
1283 
1284 	return 0;
1285 }
1286 
kvm_arm_vcpu_set_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1287 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1288 				 struct kvm_device_attr *attr)
1289 {
1290 	int ret = -ENXIO;
1291 
1292 	switch (attr->group) {
1293 	default:
1294 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
1295 		break;
1296 	}
1297 
1298 	return ret;
1299 }
1300 
kvm_arm_vcpu_get_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1301 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1302 				 struct kvm_device_attr *attr)
1303 {
1304 	int ret = -ENXIO;
1305 
1306 	switch (attr->group) {
1307 	default:
1308 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
1309 		break;
1310 	}
1311 
1312 	return ret;
1313 }
1314 
kvm_arm_vcpu_has_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1315 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1316 				 struct kvm_device_attr *attr)
1317 {
1318 	int ret = -ENXIO;
1319 
1320 	switch (attr->group) {
1321 	default:
1322 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
1323 		break;
1324 	}
1325 
1326 	return ret;
1327 }
1328 
kvm_arm_vcpu_get_events(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)1329 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1330 				   struct kvm_vcpu_events *events)
1331 {
1332 	memset(events, 0, sizeof(*events));
1333 
1334 	return __kvm_arm_vcpu_get_events(vcpu, events);
1335 }
1336 
kvm_arm_vcpu_set_events(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)1337 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1338 				   struct kvm_vcpu_events *events)
1339 {
1340 	int i;
1341 
1342 	/* check whether the reserved field is zero */
1343 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1344 		if (events->reserved[i])
1345 			return -EINVAL;
1346 
1347 	/* check whether the pad field is zero */
1348 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1349 		if (events->exception.pad[i])
1350 			return -EINVAL;
1351 
1352 	return __kvm_arm_vcpu_set_events(vcpu, events);
1353 }
1354 
kvm_arch_vcpu_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1355 long kvm_arch_vcpu_ioctl(struct file *filp,
1356 			 unsigned int ioctl, unsigned long arg)
1357 {
1358 	struct kvm_vcpu *vcpu = filp->private_data;
1359 	void __user *argp = (void __user *)arg;
1360 	struct kvm_device_attr attr;
1361 	long r;
1362 
1363 	switch (ioctl) {
1364 	case KVM_ARM_VCPU_INIT: {
1365 		struct kvm_vcpu_init init;
1366 
1367 		r = -EFAULT;
1368 		if (copy_from_user(&init, argp, sizeof(init)))
1369 			break;
1370 
1371 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1372 		break;
1373 	}
1374 	case KVM_SET_ONE_REG:
1375 	case KVM_GET_ONE_REG: {
1376 		struct kvm_one_reg reg;
1377 
1378 		r = -ENOEXEC;
1379 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
1380 			break;
1381 
1382 		r = -EFAULT;
1383 		if (copy_from_user(&reg, argp, sizeof(reg)))
1384 			break;
1385 
1386 		/*
1387 		 * We could owe a reset due to PSCI. Handle the pending reset
1388 		 * here to ensure userspace register accesses are ordered after
1389 		 * the reset.
1390 		 */
1391 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
1392 			kvm_reset_vcpu(vcpu);
1393 
1394 		if (ioctl == KVM_SET_ONE_REG)
1395 			r = kvm_arm_set_reg(vcpu, &reg);
1396 		else
1397 			r = kvm_arm_get_reg(vcpu, &reg);
1398 		break;
1399 	}
1400 	case KVM_GET_REG_LIST: {
1401 		struct kvm_reg_list __user *user_list = argp;
1402 		struct kvm_reg_list reg_list;
1403 		unsigned n;
1404 
1405 		r = -ENOEXEC;
1406 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
1407 			break;
1408 
1409 		r = -EPERM;
1410 		if (!kvm_arm_vcpu_is_finalized(vcpu))
1411 			break;
1412 
1413 		r = -EFAULT;
1414 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1415 			break;
1416 		n = reg_list.n;
1417 		reg_list.n = kvm_arm_num_regs(vcpu);
1418 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1419 			break;
1420 		r = -E2BIG;
1421 		if (n < reg_list.n)
1422 			break;
1423 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1424 		break;
1425 	}
1426 	case KVM_SET_DEVICE_ATTR: {
1427 		r = -EFAULT;
1428 		if (copy_from_user(&attr, argp, sizeof(attr)))
1429 			break;
1430 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1431 		break;
1432 	}
1433 	case KVM_GET_DEVICE_ATTR: {
1434 		r = -EFAULT;
1435 		if (copy_from_user(&attr, argp, sizeof(attr)))
1436 			break;
1437 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1438 		break;
1439 	}
1440 	case KVM_HAS_DEVICE_ATTR: {
1441 		r = -EFAULT;
1442 		if (copy_from_user(&attr, argp, sizeof(attr)))
1443 			break;
1444 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1445 		break;
1446 	}
1447 	case KVM_GET_VCPU_EVENTS: {
1448 		struct kvm_vcpu_events events;
1449 
1450 		if (kvm_arm_vcpu_get_events(vcpu, &events))
1451 			return -EINVAL;
1452 
1453 		if (copy_to_user(argp, &events, sizeof(events)))
1454 			return -EFAULT;
1455 
1456 		return 0;
1457 	}
1458 	case KVM_SET_VCPU_EVENTS: {
1459 		struct kvm_vcpu_events events;
1460 
1461 		if (copy_from_user(&events, argp, sizeof(events)))
1462 			return -EFAULT;
1463 
1464 		return kvm_arm_vcpu_set_events(vcpu, &events);
1465 	}
1466 	case KVM_ARM_VCPU_FINALIZE: {
1467 		int what;
1468 
1469 		if (!kvm_vcpu_initialized(vcpu))
1470 			return -ENOEXEC;
1471 
1472 		if (get_user(what, (const int __user *)argp))
1473 			return -EFAULT;
1474 
1475 		return kvm_arm_vcpu_finalize(vcpu, what);
1476 	}
1477 	default:
1478 		r = -EINVAL;
1479 	}
1480 
1481 	return r;
1482 }
1483 
kvm_arch_sync_dirty_log(struct kvm * kvm,struct kvm_memory_slot * memslot)1484 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1485 {
1486 
1487 }
1488 
kvm_arch_flush_remote_tlbs_memslot(struct kvm * kvm,const struct kvm_memory_slot * memslot)1489 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
1490 					const struct kvm_memory_slot *memslot)
1491 {
1492 	kvm_flush_remote_tlbs(kvm);
1493 }
1494 
kvm_vm_ioctl_set_device_addr(struct kvm * kvm,struct kvm_arm_device_addr * dev_addr)1495 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1496 					struct kvm_arm_device_addr *dev_addr)
1497 {
1498 	unsigned long dev_id, type;
1499 
1500 	dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
1501 		KVM_ARM_DEVICE_ID_SHIFT;
1502 	type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
1503 		KVM_ARM_DEVICE_TYPE_SHIFT;
1504 
1505 	switch (dev_id) {
1506 	case KVM_ARM_DEVICE_VGIC_V2:
1507 		if (!vgic_present)
1508 			return -ENXIO;
1509 		return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
1510 	default:
1511 		return -ENODEV;
1512 	}
1513 }
1514 
kvm_arch_vm_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1515 long kvm_arch_vm_ioctl(struct file *filp,
1516 		       unsigned int ioctl, unsigned long arg)
1517 {
1518 	struct kvm *kvm = filp->private_data;
1519 	void __user *argp = (void __user *)arg;
1520 
1521 	switch (ioctl) {
1522 	case KVM_CREATE_IRQCHIP: {
1523 		int ret;
1524 		if (!vgic_present)
1525 			return -ENXIO;
1526 		mutex_lock(&kvm->lock);
1527 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1528 		mutex_unlock(&kvm->lock);
1529 		return ret;
1530 	}
1531 	case KVM_ARM_SET_DEVICE_ADDR: {
1532 		struct kvm_arm_device_addr dev_addr;
1533 
1534 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1535 			return -EFAULT;
1536 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1537 	}
1538 	case KVM_ARM_PREFERRED_TARGET: {
1539 		struct kvm_vcpu_init init;
1540 
1541 		kvm_vcpu_preferred_target(&init);
1542 
1543 		if (copy_to_user(argp, &init, sizeof(init)))
1544 			return -EFAULT;
1545 
1546 		return 0;
1547 	}
1548 	case KVM_ARM_MTE_COPY_TAGS: {
1549 		struct kvm_arm_copy_mte_tags copy_tags;
1550 
1551 		if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1552 			return -EFAULT;
1553 		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1554 	}
1555 	default:
1556 		return -EINVAL;
1557 	}
1558 }
1559 
nvhe_percpu_size(void)1560 static unsigned long nvhe_percpu_size(void)
1561 {
1562 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
1563 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
1564 }
1565 
nvhe_percpu_order(void)1566 static unsigned long nvhe_percpu_order(void)
1567 {
1568 	unsigned long size = nvhe_percpu_size();
1569 
1570 	return size ? get_order(size) : 0;
1571 }
1572 
1573 /* A lookup table holding the hypervisor VA for each vector slot */
1574 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1575 
kvm_init_vector_slot(void * base,enum arm64_hyp_spectre_vector slot)1576 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1577 {
1578 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
1579 }
1580 
kvm_init_vector_slots(void)1581 static int kvm_init_vector_slots(void)
1582 {
1583 	int err;
1584 	void *base;
1585 
1586 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1587 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1588 
1589 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1590 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1591 
1592 	if (kvm_system_needs_idmapped_vectors() &&
1593 	    !is_protected_kvm_enabled()) {
1594 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1595 					       __BP_HARDEN_HYP_VECS_SZ, &base);
1596 		if (err)
1597 			return err;
1598 	}
1599 
1600 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1601 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
1602 	return 0;
1603 }
1604 
cpu_prepare_hyp_mode(int cpu)1605 static void cpu_prepare_hyp_mode(int cpu)
1606 {
1607 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1608 	unsigned long tcr;
1609 
1610 	/*
1611 	 * Calculate the raw per-cpu offset without a translation from the
1612 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
1613 	 * so that we can use adr_l to access per-cpu variables in EL2.
1614 	 * Also drop the KASAN tag which gets in the way...
1615 	 */
1616 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
1617 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
1618 
1619 	params->mair_el2 = read_sysreg(mair_el1);
1620 
1621 	/*
1622 	 * The ID map may be configured to use an extended virtual address
1623 	 * range. This is only the case if system RAM is out of range for the
1624 	 * currently configured page size and VA_BITS, in which case we will
1625 	 * also need the extended virtual range for the HYP ID map, or we won't
1626 	 * be able to enable the EL2 MMU.
1627 	 *
1628 	 * However, at EL2, there is only one TTBR register, and we can't switch
1629 	 * between translation tables *and* update TCR_EL2.T0SZ at the same
1630 	 * time. Bottom line: we need to use the extended range with *both* our
1631 	 * translation tables.
1632 	 *
1633 	 * So use the same T0SZ value we use for the ID map.
1634 	 */
1635 	tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1;
1636 	tcr &= ~TCR_T0SZ_MASK;
1637 	tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET;
1638 	params->tcr_el2 = tcr;
1639 
1640 	params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE);
1641 	params->pgd_pa = kvm_mmu_get_httbr();
1642 	if (is_protected_kvm_enabled())
1643 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1644 	else
1645 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1646 	params->vttbr = params->vtcr = 0;
1647 
1648 	/*
1649 	 * Flush the init params from the data cache because the struct will
1650 	 * be read while the MMU is off.
1651 	 */
1652 	kvm_flush_dcache_to_poc(params, sizeof(*params));
1653 }
1654 
hyp_install_host_vector(void)1655 static void hyp_install_host_vector(void)
1656 {
1657 	struct kvm_nvhe_init_params *params;
1658 	struct arm_smccc_res res;
1659 
1660 	/* Switch from the HYP stub to our own HYP init vector */
1661 	__hyp_set_vectors(kvm_get_idmap_vector());
1662 
1663 	/*
1664 	 * Call initialization code, and switch to the full blown HYP code.
1665 	 * If the cpucaps haven't been finalized yet, something has gone very
1666 	 * wrong, and hyp will crash and burn when it uses any
1667 	 * cpus_have_const_cap() wrapper.
1668 	 */
1669 	BUG_ON(!system_capabilities_finalized());
1670 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
1671 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
1672 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1673 }
1674 
cpu_init_hyp_mode(void)1675 static void cpu_init_hyp_mode(void)
1676 {
1677 	hyp_install_host_vector();
1678 
1679 	/*
1680 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
1681 	 * at EL2.
1682 	 */
1683 	if (this_cpu_has_cap(ARM64_SSBS) &&
1684 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
1685 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
1686 	}
1687 }
1688 
cpu_hyp_reset(void)1689 static void cpu_hyp_reset(void)
1690 {
1691 	if (!is_kernel_in_hyp_mode())
1692 		__hyp_reset_vectors();
1693 }
1694 
1695 /*
1696  * EL2 vectors can be mapped and rerouted in a number of ways,
1697  * depending on the kernel configuration and CPU present:
1698  *
1699  * - If the CPU is affected by Spectre-v2, the hardening sequence is
1700  *   placed in one of the vector slots, which is executed before jumping
1701  *   to the real vectors.
1702  *
1703  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1704  *   containing the hardening sequence is mapped next to the idmap page,
1705  *   and executed before jumping to the real vectors.
1706  *
1707  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1708  *   empty slot is selected, mapped next to the idmap page, and
1709  *   executed before jumping to the real vectors.
1710  *
1711  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1712  * VHE, as we don't have hypervisor-specific mappings. If the system
1713  * is VHE and yet selects this capability, it will be ignored.
1714  */
cpu_set_hyp_vector(void)1715 static void cpu_set_hyp_vector(void)
1716 {
1717 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1718 	void *vector = hyp_spectre_vector_selector[data->slot];
1719 
1720 	if (!is_protected_kvm_enabled())
1721 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1722 	else
1723 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1724 }
1725 
cpu_hyp_init_context(void)1726 static void cpu_hyp_init_context(void)
1727 {
1728 	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
1729 
1730 	if (!is_kernel_in_hyp_mode())
1731 		cpu_init_hyp_mode();
1732 }
1733 
cpu_hyp_init_features(void)1734 static void cpu_hyp_init_features(void)
1735 {
1736 	cpu_set_hyp_vector();
1737 	kvm_arm_init_debug();
1738 
1739 	if (is_kernel_in_hyp_mode())
1740 		kvm_timer_init_vhe();
1741 
1742 	if (vgic_present)
1743 		kvm_vgic_init_cpu_hardware();
1744 }
1745 
cpu_hyp_reinit(void)1746 static void cpu_hyp_reinit(void)
1747 {
1748 	cpu_hyp_reset();
1749 	cpu_hyp_init_context();
1750 	cpu_hyp_init_features();
1751 }
1752 
_kvm_arch_hardware_enable(void * discard)1753 static void _kvm_arch_hardware_enable(void *discard)
1754 {
1755 	if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1756 		cpu_hyp_reinit();
1757 		__this_cpu_write(kvm_arm_hardware_enabled, 1);
1758 	}
1759 }
1760 
kvm_arch_hardware_enable(void)1761 int kvm_arch_hardware_enable(void)
1762 {
1763 	_kvm_arch_hardware_enable(NULL);
1764 	return 0;
1765 }
1766 
_kvm_arch_hardware_disable(void * discard)1767 static void _kvm_arch_hardware_disable(void *discard)
1768 {
1769 	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1770 		cpu_hyp_reset();
1771 		__this_cpu_write(kvm_arm_hardware_enabled, 0);
1772 	}
1773 }
1774 
kvm_arch_hardware_disable(void)1775 void kvm_arch_hardware_disable(void)
1776 {
1777 	if (!is_protected_kvm_enabled())
1778 		_kvm_arch_hardware_disable(NULL);
1779 }
1780 
1781 #ifdef CONFIG_CPU_PM
hyp_init_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)1782 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1783 				    unsigned long cmd,
1784 				    void *v)
1785 {
1786 	/*
1787 	 * kvm_arm_hardware_enabled is left with its old value over
1788 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1789 	 * re-enable hyp.
1790 	 */
1791 	switch (cmd) {
1792 	case CPU_PM_ENTER:
1793 		if (__this_cpu_read(kvm_arm_hardware_enabled))
1794 			/*
1795 			 * don't update kvm_arm_hardware_enabled here
1796 			 * so that the hardware will be re-enabled
1797 			 * when we resume. See below.
1798 			 */
1799 			cpu_hyp_reset();
1800 
1801 		return NOTIFY_OK;
1802 	case CPU_PM_ENTER_FAILED:
1803 	case CPU_PM_EXIT:
1804 		if (__this_cpu_read(kvm_arm_hardware_enabled))
1805 			/* The hardware was enabled before suspend. */
1806 			cpu_hyp_reinit();
1807 
1808 		return NOTIFY_OK;
1809 
1810 	default:
1811 		return NOTIFY_DONE;
1812 	}
1813 }
1814 
1815 static struct notifier_block hyp_init_cpu_pm_nb = {
1816 	.notifier_call = hyp_init_cpu_pm_notifier,
1817 };
1818 
hyp_cpu_pm_init(void)1819 static void hyp_cpu_pm_init(void)
1820 {
1821 	if (!is_protected_kvm_enabled())
1822 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1823 }
hyp_cpu_pm_exit(void)1824 static void hyp_cpu_pm_exit(void)
1825 {
1826 	if (!is_protected_kvm_enabled())
1827 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1828 }
1829 #else
hyp_cpu_pm_init(void)1830 static inline void hyp_cpu_pm_init(void)
1831 {
1832 }
hyp_cpu_pm_exit(void)1833 static inline void hyp_cpu_pm_exit(void)
1834 {
1835 }
1836 #endif
1837 
init_cpu_logical_map(void)1838 static void init_cpu_logical_map(void)
1839 {
1840 	unsigned int cpu;
1841 
1842 	/*
1843 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
1844 	 * Only copy the set of online CPUs whose features have been chacked
1845 	 * against the finalized system capabilities. The hypervisor will not
1846 	 * allow any other CPUs from the `possible` set to boot.
1847 	 */
1848 	for_each_online_cpu(cpu)
1849 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
1850 }
1851 
1852 #define init_psci_0_1_impl_state(config, what)	\
1853 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
1854 
init_psci_relay(void)1855 static bool init_psci_relay(void)
1856 {
1857 	/*
1858 	 * If PSCI has not been initialized, protected KVM cannot install
1859 	 * itself on newly booted CPUs.
1860 	 */
1861 	if (!psci_ops.get_version) {
1862 		kvm_err("Cannot initialize protected mode without PSCI\n");
1863 		return false;
1864 	}
1865 
1866 	kvm_host_psci_config.version = psci_ops.get_version();
1867 	kvm_host_psci_config.smccc_version = arm_smccc_get_version();
1868 
1869 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
1870 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
1871 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
1872 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
1873 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
1874 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
1875 	}
1876 	return true;
1877 }
1878 
init_subsystems(void)1879 static int init_subsystems(void)
1880 {
1881 	int err = 0;
1882 
1883 	/*
1884 	 * Enable hardware so that subsystem initialisation can access EL2.
1885 	 */
1886 	on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
1887 
1888 	/*
1889 	 * Register CPU lower-power notifier
1890 	 */
1891 	hyp_cpu_pm_init();
1892 
1893 	/*
1894 	 * Init HYP view of VGIC
1895 	 */
1896 	err = kvm_vgic_hyp_init();
1897 	switch (err) {
1898 	case 0:
1899 		vgic_present = true;
1900 		break;
1901 	case -ENODEV:
1902 	case -ENXIO:
1903 		vgic_present = false;
1904 		err = 0;
1905 		break;
1906 	default:
1907 		goto out;
1908 	}
1909 
1910 	/*
1911 	 * Init HYP architected timer support
1912 	 */
1913 	err = kvm_timer_hyp_init(vgic_present);
1914 	if (err)
1915 		goto out;
1916 
1917 	kvm_perf_init();
1918 	kvm_sys_reg_table_init();
1919 
1920 out:
1921 	if (err || !is_protected_kvm_enabled())
1922 		on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1923 
1924 	return err;
1925 }
1926 
teardown_hyp_mode(void)1927 static void teardown_hyp_mode(void)
1928 {
1929 	int cpu;
1930 
1931 	free_hyp_pgds();
1932 	for_each_possible_cpu(cpu) {
1933 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1934 		free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
1935 	}
1936 }
1937 
do_pkvm_init(u32 hyp_va_bits)1938 static int do_pkvm_init(u32 hyp_va_bits)
1939 {
1940 	void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
1941 	int ret;
1942 
1943 	preempt_disable();
1944 	cpu_hyp_init_context();
1945 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
1946 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
1947 				hyp_va_bits);
1948 	cpu_hyp_init_features();
1949 
1950 	/*
1951 	 * The stub hypercalls are now disabled, so set our local flag to
1952 	 * prevent a later re-init attempt in kvm_arch_hardware_enable().
1953 	 */
1954 	__this_cpu_write(kvm_arm_hardware_enabled, 1);
1955 	preempt_enable();
1956 
1957 	return ret;
1958 }
1959 
kvm_hyp_init_protection(u32 hyp_va_bits)1960 static int kvm_hyp_init_protection(u32 hyp_va_bits)
1961 {
1962 	void *addr = phys_to_virt(hyp_mem_base);
1963 	int ret;
1964 
1965 	kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
1966 	kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
1967 	kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
1968 	kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
1969 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
1970 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
1971 	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
1972 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
1973 	kvm_nvhe_sym(smccc_trng_available) = smccc_trng_available;
1974 
1975 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
1976 	if (ret)
1977 		return ret;
1978 
1979 	ret = do_pkvm_init(hyp_va_bits);
1980 	if (ret)
1981 		return ret;
1982 
1983 	free_hyp_pgds();
1984 
1985 	return 0;
1986 }
1987 
1988 /**
1989  * Inits Hyp-mode on all online CPUs
1990  */
init_hyp_mode(void)1991 static int init_hyp_mode(void)
1992 {
1993 	u32 hyp_va_bits;
1994 	int cpu;
1995 	int err = -ENOMEM;
1996 
1997 	/*
1998 	 * The protected Hyp-mode cannot be initialized if the memory pool
1999 	 * allocation has failed.
2000 	 */
2001 	if (is_protected_kvm_enabled() && !hyp_mem_base)
2002 		goto out_err;
2003 
2004 	/*
2005 	 * Allocate Hyp PGD and setup Hyp identity mapping
2006 	 */
2007 	err = kvm_mmu_init(&hyp_va_bits);
2008 	if (err)
2009 		goto out_err;
2010 
2011 	/*
2012 	 * Allocate stack pages for Hypervisor-mode
2013 	 */
2014 	for_each_possible_cpu(cpu) {
2015 		unsigned long stack_page;
2016 
2017 		stack_page = __get_free_page(GFP_KERNEL);
2018 		if (!stack_page) {
2019 			err = -ENOMEM;
2020 			goto out_err;
2021 		}
2022 
2023 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
2024 	}
2025 
2026 	/*
2027 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
2028 	 */
2029 	for_each_possible_cpu(cpu) {
2030 		struct page *page;
2031 		void *page_addr;
2032 
2033 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
2034 		if (!page) {
2035 			err = -ENOMEM;
2036 			goto out_err;
2037 		}
2038 
2039 		page_addr = page_address(page);
2040 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2041 		kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
2042 	}
2043 
2044 	/*
2045 	 * Map the Hyp-code called directly from the host
2046 	 */
2047 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
2048 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
2049 	if (err) {
2050 		kvm_err("Cannot map world-switch code\n");
2051 		goto out_err;
2052 	}
2053 
2054 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start),
2055 				  kvm_ksym_ref(__hyp_data_end), PAGE_HYP);
2056 	if (err) {
2057 		kvm_err("Cannot map .hyp.data section\n");
2058 		goto out_err;
2059 	}
2060 
2061 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
2062 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
2063 	if (err) {
2064 		kvm_err("Cannot map .hyp.rodata section\n");
2065 		goto out_err;
2066 	}
2067 
2068 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
2069 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
2070 	if (err) {
2071 		kvm_err("Cannot map rodata section\n");
2072 		goto out_err;
2073 	}
2074 
2075 	/*
2076 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2077 	 * section thanks to an assertion in the linker script. Map it RW and
2078 	 * the rest of .bss RO.
2079 	 */
2080 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2081 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2082 	if (err) {
2083 		kvm_err("Cannot map hyp bss section: %d\n", err);
2084 		goto out_err;
2085 	}
2086 
2087 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
2088 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
2089 	if (err) {
2090 		kvm_err("Cannot map bss section\n");
2091 		goto out_err;
2092 	}
2093 
2094 	/*
2095 	 * Map the Hyp stack pages
2096 	 */
2097 	for_each_possible_cpu(cpu) {
2098 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
2099 		err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
2100 					  PAGE_HYP);
2101 
2102 		if (err) {
2103 			kvm_err("Cannot map hyp stack\n");
2104 			goto out_err;
2105 		}
2106 	}
2107 
2108 	for_each_possible_cpu(cpu) {
2109 		char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
2110 		char *percpu_end = percpu_begin + nvhe_percpu_size();
2111 
2112 		/* Map Hyp percpu pages */
2113 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
2114 		if (err) {
2115 			kvm_err("Cannot map hyp percpu region\n");
2116 			goto out_err;
2117 		}
2118 
2119 		/* Prepare the CPU initialization parameters */
2120 		cpu_prepare_hyp_mode(cpu);
2121 	}
2122 
2123 	if (is_protected_kvm_enabled()) {
2124 		init_cpu_logical_map();
2125 
2126 		if (!init_psci_relay()) {
2127 			err = -ENODEV;
2128 			goto out_err;
2129 		}
2130 	}
2131 
2132 	if (is_protected_kvm_enabled()) {
2133 		err = kvm_hyp_init_protection(hyp_va_bits);
2134 		if (err) {
2135 			kvm_err("Failed to init hyp memory protection\n");
2136 			goto out_err;
2137 		}
2138 	}
2139 
2140 	return 0;
2141 
2142 out_err:
2143 	teardown_hyp_mode();
2144 	kvm_err("error initializing Hyp mode: %d\n", err);
2145 	return err;
2146 }
2147 
_kvm_host_prot_finalize(void * arg)2148 static void _kvm_host_prot_finalize(void *arg)
2149 {
2150 	int *err = arg;
2151 
2152 	if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)))
2153 		WRITE_ONCE(*err, -EINVAL);
2154 }
2155 
pkvm_drop_host_privileges(void)2156 static int pkvm_drop_host_privileges(void)
2157 {
2158 	int ret = 0;
2159 
2160 	/*
2161 	 * Flip the static key upfront as that may no longer be possible
2162 	 * once the host stage 2 is installed.
2163 	 */
2164 	static_branch_enable(&kvm_protected_mode_initialized);
2165 
2166 	/*
2167 	 * Fixup the boot mode so that we don't take spurious round
2168 	 * trips via EL2 on cpu_resume. Flush to the PoC for a good
2169 	 * measure, so that it can be observed by a CPU coming out of
2170 	 * suspend with the MMU off.
2171 	 */
2172 	__boot_cpu_mode[0] = __boot_cpu_mode[1] = BOOT_CPU_MODE_EL1;
2173 	dcache_clean_poc((unsigned long)__boot_cpu_mode,
2174 			 (unsigned long)(__boot_cpu_mode + 2));
2175 
2176 	on_each_cpu(_kvm_host_prot_finalize, &ret, 1);
2177 	return ret;
2178 }
2179 
finalize_hyp_mode(void)2180 static int finalize_hyp_mode(void)
2181 {
2182 	if (!is_protected_kvm_enabled())
2183 		return 0;
2184 
2185 	/*
2186 	 * Exclude HYP sections from kmemleak so that they don't get peeked
2187 	 * at, which would end badly once inaccessible.
2188 	 */
2189 	kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start);
2190 	kmemleak_free_part(__hyp_data_start, __hyp_data_end - __hyp_data_start);
2191 	kmemleak_free_part(__va(hyp_mem_base), hyp_mem_size);
2192 	return pkvm_drop_host_privileges();
2193 }
2194 
kvm_mpidr_to_vcpu(struct kvm * kvm,unsigned long mpidr)2195 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
2196 {
2197 	struct kvm_vcpu *vcpu;
2198 	int i;
2199 
2200 	mpidr &= MPIDR_HWID_BITMASK;
2201 	kvm_for_each_vcpu(i, vcpu, kvm) {
2202 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
2203 			return vcpu;
2204 	}
2205 	return NULL;
2206 }
2207 
kvm_arch_has_irq_bypass(void)2208 bool kvm_arch_has_irq_bypass(void)
2209 {
2210 	return true;
2211 }
2212 
kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)2213 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
2214 				      struct irq_bypass_producer *prod)
2215 {
2216 	struct kvm_kernel_irqfd *irqfd =
2217 		container_of(cons, struct kvm_kernel_irqfd, consumer);
2218 
2219 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
2220 					  &irqfd->irq_entry);
2221 }
kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)2222 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
2223 				      struct irq_bypass_producer *prod)
2224 {
2225 	struct kvm_kernel_irqfd *irqfd =
2226 		container_of(cons, struct kvm_kernel_irqfd, consumer);
2227 
2228 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
2229 				     &irqfd->irq_entry);
2230 }
2231 
kvm_arch_irq_bypass_stop(struct irq_bypass_consumer * cons)2232 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
2233 {
2234 	struct kvm_kernel_irqfd *irqfd =
2235 		container_of(cons, struct kvm_kernel_irqfd, consumer);
2236 
2237 	kvm_arm_halt_guest(irqfd->kvm);
2238 }
2239 
kvm_arch_irq_bypass_start(struct irq_bypass_consumer * cons)2240 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
2241 {
2242 	struct kvm_kernel_irqfd *irqfd =
2243 		container_of(cons, struct kvm_kernel_irqfd, consumer);
2244 
2245 	kvm_arm_resume_guest(irqfd->kvm);
2246 }
2247 
2248 /**
2249  * Initialize Hyp-mode and memory mappings on all CPUs.
2250  */
kvm_arch_init(void * opaque)2251 int kvm_arch_init(void *opaque)
2252 {
2253 	int err;
2254 	bool in_hyp_mode;
2255 
2256 	if (!is_hyp_mode_available()) {
2257 		kvm_info("HYP mode not available\n");
2258 		return -ENODEV;
2259 	}
2260 
2261 	if (kvm_get_mode() == KVM_MODE_NONE) {
2262 		kvm_info("KVM disabled from command line\n");
2263 		return -ENODEV;
2264 	}
2265 
2266 	in_hyp_mode = is_kernel_in_hyp_mode();
2267 
2268 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
2269 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2270 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2271 			 "Only trusted guests should be used on this system.\n");
2272 
2273 	err = kvm_set_ipa_limit();
2274 	if (err)
2275 		return err;
2276 
2277 	err = kvm_arm_init_sve();
2278 	if (err)
2279 		return err;
2280 
2281 	if (!in_hyp_mode) {
2282 		err = init_hyp_mode();
2283 		if (err)
2284 			goto out_err;
2285 	}
2286 
2287 	err = kvm_init_vector_slots();
2288 	if (err) {
2289 		kvm_err("Cannot initialise vector slots\n");
2290 		goto out_err;
2291 	}
2292 
2293 	err = init_subsystems();
2294 	if (err)
2295 		goto out_hyp;
2296 
2297 	if (!in_hyp_mode) {
2298 		err = finalize_hyp_mode();
2299 		if (err) {
2300 			kvm_err("Failed to finalize Hyp protection\n");
2301 			goto out_hyp;
2302 		}
2303 	}
2304 
2305 	if (is_protected_kvm_enabled()) {
2306 		kvm_info("Protected nVHE mode initialized successfully\n");
2307 	} else if (in_hyp_mode) {
2308 		kvm_info("VHE mode initialized successfully\n");
2309 	} else {
2310 		kvm_info("Hyp mode initialized successfully\n");
2311 	}
2312 
2313 	return 0;
2314 
2315 out_hyp:
2316 	hyp_cpu_pm_exit();
2317 	if (!in_hyp_mode)
2318 		teardown_hyp_mode();
2319 out_err:
2320 	return err;
2321 }
2322 
2323 /* NOP: Compiling as a module not supported */
kvm_arch_exit(void)2324 void kvm_arch_exit(void)
2325 {
2326 	kvm_perf_teardown();
2327 }
2328 
early_kvm_mode_cfg(char * arg)2329 static int __init early_kvm_mode_cfg(char *arg)
2330 {
2331 	if (!arg)
2332 		return -EINVAL;
2333 
2334 	if (strcmp(arg, "none") == 0) {
2335 		kvm_mode = KVM_MODE_NONE;
2336 		return 0;
2337 	}
2338 
2339 	if (!is_hyp_mode_available()) {
2340 		pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2341 		return 0;
2342 	}
2343 
2344 	if (strcmp(arg, "protected") == 0) {
2345 		if (!is_kernel_in_hyp_mode())
2346 			kvm_mode = KVM_MODE_PROTECTED;
2347 		else
2348 			pr_warn_once("Protected KVM not available with VHE\n");
2349 
2350 		return 0;
2351 	}
2352 
2353 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2354 		kvm_mode = KVM_MODE_DEFAULT;
2355 		return 0;
2356 	}
2357 
2358 	return -EINVAL;
2359 }
2360 early_param("kvm-arm.mode", early_kvm_mode_cfg);
2361 
kvm_get_mode(void)2362 enum kvm_mode kvm_get_mode(void)
2363 {
2364 	return kvm_mode;
2365 }
2366 
arm_init(void)2367 static int arm_init(void)
2368 {
2369 	int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2370 	return rc;
2371 }
2372 
2373 module_init(arm_init);
2374