#include "cpu.h"
#include "cpu_map.h"
#include "cpu_x86.h"
+#include "buf.h"
#define VIR_FROM_THIS VIR_FROM_CPU
{ data, -1, false }
-static void
-x86DataIteratorInit(struct data_iterator *iter,
- union cpuData *data)
-{
- struct data_iterator init = DATA_ITERATOR_INIT(data);
-
- *iter = init;
-}
-
-
static int
x86cpuidMatch(const struct cpuX86cpuid *cpuid1,
const struct cpuX86cpuid *cpuid2)
}
-static int
-x86cpuidMatchAny(const struct cpuX86cpuid *cpuid,
- const struct cpuX86cpuid *mask)
-{
- return ((cpuid->eax & mask->eax) ||
- (cpuid->ebx & mask->ebx) ||
- (cpuid->ecx & mask->ecx) ||
- (cpuid->edx & mask->edx));
-}
-
-
static void
x86cpuidSetBits(struct cpuX86cpuid *cpuid,
const struct cpuX86cpuid *mask)
}
+static char *
+x86FeatureNames(const struct x86_map *map,
+ const char *separator,
+ union cpuData *data)
+{
+ virBuffer ret = VIR_BUFFER_INITIALIZER;
+ bool first = true;
+
+ struct x86_feature *next_feature = map->features;
+
+ virBufferAdd(&ret, "", 0);
+
+ while (next_feature) {
+ if (x86DataIsSubset(data, next_feature->data)) {
+ if (!first)
+ virBufferAdd(&ret, separator, -1);
+ else
+ first = false;
+
+ virBufferAdd(&ret, next_feature->name, -1);
+ }
+ next_feature = next_feature->next;
+ }
+
+ return virBufferContentAndReset(&ret);
+}
+
+
static int
x86FeatureLoad(xmlXPathContextPtr ctxt,
struct x86_map *map)
}
+/* A helper macro to exit the cpu computation function without writing
+ * redundant code:
+ * MSG: error message
+ * CPU_DEF: a union cpuData pointer with flags that are conflicting
+ * RET: return code to set
+ *
+ * This macro generates the error string outputs it into logs.
+ */
+#define virX86CpuIncompatible(MSG, CPU_DEF) \
+ do { \
+ char *flagsStr = NULL; \
+ if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) \
+ goto no_memory; \
+ if (message && \
+ virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
+ VIR_FREE(flagsStr); \
+ goto no_memory; \
+ } \
+ VIR_DEBUG("%s: %s", MSG, flagsStr); \
+ VIR_FREE(flagsStr); \
+ ret = VIR_CPU_COMPARE_INCOMPATIBLE; \
+ } while (0)
+
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
virCPUDefPtr cpu,
- union cpuData **guest)
+ union cpuData **guest,
+ char **message)
{
struct x86_map *map = NULL;
struct x86_model *host_model = NULL;
struct x86_model *cpu_forbid = NULL;
struct x86_model *diff = NULL;
struct x86_model *guest_model = NULL;
- struct data_iterator iter;
- const struct cpuX86cpuid *cpuid;
virCPUCompareResult ret;
enum compare_result result;
unsigned int i;
if (!found) {
VIR_DEBUG("CPU arch %s does not match host arch", cpu->arch);
+ if (message &&
+ virAsprintf(message,
+ _("CPU arch %s does not match host arch"),
+ cpu->arch) < 0)
+ goto no_memory;
return VIR_CPU_COMPARE_INCOMPATIBLE;
}
}
(!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
cpu->vendor);
+ if (message &&
+ virAsprintf(message,
+ _("host CPU vendor does not match required "
+ "CPU vendor %s"),
+ cpu->vendor) < 0)
+ goto no_memory;
return VIR_CPU_COMPARE_INCOMPATIBLE;
}
!(cpu_forbid = x86ModelFromCPU(cpu, map, VIR_CPU_FEATURE_FORBID)))
goto error;
- x86DataIteratorInit(&iter, cpu_forbid->data);
- while ((cpuid = x86DataCpuidNext(&iter))) {
- const struct cpuX86cpuid *cpuid2;
-
- cpuid2 = x86DataCpuid(host_model->data, cpuid->function);
- if (cpuid2 != NULL && x86cpuidMatchAny(cpuid2, cpuid)) {
- VIR_DEBUG("Host CPU provides forbidden features in CPUID function 0x%x",
- cpuid->function);
- ret = VIR_CPU_COMPARE_INCOMPATIBLE;
- goto out;
- }
+ x86DataIntersect(cpu_forbid->data, host_model->data);
+ if (!x86DataIsEmpty(cpu_forbid->data)) {
+ virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
+ cpu_forbid->data);
+ goto out;
}
x86DataSubtract(cpu_require->data, cpu_disable->data);
result = x86ModelCompare(host_model, cpu_require);
if (result == SUBSET || result == UNRELATED) {
- VIR_DEBUG("Host CPU does not provide all required features");
- ret = VIR_CPU_COMPARE_INCOMPATIBLE;
+ x86DataSubtract(cpu_require->data, host_model->data);
+ virX86CpuIncompatible(N_("Host CPU does not provide required "
+ "features"),
+ cpu_require->data);
goto out;
}
if (ret == VIR_CPU_COMPARE_SUPERSET
&& cpu->type == VIR_CPU_TYPE_GUEST
&& cpu->match == VIR_CPU_MATCH_STRICT) {
- VIR_DEBUG("Host CPU does not strictly match guest CPU");
- ret = VIR_CPU_COMPARE_INCOMPATIBLE;
+ virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
+ "Extra features"),
+ diff->data);
goto out;
}
ret = VIR_CPU_COMPARE_ERROR;
goto out;
}
+#undef virX86CpuIncompatible
static virCPUCompareResult
x86Compare(virCPUDefPtr host,
virCPUDefPtr cpu)
{
- return x86Compute(host, cpu, NULL);
+ return x86Compute(host, cpu, NULL, NULL);
}
static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
virCPUDefPtr guest,
- union cpuData **data)
+ union cpuData **data,
+ char **message)
{
- return x86Compute(host, guest, data);
+ return x86Compute(host, guest, data, message);
}