]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
cpu_x86: Implement virCPUDataIsIdentical for x86
authorTim Wiederhake <twiederh@redhat.com>
Wed, 6 Oct 2021 13:16:11 +0000 (15:16 +0200)
committerTim Wiederhake <twiederh@redhat.com>
Fri, 5 Nov 2021 16:12:25 +0000 (17:12 +0100)
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/cpu/cpu_x86.c

index 4b2a0ef1f93f8a05aa11e6399d75223670176171..02766ca97911f04141bde0020e35e1b8e273df88 100644 (file)
@@ -3330,6 +3330,67 @@ virCPUx86DataAddFeature(virCPUData *cpuData,
 }
 
 
+static bool
+virCPUx86DataItemIsIdentical(const virCPUx86DataItem *a,
+                             const virCPUx86DataItem *b)
+{
+    if (a->type != b->type)
+        return false;
+
+    switch (a->type) {
+    case VIR_CPU_X86_DATA_NONE:
+        break;
+
+    case VIR_CPU_X86_DATA_CPUID:
+        return memcmp(&a->data.cpuid, &b->data.cpuid, sizeof(a->data.cpuid)) == 0;
+
+    case VIR_CPU_X86_DATA_MSR:
+        return memcmp(&a->data.msr, &b->data.msr, sizeof(a->data.msr)) == 0;
+    }
+
+    return true;
+}
+
+static virCPUCompareResult
+virCPUx86DataIsIdentical(const virCPUData *a,
+                         const virCPUData *b)
+{
+    const virCPUx86Data *adata;
+    const virCPUx86Data *bdata;
+    size_t i;
+    size_t j;
+
+    if (!a || !b)
+        return VIR_CPU_COMPARE_ERROR;
+
+    if (a->arch != b->arch)
+        return VIR_CPU_COMPARE_INCOMPATIBLE;
+
+    if (!((adata = &a->data.x86) && (bdata = &b->data.x86)))
+        return VIR_CPU_COMPARE_ERROR;
+
+    if (adata->len != bdata->len)
+        return VIR_CPU_COMPARE_INCOMPATIBLE;
+
+    for (i = 0; i < adata->len; ++i) {
+        bool found = false;
+
+        for (j = 0; j < bdata->len; ++j) {
+            if (!virCPUx86DataItemIsIdentical(&adata->items[i],
+                                              &bdata->items[j]))
+                continue;
+
+            found = true;
+            break;
+        }
+
+        if (!found)
+            return VIR_CPU_COMPARE_INCOMPATIBLE;
+    }
+
+    return VIR_CPU_COMPARE_IDENTICAL;
+}
+
 static bool
 virCPUx86FeatureIsMSR(const char *name)
 {
@@ -3413,4 +3474,5 @@ struct cpuArchDriver cpuDriverX86 = {
     .copyMigratable = virCPUx86CopyMigratable,
     .validateFeatures = virCPUx86ValidateFeatures,
     .dataAddFeature = virCPUx86DataAddFeature,
+    .dataIsIdentical = virCPUx86DataIsIdentical,
 };