Merge branches 'pm-cpufreq', 'pm-sleep' and 'pm-em'
[linux-2.6-microblaze.git] / arch / arm64 / kvm / arm.c
index 0ca72f5..fe102cd 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <linux/bug.h>
 #include <linux/cpu_pm.h>
+#include <linux/entry-kvm.h>
 #include <linux/errno.h>
 #include <linux/err.h>
 #include <linux/kvm_host.h>
@@ -15,6 +16,7 @@
 #include <linux/fs.h>
 #include <linux/mman.h>
 #include <linux/sched.h>
+#include <linux/kmemleak.h>
 #include <linux/kvm.h>
 #include <linux/kvm_irqfd.h>
 #include <linux/irqbypass.h>
 #include <kvm/arm_pmu.h>
 #include <kvm/arm_psci.h>
 
-#ifdef REQUIRES_VIRT
-__asm__(".arch_extension       virt");
-#endif
-
 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
 DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
 
@@ -575,7 +573,7 @@ static void update_vmid(struct kvm_vmid *vmid)
                kvm_call_hyp(__kvm_flush_vm_context);
        }
 
-       vmid->vmid = kvm_next_vmid;
+       WRITE_ONCE(vmid->vmid, kvm_next_vmid);
        kvm_next_vmid++;
        kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
 
@@ -718,6 +716,45 @@ static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
                static_branch_unlikely(&arm64_mismatched_32bit_el0);
 }
 
+/**
+ * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
+ * @vcpu:      The VCPU pointer
+ * @ret:       Pointer to write optional return code
+ *
+ * Returns: true if the VCPU needs to return to a preemptible + interruptible
+ *         and skip guest entry.
+ *
+ * This function disambiguates between two different types of exits: exits to a
+ * preemptible + interruptible kernel context and exits to userspace. For an
+ * exit to userspace, this function will write the return code to ret and return
+ * true. For an exit to preemptible + interruptible kernel context (i.e. check
+ * for pending work and re-enter), return true without writing to ret.
+ */
+static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
+{
+       struct kvm_run *run = vcpu->run;
+
+       /*
+        * If we're using a userspace irqchip, then check if we need
+        * to tell a userspace irqchip about timer or PMU level
+        * changes and if so, exit to userspace (the actual level
+        * state gets updated in kvm_timer_update_run and
+        * kvm_pmu_update_run below).
+        */
+       if (static_branch_unlikely(&userspace_irqchip_in_use)) {
+               if (kvm_timer_should_notify_user(vcpu) ||
+                   kvm_pmu_should_notify_user(vcpu)) {
+                       *ret = -EINTR;
+                       run->exit_reason = KVM_EXIT_INTR;
+                       return true;
+               }
+       }
+
+       return kvm_request_pending(vcpu) ||
+                       need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) ||
+                       xfer_to_guest_mode_work_pending();
+}
+
 /**
  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
  * @vcpu:      The VCPU pointer
@@ -761,7 +798,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
                /*
                 * Check conditions before entering the guest
                 */
-               cond_resched();
+               ret = xfer_to_guest_mode_handle_work(vcpu);
+               if (!ret)
+                       ret = 1;
 
                update_vmid(&vcpu->arch.hw_mmu->vmid);
 
@@ -780,30 +819,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
                kvm_vgic_flush_hwstate(vcpu);
 
-               /*
-                * Exit if we have a signal pending so that we can deliver the
-                * signal to user space.
-                */
-               if (signal_pending(current)) {
-                       ret = -EINTR;
-                       run->exit_reason = KVM_EXIT_INTR;
-               }
-
-               /*
-                * If we're using a userspace irqchip, then check if we need
-                * to tell a userspace irqchip about timer or PMU level
-                * changes and if so, exit to userspace (the actual level
-                * state gets updated in kvm_timer_update_run and
-                * kvm_pmu_update_run below).
-                */
-               if (static_branch_unlikely(&userspace_irqchip_in_use)) {
-                       if (kvm_timer_should_notify_user(vcpu) ||
-                           kvm_pmu_should_notify_user(vcpu)) {
-                               ret = -EINTR;
-                               run->exit_reason = KVM_EXIT_INTR;
-                       }
-               }
-
                /*
                 * Ensure we set mode to IN_GUEST_MODE after we disable
                 * interrupts and before the final VCPU requests check.
@@ -812,8 +827,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
                 */
                smp_store_mb(vcpu->mode, IN_GUEST_MODE);
 
-               if (ret <= 0 || need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) ||
-                   kvm_request_pending(vcpu)) {
+               if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
                        vcpu->mode = OUTSIDE_GUEST_MODE;
                        isb(); /* Ensure work in x_flush_hwstate is committed */
                        kvm_pmu_sync_hwstate(vcpu);
@@ -1039,7 +1053,7 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
                               const struct kvm_vcpu_init *init)
 {
        unsigned int i, ret;
-       int phys_target = kvm_target_cpu();
+       u32 phys_target = kvm_target_cpu();
 
        if (init->target != phys_target)
                return -EINVAL;
@@ -1108,6 +1122,7 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
        }
 
        vcpu_reset_hcr(vcpu);
+       vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
 
        /*
         * Handle the "start in power-off" case.
@@ -1219,6 +1234,14 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
                if (copy_from_user(&reg, argp, sizeof(reg)))
                        break;
 
+               /*
+                * We could owe a reset due to PSCI. Handle the pending reset
+                * here to ensure userspace register accesses are ordered after
+                * the reset.
+                */
+               if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
+                       kvm_reset_vcpu(vcpu);
+
                if (ioctl == KVM_SET_ONE_REG)
                        r = kvm_arm_set_reg(vcpu, &reg);
                else
@@ -1700,11 +1723,6 @@ static bool init_psci_relay(void)
        return true;
 }
 
-static int init_common_resources(void)
-{
-       return kvm_set_ipa_limit();
-}
-
 static int init_subsystems(void)
 {
        int err = 0;
@@ -1958,56 +1976,17 @@ static void _kvm_host_prot_finalize(void *discard)
        WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize));
 }
 
-static inline int pkvm_mark_hyp(phys_addr_t start, phys_addr_t end)
-{
-       return kvm_call_hyp_nvhe(__pkvm_mark_hyp, start, end);
-}
-
-#define pkvm_mark_hyp_section(__section)               \
-       pkvm_mark_hyp(__pa_symbol(__section##_start),   \
-                       __pa_symbol(__section##_end))
-
 static int finalize_hyp_mode(void)
 {
-       int cpu, ret;
-
        if (!is_protected_kvm_enabled())
                return 0;
 
-       ret = pkvm_mark_hyp_section(__hyp_idmap_text);
-       if (ret)
-               return ret;
-
-       ret = pkvm_mark_hyp_section(__hyp_text);
-       if (ret)
-               return ret;
-
-       ret = pkvm_mark_hyp_section(__hyp_rodata);
-       if (ret)
-               return ret;
-
-       ret = pkvm_mark_hyp_section(__hyp_bss);
-       if (ret)
-               return ret;
-
-       ret = pkvm_mark_hyp(hyp_mem_base, hyp_mem_base + hyp_mem_size);
-       if (ret)
-               return ret;
-
-       for_each_possible_cpu(cpu) {
-               phys_addr_t start = virt_to_phys((void *)kvm_arm_hyp_percpu_base[cpu]);
-               phys_addr_t end = start + (PAGE_SIZE << nvhe_percpu_order());
-
-               ret = pkvm_mark_hyp(start, end);
-               if (ret)
-                       return ret;
-
-               start = virt_to_phys((void *)per_cpu(kvm_arm_hyp_stack_page, cpu));
-               end = start + PAGE_SIZE;
-               ret = pkvm_mark_hyp(start, end);
-               if (ret)
-                       return ret;
-       }
+       /*
+        * Exclude HYP BSS from kmemleak so that it doesn't get peeked
+        * at, which would end badly once the section is inaccessible.
+        * None of other sections should ever be introspected.
+        */
+       kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start);
 
        /*
         * Flip the static key upfront as that may no longer be possible
@@ -2019,11 +1998,6 @@ static int finalize_hyp_mode(void)
        return 0;
 }
 
-static void check_kvm_target_cpu(void *ret)
-{
-       *(int *)ret = kvm_target_cpu();
-}
-
 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
 {
        struct kvm_vcpu *vcpu;
@@ -2083,7 +2057,6 @@ void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
 int kvm_arch_init(void *opaque)
 {
        int err;
-       int ret, cpu;
        bool in_hyp_mode;
 
        if (!is_hyp_mode_available()) {
@@ -2098,15 +2071,7 @@ int kvm_arch_init(void *opaque)
                kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
                         "Only trusted guests should be used on this system.\n");
 
-       for_each_online_cpu(cpu) {
-               smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
-               if (ret < 0) {
-                       kvm_err("Error, CPU %d not supported!\n", cpu);
-                       return -ENODEV;
-               }
-       }
-
-       err = init_common_resources();
+       err = kvm_set_ipa_limit();
        if (err)
                return err;