cpu_x86.c 55.3 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

J
Jiri Denemark 已提交
47 48
struct x86_vendor {
    char *name;
49
    virCPUx86CPUID cpuid;
J
Jiri Denemark 已提交
50 51 52 53

    struct x86_vendor *next;
};

J
Jiri Denemark 已提交
54 55
struct x86_feature {
    char *name;
56
    virCPUx86Data *data;
J
Jiri Denemark 已提交
57 58 59 60

    struct x86_feature *next;
};

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
struct x86_kvm_feature {
    const char *name;
    const virCPUx86CPUID cpuid;
};

static const struct x86_kvm_feature x86_kvm_features[] =
{
    {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 }},
};

J
Jiri Denemark 已提交
80 81
struct x86_model {
    char *name;
J
Jiri Denemark 已提交
82
    const struct x86_vendor *vendor;
83
    virCPUx86Data *data;
J
Jiri Denemark 已提交
84 85 86 87 88

    struct x86_model *next;
};

struct x86_map {
J
Jiri Denemark 已提交
89
    struct x86_vendor *vendors;
J
Jiri Denemark 已提交
90 91
    struct x86_feature *features;
    struct x86_model *models;
92
    struct x86_feature *migrate_blockers;
J
Jiri Denemark 已提交
93 94
};

95
static struct x86_map* virCPUx86Map;
96 97 98
int virCPUx86MapOnceInit(void);
VIR_ONCE_GLOBAL_INIT(virCPUx86Map);

J
Jiri Denemark 已提交
99 100 101 102 103 104 105 106 107

enum compare_result {
    SUBSET,
    EQUAL,
    SUPERSET,
    UNRELATED
};


108
struct virCPUx86DataIterator {
109
    const virCPUx86Data *data;
J
Jiri Denemark 已提交
110 111
    int pos;
};
J
Jiri Denemark 已提交
112 113


114
#define virCPUx86DataIteratorInit(data) \
115
    { data, -1 }
J
Jiri Denemark 已提交
116 117


118
static bool
119 120
x86cpuidMatch(const virCPUx86CPUID *cpuid1,
              const virCPUx86CPUID *cpuid2)
J
Jiri Denemark 已提交
121 122 123 124 125 126 127 128
{
    return (cpuid1->eax == cpuid2->eax &&
            cpuid1->ebx == cpuid2->ebx &&
            cpuid1->ecx == cpuid2->ecx &&
            cpuid1->edx == cpuid2->edx);
}


129
static bool
130 131
x86cpuidMatchMasked(const virCPUx86CPUID *cpuid,
                    const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
132 133 134 135 136 137 138 139
{
    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 已提交
140
static void
141 142
x86cpuidSetBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
143
{
144 145 146
    if (!mask)
        return;

J
Jiri Denemark 已提交
147 148 149 150 151 152 153
    cpuid->eax |= mask->eax;
    cpuid->ebx |= mask->ebx;
    cpuid->ecx |= mask->ecx;
    cpuid->edx |= mask->edx;
}


J
Jiri Denemark 已提交
154
static void
155 156
x86cpuidClearBits(virCPUx86CPUID *cpuid,
                  const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
157
{
158 159 160
    if (!mask)
        return;

J
Jiri Denemark 已提交
161 162 163 164 165 166 167
    cpuid->eax &= ~mask->eax;
    cpuid->ebx &= ~mask->ebx;
    cpuid->ecx &= ~mask->ecx;
    cpuid->edx &= ~mask->edx;
}


J
Jiri Denemark 已提交
168
static void
169 170
x86cpuidAndBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
171
{
172 173 174
    if (!mask)
        return;

175 176 177 178 179 180
    cpuid->eax &= mask->eax;
    cpuid->ebx &= mask->ebx;
    cpuid->ecx &= mask->ecx;
    cpuid->edx &= mask->edx;
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194
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;
}

195

J
Jiri Denemark 已提交
196
/* skips all zero CPUID leafs */
197
static virCPUx86CPUID *
198
x86DataCpuidNext(struct virCPUx86DataIterator *iterator)
J
Jiri Denemark 已提交
199
{
200
    const virCPUx86Data *data = iterator->data;
J
Jiri Denemark 已提交
201

202
    if (!data)
J
Jiri Denemark 已提交
203 204
        return NULL;

205 206 207 208
    while (++iterator->pos < data->len) {
        if (!x86cpuidMatch(data->data + iterator->pos, &cpuidNull))
            return data->data + iterator->pos;
    }
J
Jiri Denemark 已提交
209

210
    return NULL;
J
Jiri Denemark 已提交
211 212 213
}


214
static virCPUx86CPUID *
215
x86DataCpuid(const virCPUx86Data *data,
J
Jiri Denemark 已提交
216 217
             uint32_t function)
{
218
    size_t i;
J
Jiri Denemark 已提交
219

220 221 222
    for (i = 0; i < data->len; i++) {
        if (data->data[i].function == function)
            return data->data + i;
J
Jiri Denemark 已提交
223 224
    }

225
    return NULL;
J
Jiri Denemark 已提交
226 227
}

J
Jiri Denemark 已提交
228
void
229
virCPUx86DataFree(virCPUx86Data *data)
J
Jiri Denemark 已提交
230 231 232 233
{
    if (data == NULL)
        return;

234
    VIR_FREE(data->data);
J
Jiri Denemark 已提交
235 236 237 238
    VIR_FREE(data);
}


J
Jiri Denemark 已提交
239
virCPUDataPtr
240
virCPUx86MakeData(virArch arch, virCPUx86Data **data)
241
{
242
    virCPUDataPtr cpuData;
243 244 245 246

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

J
Jiri Denemark 已提交
247 248
    cpuData->arch = arch;
    cpuData->data.x86 = *data;
249 250 251 252 253 254
    *data = NULL;

    return cpuData;
}

static void
255
x86FreeCPUData(virCPUDataPtr data)
256 257 258 259
{
    if (!data)
        return;

260
    virCPUx86DataFree(data->data.x86);
261 262 263 264
    VIR_FREE(data);
}


265 266
static virCPUx86Data *
x86DataCopy(const virCPUx86Data *data)
J
Jiri Denemark 已提交
267
{
268
    virCPUx86Data *copy = NULL;
269
    size_t i;
J
Jiri Denemark 已提交
270

271 272
    if (VIR_ALLOC(copy) < 0 ||
        VIR_ALLOC_N(copy->data, data->len) < 0) {
273
        virCPUx86DataFree(copy);
J
Jiri Denemark 已提交
274 275 276
        return NULL;
    }

277 278 279
    copy->len = data->len;
    for (i = 0; i < data->len; i++)
        copy->data[i] = data->data[i];
J
Jiri Denemark 已提交
280 281 282 283 284

    return copy;
}


J
Jiri Denemark 已提交
285
int
286 287
virCPUx86DataAddCPUID(virCPUx86Data *data,
                      const virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
288
{
289
    virCPUx86CPUID *existing;
J
Jiri Denemark 已提交
290

291 292 293 294 295 296
    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 已提交
297

298 299 300
        qsort(data->data, data->len,
              sizeof(virCPUx86CPUID), virCPUx86CPUIDSorter);
    }
J
Jiri Denemark 已提交
301

J
Jiri Denemark 已提交
302 303 304 305 306
    return 0;
}


static int
307 308
x86DataAdd(virCPUx86Data *data1,
           const virCPUx86Data *data2)
J
Jiri Denemark 已提交
309
{
310 311 312
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data2);
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
313

314 315
    while ((cpuid2 = x86DataCpuidNext(&iter))) {
        cpuid1 = x86DataCpuid(data1, cpuid2->function);
J
Jiri Denemark 已提交
316

317 318 319 320 321 322
        if (cpuid1) {
            x86cpuidSetBits(cpuid1, cpuid2);
        } else {
            if (virCPUx86DataAddCPUID(data1, cpuid2) < 0)
                return -1;
        }
J
Jiri Denemark 已提交
323
    }
J
Jiri Denemark 已提交
324 325 326 327 328

    return 0;
}


329
static void
330 331
x86DataSubtract(virCPUx86Data *data1,
                const virCPUx86Data *data2)
332
{
333 334 335
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
336

337 338 339
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
        cpuid2 = x86DataCpuid(data2, cpuid1->function);
        x86cpuidClearBits(cpuid1, cpuid2);
340 341 342 343
    }
}


J
Jiri Denemark 已提交
344
static void
345 346
x86DataIntersect(virCPUx86Data *data1,
                 const virCPUx86Data *data2)
347
{
348
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
349 350
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
351

J
Jiri Denemark 已提交
352 353 354 355 356 357
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
        cpuid2 = x86DataCpuid(data2, cpuid1->function);
        if (cpuid2)
            x86cpuidAndBits(cpuid1, cpuid2);
        else
            x86cpuidClearBits(cpuid1, cpuid1);
358 359 360 361
    }
}


J
Jiri Denemark 已提交
362
static bool
363
x86DataIsEmpty(virCPUx86Data *data)
J
Jiri Denemark 已提交
364
{
365
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data);
J
Jiri Denemark 已提交
366

367
    return x86DataCpuidNext(&iter) == NULL;
J
Jiri Denemark 已提交
368
}
J
Jiri Denemark 已提交
369 370


J
Jiri Denemark 已提交
371
static bool
372 373
x86DataIsSubset(const virCPUx86Data *data,
                const virCPUx86Data *subset)
J
Jiri Denemark 已提交
374 375
{

376
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit((virCPUx86Data *)subset);
377 378
    const virCPUx86CPUID *cpuid;
    const virCPUx86CPUID *cpuidSubset;
J
Jiri Denemark 已提交
379

J
Jiri Denemark 已提交
380 381 382 383
    while ((cpuidSubset = x86DataCpuidNext(&iter))) {
        if (!(cpuid = x86DataCpuid(data, cpuidSubset->function)) ||
            !x86cpuidMatchMasked(cpuid, cpuidSubset))
            return false;
J
Jiri Denemark 已提交
384 385
    }

J
Jiri Denemark 已提交
386
    return true;
J
Jiri Denemark 已提交
387 388 389
}


390 391 392 393
/* also removes all detected features from data */
static int
x86DataToCPUFeatures(virCPUDefPtr cpu,
                     int policy,
394
                     virCPUx86Data *data,
395 396 397 398 399
                     const struct x86_map *map)
{
    const struct x86_feature *feature = map->features;

    while (feature != NULL) {
J
Jiri Denemark 已提交
400 401 402 403
        if (x86DataIsSubset(data, feature->data)) {
            x86DataSubtract(data, feature->data);
            if (virCPUDefAddFeature(cpu, feature->name, policy) < 0)
                return -1;
404 405 406 407 408 409 410 411
        }
        feature = feature->next;
    }

    return 0;
}


J
Jiri Denemark 已提交
412 413
/* also removes bits corresponding to vendor string from data */
static const struct x86_vendor *
414
x86DataToVendor(virCPUx86Data *data,
J
Jiri Denemark 已提交
415 416 417
                const struct x86_map *map)
{
    const struct x86_vendor *vendor = map->vendors;
418
    virCPUx86CPUID *cpuid;
J
Jiri Denemark 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432

    while (vendor) {
        if ((cpuid = x86DataCpuid(data, vendor->cpuid.function)) &&
            x86cpuidMatchMasked(cpuid, &vendor->cpuid)) {
            x86cpuidClearBits(cpuid, &vendor->cpuid);
            return vendor;
        }
        vendor = vendor->next;
    }

    return NULL;
}


433
static virCPUDefPtr
434
x86DataToCPU(const virCPUx86Data *data,
435 436 437 438
             const struct x86_model *model,
             const struct x86_map *map)
{
    virCPUDefPtr cpu;
439 440
    virCPUx86Data *copy = NULL;
    virCPUx86Data *modelData = NULL;
J
Jiri Denemark 已提交
441
    const struct x86_vendor *vendor;
442 443

    if (VIR_ALLOC(cpu) < 0 ||
444
        VIR_STRDUP(cpu->model, model->name) < 0 ||
445
        !(copy = x86DataCopy(data)) ||
J
Jiri Denemark 已提交
446
        !(modelData = x86DataCopy(model->data)))
447
        goto error;
448

J
Jiri Denemark 已提交
449
    if ((vendor = x86DataToVendor(copy, map)) &&
450
        VIR_STRDUP(cpu->vendor, vendor->name) < 0)
451
        goto error;
J
Jiri Denemark 已提交
452

453 454 455 456 457
    x86DataSubtract(copy, modelData);
    x86DataSubtract(modelData, data);

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

459 460
    if (x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_REQUIRE, copy, map) ||
        x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_DISABLE, modelData, map))
461
        goto error;
462

463
 cleanup:
464 465
    virCPUx86DataFree(modelData);
    virCPUx86DataFree(copy);
466 467
    return cpu;

468
 error:
469 470 471 472 473 474
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


J
Jiri Denemark 已提交
475 476 477 478 479 480 481 482
static void
x86VendorFree(struct x86_vendor *vendor)
{
    if (!vendor)
        return;

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
J
Jiri Denemark 已提交
483
}
J
Jiri Denemark 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512


static struct x86_vendor *
x86VendorFind(const struct x86_map *map,
              const char *name)
{
    struct x86_vendor *vendor;

    vendor = map->vendors;
    while (vendor) {
        if (STREQ(vendor->name, name))
            return vendor;

        vendor = vendor->next;
    }

    return NULL;
}


static int
x86VendorLoad(xmlXPathContextPtr ctxt,
              struct x86_map *map)
{
    struct x86_vendor *vendor = NULL;
    char *string = NULL;
    int ret = 0;

    if (VIR_ALLOC(vendor) < 0)
513
        goto error;
J
Jiri Denemark 已提交
514 515 516

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
517 518
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing CPU vendor name"));
J
Jiri Denemark 已提交
519 520 521 522
        goto ignore;
    }

    if (x86VendorFind(map, vendor->name)) {
523 524
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU vendor %s already defined"), vendor->name);
J
Jiri Denemark 已提交
525 526 527 528 529
        goto ignore;
    }

    string = virXPathString("string(@string)", ctxt);
    if (!string) {
530
        virReportError(VIR_ERR_INTERNAL_ERROR,
531 532
                       _("Missing vendor string for CPU vendor %s"),
                       vendor->name);
J
Jiri Denemark 已提交
533 534 535
        goto ignore;
    }
    if (strlen(string) != VENDOR_STRING_LENGTH) {
536 537
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid CPU vendor string '%s'"), string);
J
Jiri Denemark 已提交
538 539 540 541
        goto ignore;
    }

    vendor->cpuid.function = 0;
E
Eric Blake 已提交
542 543 544 545 546
    vendor->cpuid.ebx = virReadBufInt32LE(string);
    vendor->cpuid.edx = virReadBufInt32LE(string + 4);
    vendor->cpuid.ecx = virReadBufInt32LE(string + 8);

    if (!map->vendors) {
J
Jiri Denemark 已提交
547
        map->vendors = vendor;
E
Eric Blake 已提交
548
    } else {
J
Jiri Denemark 已提交
549 550 551 552
        vendor->next = map->vendors;
        map->vendors = vendor;
    }

553
 out:
J
Jiri Denemark 已提交
554 555 556 557
    VIR_FREE(string);

    return ret;

558
 error:
J
Jiri Denemark 已提交
559
    ret = -1;
560
 ignore:
J
Jiri Denemark 已提交
561 562 563 564 565
    x86VendorFree(vendor);
    goto out;
}


J
Jiri Denemark 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
static struct x86_feature *
x86FeatureNew(void)
{
    struct x86_feature *feature;

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

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

    return feature;
}


J
Jiri Denemark 已提交
583 584 585 586 587 588 589
static void
x86FeatureFree(struct x86_feature *feature)
{
    if (feature == NULL)
        return;

    VIR_FREE(feature->name);
590
    virCPUx86DataFree(feature->data);
J
Jiri Denemark 已提交
591 592 593 594
    VIR_FREE(feature);
}


595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
static struct x86_feature *
x86FeatureCopy(const struct x86_feature *src)
{
    struct x86_feature *feature;

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

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

    if ((feature->data = x86DataCopy(src->data)) == NULL)
        goto error;

    return feature;

 error:
    x86FeatureFree(feature);
    return NULL;
}


J
Jiri Denemark 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
static struct x86_feature *
x86FeatureFind(const struct x86_map *map,
               const char *name)
{
    struct x86_feature *feature;

    feature = map->features;
    while (feature != NULL) {
        if (STREQ(feature->name, name))
            return feature;

        feature = feature->next;
    }

    return NULL;
}


635 636 637
static char *
x86FeatureNames(const struct x86_map *map,
                const char *separator,
638
                virCPUx86Data *data)
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
{
    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);
}


663 664
static int
x86ParseCPUID(xmlXPathContextPtr ctxt,
665
              virCPUx86CPUID *cpuid)
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
{
    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 已提交
692 693
static int
x86FeatureLoad(xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
694
               struct x86_map *map)
J
Jiri Denemark 已提交
695 696
{
    xmlNodePtr *nodes = NULL;
697
    xmlNodePtr ctxt_node = ctxt->node;
J
Jiri Denemark 已提交
698
    struct x86_feature *feature;
699
    virCPUx86CPUID cpuid;
J
Jiri Denemark 已提交
700
    int ret = 0;
701
    size_t i;
J
Jiri Denemark 已提交
702
    int n;
703 704 705
    char *str = NULL;
    bool migratable = true;
    struct x86_feature *migrate_blocker = NULL;
J
Jiri Denemark 已提交
706

J
Jiri Denemark 已提交
707
    if (!(feature = x86FeatureNew()))
708
        goto error;
J
Jiri Denemark 已提交
709

710
    feature->name = virXPathString("string(@name)", ctxt);
J
Jiri Denemark 已提交
711
    if (feature->name == NULL) {
712 713
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU feature name"));
J
Jiri Denemark 已提交
714 715 716 717
        goto ignore;
    }

    if (x86FeatureFind(map, feature->name)) {
718 719
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature %s already defined"), feature->name);
J
Jiri Denemark 已提交
720 721 722
        goto ignore;
    }

723 724 725 726
    str = virXPathString("string(@migratable)", ctxt);
    if (STREQ_NULLABLE(str, "no"))
        migratable = false;

727
    n = virXPathNodeSet("./cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
728 729 730 731 732
    if (n < 0)
        goto ignore;

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
733
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
734
            virReportError(VIR_ERR_INTERNAL_ERROR,
735 736
                           _("Invalid cpuid[%zu] in %s feature"),
                           i, feature->name);
J
Jiri Denemark 已提交
737 738
            goto ignore;
        }
739
        if (virCPUx86DataAddCPUID(feature->data, &cpuid))
740
            goto error;
J
Jiri Denemark 已提交
741 742
    }

743 744 745 746 747 748 749 750
    if (!migratable) {
        if ((migrate_blocker = x86FeatureCopy(feature)) == NULL)
            goto error;

        migrate_blocker->next = map->migrate_blockers;
        map->migrate_blockers = migrate_blocker;
    }

751
    if (map->features == NULL) {
J
Jiri Denemark 已提交
752
        map->features = feature;
753
    } else {
J
Jiri Denemark 已提交
754 755 756 757
        feature->next = map->features;
        map->features = feature;
    }

758
 out:
759 760
    ctxt->node = ctxt_node;
    VIR_FREE(nodes);
761
    VIR_FREE(str);
762

J
Jiri Denemark 已提交
763 764
    return ret;

765
 error:
J
Jiri Denemark 已提交
766 767
    ret = -1;

768
 ignore:
J
Jiri Denemark 已提交
769
    x86FeatureFree(feature);
770
    x86FeatureFree(migrate_blocker);
J
Jiri Denemark 已提交
771 772 773 774
    goto out;
}


775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
static virCPUx86Data *
x86DataFromCPUFeatures(virCPUDefPtr cpu,
                       const struct x86_map *map)
{
    virCPUx86Data *data;
    size_t i;

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

    for (i = 0; i < cpu->nfeatures; i++) {
        const struct x86_feature *feature;
        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 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
static struct x86_model *
x86ModelNew(void)
{
    struct x86_model *model;

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

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

    return model;
}


J
Jiri Denemark 已提交
822 823 824 825 826 827 828
static void
x86ModelFree(struct x86_model *model)
{
    if (model == NULL)
        return;

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


static struct x86_model *
x86ModelCopy(const struct x86_model *model)
{
    struct x86_model *copy;

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 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

    return copy;
}


static struct x86_model *
x86ModelFind(const struct x86_map *map,
             const char *name)
{
    struct x86_model *model;

    model = map->models;
    while (model != NULL) {
        if (STREQ(model->name, name))
            return model;

        model = model->next;
    }

    return NULL;
}


static struct x86_model *
871
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
872 873 874 875
                const struct x86_map *map,
                int policy)
{
    struct x86_model *model = NULL;
876
    size_t i;
J
Jiri Denemark 已提交
877

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

        if ((model = x86ModelCopy(model)) == NULL)
886
            goto error;
J
Jiri Denemark 已提交
887
    } else if (!(model = x86ModelNew())) {
888
        goto error;
J
Jiri Denemark 已提交
889
    } else if (cpu->type == VIR_CPU_TYPE_HOST) {
890
        return model;
J
Jiri Denemark 已提交
891
    }
J
Jiri Denemark 已提交
892 893 894 895 896 897 898 899 900

    for (i = 0; i < cpu->nfeatures; i++) {
        const struct x86_feature *feature;

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

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

J
Jiri Denemark 已提交
906
        if (x86DataAdd(model->data, feature->data))
907
            goto error;
J
Jiri Denemark 已提交
908 909 910 911
    }

    return model;

912
 error:
J
Jiri Denemark 已提交
913 914 915 916 917
    x86ModelFree(model);
    return NULL;
}


918 919
static int
x86ModelSubtractCPU(struct x86_model *model,
920
                    const virCPUDef *cpu,
921 922 923
                    const struct x86_map *map)
{
    const struct x86_model *cpu_model;
924
    size_t i;
925 926

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

J
Jiri Denemark 已提交
933
    x86DataSubtract(model->data, cpu_model->data);
934 935 936 937 938

    for (i = 0; i < cpu->nfeatures; i++) {
        const struct x86_feature *feature;

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

J
Jiri Denemark 已提交
945
        x86DataSubtract(model->data, feature->data);
946 947 948 949 950 951
    }

    return 0;
}


J
Jiri Denemark 已提交
952 953 954 955 956
static enum compare_result
x86ModelCompare(const struct x86_model *model1,
                const struct x86_model *model2)
{
    enum compare_result result = EQUAL;
957 958
    struct virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(model1->data);
    struct virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(model2->data);
959 960
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
961

J
Jiri Denemark 已提交
962
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
J
Jiri Denemark 已提交
963 964
        enum compare_result match = SUPERSET;

J
Jiri Denemark 已提交
965
        if ((cpuid2 = x86DataCpuid(model2->data, cpuid1->function))) {
J
Jiri Denemark 已提交
966 967 968 969 970 971 972 973 974 975 976 977
            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 已提交
978
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
J
Jiri Denemark 已提交
979 980
        enum compare_result match = SUBSET;

J
Jiri Denemark 已提交
981
        if ((cpuid1 = x86DataCpuid(model1->data, cpuid2->function))) {
J
Jiri Denemark 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
            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 已提交
1000
             struct x86_map *map)
J
Jiri Denemark 已提交
1001 1002
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
1003
    struct x86_model *model;
J
Jiri Denemark 已提交
1004
    char *vendor = NULL;
J
Jiri Denemark 已提交
1005
    int ret = 0;
1006
    size_t i;
J
Jiri Denemark 已提交
1007 1008
    int n;

J
Jiri Denemark 已提交
1009
    if (!(model = x86ModelNew()))
1010
        goto error;
J
Jiri Denemark 已提交
1011

1012
    model->name = virXPathString("string(@name)", ctxt);
J
Jiri Denemark 已提交
1013
    if (model->name == NULL) {
1014 1015
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
J
Jiri Denemark 已提交
1016 1017 1018
        goto ignore;
    }

1019
    if (virXPathNode("./model", ctxt) != NULL) {
J
Jiri Denemark 已提交
1020 1021 1022
        const struct x86_model *ancestor;
        char *name;

1023
        name = virXPathString("string(./model/@name)", ctxt);
J
Jiri Denemark 已提交
1024
        if (name == NULL) {
1025 1026 1027
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1028 1029 1030 1031
            goto ignore;
        }

        if ((ancestor = x86ModelFind(map, name)) == NULL) {
1032 1033 1034
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
1035 1036 1037 1038 1039 1040
            VIR_FREE(name);
            goto ignore;
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1041
        model->vendor = ancestor->vendor;
1042
        virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
1043
        if (!(model->data = x86DataCopy(ancestor->data)))
1044
            goto error;
J
Jiri Denemark 已提交
1045 1046
    }

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

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

1064
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1065 1066 1067 1068 1069 1070 1071 1072
    if (n < 0)
        goto ignore;

    for (i = 0; i < n; i++) {
        const struct x86_feature *feature;
        char *name;

        if ((name = virXMLPropString(nodes[i], "name")) == NULL) {
1073 1074
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
J
Jiri Denemark 已提交
1075 1076 1077 1078
            goto ignore;
        }

        if ((feature = x86FeatureFind(map, name)) == NULL) {
1079 1080 1081
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1082 1083 1084 1085 1086
            VIR_FREE(name);
            goto ignore;
        }
        VIR_FREE(name);

J
Jiri Denemark 已提交
1087
        if (x86DataAdd(model->data, feature->data))
1088
            goto error;
J
Jiri Denemark 已提交
1089 1090
    }

1091
    if (map->models == NULL) {
J
Jiri Denemark 已提交
1092
        map->models = model;
1093
    } else {
J
Jiri Denemark 已提交
1094 1095 1096 1097
        model->next = map->models;
        map->models = model;
    }

1098
 out:
J
Jiri Denemark 已提交
1099
    VIR_FREE(vendor);
1100
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1101 1102
    return ret;

1103
 error:
J
Jiri Denemark 已提交
1104 1105
    ret = -1;

1106
 ignore:
J
Jiri Denemark 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    x86ModelFree(model);
    goto out;
}


static void
x86MapFree(struct x86_map *map)
{
    if (map == NULL)
        return;

    while (map->features != NULL) {
        struct x86_feature *feature = map->features;
        map->features = feature->next;
        x86FeatureFree(feature);
    }

    while (map->models != NULL) {
        struct x86_model *model = map->models;
        map->models = model->next;
        x86ModelFree(model);
    }

1130 1131 1132 1133 1134 1135
    while (map->vendors != NULL) {
        struct x86_vendor *vendor = map->vendors;
        map->vendors = vendor->next;
        x86VendorFree(vendor);
    }

1136 1137 1138 1139 1140 1141
    while (map->migrate_blockers != NULL) {
        struct x86_feature *migrate_blocker = map->migrate_blockers;
        map->migrate_blockers = migrate_blocker->next;
        x86FeatureFree(migrate_blocker);
    }

J
Jiri Denemark 已提交
1142 1143 1144 1145
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1146
static int
1147
x86MapLoadCallback(cpuMapElement element,
J
Jiri Denemark 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
                   xmlXPathContextPtr ctxt,
                   void *data)
{
    struct x86_map *map = data;

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


1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
static int
x86MapLoadInternalFeatures(struct x86_map *map)
{
    size_t i;
    struct x86_feature *feature = NULL;

    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;

        if (map->features == NULL) {
            map->features = feature;
        } else {
            feature->next = map->features;
            map->features = feature;
        }

        feature = NULL;
    }

    return 0;

1204
 error:
1205 1206 1207 1208 1209
    x86FeatureFree(feature);
    return -1;
}


J
Jiri Denemark 已提交
1210
static struct x86_map *
1211
virCPUx86LoadMap(void)
J
Jiri Denemark 已提交
1212 1213 1214
{
    struct x86_map *map;

1215
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1216 1217
        return NULL;

J
Jiri Denemark 已提交
1218
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1219 1220
        goto error;

1221 1222 1223
    if (x86MapLoadInternalFeatures(map) < 0)
        goto error;

J
Jiri Denemark 已提交
1224 1225
    return map;

1226
 error:
J
Jiri Denemark 已提交
1227 1228 1229 1230 1231
    x86MapFree(map);
    return NULL;
}


1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
int
virCPUx86MapOnceInit(void)
{
    if (!(virCPUx86Map = virCPUx86LoadMap()))
        return -1;

    return 0;
}


static const struct x86_map *
virCPUx86GetMap(void)
{
    if (virCPUx86MapInitialize() < 0)
        return NULL;

    return virCPUx86Map;
}


1252 1253 1254
static char *
x86CPUDataFormat(const virCPUData *data)
{
1255
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data->data.x86);
1256
    virCPUx86CPUID *cpuid;
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    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");

1270
    if (virBufferCheckError(&buf) < 0)
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
x86CPUDataParse(const char *xmlStr)
{
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1284
    virCPUx86Data *data = NULL;
1285
    virCPUx86CPUID cpuid;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
    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;
        }
1313
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
1314 1315 1316
            goto cleanup;
    }

1317
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1318

1319
 cleanup:
1320 1321 1322
    VIR_FREE(nodes);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1323
    virCPUx86DataFree(data);
1324 1325 1326 1327
    return cpuData;
}


1328 1329 1330
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1331
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1332 1333 1334 1335 1336 1337 1338
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
#define virX86CpuIncompatible(MSG, CPU_DEF)                             \
        do {                                                            \
            char *flagsStr = NULL;                                      \
1339 1340 1341 1342
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) {  \
                virReportOOMError();                                    \
                goto error;                                             \
            }                                                           \
1343 1344 1345
            if (message &&                                              \
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
                VIR_FREE(flagsStr);                                     \
1346
                goto error;                                             \
1347 1348 1349 1350 1351 1352
            }                                                           \
            VIR_DEBUG("%s: %s", MSG, flagsStr);                         \
            VIR_FREE(flagsStr);                                         \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;                         \
        } while (0)

1353

J
Jiri Denemark 已提交
1354 1355 1356
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1357
           virCPUDataPtr *guest,
1358
           char **message)
J
Jiri Denemark 已提交
1359
{
1360
    const struct x86_map *map = NULL;
J
Jiri Denemark 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    struct x86_model *host_model = NULL;
    struct x86_model *cpu_force = NULL;
    struct x86_model *cpu_require = NULL;
    struct x86_model *cpu_optional = NULL;
    struct x86_model *cpu_disable = NULL;
    struct x86_model *cpu_forbid = NULL;
    struct x86_model *diff = NULL;
    struct x86_model *guest_model = NULL;
    virCPUCompareResult ret;
    enum compare_result result;
J
Jiri Denemark 已提交
1371
    virArch arch;
1372
    size_t i;
J
Jiri Denemark 已提交
1373

1374
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1375 1376 1377
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1378
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1379 1380 1381 1382 1383
                found = true;
                break;
            }
        }

1384
        if (!found) {
1385 1386
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1387 1388 1389
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1390
                            virArchToString(cpu->arch)) < 0)
1391
                goto error;
J
Jiri Denemark 已提交
1392
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1393
        }
J
Jiri Denemark 已提交
1394 1395 1396
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1397 1398
    }

J
Jiri Denemark 已提交
1399 1400 1401 1402
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1403 1404 1405 1406 1407
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1408
            goto error;
1409

J
Jiri Denemark 已提交
1410 1411 1412
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1413
    if (!(map = virCPUx86GetMap()) ||
1414
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1415 1416 1417 1418 1419
        !(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 已提交
1420 1421
        goto error;

1422 1423 1424 1425
    x86DataIntersect(cpu_forbid->data, host_model->data);
    if (!x86DataIsEmpty(cpu_forbid->data)) {
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
                              cpu_forbid->data);
1426
        goto cleanup;
J
Jiri Denemark 已提交
1427 1428
    }

1429 1430 1431 1432 1433
    /* 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 已提交
1434
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1435 1436
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1437 1438 1439 1440
        x86DataSubtract(cpu_require->data, host_model->data);
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
                              cpu_require->data);
1441
        goto cleanup;
J
Jiri Denemark 已提交
1442 1443 1444 1445
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

J
Jiri Denemark 已提交
1446
    if ((diff = x86ModelCopy(host_model)) == NULL)
1447
        goto error;
J
Jiri Denemark 已提交
1448

J
Jiri Denemark 已提交
1449 1450 1451 1452
    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 已提交
1453

J
Jiri Denemark 已提交
1454 1455
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1456 1457 1458 1459

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1460 1461 1462
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
                              diff->data);
1463
        goto cleanup;
J
Jiri Denemark 已提交
1464 1465 1466
    }

    if (guest != NULL) {
1467
        virCPUx86Data *guestData;
1468

J
Jiri Denemark 已提交
1469
        if ((guest_model = x86ModelCopy(host_model)) == NULL)
1470
            goto error;
J
Jiri Denemark 已提交
1471 1472 1473

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

J
Jiri Denemark 已提交
1476
        if (x86DataAdd(guest_model->data, cpu_force->data))
1477
            goto error;
J
Jiri Denemark 已提交
1478

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

1481
        if (!(guestData = x86DataCopy(guest_model->data)) ||
1482
            !(*guest = virCPUx86MakeData(arch, &guestData))) {
1483
            virCPUx86DataFree(guestData);
1484
            goto error;
1485
        }
J
Jiri Denemark 已提交
1486 1487
    }

1488
 cleanup:
J
Jiri Denemark 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
    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;

1500
 error:
J
Jiri Denemark 已提交
1501
    ret = VIR_CPU_COMPARE_ERROR;
1502
    goto cleanup;
J
Jiri Denemark 已提交
1503
}
1504
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1505 1506 1507 1508


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
1509 1510
           virCPUDefPtr cpu,
           bool failIncomaptible)
J
Jiri Denemark 已提交
1511
{
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    virCPUCompareResult ret;
    char *message = NULL;

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

    if (failIncomaptible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
        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 已提交
1528 1529 1530 1531 1532 1533
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1534
             virCPUDataPtr *data,
1535
             char **message)
J
Jiri Denemark 已提交
1536
{
1537
    return x86Compute(host, guest, data, message);
J
Jiri Denemark 已提交
1538 1539
}

1540

J
Jiri Denemark 已提交
1541 1542
static int
x86Decode(virCPUDefPtr cpu,
1543
          const virCPUx86Data *data,
1544
          const char **models,
1545
          unsigned int nmodels,
1546 1547
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1548 1549
{
    int ret = -1;
1550
    const struct x86_map *map;
J
Jiri Denemark 已提交
1551
    const struct x86_model *candidate;
1552 1553
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1554 1555 1556
    virCPUx86Data *copy = NULL;
    virCPUx86Data *features = NULL;
    const virCPUx86Data *cpuData = NULL;
1557
    size_t i;
J
Jiri Denemark 已提交
1558

1559 1560
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, -1);

1561
    if (!data || !(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1562 1563 1564 1565
        return -1;

    candidate = map->models;
    while (candidate != NULL) {
1566
        if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
1567 1568
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1569 1570 1571
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
                    goto out;
                } 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);
            }
1582
            goto next;
J
Jiri Denemark 已提交
1583 1584
        }

1585 1586 1587
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto out;

J
Jiri Denemark 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596
        if (candidate->vendor && cpuCandidate->vendor &&
            STRNEQ(candidate->vendor->name, cpuCandidate->vendor)) {
            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
                      candidate->vendor->name, candidate->name,
                      cpuCandidate->vendor);
            virCPUDefFree(cpuCandidate);
            goto next;
        }

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
        if (cpu->type == VIR_CPU_TYPE_HOST) {
            cpuCandidate->type = VIR_CPU_TYPE_HOST;
            for (i = 0; i < cpuCandidate->nfeatures; i++) {
                switch (cpuCandidate->features[i].policy) {
                case VIR_CPU_FEATURE_DISABLE:
                    virCPUDefFree(cpuCandidate);
                    goto next;
                default:
                    cpuCandidate->features[i].policy = -1;
                }
            }
        }

1610 1611 1612
        if (preferred && STREQ(cpuCandidate->model, preferred)) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1613
            cpuData = candidate->data;
1614 1615 1616
            break;
        }

1617 1618 1619 1620
        if (cpuModel == NULL
            || cpuModel->nfeatures > cpuCandidate->nfeatures) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1621 1622
            cpuData = candidate->data;
        } else {
1623
            virCPUDefFree(cpuCandidate);
1624
        }
1625

J
Jiri Denemark 已提交
1626 1627 1628 1629
    next:
        candidate = candidate->next;
    }

1630
    if (cpuModel == NULL) {
1631 1632
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1633 1634 1635
        goto out;
    }

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) {
        if (!(copy = x86DataCopy(cpuData)) ||
            !(features = x86DataFromCPUFeatures(cpuModel, map)))
            goto out;

        x86DataSubtract(copy, features);
        if (x86DataToCPUFeatures(cpuModel, VIR_CPU_FEATURE_REQUIRE,
                                 copy, map) < 0)
            goto out;
    }

1647
    cpu->model = cpuModel->model;
J
Jiri Denemark 已提交
1648
    cpu->vendor = cpuModel->vendor;
1649 1650 1651
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1652 1653 1654

    ret = 0;

1655
 out:
1656
    virCPUDefFree(cpuModel);
1657 1658
    virCPUx86DataFree(copy);
    virCPUx86DataFree(features);
J
Jiri Denemark 已提交
1659 1660 1661
    return ret;
}

1662 1663
static int
x86DecodeCPUData(virCPUDefPtr cpu,
1664
                 const virCPUData *data,
1665 1666
                 const char **models,
                 unsigned int nmodels,
1667 1668
                 const char *preferred,
                 unsigned int flags)
1669
{
1670
    return x86Decode(cpu, data->data.x86, models, nmodels, preferred, flags);
1671
}
J
Jiri Denemark 已提交
1672

1673

1674
static virCPUx86Data *
1675
x86EncodePolicy(const virCPUDef *cpu,
J
Jiri Denemark 已提交
1676
                const struct x86_map *map,
1677
                virCPUFeaturePolicy policy)
J
Jiri Denemark 已提交
1678 1679
{
    struct x86_model *model;
1680
    virCPUx86Data *data = NULL;
J
Jiri Denemark 已提交
1681 1682 1683 1684

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

J
Jiri Denemark 已提交
1685 1686
    data = model->data;
    model->data = NULL;
J
Jiri Denemark 已提交
1687 1688 1689 1690 1691 1692 1693
    x86ModelFree(model);

    return data;
}


static int
J
Jiri Denemark 已提交
1694
x86Encode(virArch arch,
1695
          const virCPUDef *cpu,
1696 1697 1698 1699 1700 1701
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
1702
{
1703
    const struct x86_map *map = NULL;
1704 1705 1706 1707 1708 1709
    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 已提交
1710

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

1724
    if ((map = virCPUx86GetMap()) == NULL)
J
Jiri Denemark 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
        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 已提交
1757 1758 1759 1760
    if (vendor) {
        const struct x86_vendor *v = NULL;

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
1761 1762
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s not found"), cpu->vendor);
J
Jiri Denemark 已提交
1763 1764 1765 1766 1767
            goto error;
        }

        if (v &&
            (VIR_ALLOC(data_vendor) < 0 ||
1768
             virCPUx86DataAddCPUID(data_vendor, &v->cpuid) < 0)) {
J
Jiri Denemark 已提交
1769 1770 1771 1772
            goto error;
        }
    }

1773
    if (forced &&
1774
        !(*forced = virCPUx86MakeData(arch, &data_forced)))
1775 1776
        goto error;
    if (required &&
1777
        !(*required = virCPUx86MakeData(arch, &data_required)))
1778 1779
        goto error;
    if (optional &&
1780
        !(*optional = virCPUx86MakeData(arch, &data_optional)))
1781 1782
        goto error;
    if (disabled &&
1783
        !(*disabled = virCPUx86MakeData(arch, &data_disabled)))
1784 1785
        goto error;
    if (forbidden &&
1786
        !(*forbidden = virCPUx86MakeData(arch, &data_forbidden)))
1787 1788
        goto error;
    if (vendor &&
1789
        !(*vendor = virCPUx86MakeData(arch, &data_vendor)))
1790
        goto error;
J
Jiri Denemark 已提交
1791

1792
    return 0;
J
Jiri Denemark 已提交
1793

1794
 error:
1795 1796 1797 1798 1799 1800
    virCPUx86DataFree(data_forced);
    virCPUx86DataFree(data_required);
    virCPUx86DataFree(data_optional);
    virCPUx86DataFree(data_disabled);
    virCPUx86DataFree(data_forbidden);
    virCPUx86DataFree(data_vendor);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    if (forced)
        x86FreeCPUData(*forced);
    if (required)
        x86FreeCPUData(*required);
    if (optional)
        x86FreeCPUData(*optional);
    if (disabled)
        x86FreeCPUData(*disabled);
    if (forbidden)
        x86FreeCPUData(*forbidden);
    if (vendor)
        x86FreeCPUData(*vendor);
1813
    return -1;
J
Jiri Denemark 已提交
1814 1815 1816 1817 1818
}


#if HAVE_CPUID
static inline void
1819
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
1820
{
1821
# if __x86_64__
1822 1823 1824 1825
    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 已提交
1826 1827 1828 1829 1830
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function));
1831
# else
J
Jiri Denemark 已提交
1832 1833 1834 1835
    /* 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;"
1836 1837 1838
        "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 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function)
        : "cc");
1848
# endif
J
Jiri Denemark 已提交
1849 1850 1851 1852
}


static int
1853
cpuidSet(uint32_t base, virCPUx86Data *data)
J
Jiri Denemark 已提交
1854 1855 1856
{
    uint32_t max;
    uint32_t i;
1857
    virCPUx86CPUID cpuid = { base, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
1858 1859

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

1862 1863
    for (i = base; i <= max; i++) {
        cpuid.function = i;
J
Jiri Denemark 已提交
1864
        cpuidCall(&cpuid);
1865 1866
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
J
Jiri Denemark 已提交
1867 1868
    }

1869
    return 0;
J
Jiri Denemark 已提交
1870 1871 1872
}


1873
static virCPUDataPtr
1874
x86NodeData(virArch arch)
J
Jiri Denemark 已提交
1875
{
1876
    virCPUDataPtr cpuData = NULL;
1877
    virCPUx86Data *data;
J
Jiri Denemark 已提交
1878

1879
    if (VIR_ALLOC(data) < 0)
J
Jiri Denemark 已提交
1880 1881
        return NULL;

1882
    if (cpuidSet(CPUX86_BASIC, data) < 0)
J
Jiri Denemark 已提交
1883 1884
        goto error;

1885
    if (cpuidSet(CPUX86_EXTENDED, data) < 0)
J
Jiri Denemark 已提交
1886 1887
        goto error;

1888
    if (!(cpuData = virCPUx86MakeData(arch, &data)))
1889 1890 1891
        goto error;

    return cpuData;
J
Jiri Denemark 已提交
1892

1893
 error:
1894
    virCPUx86DataFree(data);
J
Jiri Denemark 已提交
1895 1896 1897 1898 1899 1900

    return NULL;
}
#endif


1901 1902 1903 1904
static virCPUDefPtr
x86Baseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
1905 1906
            unsigned int nmodels,
            unsigned int flags)
1907
{
1908
    const struct x86_map *map = NULL;
1909 1910
    struct x86_model *base_model = NULL;
    virCPUDefPtr cpu = NULL;
1911
    size_t i;
J
Jiri Denemark 已提交
1912 1913
    const struct x86_vendor *vendor = NULL;
    struct x86_model *model = NULL;
1914
    bool outputVendor = true;
1915 1916
    const char *modelName;
    bool matchingNames = true;
1917

1918
    if (!(map = virCPUx86GetMap()))
1919 1920
        goto error;

1921
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1922 1923
        goto error;

1924
    if (VIR_ALLOC(cpu) < 0)
1925
        goto error;
1926 1927

    cpu->arch = cpus[0]->arch;
1928 1929
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1930

1931
    if (!cpus[0]->vendor) {
1932
        outputVendor = false;
1933
    } else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
1934 1935
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Unknown CPU vendor %s"), cpus[0]->vendor);
J
Jiri Denemark 已提交
1936 1937 1938
        goto error;
    }

1939
    modelName = cpus[0]->model;
1940
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
1941 1942
        const char *vn = NULL;

1943 1944 1945 1946 1947 1948 1949 1950 1951
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

1952
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
1953 1954
            goto error;

J
Jiri Denemark 已提交
1955 1956
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
1957 1958 1959
            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 已提交
1960 1961 1962
            goto error;
        }

1963
        if (cpus[i]->vendor) {
J
Jiri Denemark 已提交
1964
            vn = cpus[i]->vendor;
1965
        } else {
1966 1967 1968 1969
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
1970 1971 1972 1973

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
1974 1975
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("Unknown CPU vendor %s"), vn);
J
Jiri Denemark 已提交
1976 1977 1978
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
1979 1980
                virReportError(VIR_ERR_OPERATION_FAILED,
                               "%s", _("CPU vendors do not match"));
J
Jiri Denemark 已提交
1981 1982 1983 1984
                goto error;
            }
        }

J
Jiri Denemark 已提交
1985
        x86DataIntersect(base_model->data, model->data);
1986
        x86ModelFree(model);
J
Jiri Denemark 已提交
1987
        model = NULL;
1988 1989
    }

J
Jiri Denemark 已提交
1990
    if (x86DataIsEmpty(base_model->data)) {
1991 1992
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
1993 1994 1995
        goto error;
    }

1996
    if (vendor && virCPUx86DataAddCPUID(base_model->data, &vendor->cpuid) < 0)
1997
        goto error;
J
Jiri Denemark 已提交
1998

1999
    if (x86Decode(cpu, base_model->data, models, nmodels, modelName, flags) < 0)
2000 2001
        goto error;

2002 2003 2004
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2005 2006 2007
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2008 2009
    cpu->arch = VIR_ARCH_NONE;

2010
 cleanup:
2011 2012 2013 2014
    x86ModelFree(base_model);

    return cpu;

2015
 error:
J
Jiri Denemark 已提交
2016
    x86ModelFree(model);
2017 2018 2019 2020 2021 2022
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


2023
static int
2024
x86UpdateCustom(virCPUDefPtr guest,
2025
                const virCPUDef *host)
2026 2027
{
    int ret = -1;
2028
    size_t i;
2029
    const struct x86_map *map;
2030 2031
    struct x86_model *host_model = NULL;

2032
    if (!(map = virCPUx86GetMap()) ||
2033
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
2034 2035 2036 2037 2038 2039
        goto cleanup;

    for (i = 0; i < guest->nfeatures; i++) {
        if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
            const struct x86_feature *feature;
            if (!(feature = x86FeatureFind(map, guest->features[i].name))) {
2040 2041 2042
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unknown CPU feature %s"),
                               guest->features[i].name);
2043 2044 2045
                goto cleanup;
            }

J
Jiri Denemark 已提交
2046
            if (x86DataIsSubset(host_model->data, feature->data))
2047 2048 2049 2050 2051 2052 2053 2054
                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 已提交
2055 2056 2057
        if (x86ModelSubtractCPU(host_model, guest, map) ||
            x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
                                 host_model->data, map))
2058 2059 2060 2061 2062
            goto cleanup;
    }

    ret = 0;

2063
 cleanup:
2064 2065 2066 2067
    x86ModelFree(host_model);
    return ret;
}

2068 2069 2070

static int
x86UpdateHostModel(virCPUDefPtr guest,
2071 2072
                   const virCPUDef *host,
                   bool passthrough)
2073
{
2074
    virCPUDefPtr oldguest = NULL;
2075 2076
    const struct x86_map *map;
    const struct x86_feature *feat;
2077
    size_t i;
2078
    int ret = -1;
2079

2080 2081
    if (!(map = virCPUx86GetMap()))
        goto cleanup;
2082 2083 2084

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
2085
        goto cleanup;
2086 2087 2088

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
2089
        goto cleanup;
2090

2091 2092 2093 2094 2095
    /* 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 已提交
2096 2097
            if (STREQ(feat->name, guest->features[i].name)) {
                VIR_FREE(guest->features[i].name);
2098
                VIR_DELETE_ELEMENT_INPLACE(guest->features, i, guest->nfeatures);
J
Ján Tomko 已提交
2099
            }
2100 2101
        }
    }
2102
    for (i = 0; !passthrough && i < oldguest->nfeatures; i++) {
2103 2104 2105
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
2106
            goto cleanup;
2107 2108
    }

2109 2110 2111 2112 2113
    ret = 0;

 cleanup:
    virCPUDefFree(oldguest);
    return ret;
2114 2115 2116
}


2117 2118
static int
x86Update(virCPUDefPtr guest,
2119
          const virCPUDef *host)
2120
{
2121
    switch ((virCPUMode) guest->mode) {
2122 2123 2124 2125
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2126 2127
        guest->match = VIR_CPU_MATCH_EXACT;
        return x86UpdateHostModel(guest, host, false);
2128

2129
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2130
        guest->match = VIR_CPU_MATCH_MINIMUM;
2131
        return x86UpdateHostModel(guest, host, true);
2132 2133 2134 2135 2136

    case VIR_CPU_MODE_LAST:
        break;
    }

2137 2138
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2139 2140 2141
    return -1;
}

2142 2143 2144 2145

static int
x86HasFeature(const virCPUData *data,
              const char *name)
D
Daniel P. Berrange 已提交
2146
{
2147
    const struct x86_map *map;
D
Daniel P. Berrange 已提交
2148 2149 2150
    struct x86_feature *feature;
    int ret = -1;

2151
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2152 2153 2154 2155 2156
        return -1;

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

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

2159
 cleanup:
D
Daniel P. Berrange 已提交
2160 2161
    return ret;
}
2162

2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
static int
x86GetModels(char ***models)
{
    const struct x86_map *map;
    struct x86_model *model;
    char *name;
    size_t nmodels = 0;

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

    if (models && VIR_ALLOC_N(*models, 0) < 0)
        goto error;

    model = map->models;
    while (model != NULL) {
2179 2180 2181
        if (models) {
            if (VIR_STRDUP(name, model->name) < 0)
                goto error;
2182

2183 2184 2185 2186 2187
            if (VIR_APPEND_ELEMENT(*models, nmodels, name) < 0)
                goto error;
        } else {
            nmodels++;
        }
2188 2189 2190 2191 2192 2193 2194

        model = model->next;
    }

    return nmodels;

 error:
2195 2196 2197 2198
    if (models) {
        virStringFreeList(*models);
        *models = NULL;
    }
2199 2200 2201
    return -1;
}

2202

J
Jiri Denemark 已提交
2203 2204 2205 2206 2207
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
    .compare    = x86Compare,
2208
    .decode     = x86DecodeCPUData,
J
Jiri Denemark 已提交
2209
    .encode     = x86Encode,
2210
    .free       = x86FreeCPUData,
J
Jiri Denemark 已提交
2211 2212 2213 2214 2215
#if HAVE_CPUID
    .nodeData   = x86NodeData,
#else
    .nodeData   = NULL,
#endif
2216
    .guestData  = x86GuestData,
2217
    .baseline   = x86Baseline,
2218
    .update     = x86Update,
D
Daniel P. Berrange 已提交
2219
    .hasFeature = x86HasFeature,
2220 2221
    .dataFormat = x86CPUDataFormat,
    .dataParse  = x86CPUDataParse,
2222
    .getModels  = x86GetModels,
J
Jiri Denemark 已提交
2223
};