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/errno.h>
10 #include <linux/err.h>
11 #include <linux/kvm_host.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/vmalloc.h>
15 #include <linux/fs.h>
16 #include <linux/mman.h>
17 #include <linux/sched.h>
18 #include <linux/kvm.h>
19 #include <linux/kvm_irqfd.h>
20 #include <linux/irqbypass.h>
21 #include <linux/sched/stat.h>
22 #include <linux/psci.h>
23 #include <trace/events/kvm.h>
24
25 #define CREATE_TRACE_POINTS
26 #include "trace_arm.h"
27
28 #include <linux/uaccess.h>
29 #include <asm/ptrace.h>
30 #include <asm/mman.h>
31 #include <asm/tlbflush.h>
32 #include <asm/cacheflush.h>
33 #include <asm/cpufeature.h>
34 #include <asm/virt.h>
35 #include <asm/kvm_arm.h>
36 #include <asm/kvm_asm.h>
37 #include <asm/kvm_emulate.h>
38 #include <asm/kvm_mmu.h>
39 #include <asm/kvm_nested.h>
40 #include <asm/kvm_pkvm.h>
41 #include <asm/kvm_ptrauth.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 #include "sys_regs.h"
49
50 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
51
52 enum kvm_wfx_trap_policy {
53 KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */
54 KVM_WFX_NOTRAP,
55 KVM_WFX_TRAP,
56 };
57
58 static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
59 static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
60
61 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
62
63 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
64 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
65
66 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
67
68 static bool vgic_present, kvm_arm_initialised;
69
70 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
71
is_kvm_arm_initialised(void)72 bool is_kvm_arm_initialised(void)
73 {
74 return kvm_arm_initialised;
75 }
76
kvm_arch_vcpu_should_kick(struct kvm_vcpu * vcpu)77 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
78 {
79 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
80 }
81
kvm_vm_ioctl_enable_cap(struct kvm * kvm,struct kvm_enable_cap * cap)82 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
83 struct kvm_enable_cap *cap)
84 {
85 int r = -EINVAL;
86
87 if (cap->flags)
88 return -EINVAL;
89
90 if (kvm_vm_is_protected(kvm) && !kvm_pvm_ext_allowed(cap->cap))
91 return -EINVAL;
92
93 switch (cap->cap) {
94 case KVM_CAP_ARM_NISV_TO_USER:
95 r = 0;
96 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
97 &kvm->arch.flags);
98 break;
99 case KVM_CAP_ARM_MTE:
100 mutex_lock(&kvm->lock);
101 if (system_supports_mte() && !kvm->created_vcpus) {
102 r = 0;
103 set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
104 }
105 mutex_unlock(&kvm->lock);
106 break;
107 case KVM_CAP_ARM_SYSTEM_SUSPEND:
108 r = 0;
109 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
110 break;
111 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
112 mutex_lock(&kvm->slots_lock);
113 /*
114 * To keep things simple, allow changing the chunk
115 * size only when no memory slots have been created.
116 */
117 if (kvm_are_all_memslots_empty(kvm)) {
118 u64 new_cap = cap->args[0];
119
120 if (!new_cap || kvm_is_block_size_supported(new_cap)) {
121 r = 0;
122 kvm->arch.mmu.split_page_chunk_size = new_cap;
123 }
124 }
125 mutex_unlock(&kvm->slots_lock);
126 break;
127 case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
128 mutex_lock(&kvm->lock);
129 if (!kvm->created_vcpus) {
130 r = 0;
131 set_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags);
132 }
133 mutex_unlock(&kvm->lock);
134 break;
135 case KVM_CAP_ARM_SEA_TO_USER:
136 r = 0;
137 set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
138 break;
139 default:
140 break;
141 }
142
143 return r;
144 }
145
kvm_arm_default_max_vcpus(void)146 static int kvm_arm_default_max_vcpus(void)
147 {
148 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
149 }
150
151 /**
152 * kvm_arch_init_vm - initializes a VM data structure
153 * @kvm: pointer to the KVM struct
154 * @type: kvm device type
155 */
kvm_arch_init_vm(struct kvm * kvm,unsigned long type)156 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
157 {
158 int ret;
159
160 mutex_init(&kvm->arch.config_lock);
161
162 #ifdef CONFIG_LOCKDEP
163 /* Clue in lockdep that the config_lock must be taken inside kvm->lock */
164 mutex_lock(&kvm->lock);
165 mutex_lock(&kvm->arch.config_lock);
166 mutex_unlock(&kvm->arch.config_lock);
167 mutex_unlock(&kvm->lock);
168 #endif
169
170 kvm_init_nested(kvm);
171
172 ret = kvm_share_hyp(kvm, kvm + 1);
173 if (ret)
174 return ret;
175
176 if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
177 ret = -ENOMEM;
178 goto err_unshare_kvm;
179 }
180 cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
181
182 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
183 if (ret)
184 goto err_free_cpumask;
185
186 if (is_protected_kvm_enabled()) {
187 /*
188 * If any failures occur after this is successful, make sure to
189 * call __pkvm_unreserve_vm to unreserve the VM in hyp.
190 */
191 ret = pkvm_init_host_vm(kvm);
192 if (ret)
193 goto err_free_cpumask;
194 }
195
196 kvm_vgic_early_init(kvm);
197
198 kvm_timer_init_vm(kvm);
199
200 /* The maximum number of VCPUs is limited by the host's GIC model */
201 kvm->max_vcpus = kvm_arm_default_max_vcpus();
202
203 kvm_arm_init_hypercalls(kvm);
204
205 bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
206
207 return 0;
208
209 err_free_cpumask:
210 free_cpumask_var(kvm->arch.supported_cpus);
211 err_unshare_kvm:
212 kvm_unshare_hyp(kvm, kvm + 1);
213 return ret;
214 }
215
kvm_arch_vcpu_fault(struct kvm_vcpu * vcpu,struct vm_fault * vmf)216 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
217 {
218 return VM_FAULT_SIGBUS;
219 }
220
kvm_arch_create_vm_debugfs(struct kvm * kvm)221 void kvm_arch_create_vm_debugfs(struct kvm *kvm)
222 {
223 kvm_sys_regs_create_debugfs(kvm);
224 kvm_s2_ptdump_create_debugfs(kvm);
225 }
226
kvm_destroy_mpidr_data(struct kvm * kvm)227 static void kvm_destroy_mpidr_data(struct kvm *kvm)
228 {
229 struct kvm_mpidr_data *data;
230
231 mutex_lock(&kvm->arch.config_lock);
232
233 data = rcu_dereference_protected(kvm->arch.mpidr_data,
234 lockdep_is_held(&kvm->arch.config_lock));
235 if (data) {
236 rcu_assign_pointer(kvm->arch.mpidr_data, NULL);
237 synchronize_rcu();
238 kfree(data);
239 }
240
241 mutex_unlock(&kvm->arch.config_lock);
242 }
243
244 /**
245 * kvm_arch_destroy_vm - destroy the VM data structure
246 * @kvm: pointer to the KVM struct
247 */
kvm_arch_destroy_vm(struct kvm * kvm)248 void kvm_arch_destroy_vm(struct kvm *kvm)
249 {
250 bitmap_free(kvm->arch.pmu_filter);
251 free_cpumask_var(kvm->arch.supported_cpus);
252
253 kvm_vgic_destroy(kvm);
254
255 if (is_protected_kvm_enabled())
256 pkvm_destroy_hyp_vm(kvm);
257
258 kvm_destroy_mpidr_data(kvm);
259
260 kfree(kvm->arch.sysreg_masks);
261 kvm_destroy_vcpus(kvm);
262
263 kvm_unshare_hyp(kvm, kvm + 1);
264
265 kvm_arm_teardown_hypercalls(kvm);
266 }
267
kvm_has_full_ptr_auth(void)268 static bool kvm_has_full_ptr_auth(void)
269 {
270 bool apa, gpa, api, gpi, apa3, gpa3;
271 u64 isar1, isar2, val;
272
273 /*
274 * Check that:
275 *
276 * - both Address and Generic auth are implemented for a given
277 * algorithm (Q5, IMPDEF or Q3)
278 * - only a single algorithm is implemented.
279 */
280 if (!system_has_full_ptr_auth())
281 return false;
282
283 isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
284 isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
285
286 apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1);
287 val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1);
288 gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP);
289
290 api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1);
291 val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1);
292 gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP);
293
294 apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2);
295 val = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2);
296 gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP);
297
298 return (apa == gpa && api == gpi && apa3 == gpa3 &&
299 (apa + api + apa3) == 1);
300 }
301
kvm_vm_ioctl_check_extension(struct kvm * kvm,long ext)302 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
303 {
304 int r;
305
306 if (kvm && kvm_vm_is_protected(kvm) && !kvm_pvm_ext_allowed(ext))
307 return 0;
308
309 switch (ext) {
310 case KVM_CAP_IRQCHIP:
311 r = vgic_present;
312 break;
313 case KVM_CAP_IOEVENTFD:
314 case KVM_CAP_USER_MEMORY:
315 case KVM_CAP_SYNC_MMU:
316 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
317 case KVM_CAP_ONE_REG:
318 case KVM_CAP_ARM_PSCI:
319 case KVM_CAP_ARM_PSCI_0_2:
320 case KVM_CAP_READONLY_MEM:
321 case KVM_CAP_MP_STATE:
322 case KVM_CAP_IMMEDIATE_EXIT:
323 case KVM_CAP_VCPU_EVENTS:
324 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
325 case KVM_CAP_ARM_NISV_TO_USER:
326 case KVM_CAP_ARM_INJECT_EXT_DABT:
327 case KVM_CAP_SET_GUEST_DEBUG:
328 case KVM_CAP_VCPU_ATTRIBUTES:
329 case KVM_CAP_PTP_KVM:
330 case KVM_CAP_ARM_SYSTEM_SUSPEND:
331 case KVM_CAP_IRQFD_RESAMPLE:
332 case KVM_CAP_COUNTER_OFFSET:
333 case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
334 case KVM_CAP_ARM_SEA_TO_USER:
335 r = 1;
336 break;
337 case KVM_CAP_SET_GUEST_DEBUG2:
338 return KVM_GUESTDBG_VALID_MASK;
339 case KVM_CAP_ARM_SET_DEVICE_ADDR:
340 r = 1;
341 break;
342 case KVM_CAP_NR_VCPUS:
343 /*
344 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
345 * architectures, as it does not always bound it to
346 * KVM_CAP_MAX_VCPUS. It should not matter much because
347 * this is just an advisory value.
348 */
349 r = min_t(unsigned int, num_online_cpus(),
350 kvm_arm_default_max_vcpus());
351 break;
352 case KVM_CAP_MAX_VCPUS:
353 case KVM_CAP_MAX_VCPU_ID:
354 if (kvm)
355 r = kvm->max_vcpus;
356 else
357 r = kvm_arm_default_max_vcpus();
358 break;
359 case KVM_CAP_MSI_DEVID:
360 if (!kvm)
361 r = -EINVAL;
362 else
363 r = kvm->arch.vgic.msis_require_devid;
364 break;
365 case KVM_CAP_ARM_USER_IRQ:
366 /*
367 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
368 * (bump this number if adding more devices)
369 */
370 r = 1;
371 break;
372 case KVM_CAP_ARM_MTE:
373 r = system_supports_mte();
374 break;
375 case KVM_CAP_STEAL_TIME:
376 r = kvm_arm_pvtime_supported();
377 break;
378 case KVM_CAP_ARM_EL1_32BIT:
379 r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1);
380 break;
381 case KVM_CAP_ARM_EL2:
382 r = cpus_have_final_cap(ARM64_HAS_NESTED_VIRT);
383 break;
384 case KVM_CAP_ARM_EL2_E2H0:
385 r = cpus_have_final_cap(ARM64_HAS_HCR_NV1);
386 break;
387 case KVM_CAP_GUEST_DEBUG_HW_BPS:
388 r = get_num_brps();
389 break;
390 case KVM_CAP_GUEST_DEBUG_HW_WPS:
391 r = get_num_wrps();
392 break;
393 case KVM_CAP_ARM_PMU_V3:
394 r = kvm_supports_guest_pmuv3();
395 break;
396 case KVM_CAP_ARM_INJECT_SERROR_ESR:
397 r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN);
398 break;
399 case KVM_CAP_ARM_VM_IPA_SIZE:
400 r = get_kvm_ipa_limit();
401 break;
402 case KVM_CAP_ARM_SVE:
403 r = system_supports_sve();
404 break;
405 case KVM_CAP_ARM_PTRAUTH_ADDRESS:
406 case KVM_CAP_ARM_PTRAUTH_GENERIC:
407 r = kvm_has_full_ptr_auth();
408 break;
409 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
410 if (kvm)
411 r = kvm->arch.mmu.split_page_chunk_size;
412 else
413 r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
414 break;
415 case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
416 r = kvm_supported_block_sizes();
417 break;
418 case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
419 r = BIT(0);
420 break;
421 case KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED:
422 if (!kvm)
423 r = -EINVAL;
424 else
425 r = kvm_supports_cacheable_pfnmap();
426 break;
427
428 default:
429 r = 0;
430 }
431
432 return r;
433 }
434
kvm_arch_dev_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)435 long kvm_arch_dev_ioctl(struct file *filp,
436 unsigned int ioctl, unsigned long arg)
437 {
438 return -EINVAL;
439 }
440
kvm_arch_alloc_vm(void)441 struct kvm *kvm_arch_alloc_vm(void)
442 {
443 size_t sz = sizeof(struct kvm);
444
445 if (!has_vhe())
446 return kzalloc(sz, GFP_KERNEL_ACCOUNT);
447
448 return kvzalloc(sz, GFP_KERNEL_ACCOUNT);
449 }
450
kvm_arch_vcpu_precreate(struct kvm * kvm,unsigned int id)451 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
452 {
453 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
454 return -EBUSY;
455
456 if (id >= kvm->max_vcpus)
457 return -EINVAL;
458
459 return 0;
460 }
461
kvm_arch_vcpu_create(struct kvm_vcpu * vcpu)462 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
463 {
464 int err;
465
466 spin_lock_init(&vcpu->arch.mp_state_lock);
467
468 #ifdef CONFIG_LOCKDEP
469 /* Inform lockdep that the config_lock is acquired after vcpu->mutex */
470 mutex_lock(&vcpu->mutex);
471 mutex_lock(&vcpu->kvm->arch.config_lock);
472 mutex_unlock(&vcpu->kvm->arch.config_lock);
473 mutex_unlock(&vcpu->mutex);
474 #endif
475
476 /* Force users to call KVM_ARM_VCPU_INIT */
477 vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
478
479 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
480
481 /* Set up the timer */
482 kvm_timer_vcpu_init(vcpu);
483
484 kvm_pmu_vcpu_init(vcpu);
485
486 kvm_arm_pvtime_vcpu_init(&vcpu->arch);
487
488 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
489
490 /*
491 * This vCPU may have been created after mpidr_data was initialized.
492 * Throw out the pre-computed mappings if that is the case which forces
493 * KVM to fall back to iteratively searching the vCPUs.
494 */
495 kvm_destroy_mpidr_data(vcpu->kvm);
496
497 err = kvm_vgic_vcpu_init(vcpu);
498 if (err)
499 return err;
500
501 err = kvm_share_hyp(vcpu, vcpu + 1);
502 if (err)
503 kvm_vgic_vcpu_destroy(vcpu);
504
505 return err;
506 }
507
kvm_arch_vcpu_postcreate(struct kvm_vcpu * vcpu)508 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
509 {
510 }
511
kvm_arch_vcpu_destroy(struct kvm_vcpu * vcpu)512 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
513 {
514 if (!is_protected_kvm_enabled())
515 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
516 else
517 free_hyp_memcache(&vcpu->arch.pkvm_memcache);
518 kvm_timer_vcpu_terminate(vcpu);
519 kvm_pmu_vcpu_destroy(vcpu);
520 kvm_vgic_vcpu_destroy(vcpu);
521 kvm_arm_vcpu_destroy(vcpu);
522 }
523
kvm_arch_vcpu_blocking(struct kvm_vcpu * vcpu)524 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
525 {
526
527 }
528
kvm_arch_vcpu_unblocking(struct kvm_vcpu * vcpu)529 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
530 {
531
532 }
533
vcpu_set_pauth_traps(struct kvm_vcpu * vcpu)534 static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu)
535 {
536 if (vcpu_has_ptrauth(vcpu) && !is_protected_kvm_enabled()) {
537 /*
538 * Either we're running an L2 guest, and the API/APK bits come
539 * from L1's HCR_EL2, or API/APK are both set.
540 */
541 if (unlikely(is_nested_ctxt(vcpu))) {
542 u64 val;
543
544 val = __vcpu_sys_reg(vcpu, HCR_EL2);
545 val &= (HCR_API | HCR_APK);
546 vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK);
547 vcpu->arch.hcr_el2 |= val;
548 } else {
549 vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
550 }
551
552 /*
553 * Save the host keys if there is any chance for the guest
554 * to use pauth, as the entry code will reload the guest
555 * keys in that case.
556 */
557 if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) {
558 struct kvm_cpu_context *ctxt;
559
560 ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt);
561 ptrauth_save_keys(ctxt);
562 }
563 }
564 }
565
kvm_vcpu_should_clear_twi(struct kvm_vcpu * vcpu)566 static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu)
567 {
568 if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
569 return kvm_wfi_trap_policy == KVM_WFX_NOTRAP;
570
571 return single_task_running() &&
572 (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
573 vcpu->kvm->arch.vgic.nassgireq);
574 }
575
kvm_vcpu_should_clear_twe(struct kvm_vcpu * vcpu)576 static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu)
577 {
578 if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
579 return kvm_wfe_trap_policy == KVM_WFX_NOTRAP;
580
581 return single_task_running();
582 }
583
kvm_arch_vcpu_load(struct kvm_vcpu * vcpu,int cpu)584 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
585 {
586 struct kvm_s2_mmu *mmu;
587 int *last_ran;
588
589 if (is_protected_kvm_enabled())
590 goto nommu;
591
592 if (vcpu_has_nv(vcpu))
593 kvm_vcpu_load_hw_mmu(vcpu);
594
595 mmu = vcpu->arch.hw_mmu;
596 last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
597
598 /*
599 * Ensure a VMID is allocated for the MMU before programming VTTBR_EL2,
600 * which happens eagerly in VHE.
601 *
602 * Also, the VMID allocator only preserves VMIDs that are active at the
603 * time of rollover, so KVM might need to grab a new VMID for the MMU if
604 * this is called from kvm_sched_in().
605 */
606 kvm_arm_vmid_update(&mmu->vmid);
607
608 /*
609 * We guarantee that both TLBs and I-cache are private to each
610 * vcpu. If detecting that a vcpu from the same VM has
611 * previously run on the same physical CPU, call into the
612 * hypervisor code to nuke the relevant contexts.
613 *
614 * We might get preempted before the vCPU actually runs, but
615 * over-invalidation doesn't affect correctness.
616 */
617 if (*last_ran != vcpu->vcpu_idx) {
618 kvm_call_hyp(__kvm_flush_cpu_context, mmu);
619 *last_ran = vcpu->vcpu_idx;
620 }
621
622 nommu:
623 vcpu->cpu = cpu;
624
625 /*
626 * The timer must be loaded before the vgic to correctly set up physical
627 * interrupt deactivation in nested state (e.g. timer interrupt).
628 */
629 kvm_timer_vcpu_load(vcpu);
630 kvm_vgic_load(vcpu);
631 kvm_vcpu_load_debug(vcpu);
632 kvm_vcpu_load_fgt(vcpu);
633 if (has_vhe())
634 kvm_vcpu_load_vhe(vcpu);
635 kvm_arch_vcpu_load_fp(vcpu);
636 kvm_vcpu_pmu_restore_guest(vcpu);
637 if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
638 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
639
640 if (kvm_vcpu_should_clear_twe(vcpu))
641 vcpu->arch.hcr_el2 &= ~HCR_TWE;
642 else
643 vcpu->arch.hcr_el2 |= HCR_TWE;
644
645 if (kvm_vcpu_should_clear_twi(vcpu))
646 vcpu->arch.hcr_el2 &= ~HCR_TWI;
647 else
648 vcpu->arch.hcr_el2 |= HCR_TWI;
649
650 vcpu_set_pauth_traps(vcpu);
651
652 if (is_protected_kvm_enabled()) {
653 kvm_call_hyp_nvhe(__pkvm_vcpu_load,
654 vcpu->kvm->arch.pkvm.handle,
655 vcpu->vcpu_idx, vcpu->arch.hcr_el2);
656 kvm_call_hyp(__vgic_v3_restore_vmcr_aprs,
657 &vcpu->arch.vgic_cpu.vgic_v3);
658 }
659
660 if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
661 vcpu_set_on_unsupported_cpu(vcpu);
662 }
663
kvm_arch_vcpu_put(struct kvm_vcpu * vcpu)664 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
665 {
666 if (is_protected_kvm_enabled()) {
667 kvm_call_hyp(__vgic_v3_save_aprs, &vcpu->arch.vgic_cpu.vgic_v3);
668 kvm_call_hyp_nvhe(__pkvm_vcpu_put);
669 }
670
671 kvm_vcpu_put_debug(vcpu);
672 kvm_arch_vcpu_put_fp(vcpu);
673 if (has_vhe())
674 kvm_vcpu_put_vhe(vcpu);
675 kvm_timer_vcpu_put(vcpu);
676 kvm_vgic_put(vcpu);
677 kvm_vcpu_pmu_restore_host(vcpu);
678 if (vcpu_has_nv(vcpu))
679 kvm_vcpu_put_hw_mmu(vcpu);
680 kvm_arm_vmid_clear_active();
681
682 vcpu_clear_on_unsupported_cpu(vcpu);
683 vcpu->cpu = -1;
684 }
685
__kvm_arm_vcpu_power_off(struct kvm_vcpu * vcpu)686 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
687 {
688 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
689 kvm_make_request(KVM_REQ_SLEEP, vcpu);
690 kvm_vcpu_kick(vcpu);
691 }
692
kvm_arm_vcpu_power_off(struct kvm_vcpu * vcpu)693 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
694 {
695 spin_lock(&vcpu->arch.mp_state_lock);
696 __kvm_arm_vcpu_power_off(vcpu);
697 spin_unlock(&vcpu->arch.mp_state_lock);
698 }
699
kvm_arm_vcpu_stopped(struct kvm_vcpu * vcpu)700 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
701 {
702 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
703 }
704
kvm_arm_vcpu_suspend(struct kvm_vcpu * vcpu)705 static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
706 {
707 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
708 kvm_make_request(KVM_REQ_SUSPEND, vcpu);
709 kvm_vcpu_kick(vcpu);
710 }
711
kvm_arm_vcpu_suspended(struct kvm_vcpu * vcpu)712 static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
713 {
714 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
715 }
716
kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)717 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
718 struct kvm_mp_state *mp_state)
719 {
720 *mp_state = READ_ONCE(vcpu->arch.mp_state);
721
722 return 0;
723 }
724
kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)725 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
726 struct kvm_mp_state *mp_state)
727 {
728 int ret = 0;
729
730 spin_lock(&vcpu->arch.mp_state_lock);
731
732 switch (mp_state->mp_state) {
733 case KVM_MP_STATE_RUNNABLE:
734 WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
735 break;
736 case KVM_MP_STATE_STOPPED:
737 __kvm_arm_vcpu_power_off(vcpu);
738 break;
739 case KVM_MP_STATE_SUSPENDED:
740 kvm_arm_vcpu_suspend(vcpu);
741 break;
742 default:
743 ret = -EINVAL;
744 }
745
746 spin_unlock(&vcpu->arch.mp_state_lock);
747
748 return ret;
749 }
750
751 /**
752 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
753 * @v: The VCPU pointer
754 *
755 * If the guest CPU is not waiting for interrupts or an interrupt line is
756 * asserted, the CPU is by definition runnable.
757 */
kvm_arch_vcpu_runnable(struct kvm_vcpu * v)758 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
759 {
760 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF | HCR_VSE);
761
762 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
763 && !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
764 }
765
kvm_arch_vcpu_in_kernel(struct kvm_vcpu * vcpu)766 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
767 {
768 return vcpu_mode_priv(vcpu);
769 }
770
771 #ifdef CONFIG_GUEST_PERF_EVENTS
kvm_arch_vcpu_get_ip(struct kvm_vcpu * vcpu)772 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
773 {
774 return *vcpu_pc(vcpu);
775 }
776 #endif
777
kvm_init_mpidr_data(struct kvm * kvm)778 static void kvm_init_mpidr_data(struct kvm *kvm)
779 {
780 struct kvm_mpidr_data *data = NULL;
781 unsigned long c, mask, nr_entries;
782 u64 aff_set = 0, aff_clr = ~0UL;
783 struct kvm_vcpu *vcpu;
784
785 mutex_lock(&kvm->arch.config_lock);
786
787 if (rcu_access_pointer(kvm->arch.mpidr_data) ||
788 atomic_read(&kvm->online_vcpus) == 1)
789 goto out;
790
791 kvm_for_each_vcpu(c, vcpu, kvm) {
792 u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
793 aff_set |= aff;
794 aff_clr &= aff;
795 }
796
797 /*
798 * A significant bit can be either 0 or 1, and will only appear in
799 * aff_set. Use aff_clr to weed out the useless stuff.
800 */
801 mask = aff_set ^ aff_clr;
802 nr_entries = BIT_ULL(hweight_long(mask));
803
804 /*
805 * Don't let userspace fool us. If we need more than a single page
806 * to describe the compressed MPIDR array, just fall back to the
807 * iterative method. Single vcpu VMs do not need this either.
808 */
809 if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE)
810 data = kzalloc(struct_size(data, cmpidr_to_idx, nr_entries),
811 GFP_KERNEL_ACCOUNT);
812
813 if (!data)
814 goto out;
815
816 data->mpidr_mask = mask;
817
818 kvm_for_each_vcpu(c, vcpu, kvm) {
819 u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
820 u16 index = kvm_mpidr_index(data, aff);
821
822 data->cmpidr_to_idx[index] = c;
823 }
824
825 rcu_assign_pointer(kvm->arch.mpidr_data, data);
826 out:
827 mutex_unlock(&kvm->arch.config_lock);
828 }
829
830 /*
831 * Handle both the initialisation that is being done when the vcpu is
832 * run for the first time, as well as the updates that must be
833 * performed each time we get a new thread dealing with this vcpu.
834 */
kvm_arch_vcpu_run_pid_change(struct kvm_vcpu * vcpu)835 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
836 {
837 struct kvm *kvm = vcpu->kvm;
838 int ret;
839
840 if (!kvm_vcpu_initialized(vcpu))
841 return -ENOEXEC;
842
843 if (!kvm_arm_vcpu_is_finalized(vcpu))
844 return -EPERM;
845
846 if (likely(vcpu_has_run_once(vcpu)))
847 return 0;
848
849 kvm_init_mpidr_data(kvm);
850
851 if (likely(irqchip_in_kernel(kvm))) {
852 /*
853 * Map the VGIC hardware resources before running a vcpu the
854 * first time on this VM.
855 */
856 ret = kvm_vgic_map_resources(kvm);
857 if (ret)
858 return ret;
859 }
860
861 ret = kvm_finalize_sys_regs(vcpu);
862 if (ret)
863 return ret;
864
865 if (vcpu_has_nv(vcpu)) {
866 ret = kvm_vcpu_allocate_vncr_tlb(vcpu);
867 if (ret)
868 return ret;
869
870 ret = kvm_vgic_vcpu_nv_init(vcpu);
871 if (ret)
872 return ret;
873 }
874
875 /*
876 * This needs to happen after any restriction has been applied
877 * to the feature set.
878 */
879 kvm_calculate_traps(vcpu);
880
881 ret = kvm_timer_enable(vcpu);
882 if (ret)
883 return ret;
884
885 if (kvm_vcpu_has_pmu(vcpu)) {
886 ret = kvm_arm_pmu_v3_enable(vcpu);
887 if (ret)
888 return ret;
889 }
890
891 if (is_protected_kvm_enabled()) {
892 ret = pkvm_create_hyp_vm(kvm);
893 if (ret)
894 return ret;
895
896 ret = pkvm_create_hyp_vcpu(vcpu);
897 if (ret)
898 return ret;
899 }
900
901 mutex_lock(&kvm->arch.config_lock);
902 set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
903 mutex_unlock(&kvm->arch.config_lock);
904
905 return ret;
906 }
907
kvm_arch_intc_initialized(struct kvm * kvm)908 bool kvm_arch_intc_initialized(struct kvm *kvm)
909 {
910 return vgic_initialized(kvm);
911 }
912
kvm_arm_halt_guest(struct kvm * kvm)913 void kvm_arm_halt_guest(struct kvm *kvm)
914 {
915 unsigned long i;
916 struct kvm_vcpu *vcpu;
917
918 kvm_for_each_vcpu(i, vcpu, kvm)
919 vcpu->arch.pause = true;
920 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
921 }
922
kvm_arm_resume_guest(struct kvm * kvm)923 void kvm_arm_resume_guest(struct kvm *kvm)
924 {
925 unsigned long i;
926 struct kvm_vcpu *vcpu;
927
928 kvm_for_each_vcpu(i, vcpu, kvm) {
929 vcpu->arch.pause = false;
930 __kvm_vcpu_wake_up(vcpu);
931 }
932 }
933
kvm_vcpu_sleep(struct kvm_vcpu * vcpu)934 static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
935 {
936 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
937
938 rcuwait_wait_event(wait,
939 (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
940 TASK_INTERRUPTIBLE);
941
942 if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
943 /* Awaken to handle a signal, request we sleep again later. */
944 kvm_make_request(KVM_REQ_SLEEP, vcpu);
945 }
946
947 /*
948 * Make sure we will observe a potential reset request if we've
949 * observed a change to the power state. Pairs with the smp_wmb() in
950 * kvm_psci_vcpu_on().
951 */
952 smp_rmb();
953 }
954
955 /**
956 * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
957 * @vcpu: The VCPU pointer
958 *
959 * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
960 * the vCPU is runnable. The vCPU may or may not be scheduled out, depending
961 * on when a wake event arrives, e.g. there may already be a pending wake event.
962 */
kvm_vcpu_wfi(struct kvm_vcpu * vcpu)963 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
964 {
965 /*
966 * Sync back the state of the GIC CPU interface so that we have
967 * the latest PMR and group enables. This ensures that
968 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
969 * we have pending interrupts, e.g. when determining if the
970 * vCPU should block.
971 *
972 * For the same reason, we want to tell GICv4 that we need
973 * doorbells to be signalled, should an interrupt become pending.
974 */
975 preempt_disable();
976 vcpu_set_flag(vcpu, IN_WFI);
977 kvm_vgic_put(vcpu);
978 preempt_enable();
979
980 kvm_vcpu_halt(vcpu);
981 vcpu_clear_flag(vcpu, IN_WFIT);
982
983 preempt_disable();
984 vcpu_clear_flag(vcpu, IN_WFI);
985 kvm_vgic_load(vcpu);
986 preempt_enable();
987 }
988
kvm_vcpu_suspend(struct kvm_vcpu * vcpu)989 static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
990 {
991 if (!kvm_arm_vcpu_suspended(vcpu))
992 return 1;
993
994 kvm_vcpu_wfi(vcpu);
995
996 /*
997 * The suspend state is sticky; we do not leave it until userspace
998 * explicitly marks the vCPU as runnable. Request that we suspend again
999 * later.
1000 */
1001 kvm_make_request(KVM_REQ_SUSPEND, vcpu);
1002
1003 /*
1004 * Check to make sure the vCPU is actually runnable. If so, exit to
1005 * userspace informing it of the wakeup condition.
1006 */
1007 if (kvm_arch_vcpu_runnable(vcpu)) {
1008 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
1009 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
1010 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
1011 return 0;
1012 }
1013
1014 /*
1015 * Otherwise, we were unblocked to process a different event, such as a
1016 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
1017 * process the event.
1018 */
1019 return 1;
1020 }
1021
1022 /**
1023 * check_vcpu_requests - check and handle pending vCPU requests
1024 * @vcpu: the VCPU pointer
1025 *
1026 * Return: 1 if we should enter the guest
1027 * 0 if we should exit to userspace
1028 * < 0 if we should exit to userspace, where the return value indicates
1029 * an error
1030 */
check_vcpu_requests(struct kvm_vcpu * vcpu)1031 static int check_vcpu_requests(struct kvm_vcpu *vcpu)
1032 {
1033 if (kvm_request_pending(vcpu)) {
1034 if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu))
1035 return -EIO;
1036
1037 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
1038 kvm_vcpu_sleep(vcpu);
1039
1040 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
1041 kvm_reset_vcpu(vcpu);
1042
1043 /*
1044 * Clear IRQ_PENDING requests that were made to guarantee
1045 * that a VCPU sees new virtual interrupts.
1046 */
1047 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
1048
1049 /* Process interrupts deactivated through a trap */
1050 if (kvm_check_request(KVM_REQ_VGIC_PROCESS_UPDATE, vcpu))
1051 kvm_vgic_process_async_update(vcpu);
1052
1053 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
1054 kvm_update_stolen_time(vcpu);
1055
1056 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
1057 /* The distributor enable bits were changed */
1058 preempt_disable();
1059 vgic_v4_put(vcpu);
1060 vgic_v4_load(vcpu);
1061 preempt_enable();
1062 }
1063
1064 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
1065 kvm_vcpu_reload_pmu(vcpu);
1066
1067 if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu))
1068 kvm_vcpu_pmu_restore_guest(vcpu);
1069
1070 if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
1071 return kvm_vcpu_suspend(vcpu);
1072
1073 if (kvm_dirty_ring_check_request(vcpu))
1074 return 0;
1075
1076 check_nested_vcpu_requests(vcpu);
1077 }
1078
1079 return 1;
1080 }
1081
vcpu_mode_is_bad_32bit(struct kvm_vcpu * vcpu)1082 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
1083 {
1084 if (likely(!vcpu_mode_is_32bit(vcpu)))
1085 return false;
1086
1087 if (vcpu_has_nv(vcpu))
1088 return true;
1089
1090 return !kvm_supports_32bit_el0();
1091 }
1092
1093 /**
1094 * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
1095 * @vcpu: The VCPU pointer
1096 * @ret: Pointer to write optional return code
1097 *
1098 * Returns: true if the VCPU needs to return to a preemptible + interruptible
1099 * and skip guest entry.
1100 *
1101 * This function disambiguates between two different types of exits: exits to a
1102 * preemptible + interruptible kernel context and exits to userspace. For an
1103 * exit to userspace, this function will write the return code to ret and return
1104 * true. For an exit to preemptible + interruptible kernel context (i.e. check
1105 * for pending work and re-enter), return true without writing to ret.
1106 */
kvm_vcpu_exit_request(struct kvm_vcpu * vcpu,int * ret)1107 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
1108 {
1109 struct kvm_run *run = vcpu->run;
1110
1111 /*
1112 * If we're using a userspace irqchip, then check if we need
1113 * to tell a userspace irqchip about timer or PMU level
1114 * changes and if so, exit to userspace (the actual level
1115 * state gets updated in kvm_timer_update_run and
1116 * kvm_pmu_update_run below).
1117 */
1118 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
1119 if (kvm_timer_should_notify_user(vcpu) ||
1120 kvm_pmu_should_notify_user(vcpu)) {
1121 *ret = -EINTR;
1122 run->exit_reason = KVM_EXIT_INTR;
1123 return true;
1124 }
1125 }
1126
1127 if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
1128 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1129 run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
1130 run->fail_entry.cpu = smp_processor_id();
1131 *ret = 0;
1132 return true;
1133 }
1134
1135 return kvm_request_pending(vcpu) ||
1136 xfer_to_guest_mode_work_pending();
1137 }
1138
1139 /*
1140 * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
1141 * the vCPU is running.
1142 *
1143 * This must be noinstr as instrumentation may make use of RCU, and this is not
1144 * safe during the EQS.
1145 */
kvm_arm_vcpu_enter_exit(struct kvm_vcpu * vcpu)1146 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
1147 {
1148 int ret;
1149
1150 guest_state_enter_irqoff();
1151 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
1152 guest_state_exit_irqoff();
1153
1154 return ret;
1155 }
1156
1157 /**
1158 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
1159 * @vcpu: The VCPU pointer
1160 *
1161 * This function is called through the VCPU_RUN ioctl called from user space. It
1162 * will execute VM code in a loop until the time slice for the process is used
1163 * or some emulation is needed from user space in which case the function will
1164 * return with return value 0 and with the kvm_run structure filled in with the
1165 * required data for the requested emulation.
1166 */
kvm_arch_vcpu_ioctl_run(struct kvm_vcpu * vcpu)1167 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1168 {
1169 struct kvm_run *run = vcpu->run;
1170 int ret;
1171
1172 if (run->exit_reason == KVM_EXIT_MMIO) {
1173 ret = kvm_handle_mmio_return(vcpu);
1174 if (ret <= 0)
1175 return ret;
1176 }
1177
1178 vcpu_load(vcpu);
1179
1180 if (!vcpu->wants_to_run) {
1181 ret = -EINTR;
1182 goto out;
1183 }
1184
1185 kvm_sigset_activate(vcpu);
1186
1187 ret = 1;
1188 run->exit_reason = KVM_EXIT_UNKNOWN;
1189 run->flags = 0;
1190 while (ret > 0) {
1191 /*
1192 * Check conditions before entering the guest
1193 */
1194 ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
1195 if (!ret)
1196 ret = 1;
1197
1198 if (ret > 0)
1199 ret = check_vcpu_requests(vcpu);
1200
1201 /*
1202 * Preparing the interrupts to be injected also
1203 * involves poking the GIC, which must be done in a
1204 * non-preemptible context.
1205 */
1206 preempt_disable();
1207
1208 kvm_nested_flush_hwstate(vcpu);
1209
1210 if (kvm_vcpu_has_pmu(vcpu))
1211 kvm_pmu_flush_hwstate(vcpu);
1212
1213 local_irq_disable();
1214
1215 kvm_vgic_flush_hwstate(vcpu);
1216
1217 kvm_pmu_update_vcpu_events(vcpu);
1218
1219 /*
1220 * Ensure we set mode to IN_GUEST_MODE after we disable
1221 * interrupts and before the final VCPU requests check.
1222 * See the comment in kvm_vcpu_exiting_guest_mode() and
1223 * Documentation/virt/kvm/vcpu-requests.rst
1224 */
1225 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1226
1227 if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
1228 vcpu->mode = OUTSIDE_GUEST_MODE;
1229 isb(); /* Ensure work in x_flush_hwstate is committed */
1230 if (kvm_vcpu_has_pmu(vcpu))
1231 kvm_pmu_sync_hwstate(vcpu);
1232 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
1233 kvm_timer_sync_user(vcpu);
1234 kvm_vgic_sync_hwstate(vcpu);
1235 local_irq_enable();
1236 preempt_enable();
1237 continue;
1238 }
1239
1240 kvm_arch_vcpu_ctxflush_fp(vcpu);
1241
1242 /**************************************************************
1243 * Enter the guest
1244 */
1245 trace_kvm_entry(*vcpu_pc(vcpu));
1246 guest_timing_enter_irqoff();
1247
1248 ret = kvm_arm_vcpu_enter_exit(vcpu);
1249
1250 vcpu->mode = OUTSIDE_GUEST_MODE;
1251 vcpu->stat.exits++;
1252 /*
1253 * Back from guest
1254 *************************************************************/
1255
1256 /*
1257 * We must sync the PMU state before the vgic state so
1258 * that the vgic can properly sample the updated state of the
1259 * interrupt line.
1260 */
1261 if (kvm_vcpu_has_pmu(vcpu))
1262 kvm_pmu_sync_hwstate(vcpu);
1263
1264 /*
1265 * Sync the vgic state before syncing the timer state because
1266 * the timer code needs to know if the virtual timer
1267 * interrupts are active.
1268 */
1269 kvm_vgic_sync_hwstate(vcpu);
1270
1271 /*
1272 * Sync the timer hardware state before enabling interrupts as
1273 * we don't want vtimer interrupts to race with syncing the
1274 * timer virtual interrupt state.
1275 */
1276 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
1277 kvm_timer_sync_user(vcpu);
1278
1279 if (is_hyp_ctxt(vcpu))
1280 kvm_timer_sync_nested(vcpu);
1281
1282 kvm_arch_vcpu_ctxsync_fp(vcpu);
1283
1284 /*
1285 * We must ensure that any pending interrupts are taken before
1286 * we exit guest timing so that timer ticks are accounted as
1287 * guest time. Transiently unmask interrupts so that any
1288 * pending interrupts are taken.
1289 *
1290 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
1291 * context synchronization event) is necessary to ensure that
1292 * pending interrupts are taken.
1293 */
1294 if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
1295 local_irq_enable();
1296 isb();
1297 local_irq_disable();
1298 }
1299
1300 guest_timing_exit_irqoff();
1301
1302 local_irq_enable();
1303
1304 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1305
1306 /* Exit types that need handling before we can be preempted */
1307 handle_exit_early(vcpu, ret);
1308
1309 kvm_nested_sync_hwstate(vcpu);
1310
1311 preempt_enable();
1312
1313 /*
1314 * The ARMv8 architecture doesn't give the hypervisor
1315 * a mechanism to prevent a guest from dropping to AArch32 EL0
1316 * if implemented by the CPU. If we spot the guest in such
1317 * state and that we decided it wasn't supposed to do so (like
1318 * with the asymmetric AArch32 case), return to userspace with
1319 * a fatal error.
1320 */
1321 if (vcpu_mode_is_bad_32bit(vcpu)) {
1322 /*
1323 * As we have caught the guest red-handed, decide that
1324 * it isn't fit for purpose anymore by making the vcpu
1325 * invalid. The VMM can try and fix it by issuing a
1326 * KVM_ARM_VCPU_INIT if it really wants to.
1327 */
1328 vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
1329 ret = ARM_EXCEPTION_IL;
1330 }
1331
1332 ret = handle_exit(vcpu, ret);
1333 }
1334
1335 /* Tell userspace about in-kernel device output levels */
1336 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
1337 kvm_timer_update_run(vcpu);
1338 kvm_pmu_update_run(vcpu);
1339 }
1340
1341 kvm_sigset_deactivate(vcpu);
1342
1343 out:
1344 /*
1345 * In the unlikely event that we are returning to userspace
1346 * with pending exceptions or PC adjustment, commit these
1347 * adjustments in order to give userspace a consistent view of
1348 * the vcpu state. Note that this relies on __kvm_adjust_pc()
1349 * being preempt-safe on VHE.
1350 */
1351 if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1352 vcpu_get_flag(vcpu, INCREMENT_PC)))
1353 kvm_call_hyp(__kvm_adjust_pc, vcpu);
1354
1355 vcpu_put(vcpu);
1356 return ret;
1357 }
1358
vcpu_interrupt_line(struct kvm_vcpu * vcpu,int number,bool level)1359 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
1360 {
1361 int bit_index;
1362 bool set;
1363 unsigned long *hcr;
1364
1365 if (number == KVM_ARM_IRQ_CPU_IRQ)
1366 bit_index = __ffs(HCR_VI);
1367 else /* KVM_ARM_IRQ_CPU_FIQ */
1368 bit_index = __ffs(HCR_VF);
1369
1370 hcr = vcpu_hcr(vcpu);
1371 if (level)
1372 set = test_and_set_bit(bit_index, hcr);
1373 else
1374 set = test_and_clear_bit(bit_index, hcr);
1375
1376 /*
1377 * If we didn't change anything, no need to wake up or kick other CPUs
1378 */
1379 if (set == level)
1380 return 0;
1381
1382 /*
1383 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
1384 * trigger a world-switch round on the running physical CPU to set the
1385 * virtual IRQ/FIQ fields in the HCR appropriately.
1386 */
1387 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
1388 kvm_vcpu_kick(vcpu);
1389
1390 return 0;
1391 }
1392
kvm_vm_ioctl_irq_line(struct kvm * kvm,struct kvm_irq_level * irq_level,bool line_status)1393 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
1394 bool line_status)
1395 {
1396 u32 irq = irq_level->irq;
1397 unsigned int irq_type, vcpu_id, irq_num;
1398 struct kvm_vcpu *vcpu = NULL;
1399 bool level = irq_level->level;
1400
1401 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
1402 vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
1403 vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
1404 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
1405
1406 trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level);
1407
1408 switch (irq_type) {
1409 case KVM_ARM_IRQ_TYPE_CPU:
1410 if (irqchip_in_kernel(kvm))
1411 return -ENXIO;
1412
1413 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1414 if (!vcpu)
1415 return -EINVAL;
1416
1417 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
1418 return -EINVAL;
1419
1420 return vcpu_interrupt_line(vcpu, irq_num, level);
1421 case KVM_ARM_IRQ_TYPE_PPI:
1422 if (!irqchip_in_kernel(kvm))
1423 return -ENXIO;
1424
1425 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1426 if (!vcpu)
1427 return -EINVAL;
1428
1429 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
1430 return -EINVAL;
1431
1432 return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL);
1433 case KVM_ARM_IRQ_TYPE_SPI:
1434 if (!irqchip_in_kernel(kvm))
1435 return -ENXIO;
1436
1437 if (irq_num < VGIC_NR_PRIVATE_IRQS)
1438 return -EINVAL;
1439
1440 return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL);
1441 }
1442
1443 return -EINVAL;
1444 }
1445
system_supported_vcpu_features(void)1446 static unsigned long system_supported_vcpu_features(void)
1447 {
1448 unsigned long features = KVM_VCPU_VALID_FEATURES;
1449
1450 if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1))
1451 clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features);
1452
1453 if (!kvm_supports_guest_pmuv3())
1454 clear_bit(KVM_ARM_VCPU_PMU_V3, &features);
1455
1456 if (!system_supports_sve())
1457 clear_bit(KVM_ARM_VCPU_SVE, &features);
1458
1459 if (!kvm_has_full_ptr_auth()) {
1460 clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
1461 clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
1462 }
1463
1464 if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
1465 clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
1466
1467 return features;
1468 }
1469
kvm_vcpu_init_check_features(struct kvm_vcpu * vcpu,const struct kvm_vcpu_init * init)1470 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
1471 const struct kvm_vcpu_init *init)
1472 {
1473 unsigned long features = init->features[0];
1474 int i;
1475
1476 if (features & ~KVM_VCPU_VALID_FEATURES)
1477 return -ENOENT;
1478
1479 for (i = 1; i < ARRAY_SIZE(init->features); i++) {
1480 if (init->features[i])
1481 return -ENOENT;
1482 }
1483
1484 if (features & ~system_supported_vcpu_features())
1485 return -EINVAL;
1486
1487 /*
1488 * For now make sure that both address/generic pointer authentication
1489 * features are requested by the userspace together.
1490 */
1491 if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) !=
1492 test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))
1493 return -EINVAL;
1494
1495 if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
1496 return 0;
1497
1498 /* MTE is incompatible with AArch32 */
1499 if (kvm_has_mte(vcpu->kvm))
1500 return -EINVAL;
1501
1502 /* NV is incompatible with AArch32 */
1503 if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
1504 return -EINVAL;
1505
1506 return 0;
1507 }
1508
kvm_vcpu_init_changed(struct kvm_vcpu * vcpu,const struct kvm_vcpu_init * init)1509 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
1510 const struct kvm_vcpu_init *init)
1511 {
1512 unsigned long features = init->features[0];
1513
1514 return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features,
1515 KVM_VCPU_MAX_FEATURES);
1516 }
1517
kvm_setup_vcpu(struct kvm_vcpu * vcpu)1518 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
1519 {
1520 struct kvm *kvm = vcpu->kvm;
1521 int ret = 0;
1522
1523 /*
1524 * When the vCPU has a PMU, but no PMU is set for the guest
1525 * yet, set the default one.
1526 */
1527 if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu)
1528 ret = kvm_arm_set_default_pmu(kvm);
1529
1530 /* Prepare for nested if required */
1531 if (!ret && vcpu_has_nv(vcpu))
1532 ret = kvm_vcpu_init_nested(vcpu);
1533
1534 return ret;
1535 }
1536
__kvm_vcpu_set_target(struct kvm_vcpu * vcpu,const struct kvm_vcpu_init * init)1537 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1538 const struct kvm_vcpu_init *init)
1539 {
1540 unsigned long features = init->features[0];
1541 struct kvm *kvm = vcpu->kvm;
1542 int ret = -EINVAL;
1543
1544 mutex_lock(&kvm->arch.config_lock);
1545
1546 if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
1547 kvm_vcpu_init_changed(vcpu, init))
1548 goto out_unlock;
1549
1550 bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
1551
1552 ret = kvm_setup_vcpu(vcpu);
1553 if (ret)
1554 goto out_unlock;
1555
1556 /* Now we know what it is, we can reset it. */
1557 kvm_reset_vcpu(vcpu);
1558
1559 set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
1560 vcpu_set_flag(vcpu, VCPU_INITIALIZED);
1561 ret = 0;
1562 out_unlock:
1563 mutex_unlock(&kvm->arch.config_lock);
1564 return ret;
1565 }
1566
kvm_vcpu_set_target(struct kvm_vcpu * vcpu,const struct kvm_vcpu_init * init)1567 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1568 const struct kvm_vcpu_init *init)
1569 {
1570 int ret;
1571
1572 if (init->target != KVM_ARM_TARGET_GENERIC_V8 &&
1573 init->target != kvm_target_cpu())
1574 return -EINVAL;
1575
1576 ret = kvm_vcpu_init_check_features(vcpu, init);
1577 if (ret)
1578 return ret;
1579
1580 if (!kvm_vcpu_initialized(vcpu))
1581 return __kvm_vcpu_set_target(vcpu, init);
1582
1583 if (kvm_vcpu_init_changed(vcpu, init))
1584 return -EINVAL;
1585
1586 kvm_reset_vcpu(vcpu);
1587 return 0;
1588 }
1589
kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu * vcpu,struct kvm_vcpu_init * init)1590 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1591 struct kvm_vcpu_init *init)
1592 {
1593 bool power_off = false;
1594 int ret;
1595
1596 /*
1597 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
1598 * reflecting it in the finalized feature set, thus limiting its scope
1599 * to a single KVM_ARM_VCPU_INIT call.
1600 */
1601 if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
1602 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
1603 power_off = true;
1604 }
1605
1606 ret = kvm_vcpu_set_target(vcpu, init);
1607 if (ret)
1608 return ret;
1609
1610 /*
1611 * Ensure a rebooted VM will fault in RAM pages and detect if the
1612 * guest MMU is turned off and flush the caches as needed.
1613 *
1614 * S2FWB enforces all memory accesses to RAM being cacheable,
1615 * ensuring that the data side is always coherent. We still
1616 * need to invalidate the I-cache though, as FWB does *not*
1617 * imply CTR_EL0.DIC.
1618 */
1619 if (vcpu_has_run_once(vcpu)) {
1620 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
1621 stage2_unmap_vm(vcpu->kvm);
1622 else
1623 icache_inval_all_pou();
1624 }
1625
1626 vcpu_reset_hcr(vcpu);
1627
1628 /*
1629 * Handle the "start in power-off" case.
1630 */
1631 spin_lock(&vcpu->arch.mp_state_lock);
1632
1633 if (power_off)
1634 __kvm_arm_vcpu_power_off(vcpu);
1635 else
1636 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
1637
1638 spin_unlock(&vcpu->arch.mp_state_lock);
1639
1640 return 0;
1641 }
1642
kvm_arm_vcpu_set_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1643 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1644 struct kvm_device_attr *attr)
1645 {
1646 int ret = -ENXIO;
1647
1648 switch (attr->group) {
1649 default:
1650 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
1651 break;
1652 }
1653
1654 return ret;
1655 }
1656
kvm_arm_vcpu_get_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1657 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1658 struct kvm_device_attr *attr)
1659 {
1660 int ret = -ENXIO;
1661
1662 switch (attr->group) {
1663 default:
1664 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
1665 break;
1666 }
1667
1668 return ret;
1669 }
1670
kvm_arm_vcpu_has_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1671 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1672 struct kvm_device_attr *attr)
1673 {
1674 int ret = -ENXIO;
1675
1676 switch (attr->group) {
1677 default:
1678 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
1679 break;
1680 }
1681
1682 return ret;
1683 }
1684
kvm_arm_vcpu_get_events(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)1685 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1686 struct kvm_vcpu_events *events)
1687 {
1688 memset(events, 0, sizeof(*events));
1689
1690 return __kvm_arm_vcpu_get_events(vcpu, events);
1691 }
1692
kvm_arm_vcpu_set_events(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)1693 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1694 struct kvm_vcpu_events *events)
1695 {
1696 int i;
1697
1698 /* check whether the reserved field is zero */
1699 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1700 if (events->reserved[i])
1701 return -EINVAL;
1702
1703 /* check whether the pad field is zero */
1704 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1705 if (events->exception.pad[i])
1706 return -EINVAL;
1707
1708 return __kvm_arm_vcpu_set_events(vcpu, events);
1709 }
1710
kvm_arch_vcpu_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1711 long kvm_arch_vcpu_ioctl(struct file *filp,
1712 unsigned int ioctl, unsigned long arg)
1713 {
1714 struct kvm_vcpu *vcpu = filp->private_data;
1715 void __user *argp = (void __user *)arg;
1716 struct kvm_device_attr attr;
1717 long r;
1718
1719 switch (ioctl) {
1720 case KVM_ARM_VCPU_INIT: {
1721 struct kvm_vcpu_init init;
1722
1723 r = -EFAULT;
1724 if (copy_from_user(&init, argp, sizeof(init)))
1725 break;
1726
1727 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1728 break;
1729 }
1730 case KVM_SET_ONE_REG:
1731 case KVM_GET_ONE_REG: {
1732 struct kvm_one_reg reg;
1733
1734 r = -ENOEXEC;
1735 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1736 break;
1737
1738 r = -EFAULT;
1739 if (copy_from_user(®, argp, sizeof(reg)))
1740 break;
1741
1742 /*
1743 * We could owe a reset due to PSCI. Handle the pending reset
1744 * here to ensure userspace register accesses are ordered after
1745 * the reset.
1746 */
1747 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
1748 kvm_reset_vcpu(vcpu);
1749
1750 if (ioctl == KVM_SET_ONE_REG)
1751 r = kvm_arm_set_reg(vcpu, ®);
1752 else
1753 r = kvm_arm_get_reg(vcpu, ®);
1754 break;
1755 }
1756 case KVM_GET_REG_LIST: {
1757 struct kvm_reg_list __user *user_list = argp;
1758 struct kvm_reg_list reg_list;
1759 unsigned n;
1760
1761 r = -ENOEXEC;
1762 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1763 break;
1764
1765 r = -EPERM;
1766 if (!kvm_arm_vcpu_is_finalized(vcpu))
1767 break;
1768
1769 r = -EFAULT;
1770 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
1771 break;
1772 n = reg_list.n;
1773 reg_list.n = kvm_arm_num_regs(vcpu);
1774 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
1775 break;
1776 r = -E2BIG;
1777 if (n < reg_list.n)
1778 break;
1779 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1780 break;
1781 }
1782 case KVM_SET_DEVICE_ATTR: {
1783 r = -EFAULT;
1784 if (copy_from_user(&attr, argp, sizeof(attr)))
1785 break;
1786 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1787 break;
1788 }
1789 case KVM_GET_DEVICE_ATTR: {
1790 r = -EFAULT;
1791 if (copy_from_user(&attr, argp, sizeof(attr)))
1792 break;
1793 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1794 break;
1795 }
1796 case KVM_HAS_DEVICE_ATTR: {
1797 r = -EFAULT;
1798 if (copy_from_user(&attr, argp, sizeof(attr)))
1799 break;
1800 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1801 break;
1802 }
1803 case KVM_GET_VCPU_EVENTS: {
1804 struct kvm_vcpu_events events;
1805
1806 if (!kvm_vcpu_initialized(vcpu))
1807 return -ENOEXEC;
1808
1809 if (kvm_arm_vcpu_get_events(vcpu, &events))
1810 return -EINVAL;
1811
1812 if (copy_to_user(argp, &events, sizeof(events)))
1813 return -EFAULT;
1814
1815 return 0;
1816 }
1817 case KVM_SET_VCPU_EVENTS: {
1818 struct kvm_vcpu_events events;
1819
1820 if (!kvm_vcpu_initialized(vcpu))
1821 return -ENOEXEC;
1822
1823 if (copy_from_user(&events, argp, sizeof(events)))
1824 return -EFAULT;
1825
1826 return kvm_arm_vcpu_set_events(vcpu, &events);
1827 }
1828 case KVM_ARM_VCPU_FINALIZE: {
1829 int what;
1830
1831 if (!kvm_vcpu_initialized(vcpu))
1832 return -ENOEXEC;
1833
1834 if (get_user(what, (const int __user *)argp))
1835 return -EFAULT;
1836
1837 return kvm_arm_vcpu_finalize(vcpu, what);
1838 }
1839 default:
1840 r = -EINVAL;
1841 }
1842
1843 return r;
1844 }
1845
kvm_arch_vcpu_unlocked_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1846 long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
1847 unsigned long arg)
1848 {
1849 return -ENOIOCTLCMD;
1850 }
1851
kvm_arch_sync_dirty_log(struct kvm * kvm,struct kvm_memory_slot * memslot)1852 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1853 {
1854
1855 }
1856
kvm_vm_ioctl_set_device_addr(struct kvm * kvm,struct kvm_arm_device_addr * dev_addr)1857 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1858 struct kvm_arm_device_addr *dev_addr)
1859 {
1860 switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
1861 case KVM_ARM_DEVICE_VGIC_V2:
1862 if (!vgic_present)
1863 return -ENXIO;
1864 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
1865 default:
1866 return -ENODEV;
1867 }
1868 }
1869
kvm_vm_has_attr(struct kvm * kvm,struct kvm_device_attr * attr)1870 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1871 {
1872 switch (attr->group) {
1873 case KVM_ARM_VM_SMCCC_CTRL:
1874 return kvm_vm_smccc_has_attr(kvm, attr);
1875 default:
1876 return -ENXIO;
1877 }
1878 }
1879
kvm_vm_set_attr(struct kvm * kvm,struct kvm_device_attr * attr)1880 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1881 {
1882 switch (attr->group) {
1883 case KVM_ARM_VM_SMCCC_CTRL:
1884 return kvm_vm_smccc_set_attr(kvm, attr);
1885 default:
1886 return -ENXIO;
1887 }
1888 }
1889
kvm_arch_vm_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1890 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1891 {
1892 struct kvm *kvm = filp->private_data;
1893 void __user *argp = (void __user *)arg;
1894 struct kvm_device_attr attr;
1895
1896 switch (ioctl) {
1897 case KVM_CREATE_IRQCHIP: {
1898 int ret;
1899 if (!vgic_present)
1900 return -ENXIO;
1901 mutex_lock(&kvm->lock);
1902 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1903 mutex_unlock(&kvm->lock);
1904 return ret;
1905 }
1906 case KVM_ARM_SET_DEVICE_ADDR: {
1907 struct kvm_arm_device_addr dev_addr;
1908
1909 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1910 return -EFAULT;
1911 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1912 }
1913 case KVM_ARM_PREFERRED_TARGET: {
1914 struct kvm_vcpu_init init = {
1915 .target = KVM_ARM_TARGET_GENERIC_V8,
1916 };
1917
1918 if (copy_to_user(argp, &init, sizeof(init)))
1919 return -EFAULT;
1920
1921 return 0;
1922 }
1923 case KVM_ARM_MTE_COPY_TAGS: {
1924 struct kvm_arm_copy_mte_tags copy_tags;
1925
1926 if (copy_from_user(©_tags, argp, sizeof(copy_tags)))
1927 return -EFAULT;
1928 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags);
1929 }
1930 case KVM_ARM_SET_COUNTER_OFFSET: {
1931 struct kvm_arm_counter_offset offset;
1932
1933 if (copy_from_user(&offset, argp, sizeof(offset)))
1934 return -EFAULT;
1935 return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
1936 }
1937 case KVM_HAS_DEVICE_ATTR: {
1938 if (copy_from_user(&attr, argp, sizeof(attr)))
1939 return -EFAULT;
1940
1941 return kvm_vm_has_attr(kvm, &attr);
1942 }
1943 case KVM_SET_DEVICE_ATTR: {
1944 if (copy_from_user(&attr, argp, sizeof(attr)))
1945 return -EFAULT;
1946
1947 return kvm_vm_set_attr(kvm, &attr);
1948 }
1949 case KVM_ARM_GET_REG_WRITABLE_MASKS: {
1950 struct reg_mask_range range;
1951
1952 if (copy_from_user(&range, argp, sizeof(range)))
1953 return -EFAULT;
1954 return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
1955 }
1956 default:
1957 return -EINVAL;
1958 }
1959 }
1960
nvhe_percpu_size(void)1961 static unsigned long nvhe_percpu_size(void)
1962 {
1963 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
1964 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
1965 }
1966
nvhe_percpu_order(void)1967 static unsigned long nvhe_percpu_order(void)
1968 {
1969 unsigned long size = nvhe_percpu_size();
1970
1971 return size ? get_order(size) : 0;
1972 }
1973
pkvm_host_sve_state_order(void)1974 static size_t pkvm_host_sve_state_order(void)
1975 {
1976 return get_order(pkvm_host_sve_state_size());
1977 }
1978
1979 /* A lookup table holding the hypervisor VA for each vector slot */
1980 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1981
kvm_init_vector_slot(void * base,enum arm64_hyp_spectre_vector slot)1982 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1983 {
1984 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
1985 }
1986
kvm_init_vector_slots(void)1987 static int kvm_init_vector_slots(void)
1988 {
1989 int err;
1990 void *base;
1991
1992 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1993 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1994
1995 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1996 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1997
1998 if (kvm_system_needs_idmapped_vectors() &&
1999 !is_protected_kvm_enabled()) {
2000 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
2001 __BP_HARDEN_HYP_VECS_SZ, &base);
2002 if (err)
2003 return err;
2004 }
2005
2006 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
2007 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
2008 return 0;
2009 }
2010
cpu_prepare_hyp_mode(int cpu,u32 hyp_va_bits)2011 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
2012 {
2013 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
2014 unsigned long tcr;
2015
2016 /*
2017 * Calculate the raw per-cpu offset without a translation from the
2018 * kernel's mapping to the linear mapping, and store it in tpidr_el2
2019 * so that we can use adr_l to access per-cpu variables in EL2.
2020 * Also drop the KASAN tag which gets in the way...
2021 */
2022 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
2023 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
2024
2025 params->mair_el2 = read_sysreg(mair_el1);
2026
2027 tcr = read_sysreg(tcr_el1);
2028 if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
2029 tcr &= ~(TCR_HD | TCR_HA | TCR_A1 | TCR_T0SZ_MASK);
2030 tcr |= TCR_EPD1_MASK;
2031 } else {
2032 unsigned long ips = FIELD_GET(TCR_IPS_MASK, tcr);
2033
2034 tcr &= TCR_EL2_MASK;
2035 tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips);
2036 if (lpa2_is_enabled())
2037 tcr |= TCR_EL2_DS;
2038 }
2039 tcr |= TCR_T0SZ(hyp_va_bits);
2040 params->tcr_el2 = tcr;
2041
2042 params->pgd_pa = kvm_mmu_get_httbr();
2043 if (is_protected_kvm_enabled())
2044 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
2045 else
2046 params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
2047 if (cpus_have_final_cap(ARM64_KVM_HVHE))
2048 params->hcr_el2 |= HCR_E2H;
2049 params->vttbr = params->vtcr = 0;
2050
2051 /*
2052 * Flush the init params from the data cache because the struct will
2053 * be read while the MMU is off.
2054 */
2055 kvm_flush_dcache_to_poc(params, sizeof(*params));
2056 }
2057
hyp_install_host_vector(void)2058 static void hyp_install_host_vector(void)
2059 {
2060 struct kvm_nvhe_init_params *params;
2061 struct arm_smccc_res res;
2062
2063 /* Switch from the HYP stub to our own HYP init vector */
2064 __hyp_set_vectors(kvm_get_idmap_vector());
2065
2066 /*
2067 * Call initialization code, and switch to the full blown HYP code.
2068 * If the cpucaps haven't been finalized yet, something has gone very
2069 * wrong, and hyp will crash and burn when it uses any
2070 * cpus_have_*_cap() wrapper.
2071 */
2072 BUG_ON(!system_capabilities_finalized());
2073 params = this_cpu_ptr_nvhe_sym(kvm_init_params);
2074 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
2075 WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
2076 }
2077
cpu_init_hyp_mode(void)2078 static void cpu_init_hyp_mode(void)
2079 {
2080 hyp_install_host_vector();
2081
2082 /*
2083 * Disabling SSBD on a non-VHE system requires us to enable SSBS
2084 * at EL2.
2085 */
2086 if (this_cpu_has_cap(ARM64_SSBS) &&
2087 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
2088 kvm_call_hyp_nvhe(__kvm_enable_ssbs);
2089 }
2090 }
2091
cpu_hyp_reset(void)2092 static void cpu_hyp_reset(void)
2093 {
2094 if (!is_kernel_in_hyp_mode())
2095 __hyp_reset_vectors();
2096 }
2097
2098 /*
2099 * EL2 vectors can be mapped and rerouted in a number of ways,
2100 * depending on the kernel configuration and CPU present:
2101 *
2102 * - If the CPU is affected by Spectre-v2, the hardening sequence is
2103 * placed in one of the vector slots, which is executed before jumping
2104 * to the real vectors.
2105 *
2106 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
2107 * containing the hardening sequence is mapped next to the idmap page,
2108 * and executed before jumping to the real vectors.
2109 *
2110 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
2111 * empty slot is selected, mapped next to the idmap page, and
2112 * executed before jumping to the real vectors.
2113 *
2114 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
2115 * VHE, as we don't have hypervisor-specific mappings. If the system
2116 * is VHE and yet selects this capability, it will be ignored.
2117 */
cpu_set_hyp_vector(void)2118 static void cpu_set_hyp_vector(void)
2119 {
2120 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
2121 void *vector = hyp_spectre_vector_selector[data->slot];
2122
2123 if (!is_protected_kvm_enabled())
2124 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
2125 else
2126 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
2127 }
2128
cpu_hyp_init_context(void)2129 static void cpu_hyp_init_context(void)
2130 {
2131 kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
2132 kvm_init_host_debug_data();
2133
2134 if (!is_kernel_in_hyp_mode())
2135 cpu_init_hyp_mode();
2136 }
2137
cpu_hyp_init_features(void)2138 static void cpu_hyp_init_features(void)
2139 {
2140 cpu_set_hyp_vector();
2141
2142 if (is_kernel_in_hyp_mode()) {
2143 kvm_timer_init_vhe();
2144 kvm_debug_init_vhe();
2145 }
2146
2147 if (vgic_present)
2148 kvm_vgic_init_cpu_hardware();
2149 }
2150
cpu_hyp_reinit(void)2151 static void cpu_hyp_reinit(void)
2152 {
2153 cpu_hyp_reset();
2154 cpu_hyp_init_context();
2155 cpu_hyp_init_features();
2156 }
2157
cpu_hyp_init(void * discard)2158 static void cpu_hyp_init(void *discard)
2159 {
2160 if (!__this_cpu_read(kvm_hyp_initialized)) {
2161 cpu_hyp_reinit();
2162 __this_cpu_write(kvm_hyp_initialized, 1);
2163 }
2164 }
2165
cpu_hyp_uninit(void * discard)2166 static void cpu_hyp_uninit(void *discard)
2167 {
2168 if (!is_protected_kvm_enabled() && __this_cpu_read(kvm_hyp_initialized)) {
2169 cpu_hyp_reset();
2170 __this_cpu_write(kvm_hyp_initialized, 0);
2171 }
2172 }
2173
kvm_arch_enable_virtualization_cpu(void)2174 int kvm_arch_enable_virtualization_cpu(void)
2175 {
2176 /*
2177 * Most calls to this function are made with migration
2178 * disabled, but not with preemption disabled. The former is
2179 * enough to ensure correctness, but most of the helpers
2180 * expect the later and will throw a tantrum otherwise.
2181 */
2182 preempt_disable();
2183
2184 cpu_hyp_init(NULL);
2185
2186 kvm_vgic_cpu_up();
2187 kvm_timer_cpu_up();
2188
2189 preempt_enable();
2190
2191 return 0;
2192 }
2193
kvm_arch_disable_virtualization_cpu(void)2194 void kvm_arch_disable_virtualization_cpu(void)
2195 {
2196 kvm_timer_cpu_down();
2197 kvm_vgic_cpu_down();
2198
2199 if (!is_protected_kvm_enabled())
2200 cpu_hyp_uninit(NULL);
2201 }
2202
2203 #ifdef CONFIG_CPU_PM
hyp_init_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)2204 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
2205 unsigned long cmd,
2206 void *v)
2207 {
2208 /*
2209 * kvm_hyp_initialized is left with its old value over
2210 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
2211 * re-enable hyp.
2212 */
2213 switch (cmd) {
2214 case CPU_PM_ENTER:
2215 if (__this_cpu_read(kvm_hyp_initialized))
2216 /*
2217 * don't update kvm_hyp_initialized here
2218 * so that the hyp will be re-enabled
2219 * when we resume. See below.
2220 */
2221 cpu_hyp_reset();
2222
2223 return NOTIFY_OK;
2224 case CPU_PM_ENTER_FAILED:
2225 case CPU_PM_EXIT:
2226 if (__this_cpu_read(kvm_hyp_initialized))
2227 /* The hyp was enabled before suspend. */
2228 cpu_hyp_reinit();
2229
2230 return NOTIFY_OK;
2231
2232 default:
2233 return NOTIFY_DONE;
2234 }
2235 }
2236
2237 static struct notifier_block hyp_init_cpu_pm_nb = {
2238 .notifier_call = hyp_init_cpu_pm_notifier,
2239 };
2240
hyp_cpu_pm_init(void)2241 static void __init hyp_cpu_pm_init(void)
2242 {
2243 if (!is_protected_kvm_enabled())
2244 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
2245 }
hyp_cpu_pm_exit(void)2246 static void __init hyp_cpu_pm_exit(void)
2247 {
2248 if (!is_protected_kvm_enabled())
2249 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
2250 }
2251 #else
hyp_cpu_pm_init(void)2252 static inline void __init hyp_cpu_pm_init(void)
2253 {
2254 }
hyp_cpu_pm_exit(void)2255 static inline void __init hyp_cpu_pm_exit(void)
2256 {
2257 }
2258 #endif
2259
init_cpu_logical_map(void)2260 static void __init init_cpu_logical_map(void)
2261 {
2262 unsigned int cpu;
2263
2264 /*
2265 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
2266 * Only copy the set of online CPUs whose features have been checked
2267 * against the finalized system capabilities. The hypervisor will not
2268 * allow any other CPUs from the `possible` set to boot.
2269 */
2270 for_each_online_cpu(cpu)
2271 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
2272 }
2273
2274 #define init_psci_0_1_impl_state(config, what) \
2275 config.psci_0_1_ ## what ## _implemented = psci_ops.what
2276
init_psci_relay(void)2277 static bool __init init_psci_relay(void)
2278 {
2279 /*
2280 * If PSCI has not been initialized, protected KVM cannot install
2281 * itself on newly booted CPUs.
2282 */
2283 if (!psci_ops.get_version) {
2284 kvm_err("Cannot initialize protected mode without PSCI\n");
2285 return false;
2286 }
2287
2288 kvm_host_psci_config.version = psci_ops.get_version();
2289 kvm_host_psci_config.smccc_version = arm_smccc_get_version();
2290
2291 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
2292 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
2293 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
2294 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
2295 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
2296 init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
2297 }
2298 return true;
2299 }
2300
init_subsystems(void)2301 static int __init init_subsystems(void)
2302 {
2303 int err = 0;
2304
2305 /*
2306 * Enable hardware so that subsystem initialisation can access EL2.
2307 */
2308 on_each_cpu(cpu_hyp_init, NULL, 1);
2309
2310 /*
2311 * Register CPU lower-power notifier
2312 */
2313 hyp_cpu_pm_init();
2314
2315 /*
2316 * Init HYP view of VGIC
2317 */
2318 err = kvm_vgic_hyp_init();
2319 switch (err) {
2320 case 0:
2321 vgic_present = true;
2322 break;
2323 case -ENODEV:
2324 case -ENXIO:
2325 /*
2326 * No VGIC? No pKVM for you.
2327 *
2328 * Protected mode assumes that VGICv3 is present, so no point
2329 * in trying to hobble along if vgic initialization fails.
2330 */
2331 if (is_protected_kvm_enabled())
2332 goto out;
2333
2334 /*
2335 * Otherwise, userspace could choose to implement a GIC for its
2336 * guest on non-cooperative hardware.
2337 */
2338 vgic_present = false;
2339 err = 0;
2340 break;
2341 default:
2342 goto out;
2343 }
2344
2345 if (kvm_mode == KVM_MODE_NV &&
2346 !(vgic_present && (kvm_vgic_global_state.type == VGIC_V3 ||
2347 kvm_vgic_global_state.has_gcie_v3_compat))) {
2348 kvm_err("NV support requires GICv3 or GICv5 with legacy support, giving up\n");
2349 err = -EINVAL;
2350 goto out;
2351 }
2352
2353 /*
2354 * Init HYP architected timer support
2355 */
2356 err = kvm_timer_hyp_init(vgic_present);
2357 if (err)
2358 goto out;
2359
2360 kvm_register_perf_callbacks(NULL);
2361
2362 out:
2363 if (err)
2364 hyp_cpu_pm_exit();
2365
2366 if (err || !is_protected_kvm_enabled())
2367 on_each_cpu(cpu_hyp_uninit, NULL, 1);
2368
2369 return err;
2370 }
2371
teardown_subsystems(void)2372 static void __init teardown_subsystems(void)
2373 {
2374 kvm_unregister_perf_callbacks();
2375 hyp_cpu_pm_exit();
2376 }
2377
teardown_hyp_mode(void)2378 static void __init teardown_hyp_mode(void)
2379 {
2380 bool free_sve = system_supports_sve() && is_protected_kvm_enabled();
2381 int cpu;
2382
2383 free_hyp_pgds();
2384 for_each_possible_cpu(cpu) {
2385 if (per_cpu(kvm_hyp_initialized, cpu))
2386 continue;
2387
2388 free_pages(per_cpu(kvm_arm_hyp_stack_base, cpu), NVHE_STACK_SHIFT - PAGE_SHIFT);
2389
2390 if (!kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu])
2391 continue;
2392
2393 if (free_sve) {
2394 struct cpu_sve_state *sve_state;
2395
2396 sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
2397 free_pages((unsigned long) sve_state, pkvm_host_sve_state_order());
2398 }
2399
2400 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
2401
2402 }
2403 }
2404
do_pkvm_init(u32 hyp_va_bits)2405 static int __init do_pkvm_init(u32 hyp_va_bits)
2406 {
2407 void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
2408 int ret;
2409
2410 preempt_disable();
2411 cpu_hyp_init_context();
2412 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
2413 num_possible_cpus(), kern_hyp_va(per_cpu_base),
2414 hyp_va_bits);
2415 cpu_hyp_init_features();
2416
2417 /*
2418 * The stub hypercalls are now disabled, so set our local flag to
2419 * prevent a later re-init attempt in kvm_arch_enable_virtualization_cpu().
2420 */
2421 __this_cpu_write(kvm_hyp_initialized, 1);
2422 preempt_enable();
2423
2424 return ret;
2425 }
2426
get_hyp_id_aa64pfr0_el1(void)2427 static u64 get_hyp_id_aa64pfr0_el1(void)
2428 {
2429 /*
2430 * Track whether the system isn't affected by spectre/meltdown in the
2431 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
2432 * Although this is per-CPU, we make it global for simplicity, e.g., not
2433 * to have to worry about vcpu migration.
2434 *
2435 * Unlike for non-protected VMs, userspace cannot override this for
2436 * protected VMs.
2437 */
2438 u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
2439
2440 val &= ~(ID_AA64PFR0_EL1_CSV2 |
2441 ID_AA64PFR0_EL1_CSV3);
2442
2443 val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV2,
2444 arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
2445 val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV3,
2446 arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
2447
2448 return val;
2449 }
2450
kvm_hyp_init_symbols(void)2451 static void kvm_hyp_init_symbols(void)
2452 {
2453 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
2454 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
2455 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
2456 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
2457 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
2458 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
2459 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
2460 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
2461 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
2462 kvm_nvhe_sym(__icache_flags) = __icache_flags;
2463 kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
2464
2465 /* Propagate the FGT state to the nVHE side */
2466 kvm_nvhe_sym(hfgrtr_masks) = hfgrtr_masks;
2467 kvm_nvhe_sym(hfgwtr_masks) = hfgwtr_masks;
2468 kvm_nvhe_sym(hfgitr_masks) = hfgitr_masks;
2469 kvm_nvhe_sym(hdfgrtr_masks) = hdfgrtr_masks;
2470 kvm_nvhe_sym(hdfgwtr_masks) = hdfgwtr_masks;
2471 kvm_nvhe_sym(hafgrtr_masks) = hafgrtr_masks;
2472 kvm_nvhe_sym(hfgrtr2_masks) = hfgrtr2_masks;
2473 kvm_nvhe_sym(hfgwtr2_masks) = hfgwtr2_masks;
2474 kvm_nvhe_sym(hfgitr2_masks) = hfgitr2_masks;
2475 kvm_nvhe_sym(hdfgrtr2_masks)= hdfgrtr2_masks;
2476 kvm_nvhe_sym(hdfgwtr2_masks)= hdfgwtr2_masks;
2477
2478 /*
2479 * Flush entire BSS since part of its data containing init symbols is read
2480 * while the MMU is off.
2481 */
2482 kvm_flush_dcache_to_poc(kvm_ksym_ref(__hyp_bss_start),
2483 kvm_ksym_ref(__hyp_bss_end) - kvm_ksym_ref(__hyp_bss_start));
2484 }
2485
kvm_hyp_init_protection(u32 hyp_va_bits)2486 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
2487 {
2488 void *addr = phys_to_virt(hyp_mem_base);
2489 int ret;
2490
2491 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
2492 if (ret)
2493 return ret;
2494
2495 ret = do_pkvm_init(hyp_va_bits);
2496 if (ret)
2497 return ret;
2498
2499 free_hyp_pgds();
2500
2501 return 0;
2502 }
2503
init_pkvm_host_sve_state(void)2504 static int init_pkvm_host_sve_state(void)
2505 {
2506 int cpu;
2507
2508 if (!system_supports_sve())
2509 return 0;
2510
2511 /* Allocate pages for host sve state in protected mode. */
2512 for_each_possible_cpu(cpu) {
2513 struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order());
2514
2515 if (!page)
2516 return -ENOMEM;
2517
2518 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page);
2519 }
2520
2521 /*
2522 * Don't map the pages in hyp since these are only used in protected
2523 * mode, which will (re)create its own mapping when initialized.
2524 */
2525
2526 return 0;
2527 }
2528
2529 /*
2530 * Finalizes the initialization of hyp mode, once everything else is initialized
2531 * and the initialziation process cannot fail.
2532 */
finalize_init_hyp_mode(void)2533 static void finalize_init_hyp_mode(void)
2534 {
2535 int cpu;
2536
2537 if (system_supports_sve() && is_protected_kvm_enabled()) {
2538 for_each_possible_cpu(cpu) {
2539 struct cpu_sve_state *sve_state;
2540
2541 sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
2542 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state =
2543 kern_hyp_va(sve_state);
2544 }
2545 }
2546 }
2547
pkvm_hyp_init_ptrauth(void)2548 static void pkvm_hyp_init_ptrauth(void)
2549 {
2550 struct kvm_cpu_context *hyp_ctxt;
2551 int cpu;
2552
2553 for_each_possible_cpu(cpu) {
2554 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
2555 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
2556 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
2557 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
2558 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
2559 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
2560 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
2561 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
2562 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
2563 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
2564 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
2565 }
2566 }
2567
2568 /* Inits Hyp-mode on all online CPUs */
init_hyp_mode(void)2569 static int __init init_hyp_mode(void)
2570 {
2571 u32 hyp_va_bits;
2572 int cpu;
2573 int err = -ENOMEM;
2574
2575 /*
2576 * The protected Hyp-mode cannot be initialized if the memory pool
2577 * allocation has failed.
2578 */
2579 if (is_protected_kvm_enabled() && !hyp_mem_base)
2580 goto out_err;
2581
2582 /*
2583 * Allocate Hyp PGD and setup Hyp identity mapping
2584 */
2585 err = kvm_mmu_init(&hyp_va_bits);
2586 if (err)
2587 goto out_err;
2588
2589 /*
2590 * Allocate stack pages for Hypervisor-mode
2591 */
2592 for_each_possible_cpu(cpu) {
2593 unsigned long stack_base;
2594
2595 stack_base = __get_free_pages(GFP_KERNEL, NVHE_STACK_SHIFT - PAGE_SHIFT);
2596 if (!stack_base) {
2597 err = -ENOMEM;
2598 goto out_err;
2599 }
2600
2601 per_cpu(kvm_arm_hyp_stack_base, cpu) = stack_base;
2602 }
2603
2604 /*
2605 * Allocate and initialize pages for Hypervisor-mode percpu regions.
2606 */
2607 for_each_possible_cpu(cpu) {
2608 struct page *page;
2609 void *page_addr;
2610
2611 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
2612 if (!page) {
2613 err = -ENOMEM;
2614 goto out_err;
2615 }
2616
2617 page_addr = page_address(page);
2618 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2619 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
2620 }
2621
2622 /*
2623 * Map the Hyp-code called directly from the host
2624 */
2625 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
2626 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
2627 if (err) {
2628 kvm_err("Cannot map world-switch code\n");
2629 goto out_err;
2630 }
2631
2632 err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start),
2633 kvm_ksym_ref(__hyp_data_end), PAGE_HYP);
2634 if (err) {
2635 kvm_err("Cannot map .hyp.data section\n");
2636 goto out_err;
2637 }
2638
2639 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
2640 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
2641 if (err) {
2642 kvm_err("Cannot map .hyp.rodata section\n");
2643 goto out_err;
2644 }
2645
2646 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
2647 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
2648 if (err) {
2649 kvm_err("Cannot map rodata section\n");
2650 goto out_err;
2651 }
2652
2653 /*
2654 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2655 * section thanks to an assertion in the linker script. Map it RW and
2656 * the rest of .bss RO.
2657 */
2658 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2659 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2660 if (err) {
2661 kvm_err("Cannot map hyp bss section: %d\n", err);
2662 goto out_err;
2663 }
2664
2665 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
2666 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
2667 if (err) {
2668 kvm_err("Cannot map bss section\n");
2669 goto out_err;
2670 }
2671
2672 /*
2673 * Map the Hyp stack pages
2674 */
2675 for_each_possible_cpu(cpu) {
2676 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
2677 char *stack_base = (char *)per_cpu(kvm_arm_hyp_stack_base, cpu);
2678
2679 err = create_hyp_stack(__pa(stack_base), ¶ms->stack_hyp_va);
2680 if (err) {
2681 kvm_err("Cannot map hyp stack\n");
2682 goto out_err;
2683 }
2684
2685 /*
2686 * Save the stack PA in nvhe_init_params. This will be needed
2687 * to recreate the stack mapping in protected nVHE mode.
2688 * __hyp_pa() won't do the right thing there, since the stack
2689 * has been mapped in the flexible private VA space.
2690 */
2691 params->stack_pa = __pa(stack_base);
2692 }
2693
2694 for_each_possible_cpu(cpu) {
2695 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
2696 char *percpu_end = percpu_begin + nvhe_percpu_size();
2697
2698 /* Map Hyp percpu pages */
2699 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
2700 if (err) {
2701 kvm_err("Cannot map hyp percpu region\n");
2702 goto out_err;
2703 }
2704
2705 /* Prepare the CPU initialization parameters */
2706 cpu_prepare_hyp_mode(cpu, hyp_va_bits);
2707 }
2708
2709 kvm_hyp_init_symbols();
2710
2711 if (is_protected_kvm_enabled()) {
2712 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
2713 cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH))
2714 pkvm_hyp_init_ptrauth();
2715
2716 init_cpu_logical_map();
2717
2718 if (!init_psci_relay()) {
2719 err = -ENODEV;
2720 goto out_err;
2721 }
2722
2723 err = init_pkvm_host_sve_state();
2724 if (err)
2725 goto out_err;
2726
2727 err = kvm_hyp_init_protection(hyp_va_bits);
2728 if (err) {
2729 kvm_err("Failed to init hyp memory protection\n");
2730 goto out_err;
2731 }
2732 }
2733
2734 return 0;
2735
2736 out_err:
2737 teardown_hyp_mode();
2738 kvm_err("error initializing Hyp mode: %d\n", err);
2739 return err;
2740 }
2741
kvm_mpidr_to_vcpu(struct kvm * kvm,unsigned long mpidr)2742 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
2743 {
2744 struct kvm_vcpu *vcpu = NULL;
2745 struct kvm_mpidr_data *data;
2746 unsigned long i;
2747
2748 mpidr &= MPIDR_HWID_BITMASK;
2749
2750 rcu_read_lock();
2751 data = rcu_dereference(kvm->arch.mpidr_data);
2752
2753 if (data) {
2754 u16 idx = kvm_mpidr_index(data, mpidr);
2755
2756 vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]);
2757 if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu))
2758 vcpu = NULL;
2759 }
2760
2761 rcu_read_unlock();
2762
2763 if (vcpu)
2764 return vcpu;
2765
2766 kvm_for_each_vcpu(i, vcpu, kvm) {
2767 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
2768 return vcpu;
2769 }
2770 return NULL;
2771 }
2772
kvm_arch_irqchip_in_kernel(struct kvm * kvm)2773 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2774 {
2775 return irqchip_in_kernel(kvm);
2776 }
2777
kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)2778 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
2779 struct irq_bypass_producer *prod)
2780 {
2781 struct kvm_kernel_irqfd *irqfd =
2782 container_of(cons, struct kvm_kernel_irqfd, consumer);
2783 struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry;
2784
2785 /*
2786 * The only thing we have a chance of directly-injecting is LPIs. Maybe
2787 * one day...
2788 */
2789 if (irq_entry->type != KVM_IRQ_ROUTING_MSI)
2790 return 0;
2791
2792 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
2793 &irqfd->irq_entry);
2794 }
2795
kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)2796 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
2797 struct irq_bypass_producer *prod)
2798 {
2799 struct kvm_kernel_irqfd *irqfd =
2800 container_of(cons, struct kvm_kernel_irqfd, consumer);
2801 struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry;
2802
2803 if (irq_entry->type != KVM_IRQ_ROUTING_MSI)
2804 return;
2805
2806 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq);
2807 }
2808
kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd * irqfd,struct kvm_kernel_irq_routing_entry * old,struct kvm_kernel_irq_routing_entry * new)2809 void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd,
2810 struct kvm_kernel_irq_routing_entry *old,
2811 struct kvm_kernel_irq_routing_entry *new)
2812 {
2813 if (old->type == KVM_IRQ_ROUTING_MSI &&
2814 new->type == KVM_IRQ_ROUTING_MSI &&
2815 !memcmp(&old->msi, &new->msi, sizeof(new->msi)))
2816 return;
2817
2818 /*
2819 * Remapping the vLPI requires taking the its_lock mutex to resolve
2820 * the new translation. We're in spinlock land at this point, so no
2821 * chance of resolving the translation.
2822 *
2823 * Unmap the vLPI and fall back to software LPI injection.
2824 */
2825 return kvm_vgic_v4_unset_forwarding(irqfd->kvm, irqfd->producer->irq);
2826 }
2827
kvm_arch_irq_bypass_stop(struct irq_bypass_consumer * cons)2828 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
2829 {
2830 struct kvm_kernel_irqfd *irqfd =
2831 container_of(cons, struct kvm_kernel_irqfd, consumer);
2832
2833 kvm_arm_halt_guest(irqfd->kvm);
2834 }
2835
kvm_arch_irq_bypass_start(struct irq_bypass_consumer * cons)2836 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
2837 {
2838 struct kvm_kernel_irqfd *irqfd =
2839 container_of(cons, struct kvm_kernel_irqfd, consumer);
2840
2841 kvm_arm_resume_guest(irqfd->kvm);
2842 }
2843
2844 /* Initialize Hyp-mode and memory mappings on all CPUs */
kvm_arm_init(void)2845 static __init int kvm_arm_init(void)
2846 {
2847 int err;
2848 bool in_hyp_mode;
2849
2850 if (!is_hyp_mode_available()) {
2851 kvm_info("HYP mode not available\n");
2852 return -ENODEV;
2853 }
2854
2855 if (kvm_get_mode() == KVM_MODE_NONE) {
2856 kvm_info("KVM disabled from command line\n");
2857 return -ENODEV;
2858 }
2859
2860 err = kvm_sys_reg_table_init();
2861 if (err) {
2862 kvm_info("Error initializing system register tables");
2863 return err;
2864 }
2865
2866 in_hyp_mode = is_kernel_in_hyp_mode();
2867
2868 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
2869 cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2870 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2871 "Only trusted guests should be used on this system.\n");
2872
2873 err = kvm_set_ipa_limit();
2874 if (err)
2875 return err;
2876
2877 err = kvm_arm_init_sve();
2878 if (err)
2879 return err;
2880
2881 err = kvm_arm_vmid_alloc_init();
2882 if (err) {
2883 kvm_err("Failed to initialize VMID allocator.\n");
2884 return err;
2885 }
2886
2887 if (!in_hyp_mode) {
2888 err = init_hyp_mode();
2889 if (err)
2890 goto out_err;
2891 }
2892
2893 err = kvm_init_vector_slots();
2894 if (err) {
2895 kvm_err("Cannot initialise vector slots\n");
2896 goto out_hyp;
2897 }
2898
2899 err = init_subsystems();
2900 if (err)
2901 goto out_hyp;
2902
2903 kvm_info("%s%sVHE%s mode initialized successfully\n",
2904 in_hyp_mode ? "" : (is_protected_kvm_enabled() ?
2905 "Protected " : "Hyp "),
2906 in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ?
2907 "h" : "n"),
2908 cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) ? "+NV2": "");
2909
2910 /*
2911 * FIXME: Do something reasonable if kvm_init() fails after pKVM
2912 * hypervisor protection is finalized.
2913 */
2914 err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2915 if (err)
2916 goto out_subs;
2917
2918 /*
2919 * This should be called after initialization is done and failure isn't
2920 * possible anymore.
2921 */
2922 if (!in_hyp_mode)
2923 finalize_init_hyp_mode();
2924
2925 kvm_arm_initialised = true;
2926
2927 return 0;
2928
2929 out_subs:
2930 teardown_subsystems();
2931 out_hyp:
2932 if (!in_hyp_mode)
2933 teardown_hyp_mode();
2934 out_err:
2935 kvm_arm_vmid_alloc_free();
2936 return err;
2937 }
2938
early_kvm_mode_cfg(char * arg)2939 static int __init early_kvm_mode_cfg(char *arg)
2940 {
2941 if (!arg)
2942 return -EINVAL;
2943
2944 if (strcmp(arg, "none") == 0) {
2945 kvm_mode = KVM_MODE_NONE;
2946 return 0;
2947 }
2948
2949 if (!is_hyp_mode_available()) {
2950 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2951 return 0;
2952 }
2953
2954 if (strcmp(arg, "protected") == 0) {
2955 if (!is_kernel_in_hyp_mode())
2956 kvm_mode = KVM_MODE_PROTECTED;
2957 else
2958 pr_warn_once("Protected KVM not available with VHE\n");
2959
2960 return 0;
2961 }
2962
2963 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2964 kvm_mode = KVM_MODE_DEFAULT;
2965 return 0;
2966 }
2967
2968 if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2969 kvm_mode = KVM_MODE_NV;
2970 return 0;
2971 }
2972
2973 return -EINVAL;
2974 }
2975 early_param("kvm-arm.mode", early_kvm_mode_cfg);
2976
early_kvm_wfx_trap_policy_cfg(char * arg,enum kvm_wfx_trap_policy * p)2977 static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p)
2978 {
2979 if (!arg)
2980 return -EINVAL;
2981
2982 if (strcmp(arg, "trap") == 0) {
2983 *p = KVM_WFX_TRAP;
2984 return 0;
2985 }
2986
2987 if (strcmp(arg, "notrap") == 0) {
2988 *p = KVM_WFX_NOTRAP;
2989 return 0;
2990 }
2991
2992 return -EINVAL;
2993 }
2994
early_kvm_wfi_trap_policy_cfg(char * arg)2995 static int __init early_kvm_wfi_trap_policy_cfg(char *arg)
2996 {
2997 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy);
2998 }
2999 early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg);
3000
early_kvm_wfe_trap_policy_cfg(char * arg)3001 static int __init early_kvm_wfe_trap_policy_cfg(char *arg)
3002 {
3003 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy);
3004 }
3005 early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg);
3006
kvm_get_mode(void)3007 enum kvm_mode kvm_get_mode(void)
3008 {
3009 return kvm_mode;
3010 }
3011
3012 module_init(kvm_arm_init);
3013