From 3e04b68ccd19a4bd0d86348620ce5e45bca04409 Mon Sep 17 00:00:00 2001 From: Tim Wiederhake Date: Tue, 15 Dec 2020 17:24:53 +0100 Subject: [PATCH] cpu-gather: Move msr decoding to new script Fixes the leaking file descriptors. Does not silently ignore errors (e.g. permission denied on /dev/cpu/0/msr if run as non-root) and always attempt to read from /dev/kvm if /dev/cpu/0/msr failed. 'gather_msr()' returns a dictionary of values, as a later patch will add more registers to be interrogated. Signed-off-by: Tim Wiederhake Reviewed-by: Michal Privoznik --- tests/cputestdata/cpu-gather.py | 34 ++++++++++++++++++++++++++++ tests/cputestdata/cpu-gather.sh | 40 --------------------------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/tests/cputestdata/cpu-gather.py b/tests/cputestdata/cpu-gather.py index 75cf290a28..db2348b460 100755 --- a/tests/cputestdata/cpu-gather.py +++ b/tests/cputestdata/cpu-gather.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 import argparse +import fcntl import os +import struct import subprocess +import sys def gather_name(args): @@ -38,6 +41,31 @@ def gather_cpuid_leaves(args): yield line.strip() +def gather_msr(): + IA32_ARCH_CAPABILITIES_MSR = 0x10a + KVM_GET_MSRS = 0xc008ae88 + + try: + with open("/dev/cpu/0/msr", "rb") as f: + f.seek(IA32_ARCH_CAPABILITIES_MSR) + buf = f.read(8) + msr = struct.unpack("=Q", buf)[0] + return "", {IA32_ARCH_CAPABILITIES_MSR: msr} + except IOError as e: + print("Warning: {}".format(e), file=sys.stderr) + + try: + bufIn = struct.pack("=LLLLQ", 1, 0, IA32_ARCH_CAPABILITIES_MSR, 0, 0) + with open("/dev/kvm", "rb") as f: + bufOut = fcntl.ioctl(f, KVM_GET_MSRS, bufIn) + msr = struct.unpack("=LLLLQ", bufOut)[4] + return " via KVM", {IA32_ARCH_CAPABILITIES_MSR: msr} + except IOError as e: + print("Warning: {}".format(e), file=sys.stderr) + + return None, {} + + def main(): parser = argparse.ArgumentParser(description="Gather cpu test data") parser.add_argument( @@ -61,6 +89,12 @@ def main(): print(" {}".format(leave)) print() + via, msr = gather_msr() + if via is not None: + print("MSR{}:".format(via)) + for key, value in sorted(msr.items()): + print(" 0x{:x}: 0x{:016x}\n".format(int(key), value)) + print(end="", flush=True) os.environ["CPU_GATHER_PY"] = "true" subprocess.check_call("./cpu-gather.sh") diff --git a/tests/cputestdata/cpu-gather.sh b/tests/cputestdata/cpu-gather.sh index f84215e777..427b81a64b 100755 --- a/tests/cputestdata/cpu-gather.sh +++ b/tests/cputestdata/cpu-gather.sh @@ -5,46 +5,6 @@ if [ -z "${CPU_GATHER_PY}" ]; then exit 1 fi -python3 <