]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
cpu-data.py: Parse kcpuid output
authorTim Wiederhake <twiederh@redhat.com>
Tue, 27 Apr 2021 08:25:02 +0000 (10:25 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 7 May 2021 15:14:53 +0000 (17:14 +0200)
Linux 5.13 introduces "kcpuid", a tool similar to "cpuid", see
https://lore.kernel.org/lkml/1614928878-86075-1-git-send-email-feng.tang@intel.com/

Output formats of cpuid and kcpuid differ slightly. This adds support
for the latter.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tests/cputestdata/cpu-data.py

index ac4d0ff8e73c66e6771dcabf4be8d5dbd8930e80..4305aacf35fe849f855896a9979765c7578e8903 100755 (executable)
@@ -58,6 +58,53 @@ def gather_cpuid_leaves_cpuid(output):
             "edx": int(match.group(6), 0)}
 
 
+def gather_cpuid_leaves_kcpuid(output):
+    leave_pattern = re.compile(
+        "^(0x[0-9a-f]+): "
+        "EAX=(0x[0-9a-f]+), "
+        "EBX=(0x[0-9a-f]+), "
+        "ECX=(0x[0-9a-f]+), "
+        "EDX=(0x[0-9a-f]+)$")
+    branch_pattern_head = re.compile(
+        "^(0x[0-9a-f]+): "
+        "subleafs:$")
+    branch_pattern_body = re.compile(
+        "^\\s*([0-9]+): "
+        "EAX=(0x[0-9a-f]+), "
+        "EBX=(0x[0-9a-f]+), "
+        "ECX=(0x[0-9a-f]+), "
+        "EDX=(0x[0-9a-f]+)$")
+
+    regs = list()
+    eax_in = 0
+    for line in output.split("\n"):
+        match = branch_pattern_head.match(line)
+        if match:
+            eax_in = int(match.group(1), 0)
+            continue
+        match = branch_pattern_body.match(line)
+        if match:
+            regs.append({
+                "eax_in": eax_in,
+                "ecx_in": int(match.group(1), 0),
+                "eax": int(match.group(2), 0),
+                "ebx": int(match.group(3), 0),
+                "ecx": int(match.group(4), 0),
+                "edx": int(match.group(5), 0)})
+            continue
+        match = leave_pattern.match(line)
+        if match:
+            regs.append({
+                "eax_in": int(match.group(1), 0),
+                "ecx_in": 0,
+                "eax": int(match.group(2), 0),
+                "ebx": int(match.group(3), 0),
+                "ecx": int(match.group(4), 0),
+                "edx": int(match.group(5), 0)})
+            continue
+    return regs
+
+
 def gather_cpuid_leaves(args):
     def mask(regs, eax_in, ecx_in, eax_mask, ebx_mask, ecx_mask, edx_mask):
         if regs["eax_in"] == eax_in and regs["ecx_in"] == ecx_in:
@@ -77,7 +124,11 @@ def gather_cpuid_leaves(args):
              "package, you can find the sources or binary packages at "
              "'http://www.etallen.com/cpuid.html'.".format(e.filename))
 
-    reglist = gather_cpuid_leaves_cpuid(output)
+    if "=====" in output:
+        reglist = gather_cpuid_leaves_kcpuid(output)
+    else:
+        reglist = gather_cpuid_leaves_cpuid(output)
+
     for regs in reglist:
         # local apic id. Pretend to always run on logical processor #0.
         mask(regs, 0x01, 0x00, 0xffffffff, 0x00ffffff, 0xffffffff, 0xffffffff)