cputest.c 41.9 KB
Newer Older
1 2 3
/*
 * cputest.c: Test the libvirtd internal CPU APIs
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
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/>.
19 20 21 22 23 24 25 26 27 28
 */

#include <config.h>

#include <unistd.h>

#include <sys/types.h>
#include <fcntl.h>

#include "internal.h"
29
#include "virxml.h"
30
#include "viralloc.h"
31
#include "virbuffer.h"
32 33 34
#include "testutils.h"
#include "cpu_conf.h"
#include "cpu/cpu.h"
J
Jiri Denemark 已提交
35
#include "cpu/cpu_x86.h"
36
#include "cpu/cpu_map.h"
37
#include "virstring.h"
38

39
#if WITH_QEMU && WITH_YAJL
J
Jiri Denemark 已提交
40 41
# include "testutilsqemu.h"
# include "qemumonitortestutils.h"
42
# define LIBVIRT_QEMU_CAPSPRIV_H_ALLOW
43
# include "qemu/qemu_capspriv.h"
J
Jiri Denemark 已提交
44 45
#endif

46 47 48 49 50 51 52 53 54 55
#define VIR_FROM_THIS VIR_FROM_CPU

enum cpuTestBoolWithError {
    FAIL    = -1,
    NO      = 0,
    YES     = 1
};


struct data {
56
    virArch arch;
57 58
    const char *host;
    const char *name;
59
    virDomainCapsCPUModelsPtr models;
60
    const char *modelsName;
61
    unsigned int flags;
62 63 64
    int result;
};

65
#if WITH_QEMU && WITH_YAJL
J
Jiri Denemark 已提交
66 67 68
static virQEMUDriver driver;
#endif

69 70

static virCPUDefPtr
71
cpuTestLoadXML(virArch arch, const char *name)
72
{
73
    char *xml = NULL;
74 75 76 77
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr cpu = NULL;

78 79
    if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml",
                    abs_srcdir, virArchToString(arch), name) < 0)
80
        goto cleanup;
81

82
    if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
83 84
        goto cleanup;

J
Jiri Denemark 已提交
85
    virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu);
86

87
 cleanup:
88 89
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
90
    VIR_FREE(xml);
91 92 93 94 95
    return cpu;
}


static virCPUDefPtr *
96
cpuTestLoadMultiXML(virArch arch,
97 98 99
                    const char *name,
                    unsigned int *count)
{
100
    char *xml = NULL;
101 102 103 104 105
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDefPtr *cpus = NULL;
    int n;
106
    size_t i;
107

108 109
    if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml",
                    abs_srcdir, virArchToString(arch), name) < 0)
110
        goto cleanup;
111

112
    if (!(doc = virXMLParseFileCtxt(xml, &ctxt)))
J
Jiri Denemark 已提交
113
        goto cleanup;
114 115

    n = virXPathNodeSet("/cpuTest/cpu", ctxt, &nodes);
116 117
    if (n <= 0 || (VIR_ALLOC_N(cpus, n) < 0)) {
        fprintf(stderr, "\nNo /cpuTest/cpu elements found in %s\n", xml);
J
Jiri Denemark 已提交
118
        goto cleanup;
119
    }
120 121 122

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
J
Jiri Denemark 已提交
123
        if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0)
J
Jiri Denemark 已提交
124
            goto cleanup_cpus;
125 126 127 128
    }

    *count = n;

129
 cleanup:
130 131
    VIR_FREE(xml);
    VIR_FREE(nodes);
132 133 134 135
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
    return cpus;

136
 cleanup_cpus:
J
Jiri Denemark 已提交
137 138 139
    for (i = 0; i < n; i++)
        virCPUDefFree(cpus[i]);
    VIR_FREE(cpus);
140 141 142 143 144
    goto cleanup;
}


static int
145
cpuTestCompareXML(virArch arch,
E
Eric Blake 已提交
146
                  virCPUDef *cpu,
147
                  const char *name)
148
{
149
    char *xml = NULL;
150 151 152
    char *actual = NULL;
    int ret = -1;

153
    if (virAsprintf(&xml, "%s/cputestdata/%s-%s.xml",
154
                    abs_srcdir, virArchToString(arch), name) < 0)
155
        goto cleanup;
156

157
    if (!(actual = virCPUDefFormat(cpu, NULL)))
158 159
        goto cleanup;

160
    if (virTestCompareToFile(actual, xml) < 0)
161 162 163 164
        goto cleanup;

    ret = 0;

165
 cleanup:
166 167
    VIR_FREE(xml);
    VIR_FREE(actual);
168 169 170 171 172 173 174 175 176 177 178 179
    return ret;
}


static const char *
cpuTestCompResStr(virCPUCompareResult result)
{
    switch (result) {
    case VIR_CPU_COMPARE_ERROR:         return "ERROR";
    case VIR_CPU_COMPARE_INCOMPATIBLE:  return "INCOMPATIBLE";
    case VIR_CPU_COMPARE_IDENTICAL:     return "IDENTICAL";
    case VIR_CPU_COMPARE_SUPERSET:      return "SUPERSET";
180
    case VIR_CPU_COMPARE_LAST:          break;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    }

    return "unknown";
}


static const char *
cpuTestBoolWithErrorStr(enum cpuTestBoolWithError result)
{
    switch (result) {
    case FAIL:  return "FAIL";
    case NO:    return "NO";
    case YES:   return "YES";
    }

    return "unknown";
}


static int
cpuTestCompare(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr host = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult result;

    if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
        !(cpu = cpuTestLoadXML(data->arch, data->name)))
        goto cleanup;

J
Jiri Denemark 已提交
213
    result = virCPUCompare(host->arch, host, cpu, false);
214 215 216 217
    if (data->result == VIR_CPU_COMPARE_ERROR)
        virResetLastError();

    if (data->result != result) {
218
        VIR_TEST_VERBOSE("\nExpected result %s, got %s",
219 220
                    cpuTestCompResStr(data->result),
                    cpuTestCompResStr(result));
221 222
        /* Pad to line up with test name ... in virTestRun */
        VIR_TEST_VERBOSE("%74s", "... ");
223 224 225 226 227
        goto cleanup;
    }

    ret = 0;

228
 cleanup:
229 230 231 232 233 234 235
    virCPUDefFree(host);
    virCPUDefFree(cpu);
    return ret;
}


static int
J
Jiri Denemark 已提交
236
cpuTestGuestCPU(const void *arg)
237 238
{
    const struct data *data = arg;
239
    int ret = -2;
240 241 242 243 244 245 246 247 248 249
    virCPUDefPtr host = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult cmpResult;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *result = NULL;

    if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
        !(cpu = cpuTestLoadXML(data->arch, data->name)))
        goto cleanup;

250 251 252
    if (virCPUConvertLegacy(host->arch, cpu) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
253
    cmpResult = virCPUCompare(host->arch, host, cpu, false);
254
    if (cmpResult == VIR_CPU_COMPARE_ERROR ||
255 256
        cmpResult == VIR_CPU_COMPARE_INCOMPATIBLE) {
        ret = -1;
257
        goto cleanup;
258
    }
259

J
Jiri Denemark 已提交
260
    if (virCPUUpdate(host->arch, cpu, host) < 0 ||
261
        virCPUTranslate(host->arch, cpu, data->models) < 0) {
262
        ret = -1;
263 264 265
        goto cleanup;
    }

266
    virBufferAsprintf(&buf, "%s+%s", data->host, data->name);
267
    if (data->modelsName)
268
        virBufferAsprintf(&buf, ",%s", data->modelsName);
269 270 271 272 273 274 275 276
    virBufferAddLit(&buf, "-result");

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        goto cleanup;
    }
    result = virBufferContentAndReset(&buf);

277
    if (cpuTestCompareXML(data->arch, cpu, result) < 0)
278 279 280
        goto cleanup;

    ret = 0;
281

282
 cleanup:
283 284 285
    VIR_FREE(result);
    virCPUDefFree(host);
    virCPUDefFree(cpu);
286 287 288 289 290 291 292

    if (ret == data->result) {
        /* We got the result we expected, whether it was
         * a success or a failure */
        virResetLastError();
        ret = 0;
    } else {
293
        VIR_TEST_VERBOSE("\nExpected result %d, got %d",
294 295 296 297 298 299
                         data->result, ret);
        /* Pad to line up with test name ... in virTestRun */
        VIR_TEST_VERBOSE("%74s", "... ");
        ret = -1;
    }

300 301 302 303 304 305 306 307 308 309 310 311
    return ret;
}


static int
cpuTestBaseline(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr baseline = NULL;
    unsigned int ncpus = 0;
312
    char *result = NULL;
313
    const char *suffix;
314
    size_t i;
315 316 317 318

    if (!(cpus = cpuTestLoadMultiXML(data->arch, data->name, &ncpus)))
        goto cleanup;

319
    baseline = virCPUBaseline(data->arch, cpus, ncpus, NULL, NULL,
320
                              !!(data->flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE));
321 322 323 324 325 326 327 328

    if (baseline &&
        (data->flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) &&
        virCPUExpandFeatures(data->arch, baseline) < 0) {
        virCPUDefFree(baseline);
        baseline = NULL;
    }

329 330
    if (data->result < 0) {
        virResetLastError();
331
        if (!baseline) {
332
            ret = 0;
333 334
        } else {
            VIR_TEST_VERBOSE("\n%-70s... ",
335
                    "virCPUBaseline was expected to fail but it succeeded");
336 337 338 339 340 341
        }
        goto cleanup;
    }
    if (!baseline)
        goto cleanup;

342 343
    if (data->flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES)
        suffix = "expanded";
344 345
    else if (data->flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE)
        suffix = "migratable";
346 347 348
    else
        suffix = "result";
    if (virAsprintf(&result, "%s-%s", data->name, suffix) < 0)
349 350
        goto cleanup;

351
    if (cpuTestCompareXML(data->arch, baseline, result) < 0)
352 353 354 355 356
        goto cleanup;

    for (i = 0; i < ncpus; i++) {
        virCPUCompareResult cmp;

J
Jiri Denemark 已提交
357
        cmp = virCPUCompare(cpus[i]->arch, cpus[i], baseline, false);
358 359
        if (cmp != VIR_CPU_COMPARE_SUPERSET &&
            cmp != VIR_CPU_COMPARE_IDENTICAL) {
360
            VIR_TEST_VERBOSE("\nbaseline CPU is incompatible with CPU %zu",
361 362
                             i);
            VIR_TEST_VERBOSE("%74s", "... ");
363 364 365 366 367 368 369
            ret = -1;
            goto cleanup;
        }
    }

    ret = 0;

370
 cleanup:
371 372 373
    if (cpus) {
        for (i = 0; i < ncpus; i++)
            virCPUDefFree(cpus[i]);
374
        VIR_FREE(cpus);
375 376
    }
    virCPUDefFree(baseline);
377
    VIR_FREE(result);
378 379 380 381 382 383 384 385 386 387
    return ret;
}


static int
cpuTestUpdate(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr host = NULL;
388
    virCPUDefPtr migHost = NULL;
389
    virCPUDefPtr cpu = NULL;
390
    char *result = NULL;
391 392 393 394 395

    if (!(host = cpuTestLoadXML(data->arch, data->host)) ||
        !(cpu = cpuTestLoadXML(data->arch, data->name)))
        goto cleanup;

396 397 398 399
    if (!(migHost = virCPUCopyMigratable(data->arch, host)))
        goto cleanup;

    if (virCPUUpdate(host->arch, cpu, migHost) < 0)
400 401
        goto cleanup;

402 403 404
    if (virAsprintf(&result, "%s+%s", data->host, data->name) < 0)
        goto cleanup;

405
    ret = cpuTestCompareXML(data->arch, cpu, result);
406

407
 cleanup:
408 409
    virCPUDefFree(host);
    virCPUDefFree(cpu);
410
    virCPUDefFree(migHost);
411
    VIR_FREE(result);
412 413 414 415 416 417 418 419 420 421
    return ret;
}


static int
cpuTestHasFeature(const void *arg)
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDefPtr host = NULL;
422
    virCPUDataPtr hostData = NULL;
423 424 425 426 427 428 429 430 431
    int result;

    if (!(host = cpuTestLoadXML(data->arch, data->host)))
        goto cleanup;

    if (cpuEncode(host->arch, host, NULL, &hostData,
                  NULL, NULL, NULL, NULL) < 0)
        goto cleanup;

432 433 434 435 436
    result = virCPUCheckFeature(host->arch, host, data->name);

    if (data->result == result)
        result = virCPUDataCheckFeature(hostData, data->name);

437 438 439 440
    if (data->result == -1)
        virResetLastError();

    if (data->result != result) {
441
        VIR_TEST_VERBOSE("\nExpected result %s, got %s",
442 443 444 445
            cpuTestBoolWithErrorStr(data->result),
            cpuTestBoolWithErrorStr(result));
        /* Pad to line up with test name ... in virTestRun */
        VIR_TEST_VERBOSE("%74s", "... ");
446 447 448 449 450
        goto cleanup;
    }

    ret = 0;

451
 cleanup:
J
Jiri Denemark 已提交
452
    virCPUDataFree(hostData);
453 454 455 456 457
    virCPUDefFree(host);
    return ret;
}


458
typedef enum {
459
    /* No JSON data from QEMU. */
460
    JSON_NONE,
461
    /* Only a reply from query-cpu-model-expansion QMP command. */
462
    JSON_HOST,
463 464 465
    /* Replies from both query-cpu-model-expansion and query-cpu-definitions
     * QMP commands.
     */
466
    JSON_MODELS,
467 468 469 470 471 472 473 474
    /* Same as JSON_MODELS, but the reply from query-cpu-definitions has to
     * be parsed for providing the correct result. This happens when the
     * CPU model detected by libvirt has non-empty unavailable-features array
     * in query-cpu-definitions reply or when the CPU model detected from CPUID
     * differs from the one we get from QEMU and we need to translate them for
     * comparison. Such tests require QEMU driver to be enabled.
     */
    JSON_MODELS_REQUIRED,
475 476
} cpuTestCPUIDJson;

477
#if WITH_QEMU && WITH_YAJL
478 479 480 481 482 483
static virQEMUCapsPtr
cpuTestMakeQEMUCaps(const struct data *data)
{
    virQEMUCapsPtr qemuCaps = NULL;
    qemuMonitorTestPtr testMon = NULL;
    qemuMonitorCPUModelInfoPtr model = NULL;
484
    virCPUDefPtr cpu = NULL;
485 486 487 488 489 490 491 492 493
    char *json = NULL;

    if (virAsprintf(&json, "%s/cputestdata/%s-cpuid-%s.json",
                    abs_srcdir, virArchToString(data->arch), data->host) < 0)
        goto error;

    if (!(testMon = qemuMonitorTestNewFromFile(json, driver.xmlopt, true)))
        goto error;

494 495 496
    if (VIR_ALLOC(cpu) < 0 || VIR_STRDUP(cpu->model, "host") < 0)
        goto cleanup;

497 498
    if (qemuMonitorGetCPUModelExpansion(qemuMonitorTestGetMonitor(testMon),
                                        QEMU_MONITOR_CPU_MODEL_EXPANSION_STATIC,
499
                                        cpu, true, &model) < 0)
500 501 502 503 504 505
        goto error;

    if (!(qemuCaps = virQEMUCapsNew()))
        goto error;

    virQEMUCapsSet(qemuCaps, QEMU_CAPS_KVM);
506 507
    if (data->flags == JSON_MODELS ||
        data->flags == JSON_MODELS_REQUIRED)
508 509 510 511 512 513 514 515 516 517 518 519 520 521
        virQEMUCapsSet(qemuCaps, QEMU_CAPS_QUERY_CPU_DEFINITIONS);

    virQEMUCapsSetArch(qemuCaps, data->arch);
    virQEMUCapsSetCPUModelInfo(qemuCaps, VIR_DOMAIN_VIRT_KVM, model);
    model = NULL;

    if (virQEMUCapsProbeQMPCPUDefinitions(qemuCaps,
                                          qemuMonitorTestGetMonitor(testMon),
                                          false) < 0)
        goto error;

 cleanup:
    qemuMonitorCPUModelInfoFree(model);
    qemuMonitorTestFree(testMon);
522
    virCPUDefFree(cpu);
523 524 525 526 527 528 529 530 531
    VIR_FREE(json);

    return qemuCaps;

 error:
    virObjectUnref(qemuCaps);
    qemuCaps = NULL;
    goto cleanup;
}
532 533


534 535 536
static int
cpuTestGetCPUModels(const struct data *data,
                    virDomainCapsCPUModelsPtr *models)
537 538 539
{
    virQEMUCapsPtr qemuCaps;

540 541 542 543 544
    *models = NULL;

    if (data->flags != JSON_MODELS &&
        data->flags != JSON_MODELS_REQUIRED)
        return 0;
545 546

    if (!(qemuCaps = cpuTestMakeQEMUCaps(data)))
547
        return -1;
548

549 550
    *models = virQEMUCapsGetCPUDefinitions(qemuCaps, VIR_DOMAIN_VIRT_KVM);
    virObjectRef(*models);
551 552 553

    virObjectUnref(qemuCaps);

554
    return 0;
555 556
}

557
#else /* if WITH_QEMU && WITH_YAJL */
558

559 560 561
static int
cpuTestGetCPUModels(const struct data *data,
                    virDomainCapsCPUModelsPtr *models)
562
{
563 564 565 566 567 568
    *models = NULL;

    if (data->flags == JSON_MODELS_REQUIRED)
        return EXIT_AM_SKIP;

    return 0;
569 570
}

571 572 573
#endif


J
Jiri Denemark 已提交
574
static int
575
cpuTestCPUID(bool guest, const void *arg)
J
Jiri Denemark 已提交
576 577 578 579 580 581 582 583
{
    const struct data *data = arg;
    int ret = -1;
    virCPUDataPtr hostData = NULL;
    char *hostFile = NULL;
    char *host = NULL;
    virCPUDefPtr cpu = NULL;
    char *result = NULL;
584
    virDomainCapsCPUModelsPtr models = NULL;
J
Jiri Denemark 已提交
585 586

    if (virAsprintf(&hostFile, "%s/cputestdata/%s-cpuid-%s.xml",
587
                    abs_srcdir, virArchToString(data->arch), data->host) < 0)
J
Jiri Denemark 已提交
588 589 590
        goto cleanup;

    if (virTestLoadFile(hostFile, &host) < 0 ||
J
Jiri Denemark 已提交
591
        !(hostData = virCPUDataParse(host)))
J
Jiri Denemark 已提交
592 593 594 595 596 597
        goto cleanup;

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

    cpu->arch = hostData->arch;
598
    if (guest) {
J
Jiri Denemark 已提交
599 600 601 602 603 604 605
        cpu->type = VIR_CPU_TYPE_GUEST;
        cpu->match = VIR_CPU_MATCH_EXACT;
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;
    } else {
        cpu->type = VIR_CPU_TYPE_HOST;
    }

606 607 608 609 610 611 612 613 614
    if (guest) {
        int rc;

        rc = cpuTestGetCPUModels(data, &models);
        if (rc != 0) {
            ret = rc;
            goto cleanup;
        }
    }
615 616

    if (cpuDecode(cpu, hostData, models) < 0)
J
Jiri Denemark 已提交
617 618 619 620
        goto cleanup;

    if (virAsprintf(&result, "cpuid-%s-%s",
                    data->host,
621
                    guest ? "guest" : "host") < 0)
J
Jiri Denemark 已提交
622 623
        goto cleanup;

624
    ret = cpuTestCompareXML(data->arch, cpu, result);
J
Jiri Denemark 已提交
625 626 627 628

 cleanup:
    VIR_FREE(hostFile);
    VIR_FREE(host);
J
Jiri Denemark 已提交
629
    virCPUDataFree(hostData);
J
Jiri Denemark 已提交
630 631
    virCPUDefFree(cpu);
    VIR_FREE(result);
632
    virObjectUnref(models);
J
Jiri Denemark 已提交
633 634 635 636
    return ret;
}


637 638 639 640 641 642 643 644 645 646 647 648 649 650
static int
cpuTestHostCPUID(const void *arg)
{
    return cpuTestCPUID(false, arg);
}


static int
cpuTestGuestCPUID(const void *arg)
{
    return cpuTestCPUID(true, arg);
}


J
Jiri Denemark 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
static int
cpuTestCompareSignature(const struct data *data,
                        virCPUDataPtr hostData)
{
    VIR_AUTOFREE(char *) result = NULL;
    VIR_AUTOFREE(char *) sigStr = NULL;
    unsigned long signature;
    unsigned int family;
    unsigned int model;
    unsigned int stepping;

    signature = virCPUx86DataGetSignature(hostData, &family, &model, &stepping);

    if (virAsprintf(&result, "%s/cputestdata/%s-cpuid-%s.sig",
                    abs_srcdir, virArchToString(data->arch), data->host) < 0)
        return -1;

    if (virAsprintf(&sigStr,
                    "%1$06lx\n"
                    "family:   %2$3u (0x%2$02x)\n"
                    "model:    %3$3u (0x%3$02x)\n"
                    "stepping: %4$3u (0x%4$02x)\n",
                    signature, family, model, stepping) < 0)
        return -1;

    return virTestCompareToFile(sigStr, result);
}


static int
cpuTestCPUIDSignature(const void *arg)
{
    const struct data *data = arg;
    virCPUDataPtr hostData = NULL;
    char *hostFile = NULL;
    char *host = NULL;
    int ret = -1;

    if (virAsprintf(&hostFile, "%s/cputestdata/%s-cpuid-%s.xml",
                    abs_srcdir, virArchToString(data->arch), data->host) < 0)
        goto cleanup;

    if (virTestLoadFile(hostFile, &host) < 0 ||
        !(hostData = virCPUDataParse(host)))
        goto cleanup;

    ret = cpuTestCompareSignature(data, hostData);

 cleanup:
    virCPUDataFree(hostData);
    VIR_FREE(hostFile);
    VIR_FREE(host);
    return ret;
}


707 708 709 710 711 712 713 714 715 716 717 718 719
static int
cpuTestUpdateLiveCompare(virArch arch,
                         virCPUDefPtr actual,
                         virCPUDefPtr expected)
{
    size_t i, j;
    int ret = 0;

    if (virCPUExpandFeatures(arch, actual) < 0 ||
        virCPUExpandFeatures(arch, expected) < 0)
        return -1;

    if (STRNEQ(actual->model, expected->model)) {
720
        VIR_TEST_VERBOSE("Actual CPU model '%s', expected '%s'",
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
                         actual->model, expected->model);
        return -1;
    }

    i = j = 0;
    while (i < actual->nfeatures || j < expected->nfeatures) {
        virCPUFeatureDefPtr featAct = NULL;
        virCPUFeatureDefPtr featExp = NULL;
        int cmp;

        if (i < actual->nfeatures)
            featAct = actual->features + i;

        if (j < expected->nfeatures)
            featExp = expected->features + j;

        /*
         * Act < Exp => cmp < 0 (missing entry in Exp)
         * Act = Exp => cmp = 0
         * Act > Exp => cmp > 0 (missing entry in Act)
         *
         * NULL > name for any name != NULL
         */
        if (featAct && featExp)
            cmp = strcmp(featAct->name, featExp->name);
        else
            cmp = featExp ? 1 : -1;

        if (cmp <= 0)
            i++;
        if (cmp >= 0)
            j++;

        /* Possible combinations of cmp, featAct->policy, and featExp->policy:
         *  cmp     Act     Exp     result
         * ---------------------------------
         *   0      dis     dis      ok
         *   0      dis     req     missing
         *   0      req     dis     extra
         *   0      req     req      ok
         * ---------------------------------
         *   -      dis      X       ok     # ignoring extra disabled features
         *   -      req      X      extra
         * ---------------------------------
         *   +       X      dis     extra
         *   +       X      req     missing
         */
        if ((cmp == 0 &&
             featAct->policy == VIR_CPU_FEATURE_DISABLE &&
             featExp->policy == VIR_CPU_FEATURE_REQUIRE) ||
            (cmp > 0 &&
             featExp->policy == VIR_CPU_FEATURE_REQUIRE)) {
773
            VIR_TEST_VERBOSE("Actual CPU lacks feature '%s'",
774 775 776 777 778 779 780 781 782 783 784 785
                             featExp->name);
            ret = -1;
            continue;
        }

        if ((cmp == 0 &&
             featAct->policy == VIR_CPU_FEATURE_REQUIRE &&
             featExp->policy == VIR_CPU_FEATURE_DISABLE) ||
            (cmp < 0 &&
             featAct->policy == VIR_CPU_FEATURE_REQUIRE) ||
            (cmp > 0 &&
             featExp->policy == VIR_CPU_FEATURE_DISABLE)) {
786
            VIR_TEST_VERBOSE("Actual CPU has extra feature '%s'",
787
                             cmp <= 0 ? featAct->name : featExp->name);
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
            ret = -1;
        }
    }

    return ret;
}


static int
cpuTestUpdateLive(const void *arg)
{
    const struct data *data = arg;
    char *cpuFile = NULL;
    virCPUDefPtr cpu = NULL;
    char *enabledFile = NULL;
    char *enabled = NULL;
    virCPUDataPtr enabledData = NULL;
    char *disabledFile = NULL;
    char *disabled = NULL;
    virCPUDataPtr disabledData = NULL;
    char *expectedFile = NULL;
    virCPUDefPtr expected = NULL;
810 811
    virDomainCapsCPUModelsPtr hvModels = NULL;
    virDomainCapsCPUModelsPtr models = NULL;
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
    int ret = -1;

    if (virAsprintf(&cpuFile, "cpuid-%s-guest", data->host) < 0 ||
        !(cpu = cpuTestLoadXML(data->arch, cpuFile)))
        goto cleanup;

    if (virAsprintf(&enabledFile, "%s/cputestdata/%s-cpuid-%s-enabled.xml",
                    abs_srcdir, virArchToString(data->arch), data->host) < 0 ||
        virTestLoadFile(enabledFile, &enabled) < 0 ||
        !(enabledData = virCPUDataParse(enabled)))
        goto cleanup;

    if (virAsprintf(&disabledFile, "%s/cputestdata/%s-cpuid-%s-disabled.xml",
                    abs_srcdir, virArchToString(data->arch), data->host) < 0 ||
        virTestLoadFile(disabledFile, &disabled) < 0 ||
        !(disabledData = virCPUDataParse(disabled)))
        goto cleanup;

    if (virAsprintf(&expectedFile, "cpuid-%s-json", data->host) < 0 ||
        !(expected = cpuTestLoadXML(data->arch, expectedFile)))
        goto cleanup;

834 835 836 837 838 839 840 841 842 843
    /* In case the host CPU signature does not exactly match any CPU model from
     * cpu_map.xml, the CPU model we detect from CPUID may differ from the one
     * we compute by asking QEMU. Since this test expands both CPU models and
     * compares their features, we can try to translate the 'actual' CPU to
     * use the CPU model from 'expected'.
     */
    if (STRNEQ(cpu->model, expected->model)) {
        virDomainCapsCPUModelPtr hvModel;
        char **blockers = NULL;
        virDomainCapsCPUUsable usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
844
        int rc;
845 846 847 848

        if (!(models = virDomainCapsCPUModelsNew(0)))
            goto cleanup;

849 850 851 852 853 854
        rc = cpuTestGetCPUModels(data, &hvModels);
        if (rc != 0) {
            ret = rc;
            goto cleanup;
        }

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
        hvModel = virDomainCapsCPUModelsGet(hvModels, expected->model);

        if (hvModel) {
            blockers = hvModel->blockers;
            usable = hvModel->usable;
        }

        if (virDomainCapsCPUModelsAdd(models, expected->model, -1,
                                      usable, blockers) < 0)
            goto cleanup;

        cpu->fallback = VIR_CPU_FALLBACK_ALLOW;
        ignore_value(virCPUTranslate(data->arch, cpu, models));
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;
    }

    if (virCPUUpdateLive(data->arch, cpu, enabledData, disabledData) < 0)
        goto cleanup;

874 875 876 877 878 879 880 881 882 883 884 885 886
    ret = cpuTestUpdateLiveCompare(data->arch, cpu, expected);

 cleanup:
    VIR_FREE(cpuFile);
    virCPUDefFree(cpu);
    VIR_FREE(enabledFile);
    VIR_FREE(enabled);
    virCPUDataFree(enabledData);
    VIR_FREE(disabledFile);
    VIR_FREE(disabled);
    virCPUDataFree(disabledData);
    VIR_FREE(expectedFile);
    virCPUDefFree(expected);
887 888
    virObjectUnref(hvModels);
    virObjectUnref(models);
889 890 891 892
    return ret;
}


893
#if WITH_QEMU && WITH_YAJL
J
Jiri Denemark 已提交
894 895 896 897
static int
cpuTestJSONCPUID(const void *arg)
{
    const struct data *data = arg;
898
    virQEMUCapsPtr qemuCaps = NULL;
J
Jiri Denemark 已提交
899 900 901 902
    virCPUDefPtr cpu = NULL;
    char *result = NULL;
    int ret = -1;

903
    if (virAsprintf(&result, "cpuid-%s-json", data->host) < 0)
J
Jiri Denemark 已提交
904 905
        goto cleanup;

906
    if (!(qemuCaps = cpuTestMakeQEMUCaps(data)))
907 908
        goto cleanup;

J
Jiri Denemark 已提交
909 910 911
    if (VIR_ALLOC(cpu) < 0)
        goto cleanup;

912
    cpu->arch = data->arch;
J
Jiri Denemark 已提交
913 914 915 916
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
    cpu->fallback = VIR_CPU_FALLBACK_FORBID;

917
    if (virQEMUCapsInitCPUModel(qemuCaps, VIR_DOMAIN_VIRT_KVM, cpu, false) != 0)
J
Jiri Denemark 已提交
918 919
        goto cleanup;

920
    ret = cpuTestCompareXML(data->arch, cpu, result);
J
Jiri Denemark 已提交
921 922

 cleanup:
923
    virObjectUnref(qemuCaps);
J
Jiri Denemark 已提交
924 925 926 927
    virCPUDefFree(cpu);
    VIR_FREE(result);
    return ret;
}
J
Jiri Denemark 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942


static int
cpuTestJSONSignature(const void *arg)
{
    const struct data *data = arg;
    virQEMUCapsPtr qemuCaps = NULL;
    virCPUDataPtr hostData = NULL;
    qemuMonitorCPUModelInfoPtr modelInfo;
    int ret = -1;

    if (!(qemuCaps = cpuTestMakeQEMUCaps(data)))
        goto cleanup;

    modelInfo = virQEMUCapsGetCPUModelInfo(qemuCaps, VIR_DOMAIN_VIRT_KVM);
943
    if (!(hostData = virQEMUCapsGetCPUModelX86Data(qemuCaps, modelInfo, false)))
J
Jiri Denemark 已提交
944 945 946 947 948 949 950 951 952
        goto cleanup;

    ret = cpuTestCompareSignature(data, hostData);

 cleanup:
    virObjectUnref(qemuCaps);
    virCPUDataFree(hostData);
    return ret;
}
J
Jiri Denemark 已提交
953 954 955
#endif


956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
static const char *model486_list[]   = { "486", NULL };
static const char *nomodel_list[]    = { "nomodel", NULL };
static const char *models_list[]     = { "qemu64", "core2duo", "Nehalem", NULL };
static const char *haswell_list[]    = { "SandyBridge", "Haswell", NULL };
static const char *ppc_models_list[] = { "POWER6", "POWER7", "POWER8", NULL };

static virDomainCapsCPUModelsPtr
cpuTestInitModels(const char **list)
{
    virDomainCapsCPUModelsPtr cpus;
    const char **model;

    if (!(cpus = virDomainCapsCPUModelsNew(0)))
        return NULL;

    for (model = list; *model; model++) {
        if (virDomainCapsCPUModelsAdd(cpus, *model, -1,
                                      VIR_DOMCAPS_CPU_USABLE_UNKNOWN, NULL) < 0)
            goto error;
    }

    return cpus;

 error:
    virObjectUnref(cpus);
    return NULL;
}

984 985

static int
E
Eric Blake 已提交
986
mymain(void)
987
{
988 989 990 991 992
    virDomainCapsCPUModelsPtr model486 = NULL;
    virDomainCapsCPUModelsPtr nomodel = NULL;
    virDomainCapsCPUModelsPtr models = NULL;
    virDomainCapsCPUModelsPtr haswell = NULL;
    virDomainCapsCPUModelsPtr ppc_models = NULL;
993 994
    int ret = 0;

995
#if WITH_QEMU && WITH_YAJL
J
Jiri Denemark 已提交
996 997 998 999 1000 1001
    if (qemuTestDriverInit(&driver) < 0)
        return EXIT_FAILURE;

    virEventRegisterDefaultImpl();
#endif

1002 1003 1004 1005 1006 1007 1008 1009 1010
    if (!(model486 = cpuTestInitModels(model486_list)) ||
        !(nomodel = cpuTestInitModels(nomodel_list)) ||
        !(models = cpuTestInitModels(models_list)) ||
        !(haswell = cpuTestInitModels(haswell_list)) ||
        !(ppc_models = cpuTestInitModels(ppc_models_list))) {
        ret = -1;
        goto cleanup;
    }

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
#define DO_TEST(arch, api, name, host, cpu, \
                models, flags, result) \
    do { \
        struct data data = { \
            arch, host, cpu, models, \
            models == NULL ? NULL : #models, \
            flags, result \
        }; \
        char *testLabel; \
        char *tmp; \
 \
        tmp = virTestLogContentAndReset(); \
        VIR_FREE(tmp); \
 \
        if (virAsprintf(&testLabel, "%s(%s): %s", \
                        #api, virArchToString(arch), name) < 0) { \
            ret = -1; \
            break; \
        } \
 \
        if (virTestRun(testLabel, api, &data) < 0) { \
            if (virTestGetDebug()) { \
                char *log; \
                if ((log = virTestLogContentAndReset()) && \
                     strlen(log) > 0) \
1036
                    VIR_TEST_DEBUG("\n%s", log); \
1037 1038 1039 1040 1041 1042
                VIR_FREE(log); \
            } \
            ret = -1; \
        } \
 \
        VIR_FREE(testLabel); \
1043 1044
    } while (0)

1045 1046 1047
#define DO_TEST_COMPARE(arch, host, cpu, result) \
    DO_TEST(arch, cpuTestCompare, \
            host "/" cpu " (" #result ")", \
1048
            host, cpu, NULL, 0, result)
1049

1050 1051 1052
#define DO_TEST_UPDATE_ONLY(arch, host, cpu) \
    DO_TEST(arch, cpuTestUpdate, \
            cpu " on " host, \
1053
            host, cpu, NULL, 0, 0)
J
Jiri Denemark 已提交
1054

1055 1056 1057 1058
#define DO_TEST_UPDATE(arch, host, cpu, result) \
    do { \
        DO_TEST_UPDATE_ONLY(arch, host, cpu); \
        DO_TEST_COMPARE(arch, host, host "+" cpu, result); \
1059 1060
    } while (0)

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
#define DO_TEST_BASELINE(arch, name, flags, result) \
    do { \
        const char *suffix = ""; \
        char *label; \
        if ((flags) & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) \
            suffix = " (expanded)"; \
        if ((flags) & VIR_CONNECT_BASELINE_CPU_MIGRATABLE) \
            suffix = " (migratable)"; \
        if (virAsprintf(&label, "%s%s", name, suffix) < 0) { \
            ret = -1; \
        } else { \
            DO_TEST(arch, cpuTestBaseline, label, NULL, \
                    "baseline-" name, NULL, flags, result); \
        } \
        VIR_FREE(label); \
1076
    } while (0)
1077

1078 1079 1080
#define DO_TEST_HASFEATURE(arch, host, feature, result) \
    DO_TEST(arch, cpuTestHasFeature, \
            host "/" feature " (" #result ")", \
1081
            host, feature, NULL, 0, result)
1082

1083 1084 1085
#define DO_TEST_GUESTCPU(arch, host, cpu, models, result) \
    DO_TEST(arch, cpuTestGuestCPU, \
            host "/" cpu " (" #models ")", \
1086
            host, cpu, models, 0, result)
1087

1088
#if WITH_QEMU && WITH_YAJL
1089 1090 1091 1092 1093 1094 1095 1096 1097
# define DO_TEST_JSON(arch, host, json) \
    do { \
        if (json == JSON_MODELS) { \
            DO_TEST(arch, cpuTestGuestCPUID, host, host, \
                    NULL, NULL, 0, 0); \
        } \
        if (json != JSON_NONE) { \
            DO_TEST(arch, cpuTestJSONCPUID, host, host, \
                    NULL, NULL, json, 0); \
J
Jiri Denemark 已提交
1098 1099
            DO_TEST(arch, cpuTestJSONSignature, host, host, \
                    NULL, NULL, 0, 0); \
1100
        } \
J
Jiri Denemark 已提交
1101 1102
    } while (0)
#else
1103
# define DO_TEST_JSON(arch, host, json)
J
Jiri Denemark 已提交
1104 1105
#endif

1106 1107 1108 1109 1110 1111
#define DO_TEST_CPUID(arch, host, json) \
    do { \
        DO_TEST(arch, cpuTestHostCPUID, host, host, \
                NULL, NULL, 0, 0); \
        DO_TEST(arch, cpuTestGuestCPUID, host, host, \
                NULL, NULL, json, 0); \
J
Jiri Denemark 已提交
1112 1113
        DO_TEST(arch, cpuTestCPUIDSignature, host, host, \
                NULL, NULL, 0, 0); \
1114 1115 1116 1117 1118
        DO_TEST_JSON(arch, host, json); \
        if (json != JSON_NONE) { \
            DO_TEST(arch, cpuTestUpdateLive, host, host, \
                    NULL, NULL, json, 0); \
        } \
J
Jiri Denemark 已提交
1119 1120
    } while (0)

1121
    /* host to host comparison */
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "host", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "host-better", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "host-worse", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "host-amd-fake", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "host-incomp-arch", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "host-no-vendor", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host-no-vendor", "host", VIR_CPU_COMPARE_INCOMPATIBLE);

    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "host", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "host-better", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "host-worse", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "host-incomp-arch", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "host-no-vendor", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host-no-vendor", "host", VIR_CPU_COMPARE_INCOMPATIBLE);
1136

1137
    /* guest to host comparison */
1138 1139 1140 1141 1142 1143 1144 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
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "bogus-model", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "bogus-feature", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "min", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "pentium3", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-forbid", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-forbid-extra", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-disable", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-disable2", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-disable-extra", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-require", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-require-extra", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "exact-force", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "strict", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "strict-full", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "strict-disable", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "strict-force-extra", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "guest", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host", "pentium3-amd", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host-amd", "pentium3-amd", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host-worse", "penryn-force", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_X86_64, "host-SandyBridge", "exact-force-Haswell", VIR_CPU_COMPARE_IDENTICAL);

    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-strict", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-exact", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-legacy", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-legacy-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-legacy-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-compat-none", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-compat-valid", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-compat-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_COMPARE(VIR_ARCH_PPC64, "host", "guest-compat-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
1170

1171 1172
    /* guest updates for migration
     * automatically compares host CPU with the result */
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    DO_TEST_UPDATE(VIR_ARCH_X86_64, "host", "min", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_X86_64, "host", "pentium3", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_X86_64, "host", "guest", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_UPDATE(VIR_ARCH_X86_64, "host", "host-model", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_X86_64, "host", "host-model-nofallback", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_X86_64, "host-invtsc", "host-model", VIR_CPU_COMPARE_SUPERSET);
    DO_TEST_UPDATE_ONLY(VIR_ARCH_X86_64, "host", "host-passthrough");
    DO_TEST_UPDATE_ONLY(VIR_ARCH_X86_64, "host", "host-passthrough-features");

    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-nofallback", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-legacy", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-legacy-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-legacy-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-compat-none", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-compat-valid", VIR_CPU_COMPARE_IDENTICAL);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-compat-invalid", VIR_CPU_COMPARE_ERROR);
    DO_TEST_UPDATE(VIR_ARCH_PPC64, "host", "guest-compat-incompatible", VIR_CPU_COMPARE_INCOMPATIBLE);
1191

1192
    /* computing baseline CPUs */
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "incompatible-vendors", 0, -1);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "no-vendor", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "some-vendors", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "1", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "2", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "3", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "3", VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "4", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "4", VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "5", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "5", VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "6", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "6", VIR_CONNECT_BASELINE_CPU_MIGRATABLE, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "7", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_X86_64, "8", 0, 0);

    DO_TEST_BASELINE(VIR_ARCH_PPC64, "incompatible-vendors", 0, -1);
    DO_TEST_BASELINE(VIR_ARCH_PPC64, "no-vendor", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_PPC64, "incompatible-models", 0, -1);
    DO_TEST_BASELINE(VIR_ARCH_PPC64, "same-model", 0, 0);
    DO_TEST_BASELINE(VIR_ARCH_PPC64, "legacy", 0, -1);
1214

1215
    /* CPU features */
1216 1217 1218 1219 1220 1221
    DO_TEST_HASFEATURE(VIR_ARCH_X86_64, "host", "vmx", YES);
    DO_TEST_HASFEATURE(VIR_ARCH_X86_64, "host", "lm", YES);
    DO_TEST_HASFEATURE(VIR_ARCH_X86_64, "host", "sse4.1", YES);
    DO_TEST_HASFEATURE(VIR_ARCH_X86_64, "host", "3dnowext", NO);
    DO_TEST_HASFEATURE(VIR_ARCH_X86_64, "host", "skinit", NO);
    DO_TEST_HASFEATURE(VIR_ARCH_X86_64, "host", "foo", FAIL);
1222 1223

    /* computing guest data and decoding the data into a guest CPU XML */
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "guest", NULL, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host-better", "pentium3", NULL, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host-worse", "guest", NULL, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "strict-force-extra", NULL, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "penryn-force", NULL, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "guest", model486, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "guest", models, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "guest", nomodel, -1);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "guest-nofallback", models, -1);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "host+host-model", models, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host", "host+host-model-nofallback", models, -1);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host-Haswell-noTSX", "Haswell", haswell, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host-Haswell-noTSX", "Haswell-noTSX", haswell, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host-Haswell-noTSX", "Haswell-noTSX-nofallback", haswell, -1);
    DO_TEST_GUESTCPU(VIR_ARCH_X86_64, "host-Haswell-noTSX", "Haswell-noTSX", NULL, 0);

    DO_TEST_GUESTCPU(VIR_ARCH_PPC64, "host", "guest", ppc_models, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_PPC64, "host", "guest-nofallback", ppc_models, -1);
    DO_TEST_GUESTCPU(VIR_ARCH_PPC64, "host", "guest-legacy", ppc_models, 0);
    DO_TEST_GUESTCPU(VIR_ARCH_PPC64, "host", "guest-legacy-incompatible", ppc_models, -1);
    DO_TEST_GUESTCPU(VIR_ARCH_PPC64, "host", "guest-legacy-invalid", ppc_models, -1);

1246 1247 1248
    DO_TEST_CPUID(VIR_ARCH_X86_64, "A10-5800K", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Atom-D510", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Atom-N450", JSON_NONE);
1249
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i5-650", JSON_MODELS_REQUIRED);
1250
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i5-2500", JSON_HOST);
1251
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i5-2540M", JSON_MODELS);
1252 1253 1254
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i5-4670T", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i5-6600", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-2600", JSON_HOST);
1255
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-2600-xsaveopt", JSON_MODELS_REQUIRED);
1256 1257 1258 1259 1260 1261 1262
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-3520M", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-3740QM", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-3770", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-4600U", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-4510U", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-5600U", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-5600U-arat", JSON_HOST);
1263
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-5600U-ibrs", JSON_HOST);
1264
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-7600U", JSON_MODELS);
1265
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-7700", JSON_MODELS);
1266
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core-i7-8700", JSON_MODELS);
1267 1268 1269
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core2-E6850", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Core2-Q9500", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "EPYC-7601-32-Core", JSON_HOST);
1270
    DO_TEST_CPUID(VIR_ARCH_X86_64, "EPYC-7601-32-Core-ibpb", JSON_MODELS_REQUIRED);
1271 1272 1273 1274 1275 1276 1277 1278 1279
    DO_TEST_CPUID(VIR_ARCH_X86_64, "FX-8150", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Opteron-1352", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Opteron-2350", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Opteron-6234", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Opteron-6282", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Pentium-P6100", JSON_NONE);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Phenom-B95", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Ryzen-7-1800X-Eight-Core", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-5110", JSON_NONE);
1280
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E3-1225-v5", JSON_MODELS);
1281
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E3-1245-v5", JSON_MODELS);
1282
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2609-v3", JSON_MODELS);
1283
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2623-v4", JSON_MODELS);
1284
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2630-v3", JSON_HOST);
1285
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2630-v4", JSON_MODELS);
1286
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2650", JSON_MODELS);
1287
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2650-v3", JSON_HOST);
1288
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E5-2650-v4", JSON_MODELS);
1289
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E7-4820", JSON_HOST);
1290
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E7-4830", JSON_MODELS_REQUIRED);
1291
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E7-8890-v3", JSON_MODELS);
1292
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-E7540", JSON_MODELS);
1293
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-Gold-5115", JSON_MODELS);
1294
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-Gold-6148", JSON_HOST);
1295
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-Platinum-8268", JSON_HOST);
1296 1297
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-W3520", JSON_HOST);
    DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-X5460", JSON_NONE);
1298

1299
 cleanup:
1300
#if WITH_QEMU && WITH_YAJL
J
Jiri Denemark 已提交
1301 1302 1303
    qemuTestDriverFree(&driver);
#endif

1304 1305 1306 1307 1308 1309
    virObjectUnref(model486);
    virObjectUnref(nomodel);
    virObjectUnref(models);
    virObjectUnref(haswell);
    virObjectUnref(ppc_models);

1310
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1311 1312
}

1313
VIR_TEST_MAIN(mymain)