]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
cputest: Add support for MSR features to cpu-gather.sh
authorJiri Denemark <jdenemar@redhat.com>
Tue, 26 Mar 2019 09:18:10 +0000 (10:18 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Fri, 12 Apr 2019 20:53:39 +0000 (22:53 +0200)
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>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/cputestdata/cpu-gather.sh

index 9c696f57bdbcc4d906a91d7cba178aac20df0192..cefd1b0d0d1689c291807e6691eb5d7b1a68d522 100755 (executable)
@@ -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