Blame SOURCES/libvirt-cpu_x86-Replace-32b-signatures-in-virCPUx86Model-with-a-struct.patch

a41c76
From 22fecd96659495908f37e1c33c1ed52be5fb2d44 Mon Sep 17 00:00:00 2001
a41c76
Message-Id: <22fecd96659495908f37e1c33c1ed52be5fb2d44@dist-git>
a41c76
From: Jiri Denemark <jdenemar@redhat.com>
a41c76
Date: Tue, 26 May 2020 10:59:32 +0200
a41c76
Subject: [PATCH] cpu_x86: Replace 32b signatures in virCPUx86Model with a
a41c76
 struct
a41c76
MIME-Version: 1.0
a41c76
Content-Type: text/plain; charset=UTF-8
a41c76
Content-Transfer-Encoding: 8bit
a41c76
a41c76
The CPU models in our cpu_map define their signatures using separate
a41c76
family and model numbers. Let's store the signatures in the same way in
a41c76
our runtime representation of the cpu_map.
a41c76
a41c76
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
a41c76
Reviewed-by: Ján Tomko <jtomko@redhat.com>
a41c76
(cherry picked from commit 22bded201ffc91661e44065203dcb987e51084ca)
a41c76
a41c76
https://bugzilla.redhat.com/show_bug.cgi?id=1840010
a41c76
a41c76
Conflicts:
a41c76
	src/cpu/cpu_x86.c
a41c76
            - v6.0.0-264-g0a125c7144 which removes the third argument
a41c76
              from virBufferTrim was not backported
a41c76
a41c76
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
a41c76
Message-Id: <21c1eee9c7bb3811f43aa044bb97fa373a159e26.1590483392.git.jdenemar@redhat.com>
a41c76
Reviewed-by: Ján Tomko <jtomko@redhat.com>
a41c76
---
a41c76
 src/cpu/cpu_x86.c | 149 +++++++++++++++++++++++++++++-----------------
a41c76
 1 file changed, 95 insertions(+), 54 deletions(-)
a41c76
a41c76
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
a41c76
index dad3bceff0..b87e3753da 100644
a41c76
--- a/src/cpu/cpu_x86.c
a41c76
+++ b/src/cpu/cpu_x86.c
a41c76
@@ -121,6 +121,19 @@ static virCPUx86Feature x86_kvm_features[] =
a41c76
     KVM_FEATURE(VIR_CPU_x86_HV_STIMER_DIRECT),
a41c76
 };
a41c76
 
a41c76
+typedef struct _virCPUx86Signature virCPUx86Signature;
a41c76
+struct _virCPUx86Signature {
a41c76
+    unsigned int family;
a41c76
+    unsigned int model;
a41c76
+};
a41c76
+
a41c76
+typedef struct _virCPUx86Signatures virCPUx86Signatures;
a41c76
+typedef virCPUx86Signatures *virCPUx86SignaturesPtr;
a41c76
+struct _virCPUx86Signatures {
a41c76
+    size_t count;
a41c76
+    virCPUx86Signature *items;
a41c76
+};
a41c76
+
a41c76
 typedef struct _virCPUx86Model virCPUx86Model;
a41c76
 typedef virCPUx86Model *virCPUx86ModelPtr;
a41c76
 struct _virCPUx86Model {
a41c76
@@ -128,8 +141,7 @@ struct _virCPUx86Model {
a41c76
     bool decodeHost;
a41c76
     bool decodeGuest;
a41c76
     virCPUx86VendorPtr vendor;
a41c76
-    size_t nsignatures;
a41c76
-    uint32_t *signatures;
a41c76
+    virCPUx86SignaturesPtr signatures;
a41c76
     virCPUx86Data data;
a41c76
 };
a41c76
 
a41c76
@@ -717,6 +729,13 @@ x86MakeSignature(unsigned int family,
a41c76
 }
a41c76
 
a41c76
 
a41c76
+static uint32_t
a41c76
+virCPUx86SignatureToCPUID(virCPUx86Signature *sig)
a41c76
+{
a41c76
+    return x86MakeSignature(sig->family, sig->model, 0);
a41c76
+}
a41c76
+
a41c76
+
a41c76
 static void
a41c76
 virCPUx86SignatureFromCPUID(uint32_t sig,
a41c76
                             unsigned int *family,
a41c76
@@ -1099,41 +1118,65 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
a41c76
 }
a41c76
 
a41c76
 
a41c76
+static virCPUx86SignaturesPtr
a41c76
+virCPUx86SignaturesNew(size_t count)
a41c76
+{
a41c76
+    virCPUx86SignaturesPtr sigs;
a41c76
+
a41c76
+    sigs = g_new0(virCPUx86Signatures, 1);
a41c76
+    sigs->items = g_new0(virCPUx86Signature, count);
a41c76
+    sigs->count = count;
a41c76
+
a41c76
+    return sigs;
a41c76
+}
a41c76
+
a41c76
+
a41c76
 static void
a41c76
-virCPUx86SignaturesFree(uint32_t *signatures)
a41c76
+virCPUx86SignaturesFree(virCPUx86SignaturesPtr sigs)
a41c76
 {
a41c76
-    g_free(signatures);
a41c76
+    if (!sigs)
a41c76
+        return;
a41c76
+
a41c76
+    g_free(sigs->items);
a41c76
+    g_free(sigs);
a41c76
 }
a41c76
 
a41c76
 
a41c76
-static int
a41c76
-virCPUx86SignaturesCopy(virCPUx86ModelPtr dst,
a41c76
-                        virCPUx86ModelPtr src)
a41c76
+static virCPUx86SignaturesPtr
a41c76
+virCPUx86SignaturesCopy(virCPUx86SignaturesPtr src)
a41c76
 {
a41c76
+    virCPUx86SignaturesPtr dst;
a41c76
     size_t i;
a41c76
 
a41c76
-    if (src->nsignatures == 0)
a41c76
-        return 0;
a41c76
+    if (!src || src->count == 0)
a41c76
+        return NULL;
a41c76
 
a41c76
-    if (VIR_ALLOC_N(dst->signatures, src->nsignatures) < 0)
a41c76
-        return -1;
a41c76
+    dst = virCPUx86SignaturesNew(src->count);
a41c76
 
a41c76
-    dst->nsignatures = src->nsignatures;
a41c76
-    for (i = 0; i < src->nsignatures; i++)
a41c76
-        dst->signatures[i] = src->signatures[i];
a41c76
+    for (i = 0; i < src->count; i++)
a41c76
+        dst->items[i] = src->items[i];
a41c76
 
a41c76
-    return 0;
a41c76
+    return dst;
a41c76
 }
a41c76
 
a41c76
 
a41c76
 static bool
a41c76
-virCPUx86SignaturesMatch(virCPUx86ModelPtr model,
a41c76
+virCPUx86SignaturesMatch(virCPUx86SignaturesPtr sigs,
a41c76
                          uint32_t signature)
a41c76
 {
a41c76
     size_t i;
a41c76
+    unsigned int family;
a41c76
+    unsigned int model;
a41c76
+    unsigned int stepping;
a41c76
 
a41c76
-    for (i = 0; i < model->nsignatures; i++) {
a41c76
-        if (model->signatures[i] == signature)
a41c76
+    if (!sigs)
a41c76
+        return false;
a41c76
+
a41c76
+    virCPUx86SignatureFromCPUID(signature, &family, &model, &stepping);
a41c76
+
a41c76
+    for (i = 0; i < sigs->count; i++) {
a41c76
+        if (sigs->items[i].family == family &&
a41c76
+            sigs->items[i].model == model)
a41c76
             return true;
a41c76
     }
a41c76
 
a41c76
@@ -1142,17 +1185,21 @@ virCPUx86SignaturesMatch(virCPUx86ModelPtr model,
a41c76
 
a41c76
 
a41c76
 static char *
a41c76
-virCPUx86SignaturesFormat(virCPUx86ModelPtr model)
a41c76
+virCPUx86SignaturesFormat(virCPUx86SignaturesPtr sigs)
a41c76
 {
a41c76
     virBuffer buf = VIR_BUFFER_INITIALIZER;
a41c76
     size_t i;
a41c76
 
a41c76
-    for (i = 0; i < model->nsignatures; i++) {
a41c76
-        virBufferAsprintf(&buf, "%06lx,",
a41c76
-                          (unsigned long)model->signatures[i]);
a41c76
+    if (!sigs)
a41c76
+        return virBufferContentAndReset(&buf;;
a41c76
+
a41c76
+    for (i = 0; i < sigs->count; i++) {
a41c76
+        virBufferAsprintf(&buf, "(%u,%u,0), ",
a41c76
+                          sigs->items[i].family,
a41c76
+                          sigs->items[i].model);
a41c76
     }
a41c76
 
a41c76
-    virBufferTrim(&buf, ",", -1);
a41c76
+    virBufferTrim(&buf, ", ", -1);
a41c76
 
a41c76
     return virBufferContentAndReset(&buf;;
a41c76
 }
a41c76
@@ -1179,16 +1226,11 @@ x86ModelCopy(virCPUx86ModelPtr model)
a41c76
 
a41c76
     copy = g_new0(virCPUx86Model, 1);
a41c76
     copy->name = g_strdup(model->name);
a41c76
-
a41c76
-    if (virCPUx86SignaturesCopy(copy, model) < 0) {
a41c76
-        x86ModelFree(copy);
a41c76
-        return NULL;
a41c76
-    }
a41c76
+    copy->signatures = virCPUx86SignaturesCopy(model->signatures);
a41c76
     x86DataCopy(&copy->data, &model->data);
a41c76
-
a41c76
     copy->vendor = model->vendor;
a41c76
 
a41c76
-    return copy;
a41c76
+    return g_steal_pointer(©);
a41c76
 }
a41c76
 
a41c76
 
a41c76
@@ -1408,9 +1450,7 @@ x86ModelParseAncestor(virCPUx86ModelPtr model,
a41c76
     }
a41c76
 
a41c76
     model->vendor = ancestor->vendor;
a41c76
-    if (virCPUx86SignaturesCopy(model, ancestor) < 0)
a41c76
-        return -1;
a41c76
-
a41c76
+    model->signatures = virCPUx86SignaturesCopy(ancestor->signatures);
a41c76
     x86DataCopy(&model->data, &ancestor->data);
a41c76
 
a41c76
     return 0;
a41c76
@@ -1432,34 +1472,29 @@ x86ModelParseSignatures(virCPUx86ModelPtr model,
a41c76
     /* Remove inherited signatures. */
a41c76
     virCPUx86SignaturesFree(model->signatures);
a41c76
 
a41c76
-    model->nsignatures = n;
a41c76
-    if (VIR_ALLOC_N(model->signatures, n) < 0)
a41c76
-       return -1;
a41c76
+    model->signatures = virCPUx86SignaturesNew(n);
a41c76
 
a41c76
     for (i = 0; i < n; i++) {
a41c76
-        unsigned int sigFamily = 0;
a41c76
-        unsigned int sigModel = 0;
a41c76
+        virCPUx86Signature *sig = &model->signatures->items[i];
a41c76
         int rc;
a41c76
 
a41c76
         ctxt->node = nodes[i];
a41c76
 
a41c76
-        rc = virXPathUInt("string(@family)", ctxt, &sigFamily);
a41c76
-        if (rc < 0 || sigFamily == 0) {
a41c76
+        rc = virXPathUInt("string(@family)", ctxt, &sig->family);
a41c76
+        if (rc < 0 || sig->family == 0) {
a41c76
             virReportError(VIR_ERR_INTERNAL_ERROR,
a41c76
                            _("Invalid CPU signature family in model %s"),
a41c76
                            model->name);
a41c76
             return -1;
a41c76
         }
a41c76
 
a41c76
-        rc = virXPathUInt("string(@model)", ctxt, &sigModel);
a41c76
+        rc = virXPathUInt("string(@model)", ctxt, &sig->model);
a41c76
         if (rc < 0) {
a41c76
             virReportError(VIR_ERR_INTERNAL_ERROR,
a41c76
                            _("Invalid CPU signature model in model %s"),
a41c76
                            model->name);
a41c76
             return -1;
a41c76
         }
a41c76
-
a41c76
-        model->signatures[i] = x86MakeSignature(sigFamily, sigModel, 0);
a41c76
     }
a41c76
 
a41c76
     ctxt->node = root;
a41c76
@@ -1865,9 +1900,12 @@ x86Compute(virCPUDefPtr host,
a41c76
                                  &host_model->vendor->data) < 0)
a41c76
             return VIR_CPU_COMPARE_ERROR;
a41c76
 
a41c76
-        if (host_model->signatures &&
a41c76
-            x86DataAddSignature(&guest_model->data, *host_model->signatures) < 0)
a41c76
-            return VIR_CPU_COMPARE_ERROR;
a41c76
+        if (host_model->signatures && host_model->signatures->count > 0) {
a41c76
+            virCPUx86Signature *sig = &host_model->signatures->items[0];
a41c76
+            if (x86DataAddSignature(&guest_model->data,
a41c76
+                                    virCPUx86SignatureToCPUID(sig)) < 0)
a41c76
+                return VIR_CPU_COMPARE_ERROR;
a41c76
+        }
a41c76
 
a41c76
         if (cpu->type == VIR_CPU_TYPE_GUEST
a41c76
             && cpu->match == VIR_CPU_MATCH_EXACT)
a41c76
@@ -1977,8 +2015,8 @@ x86DecodeUseCandidate(virCPUx86ModelPtr current,
a41c76
      * consider candidates with matching family/model.
a41c76
      */
a41c76
     if (signature &&
a41c76
-        virCPUx86SignaturesMatch(current, signature) &&
a41c76
-        !virCPUx86SignaturesMatch(candidate, signature)) {
a41c76
+        virCPUx86SignaturesMatch(current->signatures, signature) &&
a41c76
+        !virCPUx86SignaturesMatch(candidate->signatures, signature)) {
a41c76
         VIR_DEBUG("%s differs in signature from matching %s",
a41c76
                   cpuCandidate->model, cpuCurrent->model);
a41c76
         return 0;
a41c76
@@ -1994,8 +2032,8 @@ x86DecodeUseCandidate(virCPUx86ModelPtr current,
a41c76
      * result in longer list of features.
a41c76
      */
a41c76
     if (signature &&
a41c76
-        virCPUx86SignaturesMatch(candidate, signature) &&
a41c76
-        !virCPUx86SignaturesMatch(current, signature)) {
a41c76
+        virCPUx86SignaturesMatch(candidate->signatures, signature) &&
a41c76
+        !virCPUx86SignaturesMatch(current->signatures, signature)) {
a41c76
         VIR_DEBUG("%s provides matching signature", cpuCandidate->model);
a41c76
         return 1;
a41c76
     }
a41c76
@@ -2150,7 +2188,7 @@ x86Decode(virCPUDefPtr cpu,
a41c76
     if (vendor)
a41c76
         cpu->vendor = g_strdup(vendor->name);
a41c76
 
a41c76
-    sigs = virCPUx86SignaturesFormat(model);
a41c76
+    sigs = virCPUx86SignaturesFormat(model->signatures);
a41c76
 
a41c76
     VIR_DEBUG("Using CPU model %s (signatures %s) for CPU with signature %06lx",
a41c76
               model->name, NULLSTR(sigs), (unsigned long)signature);
a41c76
@@ -3046,9 +3084,12 @@ virCPUx86Translate(virCPUDefPtr cpu,
a41c76
         virCPUx86DataAddItem(&model->data, &model->vendor->data) < 0)
a41c76
         return -1;
a41c76
 
a41c76
-    if (model->signatures &&
a41c76
-        x86DataAddSignature(&model->data, model->signatures[0]) < 0)
a41c76
-        return -1;
a41c76
+    if (model->signatures && model->signatures->count > 0) {
a41c76
+        virCPUx86Signature *sig = &model->signatures->items[0];
a41c76
+        if (x86DataAddSignature(&model->data,
a41c76
+                                virCPUx86SignatureToCPUID(sig)) < 0)
a41c76
+            return -1;
a41c76
+    }
a41c76
 
a41c76
     if (!(translated = virCPUDefCopyWithoutModel(cpu)))
a41c76
         return -1;
a41c76
-- 
a41c76
2.26.2
a41c76