cpu_x86.c 57.1 KB
Newer Older
J
Jiri Denemark 已提交
1 2 3
/*
 * cpu_x86.c: CPU driver for CPUs with x86 compatible CPUID instruction
 *
4
 * Copyright (C) 2009-2014 Red Hat, Inc.
J
Jiri Denemark 已提交
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
J
Jiri Denemark 已提交
19 20 21 22 23 24 25 26 27
 *
 * Authors:
 *      Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

#include <stdint.h>

28
#include "virlog.h"
29
#include "viralloc.h"
J
Jiri Denemark 已提交
30 31 32
#include "cpu.h"
#include "cpu_map.h"
#include "cpu_x86.h"
33
#include "virbuffer.h"
E
Eric Blake 已提交
34
#include "virendian.h"
35
#include "virstring.h"
J
Jiri Denemark 已提交
36 37 38

#define VIR_FROM_THIS VIR_FROM_CPU

39 40
VIR_LOG_INIT("cpu.cpu_x86");

J
Jiri Denemark 已提交
41 42
#define VENDOR_STRING_LENGTH    12

43
static const virCPUx86CPUID cpuidNull = { 0, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
44

45
static const virArch archs[] = { VIR_ARCH_I686, VIR_ARCH_X86_64 };
J
Jiri Denemark 已提交
46

47 48 49
typedef struct _virCPUx86Vendor virCPUx86Vendor;
typedef virCPUx86Vendor *virCPUx86VendorPtr;
struct _virCPUx86Vendor {
J
Jiri Denemark 已提交
50
    char *name;
51
    virCPUx86CPUID cpuid;
J
Jiri Denemark 已提交
52 53
};

54 55 56
typedef struct _virCPUx86Feature virCPUx86Feature;
typedef virCPUx86Feature *virCPUx86FeaturePtr;
struct _virCPUx86Feature {
J
Jiri Denemark 已提交
57
    char *name;
58
    virCPUx86Data *data;
J
Jiri Denemark 已提交
59

60
    virCPUx86FeaturePtr next;
J
Jiri Denemark 已提交
61 62
};

63 64 65
typedef struct _virCPUx86KVMFeature virCPUx86KVMFeature;
typedef virCPUx86KVMFeature *virCPUx86KVMFeaturePtr;
struct _virCPUx86KVMFeature {
66 67 68 69
    const char *name;
    const virCPUx86CPUID cpuid;
};

70
static const virCPUx86KVMFeature x86_kvm_features[] =
71 72 73 74 75 76 77 78 79 80 81
{
    {VIR_CPU_x86_KVM_CLOCKSOURCE,  { .function = 0x40000001, .eax = 0x00000001 }},
    {VIR_CPU_x86_KVM_NOP_IO_DELAY, { .function = 0x40000001, .eax = 0x00000002 }},
    {VIR_CPU_x86_KVM_MMU_OP,       { .function = 0x40000001, .eax = 0x00000004 }},
    {VIR_CPU_x86_KVM_CLOCKSOURCE2, { .function = 0x40000001, .eax = 0x00000008 }},
    {VIR_CPU_x86_KVM_ASYNC_PF,     { .function = 0x40000001, .eax = 0x00000010 }},
    {VIR_CPU_x86_KVM_STEAL_TIME,   { .function = 0x40000001, .eax = 0x00000020 }},
    {VIR_CPU_x86_KVM_PV_EOI,       { .function = 0x40000001, .eax = 0x00000040 }},
    {VIR_CPU_x86_KVM_PV_UNHALT,    { .function = 0x40000001, .eax = 0x00000080 }},
    {VIR_CPU_x86_KVM_CLOCKSOURCE_STABLE_BIT,
                                   { .function = 0x40000001, .eax = 0x01000000 }},
82 83 84 85 86 87 88 89
    {VIR_CPU_x86_KVM_HV_RUNTIME,   { .function = 0x40000003, .eax = 0x00000001 }},
    {VIR_CPU_x86_KVM_HV_SYNIC,     { .function = 0x40000003, .eax = 0x00000004 }},
    {VIR_CPU_x86_KVM_HV_STIMER,    { .function = 0x40000003, .eax = 0x00000008 }},
    {VIR_CPU_x86_KVM_HV_RELAXED,   { .function = 0x40000003, .eax = 0x00000020 }},
    {VIR_CPU_x86_KVM_HV_SPINLOCK,  { .function = 0x40000003, .eax = 0x00000022 }},
    {VIR_CPU_x86_KVM_HV_VAPIC,     { .function = 0x40000003, .eax = 0x00000030 }},
    {VIR_CPU_x86_KVM_HV_VPINDEX,   { .function = 0x40000003, .eax = 0x00000040 }},
    {VIR_CPU_x86_KVM_HV_RESET,     { .function = 0x40000003, .eax = 0x00000080 }},
90 91
};

J
Jiri Denemark 已提交
92 93 94
typedef struct _virCPUx86Model virCPUx86Model;
typedef virCPUx86Model *virCPUx86ModelPtr;
struct _virCPUx86Model {
J
Jiri Denemark 已提交
95
    char *name;
96
    virCPUx86VendorPtr vendor;
97
    virCPUx86Data *data;
J
Jiri Denemark 已提交
98 99
};

J
Jiri Denemark 已提交
100 101 102
typedef struct _virCPUx86Map virCPUx86Map;
typedef virCPUx86Map *virCPUx86MapPtr;
struct _virCPUx86Map {
103 104
    size_t nvendors;
    virCPUx86VendorPtr *vendors;
105
    virCPUx86FeaturePtr features;
106 107
    size_t nmodels;
    virCPUx86ModelPtr *models;
108
    virCPUx86FeaturePtr migrate_blockers;
J
Jiri Denemark 已提交
109 110
};

J
Jiri Denemark 已提交
111
static virCPUx86MapPtr cpuMap;
112 113 114
int virCPUx86MapOnceInit(void);
VIR_ONCE_GLOBAL_INIT(virCPUx86Map);

J
Jiri Denemark 已提交
115

116
typedef enum {
J
Jiri Denemark 已提交
117 118 119 120
    SUBSET,
    EQUAL,
    SUPERSET,
    UNRELATED
121
} virCPUx86CompareResult;
J
Jiri Denemark 已提交
122 123


124 125 126
typedef struct _virCPUx86DataIterator virCPUx86DataIterator;
typedef virCPUx86DataIterator *virCPUx86DataIteratorPtr;
struct _virCPUx86DataIterator {
127
    const virCPUx86Data *data;
J
Jiri Denemark 已提交
128 129
    int pos;
};
J
Jiri Denemark 已提交
130 131


132
#define virCPUx86DataIteratorInit(data) \
133
    { data, -1 }
J
Jiri Denemark 已提交
134 135


136
static bool
137 138
x86cpuidMatch(const virCPUx86CPUID *cpuid1,
              const virCPUx86CPUID *cpuid2)
J
Jiri Denemark 已提交
139 140 141 142 143 144 145 146
{
    return (cpuid1->eax == cpuid2->eax &&
            cpuid1->ebx == cpuid2->ebx &&
            cpuid1->ecx == cpuid2->ecx &&
            cpuid1->edx == cpuid2->edx);
}


147
static bool
148 149
x86cpuidMatchMasked(const virCPUx86CPUID *cpuid,
                    const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
150 151 152 153 154 155 156 157
{
    return ((cpuid->eax & mask->eax) == mask->eax &&
            (cpuid->ebx & mask->ebx) == mask->ebx &&
            (cpuid->ecx & mask->ecx) == mask->ecx &&
            (cpuid->edx & mask->edx) == mask->edx);
}


J
Jiri Denemark 已提交
158
static void
159 160
x86cpuidSetBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
161
{
162 163 164
    if (!mask)
        return;

J
Jiri Denemark 已提交
165 166 167 168 169 170 171
    cpuid->eax |= mask->eax;
    cpuid->ebx |= mask->ebx;
    cpuid->ecx |= mask->ecx;
    cpuid->edx |= mask->edx;
}


J
Jiri Denemark 已提交
172
static void
173 174
x86cpuidClearBits(virCPUx86CPUID *cpuid,
                  const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
175
{
176 177 178
    if (!mask)
        return;

J
Jiri Denemark 已提交
179 180 181 182 183 184 185
    cpuid->eax &= ~mask->eax;
    cpuid->ebx &= ~mask->ebx;
    cpuid->ecx &= ~mask->ecx;
    cpuid->edx &= ~mask->edx;
}


J
Jiri Denemark 已提交
186
static void
187 188
x86cpuidAndBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
189
{
190 191 192
    if (!mask)
        return;

193 194 195 196 197 198
    cpuid->eax &= mask->eax;
    cpuid->ebx &= mask->ebx;
    cpuid->ecx &= mask->ecx;
    cpuid->edx &= mask->edx;
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212
static int
virCPUx86CPUIDSorter(const void *a, const void *b)
{
    virCPUx86CPUID *da = (virCPUx86CPUID *) a;
    virCPUx86CPUID *db = (virCPUx86CPUID *) b;

    if (da->function > db->function)
        return 1;
    else if (da->function < db->function)
        return -1;

    return 0;
}

213

J
Jiri Denemark 已提交
214
/* skips all zero CPUID leafs */
215
static virCPUx86CPUID *
216
x86DataCpuidNext(virCPUx86DataIteratorPtr iterator)
J
Jiri Denemark 已提交
217
{
218
    const virCPUx86Data *data = iterator->data;
J
Jiri Denemark 已提交
219

220
    if (!data)
J
Jiri Denemark 已提交
221 222
        return NULL;

223 224 225 226
    while (++iterator->pos < data->len) {
        if (!x86cpuidMatch(data->data + iterator->pos, &cpuidNull))
            return data->data + iterator->pos;
    }
J
Jiri Denemark 已提交
227

228
    return NULL;
J
Jiri Denemark 已提交
229 230 231
}


232
static virCPUx86CPUID *
233
x86DataCpuid(const virCPUx86Data *data,
J
Jiri Denemark 已提交
234 235
             uint32_t function)
{
236
    size_t i;
J
Jiri Denemark 已提交
237

238 239 240
    for (i = 0; i < data->len; i++) {
        if (data->data[i].function == function)
            return data->data + i;
J
Jiri Denemark 已提交
241 242
    }

243
    return NULL;
J
Jiri Denemark 已提交
244 245
}

J
Jiri Denemark 已提交
246
void
247
virCPUx86DataFree(virCPUx86Data *data)
J
Jiri Denemark 已提交
248
{
249
    if (!data)
J
Jiri Denemark 已提交
250 251
        return;

252
    VIR_FREE(data->data);
J
Jiri Denemark 已提交
253 254 255 256
    VIR_FREE(data);
}


J
Jiri Denemark 已提交
257
virCPUDataPtr
258
virCPUx86MakeData(virArch arch, virCPUx86Data **data)
259
{
260
    virCPUDataPtr cpuData;
261 262 263 264

    if (VIR_ALLOC(cpuData) < 0)
        return NULL;

J
Jiri Denemark 已提交
265 266
    cpuData->arch = arch;
    cpuData->data.x86 = *data;
267 268 269 270 271 272
    *data = NULL;

    return cpuData;
}

static void
273
x86FreeCPUData(virCPUDataPtr data)
274 275 276 277
{
    if (!data)
        return;

278
    virCPUx86DataFree(data->data.x86);
279 280 281 282
    VIR_FREE(data);
}


283 284
static virCPUx86Data *
x86DataCopy(const virCPUx86Data *data)
J
Jiri Denemark 已提交
285
{
286
    virCPUx86Data *copy = NULL;
287
    size_t i;
J
Jiri Denemark 已提交
288

289 290
    if (VIR_ALLOC(copy) < 0 ||
        VIR_ALLOC_N(copy->data, data->len) < 0) {
291
        virCPUx86DataFree(copy);
J
Jiri Denemark 已提交
292 293 294
        return NULL;
    }

295 296 297
    copy->len = data->len;
    for (i = 0; i < data->len; i++)
        copy->data[i] = data->data[i];
J
Jiri Denemark 已提交
298 299 300 301 302

    return copy;
}


J
Jiri Denemark 已提交
303
int
304 305
virCPUx86DataAddCPUID(virCPUx86Data *data,
                      const virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
306
{
307
    virCPUx86CPUID *existing;
J
Jiri Denemark 已提交
308

309 310 311 312 313 314
    if ((existing = x86DataCpuid(data, cpuid->function))) {
        x86cpuidSetBits(existing, cpuid);
    } else {
        if (VIR_APPEND_ELEMENT_COPY(data->data, data->len,
                                    *((virCPUx86CPUID *)cpuid)) < 0)
            return -1;
J
Jiri Denemark 已提交
315

316 317 318
        qsort(data->data, data->len,
              sizeof(virCPUx86CPUID), virCPUx86CPUIDSorter);
    }
J
Jiri Denemark 已提交
319

J
Jiri Denemark 已提交
320 321 322 323 324
    return 0;
}


static int
325 326
x86DataAdd(virCPUx86Data *data1,
           const virCPUx86Data *data2)
J
Jiri Denemark 已提交
327
{
328
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data2);
329 330
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
331

332 333
    while ((cpuid2 = x86DataCpuidNext(&iter))) {
        cpuid1 = x86DataCpuid(data1, cpuid2->function);
J
Jiri Denemark 已提交
334

335 336 337 338 339 340
        if (cpuid1) {
            x86cpuidSetBits(cpuid1, cpuid2);
        } else {
            if (virCPUx86DataAddCPUID(data1, cpuid2) < 0)
                return -1;
        }
J
Jiri Denemark 已提交
341
    }
J
Jiri Denemark 已提交
342 343 344 345 346

    return 0;
}


347
static void
348 349
x86DataSubtract(virCPUx86Data *data1,
                const virCPUx86Data *data2)
350
{
351
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
352 353
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
354

355 356 357
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
        cpuid2 = x86DataCpuid(data2, cpuid1->function);
        x86cpuidClearBits(cpuid1, cpuid2);
358 359 360 361
    }
}


J
Jiri Denemark 已提交
362
static void
363 364
x86DataIntersect(virCPUx86Data *data1,
                 const virCPUx86Data *data2)
365
{
366
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
367 368
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
369

J
Jiri Denemark 已提交
370 371 372 373 374 375
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
        cpuid2 = x86DataCpuid(data2, cpuid1->function);
        if (cpuid2)
            x86cpuidAndBits(cpuid1, cpuid2);
        else
            x86cpuidClearBits(cpuid1, cpuid1);
376 377 378 379
    }
}


J
Jiri Denemark 已提交
380
static bool
381
x86DataIsEmpty(virCPUx86Data *data)
J
Jiri Denemark 已提交
382
{
383
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data);
J
Jiri Denemark 已提交
384

385
    return !x86DataCpuidNext(&iter);
J
Jiri Denemark 已提交
386
}
J
Jiri Denemark 已提交
387 388


J
Jiri Denemark 已提交
389
static bool
390 391
x86DataIsSubset(const virCPUx86Data *data,
                const virCPUx86Data *subset)
J
Jiri Denemark 已提交
392 393
{

394
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit((virCPUx86Data *)subset);
395 396
    const virCPUx86CPUID *cpuid;
    const virCPUx86CPUID *cpuidSubset;
J
Jiri Denemark 已提交
397

J
Jiri Denemark 已提交
398 399 400 401
    while ((cpuidSubset = x86DataCpuidNext(&iter))) {
        if (!(cpuid = x86DataCpuid(data, cpuidSubset->function)) ||
            !x86cpuidMatchMasked(cpuid, cpuidSubset))
            return false;
J
Jiri Denemark 已提交
402 403
    }

J
Jiri Denemark 已提交
404
    return true;
J
Jiri Denemark 已提交
405 406 407
}


408 409 410 411
/* also removes all detected features from data */
static int
x86DataToCPUFeatures(virCPUDefPtr cpu,
                     int policy,
412
                     virCPUx86Data *data,
J
Jiri Denemark 已提交
413
                     virCPUx86MapPtr map)
414
{
415
    virCPUx86FeaturePtr feature = map->features;
416

417
    while (feature) {
J
Jiri Denemark 已提交
418 419 420 421
        if (x86DataIsSubset(data, feature->data)) {
            x86DataSubtract(data, feature->data);
            if (virCPUDefAddFeature(cpu, feature->name, policy) < 0)
                return -1;
422 423 424 425 426 427 428 429
        }
        feature = feature->next;
    }

    return 0;
}


J
Jiri Denemark 已提交
430
/* also removes bits corresponding to vendor string from data */
431
static virCPUx86VendorPtr
J
Jiri Denemark 已提交
432
x86DataToVendor(const virCPUx86Data *data,
J
Jiri Denemark 已提交
433
                virCPUx86MapPtr map)
J
Jiri Denemark 已提交
434
{
435
    virCPUx86CPUID *cpuid;
436
    size_t i;
J
Jiri Denemark 已提交
437

438 439
    for (i = 0; i < map->nvendors; i++) {
        virCPUx86VendorPtr vendor = map->vendors[i];
J
Jiri Denemark 已提交
440 441 442 443 444 445 446 447 448 449 450
        if ((cpuid = x86DataCpuid(data, vendor->cpuid.function)) &&
            x86cpuidMatchMasked(cpuid, &vendor->cpuid)) {
            x86cpuidClearBits(cpuid, &vendor->cpuid);
            return vendor;
        }
    }

    return NULL;
}


451
static virCPUDefPtr
452
x86DataToCPU(const virCPUx86Data *data,
J
Jiri Denemark 已提交
453
             virCPUx86ModelPtr model,
J
Jiri Denemark 已提交
454
             virCPUx86MapPtr map)
455 456
{
    virCPUDefPtr cpu;
457 458
    virCPUx86Data *copy = NULL;
    virCPUx86Data *modelData = NULL;
459
    virCPUx86VendorPtr vendor;
460 461

    if (VIR_ALLOC(cpu) < 0 ||
462
        VIR_STRDUP(cpu->model, model->name) < 0 ||
463
        !(copy = x86DataCopy(data)) ||
J
Jiri Denemark 已提交
464
        !(modelData = x86DataCopy(model->data)))
465
        goto error;
466

J
Jiri Denemark 已提交
467
    if ((vendor = x86DataToVendor(copy, map)) &&
468
        VIR_STRDUP(cpu->vendor, vendor->name) < 0)
469
        goto error;
J
Jiri Denemark 已提交
470

471 472 473 474 475
    x86DataSubtract(copy, modelData);
    x86DataSubtract(modelData, data);

    /* because feature policy is ignored for host CPU */
    cpu->type = VIR_CPU_TYPE_GUEST;
476

477 478
    if (x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_REQUIRE, copy, map) ||
        x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_DISABLE, modelData, map))
479
        goto error;
480

481
 cleanup:
482 483
    virCPUx86DataFree(modelData);
    virCPUx86DataFree(copy);
484 485
    return cpu;

486
 error:
487 488 489 490 491 492
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


J
Jiri Denemark 已提交
493
static void
494
x86VendorFree(virCPUx86VendorPtr vendor)
J
Jiri Denemark 已提交
495 496 497 498 499 500
{
    if (!vendor)
        return;

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
J
Jiri Denemark 已提交
501
}
J
Jiri Denemark 已提交
502 503


504
static virCPUx86VendorPtr
J
Jiri Denemark 已提交
505
x86VendorFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
506 507
              const char *name)
{
508
    size_t i;
J
Jiri Denemark 已提交
509

510 511 512
    for (i = 0; i < map->nvendors; i++) {
        if (STREQ(map->vendors[i]->name, name))
            return map->vendors[i];
J
Jiri Denemark 已提交
513 514 515 516 517 518 519 520
    }

    return NULL;
}


static int
x86VendorLoad(xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
521
              virCPUx86MapPtr map)
J
Jiri Denemark 已提交
522
{
523
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
524
    char *string = NULL;
525
    int ret = -1;
J
Jiri Denemark 已提交
526 527

    if (VIR_ALLOC(vendor) < 0)
528
        goto cleanup;
J
Jiri Denemark 已提交
529 530 531

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
532 533
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing CPU vendor name"));
534
        goto cleanup;
J
Jiri Denemark 已提交
535 536 537
    }

    if (x86VendorFind(map, vendor->name)) {
538 539
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU vendor %s already defined"), vendor->name);
540
        goto cleanup;
J
Jiri Denemark 已提交
541 542 543 544
    }

    string = virXPathString("string(@string)", ctxt);
    if (!string) {
545
        virReportError(VIR_ERR_INTERNAL_ERROR,
546 547
                       _("Missing vendor string for CPU vendor %s"),
                       vendor->name);
548
        goto cleanup;
J
Jiri Denemark 已提交
549 550
    }
    if (strlen(string) != VENDOR_STRING_LENGTH) {
551 552
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid CPU vendor string '%s'"), string);
553
        goto cleanup;
J
Jiri Denemark 已提交
554 555 556
    }

    vendor->cpuid.function = 0;
E
Eric Blake 已提交
557 558 559 560
    vendor->cpuid.ebx = virReadBufInt32LE(string);
    vendor->cpuid.edx = virReadBufInt32LE(string + 4);
    vendor->cpuid.ecx = virReadBufInt32LE(string + 8);

561 562
    if (VIR_APPEND_ELEMENT(map->vendors, map->nvendors, vendor) < 0)
        goto cleanup;
563 564

    ret = 0;
J
Jiri Denemark 已提交
565

J
Jiri Denemark 已提交
566
 cleanup:
J
Jiri Denemark 已提交
567 568
    VIR_FREE(string);
    x86VendorFree(vendor);
569
    return ret;
J
Jiri Denemark 已提交
570 571 572
}


573
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
574 575
x86FeatureNew(void)
{
576
    virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589

    if (VIR_ALLOC(feature) < 0)
        return NULL;

    if (VIR_ALLOC(feature->data) < 0) {
        VIR_FREE(feature);
        return NULL;
    }

    return feature;
}


J
Jiri Denemark 已提交
590
static void
591
x86FeatureFree(virCPUx86FeaturePtr feature)
J
Jiri Denemark 已提交
592
{
593
    if (!feature)
J
Jiri Denemark 已提交
594 595 596
        return;

    VIR_FREE(feature->name);
597
    virCPUx86DataFree(feature->data);
J
Jiri Denemark 已提交
598 599 600 601
    VIR_FREE(feature);
}


602 603
static virCPUx86FeaturePtr
x86FeatureCopy(virCPUx86FeaturePtr src)
604
{
605
    virCPUx86FeaturePtr feature;
606 607 608 609 610 611 612

    if (VIR_ALLOC(feature) < 0)
        return NULL;

    if (VIR_STRDUP(feature->name, src->name) < 0)
        goto error;

613
    if (!(feature->data = x86DataCopy(src->data)))
614 615 616 617 618 619 620 621 622 623
        goto error;

    return feature;

 error:
    x86FeatureFree(feature);
    return NULL;
}


624
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
625
x86FeatureFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
626 627
               const char *name)
{
628
    virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
629 630

    feature = map->features;
631
    while (feature) {
J
Jiri Denemark 已提交
632 633 634 635 636 637 638 639 640 641
        if (STREQ(feature->name, name))
            return feature;

        feature = feature->next;
    }

    return NULL;
}


642
static char *
J
Jiri Denemark 已提交
643
x86FeatureNames(virCPUx86MapPtr map,
644
                const char *separator,
645
                virCPUx86Data *data)
646 647 648 649
{
    virBuffer ret = VIR_BUFFER_INITIALIZER;
    bool first = true;

650
    virCPUx86FeaturePtr next_feature = map->features;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

    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);
}


670 671
static int
x86ParseCPUID(xmlXPathContextPtr ctxt,
672
              virCPUx86CPUID *cpuid)
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
{
    unsigned long fun, eax, ebx, ecx, edx;
    int ret_fun, ret_eax, ret_ebx, ret_ecx, ret_edx;

    memset(cpuid, 0, sizeof(*cpuid));

    fun = eax = ebx = ecx = edx = 0;
    ret_fun = virXPathULongHex("string(@function)", ctxt, &fun);
    ret_eax = virXPathULongHex("string(@eax)", ctxt, &eax);
    ret_ebx = virXPathULongHex("string(@ebx)", ctxt, &ebx);
    ret_ecx = virXPathULongHex("string(@ecx)", ctxt, &ecx);
    ret_edx = virXPathULongHex("string(@edx)", ctxt, &edx);

    if (ret_fun < 0 || ret_eax == -2 || ret_ebx == -2
        || ret_ecx == -2 || ret_edx == -2)
        return -1;

    cpuid->function = fun;
    cpuid->eax = eax;
    cpuid->ebx = ebx;
    cpuid->ecx = ecx;
    cpuid->edx = edx;
    return 0;
}


J
Jiri Denemark 已提交
699 700
static int
x86FeatureLoad(xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
701
               virCPUx86MapPtr map)
J
Jiri Denemark 已提交
702 703
{
    xmlNodePtr *nodes = NULL;
704
    xmlNodePtr ctxt_node = ctxt->node;
705
    virCPUx86FeaturePtr feature;
706
    virCPUx86CPUID cpuid;
707
    int ret = -1;
708
    size_t i;
J
Jiri Denemark 已提交
709
    int n;
710 711
    char *str = NULL;
    bool migratable = true;
J
Jiri Denemark 已提交
712

J
Jiri Denemark 已提交
713
    if (!(feature = x86FeatureNew()))
714
        goto cleanup;
J
Jiri Denemark 已提交
715

716
    feature->name = virXPathString("string(@name)", ctxt);
717
    if (!feature->name) {
718 719
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU feature name"));
720
        goto cleanup;
J
Jiri Denemark 已提交
721 722 723
    }

    if (x86FeatureFind(map, feature->name)) {
724 725
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature %s already defined"), feature->name);
726
        goto cleanup;
J
Jiri Denemark 已提交
727 728
    }

729 730 731 732
    str = virXPathString("string(@migratable)", ctxt);
    if (STREQ_NULLABLE(str, "no"))
        migratable = false;

733
    n = virXPathNodeSet("./cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
734
    if (n < 0)
735
        goto cleanup;
J
Jiri Denemark 已提交
736 737 738

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
739
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
740
            virReportError(VIR_ERR_INTERNAL_ERROR,
741 742
                           _("Invalid cpuid[%zu] in %s feature"),
                           i, feature->name);
743
            goto cleanup;
J
Jiri Denemark 已提交
744
        }
745
        if (virCPUx86DataAddCPUID(feature->data, &cpuid))
746
            goto cleanup;
J
Jiri Denemark 已提交
747 748
    }

749
    if (!migratable) {
750
        virCPUx86FeaturePtr blocker;
751

752 753 754 755 756
        if (!(blocker = x86FeatureCopy(feature)))
            goto cleanup;

        blocker->next = map->migrate_blockers;
        map->migrate_blockers = blocker;
757 758
    }

759 760
    feature->next = map->features;
    map->features = feature;
761 762 763
    feature = NULL;

    ret = 0;
J
Jiri Denemark 已提交
764

J
Jiri Denemark 已提交
765
 cleanup:
766
    x86FeatureFree(feature);
767 768
    ctxt->node = ctxt_node;
    VIR_FREE(nodes);
769
    VIR_FREE(str);
770

J
Jiri Denemark 已提交
771 772 773 774
    return ret;
}


775 776
static virCPUx86Data *
x86DataFromCPUFeatures(virCPUDefPtr cpu,
J
Jiri Denemark 已提交
777
                       virCPUx86MapPtr map)
778 779 780 781 782 783 784 785
{
    virCPUx86Data *data;
    size_t i;

    if (VIR_ALLOC(data) < 0)
        return NULL;

    for (i = 0; i < cpu->nfeatures; i++) {
786
        virCPUx86FeaturePtr feature;
787 788 789 790 791 792 793 794 795 796 797 798
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
            goto error;
        }

        if (x86DataAdd(data, feature->data) < 0)
            goto error;
    }

    return data;

799
 error:
800 801 802 803 804
    virCPUx86DataFree(data);
    return NULL;
}


J
Jiri Denemark 已提交
805
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
806 807
x86ModelNew(void)
{
J
Jiri Denemark 已提交
808
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821

    if (VIR_ALLOC(model) < 0)
        return NULL;

    if (VIR_ALLOC(model->data) < 0) {
        VIR_FREE(model);
        return NULL;
    }

    return model;
}


J
Jiri Denemark 已提交
822
static void
J
Jiri Denemark 已提交
823
x86ModelFree(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
824
{
825
    if (!model)
J
Jiri Denemark 已提交
826 827 828
        return;

    VIR_FREE(model->name);
829
    virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
830 831 832 833
    VIR_FREE(model);
}


J
Jiri Denemark 已提交
834 835
static virCPUx86ModelPtr
x86ModelCopy(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
836
{
J
Jiri Denemark 已提交
837
    virCPUx86ModelPtr copy;
J
Jiri Denemark 已提交
838

839 840 841
    if (VIR_ALLOC(copy) < 0 ||
        VIR_STRDUP(copy->name, model->name) < 0 ||
        !(copy->data = x86DataCopy(model->data))) {
J
Jiri Denemark 已提交
842 843 844 845
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
846
    copy->vendor = model->vendor;
J
Jiri Denemark 已提交
847 848 849 850 851

    return copy;
}


J
Jiri Denemark 已提交
852
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
853
x86ModelFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
854 855
             const char *name)
{
856
    size_t i;
J
Jiri Denemark 已提交
857

858 859 860
    for (i = 0; i < map->nmodels; i++) {
        if (STREQ(map->models[i]->name, name))
            return map->models[i];
J
Jiri Denemark 已提交
861 862 863 864 865 866
    }

    return NULL;
}


J
Jiri Denemark 已提交
867
static virCPUx86ModelPtr
868
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
869
                virCPUx86MapPtr map,
J
Jiri Denemark 已提交
870 871
                int policy)
{
J
Jiri Denemark 已提交
872
    virCPUx86ModelPtr model = NULL;
873
    size_t i;
J
Jiri Denemark 已提交
874

875
    if (policy == VIR_CPU_FEATURE_REQUIRE) {
876
        if (!(model = x86ModelFind(map, cpu->model))) {
877 878
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU model %s"), cpu->model);
J
Jiri Denemark 已提交
879 880 881
            goto error;
        }

882
        if (!(model = x86ModelCopy(model)))
883
            goto error;
J
Jiri Denemark 已提交
884
    } else if (!(model = x86ModelNew())) {
885
        goto error;
J
Jiri Denemark 已提交
886
    } else if (cpu->type == VIR_CPU_TYPE_HOST) {
887
        return model;
J
Jiri Denemark 已提交
888
    }
J
Jiri Denemark 已提交
889 890

    for (i = 0; i < cpu->nfeatures; i++) {
891
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
892 893 894 895 896

        if (cpu->type == VIR_CPU_TYPE_GUEST
            && cpu->features[i].policy != policy)
            continue;

897
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
898 899
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
J
Jiri Denemark 已提交
900 901 902
            goto error;
        }

J
Jiri Denemark 已提交
903
        if (x86DataAdd(model->data, feature->data))
904
            goto error;
J
Jiri Denemark 已提交
905 906 907 908
    }

    return model;

909
 error:
J
Jiri Denemark 已提交
910 911 912 913 914
    x86ModelFree(model);
    return NULL;
}


915
static int
J
Jiri Denemark 已提交
916
x86ModelSubtractCPU(virCPUx86ModelPtr model,
917
                    const virCPUDef *cpu,
J
Jiri Denemark 已提交
918
                    virCPUx86MapPtr map)
919
{
J
Jiri Denemark 已提交
920
    virCPUx86ModelPtr cpu_model;
921
    size_t i;
922 923

    if (!(cpu_model = x86ModelFind(map, cpu->model))) {
924 925 926
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU model %s"),
                       cpu->model);
927 928 929
        return -1;
    }

J
Jiri Denemark 已提交
930
    x86DataSubtract(model->data, cpu_model->data);
931 932

    for (i = 0; i < cpu->nfeatures; i++) {
933
        virCPUx86FeaturePtr feature;
934 935

        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
936 937 938
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"),
                           cpu->features[i].name);
939 940 941
            return -1;
        }

J
Jiri Denemark 已提交
942
        x86DataSubtract(model->data, feature->data);
943 944 945 946 947 948
    }

    return 0;
}


949
static virCPUx86CompareResult
J
Jiri Denemark 已提交
950 951
x86ModelCompare(virCPUx86ModelPtr model1,
                virCPUx86ModelPtr model2)
J
Jiri Denemark 已提交
952
{
953
    virCPUx86CompareResult result = EQUAL;
954 955
    virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(model1->data);
    virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(model2->data);
956 957
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
958

J
Jiri Denemark 已提交
959
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
960
        virCPUx86CompareResult match = SUPERSET;
J
Jiri Denemark 已提交
961

J
Jiri Denemark 已提交
962
        if ((cpuid2 = x86DataCpuid(model2->data, cpuid1->function))) {
J
Jiri Denemark 已提交
963 964 965 966 967 968 969 970 971 972 973 974
            if (x86cpuidMatch(cpuid1, cpuid2))
                continue;
            else if (!x86cpuidMatchMasked(cpuid1, cpuid2))
                match = SUBSET;
        }

        if (result == EQUAL)
            result = match;
        else if (result != match)
            return UNRELATED;
    }

J
Jiri Denemark 已提交
975
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
976
        virCPUx86CompareResult match = SUBSET;
J
Jiri Denemark 已提交
977

J
Jiri Denemark 已提交
978
        if ((cpuid1 = x86DataCpuid(model1->data, cpuid2->function))) {
J
Jiri Denemark 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
            if (x86cpuidMatch(cpuid2, cpuid1))
                continue;
            else if (!x86cpuidMatchMasked(cpuid2, cpuid1))
                match = SUPERSET;
        }

        if (result == EQUAL)
            result = match;
        else if (result != match)
            return UNRELATED;
    }

    return result;
}


static int
x86ModelLoad(xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
997
             virCPUx86MapPtr map)
J
Jiri Denemark 已提交
998 999
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
1000
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
1001
    char *vendor = NULL;
1002
    int ret = -1;
1003
    size_t i;
J
Jiri Denemark 已提交
1004 1005
    int n;

J
Jiri Denemark 已提交
1006
    if (!(model = x86ModelNew()))
1007
        goto cleanup;
J
Jiri Denemark 已提交
1008

1009
    model->name = virXPathString("string(@name)", ctxt);
1010
    if (!model->name) {
1011 1012
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
1013
        goto cleanup;
J
Jiri Denemark 已提交
1014 1015
    }

1016
    if (virXPathNode("./model", ctxt)) {
J
Jiri Denemark 已提交
1017
        virCPUx86ModelPtr ancestor;
J
Jiri Denemark 已提交
1018 1019
        char *name;

1020
        name = virXPathString("string(./model/@name)", ctxt);
1021
        if (!name) {
1022 1023 1024
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
1025
            goto cleanup;
J
Jiri Denemark 已提交
1026 1027
        }

1028
        if (!(ancestor = x86ModelFind(map, name))) {
1029 1030 1031
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
1032
            VIR_FREE(name);
1033
            goto cleanup;
J
Jiri Denemark 已提交
1034 1035 1036 1037
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1038
        model->vendor = ancestor->vendor;
1039
        virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
1040
        if (!(model->data = x86DataCopy(ancestor->data)))
1041
            goto cleanup;
J
Jiri Denemark 已提交
1042 1043
    }

1044 1045 1046
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
1047 1048 1049
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
1050
            goto cleanup;
1051 1052
        }

J
Jiri Denemark 已提交
1053
        if (!(model->vendor = x86VendorFind(map, vendor))) {
1054 1055 1056
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
1057
            goto cleanup;
J
Jiri Denemark 已提交
1058 1059 1060
        }
    }

1061
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1062
    if (n < 0)
1063
        goto cleanup;
J
Jiri Denemark 已提交
1064 1065

    for (i = 0; i < n; i++) {
1066
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
1067 1068
        char *name;

1069
        if (!(name = virXMLPropString(nodes[i], "name"))) {
1070 1071
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
1072
            goto cleanup;
J
Jiri Denemark 已提交
1073 1074
        }

1075
        if (!(feature = x86FeatureFind(map, name))) {
1076 1077 1078
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1079
            VIR_FREE(name);
1080
            goto cleanup;
J
Jiri Denemark 已提交
1081 1082 1083
        }
        VIR_FREE(name);

J
Jiri Denemark 已提交
1084
        if (x86DataAdd(model->data, feature->data))
1085
            goto cleanup;
J
Jiri Denemark 已提交
1086 1087
    }

1088 1089
    if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
        goto cleanup;
1090 1091

    ret = 0;
J
Jiri Denemark 已提交
1092

J
Jiri Denemark 已提交
1093
 cleanup:
1094
    x86ModelFree(model);
J
Jiri Denemark 已提交
1095
    VIR_FREE(vendor);
1096
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1097 1098 1099 1100 1101
    return ret;
}


static void
J
Jiri Denemark 已提交
1102
x86MapFree(virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1103
{
1104 1105
    size_t i;

1106
    if (!map)
J
Jiri Denemark 已提交
1107 1108
        return;

1109
    while (map->features) {
1110
        virCPUx86FeaturePtr feature = map->features;
J
Jiri Denemark 已提交
1111 1112 1113 1114
        map->features = feature->next;
        x86FeatureFree(feature);
    }

1115 1116 1117
    for (i = 0; i < map->nmodels; i++)
        x86ModelFree(map->models[i]);
    VIR_FREE(map->models);
J
Jiri Denemark 已提交
1118

1119 1120 1121
    for (i = 0; i < map->nvendors; i++)
        x86VendorFree(map->vendors[i]);
    VIR_FREE(map->vendors);
1122

1123
    while (map->migrate_blockers) {
1124
        virCPUx86FeaturePtr migrate_blocker = map->migrate_blockers;
1125 1126 1127 1128
        map->migrate_blockers = migrate_blocker->next;
        x86FeatureFree(migrate_blocker);
    }

J
Jiri Denemark 已提交
1129 1130 1131 1132
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1133
static int
1134
x86MapLoadCallback(cpuMapElement element,
J
Jiri Denemark 已提交
1135 1136 1137
                   xmlXPathContextPtr ctxt,
                   void *data)
{
J
Jiri Denemark 已提交
1138
    virCPUx86MapPtr map = data;
J
Jiri Denemark 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

    switch (element) {
    case CPU_MAP_ELEMENT_VENDOR:
        return x86VendorLoad(ctxt, map);
    case CPU_MAP_ELEMENT_FEATURE:
        return x86FeatureLoad(ctxt, map);
    case CPU_MAP_ELEMENT_MODEL:
        return x86ModelLoad(ctxt, map);
    case CPU_MAP_ELEMENT_LAST:
        break;
    }

    return 0;
}


1155
static int
J
Jiri Denemark 已提交
1156
x86MapLoadInternalFeatures(virCPUx86MapPtr map)
1157 1158
{
    size_t i;
1159
    virCPUx86FeaturePtr feature = NULL;
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

    for (i = 0; i < ARRAY_CARDINALITY(x86_kvm_features); i++) {
        const char *name = x86_kvm_features[i].name;

        if (x86FeatureFind(map, name)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("CPU feature %s already defined"), name);
            return -1;
        }

        if (!(feature = x86FeatureNew()))
            goto error;

        if (VIR_STRDUP(feature->name, name) < 0)
            goto error;

        if (virCPUx86DataAddCPUID(feature->data, &x86_kvm_features[i].cpuid))
            goto error;

1179 1180
        feature->next = map->features;
        map->features = feature;
1181 1182 1183 1184 1185
        feature = NULL;
    }

    return 0;

1186
 error:
1187 1188 1189 1190 1191
    x86FeatureFree(feature);
    return -1;
}


J
Jiri Denemark 已提交
1192
static virCPUx86MapPtr
1193
virCPUx86LoadMap(void)
J
Jiri Denemark 已提交
1194
{
J
Jiri Denemark 已提交
1195
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1196

1197
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1198 1199
        return NULL;

J
Jiri Denemark 已提交
1200
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1201 1202
        goto error;

1203 1204 1205
    if (x86MapLoadInternalFeatures(map) < 0)
        goto error;

J
Jiri Denemark 已提交
1206 1207
    return map;

1208
 error:
J
Jiri Denemark 已提交
1209 1210 1211 1212 1213
    x86MapFree(map);
    return NULL;
}


1214 1215 1216
int
virCPUx86MapOnceInit(void)
{
J
Jiri Denemark 已提交
1217
    if (!(cpuMap = virCPUx86LoadMap()))
1218 1219 1220 1221 1222 1223
        return -1;

    return 0;
}


J
Jiri Denemark 已提交
1224
static virCPUx86MapPtr
1225 1226 1227 1228 1229
virCPUx86GetMap(void)
{
    if (virCPUx86MapInitialize() < 0)
        return NULL;

J
Jiri Denemark 已提交
1230
    return cpuMap;
1231 1232 1233
}


1234 1235 1236
static char *
x86CPUDataFormat(const virCPUData *data)
{
1237
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data->data.x86);
1238
    virCPUx86CPUID *cpuid;
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "<cpudata arch='x86'>\n");
    while ((cpuid = x86DataCpuidNext(&iter))) {
        virBufferAsprintf(&buf,
                          "  <cpuid function='0x%08x'"
                          " eax='0x%08x' ebx='0x%08x'"
                          " ecx='0x%08x' edx='0x%08x'/>\n",
                          cpuid->function,
                          cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx);
    }
    virBufferAddLit(&buf, "</cpudata>\n");

1252
    if (virBufferCheckError(&buf) < 0)
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
x86CPUDataParse(const char *xmlStr)
{
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1266
    virCPUx86Data *data = NULL;
1267
    virCPUx86CPUID cpuid;
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    size_t i;
    int n;

    if (VIR_ALLOC(data) < 0)
        goto cleanup;

    if (!(xml = virXMLParseStringCtxt(xmlStr, _("CPU data"), &ctxt))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot parse CPU data"));
        goto cleanup;
    }
    ctxt->node = xmlDocGetRootElement(xml);

    n = virXPathNodeSet("/cpudata[@arch='x86']/data", ctxt, &nodes);
    if (n < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no x86 CPU data found"));
        goto cleanup;
    }

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse cpuid[%zu]"), i);
            goto cleanup;
        }
1295
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
1296 1297 1298
            goto cleanup;
    }

1299
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1300

1301
 cleanup:
1302 1303 1304
    VIR_FREE(nodes);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1305
    virCPUx86DataFree(data);
1306 1307 1308 1309
    return cpuData;
}


1310 1311 1312
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1313
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1314 1315 1316 1317 1318 1319 1320
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
#define virX86CpuIncompatible(MSG, CPU_DEF)                             \
        do {                                                            \
            char *flagsStr = NULL;                                      \
1321 1322 1323 1324
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) {  \
                virReportOOMError();                                    \
                goto error;                                             \
            }                                                           \
1325 1326 1327
            if (message &&                                              \
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
                VIR_FREE(flagsStr);                                     \
1328
                goto error;                                             \
1329 1330 1331 1332 1333 1334
            }                                                           \
            VIR_DEBUG("%s: %s", MSG, flagsStr);                         \
            VIR_FREE(flagsStr);                                         \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;                         \
        } while (0)

1335

J
Jiri Denemark 已提交
1336 1337 1338
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1339
           virCPUDataPtr *guest,
1340
           char **message)
J
Jiri Denemark 已提交
1341
{
J
Jiri Denemark 已提交
1342
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1343 1344 1345 1346 1347 1348 1349 1350
    virCPUx86ModelPtr host_model = NULL;
    virCPUx86ModelPtr cpu_force = NULL;
    virCPUx86ModelPtr cpu_require = NULL;
    virCPUx86ModelPtr cpu_optional = NULL;
    virCPUx86ModelPtr cpu_disable = NULL;
    virCPUx86ModelPtr cpu_forbid = NULL;
    virCPUx86ModelPtr diff = NULL;
    virCPUx86ModelPtr guest_model = NULL;
J
Jiri Denemark 已提交
1351
    virCPUCompareResult ret;
1352
    virCPUx86CompareResult result;
J
Jiri Denemark 已提交
1353
    virArch arch;
1354
    size_t i;
J
Jiri Denemark 已提交
1355

1356 1357 1358 1359 1360 1361
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

1362
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1363 1364 1365
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1366
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1367 1368 1369 1370 1371
                found = true;
                break;
            }
        }

1372
        if (!found) {
1373 1374
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1375 1376 1377
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1378
                            virArchToString(cpu->arch)) < 0)
1379
                goto error;
J
Jiri Denemark 已提交
1380
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1381
        }
J
Jiri Denemark 已提交
1382 1383 1384
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1385 1386
    }

J
Jiri Denemark 已提交
1387 1388 1389 1390
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1391 1392 1393 1394 1395
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1396
            goto error;
1397

J
Jiri Denemark 已提交
1398 1399 1400
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1401
    if (!(map = virCPUx86GetMap()) ||
1402
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1403 1404 1405 1406 1407
        !(cpu_force = x86ModelFromCPU(cpu, map, VIR_CPU_FEATURE_FORCE)) ||
        !(cpu_require = x86ModelFromCPU(cpu, map, VIR_CPU_FEATURE_REQUIRE)) ||
        !(cpu_optional = x86ModelFromCPU(cpu, map, VIR_CPU_FEATURE_OPTIONAL)) ||
        !(cpu_disable = x86ModelFromCPU(cpu, map, VIR_CPU_FEATURE_DISABLE)) ||
        !(cpu_forbid = x86ModelFromCPU(cpu, map, VIR_CPU_FEATURE_FORBID)))
J
Jiri Denemark 已提交
1408 1409
        goto error;

1410 1411 1412 1413
    x86DataIntersect(cpu_forbid->data, host_model->data);
    if (!x86DataIsEmpty(cpu_forbid->data)) {
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
                              cpu_forbid->data);
1414
        goto cleanup;
J
Jiri Denemark 已提交
1415 1416
    }

1417 1418 1419 1420 1421
    /* first remove features that were inherited from the CPU model and were
     * explicitly forced, disabled, or made optional
     */
    x86DataSubtract(cpu_require->data, cpu_force->data);
    x86DataSubtract(cpu_require->data, cpu_optional->data);
J
Jiri Denemark 已提交
1422
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1423 1424
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1425 1426 1427 1428
        x86DataSubtract(cpu_require->data, host_model->data);
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
                              cpu_require->data);
1429
        goto cleanup;
J
Jiri Denemark 已提交
1430 1431 1432 1433
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

1434
    if (!(diff = x86ModelCopy(host_model)))
1435
        goto error;
J
Jiri Denemark 已提交
1436

J
Jiri Denemark 已提交
1437 1438 1439 1440
    x86DataSubtract(diff->data, cpu_optional->data);
    x86DataSubtract(diff->data, cpu_require->data);
    x86DataSubtract(diff->data, cpu_disable->data);
    x86DataSubtract(diff->data, cpu_force->data);
J
Jiri Denemark 已提交
1441

J
Jiri Denemark 已提交
1442 1443
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1444 1445 1446 1447

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1448 1449 1450
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
                              diff->data);
1451
        goto cleanup;
J
Jiri Denemark 已提交
1452 1453
    }

1454
    if (guest) {
1455
        virCPUx86Data *guestData;
1456

1457
        if (!(guest_model = x86ModelCopy(host_model)))
1458
            goto error;
J
Jiri Denemark 已提交
1459 1460 1461

        if (cpu->type == VIR_CPU_TYPE_GUEST
            && cpu->match == VIR_CPU_MATCH_EXACT)
J
Jiri Denemark 已提交
1462
            x86DataSubtract(guest_model->data, diff->data);
J
Jiri Denemark 已提交
1463

J
Jiri Denemark 已提交
1464
        if (x86DataAdd(guest_model->data, cpu_force->data))
1465
            goto error;
J
Jiri Denemark 已提交
1466

J
Jiri Denemark 已提交
1467
        x86DataSubtract(guest_model->data, cpu_disable->data);
J
Jiri Denemark 已提交
1468

1469
        if (!(guestData = x86DataCopy(guest_model->data)) ||
1470
            !(*guest = virCPUx86MakeData(arch, &guestData))) {
1471
            virCPUx86DataFree(guestData);
1472
            goto error;
1473
        }
J
Jiri Denemark 已提交
1474 1475
    }

1476
 cleanup:
J
Jiri Denemark 已提交
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
    x86ModelFree(host_model);
    x86ModelFree(diff);
    x86ModelFree(cpu_force);
    x86ModelFree(cpu_require);
    x86ModelFree(cpu_optional);
    x86ModelFree(cpu_disable);
    x86ModelFree(cpu_forbid);
    x86ModelFree(guest_model);

    return ret;

1488
 error:
J
Jiri Denemark 已提交
1489
    ret = VIR_CPU_COMPARE_ERROR;
1490
    goto cleanup;
J
Jiri Denemark 已提交
1491
}
1492
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1493 1494 1495 1496


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
1497
           virCPUDefPtr cpu,
1498
           bool failIncompatible)
J
Jiri Denemark 已提交
1499
{
1500 1501 1502 1503 1504
    virCPUCompareResult ret;
    char *message = NULL;

    ret = x86Compute(host, cpu, NULL, &message);

1505
    if (failIncompatible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
        ret = VIR_CPU_COMPARE_ERROR;
        if (message) {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s", message);
        } else {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, NULL);
        }
    }
    VIR_FREE(message);

    return ret;
J
Jiri Denemark 已提交
1516 1517 1518 1519 1520 1521
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1522
             virCPUDataPtr *data,
1523
             char **message)
J
Jiri Denemark 已提交
1524
{
1525
    return x86Compute(host, guest, data, message);
J
Jiri Denemark 已提交
1526 1527
}

1528

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
/*
 * Checks whether cpuCandidate is a better fit for the CPU data than the
 * currently selected one from cpuCurrent.
 *
 * Returns 0 if cpuCurrent is better,
 *         1 if cpuCandidate is better,
 *         2 if cpuCandidate is the best one (search should stop now).
 */
static int
x86DecodeUseCandidate(virCPUDefPtr cpuCurrent,
                      virCPUDefPtr cpuCandidate,
                      const char *preferred,
                      bool checkPolicy)
{
    if (checkPolicy) {
        size_t i;
        for (i = 0; i < cpuCandidate->nfeatures; i++) {
            if (cpuCandidate->features[i].policy == VIR_CPU_FEATURE_DISABLE)
                return 0;
            cpuCandidate->features[i].policy = -1;
        }
    }

    if (preferred &&
        STREQ(cpuCandidate->model, preferred))
        return 2;

    if (!cpuCurrent)
        return 1;

    if (cpuCurrent->nfeatures > cpuCandidate->nfeatures)
        return 1;

    return 0;
}


J
Jiri Denemark 已提交
1566 1567
static int
x86Decode(virCPUDefPtr cpu,
1568
          const virCPUx86Data *data,
1569
          const char **models,
1570
          unsigned int nmodels,
1571 1572
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1573 1574
{
    int ret = -1;
J
Jiri Denemark 已提交
1575
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1576
    virCPUx86ModelPtr candidate;
1577 1578
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1579 1580 1581
    virCPUx86Data *copy = NULL;
    virCPUx86Data *features = NULL;
    const virCPUx86Data *cpuData = NULL;
J
Jiri Denemark 已提交
1582
    virCPUx86VendorPtr vendor;
1583
    ssize_t i;
1584
    int rc;
J
Jiri Denemark 已提交
1585

1586 1587
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, -1);
1588

1589
    if (!data || !(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1590 1591
        return -1;

J
Jiri Denemark 已提交
1592 1593
    vendor = x86DataToVendor(data, map);

1594 1595 1596 1597 1598
    /* Walk through the CPU models in reverse order to check newest
     * models first.
     */
    for (i = map->nmodels - 1; i >= 0; i--) {
        candidate = map->models[i];
1599
        if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
1600 1601
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1602 1603 1604
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
J
Jiri Denemark 已提交
1605
                    goto cleanup;
1606 1607 1608 1609 1610 1611 1612 1613 1614
                } else {
                    VIR_WARN("Preferred CPU model %s not allowed by"
                             " hypervisor; closest supported model will be"
                             " used", preferred);
                }
            } else {
                VIR_DEBUG("CPU model %s not allowed by hypervisor; ignoring",
                          candidate->name);
            }
1615
            continue;
J
Jiri Denemark 已提交
1616 1617
        }

J
Jiri Denemark 已提交
1618 1619 1620 1621
        /* Both vendor and candidate->vendor are pointers to a single list of
         * known vendors stored in the map.
         */
        if (vendor && candidate->vendor && vendor != candidate->vendor) {
J
Jiri Denemark 已提交
1622
            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
J
Jiri Denemark 已提交
1623
                      candidate->vendor->name, candidate->name, vendor->name);
1624
            continue;
J
Jiri Denemark 已提交
1625 1626
        }

J
Jiri Denemark 已提交
1627 1628 1629 1630
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto cleanup;
        cpuCandidate->type = cpu->type;

1631 1632
        if ((rc = x86DecodeUseCandidate(cpuModel, cpuCandidate, preferred,
                                        cpu->type == VIR_CPU_TYPE_HOST))) {
1633 1634
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1635
            cpuData = candidate->data;
1636 1637
            if (rc == 2)
                break;
1638
        } else {
1639
            virCPUDefFree(cpuCandidate);
1640
        }
J
Jiri Denemark 已提交
1641 1642
    }

1643
    if (!cpuModel) {
1644 1645
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1646
        goto cleanup;
J
Jiri Denemark 已提交
1647 1648
    }

1649 1650 1651 1652 1653
    /* Remove non-migratable features if requested
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
    if (flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE) {
        for (i = 0; i < cpuModel->nfeatures; i++) {
1654
            virCPUx86FeaturePtr feat;
1655 1656 1657 1658 1659 1660 1661 1662 1663
            for (feat = map->migrate_blockers; feat; feat = feat->next) {
                if (STREQ(feat->name, cpuModel->features[i].name)) {
                    VIR_FREE(cpuModel->features[i].name);
                    VIR_DELETE_ELEMENT_INPLACE(cpuModel->features, i, cpuModel->nfeatures);
                }
            }
        }
    }

1664 1665 1666
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) {
        if (!(copy = x86DataCopy(cpuData)) ||
            !(features = x86DataFromCPUFeatures(cpuModel, map)))
J
Jiri Denemark 已提交
1667
            goto cleanup;
1668 1669 1670 1671

        x86DataSubtract(copy, features);
        if (x86DataToCPUFeatures(cpuModel, VIR_CPU_FEATURE_REQUIRE,
                                 copy, map) < 0)
J
Jiri Denemark 已提交
1672
            goto cleanup;
1673 1674
    }

J
Jiri Denemark 已提交
1675 1676 1677
    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
        goto cleanup;

1678 1679 1680 1681
    cpu->model = cpuModel->model;
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1682 1683 1684

    ret = 0;

J
Jiri Denemark 已提交
1685
 cleanup:
1686
    virCPUDefFree(cpuModel);
1687 1688
    virCPUx86DataFree(copy);
    virCPUx86DataFree(features);
J
Jiri Denemark 已提交
1689 1690 1691
    return ret;
}

1692 1693
static int
x86DecodeCPUData(virCPUDefPtr cpu,
1694
                 const virCPUData *data,
1695 1696
                 const char **models,
                 unsigned int nmodels,
1697 1698
                 const char *preferred,
                 unsigned int flags)
1699
{
1700
    return x86Decode(cpu, data->data.x86, models, nmodels, preferred, flags);
1701
}
J
Jiri Denemark 已提交
1702

1703

1704
static virCPUx86Data *
1705
x86EncodePolicy(const virCPUDef *cpu,
J
Jiri Denemark 已提交
1706
                virCPUx86MapPtr map,
1707
                virCPUFeaturePolicy policy)
J
Jiri Denemark 已提交
1708
{
J
Jiri Denemark 已提交
1709
    virCPUx86ModelPtr model;
1710
    virCPUx86Data *data = NULL;
J
Jiri Denemark 已提交
1711 1712 1713 1714

    if (!(model = x86ModelFromCPU(cpu, map, policy)))
        return NULL;

J
Jiri Denemark 已提交
1715 1716
    data = model->data;
    model->data = NULL;
J
Jiri Denemark 已提交
1717 1718 1719 1720 1721 1722 1723
    x86ModelFree(model);

    return data;
}


static int
J
Jiri Denemark 已提交
1724
x86Encode(virArch arch,
1725
          const virCPUDef *cpu,
1726 1727 1728 1729 1730 1731
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
1732
{
J
Jiri Denemark 已提交
1733
    virCPUx86MapPtr map = NULL;
1734 1735 1736 1737 1738 1739
    virCPUx86Data *data_forced = NULL;
    virCPUx86Data *data_required = NULL;
    virCPUx86Data *data_optional = NULL;
    virCPUx86Data *data_disabled = NULL;
    virCPUx86Data *data_forbidden = NULL;
    virCPUx86Data *data_vendor = NULL;
J
Jiri Denemark 已提交
1740

1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

1754
    if (!(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
        goto error;

    if (forced) {
        data_forced = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_FORCE);
        if (!data_forced)
            goto error;
    }

    if (required) {
        data_required = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_REQUIRE);
        if (!data_required)
            goto error;
    }

    if (optional) {
        data_optional = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_OPTIONAL);
        if (!data_optional)
            goto error;
    }

    if (disabled) {
        data_disabled = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_DISABLE);
        if (!data_disabled)
            goto error;
    }

    if (forbidden) {
        data_forbidden = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_FORBID);
        if (!data_forbidden)
            goto error;
    }

J
Jiri Denemark 已提交
1787
    if (vendor) {
1788
        virCPUx86VendorPtr v = NULL;
J
Jiri Denemark 已提交
1789 1790

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
1791 1792
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s not found"), cpu->vendor);
J
Jiri Denemark 已提交
1793 1794 1795 1796 1797
            goto error;
        }

        if (v &&
            (VIR_ALLOC(data_vendor) < 0 ||
1798
             virCPUx86DataAddCPUID(data_vendor, &v->cpuid) < 0)) {
J
Jiri Denemark 已提交
1799 1800 1801 1802
            goto error;
        }
    }

1803
    if (forced &&
1804
        !(*forced = virCPUx86MakeData(arch, &data_forced)))
1805 1806
        goto error;
    if (required &&
1807
        !(*required = virCPUx86MakeData(arch, &data_required)))
1808 1809
        goto error;
    if (optional &&
1810
        !(*optional = virCPUx86MakeData(arch, &data_optional)))
1811 1812
        goto error;
    if (disabled &&
1813
        !(*disabled = virCPUx86MakeData(arch, &data_disabled)))
1814 1815
        goto error;
    if (forbidden &&
1816
        !(*forbidden = virCPUx86MakeData(arch, &data_forbidden)))
1817 1818
        goto error;
    if (vendor &&
1819
        !(*vendor = virCPUx86MakeData(arch, &data_vendor)))
1820
        goto error;
J
Jiri Denemark 已提交
1821

1822
    return 0;
J
Jiri Denemark 已提交
1823

1824
 error:
1825 1826 1827 1828 1829 1830
    virCPUx86DataFree(data_forced);
    virCPUx86DataFree(data_required);
    virCPUx86DataFree(data_optional);
    virCPUx86DataFree(data_disabled);
    virCPUx86DataFree(data_forbidden);
    virCPUx86DataFree(data_vendor);
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
    if (forced)
        x86FreeCPUData(*forced);
    if (required)
        x86FreeCPUData(*required);
    if (optional)
        x86FreeCPUData(*optional);
    if (disabled)
        x86FreeCPUData(*disabled);
    if (forbidden)
        x86FreeCPUData(*forbidden);
    if (vendor)
        x86FreeCPUData(*vendor);
1843
    return -1;
J
Jiri Denemark 已提交
1844 1845 1846 1847 1848
}


#if HAVE_CPUID
static inline void
1849
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
1850
{
1851
# if __x86_64__
1852 1853 1854 1855
    asm("xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
        "xor %%ecx, %%ecx;" /* functions may use them as additional */
        "xor %%edx, %%edx;" /* arguments */
        "cpuid;"
J
Jiri Denemark 已提交
1856 1857 1858 1859 1860
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function));
1861
# else
J
Jiri Denemark 已提交
1862 1863 1864 1865
    /* we need to avoid direct use of ebx for CPUID output as it is used
     * for global offset table on i386 with -fPIC
     */
    asm("push %%ebx;"
1866 1867 1868
        "xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
        "xor %%ecx, %%ecx;" /* functions may use them as additional */
        "xor %%edx, %%edx;" /* arguments */
J
Jiri Denemark 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function)
        : "cc");
1878
# endif
J
Jiri Denemark 已提交
1879 1880 1881 1882
}


static int
1883
cpuidSet(uint32_t base, virCPUx86Data *data)
J
Jiri Denemark 已提交
1884 1885 1886
{
    uint32_t max;
    uint32_t i;
1887
    virCPUx86CPUID cpuid = { base, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
1888 1889

    cpuidCall(&cpuid);
1890
    max = cpuid.eax;
J
Jiri Denemark 已提交
1891

1892 1893
    for (i = base; i <= max; i++) {
        cpuid.function = i;
J
Jiri Denemark 已提交
1894
        cpuidCall(&cpuid);
1895 1896
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
J
Jiri Denemark 已提交
1897 1898
    }

1899
    return 0;
J
Jiri Denemark 已提交
1900 1901 1902
}


1903
static virCPUDataPtr
1904
x86NodeData(virArch arch)
J
Jiri Denemark 已提交
1905
{
1906
    virCPUDataPtr cpuData = NULL;
1907
    virCPUx86Data *data;
J
Jiri Denemark 已提交
1908

1909
    if (VIR_ALLOC(data) < 0)
J
Jiri Denemark 已提交
1910 1911
        return NULL;

1912
    if (cpuidSet(CPUX86_BASIC, data) < 0)
J
Jiri Denemark 已提交
1913 1914
        goto error;

1915
    if (cpuidSet(CPUX86_EXTENDED, data) < 0)
J
Jiri Denemark 已提交
1916 1917
        goto error;

1918
    if (!(cpuData = virCPUx86MakeData(arch, &data)))
1919 1920 1921
        goto error;

    return cpuData;
J
Jiri Denemark 已提交
1922

1923
 error:
1924
    virCPUx86DataFree(data);
J
Jiri Denemark 已提交
1925 1926 1927 1928 1929 1930

    return NULL;
}
#endif


1931 1932 1933 1934
static virCPUDefPtr
x86Baseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
1935 1936
            unsigned int nmodels,
            unsigned int flags)
1937
{
J
Jiri Denemark 已提交
1938
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1939
    virCPUx86ModelPtr base_model = NULL;
1940
    virCPUDefPtr cpu = NULL;
1941
    size_t i;
1942
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
1943
    virCPUx86ModelPtr model = NULL;
1944
    bool outputVendor = true;
1945 1946
    const char *modelName;
    bool matchingNames = true;
1947

1948 1949 1950
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, NULL);

1951
    if (!(map = virCPUx86GetMap()))
1952 1953
        goto error;

1954
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1955 1956
        goto error;

1957
    if (VIR_ALLOC(cpu) < 0)
1958
        goto error;
1959 1960

    cpu->arch = cpus[0]->arch;
1961 1962
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1963

1964
    if (!cpus[0]->vendor) {
1965
        outputVendor = false;
1966
    } else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
1967 1968
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Unknown CPU vendor %s"), cpus[0]->vendor);
J
Jiri Denemark 已提交
1969 1970 1971
        goto error;
    }

1972
    modelName = cpus[0]->model;
1973
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
1974 1975
        const char *vn = NULL;

1976 1977 1978 1979 1980 1981 1982 1983 1984
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

1985
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
1986 1987
            goto error;

J
Jiri Denemark 已提交
1988 1989
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
1990 1991 1992
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s of model %s differs from vendor %s"),
                           model->vendor->name, model->name, cpus[i]->vendor);
J
Jiri Denemark 已提交
1993 1994 1995
            goto error;
        }

1996
        if (cpus[i]->vendor) {
J
Jiri Denemark 已提交
1997
            vn = cpus[i]->vendor;
1998
        } else {
1999 2000 2001 2002
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
2003 2004 2005 2006

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
2007 2008
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("Unknown CPU vendor %s"), vn);
J
Jiri Denemark 已提交
2009 2010 2011
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
2012 2013
                virReportError(VIR_ERR_OPERATION_FAILED,
                               "%s", _("CPU vendors do not match"));
J
Jiri Denemark 已提交
2014 2015 2016 2017
                goto error;
            }
        }

J
Jiri Denemark 已提交
2018
        x86DataIntersect(base_model->data, model->data);
2019
        x86ModelFree(model);
J
Jiri Denemark 已提交
2020
        model = NULL;
2021 2022
    }

J
Jiri Denemark 已提交
2023
    if (x86DataIsEmpty(base_model->data)) {
2024 2025
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
2026 2027 2028
        goto error;
    }

2029
    if (vendor && virCPUx86DataAddCPUID(base_model->data, &vendor->cpuid) < 0)
2030
        goto error;
J
Jiri Denemark 已提交
2031

2032
    if (x86Decode(cpu, base_model->data, models, nmodels, modelName, flags) < 0)
2033 2034
        goto error;

2035 2036 2037
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2038 2039 2040
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2041 2042
    cpu->arch = VIR_ARCH_NONE;

2043
 cleanup:
2044 2045 2046 2047
    x86ModelFree(base_model);

    return cpu;

2048
 error:
J
Jiri Denemark 已提交
2049
    x86ModelFree(model);
2050 2051 2052 2053 2054 2055
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


2056
static int
2057
x86UpdateCustom(virCPUDefPtr guest,
2058
                const virCPUDef *host)
2059 2060
{
    int ret = -1;
2061
    size_t i;
J
Jiri Denemark 已提交
2062
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
2063
    virCPUx86ModelPtr host_model = NULL;
2064

2065
    if (!(map = virCPUx86GetMap()) ||
2066
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
2067 2068 2069 2070
        goto cleanup;

    for (i = 0; i < guest->nfeatures; i++) {
        if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
2071
            virCPUx86FeaturePtr feature;
2072
            if (!(feature = x86FeatureFind(map, guest->features[i].name))) {
2073 2074 2075
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unknown CPU feature %s"),
                               guest->features[i].name);
2076 2077 2078
                goto cleanup;
            }

J
Jiri Denemark 已提交
2079
            if (x86DataIsSubset(host_model->data, feature->data))
2080 2081 2082 2083 2084 2085 2086 2087
                guest->features[i].policy = VIR_CPU_FEATURE_REQUIRE;
            else
                guest->features[i].policy = VIR_CPU_FEATURE_DISABLE;
        }
    }

    if (guest->match == VIR_CPU_MATCH_MINIMUM) {
        guest->match = VIR_CPU_MATCH_EXACT;
P
Peter Krempa 已提交
2088 2089 2090
        if (x86ModelSubtractCPU(host_model, guest, map) ||
            x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
                                 host_model->data, map))
2091 2092 2093 2094 2095
            goto cleanup;
    }

    ret = 0;

2096
 cleanup:
2097 2098 2099 2100
    x86ModelFree(host_model);
    return ret;
}

2101 2102 2103

static int
x86UpdateHostModel(virCPUDefPtr guest,
2104 2105
                   const virCPUDef *host,
                   bool passthrough)
2106
{
2107
    virCPUDefPtr oldguest = NULL;
J
Jiri Denemark 已提交
2108
    virCPUx86MapPtr map;
2109
    virCPUx86FeaturePtr feat;
2110
    size_t i;
2111
    int ret = -1;
2112

2113 2114
    if (!(map = virCPUx86GetMap()))
        goto cleanup;
2115 2116 2117

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
2118
        goto cleanup;
2119 2120 2121

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
2122
        goto cleanup;
2123

2124 2125 2126 2127 2128 2129
    if (oldguest->vendor_id) {
        VIR_FREE(guest->vendor_id);
        if (VIR_STRDUP(guest->vendor_id, oldguest->vendor_id) < 0)
            goto cleanup;
    }

2130 2131 2132 2133 2134
    /* Remove non-migratable features by default
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
    for (i = 0; i < guest->nfeatures; i++) {
        for (feat = map->migrate_blockers; feat; feat = feat->next) {
J
Ján Tomko 已提交
2135 2136
            if (STREQ(feat->name, guest->features[i].name)) {
                VIR_FREE(guest->features[i].name);
2137
                VIR_DELETE_ELEMENT_INPLACE(guest->features, i, guest->nfeatures);
J
Ján Tomko 已提交
2138
            }
2139 2140
        }
    }
2141
    for (i = 0; !passthrough && i < oldguest->nfeatures; i++) {
2142 2143 2144
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
2145
            goto cleanup;
2146 2147
    }

2148 2149 2150 2151 2152
    ret = 0;

 cleanup:
    virCPUDefFree(oldguest);
    return ret;
2153 2154 2155
}


2156 2157
static int
x86Update(virCPUDefPtr guest,
2158
          const virCPUDef *host)
2159
{
2160
    switch ((virCPUMode) guest->mode) {
2161 2162 2163 2164
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2165 2166
        guest->match = VIR_CPU_MATCH_EXACT;
        return x86UpdateHostModel(guest, host, false);
2167

2168
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2169
        guest->match = VIR_CPU_MATCH_MINIMUM;
2170
        return x86UpdateHostModel(guest, host, true);
2171 2172 2173 2174 2175

    case VIR_CPU_MODE_LAST:
        break;
    }

2176 2177
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2178 2179 2180
    return -1;
}

2181 2182 2183 2184

static int
x86HasFeature(const virCPUData *data,
              const char *name)
D
Daniel P. Berrange 已提交
2185
{
J
Jiri Denemark 已提交
2186
    virCPUx86MapPtr map;
2187
    virCPUx86FeaturePtr feature;
D
Daniel P. Berrange 已提交
2188 2189
    int ret = -1;

2190
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2191 2192 2193 2194 2195
        return -1;

    if (!(feature = x86FeatureFind(map, name)))
        goto cleanup;

J
Jiri Denemark 已提交
2196
    ret = x86DataIsSubset(data->data.x86, feature->data) ? 1 : 0;
D
Daniel P. Berrange 已提交
2197

2198
 cleanup:
D
Daniel P. Berrange 已提交
2199 2200
    return ret;
}
2201

2202 2203 2204
static int
x86GetModels(char ***models)
{
J
Jiri Denemark 已提交
2205
    virCPUx86MapPtr map;
2206
    size_t i;
2207 2208 2209 2210

    if (!(map = virCPUx86GetMap()))
        return -1;

2211 2212 2213
    if (models) {
        if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
            goto error;
2214

2215 2216
        for (i = 0; i < map->nmodels; i++) {
            if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
2217 2218
                goto error;
        }
2219 2220
    }

2221
    return map->nmodels;
2222 2223

 error:
2224 2225 2226 2227
    if (models) {
        virStringFreeList(*models);
        *models = NULL;
    }
2228 2229 2230
    return -1;
}

2231

J
Jiri Denemark 已提交
2232 2233 2234 2235 2236
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
    .compare    = x86Compare,
2237
    .decode     = x86DecodeCPUData,
J
Jiri Denemark 已提交
2238
    .encode     = x86Encode,
2239
    .free       = x86FreeCPUData,
J
Jiri Denemark 已提交
2240 2241 2242 2243 2244
#if HAVE_CPUID
    .nodeData   = x86NodeData,
#else
    .nodeData   = NULL,
#endif
2245
    .guestData  = x86GuestData,
2246
    .baseline   = x86Baseline,
2247
    .update     = x86Update,
D
Daniel P. Berrange 已提交
2248
    .hasFeature = x86HasFeature,
2249 2250
    .dataFormat = x86CPUDataFormat,
    .dataParse  = x86CPUDataParse,
2251
    .getModels  = x86GetModels,
J
Jiri Denemark 已提交
2252
};