fbe740
From 51290a9442c1b9347c43b2fec34b7aa979d26c77 Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <51290a9442c1b9347c43b2fec34b7aa979d26c77@dist-git>
fbe740
From: Jiri Denemark <jdenemar@redhat.com>
fbe740
Date: Tue, 26 May 2020 10:59:34 +0200
fbe740
Subject: [PATCH] cpu_x86: Add support for stepping part of CPU signature
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
CPU models defined in the cpu_map can use signature/@stepping attribute
fbe740
to match a limited set of stepping numbers. The value is a bitmap for
fbe740
bits 0..15 each corresponding to a single stepping value. For example,
fbe740
stepping='4-6,9' will match 4, 5, 6, and 9. Omitting the attribute is
fbe740
equivalent to stepping='0-15'.
fbe740
fbe740
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
(cherry picked from commit c7a27949954d78dc95459758e329fb9c580361bb)
fbe740
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1840010
fbe740
fbe740
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
fbe740
Message-Id: <20bd9df72a22a004bb665409ddba20ff89a5b66d.1590483392.git.jdenemar@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 src/cpu/cpu_x86.c | 60 +++++++++++++++++++++++++++++++++++++++--------
fbe740
 1 file changed, 50 insertions(+), 10 deletions(-)
fbe740
fbe740
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
fbe740
index 45a073c1d0..0d81f3d2ae 100644
fbe740
--- a/src/cpu/cpu_x86.c
fbe740
+++ b/src/cpu/cpu_x86.c
fbe740
@@ -125,6 +125,7 @@ typedef struct _virCPUx86Signature virCPUx86Signature;
fbe740
 struct _virCPUx86Signature {
fbe740
     unsigned int family;
fbe740
     unsigned int model;
fbe740
+    virBitmapPtr stepping;
fbe740
 };
fbe740
 
fbe740
 typedef struct _virCPUx86Signatures virCPUx86Signatures;
fbe740
@@ -732,7 +733,17 @@ x86MakeSignature(unsigned int family,
fbe740
 static uint32_t
fbe740
 virCPUx86SignatureToCPUID(virCPUx86Signature *sig)
fbe740
 {
fbe740
-    return x86MakeSignature(sig->family, sig->model, 0);
fbe740
+    unsigned int stepping = 0;
fbe740
+
fbe740
+    if (sig->stepping) {
fbe740
+        ssize_t firstBit;
fbe740
+
fbe740
+        firstBit = virBitmapNextSetBit(sig->stepping, -1);
fbe740
+        if (firstBit >= 0)
fbe740
+            stepping = firstBit;
fbe740
+    }
fbe740
+
fbe740
+    return x86MakeSignature(sig->family, sig->model, stepping);
fbe740
 }
fbe740
 
fbe740
 
fbe740
@@ -767,8 +778,8 @@ x86DataToSignatureFull(const virCPUx86Data *data,
fbe740
 }
fbe740
 
fbe740
 
fbe740
-/* Mask out irrelevant bits (R and Step) from processor signature. */
fbe740
-#define SIGNATURE_MASK  0x0fff3ff0
fbe740
+/* Mask out reserved bits from processor signature. */
fbe740
+#define SIGNATURE_MASK  0x0fff3fff
fbe740
 
fbe740
 static uint32_t
fbe740
 x86DataToSignature(const virCPUx86Data *data)
fbe740
@@ -1134,9 +1145,14 @@ virCPUx86SignaturesNew(size_t count)
fbe740
 static void
fbe740
 virCPUx86SignaturesFree(virCPUx86SignaturesPtr sigs)
fbe740
 {
fbe740
+    size_t i;
fbe740
+
fbe740
     if (!sigs)
fbe740
         return;
fbe740
 
fbe740
+    for (i = 0; i < sigs->count; i++)
fbe740
+        virBitmapFree(sigs->items[i].stepping);
fbe740
+
fbe740
     g_free(sigs->items);
fbe740
     g_free(sigs);
fbe740
 }
fbe740
@@ -1153,8 +1169,12 @@ virCPUx86SignaturesCopy(virCPUx86SignaturesPtr src)
fbe740
 
fbe740
     dst = virCPUx86SignaturesNew(src->count);
fbe740
 
fbe740
-    for (i = 0; i < src->count; i++)
fbe740
-        dst->items[i] = src->items[i];
fbe740
+    for (i = 0; i < src->count; i++) {
fbe740
+        dst->items[i].family = src->items[i].family;
fbe740
+        dst->items[i].model = src->items[i].model;
fbe740
+        if (src->items[i].stepping)
fbe740
+            dst->items[i].stepping = virBitmapNewCopy(src->items[i].stepping);
fbe740
+    }
fbe740
 
fbe740
     return dst;
fbe740
 }
fbe740
@@ -1176,7 +1196,9 @@ virCPUx86SignaturesMatch(virCPUx86SignaturesPtr sigs,
fbe740
 
fbe740
     for (i = 0; i < sigs->count; i++) {
fbe740
         if (sigs->items[i].family == family &&
fbe740
-            sigs->items[i].model == model)
fbe740
+            sigs->items[i].model == model &&
fbe740
+            (!sigs->items[i].stepping ||
fbe740
+             virBitmapIsBitSet(sigs->items[i].stepping, stepping)))
fbe740
             return true;
fbe740
     }
fbe740
 
fbe740
@@ -1194,9 +1216,15 @@ virCPUx86SignaturesFormat(virCPUx86SignaturesPtr sigs)
fbe740
         return virBufferContentAndReset(&buf;;
fbe740
 
fbe740
     for (i = 0; i < sigs->count; i++) {
fbe740
-        virBufferAsprintf(&buf, "(%u,%u,0), ",
fbe740
+        g_autofree char *stepping = NULL;
fbe740
+
fbe740
+        if (sigs->items[i].stepping)
fbe740
+            stepping = virBitmapFormat(sigs->items[i].stepping);
fbe740
+
fbe740
+        virBufferAsprintf(&buf, "(%u,%u,%s), ",
fbe740
                           sigs->items[i].family,
fbe740
-                          sigs->items[i].model);
fbe740
+                          sigs->items[i].model,
fbe740
+                          stepping ? stepping : "0-15");
fbe740
     }
fbe740
 
fbe740
     virBufferTrim(&buf, ", ", -1);
fbe740
@@ -1473,6 +1501,7 @@ x86ModelParseSignatures(virCPUx86ModelPtr model,
fbe740
 
fbe740
     for (i = 0; i < n; i++) {
fbe740
         virCPUx86Signature *sig = &model->signatures->items[i];
fbe740
+        g_autofree char *stepping = NULL;
fbe740
         int rc;
fbe740
 
fbe740
         ctxt->node = nodes[i];
fbe740
@@ -1492,6 +1521,11 @@ x86ModelParseSignatures(virCPUx86ModelPtr model,
fbe740
                            model->name);
fbe740
             return -1;
fbe740
         }
fbe740
+
fbe740
+        stepping = virXPathString("string(@stepping)", ctxt);
fbe740
+        /* stepping corresponds to 4 bits in 32b signature, see above */
fbe740
+        if (stepping && virBitmapParse(stepping, &sig->stepping, 16) < 0)
fbe740
+            return -1;
fbe740
     }
fbe740
 
fbe740
     ctxt->node = root;
fbe740
@@ -2090,6 +2124,9 @@ x86Decode(virCPUDefPtr cpu,
fbe740
     virDomainCapsCPUModelPtr hvModel = NULL;
fbe740
     g_autofree char *sigs = NULL;
fbe740
     uint32_t signature;
fbe740
+    unsigned int sigFamily;
fbe740
+    unsigned int sigModel;
fbe740
+    unsigned int sigStepping;
fbe740
     ssize_t i;
fbe740
     int rc;
fbe740
 
fbe740
@@ -2103,6 +2140,7 @@ x86Decode(virCPUDefPtr cpu,
fbe740
 
fbe740
     vendor = x86DataToVendor(&data, map);
fbe740
     signature = x86DataToSignature(&data);
fbe740
+    virCPUx86SignatureFromCPUID(signature, &sigFamily, &sigModel, &sigStepping);
fbe740
 
fbe740
     x86DataFilterTSX(&data, vendor, map);
fbe740
 
fbe740
@@ -2184,8 +2222,10 @@ x86Decode(virCPUDefPtr cpu,
fbe740
 
fbe740
     sigs = virCPUx86SignaturesFormat(model->signatures);
fbe740
 
fbe740
-    VIR_DEBUG("Using CPU model %s (signatures %s) for CPU with signature %06lx",
fbe740
-              model->name, NULLSTR(sigs), (unsigned long)signature);
fbe740
+    VIR_DEBUG("Using CPU model %s with signatures [%s] for "
fbe740
+              "CPU with signature (%u,%u,%u)",
fbe740
+              model->name, NULLSTR(sigs),
fbe740
+              sigFamily, sigModel, sigStepping);
fbe740
 
fbe740
     cpu->model = g_steal_pointer(&cpuModel->model);
fbe740
     cpu->features = g_steal_pointer(&cpuModel->features);
fbe740
-- 
fbe740
2.26.2
fbe740