render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From 6094ea6111dd57274b6037e76f662a06b8e5c9d1 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <6094ea6111dd57274b6037e76f662a06b8e5c9d1@dist-git>
c480ed
From: Jiri Denemark <jdenemar@redhat.com>
c480ed
Date: Fri, 21 Jun 2019 09:25:43 +0200
c480ed
Subject: [PATCH] vircpuhost: Add support for reading MSRs
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
The new virHostCPUGetMSR internal API will try to read the MSR from
c480ed
/dev/cpu/0/msr and if it is not possible (the device does not exist or
c480ed
libvirt is running unprivileged), it will fallback to asking KVM for the
c480ed
MSR using KVM_GET_MSRS ioctl.
c480ed
c480ed
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
(cherry picked from commit df4b46737f43a1a67f9b5de2840213a1bd2b3cce)
c480ed
c480ed
https://bugzilla.redhat.com/show_bug.cgi?id=1697627
c480ed
c480ed
Conflicts:
c480ed
	src/util/virhostcpu.h
c480ed
            - different header file guard symbol
c480ed
c480ed
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Message-Id: <0d4c18704c435cccc2b18ebd55f4f914caf05d13.1561068591.git.jdenemar@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/libvirt_private.syms |  1 +
c480ed
 src/util/virhostcpu.c    | 80 ++++++++++++++++++++++++++++++++++++++++
c480ed
 src/util/virhostcpu.h    |  3 ++
c480ed
 3 files changed, 84 insertions(+)
c480ed
c480ed
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c480ed
index 347667b17c..9ebc5384fb 100644
c480ed
--- a/src/libvirt_private.syms
c480ed
+++ b/src/libvirt_private.syms
c480ed
@@ -1974,6 +1974,7 @@ virHostCPUGetInfo;
c480ed
 virHostCPUGetKVMMaxVCPUs;
c480ed
 virHostCPUGetMap;
c480ed
 virHostCPUGetMicrocodeVersion;
c480ed
+virHostCPUGetMSR;
c480ed
 virHostCPUGetOnline;
c480ed
 virHostCPUGetOnlineBitmap;
c480ed
 virHostCPUGetPresentBitmap;
c480ed
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
c480ed
index effe04ca3a..d0c47faa18 100644
c480ed
--- a/src/util/virhostcpu.c
c480ed
+++ b/src/util/virhostcpu.c
c480ed
@@ -64,6 +64,7 @@
c480ed
 VIR_LOG_INIT("util.hostcpu");
c480ed
 
c480ed
 #define KVM_DEVICE "/dev/kvm"
c480ed
+#define MSR_DEVICE "/dev/cpu/0/msr"
c480ed
 
c480ed
 
c480ed
 #if defined(__FreeBSD__) || defined(__APPLE__)
c480ed
@@ -1266,3 +1267,82 @@ virHostCPUGetMicrocodeVersion(void)
c480ed
 }
c480ed
 
c480ed
 #endif /* __linux__ */
c480ed
+
c480ed
+
c480ed
+#if HAVE_LINUX_KVM_H && defined(KVM_GET_MSRS)
c480ed
+static int
c480ed
+virHostCPUGetMSRFromKVM(unsigned long index,
c480ed
+                        uint64_t *result)
c480ed
+{
c480ed
+    VIR_AUTOCLOSE fd = -1;
c480ed
+    struct {
c480ed
+        struct kvm_msrs header;
c480ed
+        struct kvm_msr_entry entry;
c480ed
+    } msr = {
c480ed
+        .header = { .nmsrs = 1 },
c480ed
+        .entry = { .index = index },
c480ed
+    };
c480ed
+
c480ed
+    if ((fd = open(KVM_DEVICE, O_RDONLY)) < 0) {
c480ed
+        virReportSystemError(errno, _("Unable to open %s"), KVM_DEVICE);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    if (ioctl(fd, KVM_GET_MSRS, &msr) < 0) {
c480ed
+        VIR_DEBUG("Cannot get MSR 0x%lx from KVM", index);
c480ed
+        return 1;
c480ed
+    }
c480ed
+
c480ed
+    *result = msr.entry.data;
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+#else
c480ed
+
c480ed
+static int
c480ed
+virHostCPUGetMSRFromKVM(unsigned long index ATTRIBUTE_UNUSED,
c480ed
+                        uint64_t *result ATTRIBUTE_UNUSED)
c480ed
+{
c480ed
+    virReportSystemError(ENOSYS, "%s",
c480ed
+                         _("Reading MSRs via KVM is not supported on this platform"));
c480ed
+    return -1;
c480ed
+}
c480ed
+#endif /* HAVE_LINUX_KVM_H && defined(KVM_GET_MSRS) */
c480ed
+
c480ed
+
c480ed
+/*
c480ed
+ * Returns 0 on success,
c480ed
+ *         1 when the MSR is not supported by the host CPU,
c480ed
+*         -1 on error.
c480ed
+ */
c480ed
+int
c480ed
+virHostCPUGetMSR(unsigned long index,
c480ed
+                 uint64_t *msr)
c480ed
+{
c480ed
+    VIR_AUTOCLOSE fd = -1;
c480ed
+    char ebuf[1024];
c480ed
+
c480ed
+    *msr = 0;
c480ed
+
c480ed
+    if ((fd = open(MSR_DEVICE, O_RDONLY)) < 0) {
c480ed
+        VIR_DEBUG("Unable to open %s: %s",
c480ed
+                  MSR_DEVICE, virStrerror(errno, ebuf, sizeof(ebuf)));
c480ed
+    } else {
c480ed
+        int rc = pread(fd, msr, sizeof(*msr), index);
c480ed
+
c480ed
+        if (rc == sizeof(*msr))
c480ed
+            return 0;
c480ed
+
c480ed
+        if (rc < 0 && errno == EIO) {
c480ed
+            VIR_DEBUG("CPU does not support MSR 0x%lx", index);
c480ed
+            return 1;
c480ed
+        }
c480ed
+
c480ed
+        VIR_DEBUG("Cannot read MSR 0x%lx from %s: %s",
c480ed
+                  index, MSR_DEVICE, virStrerror(errno, ebuf, sizeof(ebuf)));
c480ed
+    }
c480ed
+
c480ed
+    VIR_DEBUG("Falling back to KVM ioctl");
c480ed
+
c480ed
+    return virHostCPUGetMSRFromKVM(index, msr);
c480ed
+}
c480ed
diff --git a/src/util/virhostcpu.h b/src/util/virhostcpu.h
c480ed
index f9f3359288..e705623d4f 100644
c480ed
--- a/src/util/virhostcpu.h
c480ed
+++ b/src/util/virhostcpu.h
c480ed
@@ -68,4 +68,7 @@ int virHostCPUGetOnline(unsigned int cpu, bool *online);
c480ed
 
c480ed
 unsigned int virHostCPUGetMicrocodeVersion(void);
c480ed
 
c480ed
+int virHostCPUGetMSR(unsigned long index,
c480ed
+                     uint64_t *msr);
c480ed
+
c480ed
 #endif /* __VIR_HOSTCPU_H__*/
c480ed
-- 
c480ed
2.22.0
c480ed