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

#include <config.h>

#include <stdint.h>

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

#define VIR_FROM_THIS VIR_FROM_CPU

39 40
VIR_LOG_INIT("cpu.cpu_x86");

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

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

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

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

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

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

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

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

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

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

J
Jiri Denemark 已提交
115

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


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


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


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


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


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

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


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

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


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

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

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

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

    return 0;
}

213

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

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

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

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


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

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

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

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

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


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

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

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

    return cpuData;
}

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

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


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

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

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

    return copy;
}


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

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

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

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


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

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

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

    return 0;
}


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

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


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

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


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

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


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

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

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

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


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

417 418
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
J
Jiri Denemark 已提交
419 420 421 422
        if (x86DataIsSubset(data, feature->data)) {
            x86DataSubtract(data, feature->data);
            if (virCPUDefAddFeature(cpu, feature->name, policy) < 0)
                return -1;
423 424 425 426 427 428 429
        }
    }

    return 0;
}


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

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

    return NULL;
}


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

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

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

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

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

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

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

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


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

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


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

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

    return NULL;
}


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

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

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

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

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

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

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

    ret = 0;
J
Jiri Denemark 已提交
565

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


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

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

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

    return feature;
}


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

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


602
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
603
x86FeatureFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
604 605
               const char *name)
{
606
    size_t i;
J
Jiri Denemark 已提交
607

608 609 610
    for (i = 0; i < map->nfeatures; i++) {
        if (STREQ(map->features[i]->name, name))
            return map->features[i];
J
Jiri Denemark 已提交
611 612 613 614 615 616
    }

    return NULL;
}


617
static char *
J
Jiri Denemark 已提交
618
x86FeatureNames(virCPUx86MapPtr map,
619
                const char *separator,
620
                virCPUx86Data *data)
621 622 623
{
    virBuffer ret = VIR_BUFFER_INITIALIZER;
    bool first = true;
624
    size_t i;
625 626 627

    virBufferAdd(&ret, "", 0);

628 629 630
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
        if (x86DataIsSubset(data, feature->data)) {
631 632 633 634 635
            if (!first)
                virBufferAdd(&ret, separator, -1);
            else
                first = false;

636
            virBufferAdd(&ret, feature->name, -1);
637 638 639 640 641 642 643
        }
    }

    return virBufferContentAndReset(&ret);
}


644 645
static int
x86ParseCPUID(xmlXPathContextPtr ctxt,
646
              virCPUx86CPUID *cpuid)
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
{
    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 已提交
673 674
static int
x86FeatureLoad(xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
675
               virCPUx86MapPtr map)
J
Jiri Denemark 已提交
676 677
{
    xmlNodePtr *nodes = NULL;
678
    xmlNodePtr ctxt_node = ctxt->node;
679
    virCPUx86FeaturePtr feature;
680
    virCPUx86CPUID cpuid;
681
    int ret = -1;
682
    size_t i;
J
Jiri Denemark 已提交
683
    int n;
684 685
    char *str = NULL;
    bool migratable = true;
J
Jiri Denemark 已提交
686

J
Jiri Denemark 已提交
687
    if (!(feature = x86FeatureNew()))
688
        goto cleanup;
J
Jiri Denemark 已提交
689

690
    feature->name = virXPathString("string(@name)", ctxt);
691
    if (!feature->name) {
692 693
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU feature name"));
694
        goto cleanup;
J
Jiri Denemark 已提交
695 696 697
    }

    if (x86FeatureFind(map, feature->name)) {
698 699
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature %s already defined"), feature->name);
700
        goto cleanup;
J
Jiri Denemark 已提交
701 702
    }

703 704 705 706
    str = virXPathString("string(@migratable)", ctxt);
    if (STREQ_NULLABLE(str, "no"))
        migratable = false;

707
    n = virXPathNodeSet("./cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
708
    if (n < 0)
709
        goto cleanup;
J
Jiri Denemark 已提交
710 711 712

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
713
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
714
            virReportError(VIR_ERR_INTERNAL_ERROR,
715 716
                           _("Invalid cpuid[%zu] in %s feature"),
                           i, feature->name);
717
            goto cleanup;
J
Jiri Denemark 已提交
718
        }
719
        if (virCPUx86DataAddCPUID(feature->data, &cpuid))
720
            goto cleanup;
J
Jiri Denemark 已提交
721 722
    }

723 724 725 726
    if (!migratable &&
        VIR_APPEND_ELEMENT_COPY(map->migrate_blockers,
                                map->nblockers, feature) < 0)
        goto cleanup;
727

728 729
    if (VIR_APPEND_ELEMENT(map->features, map->nfeatures, feature) < 0)
        goto cleanup;
730 731

    ret = 0;
J
Jiri Denemark 已提交
732

J
Jiri Denemark 已提交
733
 cleanup:
734
    x86FeatureFree(feature);
735 736
    ctxt->node = ctxt_node;
    VIR_FREE(nodes);
737
    VIR_FREE(str);
738

J
Jiri Denemark 已提交
739 740 741 742
    return ret;
}


743 744
static virCPUx86Data *
x86DataFromCPUFeatures(virCPUDefPtr cpu,
J
Jiri Denemark 已提交
745
                       virCPUx86MapPtr map)
746 747 748 749 750 751 752 753
{
    virCPUx86Data *data;
    size_t i;

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

    for (i = 0; i < cpu->nfeatures; i++) {
754
        virCPUx86FeaturePtr feature;
755 756 757 758 759 760 761 762 763 764 765 766
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
            goto error;
        }

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

    return data;

767
 error:
768 769 770 771 772
    virCPUx86DataFree(data);
    return NULL;
}


J
Jiri Denemark 已提交
773
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
774 775
x86ModelNew(void)
{
J
Jiri Denemark 已提交
776
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789

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

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

    return model;
}


J
Jiri Denemark 已提交
790
static void
J
Jiri Denemark 已提交
791
x86ModelFree(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
792
{
793
    if (!model)
J
Jiri Denemark 已提交
794 795 796
        return;

    VIR_FREE(model->name);
797
    virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
798 799 800 801
    VIR_FREE(model);
}


J
Jiri Denemark 已提交
802 803
static virCPUx86ModelPtr
x86ModelCopy(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
804
{
J
Jiri Denemark 已提交
805
    virCPUx86ModelPtr copy;
J
Jiri Denemark 已提交
806

807 808 809
    if (VIR_ALLOC(copy) < 0 ||
        VIR_STRDUP(copy->name, model->name) < 0 ||
        !(copy->data = x86DataCopy(model->data))) {
J
Jiri Denemark 已提交
810 811 812 813
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
814
    copy->vendor = model->vendor;
J
Jiri Denemark 已提交
815 816 817 818 819

    return copy;
}


J
Jiri Denemark 已提交
820
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
821
x86ModelFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
822 823
             const char *name)
{
824
    size_t i;
J
Jiri Denemark 已提交
825

826 827 828
    for (i = 0; i < map->nmodels; i++) {
        if (STREQ(map->models[i]->name, name))
            return map->models[i];
J
Jiri Denemark 已提交
829 830 831 832 833 834
    }

    return NULL;
}


J
Jiri Denemark 已提交
835
static virCPUx86ModelPtr
836
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
837
                virCPUx86MapPtr map,
J
Jiri Denemark 已提交
838 839
                int policy)
{
J
Jiri Denemark 已提交
840
    virCPUx86ModelPtr model = NULL;
841
    size_t i;
J
Jiri Denemark 已提交
842

843
    if (policy == VIR_CPU_FEATURE_REQUIRE) {
844
        if (!(model = x86ModelFind(map, cpu->model))) {
845 846
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU model %s"), cpu->model);
J
Jiri Denemark 已提交
847 848 849
            goto error;
        }

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

    for (i = 0; i < cpu->nfeatures; i++) {
859
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
860 861 862 863 864

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

865
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
866 867
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
J
Jiri Denemark 已提交
868 869 870
            goto error;
        }

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

    return model;

877
 error:
J
Jiri Denemark 已提交
878 879 880 881 882
    x86ModelFree(model);
    return NULL;
}


883
static int
J
Jiri Denemark 已提交
884
x86ModelSubtractCPU(virCPUx86ModelPtr model,
885
                    const virCPUDef *cpu,
J
Jiri Denemark 已提交
886
                    virCPUx86MapPtr map)
887
{
J
Jiri Denemark 已提交
888
    virCPUx86ModelPtr cpu_model;
889
    size_t i;
890 891

    if (!(cpu_model = x86ModelFind(map, cpu->model))) {
892 893 894
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU model %s"),
                       cpu->model);
895 896 897
        return -1;
    }

J
Jiri Denemark 已提交
898
    x86DataSubtract(model->data, cpu_model->data);
899 900

    for (i = 0; i < cpu->nfeatures; i++) {
901
        virCPUx86FeaturePtr feature;
902 903

        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
904 905 906
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"),
                           cpu->features[i].name);
907 908 909
            return -1;
        }

J
Jiri Denemark 已提交
910
        x86DataSubtract(model->data, feature->data);
911 912 913 914 915 916
    }

    return 0;
}


917
static virCPUx86CompareResult
J
Jiri Denemark 已提交
918 919
x86ModelCompare(virCPUx86ModelPtr model1,
                virCPUx86ModelPtr model2)
J
Jiri Denemark 已提交
920
{
921
    virCPUx86CompareResult result = EQUAL;
922 923
    virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(model1->data);
    virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(model2->data);
924 925
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
926

J
Jiri Denemark 已提交
927
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
928
        virCPUx86CompareResult match = SUPERSET;
J
Jiri Denemark 已提交
929

J
Jiri Denemark 已提交
930
        if ((cpuid2 = x86DataCpuid(model2->data, cpuid1->function))) {
J
Jiri Denemark 已提交
931 932 933 934 935 936 937 938 939 940 941 942
            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 已提交
943
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
944
        virCPUx86CompareResult match = SUBSET;
J
Jiri Denemark 已提交
945

J
Jiri Denemark 已提交
946
        if ((cpuid1 = x86DataCpuid(model1->data, cpuid2->function))) {
J
Jiri Denemark 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
            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 已提交
965
             virCPUx86MapPtr map)
J
Jiri Denemark 已提交
966 967
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
968
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
969
    char *vendor = NULL;
970
    int ret = -1;
971
    size_t i;
J
Jiri Denemark 已提交
972 973
    int n;

J
Jiri Denemark 已提交
974
    if (!(model = x86ModelNew()))
975
        goto cleanup;
J
Jiri Denemark 已提交
976

977
    model->name = virXPathString("string(@name)", ctxt);
978
    if (!model->name) {
979 980
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
981
        goto cleanup;
J
Jiri Denemark 已提交
982 983
    }

984
    if (virXPathNode("./model", ctxt)) {
J
Jiri Denemark 已提交
985
        virCPUx86ModelPtr ancestor;
J
Jiri Denemark 已提交
986 987
        char *name;

988
        name = virXPathString("string(./model/@name)", ctxt);
989
        if (!name) {
990 991 992
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
993
            goto cleanup;
J
Jiri Denemark 已提交
994 995
        }

996
        if (!(ancestor = x86ModelFind(map, name))) {
997 998 999
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
1000
            VIR_FREE(name);
1001
            goto cleanup;
J
Jiri Denemark 已提交
1002 1003 1004 1005
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1006
        model->vendor = ancestor->vendor;
1007
        virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
1008
        if (!(model->data = x86DataCopy(ancestor->data)))
1009
            goto cleanup;
J
Jiri Denemark 已提交
1010 1011
    }

1012 1013 1014
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
1015 1016 1017
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
1018
            goto cleanup;
1019 1020
        }

J
Jiri Denemark 已提交
1021
        if (!(model->vendor = x86VendorFind(map, vendor))) {
1022 1023 1024
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
1025
            goto cleanup;
J
Jiri Denemark 已提交
1026 1027 1028
        }
    }

1029
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1030
    if (n < 0)
1031
        goto cleanup;
J
Jiri Denemark 已提交
1032 1033

    for (i = 0; i < n; i++) {
1034
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
1035 1036
        char *name;

1037
        if (!(name = virXMLPropString(nodes[i], "name"))) {
1038 1039
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
1040
            goto cleanup;
J
Jiri Denemark 已提交
1041 1042
        }

1043
        if (!(feature = x86FeatureFind(map, name))) {
1044 1045 1046
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1047
            VIR_FREE(name);
1048
            goto cleanup;
J
Jiri Denemark 已提交
1049 1050 1051
        }
        VIR_FREE(name);

J
Jiri Denemark 已提交
1052
        if (x86DataAdd(model->data, feature->data))
1053
            goto cleanup;
J
Jiri Denemark 已提交
1054 1055
    }

1056 1057
    if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
        goto cleanup;
1058 1059

    ret = 0;
J
Jiri Denemark 已提交
1060

J
Jiri Denemark 已提交
1061
 cleanup:
1062
    x86ModelFree(model);
J
Jiri Denemark 已提交
1063
    VIR_FREE(vendor);
1064
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1065 1066 1067 1068 1069
    return ret;
}


static void
J
Jiri Denemark 已提交
1070
x86MapFree(virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1071
{
1072 1073
    size_t i;

1074
    if (!map)
J
Jiri Denemark 已提交
1075 1076
        return;

1077 1078 1079
    for (i = 0; i < map->nfeatures; i++)
        x86FeatureFree(map->features[i]);
    VIR_FREE(map->features);
J
Jiri Denemark 已提交
1080

1081 1082 1083
    for (i = 0; i < map->nmodels; i++)
        x86ModelFree(map->models[i]);
    VIR_FREE(map->models);
J
Jiri Denemark 已提交
1084

1085 1086 1087
    for (i = 0; i < map->nvendors; i++)
        x86VendorFree(map->vendors[i]);
    VIR_FREE(map->vendors);
1088

1089 1090 1091 1092
    /* migrate_blockers only points to the features from map->features list,
     * which were already freed above
     */
    VIR_FREE(map->migrate_blockers);
1093

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


J
Jiri Denemark 已提交
1098
static int
1099
x86MapLoadCallback(cpuMapElement element,
J
Jiri Denemark 已提交
1100 1101 1102
                   xmlXPathContextPtr ctxt,
                   void *data)
{
J
Jiri Denemark 已提交
1103
    virCPUx86MapPtr map = data;
J
Jiri Denemark 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

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


1120
static int
J
Jiri Denemark 已提交
1121
x86MapLoadInternalFeatures(virCPUx86MapPtr map)
1122 1123
{
    size_t i;
1124
    virCPUx86FeaturePtr feature = NULL;
1125 1126 1127 1128 1129
    size_t nfeatures = map->nfeatures;
    size_t count = ARRAY_CARDINALITY(x86_kvm_features);

    if (VIR_EXPAND_N(map->features, nfeatures, count) < 0)
        goto error;
1130

1131
    for (i = 0; i < count; i++) {
1132 1133 1134 1135 1136
        const char *name = x86_kvm_features[i].name;

        if (x86FeatureFind(map, name)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("CPU feature %s already defined"), name);
1137
            goto error;
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
        }

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

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

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

1149
        map->features[map->nfeatures++] = feature;
1150 1151 1152 1153 1154
        feature = NULL;
    }

    return 0;

1155
 error:
1156 1157 1158 1159 1160
    x86FeatureFree(feature);
    return -1;
}


J
Jiri Denemark 已提交
1161
static virCPUx86MapPtr
1162
virCPUx86LoadMap(void)
J
Jiri Denemark 已提交
1163
{
J
Jiri Denemark 已提交
1164
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1165

1166
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1167 1168
        return NULL;

J
Jiri Denemark 已提交
1169
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1170 1171
        goto error;

1172 1173 1174
    if (x86MapLoadInternalFeatures(map) < 0)
        goto error;

J
Jiri Denemark 已提交
1175 1176
    return map;

1177
 error:
J
Jiri Denemark 已提交
1178 1179 1180 1181 1182
    x86MapFree(map);
    return NULL;
}


1183 1184 1185
int
virCPUx86MapOnceInit(void)
{
J
Jiri Denemark 已提交
1186
    if (!(cpuMap = virCPUx86LoadMap()))
1187 1188 1189 1190 1191 1192
        return -1;

    return 0;
}


J
Jiri Denemark 已提交
1193
static virCPUx86MapPtr
1194 1195 1196 1197 1198
virCPUx86GetMap(void)
{
    if (virCPUx86MapInitialize() < 0)
        return NULL;

J
Jiri Denemark 已提交
1199
    return cpuMap;
1200 1201 1202
}


1203 1204 1205
static char *
x86CPUDataFormat(const virCPUData *data)
{
1206
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data->data.x86);
1207
    virCPUx86CPUID *cpuid;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
    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");

1221
    if (virBufferCheckError(&buf) < 0)
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
x86CPUDataParse(const char *xmlStr)
{
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1235
    virCPUx86Data *data = NULL;
1236
    virCPUx86CPUID cpuid;
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    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;
        }
1264
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
1265 1266 1267
            goto cleanup;
    }

1268
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1269

1270
 cleanup:
1271 1272 1273
    VIR_FREE(nodes);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1274
    virCPUx86DataFree(data);
1275 1276 1277 1278
    return cpuData;
}


1279 1280 1281
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1282
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1283 1284 1285 1286 1287 1288 1289
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
#define virX86CpuIncompatible(MSG, CPU_DEF)                             \
        do {                                                            \
            char *flagsStr = NULL;                                      \
1290 1291 1292 1293
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) {  \
                virReportOOMError();                                    \
                goto error;                                             \
            }                                                           \
1294 1295 1296
            if (message &&                                              \
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
                VIR_FREE(flagsStr);                                     \
1297
                goto error;                                             \
1298 1299 1300 1301 1302 1303
            }                                                           \
            VIR_DEBUG("%s: %s", MSG, flagsStr);                         \
            VIR_FREE(flagsStr);                                         \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;                         \
        } while (0)

1304

J
Jiri Denemark 已提交
1305 1306 1307
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1308
           virCPUDataPtr *guest,
1309
           char **message)
J
Jiri Denemark 已提交
1310
{
J
Jiri Denemark 已提交
1311
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1312 1313 1314 1315 1316 1317 1318 1319
    virCPUx86ModelPtr host_model = NULL;
    virCPUx86ModelPtr cpu_force = NULL;
    virCPUx86ModelPtr cpu_require = NULL;
    virCPUx86ModelPtr cpu_optional = NULL;
    virCPUx86ModelPtr cpu_disable = NULL;
    virCPUx86ModelPtr cpu_forbid = NULL;
    virCPUx86ModelPtr diff = NULL;
    virCPUx86ModelPtr guest_model = NULL;
J
Jiri Denemark 已提交
1320
    virCPUCompareResult ret;
1321
    virCPUx86CompareResult result;
J
Jiri Denemark 已提交
1322
    virArch arch;
1323
    size_t i;
J
Jiri Denemark 已提交
1324

1325 1326 1327 1328 1329 1330
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

1331
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1332 1333 1334
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1335
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1336 1337 1338 1339 1340
                found = true;
                break;
            }
        }

1341
        if (!found) {
1342 1343
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1344 1345 1346
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1347
                            virArchToString(cpu->arch)) < 0)
1348
                goto error;
J
Jiri Denemark 已提交
1349
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1350
        }
J
Jiri Denemark 已提交
1351 1352 1353
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1354 1355
    }

J
Jiri Denemark 已提交
1356 1357 1358 1359
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1360 1361 1362 1363 1364
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1365
            goto error;
1366

J
Jiri Denemark 已提交
1367 1368 1369
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1370
    if (!(map = virCPUx86GetMap()) ||
1371
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1372 1373 1374 1375 1376
        !(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 已提交
1377 1378
        goto error;

1379 1380 1381 1382
    x86DataIntersect(cpu_forbid->data, host_model->data);
    if (!x86DataIsEmpty(cpu_forbid->data)) {
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
                              cpu_forbid->data);
1383
        goto cleanup;
J
Jiri Denemark 已提交
1384 1385
    }

1386 1387 1388 1389 1390
    /* 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 已提交
1391
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1392 1393
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1394 1395 1396 1397
        x86DataSubtract(cpu_require->data, host_model->data);
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
                              cpu_require->data);
1398
        goto cleanup;
J
Jiri Denemark 已提交
1399 1400 1401 1402
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

1403
    if (!(diff = x86ModelCopy(host_model)))
1404
        goto error;
J
Jiri Denemark 已提交
1405

J
Jiri Denemark 已提交
1406 1407 1408 1409
    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 已提交
1410

J
Jiri Denemark 已提交
1411 1412
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1413 1414 1415 1416

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1417 1418 1419
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
                              diff->data);
1420
        goto cleanup;
J
Jiri Denemark 已提交
1421 1422
    }

1423
    if (guest) {
1424
        virCPUx86Data *guestData;
1425

1426
        if (!(guest_model = x86ModelCopy(host_model)))
1427
            goto error;
J
Jiri Denemark 已提交
1428 1429 1430

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

J
Jiri Denemark 已提交
1433
        if (x86DataAdd(guest_model->data, cpu_force->data))
1434
            goto error;
J
Jiri Denemark 已提交
1435

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

1438
        if (!(guestData = x86DataCopy(guest_model->data)) ||
1439
            !(*guest = virCPUx86MakeData(arch, &guestData))) {
1440
            virCPUx86DataFree(guestData);
1441
            goto error;
1442
        }
J
Jiri Denemark 已提交
1443 1444
    }

1445
 cleanup:
J
Jiri Denemark 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    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;

1457
 error:
J
Jiri Denemark 已提交
1458
    ret = VIR_CPU_COMPARE_ERROR;
1459
    goto cleanup;
J
Jiri Denemark 已提交
1460
}
1461
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1462 1463 1464 1465


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
1466
           virCPUDefPtr cpu,
1467
           bool failIncompatible)
J
Jiri Denemark 已提交
1468
{
1469 1470 1471 1472 1473
    virCPUCompareResult ret;
    char *message = NULL;

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

1474
    if (failIncompatible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
        ret = VIR_CPU_COMPARE_ERROR;
        if (message) {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s", message);
        } else {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, NULL);
        }
    }
    VIR_FREE(message);

    return ret;
J
Jiri Denemark 已提交
1485 1486 1487 1488 1489 1490
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1491
             virCPUDataPtr *data,
1492
             char **message)
J
Jiri Denemark 已提交
1493
{
1494
    return x86Compute(host, guest, data, message);
J
Jiri Denemark 已提交
1495 1496
}

1497

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
/*
 * Checks whether cpuCandidate is a better fit for the CPU data than the
 * currently selected one from cpuCurrent.
 *
 * Returns 0 if cpuCurrent is better,
 *         1 if cpuCandidate is better,
 *         2 if cpuCandidate is the best one (search should stop now).
 */
static int
x86DecodeUseCandidate(virCPUDefPtr cpuCurrent,
                      virCPUDefPtr cpuCandidate,
                      const char *preferred,
                      bool checkPolicy)
{
    if (checkPolicy) {
        size_t i;
        for (i = 0; i < cpuCandidate->nfeatures; i++) {
            if (cpuCandidate->features[i].policy == VIR_CPU_FEATURE_DISABLE)
                return 0;
            cpuCandidate->features[i].policy = -1;
        }
    }

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

    if (!cpuCurrent)
        return 1;

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

    return 0;
}


J
Jiri Denemark 已提交
1535 1536
static int
x86Decode(virCPUDefPtr cpu,
1537
          const virCPUx86Data *data,
1538
          const char **models,
1539
          unsigned int nmodels,
1540 1541
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1542 1543
{
    int ret = -1;
J
Jiri Denemark 已提交
1544
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1545
    virCPUx86ModelPtr candidate;
1546 1547
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1548 1549 1550
    virCPUx86Data *copy = NULL;
    virCPUx86Data *features = NULL;
    const virCPUx86Data *cpuData = NULL;
J
Jiri Denemark 已提交
1551
    virCPUx86VendorPtr vendor;
1552
    ssize_t i;
1553
    int rc;
J
Jiri Denemark 已提交
1554

1555 1556
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, -1);
1557

1558
    if (!data || !(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1559 1560
        return -1;

J
Jiri Denemark 已提交
1561 1562
    vendor = x86DataToVendor(data, map);

1563 1564 1565 1566 1567
    /* Walk through the CPU models in reverse order to check newest
     * models first.
     */
    for (i = map->nmodels - 1; i >= 0; i--) {
        candidate = map->models[i];
1568
        if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
1569 1570
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1571 1572 1573
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
J
Jiri Denemark 已提交
1574
                    goto cleanup;
1575 1576 1577 1578 1579 1580 1581 1582 1583
                } 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);
            }
1584
            continue;
J
Jiri Denemark 已提交
1585 1586
        }

J
Jiri Denemark 已提交
1587 1588 1589 1590
        /* Both vendor and candidate->vendor are pointers to a single list of
         * known vendors stored in the map.
         */
        if (vendor && candidate->vendor && vendor != candidate->vendor) {
J
Jiri Denemark 已提交
1591
            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
J
Jiri Denemark 已提交
1592
                      candidate->vendor->name, candidate->name, vendor->name);
1593
            continue;
J
Jiri Denemark 已提交
1594 1595
        }

J
Jiri Denemark 已提交
1596 1597 1598 1599
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto cleanup;
        cpuCandidate->type = cpu->type;

1600 1601
        if ((rc = x86DecodeUseCandidate(cpuModel, cpuCandidate, preferred,
                                        cpu->type == VIR_CPU_TYPE_HOST))) {
1602 1603
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1604
            cpuData = candidate->data;
1605 1606
            if (rc == 2)
                break;
1607
        } else {
1608
            virCPUDefFree(cpuCandidate);
1609
        }
J
Jiri Denemark 已提交
1610 1611
    }

1612
    if (!cpuModel) {
1613 1614
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1615
        goto cleanup;
J
Jiri Denemark 已提交
1616 1617
    }

1618 1619 1620 1621 1622
    /* Remove non-migratable features if requested
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
    if (flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE) {
        for (i = 0; i < cpuModel->nfeatures; i++) {
1623 1624 1625 1626
            size_t j;
            for (j = 0; j < map->nblockers; j++) {
                if (STREQ(map->migrate_blockers[j]->name,
                          cpuModel->features[i].name)) {
1627
                    VIR_FREE(cpuModel->features[i].name);
1628 1629
                    VIR_DELETE_ELEMENT_INPLACE(cpuModel->features, i,
                                               cpuModel->nfeatures);
1630 1631 1632 1633 1634
                }
            }
        }
    }

1635 1636 1637
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) {
        if (!(copy = x86DataCopy(cpuData)) ||
            !(features = x86DataFromCPUFeatures(cpuModel, map)))
J
Jiri Denemark 已提交
1638
            goto cleanup;
1639 1640 1641 1642

        x86DataSubtract(copy, features);
        if (x86DataToCPUFeatures(cpuModel, VIR_CPU_FEATURE_REQUIRE,
                                 copy, map) < 0)
J
Jiri Denemark 已提交
1643
            goto cleanup;
1644 1645
    }

J
Jiri Denemark 已提交
1646 1647 1648
    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
        goto cleanup;

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

    ret = 0;

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

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

1674

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

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

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

    return data;
}


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

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

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

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

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

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

1793
    return 0;
J
Jiri Denemark 已提交
1794

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


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


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

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

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

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


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

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

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

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

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

    return cpuData;
J
Jiri Denemark 已提交
1893

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

    return NULL;
}
#endif


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

1919 1920 1921
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, NULL);

1922
    if (!(map = virCPUx86GetMap()))
1923 1924
        goto error;

1925
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1926 1927
        goto error;

1928
    if (VIR_ALLOC(cpu) < 0)
1929
        goto error;
1930 1931

    cpu->arch = cpus[0]->arch;
1932 1933
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1934

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

1943
    modelName = cpus[0]->model;
1944
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
1945 1946
        const char *vn = NULL;

1947 1948 1949 1950 1951 1952 1953 1954 1955
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

1956
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
1957 1958
            goto error;

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

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

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

J
Jiri Denemark 已提交
1989
        x86DataIntersect(base_model->data, model->data);
1990
        x86ModelFree(model);
J
Jiri Denemark 已提交
1991
        model = NULL;
1992 1993
    }

J
Jiri Denemark 已提交
1994
    if (x86DataIsEmpty(base_model->data)) {
1995 1996
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
1997 1998 1999
        goto error;
    }

2000
    if (vendor && virCPUx86DataAddCPUID(base_model->data, &vendor->cpuid) < 0)
2001
        goto error;
J
Jiri Denemark 已提交
2002

2003
    if (x86Decode(cpu, base_model->data, models, nmodels, modelName, flags) < 0)
2004 2005
        goto error;

2006 2007 2008
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2009 2010 2011
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2012 2013
    cpu->arch = VIR_ARCH_NONE;

2014
 cleanup:
2015 2016 2017 2018
    x86ModelFree(base_model);

    return cpu;

2019
 error:
J
Jiri Denemark 已提交
2020
    x86ModelFree(model);
2021 2022 2023 2024 2025 2026
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


2027
static int
2028
x86UpdateCustom(virCPUDefPtr guest,
2029
                const virCPUDef *host)
2030 2031
{
    int ret = -1;
2032
    size_t i;
J
Jiri Denemark 已提交
2033
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
2034
    virCPUx86ModelPtr host_model = NULL;
2035

2036
    if (!(map = virCPUx86GetMap()) ||
2037
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
2038 2039 2040 2041
        goto cleanup;

    for (i = 0; i < guest->nfeatures; i++) {
        if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
2042
            virCPUx86FeaturePtr feature;
2043
            if (!(feature = x86FeatureFind(map, guest->features[i].name))) {
2044 2045 2046
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unknown CPU feature %s"),
                               guest->features[i].name);
2047 2048 2049
                goto cleanup;
            }

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

    ret = 0;

2067
 cleanup:
2068 2069 2070 2071
    x86ModelFree(host_model);
    return ret;
}

2072 2073 2074

static int
x86UpdateHostModel(virCPUDefPtr guest,
2075 2076
                   const virCPUDef *host,
                   bool passthrough)
2077
{
2078
    virCPUDefPtr oldguest = NULL;
J
Jiri Denemark 已提交
2079
    virCPUx86MapPtr map;
2080
    size_t i;
2081
    int ret = -1;
2082

2083 2084
    if (!(map = virCPUx86GetMap()))
        goto cleanup;
2085 2086 2087

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
2088
        goto cleanup;
2089 2090 2091

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
2092
        goto cleanup;
2093

2094 2095 2096 2097 2098 2099
    if (oldguest->vendor_id) {
        VIR_FREE(guest->vendor_id);
        if (VIR_STRDUP(guest->vendor_id, oldguest->vendor_id) < 0)
            goto cleanup;
    }

2100 2101 2102 2103
    /* Remove non-migratable features by default
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
    for (i = 0; i < guest->nfeatures; i++) {
2104 2105 2106
        size_t j;
        for (j = 0; j < map->nblockers; j++) {
            if (STREQ(map->migrate_blockers[j]->name, guest->features[i].name)) {
J
Ján Tomko 已提交
2107
                VIR_FREE(guest->features[i].name);
2108
                VIR_DELETE_ELEMENT_INPLACE(guest->features, i, guest->nfeatures);
J
Ján Tomko 已提交
2109
            }
2110 2111
        }
    }
2112
    for (i = 0; !passthrough && i < oldguest->nfeatures; i++) {
2113 2114 2115
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
2116
            goto cleanup;
2117 2118
    }

2119 2120 2121 2122 2123
    ret = 0;

 cleanup:
    virCPUDefFree(oldguest);
    return ret;
2124 2125 2126
}


2127 2128
static int
x86Update(virCPUDefPtr guest,
2129
          const virCPUDef *host)
2130
{
2131
    switch ((virCPUMode) guest->mode) {
2132 2133 2134 2135
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2136 2137
        guest->match = VIR_CPU_MATCH_EXACT;
        return x86UpdateHostModel(guest, host, false);
2138

2139
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2140
        guest->match = VIR_CPU_MATCH_MINIMUM;
2141
        return x86UpdateHostModel(guest, host, true);
2142 2143 2144 2145 2146

    case VIR_CPU_MODE_LAST:
        break;
    }

2147 2148
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2149 2150 2151
    return -1;
}

2152 2153 2154 2155

static int
x86HasFeature(const virCPUData *data,
              const char *name)
D
Daniel P. Berrange 已提交
2156
{
J
Jiri Denemark 已提交
2157
    virCPUx86MapPtr map;
2158
    virCPUx86FeaturePtr feature;
D
Daniel P. Berrange 已提交
2159 2160
    int ret = -1;

2161
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2162 2163 2164 2165 2166
        return -1;

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

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

2169
 cleanup:
D
Daniel P. Berrange 已提交
2170 2171
    return ret;
}
2172

2173 2174 2175
static int
x86GetModels(char ***models)
{
J
Jiri Denemark 已提交
2176
    virCPUx86MapPtr map;
2177
    size_t i;
2178 2179 2180 2181

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

2182 2183 2184
    if (models) {
        if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
            goto error;
2185

2186 2187
        for (i = 0; i < map->nmodels; i++) {
            if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
2188 2189
                goto error;
        }
2190 2191
    }

2192
    return map->nmodels;
2193 2194

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

2202

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