[libvirt] [PATCH 31/36] cputest: Add support for MSR features to cpu-gather.sh

Jiri Denemark posted 36 patches 6 years, 10 months ago
[libvirt] [PATCH 31/36] cputest: Add support for MSR features to cpu-gather.sh
Posted by Jiri Denemark 6 years, 10 months ago
This patch adds an inline python code for reading MSR features. Since
reading MSRs is a privileged operation, we have to read them from
/dev/cpu/*/msr if it is readable (i.e., the script runs as root) or
fallback to using KVM ioctl which can be done by any user that can start
virtual machines.

The python code is inlined rather than provided in a separate script
because whenever there's an issue with proper detection of CPU features,
we ask the reporter to run cpu-gather.sh script to give us all data we
need to know about the host CPU. Asking them to run several scripts
would likely result in one of them being ignored or forgotten.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 tests/cputestdata/cpu-gather.sh | 49 ++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/tests/cputestdata/cpu-gather.sh b/tests/cputestdata/cpu-gather.sh
index 9c696f57bd..cefd1b0d0d 100755
--- a/tests/cputestdata/cpu-gather.sh
+++ b/tests/cputestdata/cpu-gather.sh
@@ -7,8 +7,55 @@
 grep 'model name' /proc/cpuinfo | head -n1
 
 cpuid -1r
-
 echo
+
+for python in python3 python2; do
+    $python <<EOF
+from __future__ import print_function
+from struct import pack, unpack
+from fcntl import ioctl
+import sys, errno
+
+IA32_ARCH_CAPABILITIES_MSR = 0x10a
+KVM_GET_MSRS = 0xc008ae88
+
+def print_msr(msr, via=None):
+    if via is None:
+        print("MSR:")
+    else:
+        print("MSR via %s:" % via)
+    print("   0x%x: 0x%016x" % (IA32_ARCH_CAPABILITIES_MSR, msr))
+    print()
+
+try:
+    fd = open("/dev/cpu/0/msr", "rb")
+    fd.seek(IA32_ARCH_CAPABILITIES_MSR)
+    buf = fd.read(8)
+    msr = unpack("=Q", buf)[0]
+
+    print_msr(msr)
+    sys.exit(0)
+except IOError as e:
+    # The MSR is not supported on the host
+    if e.errno == errno.EIO:
+        sys.exit(0)
+
+try:
+    fd = open("/dev/kvm", "r")
+    bufIn = pack("=LLLLQ", 1, 0, IA32_ARCH_CAPABILITIES_MSR, 0, 0)
+    bufOut = ioctl(fd, KVM_GET_MSRS, bufIn)
+    msr = unpack("=LLLLQ", bufOut)[4]
+
+    print_msr(msr, via="KVM")
+except IOError as e:
+    pass
+EOF
+
+    if [[ $? -eq 0 ]]; then
+        break
+    fi
+done
+
 qemu=qemu-system-x86_64
 for cmd in /usr/bin/$qemu /usr/bin/qemu-kvm /usr/libexec/qemu-kvm; do
     if [[ -x $cmd ]]; then
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 31/36] cputest: Add support for MSR features to cpu-gather.sh
Posted by Ján Tomko 6 years, 10 months ago
On Mon, Apr 08, 2019 at 10:42:35AM +0200, Jiri Denemark wrote:
>This patch adds an inline python code for reading MSR features. Since
>reading MSRs is a privileged operation, we have to read them from
>/dev/cpu/*/msr if it is readable (i.e., the script runs as root) or
>fallback to using KVM ioctl which can be done by any user that can start
>virtual machines.
>
>The python code is inlined rather than provided in a separate script
>because whenever there's an issue with proper detection of CPU features,
>we ask the reporter to run cpu-gather.sh script to give us all data we
>need to know about the host CPU. Asking them to run several scripts
>would likely result in one of them being ignored or forgotten.
>
>Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
>---
> tests/cputestdata/cpu-gather.sh | 49 ++++++++++++++++++++++++++++++++-
> 1 file changed, 48 insertions(+), 1 deletion(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list