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_pkvm.h>
40 #include <asm/kvm_emulate.h>
41 #include <asm/sections.h>
43 #include <kvm/arm_hypercalls.h>
44 #include <kvm/arm_pmu.h>
45 #include <kvm/arm_psci.h>
47 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
49 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
51 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
52 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
54 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
56 static bool vgic_present, kvm_arm_initialised;
58 static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
59 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
61 bool is_kvm_arm_initialised(void)
63 return kvm_arm_initialised;
66 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
68 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
71 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
72 struct kvm_enable_cap *cap)
81 case KVM_CAP_ARM_NISV_TO_USER:
83 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
87 mutex_lock(&kvm->lock);
88 if (!system_supports_mte() || kvm->created_vcpus) {
92 set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
94 mutex_unlock(&kvm->lock);
96 case KVM_CAP_ARM_SYSTEM_SUSPEND:
98 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
100 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
101 new_cap = cap->args[0];
103 mutex_lock(&kvm->slots_lock);
105 * To keep things simple, allow changing the chunk
106 * size only when no memory slots have been created.
108 if (!kvm_are_all_memslots_empty(kvm)) {
110 } else if (new_cap && !kvm_is_block_size_supported(new_cap)) {
114 kvm->arch.mmu.split_page_chunk_size = new_cap;
116 mutex_unlock(&kvm->slots_lock);
126 static int kvm_arm_default_max_vcpus(void)
128 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
132 * kvm_arch_init_vm - initializes a VM data structure
133 * @kvm: pointer to the KVM struct
135 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
139 mutex_init(&kvm->arch.config_lock);
141 #ifdef CONFIG_LOCKDEP
142 /* Clue in lockdep that the config_lock must be taken inside kvm->lock */
143 mutex_lock(&kvm->lock);
144 mutex_lock(&kvm->arch.config_lock);
145 mutex_unlock(&kvm->arch.config_lock);
146 mutex_unlock(&kvm->lock);
149 ret = kvm_share_hyp(kvm, kvm + 1);
153 ret = pkvm_init_host_vm(kvm);
155 goto err_unshare_kvm;
157 if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
159 goto err_unshare_kvm;
161 cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
163 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
165 goto err_free_cpumask;
167 kvm_vgic_early_init(kvm);
169 kvm_timer_init_vm(kvm);
171 /* The maximum number of VCPUs is limited by the host's GIC model */
172 kvm->max_vcpus = kvm_arm_default_max_vcpus();
174 kvm_arm_init_hypercalls(kvm);
176 bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
181 free_cpumask_var(kvm->arch.supported_cpus);
183 kvm_unshare_hyp(kvm, kvm + 1);
187 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
189 return VM_FAULT_SIGBUS;
194 * kvm_arch_destroy_vm - destroy the VM data structure
195 * @kvm: pointer to the KVM struct
197 void kvm_arch_destroy_vm(struct kvm *kvm)
199 bitmap_free(kvm->arch.pmu_filter);
200 free_cpumask_var(kvm->arch.supported_cpus);
202 kvm_vgic_destroy(kvm);
204 if (is_protected_kvm_enabled())
205 pkvm_destroy_hyp_vm(kvm);
207 kvm_destroy_vcpus(kvm);
209 kvm_unshare_hyp(kvm, kvm + 1);
211 kvm_arm_teardown_hypercalls(kvm);
214 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
218 case KVM_CAP_IRQCHIP:
221 case KVM_CAP_IOEVENTFD:
222 case KVM_CAP_DEVICE_CTRL:
223 case KVM_CAP_USER_MEMORY:
224 case KVM_CAP_SYNC_MMU:
225 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
226 case KVM_CAP_ONE_REG:
227 case KVM_CAP_ARM_PSCI:
228 case KVM_CAP_ARM_PSCI_0_2:
229 case KVM_CAP_READONLY_MEM:
230 case KVM_CAP_MP_STATE:
231 case KVM_CAP_IMMEDIATE_EXIT:
232 case KVM_CAP_VCPU_EVENTS:
233 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
234 case KVM_CAP_ARM_NISV_TO_USER:
235 case KVM_CAP_ARM_INJECT_EXT_DABT:
236 case KVM_CAP_SET_GUEST_DEBUG:
237 case KVM_CAP_VCPU_ATTRIBUTES:
238 case KVM_CAP_PTP_KVM:
239 case KVM_CAP_ARM_SYSTEM_SUSPEND:
240 case KVM_CAP_IRQFD_RESAMPLE:
241 case KVM_CAP_COUNTER_OFFSET:
244 case KVM_CAP_SET_GUEST_DEBUG2:
245 return KVM_GUESTDBG_VALID_MASK;
246 case KVM_CAP_ARM_SET_DEVICE_ADDR:
249 case KVM_CAP_NR_VCPUS:
251 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
252 * architectures, as it does not always bound it to
253 * KVM_CAP_MAX_VCPUS. It should not matter much because
254 * this is just an advisory value.
256 r = min_t(unsigned int, num_online_cpus(),
257 kvm_arm_default_max_vcpus());
259 case KVM_CAP_MAX_VCPUS:
260 case KVM_CAP_MAX_VCPU_ID:
264 r = kvm_arm_default_max_vcpus();
266 case KVM_CAP_MSI_DEVID:
270 r = kvm->arch.vgic.msis_require_devid;
272 case KVM_CAP_ARM_USER_IRQ:
274 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
275 * (bump this number if adding more devices)
279 case KVM_CAP_ARM_MTE:
280 r = system_supports_mte();
282 case KVM_CAP_STEAL_TIME:
283 r = kvm_arm_pvtime_supported();
285 case KVM_CAP_ARM_EL1_32BIT:
286 r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
288 case KVM_CAP_GUEST_DEBUG_HW_BPS:
291 case KVM_CAP_GUEST_DEBUG_HW_WPS:
294 case KVM_CAP_ARM_PMU_V3:
295 r = kvm_arm_support_pmu_v3();
297 case KVM_CAP_ARM_INJECT_SERROR_ESR:
298 r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
300 case KVM_CAP_ARM_VM_IPA_SIZE:
301 r = get_kvm_ipa_limit();
303 case KVM_CAP_ARM_SVE:
304 r = system_supports_sve();
306 case KVM_CAP_ARM_PTRAUTH_ADDRESS:
307 case KVM_CAP_ARM_PTRAUTH_GENERIC:
308 r = system_has_full_ptr_auth();
310 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
312 r = kvm->arch.mmu.split_page_chunk_size;
314 r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
316 case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
317 r = kvm_supported_block_sizes();
326 long kvm_arch_dev_ioctl(struct file *filp,
327 unsigned int ioctl, unsigned long arg)
332 struct kvm *kvm_arch_alloc_vm(void)
334 size_t sz = sizeof(struct kvm);
337 return kzalloc(sz, GFP_KERNEL_ACCOUNT);
339 return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
342 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
344 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
347 if (id >= kvm->max_vcpus)
353 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
357 spin_lock_init(&vcpu->arch.mp_state_lock);
359 #ifdef CONFIG_LOCKDEP
360 /* Inform lockdep that the config_lock is acquired after vcpu->mutex */
361 mutex_lock(&vcpu->mutex);
362 mutex_lock(&vcpu->kvm->arch.config_lock);
363 mutex_unlock(&vcpu->kvm->arch.config_lock);
364 mutex_unlock(&vcpu->mutex);
367 /* Force users to call KVM_ARM_VCPU_INIT */
368 vcpu->arch.target = -1;
369 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
371 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
374 * Default value for the FP state, will be overloaded at load
375 * time if we support FP (pretty likely)
377 vcpu->arch.fp_state = FP_STATE_FREE;
379 /* Set up the timer */
380 kvm_timer_vcpu_init(vcpu);
382 kvm_pmu_vcpu_init(vcpu);
384 kvm_arm_reset_debug_ptr(vcpu);
386 kvm_arm_pvtime_vcpu_init(&vcpu->arch);
388 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
390 err = kvm_vgic_vcpu_init(vcpu);
394 return kvm_share_hyp(vcpu, vcpu + 1);
397 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
401 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
403 if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
404 static_branch_dec(&userspace_irqchip_in_use);
406 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
407 kvm_timer_vcpu_terminate(vcpu);
408 kvm_pmu_vcpu_destroy(vcpu);
410 kvm_arm_vcpu_destroy(vcpu);
413 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
418 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
423 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
425 struct kvm_s2_mmu *mmu;
428 mmu = vcpu->arch.hw_mmu;
429 last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
432 * We guarantee that both TLBs and I-cache are private to each
433 * vcpu. If detecting that a vcpu from the same VM has
434 * previously run on the same physical CPU, call into the
435 * hypervisor code to nuke the relevant contexts.
437 * We might get preempted before the vCPU actually runs, but
438 * over-invalidation doesn't affect correctness.
440 if (*last_ran != vcpu->vcpu_id) {
441 kvm_call_hyp(__kvm_flush_cpu_context, mmu);
442 *last_ran = vcpu->vcpu_id;
448 kvm_timer_vcpu_load(vcpu);
450 kvm_vcpu_load_sysregs_vhe(vcpu);
451 kvm_arch_vcpu_load_fp(vcpu);
452 kvm_vcpu_pmu_restore_guest(vcpu);
453 if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
454 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
456 if (single_task_running())
457 vcpu_clear_wfx_traps(vcpu);
459 vcpu_set_wfx_traps(vcpu);
461 if (vcpu_has_ptrauth(vcpu))
462 vcpu_ptrauth_disable(vcpu);
463 kvm_arch_vcpu_load_debug_state_flags(vcpu);
465 if (!cpumask_test_cpu(smp_processor_id(), vcpu->kvm->arch.supported_cpus))
466 vcpu_set_on_unsupported_cpu(vcpu);
469 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
471 kvm_arch_vcpu_put_debug_state_flags(vcpu);
472 kvm_arch_vcpu_put_fp(vcpu);
474 kvm_vcpu_put_sysregs_vhe(vcpu);
475 kvm_timer_vcpu_put(vcpu);
477 kvm_vcpu_pmu_restore_host(vcpu);
478 kvm_arm_vmid_clear_active();
480 vcpu_clear_on_unsupported_cpu(vcpu);
484 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
486 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
487 kvm_make_request(KVM_REQ_SLEEP, vcpu);
491 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
493 spin_lock(&vcpu->arch.mp_state_lock);
494 __kvm_arm_vcpu_power_off(vcpu);
495 spin_unlock(&vcpu->arch.mp_state_lock);
498 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
500 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
503 static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
505 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
506 kvm_make_request(KVM_REQ_SUSPEND, vcpu);
510 static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
512 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
515 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
516 struct kvm_mp_state *mp_state)
518 *mp_state = READ_ONCE(vcpu->arch.mp_state);
523 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
524 struct kvm_mp_state *mp_state)
528 spin_lock(&vcpu->arch.mp_state_lock);
530 switch (mp_state->mp_state) {
531 case KVM_MP_STATE_RUNNABLE:
532 WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
534 case KVM_MP_STATE_STOPPED:
535 __kvm_arm_vcpu_power_off(vcpu);
537 case KVM_MP_STATE_SUSPENDED:
538 kvm_arm_vcpu_suspend(vcpu);
544 spin_unlock(&vcpu->arch.mp_state_lock);
550 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
551 * @v: The VCPU pointer
553 * If the guest CPU is not waiting for interrupts or an interrupt line is
554 * asserted, the CPU is by definition runnable.
556 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
558 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
559 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
560 && !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
563 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
565 return vcpu_mode_priv(vcpu);
568 #ifdef CONFIG_GUEST_PERF_EVENTS
569 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
571 return *vcpu_pc(vcpu);
575 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
577 return vcpu->arch.target >= 0;
581 * Handle both the initialisation that is being done when the vcpu is
582 * run for the first time, as well as the updates that must be
583 * performed each time we get a new thread dealing with this vcpu.
585 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
587 struct kvm *kvm = vcpu->kvm;
590 if (!kvm_vcpu_initialized(vcpu))
593 if (!kvm_arm_vcpu_is_finalized(vcpu))
596 ret = kvm_arch_vcpu_run_map_fp(vcpu);
600 if (likely(vcpu_has_run_once(vcpu)))
603 kvm_arm_vcpu_init_debug(vcpu);
605 if (likely(irqchip_in_kernel(kvm))) {
607 * Map the VGIC hardware resources before running a vcpu the
608 * first time on this VM.
610 ret = kvm_vgic_map_resources(kvm);
615 ret = kvm_timer_enable(vcpu);
619 ret = kvm_arm_pmu_v3_enable(vcpu);
623 if (is_protected_kvm_enabled()) {
624 ret = pkvm_create_hyp_vm(kvm);
629 if (!irqchip_in_kernel(kvm)) {
631 * Tell the rest of the code that there are userspace irqchip
634 static_branch_inc(&userspace_irqchip_in_use);
638 * Initialize traps for protected VMs.
639 * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
640 * the code is in place for first run initialization at EL2.
642 if (kvm_vm_is_protected(kvm))
643 kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
645 mutex_lock(&kvm->arch.config_lock);
646 set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
647 mutex_unlock(&kvm->arch.config_lock);
652 bool kvm_arch_intc_initialized(struct kvm *kvm)
654 return vgic_initialized(kvm);
657 void kvm_arm_halt_guest(struct kvm *kvm)
660 struct kvm_vcpu *vcpu;
662 kvm_for_each_vcpu(i, vcpu, kvm)
663 vcpu->arch.pause = true;
664 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
667 void kvm_arm_resume_guest(struct kvm *kvm)
670 struct kvm_vcpu *vcpu;
672 kvm_for_each_vcpu(i, vcpu, kvm) {
673 vcpu->arch.pause = false;
674 __kvm_vcpu_wake_up(vcpu);
678 static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
680 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
682 rcuwait_wait_event(wait,
683 (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
686 if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
687 /* Awaken to handle a signal, request we sleep again later. */
688 kvm_make_request(KVM_REQ_SLEEP, vcpu);
692 * Make sure we will observe a potential reset request if we've
693 * observed a change to the power state. Pairs with the smp_wmb() in
694 * kvm_psci_vcpu_on().
700 * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
701 * @vcpu: The VCPU pointer
703 * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
704 * the vCPU is runnable. The vCPU may or may not be scheduled out, depending
705 * on when a wake event arrives, e.g. there may already be a pending wake event.
707 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
710 * Sync back the state of the GIC CPU interface so that we have
711 * the latest PMR and group enables. This ensures that
712 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
713 * we have pending interrupts, e.g. when determining if the
716 * For the same reason, we want to tell GICv4 that we need
717 * doorbells to be signalled, should an interrupt become pending.
720 kvm_vgic_vmcr_sync(vcpu);
721 vgic_v4_put(vcpu, true);
725 vcpu_clear_flag(vcpu, IN_WFIT);
732 static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
734 if (!kvm_arm_vcpu_suspended(vcpu))
740 * The suspend state is sticky; we do not leave it until userspace
741 * explicitly marks the vCPU as runnable. Request that we suspend again
744 kvm_make_request(KVM_REQ_SUSPEND, vcpu);
747 * Check to make sure the vCPU is actually runnable. If so, exit to
748 * userspace informing it of the wakeup condition.
750 if (kvm_arch_vcpu_runnable(vcpu)) {
751 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
752 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
753 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
758 * Otherwise, we were unblocked to process a different event, such as a
759 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
766 * check_vcpu_requests - check and handle pending vCPU requests
767 * @vcpu: the VCPU pointer
769 * Return: 1 if we should enter the guest
770 * 0 if we should exit to userspace
771 * < 0 if we should exit to userspace, where the return value indicates
774 static int check_vcpu_requests(struct kvm_vcpu *vcpu)
776 if (kvm_request_pending(vcpu)) {
777 if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
778 kvm_vcpu_sleep(vcpu);
780 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
781 kvm_reset_vcpu(vcpu);
784 * Clear IRQ_PENDING requests that were made to guarantee
785 * that a VCPU sees new virtual interrupts.
787 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
789 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
790 kvm_update_stolen_time(vcpu);
792 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
793 /* The distributor enable bits were changed */
795 vgic_v4_put(vcpu, false);
800 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
801 kvm_pmu_handle_pmcr(vcpu,
802 __vcpu_sys_reg(vcpu, PMCR_EL0));
804 if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
805 return kvm_vcpu_suspend(vcpu);
807 if (kvm_dirty_ring_check_request(vcpu))
814 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
816 if (likely(!vcpu_mode_is_32bit(vcpu)))
819 return !kvm_supports_32bit_el0();
823 * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
824 * @vcpu: The VCPU pointer
825 * @ret: Pointer to write optional return code
827 * Returns: true if the VCPU needs to return to a preemptible + interruptible
828 * and skip guest entry.
830 * This function disambiguates between two different types of exits: exits to a
831 * preemptible + interruptible kernel context and exits to userspace. For an
832 * exit to userspace, this function will write the return code to ret and return
833 * true. For an exit to preemptible + interruptible kernel context (i.e. check
834 * for pending work and re-enter), return true without writing to ret.
836 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
838 struct kvm_run *run = vcpu->run;
841 * If we're using a userspace irqchip, then check if we need
842 * to tell a userspace irqchip about timer or PMU level
843 * changes and if so, exit to userspace (the actual level
844 * state gets updated in kvm_timer_update_run and
845 * kvm_pmu_update_run below).
847 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
848 if (kvm_timer_should_notify_user(vcpu) ||
849 kvm_pmu_should_notify_user(vcpu)) {
851 run->exit_reason = KVM_EXIT_INTR;
856 if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
857 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
858 run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
859 run->fail_entry.cpu = smp_processor_id();
864 return kvm_request_pending(vcpu) ||
865 xfer_to_guest_mode_work_pending();
869 * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
870 * the vCPU is running.
872 * This must be noinstr as instrumentation may make use of RCU, and this is not
873 * safe during the EQS.
875 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
879 guest_state_enter_irqoff();
880 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
881 guest_state_exit_irqoff();
887 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
888 * @vcpu: The VCPU pointer
890 * This function is called through the VCPU_RUN ioctl called from user space. It
891 * will execute VM code in a loop until the time slice for the process is used
892 * or some emulation is needed from user space in which case the function will
893 * return with return value 0 and with the kvm_run structure filled in with the
894 * required data for the requested emulation.
896 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
898 struct kvm_run *run = vcpu->run;
901 if (run->exit_reason == KVM_EXIT_MMIO) {
902 ret = kvm_handle_mmio_return(vcpu);
909 if (run->immediate_exit) {
914 kvm_sigset_activate(vcpu);
917 run->exit_reason = KVM_EXIT_UNKNOWN;
921 * Check conditions before entering the guest
923 ret = xfer_to_guest_mode_handle_work(vcpu);
928 ret = check_vcpu_requests(vcpu);
931 * Preparing the interrupts to be injected also
932 * involves poking the GIC, which must be done in a
933 * non-preemptible context.
938 * The VMID allocator only tracks active VMIDs per
939 * physical CPU, and therefore the VMID allocated may not be
940 * preserved on VMID roll-over if the task was preempted,
941 * making a thread's VMID inactive. So we need to call
942 * kvm_arm_vmid_update() in non-premptible context.
944 kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
946 kvm_pmu_flush_hwstate(vcpu);
950 kvm_vgic_flush_hwstate(vcpu);
952 kvm_pmu_update_vcpu_events(vcpu);
955 * Ensure we set mode to IN_GUEST_MODE after we disable
956 * interrupts and before the final VCPU requests check.
957 * See the comment in kvm_vcpu_exiting_guest_mode() and
958 * Documentation/virt/kvm/vcpu-requests.rst
960 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
962 if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
963 vcpu->mode = OUTSIDE_GUEST_MODE;
964 isb(); /* Ensure work in x_flush_hwstate is committed */
965 kvm_pmu_sync_hwstate(vcpu);
966 if (static_branch_unlikely(&userspace_irqchip_in_use))
967 kvm_timer_sync_user(vcpu);
968 kvm_vgic_sync_hwstate(vcpu);
974 kvm_arm_setup_debug(vcpu);
975 kvm_arch_vcpu_ctxflush_fp(vcpu);
977 /**************************************************************
980 trace_kvm_entry(*vcpu_pc(vcpu));
981 guest_timing_enter_irqoff();
983 ret = kvm_arm_vcpu_enter_exit(vcpu);
985 vcpu->mode = OUTSIDE_GUEST_MODE;
989 *************************************************************/
991 kvm_arm_clear_debug(vcpu);
994 * We must sync the PMU state before the vgic state so
995 * that the vgic can properly sample the updated state of the
998 kvm_pmu_sync_hwstate(vcpu);
1001 * Sync the vgic state before syncing the timer state because
1002 * the timer code needs to know if the virtual timer
1003 * interrupts are active.
1005 kvm_vgic_sync_hwstate(vcpu);
1008 * Sync the timer hardware state before enabling interrupts as
1009 * we don't want vtimer interrupts to race with syncing the
1010 * timer virtual interrupt state.
1012 if (static_branch_unlikely(&userspace_irqchip_in_use))
1013 kvm_timer_sync_user(vcpu);
1015 kvm_arch_vcpu_ctxsync_fp(vcpu);
1018 * We must ensure that any pending interrupts are taken before
1019 * we exit guest timing so that timer ticks are accounted as
1020 * guest time. Transiently unmask interrupts so that any
1021 * pending interrupts are taken.
1023 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
1024 * context synchronization event) is necessary to ensure that
1025 * pending interrupts are taken.
1027 if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
1030 local_irq_disable();
1033 guest_timing_exit_irqoff();
1037 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
1039 /* Exit types that need handling before we can be preempted */
1040 handle_exit_early(vcpu, ret);
1045 * The ARMv8 architecture doesn't give the hypervisor
1046 * a mechanism to prevent a guest from dropping to AArch32 EL0
1047 * if implemented by the CPU. If we spot the guest in such
1048 * state and that we decided it wasn't supposed to do so (like
1049 * with the asymmetric AArch32 case), return to userspace with
1052 if (vcpu_mode_is_bad_32bit(vcpu)) {
1054 * As we have caught the guest red-handed, decide that
1055 * it isn't fit for purpose anymore by making the vcpu
1056 * invalid. The VMM can try and fix it by issuing a
1057 * KVM_ARM_VCPU_INIT if it really wants to.
1059 vcpu->arch.target = -1;
1060 ret = ARM_EXCEPTION_IL;
1063 ret = handle_exit(vcpu, ret);
1066 /* Tell userspace about in-kernel device output levels */
1067 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
1068 kvm_timer_update_run(vcpu);
1069 kvm_pmu_update_run(vcpu);
1072 kvm_sigset_deactivate(vcpu);
1076 * In the unlikely event that we are returning to userspace
1077 * with pending exceptions or PC adjustment, commit these
1078 * adjustments in order to give userspace a consistent view of
1079 * the vcpu state. Note that this relies on __kvm_adjust_pc()
1080 * being preempt-safe on VHE.
1082 if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1083 vcpu_get_flag(vcpu, INCREMENT_PC)))
1084 kvm_call_hyp(__kvm_adjust_pc, vcpu);
1090 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
1096 if (number == KVM_ARM_IRQ_CPU_IRQ)
1097 bit_index = __ffs(HCR_VI);
1098 else /* KVM_ARM_IRQ_CPU_FIQ */
1099 bit_index = __ffs(HCR_VF);
1101 hcr = vcpu_hcr(vcpu);
1103 set = test_and_set_bit(bit_index, hcr);
1105 set = test_and_clear_bit(bit_index, hcr);
1108 * If we didn't change anything, no need to wake up or kick other CPUs
1114 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
1115 * trigger a world-switch round on the running physical CPU to set the
1116 * virtual IRQ/FIQ fields in the HCR appropriately.
1118 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
1119 kvm_vcpu_kick(vcpu);
1124 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
1127 u32 irq = irq_level->irq;
1128 unsigned int irq_type, vcpu_idx, irq_num;
1129 int nrcpus = atomic_read(&kvm->online_vcpus);
1130 struct kvm_vcpu *vcpu = NULL;
1131 bool level = irq_level->level;
1133 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
1134 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
1135 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
1136 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
1138 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
1141 case KVM_ARM_IRQ_TYPE_CPU:
1142 if (irqchip_in_kernel(kvm))
1145 if (vcpu_idx >= nrcpus)
1148 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1152 if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
1155 return vcpu_interrupt_line(vcpu, irq_num, level);
1156 case KVM_ARM_IRQ_TYPE_PPI:
1157 if (!irqchip_in_kernel(kvm))
1160 if (vcpu_idx >= nrcpus)
1163 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1167 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
1170 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
1171 case KVM_ARM_IRQ_TYPE_SPI:
1172 if (!irqchip_in_kernel(kvm))
1175 if (irq_num < VGIC_NR_PRIVATE_IRQS)
1178 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
1184 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
1185 const struct kvm_vcpu_init *init)
1187 unsigned long features = init->features[0];
1190 if (features & ~KVM_VCPU_VALID_FEATURES)
1193 for (i = 1; i < ARRAY_SIZE(init->features); i++) {
1194 if (init->features[i])
1198 if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
1201 if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1))
1204 /* MTE is incompatible with AArch32 */
1205 if (kvm_has_mte(vcpu->kvm))
1208 /* NV is incompatible with AArch32 */
1209 if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
1215 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
1216 const struct kvm_vcpu_init *init)
1218 unsigned long features = init->features[0];
1220 return !bitmap_equal(vcpu->arch.features, &features, KVM_VCPU_MAX_FEATURES) ||
1221 vcpu->arch.target != init->target;
1224 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1225 const struct kvm_vcpu_init *init)
1227 unsigned long features = init->features[0];
1228 struct kvm *kvm = vcpu->kvm;
1231 mutex_lock(&kvm->arch.config_lock);
1233 if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
1234 !bitmap_equal(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES))
1237 vcpu->arch.target = init->target;
1238 bitmap_copy(vcpu->arch.features, &features, KVM_VCPU_MAX_FEATURES);
1240 /* Now we know what it is, we can reset it. */
1241 ret = kvm_reset_vcpu(vcpu);
1243 vcpu->arch.target = -1;
1244 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
1248 bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
1249 set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
1252 mutex_unlock(&kvm->arch.config_lock);
1256 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1257 const struct kvm_vcpu_init *init)
1261 if (init->target != kvm_target_cpu())
1264 ret = kvm_vcpu_init_check_features(vcpu, init);
1268 if (vcpu->arch.target == -1)
1269 return __kvm_vcpu_set_target(vcpu, init);
1271 if (kvm_vcpu_init_changed(vcpu, init))
1274 return kvm_reset_vcpu(vcpu);
1277 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
1278 struct kvm_vcpu_init *init)
1280 bool power_off = false;
1284 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
1285 * reflecting it in the finalized feature set, thus limiting its scope
1286 * to a single KVM_ARM_VCPU_INIT call.
1288 if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
1289 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
1293 ret = kvm_vcpu_set_target(vcpu, init);
1298 * Ensure a rebooted VM will fault in RAM pages and detect if the
1299 * guest MMU is turned off and flush the caches as needed.
1301 * S2FWB enforces all memory accesses to RAM being cacheable,
1302 * ensuring that the data side is always coherent. We still
1303 * need to invalidate the I-cache though, as FWB does *not*
1304 * imply CTR_EL0.DIC.
1306 if (vcpu_has_run_once(vcpu)) {
1307 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
1308 stage2_unmap_vm(vcpu->kvm);
1310 icache_inval_all_pou();
1313 vcpu_reset_hcr(vcpu);
1314 vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu);
1317 * Handle the "start in power-off" case.
1319 spin_lock(&vcpu->arch.mp_state_lock);
1322 __kvm_arm_vcpu_power_off(vcpu);
1324 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
1326 spin_unlock(&vcpu->arch.mp_state_lock);
1331 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
1332 struct kvm_device_attr *attr)
1336 switch (attr->group) {
1338 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
1345 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
1346 struct kvm_device_attr *attr)
1350 switch (attr->group) {
1352 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
1359 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
1360 struct kvm_device_attr *attr)
1364 switch (attr->group) {
1366 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
1373 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
1374 struct kvm_vcpu_events *events)
1376 memset(events, 0, sizeof(*events));
1378 return __kvm_arm_vcpu_get_events(vcpu, events);
1381 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
1382 struct kvm_vcpu_events *events)
1386 /* check whether the reserved field is zero */
1387 for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
1388 if (events->reserved[i])
1391 /* check whether the pad field is zero */
1392 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
1393 if (events->exception.pad[i])
1396 return __kvm_arm_vcpu_set_events(vcpu, events);
1399 long kvm_arch_vcpu_ioctl(struct file *filp,
1400 unsigned int ioctl, unsigned long arg)
1402 struct kvm_vcpu *vcpu = filp->private_data;
1403 void __user *argp = (void __user *)arg;
1404 struct kvm_device_attr attr;
1408 case KVM_ARM_VCPU_INIT: {
1409 struct kvm_vcpu_init init;
1412 if (copy_from_user(&init, argp, sizeof(init)))
1415 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
1418 case KVM_SET_ONE_REG:
1419 case KVM_GET_ONE_REG: {
1420 struct kvm_one_reg reg;
1423 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1427 if (copy_from_user(®, argp, sizeof(reg)))
1431 * We could owe a reset due to PSCI. Handle the pending reset
1432 * here to ensure userspace register accesses are ordered after
1435 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
1436 kvm_reset_vcpu(vcpu);
1438 if (ioctl == KVM_SET_ONE_REG)
1439 r = kvm_arm_set_reg(vcpu, ®);
1441 r = kvm_arm_get_reg(vcpu, ®);
1444 case KVM_GET_REG_LIST: {
1445 struct kvm_reg_list __user *user_list = argp;
1446 struct kvm_reg_list reg_list;
1450 if (unlikely(!kvm_vcpu_initialized(vcpu)))
1454 if (!kvm_arm_vcpu_is_finalized(vcpu))
1458 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
1461 reg_list.n = kvm_arm_num_regs(vcpu);
1462 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
1467 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
1470 case KVM_SET_DEVICE_ATTR: {
1472 if (copy_from_user(&attr, argp, sizeof(attr)))
1474 r = kvm_arm_vcpu_set_attr(vcpu, &attr);
1477 case KVM_GET_DEVICE_ATTR: {
1479 if (copy_from_user(&attr, argp, sizeof(attr)))
1481 r = kvm_arm_vcpu_get_attr(vcpu, &attr);
1484 case KVM_HAS_DEVICE_ATTR: {
1486 if (copy_from_user(&attr, argp, sizeof(attr)))
1488 r = kvm_arm_vcpu_has_attr(vcpu, &attr);
1491 case KVM_GET_VCPU_EVENTS: {
1492 struct kvm_vcpu_events events;
1494 if (kvm_arm_vcpu_get_events(vcpu, &events))
1497 if (copy_to_user(argp, &events, sizeof(events)))
1502 case KVM_SET_VCPU_EVENTS: {
1503 struct kvm_vcpu_events events;
1505 if (copy_from_user(&events, argp, sizeof(events)))
1508 return kvm_arm_vcpu_set_events(vcpu, &events);
1510 case KVM_ARM_VCPU_FINALIZE: {
1513 if (!kvm_vcpu_initialized(vcpu))
1516 if (get_user(what, (const int __user *)argp))
1519 return kvm_arm_vcpu_finalize(vcpu, what);
1528 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1533 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
1534 const struct kvm_memory_slot *memslot)
1536 kvm_flush_remote_tlbs(kvm);
1539 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
1540 struct kvm_arm_device_addr *dev_addr)
1542 switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
1543 case KVM_ARM_DEVICE_VGIC_V2:
1546 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
1552 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1554 switch (attr->group) {
1555 case KVM_ARM_VM_SMCCC_CTRL:
1556 return kvm_vm_smccc_has_attr(kvm, attr);
1562 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1564 switch (attr->group) {
1565 case KVM_ARM_VM_SMCCC_CTRL:
1566 return kvm_vm_smccc_set_attr(kvm, attr);
1572 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1574 struct kvm *kvm = filp->private_data;
1575 void __user *argp = (void __user *)arg;
1576 struct kvm_device_attr attr;
1579 case KVM_CREATE_IRQCHIP: {
1583 mutex_lock(&kvm->lock);
1584 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
1585 mutex_unlock(&kvm->lock);
1588 case KVM_ARM_SET_DEVICE_ADDR: {
1589 struct kvm_arm_device_addr dev_addr;
1591 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
1593 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
1595 case KVM_ARM_PREFERRED_TARGET: {
1596 struct kvm_vcpu_init init;
1598 kvm_vcpu_preferred_target(&init);
1600 if (copy_to_user(argp, &init, sizeof(init)))
1605 case KVM_ARM_MTE_COPY_TAGS: {
1606 struct kvm_arm_copy_mte_tags copy_tags;
1608 if (copy_from_user(©_tags, argp, sizeof(copy_tags)))
1610 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags);
1612 case KVM_ARM_SET_COUNTER_OFFSET: {
1613 struct kvm_arm_counter_offset offset;
1615 if (copy_from_user(&offset, argp, sizeof(offset)))
1617 return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
1619 case KVM_HAS_DEVICE_ATTR: {
1620 if (copy_from_user(&attr, argp, sizeof(attr)))
1623 return kvm_vm_has_attr(kvm, &attr);
1625 case KVM_SET_DEVICE_ATTR: {
1626 if (copy_from_user(&attr, argp, sizeof(attr)))
1629 return kvm_vm_set_attr(kvm, &attr);
1636 /* unlocks vcpus from @vcpu_lock_idx and smaller */
1637 static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx)
1639 struct kvm_vcpu *tmp_vcpu;
1641 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
1642 tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
1643 mutex_unlock(&tmp_vcpu->mutex);
1647 void unlock_all_vcpus(struct kvm *kvm)
1649 lockdep_assert_held(&kvm->lock);
1651 unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1);
1654 /* Returns true if all vcpus were locked, false otherwise */
1655 bool lock_all_vcpus(struct kvm *kvm)
1657 struct kvm_vcpu *tmp_vcpu;
1660 lockdep_assert_held(&kvm->lock);
1663 * Any time a vcpu is in an ioctl (including running), the
1664 * core KVM code tries to grab the vcpu->mutex.
1666 * By grabbing the vcpu->mutex of all VCPUs we ensure that no
1667 * other VCPUs can fiddle with the state while we access it.
1669 kvm_for_each_vcpu(c, tmp_vcpu, kvm) {
1670 if (!mutex_trylock(&tmp_vcpu->mutex)) {
1671 unlock_vcpus(kvm, c - 1);
1679 static unsigned long nvhe_percpu_size(void)
1681 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
1682 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
1685 static unsigned long nvhe_percpu_order(void)
1687 unsigned long size = nvhe_percpu_size();
1689 return size ? get_order(size) : 0;
1692 /* A lookup table holding the hypervisor VA for each vector slot */
1693 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1695 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1697 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
1700 static int kvm_init_vector_slots(void)
1705 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1706 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1708 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1709 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1711 if (kvm_system_needs_idmapped_vectors() &&
1712 !is_protected_kvm_enabled()) {
1713 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1714 __BP_HARDEN_HYP_VECS_SZ, &base);
1719 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1720 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
1724 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
1726 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1730 * Calculate the raw per-cpu offset without a translation from the
1731 * kernel's mapping to the linear mapping, and store it in tpidr_el2
1732 * so that we can use adr_l to access per-cpu variables in EL2.
1733 * Also drop the KASAN tag which gets in the way...
1735 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
1736 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
1738 params->mair_el2 = read_sysreg(mair_el1);
1740 tcr = read_sysreg(tcr_el1);
1741 if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
1742 tcr |= TCR_EPD1_MASK;
1744 tcr &= TCR_EL2_MASK;
1745 tcr |= TCR_EL2_RES1;
1747 tcr &= ~TCR_T0SZ_MASK;
1748 tcr |= TCR_T0SZ(hyp_va_bits);
1749 params->tcr_el2 = tcr;
1751 params->pgd_pa = kvm_mmu_get_httbr();
1752 if (is_protected_kvm_enabled())
1753 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1755 params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1756 if (cpus_have_final_cap(ARM64_KVM_HVHE))
1757 params->hcr_el2 |= HCR_E2H;
1758 params->vttbr = params->vtcr = 0;
1761 * Flush the init params from the data cache because the struct will
1762 * be read while the MMU is off.
1764 kvm_flush_dcache_to_poc(params, sizeof(*params));
1767 static void hyp_install_host_vector(void)
1769 struct kvm_nvhe_init_params *params;
1770 struct arm_smccc_res res;
1772 /* Switch from the HYP stub to our own HYP init vector */
1773 __hyp_set_vectors(kvm_get_idmap_vector());
1776 * Call initialization code, and switch to the full blown HYP code.
1777 * If the cpucaps haven't been finalized yet, something has gone very
1778 * wrong, and hyp will crash and burn when it uses any
1779 * cpus_have_const_cap() wrapper.
1781 BUG_ON(!system_capabilities_finalized());
1782 params = this_cpu_ptr_nvhe_sym(kvm_init_params);
1783 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
1784 WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1787 static void cpu_init_hyp_mode(void)
1789 hyp_install_host_vector();
1792 * Disabling SSBD on a non-VHE system requires us to enable SSBS
1795 if (this_cpu_has_cap(ARM64_SSBS) &&
1796 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
1797 kvm_call_hyp_nvhe(__kvm_enable_ssbs);
1801 static void cpu_hyp_reset(void)
1803 if (!is_kernel_in_hyp_mode())
1804 __hyp_reset_vectors();
1808 * EL2 vectors can be mapped and rerouted in a number of ways,
1809 * depending on the kernel configuration and CPU present:
1811 * - If the CPU is affected by Spectre-v2, the hardening sequence is
1812 * placed in one of the vector slots, which is executed before jumping
1813 * to the real vectors.
1815 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1816 * containing the hardening sequence is mapped next to the idmap page,
1817 * and executed before jumping to the real vectors.
1819 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1820 * empty slot is selected, mapped next to the idmap page, and
1821 * executed before jumping to the real vectors.
1823 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1824 * VHE, as we don't have hypervisor-specific mappings. If the system
1825 * is VHE and yet selects this capability, it will be ignored.
1827 static void cpu_set_hyp_vector(void)
1829 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1830 void *vector = hyp_spectre_vector_selector[data->slot];
1832 if (!is_protected_kvm_enabled())
1833 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1835 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1838 static void cpu_hyp_init_context(void)
1840 kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
1842 if (!is_kernel_in_hyp_mode())
1843 cpu_init_hyp_mode();
1846 static void cpu_hyp_init_features(void)
1848 cpu_set_hyp_vector();
1849 kvm_arm_init_debug();
1851 if (is_kernel_in_hyp_mode())
1852 kvm_timer_init_vhe();
1855 kvm_vgic_init_cpu_hardware();
1858 static void cpu_hyp_reinit(void)
1861 cpu_hyp_init_context();
1862 cpu_hyp_init_features();
1865 static void _kvm_arch_hardware_enable(void *discard)
1867 if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
1869 __this_cpu_write(kvm_arm_hardware_enabled, 1);
1873 int kvm_arch_hardware_enable(void)
1875 int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
1877 _kvm_arch_hardware_enable(NULL);
1887 static void _kvm_arch_hardware_disable(void *discard)
1889 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1891 __this_cpu_write(kvm_arm_hardware_enabled, 0);
1895 void kvm_arch_hardware_disable(void)
1897 if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1898 kvm_timer_cpu_down();
1899 kvm_vgic_cpu_down();
1902 if (!is_protected_kvm_enabled())
1903 _kvm_arch_hardware_disable(NULL);
1906 #ifdef CONFIG_CPU_PM
1907 static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1912 * kvm_arm_hardware_enabled is left with its old value over
1913 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1918 if (__this_cpu_read(kvm_arm_hardware_enabled))
1920 * don't update kvm_arm_hardware_enabled here
1921 * so that the hardware will be re-enabled
1922 * when we resume. See below.
1927 case CPU_PM_ENTER_FAILED:
1929 if (__this_cpu_read(kvm_arm_hardware_enabled))
1930 /* The hardware was enabled before suspend. */
1940 static struct notifier_block hyp_init_cpu_pm_nb = {
1941 .notifier_call = hyp_init_cpu_pm_notifier,
1944 static void __init hyp_cpu_pm_init(void)
1946 if (!is_protected_kvm_enabled())
1947 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
1949 static void __init hyp_cpu_pm_exit(void)
1951 if (!is_protected_kvm_enabled())
1952 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
1955 static inline void __init hyp_cpu_pm_init(void)
1958 static inline void __init hyp_cpu_pm_exit(void)
1963 static void __init init_cpu_logical_map(void)
1968 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
1969 * Only copy the set of online CPUs whose features have been checked
1970 * against the finalized system capabilities. The hypervisor will not
1971 * allow any other CPUs from the `possible` set to boot.
1973 for_each_online_cpu(cpu)
1974 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
1977 #define init_psci_0_1_impl_state(config, what) \
1978 config.psci_0_1_ ## what ## _implemented = psci_ops.what
1980 static bool __init init_psci_relay(void)
1983 * If PSCI has not been initialized, protected KVM cannot install
1984 * itself on newly booted CPUs.
1986 if (!psci_ops.get_version) {
1987 kvm_err("Cannot initialize protected mode without PSCI\n");
1991 kvm_host_psci_config.version = psci_ops.get_version();
1992 kvm_host_psci_config.smccc_version = arm_smccc_get_version();
1994 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
1995 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
1996 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
1997 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
1998 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
1999 init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
2004 static int __init init_subsystems(void)
2009 * Enable hardware so that subsystem initialisation can access EL2.
2011 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
2014 * Register CPU lower-power notifier
2019 * Init HYP view of VGIC
2021 err = kvm_vgic_hyp_init();
2024 vgic_present = true;
2028 vgic_present = false;
2036 * Init HYP architected timer support
2038 err = kvm_timer_hyp_init(vgic_present);
2042 kvm_register_perf_callbacks(NULL);
2048 if (err || !is_protected_kvm_enabled())
2049 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
2054 static void __init teardown_subsystems(void)
2056 kvm_unregister_perf_callbacks();
2060 static void __init teardown_hyp_mode(void)
2065 for_each_possible_cpu(cpu) {
2066 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
2067 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
2071 static int __init do_pkvm_init(u32 hyp_va_bits)
2073 void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
2077 cpu_hyp_init_context();
2078 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
2079 num_possible_cpus(), kern_hyp_va(per_cpu_base),
2081 cpu_hyp_init_features();
2084 * The stub hypercalls are now disabled, so set our local flag to
2085 * prevent a later re-init attempt in kvm_arch_hardware_enable().
2087 __this_cpu_write(kvm_arm_hardware_enabled, 1);
2093 static u64 get_hyp_id_aa64pfr0_el1(void)
2096 * Track whether the system isn't affected by spectre/meltdown in the
2097 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
2098 * Although this is per-CPU, we make it global for simplicity, e.g., not
2099 * to have to worry about vcpu migration.
2101 * Unlike for non-protected VMs, userspace cannot override this for
2104 u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
2106 val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
2107 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
2109 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
2110 arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
2111 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
2112 arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
2117 static void kvm_hyp_init_symbols(void)
2119 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
2120 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
2121 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
2122 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
2123 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
2124 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
2125 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
2126 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
2127 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
2128 kvm_nvhe_sym(__icache_flags) = __icache_flags;
2129 kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
2132 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
2134 void *addr = phys_to_virt(hyp_mem_base);
2137 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
2141 ret = do_pkvm_init(hyp_va_bits);
2150 static void pkvm_hyp_init_ptrauth(void)
2152 struct kvm_cpu_context *hyp_ctxt;
2155 for_each_possible_cpu(cpu) {
2156 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
2157 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
2158 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
2159 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
2160 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
2161 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
2162 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
2163 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
2164 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
2165 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
2166 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
2170 /* Inits Hyp-mode on all online CPUs */
2171 static int __init init_hyp_mode(void)
2178 * The protected Hyp-mode cannot be initialized if the memory pool
2179 * allocation has failed.
2181 if (is_protected_kvm_enabled() && !hyp_mem_base)
2185 * Allocate Hyp PGD and setup Hyp identity mapping
2187 err = kvm_mmu_init(&hyp_va_bits);
2192 * Allocate stack pages for Hypervisor-mode
2194 for_each_possible_cpu(cpu) {
2195 unsigned long stack_page;
2197 stack_page = __get_free_page(GFP_KERNEL);
2203 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
2207 * Allocate and initialize pages for Hypervisor-mode percpu regions.
2209 for_each_possible_cpu(cpu) {
2213 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
2219 page_addr = page_address(page);
2220 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2221 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
2225 * Map the Hyp-code called directly from the host
2227 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
2228 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
2230 kvm_err("Cannot map world-switch code\n");
2234 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
2235 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
2237 kvm_err("Cannot map .hyp.rodata section\n");
2241 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
2242 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
2244 kvm_err("Cannot map rodata section\n");
2249 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2250 * section thanks to an assertion in the linker script. Map it RW and
2251 * the rest of .bss RO.
2253 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2254 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2256 kvm_err("Cannot map hyp bss section: %d\n", err);
2260 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
2261 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
2263 kvm_err("Cannot map bss section\n");
2268 * Map the Hyp stack pages
2270 for_each_possible_cpu(cpu) {
2271 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
2272 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
2273 unsigned long hyp_addr;
2276 * Allocate a contiguous HYP private VA range for the stack
2277 * and guard page. The allocation is also aligned based on
2278 * the order of its size.
2280 err = hyp_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr);
2282 kvm_err("Cannot allocate hyp stack guard page\n");
2287 * Since the stack grows downwards, map the stack to the page
2288 * at the higher address and leave the lower guard page
2291 * Any valid stack address now has the PAGE_SHIFT bit as 1
2292 * and addresses corresponding to the guard page have the
2293 * PAGE_SHIFT bit as 0 - this is used for overflow detection.
2295 err = __create_hyp_mappings(hyp_addr + PAGE_SIZE, PAGE_SIZE,
2296 __pa(stack_page), PAGE_HYP);
2298 kvm_err("Cannot map hyp stack\n");
2303 * Save the stack PA in nvhe_init_params. This will be needed
2304 * to recreate the stack mapping in protected nVHE mode.
2305 * __hyp_pa() won't do the right thing there, since the stack
2306 * has been mapped in the flexible private VA space.
2308 params->stack_pa = __pa(stack_page);
2310 params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE);
2313 for_each_possible_cpu(cpu) {
2314 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
2315 char *percpu_end = percpu_begin + nvhe_percpu_size();
2317 /* Map Hyp percpu pages */
2318 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
2320 kvm_err("Cannot map hyp percpu region\n");
2324 /* Prepare the CPU initialization parameters */
2325 cpu_prepare_hyp_mode(cpu, hyp_va_bits);
2328 kvm_hyp_init_symbols();
2330 if (is_protected_kvm_enabled()) {
2331 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
2332 cpus_have_const_cap(ARM64_HAS_ADDRESS_AUTH))
2333 pkvm_hyp_init_ptrauth();
2335 init_cpu_logical_map();
2337 if (!init_psci_relay()) {
2342 err = kvm_hyp_init_protection(hyp_va_bits);
2344 kvm_err("Failed to init hyp memory protection\n");
2352 teardown_hyp_mode();
2353 kvm_err("error initializing Hyp mode: %d\n", err);
2357 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
2359 struct kvm_vcpu *vcpu;
2362 mpidr &= MPIDR_HWID_BITMASK;
2363 kvm_for_each_vcpu(i, vcpu, kvm) {
2364 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
2370 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2372 return irqchip_in_kernel(kvm);
2375 bool kvm_arch_has_irq_bypass(void)
2380 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
2381 struct irq_bypass_producer *prod)
2383 struct kvm_kernel_irqfd *irqfd =
2384 container_of(cons, struct kvm_kernel_irqfd, consumer);
2386 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
2389 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
2390 struct irq_bypass_producer *prod)
2392 struct kvm_kernel_irqfd *irqfd =
2393 container_of(cons, struct kvm_kernel_irqfd, consumer);
2395 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
2399 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
2401 struct kvm_kernel_irqfd *irqfd =
2402 container_of(cons, struct kvm_kernel_irqfd, consumer);
2404 kvm_arm_halt_guest(irqfd->kvm);
2407 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
2409 struct kvm_kernel_irqfd *irqfd =
2410 container_of(cons, struct kvm_kernel_irqfd, consumer);
2412 kvm_arm_resume_guest(irqfd->kvm);
2415 /* Initialize Hyp-mode and memory mappings on all CPUs */
2416 static __init int kvm_arm_init(void)
2421 if (!is_hyp_mode_available()) {
2422 kvm_info("HYP mode not available\n");
2426 if (kvm_get_mode() == KVM_MODE_NONE) {
2427 kvm_info("KVM disabled from command line\n");
2431 err = kvm_sys_reg_table_init();
2433 kvm_info("Error initializing system register tables");
2437 in_hyp_mode = is_kernel_in_hyp_mode();
2439 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
2440 cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2441 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2442 "Only trusted guests should be used on this system.\n");
2444 err = kvm_set_ipa_limit();
2448 err = kvm_arm_init_sve();
2452 err = kvm_arm_vmid_alloc_init();
2454 kvm_err("Failed to initialize VMID allocator.\n");
2459 err = init_hyp_mode();
2464 err = kvm_init_vector_slots();
2466 kvm_err("Cannot initialise vector slots\n");
2470 err = init_subsystems();
2474 if (is_protected_kvm_enabled()) {
2475 kvm_info("Protected nVHE mode initialized successfully\n");
2476 } else if (in_hyp_mode) {
2477 kvm_info("VHE mode initialized successfully\n");
2479 kvm_info("Hyp mode initialized successfully\n");
2483 * FIXME: Do something reasonable if kvm_init() fails after pKVM
2484 * hypervisor protection is finalized.
2486 err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2490 kvm_arm_initialised = true;
2495 teardown_subsystems();
2498 teardown_hyp_mode();
2500 kvm_arm_vmid_alloc_free();
2504 static int __init early_kvm_mode_cfg(char *arg)
2509 if (strcmp(arg, "none") == 0) {
2510 kvm_mode = KVM_MODE_NONE;
2514 if (!is_hyp_mode_available()) {
2515 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2519 if (strcmp(arg, "protected") == 0) {
2520 if (!is_kernel_in_hyp_mode())
2521 kvm_mode = KVM_MODE_PROTECTED;
2523 pr_warn_once("Protected KVM not available with VHE\n");
2528 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2529 kvm_mode = KVM_MODE_DEFAULT;
2533 if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2534 kvm_mode = KVM_MODE_NV;
2540 early_param("kvm-arm.mode", early_kvm_mode_cfg);
2542 enum kvm_mode kvm_get_mode(void)
2547 module_init(kvm_arm_init);