cpu_x86.c 51.1 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, 2013 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

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

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

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

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

    struct x86_vendor *next;
};

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

    struct x86_feature *next;
};

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

    struct x86_model *next;
};

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


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


82
struct virCPUx86DataIterator {
83
    virCPUx86Data *data;
J
Jiri Denemark 已提交
84 85 86
    int pos;
    bool extended;
};
J
Jiri Denemark 已提交
87 88


89
#define virCPUx86DataIteratorInit(data) \
J
Jiri Denemark 已提交
90 91 92
    { data, -1, false }


93
static bool
94 95
x86cpuidMatch(const virCPUx86CPUID *cpuid1,
              const virCPUx86CPUID *cpuid2)
J
Jiri Denemark 已提交
96 97 98 99 100 101 102 103
{
    return (cpuid1->eax == cpuid2->eax &&
            cpuid1->ebx == cpuid2->ebx &&
            cpuid1->ecx == cpuid2->ecx &&
            cpuid1->edx == cpuid2->edx);
}


104
static bool
105 106
x86cpuidMatchMasked(const virCPUx86CPUID *cpuid,
                    const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
107 108 109 110 111 112 113 114
{
    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 已提交
115
static void
116 117
x86cpuidSetBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
118 119 120 121 122 123 124 125
{
    cpuid->eax |= mask->eax;
    cpuid->ebx |= mask->ebx;
    cpuid->ecx |= mask->ecx;
    cpuid->edx |= mask->edx;
}


J
Jiri Denemark 已提交
126
static void
127 128
x86cpuidClearBits(virCPUx86CPUID *cpuid,
                  const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
129 130 131 132 133 134 135 136
{
    cpuid->eax &= ~mask->eax;
    cpuid->ebx &= ~mask->ebx;
    cpuid->ecx &= ~mask->ecx;
    cpuid->edx &= ~mask->edx;
}


J
Jiri Denemark 已提交
137
static void
138 139
x86cpuidAndBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
140 141 142 143 144 145 146 147
{
    cpuid->eax &= mask->eax;
    cpuid->ebx &= mask->ebx;
    cpuid->ecx &= mask->ecx;
    cpuid->edx &= mask->edx;
}


J
Jiri Denemark 已提交
148
/* skips all zero CPUID leafs */
149
static virCPUx86CPUID *
150
x86DataCpuidNext(struct virCPUx86DataIterator *iterator)
J
Jiri Denemark 已提交
151
{
152
    virCPUx86CPUID *ret;
153
    virCPUx86Data *data = iterator->data;
J
Jiri Denemark 已提交
154

155
    if (!data)
J
Jiri Denemark 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        return NULL;

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


180
static virCPUx86CPUID *
181
x86DataCpuid(const virCPUx86Data *data,
J
Jiri Denemark 已提交
182 183
             uint32_t function)
{
184
    virCPUx86CPUID *cpuids;
J
Jiri Denemark 已提交
185
    int len;
186
    size_t i;
J
Jiri Denemark 已提交
187 188

    if (function < CPUX86_EXTENDED) {
189 190
        cpuids = data->basic;
        len = data->basic_len;
J
Jiri Denemark 已提交
191 192 193
        i = function;
    }
    else {
194 195
        cpuids = data->extended;
        len = data->extended_len;
J
Jiri Denemark 已提交
196 197 198
        i = function - CPUX86_EXTENDED;
    }

J
Jiri Denemark 已提交
199
    if (i < len && !x86cpuidMatch(cpuids + i, &cpuidNull))
J
Jiri Denemark 已提交
200 201 202 203 204 205 206
        return cpuids + i;
    else
        return NULL;
}


static void
207
virCPUx86DataFree(virCPUx86Data *data)
J
Jiri Denemark 已提交
208 209 210 211
{
    if (data == NULL)
        return;

212 213
    VIR_FREE(data->basic);
    VIR_FREE(data->extended);
J
Jiri Denemark 已提交
214 215 216 217
    VIR_FREE(data);
}


218
static virCPUDataPtr
219
virCPUx86MakeData(virArch arch, virCPUx86Data **data)
220
{
221
    virCPUDataPtr cpuData;
222 223 224 225

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

J
Jiri Denemark 已提交
226 227
    cpuData->arch = arch;
    cpuData->data.x86 = *data;
228 229 230 231 232 233
    *data = NULL;

    return cpuData;
}

static void
234
x86FreeCPUData(virCPUDataPtr data)
235 236 237 238
{
    if (!data)
        return;

239
    virCPUx86DataFree(data->data.x86);
240 241 242 243
    VIR_FREE(data);
}


244 245
static virCPUx86Data *
x86DataCopy(const virCPUx86Data *data)
J
Jiri Denemark 已提交
246
{
247
    virCPUx86Data *copy = NULL;
248
    size_t i;
J
Jiri Denemark 已提交
249 250

    if (VIR_ALLOC(copy) < 0
251 252
        || VIR_ALLOC_N(copy->basic, data->basic_len) < 0
        || VIR_ALLOC_N(copy->extended, data->extended_len) < 0) {
253
        virCPUx86DataFree(copy);
J
Jiri Denemark 已提交
254 255 256
        return NULL;
    }

257 258 259
    copy->basic_len = data->basic_len;
    for (i = 0; i < data->basic_len; i++)
        copy->basic[i] = data->basic[i];
J
Jiri Denemark 已提交
260

261 262 263
    copy->extended_len = data->extended_len;
    for (i = 0; i < data->extended_len; i++)
        copy->extended[i] = data->extended[i];
J
Jiri Denemark 已提交
264 265 266 267 268

    return copy;
}


J
Jiri Denemark 已提交
269
static int
270
x86DataExpand(virCPUx86Data *data,
J
Jiri Denemark 已提交
271 272 273 274 275 276
              int basic_by,
              int extended_by)
{
    size_t i;

    if (basic_by > 0) {
277 278
        size_t len = data->basic_len;
        if (VIR_EXPAND_N(data->basic, data->basic_len, basic_by) < 0)
279
            return -1;
J
Jiri Denemark 已提交
280 281

        for (i = 0; i < basic_by; i++)
282
            data->basic[len + i].function = len + i;
J
Jiri Denemark 已提交
283 284 285
    }

    if (extended_by > 0) {
286 287
        size_t len = data->extended_len;
        if (VIR_EXPAND_N(data->extended, data->extended_len, extended_by) < 0)
288
            return -1;
J
Jiri Denemark 已提交
289 290

        for (i = 0; i < extended_by; i++)
291
            data->extended[len + i].function = len + i + CPUX86_EXTENDED;
J
Jiri Denemark 已提交
292 293 294 295 296 297
    }

    return 0;
}


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

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

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


static int
327 328
x86DataAdd(virCPUx86Data *data1,
           const virCPUx86Data *data2)
J
Jiri Denemark 已提交
329
{
330
    size_t i;
J
Jiri Denemark 已提交
331 332

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

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

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

    return 0;
}


351
static void
352 353
x86DataSubtract(virCPUx86Data *data1,
                const virCPUx86Data *data2)
354
{
355
    size_t i;
356 357
    unsigned int len;

358
    len = MIN(data1->basic_len, data2->basic_len);
359
    for (i = 0; i < len; i++) {
360 361
        x86cpuidClearBits(data1->basic + i,
                          data2->basic + i);
362 363
    }

364
    len = MIN(data1->extended_len, data2->extended_len);
365
    for (i = 0; i < len; i++) {
366 367
        x86cpuidClearBits(data1->extended + i,
                          data2->extended + i);
368 369 370 371
    }
}


J
Jiri Denemark 已提交
372
static void
373 374
x86DataIntersect(virCPUx86Data *data1,
                 const virCPUx86Data *data2)
375
{
376
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
377 378
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *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
static bool
391
x86DataIsEmpty(virCPUx86Data *data)
J
Jiri Denemark 已提交
392
{
393
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data);
J
Jiri Denemark 已提交
394

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


J
Jiri Denemark 已提交
399
static bool
400 401
x86DataIsSubset(const virCPUx86Data *data,
                const virCPUx86Data *subset)
J
Jiri Denemark 已提交
402 403
{

404
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit((virCPUx86Data *)subset);
405 406
    const virCPUx86CPUID *cpuid;
    const virCPUx86CPUID *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
/* also removes all detected features from data */
static int
x86DataToCPUFeatures(virCPUDefPtr cpu,
                     int policy,
422
                     virCPUx86Data *data,
423 424 425 426 427
                     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
/* also removes bits corresponding to vendor string from data */
static const struct x86_vendor *
442
x86DataToVendor(virCPUx86Data *data,
J
Jiri Denemark 已提交
443 444 445
                const struct x86_map *map)
{
    const struct x86_vendor *vendor = map->vendors;
446
    virCPUx86CPUID *cpuid;
J
Jiri Denemark 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460

    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
static virCPUDefPtr
462
x86DataToCPU(const virCPUx86Data *data,
463 464 465 466
             const struct x86_model *model,
             const struct x86_map *map)
{
    virCPUDefPtr cpu;
467 468
    virCPUx86Data *copy = NULL;
    virCPUx86Data *modelData = NULL;
J
Jiri Denemark 已提交
469
    const struct x86_vendor *vendor;
470 471

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

J
Jiri Denemark 已提交
477
    if ((vendor = x86DataToVendor(copy, map)) &&
478
        VIR_STRDUP(cpu->vendor, vendor->name) < 0)
479
        goto error;
J
Jiri Denemark 已提交
480

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
    virCPUx86DataFree(modelData);
    virCPUx86DataFree(copy);
494 495 496 497 498 499 500 501 502
    return cpu;

error:
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


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

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
J
Jiri Denemark 已提交
511
}
J
Jiri Denemark 已提交
512 513 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


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)
541
        goto error;
J
Jiri Denemark 已提交
542 543 544

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
545 546
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing CPU vendor name"));
J
Jiri Denemark 已提交
547 548 549 550
        goto ignore;
    }

    if (x86VendorFind(map, vendor->name)) {
551 552
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU vendor %s already defined"), vendor->name);
J
Jiri Denemark 已提交
553 554 555 556 557
        goto ignore;
    }

    string = virXPathString("string(@string)", ctxt);
    if (!string) {
558
        virReportError(VIR_ERR_INTERNAL_ERROR,
559 560
                       _("Missing vendor string for CPU vendor %s"),
                       vendor->name);
J
Jiri Denemark 已提交
561 562 563
        goto ignore;
    }
    if (strlen(string) != VENDOR_STRING_LENGTH) {
564 565
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid CPU vendor string '%s'"), string);
J
Jiri Denemark 已提交
566 567 568 569
        goto ignore;
    }

    vendor->cpuid.function = 0;
E
Eric Blake 已提交
570 571 572 573 574
    vendor->cpuid.ebx = virReadBufInt32LE(string);
    vendor->cpuid.edx = virReadBufInt32LE(string + 4);
    vendor->cpuid.ecx = virReadBufInt32LE(string + 8);

    if (!map->vendors) {
J
Jiri Denemark 已提交
575
        map->vendors = vendor;
E
Eric Blake 已提交
576
    } else {
J
Jiri Denemark 已提交
577 578 579 580 581 582 583 584 585
        vendor->next = map->vendors;
        map->vendors = vendor;
    }

out:
    VIR_FREE(string);

    return ret;

586
error:
J
Jiri Denemark 已提交
587 588 589 590 591 592 593
    ret = -1;
ignore:
    x86VendorFree(vendor);
    goto out;
}


J
Jiri Denemark 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
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 已提交
611 612 613 614 615 616 617
static void
x86FeatureFree(struct x86_feature *feature)
{
    if (feature == NULL)
        return;

    VIR_FREE(feature->name);
618
    virCPUx86DataFree(feature->data);
J
Jiri Denemark 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    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;
}


641 642 643
static char *
x86FeatureNames(const struct x86_map *map,
                const char *separator,
644
                virCPUx86Data *data)
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
{
    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);
}


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

J
Jiri Denemark 已提交
710
    if (!(feature = x86FeatureNew()))
711
        goto error;
J
Jiri Denemark 已提交
712

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

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

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

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

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

out:
750 751 752
    ctxt->node = ctxt_node;
    VIR_FREE(nodes);

J
Jiri Denemark 已提交
753 754
    return ret;

755
error:
J
Jiri Denemark 已提交
756 757 758 759 760 761 762 763
    ret = -1;

ignore:
    x86FeatureFree(feature);
    goto out;
}


J
Jiri Denemark 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
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 已提交
781 782 783 784 785 786 787
static void
x86ModelFree(struct x86_model *model)
{
    if (model == NULL)
        return;

    VIR_FREE(model->name);
788
    virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
789 790 791 792 793 794 795 796 797
    VIR_FREE(model);
}


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

798 799 800
    if (VIR_ALLOC(copy) < 0 ||
        VIR_STRDUP(copy->name, model->name) < 0 ||
        !(copy->data = x86DataCopy(model->data))) {
J
Jiri Denemark 已提交
801 802 803 804
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
805
    copy->vendor = model->vendor;
J
Jiri Denemark 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

    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 *
830
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
831 832 833 834
                const struct x86_map *map,
                int policy)
{
    struct x86_model *model = NULL;
835
    size_t i;
J
Jiri Denemark 已提交
836

837
    if (policy == VIR_CPU_FEATURE_REQUIRE) {
J
Jiri Denemark 已提交
838
        if ((model = x86ModelFind(map, cpu->model)) == NULL) {
839 840
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU model %s"), cpu->model);
J
Jiri Denemark 已提交
841 842 843 844
            goto error;
        }

        if ((model = x86ModelCopy(model)) == NULL)
845
            goto error;
J
Jiri Denemark 已提交
846
    } else if (!(model = x86ModelNew())) {
847
        goto error;
J
Jiri Denemark 已提交
848
    } else if (cpu->type == VIR_CPU_TYPE_HOST) {
849
        return model;
J
Jiri Denemark 已提交
850
    }
J
Jiri Denemark 已提交
851 852 853 854 855 856 857 858 859

    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) {
860 861
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
J
Jiri Denemark 已提交
862 863 864
            goto error;
        }

J
Jiri Denemark 已提交
865
        if (x86DataAdd(model->data, feature->data))
866
            goto error;
J
Jiri Denemark 已提交
867 868 869 870 871 872 873 874 875 876
    }

    return model;

error:
    x86ModelFree(model);
    return NULL;
}


877 878
static int
x86ModelSubtractCPU(struct x86_model *model,
879
                    const virCPUDef *cpu,
880 881 882
                    const struct x86_map *map)
{
    const struct x86_model *cpu_model;
883
    size_t i;
884 885

    if (!(cpu_model = x86ModelFind(map, cpu->model))) {
886 887 888
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU model %s"),
                       cpu->model);
889 890 891
        return -1;
    }

J
Jiri Denemark 已提交
892
    x86DataSubtract(model->data, cpu_model->data);
893 894 895 896 897

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

        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
898 899 900
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"),
                           cpu->features[i].name);
901 902 903
            return -1;
        }

J
Jiri Denemark 已提交
904
        x86DataSubtract(model->data, feature->data);
905 906 907 908 909 910
    }

    return 0;
}


J
Jiri Denemark 已提交
911 912 913 914 915
static enum compare_result
x86ModelCompare(const struct x86_model *model1,
                const struct x86_model *model2)
{
    enum compare_result result = EQUAL;
916 917
    struct virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(model1->data);
    struct virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(model2->data);
918 919
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
920

J
Jiri Denemark 已提交
921
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
J
Jiri Denemark 已提交
922 923
        enum compare_result match = SUPERSET;

J
Jiri Denemark 已提交
924
        if ((cpuid2 = x86DataCpuid(model2->data, cpuid1->function))) {
J
Jiri Denemark 已提交
925 926 927 928 929 930 931 932 933 934 935 936
            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 已提交
937
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
J
Jiri Denemark 已提交
938 939
        enum compare_result match = SUBSET;

J
Jiri Denemark 已提交
940
        if ((cpuid1 = x86DataCpuid(model1->data, cpuid2->function))) {
J
Jiri Denemark 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
            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 已提交
959
             struct x86_map *map)
J
Jiri Denemark 已提交
960 961
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
962
    struct x86_model *model;
J
Jiri Denemark 已提交
963
    char *vendor = NULL;
J
Jiri Denemark 已提交
964
    int ret = 0;
965
    size_t i;
J
Jiri Denemark 已提交
966 967
    int n;

J
Jiri Denemark 已提交
968
    if (!(model = x86ModelNew()))
969
        goto error;
J
Jiri Denemark 已提交
970

971
    model->name = virXPathString("string(@name)", ctxt);
J
Jiri Denemark 已提交
972
    if (model->name == NULL) {
973 974
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
J
Jiri Denemark 已提交
975 976 977
        goto ignore;
    }

978
    if (virXPathNode("./model", ctxt) != NULL) {
J
Jiri Denemark 已提交
979 980 981
        const struct x86_model *ancestor;
        char *name;

982
        name = virXPathString("string(./model/@name)", ctxt);
J
Jiri Denemark 已提交
983
        if (name == NULL) {
984 985 986
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
987 988 989 990
            goto ignore;
        }

        if ((ancestor = x86ModelFind(map, name)) == NULL) {
991 992 993
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
994 995 996 997 998 999
            VIR_FREE(name);
            goto ignore;
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1000
        model->vendor = ancestor->vendor;
1001
        virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
1002
        if (!(model->data = x86DataCopy(ancestor->data)))
1003
            goto error;
J
Jiri Denemark 已提交
1004 1005
    }

1006 1007 1008
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
1009 1010 1011
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
1012 1013 1014
            goto ignore;
        }

J
Jiri Denemark 已提交
1015
        if (!(model->vendor = x86VendorFind(map, vendor))) {
1016 1017 1018
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
J
Jiri Denemark 已提交
1019 1020 1021 1022
            goto ignore;
        }
    }

1023
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1024 1025 1026 1027 1028 1029 1030 1031
    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) {
1032 1033
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
J
Jiri Denemark 已提交
1034 1035 1036 1037
            goto ignore;
        }

        if ((feature = x86FeatureFind(map, name)) == NULL) {
1038 1039 1040
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1041 1042 1043 1044 1045
            VIR_FREE(name);
            goto ignore;
        }
        VIR_FREE(name);

J
Jiri Denemark 已提交
1046
        if (x86DataAdd(model->data, feature->data))
1047
            goto error;
J
Jiri Denemark 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    }

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

out:
J
Jiri Denemark 已提交
1058
    VIR_FREE(vendor);
1059
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1060 1061
    return ret;

1062
error:
J
Jiri Denemark 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    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);
    }

1089 1090 1091 1092 1093 1094
    while (map->vendors != NULL) {
        struct x86_vendor *vendor = map->vendors;
        map->vendors = vendor->next;
        x86VendorFree(vendor);
    }

J
Jiri Denemark 已提交
1095 1096 1097 1098
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
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 已提交
1121 1122 1123 1124 1125
static struct x86_map *
x86LoadMap(void)
{
    struct x86_map *map;

1126
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1127 1128
        return NULL;

J
Jiri Denemark 已提交
1129
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
        goto error;

    return map;

error:
    x86MapFree(map);
    return NULL;
}


1140 1141 1142
static char *
x86CPUDataFormat(const virCPUData *data)
{
1143
    struct virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data->data.x86);
1144
    virCPUx86CPUID *cpuid;
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    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");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return NULL;
    }

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
x86CPUDataParse(const char *xmlStr)
{
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1175
    virCPUx86Data *data = NULL;
1176
    virCPUx86CPUID cpuid;
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
    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;
        }
1204
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
1205 1206 1207
            goto cleanup;
    }

1208
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1209 1210 1211 1212 1213

cleanup:
    VIR_FREE(nodes);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1214
    virCPUx86DataFree(data);
1215 1216 1217 1218
    return cpuData;
}


1219 1220 1221
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1222
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1223 1224 1225 1226 1227 1228 1229
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
#define virX86CpuIncompatible(MSG, CPU_DEF)                             \
        do {                                                            \
            char *flagsStr = NULL;                                      \
1230 1231 1232 1233
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) {  \
                virReportOOMError();                                    \
                goto error;                                             \
            }                                                           \
1234 1235 1236
            if (message &&                                              \
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
                VIR_FREE(flagsStr);                                     \
1237
                goto error;                                             \
1238 1239 1240 1241 1242 1243
            }                                                           \
            VIR_DEBUG("%s: %s", MSG, flagsStr);                         \
            VIR_FREE(flagsStr);                                         \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;                         \
        } while (0)

1244

J
Jiri Denemark 已提交
1245 1246 1247
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1248
           virCPUDataPtr *guest,
1249
           char **message)
J
Jiri Denemark 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
{
    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;
    virCPUCompareResult ret;
    enum compare_result result;
J
Jiri Denemark 已提交
1262
    virArch arch;
1263
    size_t i;
J
Jiri Denemark 已提交
1264

1265
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1266 1267 1268
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1269
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1270 1271 1272 1273 1274
                found = true;
                break;
            }
        }

1275
        if (!found) {
1276 1277
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1278 1279 1280
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1281
                            virArchToString(cpu->arch)) < 0)
1282
                goto error;
J
Jiri Denemark 已提交
1283
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1284
        }
J
Jiri Denemark 已提交
1285 1286 1287
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1288 1289
    }

J
Jiri Denemark 已提交
1290 1291 1292 1293
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1294 1295 1296 1297 1298
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1299
            goto error;
1300

J
Jiri Denemark 已提交
1301 1302 1303
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

J
Jiri Denemark 已提交
1304
    if (!(map = x86LoadMap()) ||
1305
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1306 1307 1308 1309 1310
        !(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 已提交
1311 1312
        goto error;

1313 1314 1315 1316
    x86DataIntersect(cpu_forbid->data, host_model->data);
    if (!x86DataIsEmpty(cpu_forbid->data)) {
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
                              cpu_forbid->data);
1317
        goto cleanup;
J
Jiri Denemark 已提交
1318 1319
    }

1320 1321 1322 1323 1324
    /* 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 已提交
1325
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1326 1327
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1328 1329 1330 1331
        x86DataSubtract(cpu_require->data, host_model->data);
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
                              cpu_require->data);
1332
        goto cleanup;
J
Jiri Denemark 已提交
1333 1334 1335 1336
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

J
Jiri Denemark 已提交
1337
    if ((diff = x86ModelCopy(host_model)) == NULL)
1338
        goto error;
J
Jiri Denemark 已提交
1339

J
Jiri Denemark 已提交
1340 1341 1342 1343
    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 已提交
1344

J
Jiri Denemark 已提交
1345 1346
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1347 1348 1349 1350

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1351 1352 1353
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
                              diff->data);
1354
        goto cleanup;
J
Jiri Denemark 已提交
1355 1356 1357
    }

    if (guest != NULL) {
1358
        virCPUx86Data *guestData;
1359

J
Jiri Denemark 已提交
1360
        if ((guest_model = x86ModelCopy(host_model)) == NULL)
1361
            goto error;
J
Jiri Denemark 已提交
1362 1363 1364

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

J
Jiri Denemark 已提交
1367
        if (x86DataAdd(guest_model->data, cpu_force->data))
1368
            goto error;
J
Jiri Denemark 已提交
1369

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

1372
        if (!(guestData = x86DataCopy(guest_model->data)) ||
1373
            !(*guest = virCPUx86MakeData(arch, &guestData))) {
1374
            virCPUx86DataFree(guestData);
1375
            goto error;
1376
        }
J
Jiri Denemark 已提交
1377 1378
    }

1379
cleanup:
J
Jiri Denemark 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    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;

error:
    ret = VIR_CPU_COMPARE_ERROR;
1394
    goto cleanup;
J
Jiri Denemark 已提交
1395
}
1396
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1397 1398 1399 1400 1401 1402


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
           virCPUDefPtr cpu)
{
1403
    return x86Compute(host, cpu, NULL, NULL);
J
Jiri Denemark 已提交
1404 1405 1406 1407 1408 1409
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1410
             virCPUDataPtr *data,
1411
             char **message)
J
Jiri Denemark 已提交
1412
{
1413
    return x86Compute(host, guest, data, message);
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
static int
x86AddFeatures(virCPUDefPtr cpu,
               struct x86_map *map)
{
    const struct x86_model *candidate;
    const struct x86_feature *feature = map->features;

    candidate = map->models;
    while (candidate != NULL) {
        if (STREQ(cpu->model, candidate->name))
            break;
        candidate = candidate->next;
    }
    if (!candidate) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("%s not a known CPU model"), cpu->model);
        return -1;
    }
    while (feature != NULL) {
        if (x86DataIsSubset(candidate->data, feature->data) &&
            virCPUDefAddFeature(cpu, feature->name,
                                VIR_CPU_FEATURE_REQUIRE) < 0)
            return -1;
        feature = feature->next;
    }
    return 0;
}

J
Jiri Denemark 已提交
1445 1446 1447

static int
x86Decode(virCPUDefPtr cpu,
1448
          const virCPUx86Data *data,
1449
          const char **models,
1450
          unsigned int nmodels,
1451 1452
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1453 1454 1455 1456
{
    int ret = -1;
    struct x86_map *map;
    const struct x86_model *candidate;
1457 1458
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1459
    size_t i;
J
Jiri Denemark 已提交
1460

1461 1462
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, -1);

J
Jiri Denemark 已提交
1463 1464 1465 1466 1467
    if (data == NULL || (map = x86LoadMap()) == NULL)
        return -1;

    candidate = map->models;
    while (candidate != NULL) {
1468
        if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
1469 1470
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1471 1472 1473
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
                    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);
            }
1484
            goto next;
J
Jiri Denemark 已提交
1485 1486
        }

1487 1488 1489
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto out;

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

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
        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;
                }
            }
        }

1512 1513 1514 1515 1516 1517
        if (preferred && STREQ(cpuCandidate->model, preferred)) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
            break;
        }

1518 1519 1520 1521 1522 1523 1524
        if (cpuModel == NULL
            || cpuModel->nfeatures > cpuCandidate->nfeatures) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
        } else
            virCPUDefFree(cpuCandidate);

J
Jiri Denemark 已提交
1525 1526 1527 1528
    next:
        candidate = candidate->next;
    }

1529
    if (cpuModel == NULL) {
1530 1531
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1532 1533 1534
        goto out;
    }

1535 1536 1537
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES &&
        x86AddFeatures(cpuModel, map) < 0)
        goto out;
1538
    cpu->model = cpuModel->model;
J
Jiri Denemark 已提交
1539
    cpu->vendor = cpuModel->vendor;
1540 1541 1542
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1543 1544 1545 1546 1547

    ret = 0;

out:
    x86MapFree(map);
1548
    virCPUDefFree(cpuModel);
J
Jiri Denemark 已提交
1549 1550 1551 1552

    return ret;
}

1553 1554
static int
x86DecodeCPUData(virCPUDefPtr cpu,
1555
                 const virCPUData *data,
1556 1557
                 const char **models,
                 unsigned int nmodels,
1558 1559
                 const char *preferred,
                 unsigned int flags)
1560
{
1561
    return x86Decode(cpu, data->data.x86, models, nmodels, preferred, flags);
1562
}
J
Jiri Denemark 已提交
1563

1564

1565
static virCPUx86Data *
1566
x86EncodePolicy(const virCPUDef *cpu,
J
Jiri Denemark 已提交
1567 1568 1569 1570
                const struct x86_map *map,
                enum virCPUFeaturePolicy policy)
{
    struct x86_model *model;
1571
    virCPUx86Data *data = NULL;
J
Jiri Denemark 已提交
1572 1573 1574 1575

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

J
Jiri Denemark 已提交
1576 1577
    data = model->data;
    model->data = NULL;
J
Jiri Denemark 已提交
1578 1579 1580 1581 1582 1583 1584
    x86ModelFree(model);

    return data;
}


static int
J
Jiri Denemark 已提交
1585
x86Encode(virArch arch,
1586
          const virCPUDef *cpu,
1587 1588 1589 1590 1591 1592
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
1593 1594
{
    struct x86_map *map = NULL;
1595 1596 1597 1598 1599 1600
    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 已提交
1601 1602
    int ret = -1;

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

J
Jiri Denemark 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
    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 已提交
1649 1650 1651 1652
    if (vendor) {
        const struct x86_vendor *v = NULL;

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
1653 1654
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s not found"), cpu->vendor);
J
Jiri Denemark 已提交
1655 1656 1657 1658 1659
            goto error;
        }

        if (v &&
            (VIR_ALLOC(data_vendor) < 0 ||
1660
             virCPUx86DataAddCPUID(data_vendor, &v->cpuid) < 0)) {
J
Jiri Denemark 已提交
1661 1662 1663 1664
            goto error;
        }
    }

1665
    if (forced &&
1666
        !(*forced = virCPUx86MakeData(arch, &data_forced)))
1667 1668
        goto error;
    if (required &&
1669
        !(*required = virCPUx86MakeData(arch, &data_required)))
1670 1671
        goto error;
    if (optional &&
1672
        !(*optional = virCPUx86MakeData(arch, &data_optional)))
1673 1674
        goto error;
    if (disabled &&
1675
        !(*disabled = virCPUx86MakeData(arch, &data_disabled)))
1676 1677
        goto error;
    if (forbidden &&
1678
        !(*forbidden = virCPUx86MakeData(arch, &data_forbidden)))
1679 1680
        goto error;
    if (vendor &&
1681
        !(*vendor = virCPUx86MakeData(arch, &data_vendor)))
1682
        goto error;
J
Jiri Denemark 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691

    ret = 0;

cleanup:
    x86MapFree(map);

    return ret;

error:
1692 1693 1694 1695 1696 1697
    virCPUx86DataFree(data_forced);
    virCPUx86DataFree(data_required);
    virCPUx86DataFree(data_optional);
    virCPUx86DataFree(data_disabled);
    virCPUx86DataFree(data_forbidden);
    virCPUx86DataFree(data_vendor);
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    if (forced)
        x86FreeCPUData(*forced);
    if (required)
        x86FreeCPUData(*required);
    if (optional)
        x86FreeCPUData(*optional);
    if (disabled)
        x86FreeCPUData(*disabled);
    if (forbidden)
        x86FreeCPUData(*forbidden);
    if (vendor)
        x86FreeCPUData(*vendor);
J
Jiri Denemark 已提交
1710 1711 1712 1713 1714 1715
    goto cleanup;
}


#if HAVE_CPUID
static inline void
1716
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
1717
{
1718
# if __x86_64__
1719 1720 1721 1722
    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 已提交
1723 1724 1725 1726 1727
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function));
1728
# else
J
Jiri Denemark 已提交
1729 1730 1731 1732
    /* 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;"
1733 1734 1735
        "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 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function)
        : "cc");
1745
# endif
J
Jiri Denemark 已提交
1746 1747 1748 1749
}


static int
1750
cpuidSet(uint32_t base, virCPUx86CPUID **set)
J
Jiri Denemark 已提交
1751 1752 1753
{
    uint32_t max;
    uint32_t i;
1754
    virCPUx86CPUID cpuid = { base, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
1755 1756 1757 1758

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

1759
    if (VIR_ALLOC_N(*set, max + 1) < 0)
J
Jiri Denemark 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
        return -1;

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

    return max + 1;
}


1772
static virCPUDataPtr
1773
x86NodeData(virArch arch)
J
Jiri Denemark 已提交
1774
{
1775
    virCPUDataPtr cpuData = NULL;
1776
    virCPUx86Data *data;
J
Jiri Denemark 已提交
1777
    int ret;
J
Jiri Denemark 已提交
1778

1779
    if (VIR_ALLOC(data) < 0)
J
Jiri Denemark 已提交
1780 1781
        return NULL;

1782
    if ((ret = cpuidSet(CPUX86_BASIC, &data->basic)) < 0)
J
Jiri Denemark 已提交
1783
        goto error;
1784
    data->basic_len = ret;
J
Jiri Denemark 已提交
1785

1786
    if ((ret = cpuidSet(CPUX86_EXTENDED, &data->extended)) < 0)
J
Jiri Denemark 已提交
1787
        goto error;
1788
    data->extended_len = ret;
J
Jiri Denemark 已提交
1789

1790
    if (!(cpuData = virCPUx86MakeData(arch, &data)))
1791 1792 1793
        goto error;

    return cpuData;
J
Jiri Denemark 已提交
1794 1795

error:
1796
    virCPUx86DataFree(data);
J
Jiri Denemark 已提交
1797 1798 1799 1800 1801 1802

    return NULL;
}
#endif


1803 1804 1805 1806
static virCPUDefPtr
x86Baseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
1807 1808
            unsigned int nmodels,
            unsigned int flags)
1809 1810 1811 1812
{
    struct x86_map *map = NULL;
    struct x86_model *base_model = NULL;
    virCPUDefPtr cpu = NULL;
1813
    size_t i;
J
Jiri Denemark 已提交
1814 1815
    const struct x86_vendor *vendor = NULL;
    struct x86_model *model = NULL;
1816
    bool outputVendor = true;
1817 1818 1819 1820

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

1821
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1822 1823
        goto error;

1824
    if (VIR_ALLOC(cpu) < 0)
1825
        goto error;
1826 1827

    cpu->arch = cpus[0]->arch;
1828 1829
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1830

1831 1832 1833
    if (!cpus[0]->vendor)
        outputVendor = false;
    else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
1834 1835
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Unknown CPU vendor %s"), cpus[0]->vendor);
J
Jiri Denemark 已提交
1836 1837 1838
        goto error;
    }

1839
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
1840 1841
        const char *vn = NULL;

1842
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
1843 1844
            goto error;

J
Jiri Denemark 已提交
1845 1846
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
1847 1848 1849
            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 已提交
1850 1851 1852 1853 1854
            goto error;
        }

        if (cpus[i]->vendor)
            vn = cpus[i]->vendor;
1855 1856 1857 1858 1859
        else {
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
1860 1861 1862 1863

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
1864 1865
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("Unknown CPU vendor %s"), vn);
J
Jiri Denemark 已提交
1866 1867 1868
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
1869 1870
                virReportError(VIR_ERR_OPERATION_FAILED,
                               "%s", _("CPU vendors do not match"));
J
Jiri Denemark 已提交
1871 1872 1873 1874
                goto error;
            }
        }

J
Jiri Denemark 已提交
1875
        x86DataIntersect(base_model->data, model->data);
1876
        x86ModelFree(model);
J
Jiri Denemark 已提交
1877
        model = NULL;
1878 1879
    }

J
Jiri Denemark 已提交
1880
    if (x86DataIsEmpty(base_model->data)) {
1881 1882
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
1883 1884 1885
        goto error;
    }

1886
    if (vendor && virCPUx86DataAddCPUID(base_model->data, &vendor->cpuid) < 0)
1887
        goto error;
J
Jiri Denemark 已提交
1888

1889
    if (x86Decode(cpu, base_model->data, models, nmodels, NULL, flags) < 0)
1890 1891
        goto error;

1892 1893 1894
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

1895 1896
    cpu->arch = VIR_ARCH_NONE;

1897 1898 1899 1900 1901 1902 1903
cleanup:
    x86ModelFree(base_model);
    x86MapFree(map);

    return cpu;

error:
J
Jiri Denemark 已提交
1904
    x86ModelFree(model);
1905 1906 1907 1908 1909 1910
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


1911
static int
1912
x86UpdateCustom(virCPUDefPtr guest,
1913
                const virCPUDef *host)
1914 1915
{
    int ret = -1;
1916
    size_t i;
1917 1918 1919 1920
    struct x86_map *map;
    struct x86_model *host_model = NULL;

    if (!(map = x86LoadMap()) ||
1921
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
1922 1923 1924 1925 1926 1927
        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))) {
1928 1929 1930
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unknown CPU feature %s"),
                               guest->features[i].name);
1931 1932 1933
                goto cleanup;
            }

J
Jiri Denemark 已提交
1934
            if (x86DataIsSubset(host_model->data, feature->data))
1935 1936 1937 1938 1939 1940 1941 1942
                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 已提交
1943 1944 1945
        if (x86ModelSubtractCPU(host_model, guest, map) ||
            x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
                                 host_model->data, map))
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
            goto cleanup;
    }

    ret = 0;

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

1957 1958 1959

static int
x86UpdateHostModel(virCPUDefPtr guest,
1960
                   const virCPUDef *host)
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
{
    virCPUDefPtr oldguest;
    size_t i;

    guest->match = VIR_CPU_MATCH_EXACT;

    /* no updates are required */
    if (guest->nfeatures == 0) {
        virCPUDefFreeModel(guest);
        return virCPUDefCopyModel(guest, host, true);
    }

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
        return -1;

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
        return -1;

    for (i = 0; i < oldguest->nfeatures; i++) {
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
            return -1;
    }

    return 0;
}


1992 1993
static int
x86Update(virCPUDefPtr guest,
1994
          const virCPUDef *host)
1995 1996 1997 1998 1999 2000
{
    switch ((enum virCPUMode) guest->mode) {
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2001 2002
        return x86UpdateHostModel(guest, host);

2003
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2004
        guest->match = VIR_CPU_MATCH_MINIMUM;
2005 2006 2007 2008 2009 2010 2011
        virCPUDefFreeModel(guest);
        return virCPUDefCopyModel(guest, host, true);

    case VIR_CPU_MODE_LAST:
        break;
    }

2012 2013
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2014 2015 2016
    return -1;
}

2017 2018 2019 2020

static int
x86HasFeature(const virCPUData *data,
              const char *name)
D
Daniel P. Berrange 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
{
    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 已提交
2032
    ret = x86DataIsSubset(data->data.x86, feature->data) ? 1 : 0;
D
Daniel P. Berrange 已提交
2033 2034 2035 2036 2037

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

2039

J
Jiri Denemark 已提交
2040 2041 2042 2043 2044
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
    .compare    = x86Compare,
2045
    .decode     = x86DecodeCPUData,
J
Jiri Denemark 已提交
2046
    .encode     = x86Encode,
2047
    .free       = x86FreeCPUData,
J
Jiri Denemark 已提交
2048 2049 2050 2051 2052
#if HAVE_CPUID
    .nodeData   = x86NodeData,
#else
    .nodeData   = NULL,
#endif
2053
    .guestData  = x86GuestData,
2054
    .baseline   = x86Baseline,
2055
    .update     = x86Update,
D
Daniel P. Berrange 已提交
2056
    .hasFeature = x86HasFeature,
2057 2058
    .dataFormat = x86CPUDataFormat,
    .dataParse  = x86CPUDataParse,
J
Jiri Denemark 已提交
2059
};