render / rpms / libvirt

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