1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
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>
17 #include <linux/mman.h>
18 #include <linux/sched.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_irqfd.h>
21 #include <linux/irqbypass.h>
22 #include <linux/sched/stat.h>
23 #include <linux/psci.h>
24 #include <trace/events/kvm.h>
26 #define CREATE_TRACE_POINTS
27 #include "trace_arm.h"
29 #include <linux/uaccess.h>
30 #include <asm/ptrace.h>
32 #include <asm/tlbflush.h>
33 #include <asm/cacheflush.h>
34 #include <asm/cpufeature.h>
36 #include <asm/kvm_arm.h>
37 #include <asm/kvm_asm.h>
38 #include <asm/kvm_mmu.h>
39 #include <asm/kvm_nested.h>
40 #include <asm/kvm_pkvm.h>
41 #include <asm/kvm_emulate.h>
42 #include <asm/sections.h>
44 #include <kvm/arm_hypercalls.h>
45 #include <kvm/arm_pmu.h>
46 #include <kvm/arm_psci.h>
48 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
50 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
52 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
53 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
55 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
57 static bool vgic_present, kvm_arm_initialised;
59 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
60 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
62 bool is_kvm_arm_initialised(void)
64 return kvm_arm_initialised;
67 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
69 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
72 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
73 struct kvm_enable_cap *cap)
82 case KVM_CAP_ARM_NISV_TO_USER:
84 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
88 mutex_lock(&kvm->lock);
89 if (!system_supports_mte() || kvm->created_vcpus) {
93 set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
95 mutex_unlock(&kvm->lock);
97 case KVM_CAP_ARM_SYSTEM_SUSPEND:
99 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
101 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
102 new_cap = cap->args[0];
104 mutex_lock(&kvm->slots_lock);
106 * To keep things simple, allow changing the chunk
107 * size only when no memory slots have been created.
109 if (!kvm_are_all_memslots_empty(kvm)) {
111 } else if (new_cap && !kvm_is_block_size_supported(new_cap)) {
115 kvm->arch.mmu.split_page_chunk_size = new_cap;
117 mutex_unlock(&kvm->slots_lock);
127 static int kvm_arm_default_max_vcpus(void)
129 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
133 * kvm_arch_init_vm - initializes a VM data structure
134 * @kvm: pointer to the KVM struct
136 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
140 mutex_init(&kvm->arch.config_lock);
142 #ifdef CONFIG_LOCKDEP
143 /* Clue in lockdep that the config_lock must be taken inside kvm->lock */
144 mutex_lock(&kvm->lock);
145 mutex_lock(&kvm->arch.config_lock);
146 mutex_unlock(&kvm->arch.config_lock);
147 mutex_unlock(&kvm->lock);
150 ret = kvm_share_hyp(kvm, kvm + 1);
154 ret = pkvm_init_host_vm(kvm);
156 goto err_unshare_kvm;
158 if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
160 goto err_unshare_kvm;
162 cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
164 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
166 goto err_free_cpumask;
168 kvm_vgic_early_init(kvm);
170 kvm_timer_init_vm(kvm);
172 /* The maximum number of VCPUs is limited by the host's GIC model */
173 kvm->max_vcpus = kvm_arm_default_max_vcpus();
175 kvm_arm_init_hypercalls(kvm);
177 bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
182 free_cpumask_var(kvm->arch.supported_cpus);
184 kvm_unshare_hyp(kvm, kvm + 1);
188 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
190 return VM_FAULT_SIGBUS;
195 * kvm_arch_destroy_vm - destroy the VM data structure
196 * @kvm: pointer to the KVM struct
198 void kvm_arch_destroy_vm(struct kvm *kvm)
200 bitmap_free(kvm->arch.pmu_filter);
201 free_cpumask_var(kvm->arch.supported_cpus);
203 kvm_vgic_destroy(kvm);
205 if (is_protected_kvm_enabled())
206 pkvm_destroy_hyp_vm(kvm);
208 kvm_destroy_vcpus(kvm);
210 kvm_unshare_hyp(kvm, kvm + 1);
212 kvm_arm_teardown_hypercalls(kvm);
215 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
219 case KVM_CAP_IRQCHIP:
222 case KVM_CAP_IOEVENTFD:
223 case KVM_CAP_DEVICE_CTRL:
224 case KVM_CAP_USER_MEMORY:
225 case KVM_CAP_SYNC_MMU:
226 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
227 case KVM_CAP_ONE_REG:
228 case KVM_CAP_ARM_PSCI:
229 case KVM_CAP_ARM_PSCI_0_2:
230 case KVM_CAP_READONLY_MEM:
231 case KVM_CAP_MP_STATE:
232 case KVM_CAP_IMMEDIATE_EXIT:
233 case KVM_CAP_VCPU_EVENTS:
234 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
235 case KVM_CAP_ARM_NISV_TO_USER:
236 case KVM_CAP_ARM_INJECT_EXT_DABT:
237 case KVM_CAP_SET_GUEST_DEBUG:
238 case KVM_CAP_VCPU_ATTRIBUTES:
239 case KVM_CAP_PTP_KVM:
240 case KVM_CAP_ARM_SYSTEM_SUSPEND:
241 case KVM_CAP_IRQFD_RESAMPLE:
242 case KVM_CAP_COUNTER_OFFSET:
245 case KVM_CAP_SET_GUEST_DEBUG2:
246 return KVM_GUESTDBG_VALID_MASK;
247 case KVM_CAP_ARM_SET_DEVICE_ADDR:
250 case KVM_CAP_NR_VCPUS:
252 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
253 * architectures, as it does not always bound it to
254 * KVM_CAP_MAX_VCPUS. It should not matter much because
255 * this is just an advisory value.
257 r = min_t(unsigned int, num_online_cpus(),
258 kvm_arm_default_max_vcpus());
260 case KVM_CAP_MAX_VCPUS:
261 case KVM_CAP_MAX_VCPU_ID:
265 r = kvm_arm_default_max_vcpus();
267 case KVM_CAP_MSI_DEVID:
271 r = kvm->arch.vgic.msis_require_devid;
273 case KVM_CAP_ARM_USER_IRQ:
275 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
276 * (bump this number if adding more devices)
280 case KVM_CAP_ARM_MTE:
281 r = system_supports_mte();
283 case KVM_CAP_STEAL_TIME:
284 r = kvm_arm_pvtime_supported();
286 case KVM_CAP_ARM_EL1_32BIT:
287 r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
289 case KVM_CAP_GUEST_DEBUG_HW_BPS:
292 case KVM_CAP_GUEST_DEBUG_HW_WPS:
295 case KVM_CAP_ARM_PMU_V3:
296 r = kvm_arm_support_pmu_v3();
298 case KVM_CAP_ARM_INJECT_SERROR_ESR:
299 r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
301 case KVM_CAP_ARM_VM_IPA_SIZE:
302 r = get_kvm_ipa_limit();
304 case KVM_CAP_ARM_SVE:
305 r = system_supports_sve();
307 case KVM_CAP_ARM_PTRAUTH_ADDRESS:
308 case KVM_CAP_ARM_PTRAUTH_GENERIC:
309 r = system_has_full_ptr_auth();
311 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
313 r = kvm->arch.mmu.split_page_chunk_size;
315 r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
317 case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
318 r = kvm_supported_block_sizes();
327 long kvm_arch_dev_ioctl(struct file *filp,
328 unsigned int ioctl, unsigned long arg)
333 struct kvm *kvm_arch_alloc_vm(void)
335 size_t sz = sizeof(struct kvm);
338 return kzalloc(sz, GFP_KERNEL_ACCOUNT);
340 return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
343 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
345 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
348 if (id >= kvm->max_vcpus)
354 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
358 spin_lock_init(&vcpu->arch.mp_state_lock);
360 #ifdef CONFIG_LOCKDEP
361 /* Inform lockdep that the config_lock is acquired after vcpu->mutex */
362 mutex_lock(&vcpu->mutex);
363 mutex_lock(&vcpu->kvm->arch.config_lock);
364 mutex_unlock(&vcpu->kvm->arch.config_lock);
365 mutex_unlock(&vcpu->mutex);
368 /* Force users to call KVM_ARM_VCPU_INIT */
369 vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
370 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
372 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
375 * Default value for the FP state, will be overloaded at load
376 * time if we support FP (pretty likely)
378 vcpu->arch.fp_state = FP_STATE_FREE;
380 /* Set up the timer */
381 kvm_timer_vcpu_init(vcpu);
383 kvm_pmu_vcpu_init(vcpu);
385 kvm_arm_reset_debug_ptr(vcpu);
387 kvm_arm_pvtime_vcpu_init(&vcpu->arch);
389 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
391 err = kvm_vgic_vcpu_init(vcpu);
395 return kvm_share_hyp(vcpu, vcpu + 1);
398 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
402 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
404 if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
405 static_branch_dec(&userspace_irqchip_in_use);
407 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
408 kvm_timer_vcpu_terminate(vcpu);
409 kvm_pmu_vcpu_destroy(vcpu);
411 kvm_arm_vcpu_destroy(vcpu);
414 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
419 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
424 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
426 struct kvm_s2_mmu *mmu;
429 mmu = vcpu->arch.hw_mmu;
430 last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
433 * We guarantee that both TLBs and I-cache are private to each
434 * vcpu. If detecting that a vcpu from the same VM has
435 * previously run on the same physical CPU, call into the
436 * hypervisor code to nuke the relevant contexts.
438 * We might get preempted before the vCPU actually runs, but
439 * over-invalidation doesn't affect correctness.
441 if (*last_ran != vcpu->vcpu_id) {
442 kvm_call_hyp(__kvm_flush_cpu_context, mmu);
443 *last_ran = vcpu->vcpu_id;
449 kvm_timer_vcpu_load(vcpu);
451 kvm_vcpu_load_sysregs_vhe(vcpu);
452 kvm_arch_vcpu_load_fp(vcpu);
453 kvm_vcpu_pmu_restore_guest(vcpu);
454 if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
455 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
457 if (single_task_running())
458 vcpu_clear_wfx_traps(vcpu);
460 vcpu_set_wfx_traps(vcpu);
462 if (vcpu_has_ptrauth(vcpu))
463 vcpu_ptrauth_disable(vcpu);
464 kvm_arch_vcpu_load_debug_state_flags(vcpu);
466 if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
467 vcpu_set_on_unsupported_cpu(vcpu);
470 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
472 kvm_arch_vcpu_put_debug_state_flags(vcpu);
473 kvm_arch_vcpu_put_fp(vcpu);
475 kvm_vcpu_put_sysregs_vhe(vcpu);
476 kvm_timer_vcpu_put(vcpu);
478 kvm_vcpu_pmu_restore_host(vcpu);
479 kvm_arm_vmid_clear_active();
481 vcpu_clear_on_unsupported_cpu(vcpu);
485 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
487 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
488 kvm_make_request(KVM_REQ_SLEEP, vcpu);
492 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
494 spin_lock(&vcpu->arch.mp_state_lock);
495 __kvm_arm_vcpu_power_off(vcpu);
496 spin_unlock(&vcpu->arch.mp_state_lock);
499 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
501 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
504 static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
506 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
507 kvm_make_request(KVM_REQ_SUSPEND, vcpu);
511 static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
513 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
516 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
517 struct kvm_mp_state *mp_state)
519 *mp_state = READ_ONCE(vcpu->arch.mp_state);
524 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
525 struct kvm_mp_state *mp_state)
529 spin_lock(&vcpu->arch.mp_state_lock);
531 switch (mp_state->mp_state) {
532 case KVM_MP_STATE_RUNNABLE:
533 WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
535 case KVM_MP_STATE_STOPPED:
536 __kvm_arm_vcpu_power_off(vcpu);
538 case KVM_MP_STATE_SUSPENDED:
539 kvm_arm_vcpu_suspend(vcpu);
545 spin_unlock(&vcpu->arch.mp_state_lock);
551 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
552 * @v: The VCPU pointer
554 * If the guest CPU is not waiting for interrupts or an interrupt line is
555 * asserted, the CPU is by definition runnable.
557 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
559 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
560 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
561 && !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
564 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
566 return vcpu_mode_priv(vcpu);
569 #ifdef CONFIG_GUEST_PERF_EVENTS
570 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
572 return *vcpu_pc(vcpu);
576 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
578 return vcpu_get_flag(vcpu, VCPU_INITIALIZED);
582 * Handle both the initialisation that is being done when the vcpu is
583 * run for the first time, as well as the updates that must be
584 * performed each time we get a new thread dealing with this vcpu.
586 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
588 struct kvm *kvm = vcpu->kvm;
591 if (!kvm_vcpu_initialized(vcpu))
594 if (!kvm_arm_vcpu_is_finalized(vcpu))
597 ret = kvm_arch_vcpu_run_map_fp(vcpu);
601 if (likely(vcpu_has_run_once(vcpu)))
604 kvm_arm_vcpu_init_debug(vcpu);
606 if (likely(irqchip_in_kernel(kvm))) {
608 * Map the VGIC hardware resources before running a vcpu the
609 * first time on this VM.
611 ret = kvm_vgic_map_resources(kvm);
616 ret = kvm_timer_enable(vcpu);
620 ret = kvm_arm_pmu_v3_enable(vcpu);
624 if (is_protected_kvm_enabled()) {
625 ret = pkvm_create_hyp_vm(kvm);
630 if (!irqchip_in_kernel(kvm)) {
632 * Tell the rest of the code that there are userspace irqchip
635 static_branch_inc(&userspace_irqchip_in_use);
639 * Initialize traps for protected VMs.
640 * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
641 * the code is in place for first run initialization at EL2.
643 if (kvm_vm_is_protected(kvm))
644 kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
646 mutex_lock(&kvm->arch.config_lock);
647 set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
648 mutex_unlock(&kvm->arch.config_lock);
653 bool kvm_arch_intc_initialized(struct kvm *kvm)
655 return vgic_initialized(kvm);
658 void kvm_arm_halt_guest(struct kvm *kvm)
661 struct kvm_vcpu *vcpu;
663 kvm_for_each_vcpu(i, vcpu, kvm)
664 vcpu->arch.pause = true;
665 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
668 void kvm_arm_resume_guest(struct kvm *kvm)
671 struct kvm_vcpu *vcpu;
673 kvm_for_each_vcpu(i, vcpu, kvm) {
674 vcpu->arch.pause = false;
675 __kvm_vcpu_wake_up(vcpu);
679 static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
681 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
683 rcuwait_wait_event(wait,
684 (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
687 if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
688 /* Awaken to handle a signal, request we sleep again later. */
689 kvm_make_request(KVM_REQ_SLEEP, vcpu);
693 * Make sure we will observe a potential reset request if we've
694 * observed a change to the power state. Pairs with the smp_wmb() in
695 * kvm_psci_vcpu_on().
701 * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
702 * @vcpu: The VCPU pointer
704 * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
705 * the vCPU is runnable. The vCPU may or may not be scheduled out, depending
706 * on when a wake event arrives, e.g. there may already be a pending wake event.
708 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
711 * Sync back the state of the GIC CPU interface so that we have
712 * the latest PMR and group enables. This ensures that
713 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
714 * we have pending interrupts, e.g. when determining if the
717 * For the same reason, we want to tell GICv4 that we need
718 * doorbells to be signalled, should an interrupt become pending.
721 kvm_vgic_vmcr_sync(vcpu);
722 vcpu_set_flag(vcpu, IN_WFI);
727 vcpu_clear_flag(vcpu, IN_WFIT);
730 vcpu_clear_flag(vcpu, IN_WFI);
735 static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
737 if (!kvm_arm_vcpu_suspended(vcpu))
743 * The suspend state is sticky; we do not leave it until userspace
744 * explicitly marks the vCPU as runnable. Request that we suspend again
747 kvm_make_request(KVM_REQ_SUSPEND, vcpu);
750 * Check to make sure the vCPU is actually runnable. If so, exit to
751 * userspace informing it of the wakeup condition.
753 if (kvm_arch_vcpu_runnable(vcpu)) {
754 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
755 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
756 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
761 * Otherwise, we were unblocked to process a different event, such as a
762 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
769 * check_vcpu_requests - check and handle pending vCPU requests
770 * @vcpu: the VCPU pointer
772 * Return: 1 if we should enter the guest
773 * 0 if we should exit to userspace
774 * < 0 if we should exit to userspace, where the return value indicates
777 static int check_vcpu_requests(struct kvm_vcpu *vcpu)
779 if (kvm_request_pending(vcpu)) {
780 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
781 kvm_vcpu_sleep(vcpu);
783 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
784 kvm_reset_vcpu(vcpu);
787 * Clear IRQ_PENDING requests that were made to guarantee
788 * that a VCPU sees new virtual interrupts.
790 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
792 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
793 kvm_update_stolen_time(vcpu);
795 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
796 /* The distributor enable bits were changed */
803 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
804 kvm_vcpu_reload_pmu(vcpu);
806 if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu))
807 kvm_vcpu_pmu_restore_guest(vcpu);
809 if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
810 return kvm_vcpu_suspend(vcpu);
812 if (kvm_dirty_ring_check_request(vcpu))
819 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
821 if (likely(!vcpu_mode_is_32bit(vcpu)))
824 if (vcpu_has_nv(vcpu))
827 return !kvm_supports_32bit_el0();
831 * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
832 * @vcpu: The VCPU pointer
833 * @ret: Pointer to write optional return code
835 * Returns: true if the VCPU needs to return to a preemptible + interruptible
836 * and skip guest entry.
838 * This function disambiguates between two different types of exits: exits to a
839 * preemptible + interruptible kernel context and exits to userspace. For an
840 * exit to userspace, this function will write the return code to ret and return
841 * true. For an exit to preemptible + interruptible kernel context (i.e. check
842 * for pending work and re-enter), return true without writing to ret.
844 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
846 struct kvm_run *run = vcpu->run;
849 * If we're using a userspace irqchip, then check if we need
850 * to tell a userspace irqchip about timer or PMU level
851 * changes and if so, exit to userspace (the actual level
852 * state gets updated in kvm_timer_update_run and
853 * kvm_pmu_update_run below).
855 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
856 if (kvm_timer_should_notify_user(vcpu) ||
857 kvm_pmu_should_notify_user(vcpu)) {
859 run->exit_reason = KVM_EXIT_INTR;
864 if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
865 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
866 run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
867 run->fail_entry.cpu = smp_processor_id();
872 return kvm_request_pending(vcpu) ||
873 xfer_to_guest_mode_work_pending();
877 * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
878 * the vCPU is running.
880 * This must be noinstr as instrumentation may make use of RCU, and this is not
881 * safe during the EQS.
883 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
887 guest_state_enter_irqoff();
888 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
889 guest_state_exit_irqoff();
895 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
896 * @vcpu: The VCPU pointer
898 * This function is called through the VCPU_RUN ioctl called from user space. It
899 * will execute VM code in a loop until the time slice for the process is used
900 * or some emulation is needed from user space in which case the function will
901 * return with return value 0 and with the kvm_run structure filled in with the
902 * required data for the requested emulation.
904 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
906 struct kvm_run *run = vcpu->run;
909 if (run->exit_reason == KVM_EXIT_MMIO) {
910 ret = kvm_handle_mmio_return(vcpu);
917 if (run->immediate_exit) {
922 kvm_sigset_activate(vcpu);
925 run->exit_reason = KVM_EXIT_UNKNOWN;
929 * Check conditions before entering the guest
931 ret = xfer_to_guest_mode_handle_work(vcpu);
936 ret = check_vcpu_requests(vcpu);
939 * Preparing the interrupts to be injected also
940 * involves poking the GIC, which must be done in a
941 * non-preemptible context.
946 * The VMID allocator only tracks active VMIDs per
947 * physical CPU, and therefore the VMID allocated may not be
948 * preserved on VMID roll-over if the task was preempted,
949 * making a thread's VMID inactive. So we need to call
950 * kvm_arm_vmid_update() in non-premptible context.
952 kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
954 kvm_pmu_flush_hwstate(vcpu);
958 kvm_vgic_flush_hwstate(vcpu);
960 kvm_pmu_update_vcpu_events(vcpu);
963 * Ensure we set mode to IN_GUEST_MODE after we disable
964 * interrupts and before the final VCPU requests check.
965 * See the comment in kvm_vcpu_exiting_guest_mode() and
966 * Documentation/virt/kvm/vcpu-requests.rst
968 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
970 if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
971 vcpu->mode = OUTSIDE_GUEST_MODE;
972 isb(); /* Ensure work in x_flush_hwstate is committed */
973 kvm_pmu_sync_hwstate(vcpu);
974 if (static_branch_unlikely(&userspace_irqchip_in_use))
975 kvm_timer_sync_user(vcpu);
976 kvm_vgic_sync_hwstate(vcpu);
982 kvm_arm_setup_debug(vcpu);
983 kvm_arch_vcpu_ctxflush_fp(vcpu);
985 /**************************************************************
988 trace_kvm_entry(*vcpu_pc(vcpu));
989 guest_timing_enter_irqoff();
991 ret = kvm_arm_vcpu_enter_exit(vcpu);
993 vcpu->mode = OUTSIDE_GUEST_MODE;
997 *************************************************************/
999 kvm_arm_clear_debug(vcpu);
1002 * We must sync the PMU state before the vgic state so
1003 * that the vgic can properly sample the updated state of the
1006 kvm_pmu_sync_hwstate(vcpu);
1009 * Sync the vgic state before syncing the timer state because
1010 * the timer code needs to know if the virtual timer
1011 * interrupts are active.
1013 kvm_vgic_sync_hwstate(vcpu);
1016 * Sync the timer hardware state before enabling interrupts as
1017 * we don't want vtimer interrupts to race with syncing the
1018 * timer virtual interrupt state.
1020 if (static_branch_unlikely(&userspace_irqchip_in_use))
1021 kvm_timer_sync_user(vcpu);
1023 kvm_arch_vcpu_ctxsync_fp(vcpu);
1026 * We must ensure that any pending interrupts are taken before
1027 * we exit guest timing so that timer ticks are accounted as
1028 * guest time. Transiently unmask interrupts so that any
1029 * pending interrupts are taken.
1031 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
1032 * context synchronization event) is necessary to ensure that
1033 * pending interrupts are taken.
1035 if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
1038 local_irq_disable();
1041 guest_timing_exit_irqoff();
1045 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1047 /* Exit types that need handling before we can be preempted */
1048 handle_exit_early(vcpu, ret);
1053 * The ARMv8 architecture doesn't give the hypervisor
1054 * a mechanism to prevent a guest from dropping to AArch32 EL0
1055 * if implemented by the CPU. If we spot the guest in such
1056 * state and that we decided it wasn't supposed to do so (like
1057 * with the asymmetric AArch32 case), return to userspace with
1060 if (vcpu_mode_is_bad_32bit(vcpu)) {
1062 * As we have caught the guest red-handed, decide that
1063 * it isn't fit for purpose anymore by making the vcpu
1064 * invalid. The VMM can try and fix it by issuing a
1065 * KVM_ARM_VCPU_INIT if it really wants to.
1067 vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
1068 ret = ARM_EXCEPTION_IL;
1071 ret = handle_exit(vcpu, ret);
1074 /* Tell userspace about in-kernel device output levels */
1075 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
1076 kvm_timer_update_run(vcpu);
1077 kvm_pmu_update_run(vcpu);
1080 kvm_sigset_deactivate(vcpu);
1084 * In the unlikely event that we are returning to userspace
1085 * with pending exceptions or PC adjustment, commit these
1086 * adjustments in order to give userspace a consistent view of
1087 * the vcpu state. Note that this relies on __kvm_adjust_pc()
1088 * being preempt-safe on VHE.
1090 if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1091 vcpu_get_flag(vcpu, INCREMENT_PC)))
1092 kvm_call_hyp(__kvm_adjust_pc, vcpu);
1098 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
1104 if (number == KVM_ARM_IRQ_CPU_IRQ)
1105 bit_index = __ffs(HCR_VI);
1106 else /* KVM_ARM_IRQ_CPU_FIQ */
1107 bit_index = __ffs(HCR_VF);
1109 hcr = vcpu_hcr(vcpu);
1111 set = test_and_set_bit(bit_index, hcr);
1113 set = test_and_clear_bit(bit_index, hcr);
1116 * If we didn't change anything, no need to wake up or kick other CPUs
1122 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
1123 * trigger a world-switch round on the running physical CPU to set the
1124 * virtual IRQ/FIQ fields in the HCR appropriately.
1126 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
1127 kvm_vcpu_kick(vcpu);
1132 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
1135 u32 irq = irq_level->irq;
1136 unsigned int irq_type, vcpu_idx, irq_num;
1137 int nrcpus = atomic_read(&kvm->online_vcpus);
1138 struct kvm_vcpu *vcpu = NULL;
1139 bool level = irq_level->level;
1141 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
1142 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
1143 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
1144 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
1146 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
1149 case KVM_ARM_IRQ_TYPE_CPU:
1150 if (irqchip_in_kernel(kvm))
1153 if (vcpu_idx >= nrcpus)
1156 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1160 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
1163 return vcpu_interrupt_line(vcpu, irq_num, level);
1164 case KVM_ARM_IRQ_TYPE_PPI:
1165 if (!irqchip_in_kernel(kvm))
1168 if (vcpu_idx >= nrcpus)
1171 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1175 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
1178 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
1179 case KVM_ARM_IRQ_TYPE_SPI:
1180 if (!irqchip_in_kernel(kvm))
1183 if (irq_num < VGIC_NR_PRIVATE_IRQS)
1186 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
1192 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
1193 const struct kvm_vcpu_init *init)
1195 unsigned long features = init->features[0];
1198 if (features & ~KVM_VCPU_VALID_FEATURES)
1201 for (i = 1; i < ARRAY_SIZE(init->features); i++) {
1202 if (init->features[i])
1206 if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
1209 if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1))
1212 /* MTE is incompatible with AArch32 */
1213 if (kvm_has_mte(vcpu->kvm))
1216 /* NV is incompatible with AArch32 */
1217 if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
1223 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
1224 const struct kvm_vcpu_init *init)
1226 unsigned long features = init->features[0];
1228 return !bitmap_equal(vcpu->arch.features, &features, KVM_VCPU_MAX_FEATURES);
1231 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
1233 struct kvm *kvm = vcpu->kvm;
1237 * When the vCPU has a PMU, but no PMU is set for the guest
1238 * yet, set the default one.
1240 if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu)
1241 ret = kvm_arm_set_default_pmu(kvm);
1246 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1247 const struct kvm_vcpu_init *init)
1249 unsigned long features = init->features[0];
1250 struct kvm *kvm = vcpu->kvm;
1253 mutex_lock(&kvm->arch.config_lock);
1255 if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
1256 !bitmap_equal(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES))
1259 bitmap_copy(vcpu->arch.features, &features, KVM_VCPU_MAX_FEATURES);
1261 ret = kvm_setup_vcpu(vcpu);
1265 /* Now we know what it is, we can reset it. */
1266 ret = kvm_reset_vcpu(vcpu);
1268 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1272 bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
1273 set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
1274 vcpu_set_flag(vcpu, VCPU_INITIALIZED);
1276 mutex_unlock(&kvm->arch.config_lock);
1280 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1281 const struct kvm_vcpu_init *init)
1285 if (init->target != KVM_ARM_TARGET_GENERIC_V8 &&
1286 init->target != kvm_target_cpu())
1289 ret = kvm_vcpu_init_check_features(vcpu, init);
1293 if (!kvm_vcpu_initialized(vcpu))
1294 return __kvm_vcpu_set_target(vcpu, init);
1296 if (kvm_vcpu_init_changed(vcpu, init))
1299 return kvm_reset_vcpu(vcpu);
1302 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1303 struct kvm_vcpu_init *init)
1305 bool power_off = false;
1309 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
1310 * reflecting it in the finalized feature set, thus limiting its scope
1311 * to a single KVM_ARM_VCPU_INIT call.
1313 if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
1314 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
1318 ret = kvm_vcpu_set_target(vcpu, init);
1323 * Ensure a rebooted VM will fault in RAM pages and detect if the
1324 * guest MMU is turned off and flush the caches as needed.
1326 * S2FWB enforces all memory accesses to RAM being cacheable,
1327 * ensuring that the data side is always coherent. We still
1328 * need to invalidate the I-cache though, as FWB does *not*
1329 * imply CTR_EL0.DIC.
1331 if (vcpu_has_run_once(vcpu)) {
1332 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
1333 stage2_unmap_vm(vcpu->kvm);
1335 icache_inval_all_pou();
1338 vcpu_reset_hcr(vcpu);
1339 vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu);
1342 * Handle the "start in power-off" case.
1344 spin_lock(&vcpu->arch.mp_state_lock);
1347 __kvm_arm_vcpu_power_off(vcpu);
1349 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
1351 spin_unlock(&vcpu->arch.mp_state_lock);
1356 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1357 struct kvm_device_attr *attr)
1361 switch (attr->group) {
1363 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
1370 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1371 struct kvm_device_attr *attr)
1375 switch (attr->group) {
1377 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
1384 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1385 struct kvm_device_attr *attr)
1389 switch (attr->group) {
1391 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
1398 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1399 struct kvm_vcpu_events *events)
1401 memset(events, 0, sizeof(*events));
1403 return __kvm_arm_vcpu_get_events(vcpu, events);
1406 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1407 struct kvm_vcpu_events *events)
1411 /* check whether the reserved field is zero */
1412 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1413 if (events->reserved[i])
1416 /* check whether the pad field is zero */
1417 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1418 if (events->exception.pad[i])
1421 return __kvm_arm_vcpu_set_events(vcpu, events);
1424 long kvm_arch_vcpu_ioctl(struct file *filp,
1425 unsigned int ioctl, unsigned long arg)
1427 struct kvm_vcpu *vcpu = filp->private_data;
1428 void __user *argp = (void __user *)arg;
1429 struct kvm_device_attr attr;
1433 case KVM_ARM_VCPU_INIT: {
1434 struct kvm_vcpu_init init;
1437 if (copy_from_user(&init, argp, sizeof(init)))
1440 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1443 case KVM_SET_ONE_REG:
1444 case KVM_GET_ONE_REG: {
1445 struct kvm_one_reg reg;
1448 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1452 if (copy_from_user(®, argp, sizeof(reg)))
1456 * We could owe a reset due to PSCI. Handle the pending reset
1457 * here to ensure userspace register accesses are ordered after
1460 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
1461 kvm_reset_vcpu(vcpu);
1463 if (ioctl == KVM_SET_ONE_REG)
1464 r = kvm_arm_set_reg(vcpu, ®);
1466 r = kvm_arm_get_reg(vcpu, ®);
1469 case KVM_GET_REG_LIST: {
1470 struct kvm_reg_list __user *user_list = argp;
1471 struct kvm_reg_list reg_list;
1475 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1479 if (!kvm_arm_vcpu_is_finalized(vcpu))
1483 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
1486 reg_list.n = kvm_arm_num_regs(vcpu);
1487 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
1492 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1495 case KVM_SET_DEVICE_ATTR: {
1497 if (copy_from_user(&attr, argp, sizeof(attr)))
1499 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1502 case KVM_GET_DEVICE_ATTR: {
1504 if (copy_from_user(&attr, argp, sizeof(attr)))
1506 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1509 case KVM_HAS_DEVICE_ATTR: {
1511 if (copy_from_user(&attr, argp, sizeof(attr)))
1513 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1516 case KVM_GET_VCPU_EVENTS: {
1517 struct kvm_vcpu_events events;
1519 if (kvm_arm_vcpu_get_events(vcpu, &events))
1522 if (copy_to_user(argp, &events, sizeof(events)))
1527 case KVM_SET_VCPU_EVENTS: {
1528 struct kvm_vcpu_events events;
1530 if (copy_from_user(&events, argp, sizeof(events)))
1533 return kvm_arm_vcpu_set_events(vcpu, &events);
1535 case KVM_ARM_VCPU_FINALIZE: {
1538 if (!kvm_vcpu_initialized(vcpu))
1541 if (get_user(what, (const int __user *)argp))
1544 return kvm_arm_vcpu_finalize(vcpu, what);
1553 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1558 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1559 struct kvm_arm_device_addr *dev_addr)
1561 switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
1562 case KVM_ARM_DEVICE_VGIC_V2:
1565 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
1571 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1573 switch (attr->group) {
1574 case KVM_ARM_VM_SMCCC_CTRL:
1575 return kvm_vm_smccc_has_attr(kvm, attr);
1581 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1583 switch (attr->group) {
1584 case KVM_ARM_VM_SMCCC_CTRL:
1585 return kvm_vm_smccc_set_attr(kvm, attr);
1591 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1593 struct kvm *kvm = filp->private_data;
1594 void __user *argp = (void __user *)arg;
1595 struct kvm_device_attr attr;
1598 case KVM_CREATE_IRQCHIP: {
1602 mutex_lock(&kvm->lock);
1603 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1604 mutex_unlock(&kvm->lock);
1607 case KVM_ARM_SET_DEVICE_ADDR: {
1608 struct kvm_arm_device_addr dev_addr;
1610 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1612 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1614 case KVM_ARM_PREFERRED_TARGET: {
1615 struct kvm_vcpu_init init = {
1616 .target = KVM_ARM_TARGET_GENERIC_V8,
1619 if (copy_to_user(argp, &init, sizeof(init)))
1624 case KVM_ARM_MTE_COPY_TAGS: {
1625 struct kvm_arm_copy_mte_tags copy_tags;
1627 if (copy_from_user(©_tags, argp, sizeof(copy_tags)))
1629 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags);
1631 case KVM_ARM_SET_COUNTER_OFFSET: {
1632 struct kvm_arm_counter_offset offset;
1634 if (copy_from_user(&offset, argp, sizeof(offset)))
1636 return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
1638 case KVM_HAS_DEVICE_ATTR: {
1639 if (copy_from_user(&attr, argp, sizeof(attr)))
1642 return kvm_vm_has_attr(kvm, &attr);
1644 case KVM_SET_DEVICE_ATTR: {
1645 if (copy_from_user(&attr, argp, sizeof(attr)))
1648 return kvm_vm_set_attr(kvm, &attr);
1655 /* unlocks vcpus from @vcpu_lock_idx and smaller */
1656 static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx)
1658 struct kvm_vcpu *tmp_vcpu;
1660 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1661 tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1662 mutex_unlock(&tmp_vcpu->mutex);
1666 void unlock_all_vcpus(struct kvm *kvm)
1668 lockdep_assert_held(&kvm->lock);
1670 unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1);
1673 /* Returns true if all vcpus were locked, false otherwise */
1674 bool lock_all_vcpus(struct kvm *kvm)
1676 struct kvm_vcpu *tmp_vcpu;
1679 lockdep_assert_held(&kvm->lock);
1682 * Any time a vcpu is in an ioctl (including running), the
1683 * core KVM code tries to grab the vcpu->mutex.
1685 * By grabbing the vcpu->mutex of all VCPUs we ensure that no
1686 * other VCPUs can fiddle with the state while we access it.
1688 kvm_for_each_vcpu(c, tmp_vcpu, kvm) {
1689 if (!mutex_trylock(&tmp_vcpu->mutex)) {
1690 unlock_vcpus(kvm, c - 1);
1698 static unsigned long nvhe_percpu_size(void)
1700 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
1701 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
1704 static unsigned long nvhe_percpu_order(void)
1706 unsigned long size = nvhe_percpu_size();
1708 return size ? get_order(size) : 0;
1711 /* A lookup table holding the hypervisor VA for each vector slot */
1712 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1714 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1716 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
1719 static int kvm_init_vector_slots(void)
1724 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1725 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1727 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1728 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1730 if (kvm_system_needs_idmapped_vectors() &&
1731 !is_protected_kvm_enabled()) {
1732 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1733 __BP_HARDEN_HYP_VECS_SZ, &base);
1738 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1739 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
1743 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
1745 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1749 * Calculate the raw per-cpu offset without a translation from the
1750 * kernel's mapping to the linear mapping, and store it in tpidr_el2
1751 * so that we can use adr_l to access per-cpu variables in EL2.
1752 * Also drop the KASAN tag which gets in the way...
1754 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
1755 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
1757 params->mair_el2 = read_sysreg(mair_el1);
1759 tcr = read_sysreg(tcr_el1);
1760 if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
1761 tcr |= TCR_EPD1_MASK;
1763 tcr &= TCR_EL2_MASK;
1764 tcr |= TCR_EL2_RES1;
1766 tcr &= ~TCR_T0SZ_MASK;
1767 tcr |= TCR_T0SZ(hyp_va_bits);
1768 params->tcr_el2 = tcr;
1770 params->pgd_pa = kvm_mmu_get_httbr();
1771 if (is_protected_kvm_enabled())
1772 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1774 params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1775 if (cpus_have_final_cap(ARM64_KVM_HVHE))
1776 params->hcr_el2 |= HCR_E2H;
1777 params->vttbr = params->vtcr = 0;
1780 * Flush the init params from the data cache because the struct will
1781 * be read while the MMU is off.
1783 kvm_flush_dcache_to_poc(params, sizeof(*params));
1786 static void hyp_install_host_vector(void)
1788 struct kvm_nvhe_init_params *params;
1789 struct arm_smccc_res res;
1791 /* Switch from the HYP stub to our own HYP init vector */
1792 __hyp_set_vectors(kvm_get_idmap_vector());
1795 * Call initialization code, and switch to the full blown HYP code.
1796 * If the cpucaps haven't been finalized yet, something has gone very
1797 * wrong, and hyp will crash and burn when it uses any
1798 * cpus_have_const_cap() wrapper.
1800 BUG_ON(!system_capabilities_finalized());
1801 params = this_cpu_ptr_nvhe_sym(kvm_init_params);
1802 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
1803 WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1806 static void cpu_init_hyp_mode(void)
1808 hyp_install_host_vector();
1811 * Disabling SSBD on a non-VHE system requires us to enable SSBS
1814 if (this_cpu_has_cap(ARM64_SSBS) &&
1815 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
1816 kvm_call_hyp_nvhe(__kvm_enable_ssbs);
1820 static void cpu_hyp_reset(void)
1822 if (!is_kernel_in_hyp_mode())
1823 __hyp_reset_vectors();
1827 * EL2 vectors can be mapped and rerouted in a number of ways,
1828 * depending on the kernel configuration and CPU present:
1830 * - If the CPU is affected by Spectre-v2, the hardening sequence is
1831 * placed in one of the vector slots, which is executed before jumping
1832 * to the real vectors.
1834 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1835 * containing the hardening sequence is mapped next to the idmap page,
1836 * and executed before jumping to the real vectors.
1838 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1839 * empty slot is selected, mapped next to the idmap page, and
1840 * executed before jumping to the real vectors.
1842 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1843 * VHE, as we don't have hypervisor-specific mappings. If the system
1844 * is VHE and yet selects this capability, it will be ignored.
1846 static void cpu_set_hyp_vector(void)
1848 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1849 void *vector = hyp_spectre_vector_selector[data->slot];
1851 if (!is_protected_kvm_enabled())
1852 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1854 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1857 static void cpu_hyp_init_context(void)
1859 kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
1861 if (!is_kernel_in_hyp_mode())
1862 cpu_init_hyp_mode();
1865 static void cpu_hyp_init_features(void)
1867 cpu_set_hyp_vector();
1868 kvm_arm_init_debug();
1870 if (is_kernel_in_hyp_mode())
1871 kvm_timer_init_vhe();
1874 kvm_vgic_init_cpu_hardware();
1877 static void cpu_hyp_reinit(void)
1880 cpu_hyp_init_context();
1881 cpu_hyp_init_features();
1884 static void cpu_hyp_init(void *discard)
1886 if (!__this_cpu_read(kvm_hyp_initialized)) {
1888 __this_cpu_write(kvm_hyp_initialized, 1);
1892 static void cpu_hyp_uninit(void *discard)
1894 if (__this_cpu_read(kvm_hyp_initialized)) {
1896 __this_cpu_write(kvm_hyp_initialized, 0);
1900 int kvm_arch_hardware_enable(void)
1903 * Most calls to this function are made with migration
1904 * disabled, but not with preemption disabled. The former is
1905 * enough to ensure correctness, but most of the helpers
1906 * expect the later and will throw a tantrum otherwise.
1920 void kvm_arch_hardware_disable(void)
1922 kvm_timer_cpu_down();
1923 kvm_vgic_cpu_down();
1925 if (!is_protected_kvm_enabled())
1926 cpu_hyp_uninit(NULL);
1929 #ifdef CONFIG_CPU_PM
1930 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1935 * kvm_hyp_initialized is left with its old value over
1936 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1941 if (__this_cpu_read(kvm_hyp_initialized))
1943 * don't update kvm_hyp_initialized here
1944 * so that the hyp will be re-enabled
1945 * when we resume. See below.
1950 case CPU_PM_ENTER_FAILED:
1952 if (__this_cpu_read(kvm_hyp_initialized))
1953 /* The hyp was enabled before suspend. */
1963 static struct notifier_block hyp_init_cpu_pm_nb = {
1964 .notifier_call = hyp_init_cpu_pm_notifier,
1967 static void __init hyp_cpu_pm_init(void)
1969 if (!is_protected_kvm_enabled())
1970 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1972 static void __init hyp_cpu_pm_exit(void)
1974 if (!is_protected_kvm_enabled())
1975 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1978 static inline void __init hyp_cpu_pm_init(void)
1981 static inline void __init hyp_cpu_pm_exit(void)
1986 static void __init init_cpu_logical_map(void)
1991 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
1992 * Only copy the set of online CPUs whose features have been checked
1993 * against the finalized system capabilities. The hypervisor will not
1994 * allow any other CPUs from the `possible` set to boot.
1996 for_each_online_cpu(cpu)
1997 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
2000 #define init_psci_0_1_impl_state(config, what) \
2001 config.psci_0_1_ ## what ## _implemented = psci_ops.what
2003 static bool __init init_psci_relay(void)
2006 * If PSCI has not been initialized, protected KVM cannot install
2007 * itself on newly booted CPUs.
2009 if (!psci_ops.get_version) {
2010 kvm_err("Cannot initialize protected mode without PSCI\n");
2014 kvm_host_psci_config.version = psci_ops.get_version();
2015 kvm_host_psci_config.smccc_version = arm_smccc_get_version();
2017 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
2018 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
2019 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
2020 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
2021 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
2022 init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
2027 static int __init init_subsystems(void)
2032 * Enable hardware so that subsystem initialisation can access EL2.
2034 on_each_cpu(cpu_hyp_init, NULL, 1);
2037 * Register CPU lower-power notifier
2042 * Init HYP view of VGIC
2044 err = kvm_vgic_hyp_init();
2047 vgic_present = true;
2051 vgic_present = false;
2059 * Init HYP architected timer support
2061 err = kvm_timer_hyp_init(vgic_present);
2065 kvm_register_perf_callbacks(NULL);
2071 if (err || !is_protected_kvm_enabled())
2072 on_each_cpu(cpu_hyp_uninit, NULL, 1);
2077 static void __init teardown_subsystems(void)
2079 kvm_unregister_perf_callbacks();
2083 static void __init teardown_hyp_mode(void)
2088 for_each_possible_cpu(cpu) {
2089 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
2090 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
2094 static int __init do_pkvm_init(u32 hyp_va_bits)
2096 void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
2100 cpu_hyp_init_context();
2101 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
2102 num_possible_cpus(), kern_hyp_va(per_cpu_base),
2104 cpu_hyp_init_features();
2107 * The stub hypercalls are now disabled, so set our local flag to
2108 * prevent a later re-init attempt in kvm_arch_hardware_enable().
2110 __this_cpu_write(kvm_hyp_initialized, 1);
2116 static u64 get_hyp_id_aa64pfr0_el1(void)
2119 * Track whether the system isn't affected by spectre/meltdown in the
2120 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
2121 * Although this is per-CPU, we make it global for simplicity, e.g., not
2122 * to have to worry about vcpu migration.
2124 * Unlike for non-protected VMs, userspace cannot override this for
2127 u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
2129 val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
2130 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
2132 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
2133 arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
2134 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
2135 arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
2140 static void kvm_hyp_init_symbols(void)
2142 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
2143 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
2144 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
2145 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
2146 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
2147 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
2148 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
2149 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
2150 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
2151 kvm_nvhe_sym(__icache_flags) = __icache_flags;
2152 kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
2155 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
2157 void *addr = phys_to_virt(hyp_mem_base);
2160 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
2164 ret = do_pkvm_init(hyp_va_bits);
2173 static void pkvm_hyp_init_ptrauth(void)
2175 struct kvm_cpu_context *hyp_ctxt;
2178 for_each_possible_cpu(cpu) {
2179 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
2180 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
2181 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
2182 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
2183 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
2184 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
2185 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
2186 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
2187 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
2188 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
2189 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
2193 /* Inits Hyp-mode on all online CPUs */
2194 static int __init init_hyp_mode(void)
2201 * The protected Hyp-mode cannot be initialized if the memory pool
2202 * allocation has failed.
2204 if (is_protected_kvm_enabled() && !hyp_mem_base)
2208 * Allocate Hyp PGD and setup Hyp identity mapping
2210 err = kvm_mmu_init(&hyp_va_bits);
2215 * Allocate stack pages for Hypervisor-mode
2217 for_each_possible_cpu(cpu) {
2218 unsigned long stack_page;
2220 stack_page = __get_free_page(GFP_KERNEL);
2226 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
2230 * Allocate and initialize pages for Hypervisor-mode percpu regions.
2232 for_each_possible_cpu(cpu) {
2236 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
2242 page_addr = page_address(page);
2243 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2244 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
2248 * Map the Hyp-code called directly from the host
2250 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
2251 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
2253 kvm_err("Cannot map world-switch code\n");
2257 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
2258 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
2260 kvm_err("Cannot map .hyp.rodata section\n");
2264 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
2265 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
2267 kvm_err("Cannot map rodata section\n");
2272 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2273 * section thanks to an assertion in the linker script. Map it RW and
2274 * the rest of .bss RO.
2276 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2277 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2279 kvm_err("Cannot map hyp bss section: %d\n", err);
2283 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
2284 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
2286 kvm_err("Cannot map bss section\n");
2291 * Map the Hyp stack pages
2293 for_each_possible_cpu(cpu) {
2294 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
2295 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
2297 err = create_hyp_stack(__pa(stack_page), ¶ms->stack_hyp_va);
2299 kvm_err("Cannot map hyp stack\n");
2304 * Save the stack PA in nvhe_init_params. This will be needed
2305 * to recreate the stack mapping in protected nVHE mode.
2306 * __hyp_pa() won't do the right thing there, since the stack
2307 * has been mapped in the flexible private VA space.
2309 params->stack_pa = __pa(stack_page);
2312 for_each_possible_cpu(cpu) {
2313 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
2314 char *percpu_end = percpu_begin + nvhe_percpu_size();
2316 /* Map Hyp percpu pages */
2317 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
2319 kvm_err("Cannot map hyp percpu region\n");
2323 /* Prepare the CPU initialization parameters */
2324 cpu_prepare_hyp_mode(cpu, hyp_va_bits);
2327 kvm_hyp_init_symbols();
2329 if (is_protected_kvm_enabled()) {
2330 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
2331 cpus_have_const_cap(ARM64_HAS_ADDRESS_AUTH))
2332 pkvm_hyp_init_ptrauth();
2334 init_cpu_logical_map();
2336 if (!init_psci_relay()) {
2341 err = kvm_hyp_init_protection(hyp_va_bits);
2343 kvm_err("Failed to init hyp memory protection\n");
2351 teardown_hyp_mode();
2352 kvm_err("error initializing Hyp mode: %d\n", err);
2356 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
2358 struct kvm_vcpu *vcpu;
2361 mpidr &= MPIDR_HWID_BITMASK;
2362 kvm_for_each_vcpu(i, vcpu, kvm) {
2363 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
2369 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2371 return irqchip_in_kernel(kvm);
2374 bool kvm_arch_has_irq_bypass(void)
2379 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
2380 struct irq_bypass_producer *prod)
2382 struct kvm_kernel_irqfd *irqfd =
2383 container_of(cons, struct kvm_kernel_irqfd, consumer);
2385 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
2388 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
2389 struct irq_bypass_producer *prod)
2391 struct kvm_kernel_irqfd *irqfd =
2392 container_of(cons, struct kvm_kernel_irqfd, consumer);
2394 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
2398 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
2400 struct kvm_kernel_irqfd *irqfd =
2401 container_of(cons, struct kvm_kernel_irqfd, consumer);
2403 kvm_arm_halt_guest(irqfd->kvm);
2406 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
2408 struct kvm_kernel_irqfd *irqfd =
2409 container_of(cons, struct kvm_kernel_irqfd, consumer);
2411 kvm_arm_resume_guest(irqfd->kvm);
2414 /* Initialize Hyp-mode and memory mappings on all CPUs */
2415 static __init int kvm_arm_init(void)
2420 if (!is_hyp_mode_available()) {
2421 kvm_info("HYP mode not available\n");
2425 if (kvm_get_mode() == KVM_MODE_NONE) {
2426 kvm_info("KVM disabled from command line\n");
2430 err = kvm_sys_reg_table_init();
2432 kvm_info("Error initializing system register tables");
2436 in_hyp_mode = is_kernel_in_hyp_mode();
2438 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
2439 cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2440 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2441 "Only trusted guests should be used on this system.\n");
2443 err = kvm_set_ipa_limit();
2447 err = kvm_arm_init_sve();
2451 err = kvm_arm_vmid_alloc_init();
2453 kvm_err("Failed to initialize VMID allocator.\n");
2458 err = init_hyp_mode();
2463 err = kvm_init_vector_slots();
2465 kvm_err("Cannot initialise vector slots\n");
2469 err = init_subsystems();
2473 if (is_protected_kvm_enabled()) {
2474 kvm_info("Protected nVHE mode initialized successfully\n");
2475 } else if (in_hyp_mode) {
2476 kvm_info("VHE mode initialized successfully\n");
2478 kvm_info("Hyp mode initialized successfully\n");
2482 * FIXME: Do something reasonable if kvm_init() fails after pKVM
2483 * hypervisor protection is finalized.
2485 err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2489 kvm_arm_initialised = true;
2494 teardown_subsystems();
2497 teardown_hyp_mode();
2499 kvm_arm_vmid_alloc_free();
2503 static int __init early_kvm_mode_cfg(char *arg)
2508 if (strcmp(arg, "none") == 0) {
2509 kvm_mode = KVM_MODE_NONE;
2513 if (!is_hyp_mode_available()) {
2514 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2518 if (strcmp(arg, "protected") == 0) {
2519 if (!is_kernel_in_hyp_mode())
2520 kvm_mode = KVM_MODE_PROTECTED;
2522 pr_warn_once("Protected KVM not available with VHE\n");
2527 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2528 kvm_mode = KVM_MODE_DEFAULT;
2532 if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2533 kvm_mode = KVM_MODE_NV;
2539 early_param("kvm-arm.mode", early_kvm_mode_cfg);
2541 enum kvm_mode kvm_get_mode(void)
2546 module_init(kvm_arm_init);