[PART1 RFC 9/9] svm: Manage vcpu load/unload when enable AVIC

From: Suravee Suthikulpanit
Date: Fri Feb 12 2016 - 09:01:31 EST


When a vcpu is loaded/unloaded to a physical core, we need to update
information in the Physical APIC-ID table accordingly.

Signed-off-by: Suravee Suthikulpanit <Suravee.Suthikulpanit@xxxxxxx>
---
arch/x86/kvm/svm.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)

diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index 32da657..41e68d2 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -32,6 +32,7 @@
#include <linux/trace_events.h>
#include <linux/slab.h>

+#include <asm/apic.h>
#include <asm/perf_event.h>
#include <asm/tlbflush.h>
#include <asm/desc.h>
@@ -1508,6 +1509,61 @@ static int avic_vcpu_init(struct kvm *kvm, struct vcpu_svm *svm, int id)
return 0;
}

+static inline int avic_update_iommu(struct kvm_vcpu *vcpu, int cpu,
+ phys_addr_t pa, bool is_running)
+{
+ if (!kvm_arch_has_assigned_device(vcpu->kvm))
+ return 0;
+
+ /* TODO: We will hook up with IOMMU API at later time */
+ return 0;
+}
+
+static int avic_set_running(struct kvm_vcpu *vcpu, int cpu, bool is_running)
+{
+ int g_phy_apic_id, h_phy_apic_id;
+ struct svm_avic_phy_ait_entry *entry;
+ struct vcpu_svm *svm = to_svm(vcpu);
+ int ret;
+
+ if (!avic)
+ return 0;
+
+ if (!svm)
+ return -EINVAL;
+
+ /* Note: APIC ID = 0xff is used for broadcast.
+ * APIC ID > 0xff is reserved.
+ */
+ g_phy_apic_id = vcpu->vcpu_id;
+ h_phy_apic_id = __default_cpu_present_to_apicid(cpu);
+
+ if ((g_phy_apic_id >= AVIC_PHY_APIC_ID_MAX) ||
+ (h_phy_apic_id >= AVIC_PHY_APIC_ID_MAX))
+ return -EINVAL;
+
+ entry = avic_get_phy_ait_entry(vcpu, g_phy_apic_id);
+ if (!entry)
+ return -EINVAL;
+
+ if (is_running) {
+ phys_addr_t pa = PFN_PHYS(page_to_pfn(svm->avic_bk_page));
+
+ entry->bk_pg_ptr = (pa >> 12) & 0xffffffffff;
+ entry->valid = 1;
+ entry->host_phy_apic_id = h_phy_apic_id;
+ barrier();
+ entry->is_running = is_running;
+ ret = avic_update_iommu(vcpu, h_phy_apic_id, pa, is_running);
+ } else {
+ ret = avic_update_iommu(vcpu, 0, 0, is_running);
+ barrier();
+ entry->is_running = is_running;
+ }
+
+ return ret;
+}
+
static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
{
struct vcpu_svm *svm = to_svm(vcpu);
@@ -1628,6 +1684,8 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
mark_all_dirty(svm->vmcb);
}

+ avic_set_running(vcpu, cpu, true);
+
#ifdef CONFIG_X86_64
rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
#endif
@@ -1668,6 +1726,9 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)
#endif
for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
+
+ avic_set_running(vcpu, 0, false);
+
}

static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
--
1.9.1