cpu_x86.c 42.7 KB
Newer Older
J
Jiri Denemark 已提交
1 2 3
/*
 * cpu_x86.c: CPU driver for CPUs with x86 compatible CPUID instruction
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2009-2011 Red Hat, Inc.
J
Jiri Denemark 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Authors:
 *      Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

#include <stdint.h>

#include "logging.h"
#include "memory.h"
30
#include "util.h"
J
Jiri Denemark 已提交
31 32 33 34 35 36 37
#include "cpu.h"
#include "cpu_map.h"
#include "cpu_x86.h"


#define VIR_FROM_THIS VIR_FROM_CPU

J
Jiri Denemark 已提交
38 39
#define VENDOR_STRING_LENGTH    12

J
Jiri Denemark 已提交
40
static const struct cpuX86cpuid cpuidNull = { 0, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
41

J
Jiri Denemark 已提交
42 43
static const char *archs[] = { "i686", "x86_64" };

J
Jiri Denemark 已提交
44 45 46 47 48 49 50
struct x86_vendor {
    char *name;
    struct cpuX86cpuid cpuid;

    struct x86_vendor *next;
};

J
Jiri Denemark 已提交
51 52
struct x86_feature {
    char *name;
J
Jiri Denemark 已提交
53
    union cpuData *data;
J
Jiri Denemark 已提交
54 55 56 57 58 59

    struct x86_feature *next;
};

struct x86_model {
    char *name;
J
Jiri Denemark 已提交
60
    const struct x86_vendor *vendor;
J
Jiri Denemark 已提交
61
    union cpuData *data;
J
Jiri Denemark 已提交
62 63 64 65 66

    struct x86_model *next;
};

struct x86_map {
J
Jiri Denemark 已提交
67
    struct x86_vendor *vendors;
J
Jiri Denemark 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
    struct x86_feature *features;
    struct x86_model *models;
};


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


J
Jiri Denemark 已提交
81 82 83 84 85
struct data_iterator {
    union cpuData *data;
    int pos;
    bool extended;
};
J
Jiri Denemark 已提交
86 87


J
Jiri Denemark 已提交
88 89 90 91 92 93 94 95 96 97 98
#define DATA_ITERATOR_INIT(data) \
    { data, -1, false }


static void
x86DataIteratorInit(struct data_iterator *iter,
                    union cpuData *data)
{
    struct data_iterator init = DATA_ITERATOR_INIT(data);

    *iter = init;
J
Jiri Denemark 已提交
99 100 101
}


J
Jiri Denemark 已提交
102
static int
J
Jiri Denemark 已提交
103 104 105 106 107 108 109 110 111 112
x86cpuidMatch(const struct cpuX86cpuid *cpuid1,
              const struct cpuX86cpuid *cpuid2)
{
    return (cpuid1->eax == cpuid2->eax &&
            cpuid1->ebx == cpuid2->ebx &&
            cpuid1->ecx == cpuid2->ecx &&
            cpuid1->edx == cpuid2->edx);
}


J
Jiri Denemark 已提交
113
static int
J
Jiri Denemark 已提交
114 115 116 117 118 119 120 121 122 123
x86cpuidMatchMasked(const struct cpuX86cpuid *cpuid,
                    const struct cpuX86cpuid *mask)
{
    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 已提交
124
static int
J
Jiri Denemark 已提交
125 126 127 128 129 130 131 132 133 134
x86cpuidMatchAny(const struct cpuX86cpuid *cpuid,
                 const struct cpuX86cpuid *mask)
{
    return ((cpuid->eax & mask->eax) ||
            (cpuid->ebx & mask->ebx) ||
            (cpuid->ecx & mask->ecx) ||
            (cpuid->edx & mask->edx));
}


J
Jiri Denemark 已提交
135
static void
J
Jiri Denemark 已提交
136 137 138 139 140 141 142 143 144 145
x86cpuidSetBits(struct cpuX86cpuid *cpuid,
                const struct cpuX86cpuid *mask)
{
    cpuid->eax |= mask->eax;
    cpuid->ebx |= mask->ebx;
    cpuid->ecx |= mask->ecx;
    cpuid->edx |= mask->edx;
}


J
Jiri Denemark 已提交
146
static void
J
Jiri Denemark 已提交
147 148 149 150 151 152 153 154 155 156
x86cpuidClearBits(struct cpuX86cpuid *cpuid,
                  const struct cpuX86cpuid *mask)
{
    cpuid->eax &= ~mask->eax;
    cpuid->ebx &= ~mask->ebx;
    cpuid->ecx &= ~mask->ecx;
    cpuid->edx &= ~mask->edx;
}


J
Jiri Denemark 已提交
157
static void
158 159 160 161 162 163 164 165 166 167
x86cpuidAndBits(struct cpuX86cpuid *cpuid,
                const struct cpuX86cpuid *mask)
{
    cpuid->eax &= mask->eax;
    cpuid->ebx &= mask->ebx;
    cpuid->ecx &= mask->ecx;
    cpuid->edx &= mask->edx;
}


J
Jiri Denemark 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/* skips all zero CPUID leafs */
static struct cpuX86cpuid *
x86DataCpuidNext(struct data_iterator *iterator)
{
    struct cpuX86cpuid *ret;
    struct cpuX86Data *data;

    if (!iterator->data)
        return NULL;

    data = &iterator->data->x86;

    do {
        ret = NULL;
        iterator->pos++;

        if (!iterator->extended) {
            if (iterator->pos < data->basic_len)
                ret = data->basic + iterator->pos;
            else {
                iterator->extended = true;
                iterator->pos = 0;
            }
        }

        if (iterator->extended && iterator->pos < data->extended_len) {
            ret = data->extended + iterator->pos;
        }
    } while (ret && x86cpuidMatch(ret, &cpuidNull));

    return ret;
}


J
Jiri Denemark 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static struct cpuX86cpuid *
x86DataCpuid(const union cpuData *data,
             uint32_t function)
{
    struct cpuX86cpuid *cpuids;
    int len;
    unsigned int i;

    if (function < CPUX86_EXTENDED) {
        cpuids = data->x86.basic;
        len = data->x86.basic_len;
        i = function;
    }
    else {
        cpuids = data->x86.extended;
        len = data->x86.extended_len;
        i = function - CPUX86_EXTENDED;
    }

J
Jiri Denemark 已提交
221
    if (i < len && !x86cpuidMatch(cpuids + i, &cpuidNull))
J
Jiri Denemark 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        return cpuids + i;
    else
        return NULL;
}


static void
x86DataFree(union cpuData *data)
{
    if (data == NULL)
        return;

    VIR_FREE(data->x86.basic);
    VIR_FREE(data->x86.extended);
    VIR_FREE(data);
}


static union cpuData *
x86DataCopy(const union cpuData *data)
{
    union cpuData *copy = NULL;
    int i;

    if (VIR_ALLOC(copy) < 0
        || VIR_ALLOC_N(copy->x86.basic, data->x86.basic_len) < 0
        || VIR_ALLOC_N(copy->x86.extended, data->x86.extended_len) < 0) {
        x86DataFree(copy);
        return NULL;
    }

    copy->x86.basic_len = data->x86.basic_len;
    for (i = 0; i < data->x86.basic_len; i++)
        copy->x86.basic[i] = data->x86.basic[i];

    copy->x86.extended_len = data->x86.extended_len;
    for (i = 0; i < data->x86.extended_len; i++)
        copy->x86.extended[i] = data->x86.extended[i];

    return copy;
}


J
Jiri Denemark 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static int
x86DataExpand(union cpuData *data,
              int basic_by,
              int extended_by)
{
    size_t i;

    if (basic_by > 0) {
        size_t len = data->x86.basic_len;
        if (VIR_EXPAND_N(data->x86.basic, data->x86.basic_len, basic_by) < 0)
            goto no_memory;

        for (i = 0; i < basic_by; i++)
            data->x86.basic[len + i].function = len + i;
    }

    if (extended_by > 0) {
        size_t len = data->x86.extended_len;
        if (VIR_EXPAND_N(data->x86.extended, data->x86.extended_len, extended_by) < 0)
            goto no_memory;

        for (i = 0; i < extended_by; i++)
            data->x86.extended[len + i].function = len + i + CPUX86_EXTENDED;
    }

    return 0;

no_memory:
    virReportOOMError();
    return -1;
}


J
Jiri Denemark 已提交
298 299 300 301
static int
x86DataAddCpuid(union cpuData *data,
                const struct cpuX86cpuid *cpuid)
{
J
Jiri Denemark 已提交
302 303
    unsigned int basic_by = 0;
    unsigned int extended_by = 0;
J
Jiri Denemark 已提交
304 305 306 307 308
    struct cpuX86cpuid **cpuids;
    unsigned int pos;

    if (cpuid->function < CPUX86_EXTENDED) {
        pos = cpuid->function;
J
Jiri Denemark 已提交
309
        basic_by = pos + 1 - data->x86.basic_len;
J
Jiri Denemark 已提交
310 311 312
        cpuids = &data->x86.basic;
    } else {
        pos = cpuid->function - CPUX86_EXTENDED;
J
Jiri Denemark 已提交
313
        extended_by = pos + 1 - data->x86.extended_len;
J
Jiri Denemark 已提交
314 315 316
        cpuids = &data->x86.extended;
    }

J
Jiri Denemark 已提交
317 318 319 320
    if (x86DataExpand(data, basic_by, extended_by) < 0)
        return -1;

    x86cpuidSetBits((*cpuids) + pos, cpuid);
J
Jiri Denemark 已提交
321

J
Jiri Denemark 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335
    return 0;
}


static int
x86DataAdd(union cpuData *data1,
           const union cpuData *data2)
{
    unsigned int i;

    if (x86DataExpand(data1,
                      data2->x86.basic_len - data1->x86.basic_len,
                      data2->x86.extended_len - data1->x86.extended_len) < 0)
        return -1;
J
Jiri Denemark 已提交
336

J
Jiri Denemark 已提交
337 338 339
    for (i = 0; i < data2->x86.basic_len; i++) {
        x86cpuidSetBits(data1->x86.basic + i,
                        data2->x86.basic + i);
J
Jiri Denemark 已提交
340 341
    }

J
Jiri Denemark 已提交
342 343 344 345
    for (i = 0; i < data2->x86.extended_len; i++) {
        x86cpuidSetBits(data1->x86.extended + i,
                        data2->x86.extended + i);
    }
J
Jiri Denemark 已提交
346 347 348 349 350

    return 0;
}


351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
static void
x86DataSubtract(union cpuData *data1,
                const union cpuData *data2)
{
    unsigned int i;
    unsigned int len;

    len = MIN(data1->x86.basic_len, data2->x86.basic_len);
    for (i = 0; i < len; i++) {
        x86cpuidClearBits(data1->x86.basic + i,
                          data2->x86.basic + i);
    }

    len = MIN(data1->x86.extended_len, data2->x86.extended_len);
    for (i = 0; i < len; i++) {
        x86cpuidClearBits(data1->x86.extended + i,
                          data2->x86.extended + i);
    }
}


J
Jiri Denemark 已提交
372 373 374
static void
x86DataIntersect(union cpuData *data1,
                 const union cpuData *data2)
375
{
J
Jiri Denemark 已提交
376 377 378
    struct data_iterator iter = DATA_ITERATOR_INIT(data1);
    struct cpuX86cpuid *cpuid1;
    struct cpuX86cpuid *cpuid2;
379

J
Jiri Denemark 已提交
380 381 382 383 384 385
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
        cpuid2 = x86DataCpuid(data2, cpuid1->function);
        if (cpuid2)
            x86cpuidAndBits(cpuid1, cpuid2);
        else
            x86cpuidClearBits(cpuid1, cpuid1);
386 387 388 389
    }
}


J
Jiri Denemark 已提交
390 391
static bool
x86DataIsEmpty(union cpuData *data)
J
Jiri Denemark 已提交
392
{
J
Jiri Denemark 已提交
393
    struct data_iterator iter = DATA_ITERATOR_INIT(data);
J
Jiri Denemark 已提交
394

J
Jiri Denemark 已提交
395 396
    return (x86DataCpuidNext(&iter) == NULL);
}
J
Jiri Denemark 已提交
397 398


J
Jiri Denemark 已提交
399 400 401 402 403 404 405 406
static bool
x86DataIsSubset(const union cpuData *data,
                const union cpuData *subset)
{

    struct data_iterator iter = DATA_ITERATOR_INIT((union cpuData *) subset);
    const struct cpuX86cpuid *cpuid;
    const struct cpuX86cpuid *cpuidSubset;
J
Jiri Denemark 已提交
407

J
Jiri Denemark 已提交
408 409 410 411
    while ((cpuidSubset = x86DataCpuidNext(&iter))) {
        if (!(cpuid = x86DataCpuid(data, cpuidSubset->function)) ||
            !x86cpuidMatchMasked(cpuid, cpuidSubset))
            return false;
J
Jiri Denemark 已提交
412 413
    }

J
Jiri Denemark 已提交
414
    return true;
J
Jiri Denemark 已提交
415 416 417
}


418 419 420 421 422 423 424 425 426 427
/* also removes all detected features from data */
static int
x86DataToCPUFeatures(virCPUDefPtr cpu,
                     int policy,
                     union cpuData *data,
                     const struct x86_map *map)
{
    const struct x86_feature *feature = map->features;

    while (feature != NULL) {
J
Jiri Denemark 已提交
428 429 430 431
        if (x86DataIsSubset(data, feature->data)) {
            x86DataSubtract(data, feature->data);
            if (virCPUDefAddFeature(cpu, feature->name, policy) < 0)
                return -1;
432 433 434 435 436 437 438 439
        }
        feature = feature->next;
    }

    return 0;
}


J
Jiri Denemark 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/* also removes bits corresponding to vendor string from data */
static const struct x86_vendor *
x86DataToVendor(union cpuData *data,
                const struct x86_map *map)
{
    const struct x86_vendor *vendor = map->vendors;
    struct cpuX86cpuid *cpuid;

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

    return NULL;
}


461 462 463 464 465 466
static virCPUDefPtr
x86DataToCPU(const union cpuData *data,
             const struct x86_model *model,
             const struct x86_map *map)
{
    virCPUDefPtr cpu;
467 468
    union cpuData *copy = NULL;
    union cpuData *modelData = NULL;
J
Jiri Denemark 已提交
469
    const struct x86_vendor *vendor;
470 471

    if (VIR_ALLOC(cpu) < 0 ||
472 473
        !(cpu->model = strdup(model->name)) ||
        !(copy = x86DataCopy(data)) ||
J
Jiri Denemark 已提交
474
        !(modelData = x86DataCopy(model->data)))
475 476
        goto no_memory;

J
Jiri Denemark 已提交
477 478 479 480
    if ((vendor = x86DataToVendor(copy, map)) &&
        !(cpu->vendor = strdup(vendor->name)))
        goto no_memory;

481 482 483 484 485
    x86DataSubtract(copy, modelData);
    x86DataSubtract(modelData, data);

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

487 488
    if (x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_REQUIRE, copy, map) ||
        x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_DISABLE, modelData, map))
489
        goto error;
490 491

cleanup:
492 493
    x86DataFree(modelData);
    x86DataFree(copy);
494 495 496
    return cpu;

no_memory:
497
    virReportOOMError();
498 499 500 501 502 503 504
error:
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


J
Jiri Denemark 已提交
505 506 507 508 509 510 511 512
static void
x86VendorFree(struct x86_vendor *vendor)
{
    if (!vendor)
        return;

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
J
Jiri Denemark 已提交
513
}
J
Jiri Denemark 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604


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)
        goto no_memory;

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                "%s", _("Missing CPU vendor name"));
        goto ignore;
    }

    if (x86VendorFind(map, vendor->name)) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                _("CPU vendor %s already defined"), vendor->name);
        goto ignore;
    }

    string = virXPathString("string(@string)", ctxt);
    if (!string) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                _("Missing vendor string for CPU vendor %s"), vendor->name);
        goto ignore;
    }
    if (strlen(string) != VENDOR_STRING_LENGTH) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                _("Invalid CPU vendor string '%s'"), string);
        goto ignore;
    }

    vendor->cpuid.function = 0;
    vendor->cpuid.ebx = (string[0]       ) |
                        (string[1]  <<  8) |
                        (string[2]  << 16) |
                        (string[3]  << 24);
    vendor->cpuid.edx = (string[4]       ) |
                        (string[5]  <<  8) |
                        (string[6]  << 16) |
                        (string[7]  << 24);
    vendor->cpuid.ecx = (string[8]       ) |
                        (string[9]  <<  8) |
                        (string[10] << 16) |
                        (string[11] << 24);

    if (!map->vendors)
        map->vendors = vendor;
    else {
        vendor->next = map->vendors;
        map->vendors = vendor;
    }

out:
    VIR_FREE(string);

    return ret;

no_memory:
    virReportOOMError();
    ret = -1;
ignore:
    x86VendorFree(vendor);
    goto out;
}


J
Jiri Denemark 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
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 已提交
622 623 624 625 626 627 628
static void
x86FeatureFree(struct x86_feature *feature)
{
    if (feature == NULL)
        return;

    VIR_FREE(feature->name);
J
Jiri Denemark 已提交
629
    x86DataFree(feature->data);
J
Jiri Denemark 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    VIR_FREE(feature);
}


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


static int
x86FeatureLoad(xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
654
               struct x86_map *map)
J
Jiri Denemark 已提交
655 656
{
    xmlNodePtr *nodes = NULL;
657
    xmlNodePtr ctxt_node = ctxt->node;
J
Jiri Denemark 已提交
658
    struct x86_feature *feature;
J
Jiri Denemark 已提交
659 660 661 662
    int ret = 0;
    int i;
    int n;

J
Jiri Denemark 已提交
663
    if (!(feature = x86FeatureNew()))
J
Jiri Denemark 已提交
664 665
        goto no_memory;

666
    feature->name = virXPathString("string(@name)", ctxt);
J
Jiri Denemark 已提交
667
    if (feature->name == NULL) {
668
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
669 670 671 672 673
                "%s", _("Missing CPU feature name"));
        goto ignore;
    }

    if (x86FeatureFind(map, feature->name)) {
674
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
675 676 677 678
                _("CPU feature %s already defined"), feature->name);
        goto ignore;
    }

679
    n = virXPathNodeSet("./cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
680 681 682 683
    if (n < 0)
        goto ignore;

    for (i = 0; i < n; i++) {
J
Jiri Denemark 已提交
684
        struct cpuX86cpuid cpuid;
J
Jiri Denemark 已提交
685 686 687 688 689
        unsigned long fun, eax, ebx, ecx, edx;
        int ret_fun, ret_eax, ret_ebx, ret_ecx, ret_edx;

        ctxt->node = nodes[i];
        fun = eax = ebx = ecx = edx = 0;
690 691 692 693 694
        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);
J
Jiri Denemark 已提交
695 696 697

        if (ret_fun < 0 || ret_eax == -2 || ret_ebx == -2
            || ret_ecx == -2 || ret_edx == -2) {
698
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
699 700 701 702
                    _("Invalid cpuid[%d] in %s feature"), i, feature->name);
            goto ignore;
        }

J
Jiri Denemark 已提交
703 704 705 706 707 708 709 710
        cpuid.function = fun;
        cpuid.eax = eax;
        cpuid.ebx = ebx;
        cpuid.ecx = ecx;
        cpuid.edx = edx;

        if (x86DataAddCpuid(feature->data, &cpuid))
            goto no_memory;
J
Jiri Denemark 已提交
711 712 713 714 715 716 717 718 719 720
    }

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

out:
721 722 723
    ctxt->node = ctxt_node;
    VIR_FREE(nodes);

J
Jiri Denemark 已提交
724 725 726
    return ret;

no_memory:
727
    virReportOOMError();
J
Jiri Denemark 已提交
728 729 730 731 732 733 734 735
    ret = -1;

ignore:
    x86FeatureFree(feature);
    goto out;
}


J
Jiri Denemark 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
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 已提交
753 754 755 756 757 758 759
static void
x86ModelFree(struct x86_model *model)
{
    if (model == NULL)
        return;

    VIR_FREE(model->name);
J
Jiri Denemark 已提交
760
    x86DataFree(model->data);
J
Jiri Denemark 已提交
761 762 763 764 765 766 767 768 769 770
    VIR_FREE(model);
}


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

    if (VIR_ALLOC(copy) < 0
J
Jiri Denemark 已提交
771 772
        || !(copy->name = strdup(model->name))
        || !(copy->data = x86DataCopy(model->data))) {
J
Jiri Denemark 已提交
773 774 775 776
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
777
    copy->vendor = model->vendor;
J
Jiri Denemark 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

    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 *
x86ModelFromCPU(const virCPUDefPtr cpu,
                const struct x86_map *map,
                int policy)
{
    struct x86_model *model = NULL;
    int i;

809
    if (policy == VIR_CPU_FEATURE_REQUIRE) {
J
Jiri Denemark 已提交
810
        if ((model = x86ModelFind(map, cpu->model)) == NULL) {
811
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
812 813 814 815 816 817
                    _("Unknown CPU model %s"), cpu->model);
            goto error;
        }

        if ((model = x86ModelCopy(model)) == NULL)
            goto no_memory;
J
Jiri Denemark 已提交
818
    } else if (!(model = x86ModelNew())) {
J
Jiri Denemark 已提交
819
        goto no_memory;
J
Jiri Denemark 已提交
820
    } else if (cpu->type == VIR_CPU_TYPE_HOST) {
821
        return model;
J
Jiri Denemark 已提交
822
    }
J
Jiri Denemark 已提交
823 824 825 826 827 828 829 830 831

    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) {
832
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
833 834 835 836
                    _("Unknown CPU feature %s"), cpu->features[i].name);
            goto error;
        }

J
Jiri Denemark 已提交
837
        if (x86DataAdd(model->data, feature->data))
J
Jiri Denemark 已提交
838 839 840 841 842 843
            goto no_memory;
    }

    return model;

no_memory:
844
    virReportOOMError();
J
Jiri Denemark 已提交
845 846 847 848 849 850 851

error:
    x86ModelFree(model);
    return NULL;
}


852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
static int
x86ModelSubtractCPU(struct x86_model *model,
                    const virCPUDefPtr cpu,
                    const struct x86_map *map)
{
    const struct x86_model *cpu_model;
    unsigned int i;

    if (!(cpu_model = x86ModelFind(map, cpu->model))) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                          _("Unknown CPU model %s"),
                          cpu->model);
        return -1;
    }

J
Jiri Denemark 已提交
867
    x86DataSubtract(model->data, cpu_model->data);
868 869 870 871 872 873 874 875 876 877 878

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

        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                              _("Unknown CPU feature %s"),
                              cpu->features[i].name);
            return -1;
        }

J
Jiri Denemark 已提交
879
        x86DataSubtract(model->data, feature->data);
880 881 882 883 884 885
    }

    return 0;
}


J
Jiri Denemark 已提交
886 887 888 889 890
static enum compare_result
x86ModelCompare(const struct x86_model *model1,
                const struct x86_model *model2)
{
    enum compare_result result = EQUAL;
J
Jiri Denemark 已提交
891 892
    struct data_iterator iter1 = DATA_ITERATOR_INIT(model1->data);
    struct data_iterator iter2 = DATA_ITERATOR_INIT(model2->data);
J
Jiri Denemark 已提交
893 894 895
    struct cpuX86cpuid *cpuid1;
    struct cpuX86cpuid *cpuid2;

J
Jiri Denemark 已提交
896
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
J
Jiri Denemark 已提交
897 898
        enum compare_result match = SUPERSET;

J
Jiri Denemark 已提交
899
        if ((cpuid2 = x86DataCpuid(model2->data, cpuid1->function))) {
J
Jiri Denemark 已提交
900 901 902 903 904 905 906 907 908 909 910 911
            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 已提交
912
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
J
Jiri Denemark 已提交
913 914
        enum compare_result match = SUBSET;

J
Jiri Denemark 已提交
915
        if ((cpuid1 = x86DataCpuid(model1->data, cpuid2->function))) {
J
Jiri Denemark 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
            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 已提交
934
             struct x86_map *map)
J
Jiri Denemark 已提交
935 936
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
937
    struct x86_model *model;
J
Jiri Denemark 已提交
938
    char *vendor = NULL;
J
Jiri Denemark 已提交
939 940 941 942
    int ret = 0;
    int i;
    int n;

J
Jiri Denemark 已提交
943
    if (!(model = x86ModelNew()))
J
Jiri Denemark 已提交
944 945
        goto no_memory;

946
    model->name = virXPathString("string(@name)", ctxt);
J
Jiri Denemark 已提交
947
    if (model->name == NULL) {
948
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
949 950 951 952
                "%s", _("Missing CPU model name"));
        goto ignore;
    }

953
    if (virXPathNode("./model", ctxt) != NULL) {
J
Jiri Denemark 已提交
954 955 956
        const struct x86_model *ancestor;
        char *name;

957
        name = virXPathString("string(./model/@name)", ctxt);
J
Jiri Denemark 已提交
958
        if (name == NULL) {
959
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
960 961 962 963 964 965
                    _("Missing ancestor's name in CPU model %s"),
                    model->name);
            goto ignore;
        }

        if ((ancestor = x86ModelFind(map, name)) == NULL) {
966
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
967 968 969 970 971 972 973 974
                    _("Ancestor model %s not found for CPU model %s"),
                    name, model->name);
            VIR_FREE(name);
            goto ignore;
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
975
        model->vendor = ancestor->vendor;
E
Eric Blake 已提交
976
        x86DataFree(model->data);
J
Jiri Denemark 已提交
977 978
        if (!(model->data = x86DataCopy(ancestor->data)))
            goto no_memory;
J
Jiri Denemark 已提交
979 980
    }

981 982 983 984 985 986 987 988 989
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                    _("Invalid vendor element in CPU model %s"),
                    model->name);
            goto ignore;
        }

J
Jiri Denemark 已提交
990 991 992 993 994 995 996 997
        if (!(model->vendor = x86VendorFind(map, vendor))) {
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                    _("Unknown vendor %s referenced by CPU model %s"),
                    vendor, model->name);
            goto ignore;
        }
    }

998
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
999 1000 1001 1002 1003 1004 1005 1006
    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) {
1007
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
1008 1009 1010 1011 1012
                    _("Missing feature name for CPU model %s"), model->name);
            goto ignore;
        }

        if ((feature = x86FeatureFind(map, name)) == NULL) {
1013
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
1014 1015 1016 1017 1018 1019 1020
                    _("Feature %s required by CPU model %s not found"),
                    name, model->name);
            VIR_FREE(name);
            goto ignore;
        }
        VIR_FREE(name);

J
Jiri Denemark 已提交
1021
        if (x86DataAdd(model->data, feature->data))
J
Jiri Denemark 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
            goto no_memory;
    }

    if (map->models == NULL)
        map->models = model;
    else {
        model->next = map->models;
        map->models = model;
    }

out:
J
Jiri Denemark 已提交
1033
    VIR_FREE(vendor);
1034
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1035 1036 1037
    return ret;

no_memory:
1038
    virReportOOMError();
J
Jiri Denemark 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    ret = -1;

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

1065 1066 1067 1068 1069 1070
    while (map->vendors != NULL) {
        struct x86_vendor *vendor = map->vendors;
        map->vendors = vendor->next;
        x86VendorFree(vendor);
    }

J
Jiri Denemark 已提交
1071 1072 1073 1074
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
static int
x86MapLoadCallback(enum cpuMapElement element,
                   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;
}


J
Jiri Denemark 已提交
1097 1098 1099 1100 1101 1102
static struct x86_map *
x86LoadMap(void)
{
    struct x86_map *map;

    if (VIR_ALLOC(map) < 0) {
1103
        virReportOOMError();
J
Jiri Denemark 已提交
1104 1105 1106
        return NULL;
    }

J
Jiri Denemark 已提交
1107
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
        goto error;

    return map;

error:
    x86MapFree(map);
    return NULL;
}


static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
           union cpuData **guest)
{
    struct x86_map *map = NULL;
    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;
J
Jiri Denemark 已提交
1132 1133
    struct data_iterator iter;
    const struct cpuX86cpuid *cpuid;
J
Jiri Denemark 已提交
1134 1135
    virCPUCompareResult ret;
    enum compare_result result;
J
Jiri Denemark 已提交
1136
    unsigned int i;
J
Jiri Denemark 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

    if (cpu->arch != NULL) {
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
            if (STREQ(archs[i], cpu->arch)) {
                found = true;
                break;
            }
        }

1148 1149
        if (!found) {
            VIR_DEBUG("CPU arch %s does not match host arch", cpu->arch);
J
Jiri Denemark 已提交
1150
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1151
        }
J
Jiri Denemark 已提交
1152 1153
    }

J
Jiri Denemark 已提交
1154 1155 1156 1157 1158 1159 1160
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

J
Jiri Denemark 已提交
1161
    if (!(map = x86LoadMap()) ||
1162
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1163 1164 1165 1166 1167
        !(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 已提交
1168 1169
        goto error;

J
Jiri Denemark 已提交
1170 1171
    x86DataIteratorInit(&iter, cpu_forbid->data);
    while ((cpuid = x86DataCpuidNext(&iter))) {
J
Jiri Denemark 已提交
1172 1173
        const struct cpuX86cpuid *cpuid2;

J
Jiri Denemark 已提交
1174 1175
        cpuid2 = x86DataCpuid(host_model->data, cpuid->function);
        if (cpuid2 != NULL && x86cpuidMatchAny(cpuid2, cpuid)) {
1176
            VIR_DEBUG("Host CPU provides forbidden features in CPUID function 0x%x",
J
Jiri Denemark 已提交
1177
                      cpuid->function);
J
Jiri Denemark 已提交
1178 1179 1180 1181 1182
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;
            goto out;
        }
    }

J
Jiri Denemark 已提交
1183
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1184 1185
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1186
        VIR_DEBUG("Host CPU does not provide all required features");
J
Jiri Denemark 已提交
1187 1188 1189 1190 1191 1192
        ret = VIR_CPU_COMPARE_INCOMPATIBLE;
        goto out;
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

J
Jiri Denemark 已提交
1193 1194 1195
    if ((diff = x86ModelCopy(host_model)) == NULL)
        goto no_memory;

J
Jiri Denemark 已提交
1196 1197 1198 1199
    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 已提交
1200

J
Jiri Denemark 已提交
1201 1202
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1203 1204 1205 1206

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1207
        VIR_DEBUG("Host CPU does not strictly match guest CPU");
J
Jiri Denemark 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
        ret = VIR_CPU_COMPARE_INCOMPATIBLE;
        goto out;
    }

    if (guest != NULL) {
        if ((guest_model = x86ModelCopy(host_model)) == NULL)
            goto no_memory;

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

J
Jiri Denemark 已提交
1220
        if (x86DataAdd(guest_model->data, cpu_force->data))
J
Jiri Denemark 已提交
1221 1222
            goto no_memory;

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

J
Jiri Denemark 已提交
1225
        if ((*guest = x86DataCopy(guest_model->data)) == NULL)
J
Jiri Denemark 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
            goto no_memory;
    }

out:
    x86MapFree(map);
    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;

no_memory:
1243
    virReportOOMError();
J
Jiri Denemark 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

error:
    ret = VIR_CPU_COMPARE_ERROR;
    goto out;
}


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
           virCPUDefPtr cpu)
{
    return x86Compute(host, cpu, NULL);
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
             union cpuData **data)
{
    return x86Compute(host, guest, data);
}


static int
x86Decode(virCPUDefPtr cpu,
          const union cpuData *data,
1271
          const char **models,
1272 1273
          unsigned int nmodels,
          const char *preferred)
J
Jiri Denemark 已提交
1274 1275 1276 1277
{
    int ret = -1;
    struct x86_map *map;
    const struct x86_model *candidate;
1278 1279
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1280
    unsigned int i;
J
Jiri Denemark 已提交
1281 1282 1283 1284 1285 1286

    if (data == NULL || (map = x86LoadMap()) == NULL)
        return -1;

    candidate = map->models;
    while (candidate != NULL) {
1287 1288 1289
        bool allowed = (models == NULL);

        for (i = 0; i < nmodels; i++) {
1290
            if (models && models[i] && STREQ(models[i], candidate->name)) {
1291 1292
                allowed = true;
                break;
J
Jiri Denemark 已提交
1293
            }
1294
        }
J
Jiri Denemark 已提交
1295

1296
        if (!allowed) {
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
                    virCPUReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("CPU model %s is not supported by hypervisor"),
                            preferred);
                    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);
            }
1312
            goto next;
J
Jiri Denemark 已提交
1313 1314
        }

1315 1316 1317
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto out;

J
Jiri Denemark 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326
        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;
        }

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
        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;
                }
            }
        }

1340 1341 1342 1343 1344 1345
        if (preferred && STREQ(cpuCandidate->model, preferred)) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
            break;
        }

1346 1347 1348 1349 1350 1351 1352
        if (cpuModel == NULL
            || cpuModel->nfeatures > cpuCandidate->nfeatures) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
        } else
            virCPUDefFree(cpuCandidate);

J
Jiri Denemark 已提交
1353 1354 1355 1356
    next:
        candidate = candidate->next;
    }

1357
    if (cpuModel == NULL) {
1358
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
J
Jiri Denemark 已提交
1359 1360 1361 1362
                "%s", _("Cannot find suitable CPU model for given data"));
        goto out;
    }

1363
    cpu->model = cpuModel->model;
J
Jiri Denemark 已提交
1364
    cpu->vendor = cpuModel->vendor;
1365 1366 1367
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1368 1369 1370 1371 1372

    ret = 0;

out:
    x86MapFree(map);
1373
    virCPUDefFree(cpuModel);
J
Jiri Denemark 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389

    return ret;
}


static union cpuData *
x86EncodePolicy(const virCPUDefPtr cpu,
                const struct x86_map *map,
                enum virCPUFeaturePolicy policy)
{
    struct x86_model *model;
    union cpuData *data = NULL;

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

J
Jiri Denemark 已提交
1390 1391
    data = model->data;
    model->data = NULL;
J
Jiri Denemark 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
    x86ModelFree(model);

    return data;
}


static int
x86Encode(const virCPUDefPtr cpu,
          union cpuData **forced,
          union cpuData **required,
          union cpuData **optional,
          union cpuData **disabled,
J
Jiri Denemark 已提交
1404 1405
          union cpuData **forbidden,
          union cpuData **vendor)
J
Jiri Denemark 已提交
1406 1407 1408 1409 1410 1411 1412
{
    struct x86_map *map = NULL;
    union cpuData *data_forced = NULL;
    union cpuData *data_required = NULL;
    union cpuData *data_optional = NULL;
    union cpuData *data_disabled = NULL;
    union cpuData *data_forbidden = NULL;
J
Jiri Denemark 已提交
1413
    union cpuData *data_vendor = NULL;
J
Jiri Denemark 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    int ret = -1;

    if ((map = x86LoadMap()) == NULL)
        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 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    if (vendor) {
        const struct x86_vendor *v = NULL;

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
            virCPUReportError(VIR_ERR_OPERATION_FAILED,
                    _("CPU vendor %s not found"), cpu->vendor);
            goto error;
        }

        if (v &&
            (VIR_ALLOC(data_vendor) < 0 ||
             x86DataAddCpuid(data_vendor, &v->cpuid) < 0)) {
            virReportOOMError();
            goto error;
        }
    }

J
Jiri Denemark 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    if (forced)
        *forced = data_forced;
    if (required)
        *required = data_required;
    if (optional)
        *optional = data_optional;
    if (disabled)
        *disabled = data_disabled;
    if (forbidden)
        *forbidden = data_forbidden;
J
Jiri Denemark 已提交
1476 1477
    if (vendor)
        *vendor = data_vendor;
J
Jiri Denemark 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

    ret = 0;

cleanup:
    x86MapFree(map);

    return ret;

error:
    x86DataFree(data_forced);
    x86DataFree(data_required);
    x86DataFree(data_optional);
    x86DataFree(data_disabled);
    x86DataFree(data_forbidden);
J
Jiri Denemark 已提交
1492
    x86DataFree(data_vendor);
J
Jiri Denemark 已提交
1493 1494 1495 1496 1497 1498 1499 1500
    goto cleanup;
}


#if HAVE_CPUID
static inline void
cpuidCall(struct cpuX86cpuid *cpuid)
{
1501
# if __x86_64__
J
Jiri Denemark 已提交
1502 1503 1504 1505 1506 1507
    asm("cpuid"
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function));
1508
# else
J
Jiri Denemark 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    /* 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;"
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function)
        : "cc");
1522
# endif
J
Jiri Denemark 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}


static int
cpuidSet(uint32_t base, struct cpuX86cpuid **set)
{
    uint32_t max;
    uint32_t i;
    struct cpuX86cpuid cpuid = { base, 0, 0, 0, 0 };

    cpuidCall(&cpuid);
    max = cpuid.eax - base;

    if (VIR_ALLOC_N(*set, max + 1) < 0) {
1537
        virReportOOMError();
J
Jiri Denemark 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
        return -1;
    }

    for (i = 0; i <= max; i++) {
        cpuid.function = base | i;
        cpuidCall(&cpuid);
        (*set)[i] = cpuid;
    }

    return max + 1;
}


static union cpuData *
x86NodeData(void)
{
    union cpuData *data;
J
Jiri Denemark 已提交
1555
    int ret;
J
Jiri Denemark 已提交
1556 1557

    if (VIR_ALLOC(data) < 0) {
1558
        virReportOOMError();
J
Jiri Denemark 已提交
1559 1560 1561
        return NULL;
    }

J
Jiri Denemark 已提交
1562
    if ((ret = cpuidSet(CPUX86_BASIC, &data->x86.basic)) < 0)
J
Jiri Denemark 已提交
1563
        goto error;
J
Jiri Denemark 已提交
1564
    data->x86.basic_len = ret;
J
Jiri Denemark 已提交
1565

J
Jiri Denemark 已提交
1566
    if ((ret = cpuidSet(CPUX86_EXTENDED, &data->x86.extended)) < 0)
J
Jiri Denemark 已提交
1567
        goto error;
J
Jiri Denemark 已提交
1568
    data->x86.extended_len = ret;
J
Jiri Denemark 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579

    return data;

error:
    x86DataFree(data);

    return NULL;
}
#endif


1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
static virCPUDefPtr
x86Baseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
            unsigned int nmodels)
{
    struct x86_map *map = NULL;
    struct x86_model *base_model = NULL;
    virCPUDefPtr cpu = NULL;
    unsigned int i;
J
Jiri Denemark 已提交
1590 1591
    const struct x86_vendor *vendor = NULL;
    struct x86_model *model = NULL;
1592
    bool outputVendor = true;
1593 1594 1595 1596

    if (!(map = x86LoadMap()))
        goto error;

1597
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1598 1599 1600 1601 1602
        goto error;

    if (VIR_ALLOC(cpu) < 0 ||
        !(cpu->arch = strdup(cpus[0]->arch)))
        goto no_memory;
1603 1604
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1605

1606 1607 1608
    if (!cpus[0]->vendor)
        outputVendor = false;
    else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
J
Jiri Denemark 已提交
1609 1610 1611 1612 1613
        virCPUReportError(VIR_ERR_OPERATION_FAILED,
                _("Unknown CPU vendor %s"), cpus[0]->vendor);
        goto error;
    }

1614
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
1615 1616
        const char *vn = NULL;

1617
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
1618 1619
            goto error;

J
Jiri Denemark 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
            virCPUReportError(VIR_ERR_OPERATION_FAILED,
                    _("CPU vendor %s of model %s differs from vendor %s"),
                    model->vendor->name, model->name, cpus[i]->vendor);
            goto error;
        }

        if (cpus[i]->vendor)
            vn = cpus[i]->vendor;
1630 1631 1632 1633 1634
        else {
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
                    virCPUReportError(VIR_ERR_OPERATION_FAILED,
                            _("Unknown CPU vendor %s"), vn);
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
                virCPUReportError(VIR_ERR_OPERATION_FAILED,
                        "%s", _("CPU vendors do not match"));
                goto error;
            }
        }

J
Jiri Denemark 已提交
1650
        x86DataIntersect(base_model->data, model->data);
1651
        x86ModelFree(model);
J
Jiri Denemark 已提交
1652
        model = NULL;
1653 1654
    }

J
Jiri Denemark 已提交
1655
    if (x86DataIsEmpty(base_model->data)) {
1656 1657 1658 1659 1660
        virCPUReportError(VIR_ERR_OPERATION_FAILED,
                "%s", _("CPUs are incompatible"));
        goto error;
    }

J
Jiri Denemark 已提交
1661
    if (vendor && x86DataAddCpuid(base_model->data, &vendor->cpuid) < 0)
J
Jiri Denemark 已提交
1662 1663
        goto no_memory;

J
Jiri Denemark 已提交
1664
    if (x86Decode(cpu, base_model->data, models, nmodels, NULL) < 0)
1665 1666
        goto error;

1667 1668 1669
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

1670 1671
    VIR_FREE(cpu->arch);

1672 1673 1674 1675 1676 1677 1678 1679 1680
cleanup:
    x86ModelFree(base_model);
    x86MapFree(map);

    return cpu;

no_memory:
    virReportOOMError();
error:
J
Jiri Denemark 已提交
1681
    x86ModelFree(model);
1682 1683 1684 1685 1686 1687
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
static int
x86Update(virCPUDefPtr guest,
          const virCPUDefPtr host)
{
    int ret = -1;
    unsigned int i;
    struct x86_map *map;
    struct x86_model *host_model = NULL;

    if (!(map = x86LoadMap()) ||
1698
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
        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))) {
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                                  _("Unknown CPU feature %s"),
                                  guest->features[i].name);
                goto cleanup;
            }

J
Jiri Denemark 已提交
1711
            if (x86DataIsSubset(host_model->data, feature->data))
1712 1713 1714 1715 1716 1717 1718 1719 1720
                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;
        if (x86ModelSubtractCPU(host_model, guest, map)
J
Jiri Denemark 已提交
1721 1722
            || x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
                                    host_model->data, map))
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
            goto cleanup;
    }

    ret = 0;

cleanup:
    x86MapFree(map);
    x86ModelFree(host_model);
    return ret;
}

D
Daniel P. Berrange 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
static int x86HasFeature(const union cpuData *data,
                         const char *name)
{
    struct x86_map *map;
    struct x86_feature *feature;
    int ret = -1;

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

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

J
Jiri Denemark 已提交
1747
    ret = x86DataIsSubset(data, feature->data) ? 1 : 0;
D
Daniel P. Berrange 已提交
1748 1749 1750 1751 1752

cleanup:
    x86MapFree(map);
    return ret;
}
1753

J
Jiri Denemark 已提交
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
    .compare    = x86Compare,
    .decode     = x86Decode,
    .encode     = x86Encode,
    .free       = x86DataFree,
#if HAVE_CPUID
    .nodeData   = x86NodeData,
#else
    .nodeData   = NULL,
#endif
1767
    .guestData  = x86GuestData,
1768
    .baseline   = x86Baseline,
1769
    .update     = x86Update,
D
Daniel P. Berrange 已提交
1770
    .hasFeature = x86HasFeature,
J
Jiri Denemark 已提交
1771
};