cpu_x86.c 68.2 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 };
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
    bool migratable;
J
Jiri Denemark 已提交
60 61
};

62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#define KVM_FEATURE_DEF(Name, Eax_in, Eax)                          \
    static virCPUx86CPUID Name ## _cpuid[] = {                      \
        { .eax_in = Eax_in, .eax = Eax },                           \
    }

#define KVM_FEATURE(Name)                                           \
    {                                                               \
        .name = (char *) Name,                                      \
        .data = {                                                   \
            .len = ARRAY_CARDINALITY(Name ## _cpuid),               \
            .data = Name ## _cpuid                                  \
        }                                                           \
    }

KVM_FEATURE_DEF(VIR_CPU_x86_KVM_CLOCKSOURCE,
                0x40000001, 0x00000001);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_NOP_IO_DELAY,
                0x40000001, 0x00000002);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_MMU_OP,
                0x40000001, 0x00000004);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_CLOCKSOURCE2,
                0x40000001, 0x00000008);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_ASYNC_PF,
                0x40000001, 0x00000010);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_STEAL_TIME,
                0x40000001, 0x00000020);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_PV_EOI,
                0x40000001, 0x00000040);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_PV_UNHALT,
                0x40000001, 0x00000080);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_CLOCKSOURCE_STABLE_BIT,
                0x40000001, 0x01000000);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_RUNTIME,
                0x40000003, 0x00000001);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_SYNIC,
                0x40000003, 0x00000004);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_STIMER,
                0x40000003, 0x00000008);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_RELAXED,
                0x40000003, 0x00000020);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_SPINLOCK,
                0x40000003, 0x00000022);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_VAPIC,
                0x40000003, 0x00000030);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_VPINDEX,
                0x40000003, 0x00000040);
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_RESET,
                0x40000003, 0x00000080);

static virCPUx86Feature x86_kvm_features[] =
{
    KVM_FEATURE(VIR_CPU_x86_KVM_CLOCKSOURCE),
    KVM_FEATURE(VIR_CPU_x86_KVM_NOP_IO_DELAY),
    KVM_FEATURE(VIR_CPU_x86_KVM_MMU_OP),
    KVM_FEATURE(VIR_CPU_x86_KVM_CLOCKSOURCE2),
    KVM_FEATURE(VIR_CPU_x86_KVM_ASYNC_PF),
    KVM_FEATURE(VIR_CPU_x86_KVM_STEAL_TIME),
    KVM_FEATURE(VIR_CPU_x86_KVM_PV_EOI),
    KVM_FEATURE(VIR_CPU_x86_KVM_PV_UNHALT),
    KVM_FEATURE(VIR_CPU_x86_KVM_CLOCKSOURCE_STABLE_BIT),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_RUNTIME),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_SYNIC),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_STIMER),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_RELAXED),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_SPINLOCK),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_VAPIC),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_VPINDEX),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_RESET),
131 132
};

J
Jiri Denemark 已提交
133 134 135
typedef struct _virCPUx86Model virCPUx86Model;
typedef virCPUx86Model *virCPUx86ModelPtr;
struct _virCPUx86Model {
J
Jiri Denemark 已提交
136
    char *name;
137
    virCPUx86VendorPtr vendor;
138
    uint32_t signature;
139
    virCPUx86Data data;
J
Jiri Denemark 已提交
140 141
};

J
Jiri Denemark 已提交
142 143 144
typedef struct _virCPUx86Map virCPUx86Map;
typedef virCPUx86Map *virCPUx86MapPtr;
struct _virCPUx86Map {
145 146
    size_t nvendors;
    virCPUx86VendorPtr *vendors;
147 148
    size_t nfeatures;
    virCPUx86FeaturePtr *features;
149 150
    size_t nmodels;
    virCPUx86ModelPtr *models;
151 152
    size_t nblockers;
    virCPUx86FeaturePtr *migrate_blockers;
J
Jiri Denemark 已提交
153 154
};

J
Jiri Denemark 已提交
155
static virCPUx86MapPtr cpuMap;
156 157 158
int virCPUx86MapOnceInit(void);
VIR_ONCE_GLOBAL_INIT(virCPUx86Map);

J
Jiri Denemark 已提交
159

160
typedef enum {
J
Jiri Denemark 已提交
161 162 163 164
    SUBSET,
    EQUAL,
    SUPERSET,
    UNRELATED
165
} virCPUx86CompareResult;
J
Jiri Denemark 已提交
166 167


168 169 170
typedef struct _virCPUx86DataIterator virCPUx86DataIterator;
typedef virCPUx86DataIterator *virCPUx86DataIteratorPtr;
struct _virCPUx86DataIterator {
171
    const virCPUx86Data *data;
J
Jiri Denemark 已提交
172 173
    int pos;
};
J
Jiri Denemark 已提交
174 175


176
#define virCPUx86DataIteratorInit(data) \
177
    { data, -1 }
J
Jiri Denemark 已提交
178 179


180
static bool
181 182
x86cpuidMatch(const virCPUx86CPUID *cpuid1,
              const virCPUx86CPUID *cpuid2)
J
Jiri Denemark 已提交
183 184 185 186 187 188 189 190
{
    return (cpuid1->eax == cpuid2->eax &&
            cpuid1->ebx == cpuid2->ebx &&
            cpuid1->ecx == cpuid2->ecx &&
            cpuid1->edx == cpuid2->edx);
}


191
static bool
192 193
x86cpuidMatchMasked(const virCPUx86CPUID *cpuid,
                    const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
194 195 196 197 198 199 200 201
{
    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 已提交
202
static void
203 204
x86cpuidSetBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
205
{
206 207 208
    if (!mask)
        return;

J
Jiri Denemark 已提交
209 210 211 212 213 214 215
    cpuid->eax |= mask->eax;
    cpuid->ebx |= mask->ebx;
    cpuid->ecx |= mask->ecx;
    cpuid->edx |= mask->edx;
}


J
Jiri Denemark 已提交
216
static void
217 218
x86cpuidClearBits(virCPUx86CPUID *cpuid,
                  const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
219
{
220 221 222
    if (!mask)
        return;

J
Jiri Denemark 已提交
223 224 225 226 227 228 229
    cpuid->eax &= ~mask->eax;
    cpuid->ebx &= ~mask->ebx;
    cpuid->ecx &= ~mask->ecx;
    cpuid->edx &= ~mask->edx;
}


J
Jiri Denemark 已提交
230
static void
231 232
x86cpuidAndBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
233
{
234 235 236
    if (!mask)
        return;

237 238 239 240 241 242
    cpuid->eax &= mask->eax;
    cpuid->ebx &= mask->ebx;
    cpuid->ecx &= mask->ecx;
    cpuid->edx &= mask->edx;
}

243 244 245 246 247 248
static int
virCPUx86CPUIDSorter(const void *a, const void *b)
{
    virCPUx86CPUID *da = (virCPUx86CPUID *) a;
    virCPUx86CPUID *db = (virCPUx86CPUID *) b;

249
    if (da->eax_in > db->eax_in)
250
        return 1;
251
    else if (da->eax_in < db->eax_in)
252 253
        return -1;

254 255 256 257 258
    if (da->ecx_in > db->ecx_in)
        return 1;
    else if (da->ecx_in < db->ecx_in)
        return -1;

259 260 261
    return 0;
}

262

263
/* skips all zero CPUID leaves */
264
static virCPUx86CPUID *
265
x86DataCpuidNext(virCPUx86DataIteratorPtr iterator)
J
Jiri Denemark 已提交
266
{
267
    const virCPUx86Data *data = iterator->data;
J
Jiri Denemark 已提交
268

269
    if (!data)
J
Jiri Denemark 已提交
270 271
        return NULL;

272 273 274 275
    while (++iterator->pos < data->len) {
        if (!x86cpuidMatch(data->data + iterator->pos, &cpuidNull))
            return data->data + iterator->pos;
    }
J
Jiri Denemark 已提交
276

277
    return NULL;
J
Jiri Denemark 已提交
278 279 280
}


281
static virCPUx86CPUID *
282
x86DataCpuid(const virCPUx86Data *data,
283
             const virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
284
{
285
    size_t i;
J
Jiri Denemark 已提交
286

287
    for (i = 0; i < data->len; i++) {
288 289
        if (data->data[i].eax_in == cpuid->eax_in &&
            data->data[i].ecx_in == cpuid->ecx_in)
290
            return data->data + i;
J
Jiri Denemark 已提交
291 292
    }

293
    return NULL;
J
Jiri Denemark 已提交
294 295
}

J
Jiri Denemark 已提交
296
void
297
virCPUx86DataClear(virCPUx86Data *data)
J
Jiri Denemark 已提交
298
{
299
    if (!data)
J
Jiri Denemark 已提交
300 301
        return;

302
    VIR_FREE(data->data);
J
Jiri Denemark 已提交
303 304 305
}


J
Jiri Denemark 已提交
306
virCPUDataPtr
307
virCPUx86MakeData(virArch arch, virCPUx86Data *data)
308
{
309
    virCPUDataPtr cpuData;
310 311 312 313

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

J
Jiri Denemark 已提交
314 315
    cpuData->arch = arch;
    cpuData->data.x86 = *data;
316 317
    data->len = 0;
    data->data = NULL;
318 319 320 321 322

    return cpuData;
}

static void
323
x86FreeCPUData(virCPUDataPtr data)
324 325 326 327
{
    if (!data)
        return;

328
    virCPUx86DataClear(&data->data.x86);
329 330 331 332
    VIR_FREE(data);
}


333 334
static int
x86DataCopy(virCPUx86Data *dst, const virCPUx86Data *src)
J
Jiri Denemark 已提交
335
{
336
    size_t i;
J
Jiri Denemark 已提交
337

338 339
    if (VIR_ALLOC_N(dst->data, src->len) < 0)
        return -1;
J
Jiri Denemark 已提交
340

341 342 343
    dst->len = src->len;
    for (i = 0; i < src->len; i++)
        dst->data[i] = src->data[i];
J
Jiri Denemark 已提交
344

345
    return 0;
J
Jiri Denemark 已提交
346 347 348
}


J
Jiri Denemark 已提交
349
int
350 351
virCPUx86DataAddCPUID(virCPUx86Data *data,
                      const virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
352
{
353
    virCPUx86CPUID *existing;
J
Jiri Denemark 已提交
354

355
    if ((existing = x86DataCpuid(data, cpuid))) {
356 357 358 359 360
        x86cpuidSetBits(existing, cpuid);
    } else {
        if (VIR_APPEND_ELEMENT_COPY(data->data, data->len,
                                    *((virCPUx86CPUID *)cpuid)) < 0)
            return -1;
J
Jiri Denemark 已提交
361

362 363 364
        qsort(data->data, data->len,
              sizeof(virCPUx86CPUID), virCPUx86CPUIDSorter);
    }
J
Jiri Denemark 已提交
365

J
Jiri Denemark 已提交
366 367 368 369 370
    return 0;
}


static int
371 372
x86DataAdd(virCPUx86Data *data1,
           const virCPUx86Data *data2)
J
Jiri Denemark 已提交
373
{
374
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data2);
375 376
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
377

378
    while ((cpuid2 = x86DataCpuidNext(&iter))) {
379
        cpuid1 = x86DataCpuid(data1, cpuid2);
J
Jiri Denemark 已提交
380

381 382 383 384 385 386
        if (cpuid1) {
            x86cpuidSetBits(cpuid1, cpuid2);
        } else {
            if (virCPUx86DataAddCPUID(data1, cpuid2) < 0)
                return -1;
        }
J
Jiri Denemark 已提交
387
    }
J
Jiri Denemark 已提交
388 389 390 391 392

    return 0;
}


393
static void
394 395
x86DataSubtract(virCPUx86Data *data1,
                const virCPUx86Data *data2)
396
{
397
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
398 399
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
400

401
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
402
        cpuid2 = x86DataCpuid(data2, cpuid1);
403
        x86cpuidClearBits(cpuid1, cpuid2);
404 405 406 407
    }
}


J
Jiri Denemark 已提交
408
static void
409 410
x86DataIntersect(virCPUx86Data *data1,
                 const virCPUx86Data *data2)
411
{
412
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
413 414
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
415

J
Jiri Denemark 已提交
416
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
417
        cpuid2 = x86DataCpuid(data2, cpuid1);
J
Jiri Denemark 已提交
418 419 420 421
        if (cpuid2)
            x86cpuidAndBits(cpuid1, cpuid2);
        else
            x86cpuidClearBits(cpuid1, cpuid1);
422 423 424 425
    }
}


J
Jiri Denemark 已提交
426
static bool
427
x86DataIsEmpty(virCPUx86Data *data)
J
Jiri Denemark 已提交
428
{
429
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data);
J
Jiri Denemark 已提交
430

431
    return !x86DataCpuidNext(&iter);
J
Jiri Denemark 已提交
432
}
J
Jiri Denemark 已提交
433 434


J
Jiri Denemark 已提交
435
static bool
436 437
x86DataIsSubset(const virCPUx86Data *data,
                const virCPUx86Data *subset)
J
Jiri Denemark 已提交
438 439
{

440
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit((virCPUx86Data *)subset);
441 442
    const virCPUx86CPUID *cpuid;
    const virCPUx86CPUID *cpuidSubset;
J
Jiri Denemark 已提交
443

J
Jiri Denemark 已提交
444
    while ((cpuidSubset = x86DataCpuidNext(&iter))) {
445
        if (!(cpuid = x86DataCpuid(data, cpuidSubset)) ||
J
Jiri Denemark 已提交
446 447
            !x86cpuidMatchMasked(cpuid, cpuidSubset))
            return false;
J
Jiri Denemark 已提交
448 449
    }

J
Jiri Denemark 已提交
450
    return true;
J
Jiri Denemark 已提交
451 452 453
}


454 455 456 457
/* also removes all detected features from data */
static int
x86DataToCPUFeatures(virCPUDefPtr cpu,
                     int policy,
458
                     virCPUx86Data *data,
J
Jiri Denemark 已提交
459
                     virCPUx86MapPtr map)
460
{
461
    size_t i;
462

463 464
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
465 466
        if (x86DataIsSubset(data, &feature->data)) {
            x86DataSubtract(data, &feature->data);
J
Jiri Denemark 已提交
467 468
            if (virCPUDefAddFeature(cpu, feature->name, policy) < 0)
                return -1;
469 470 471 472 473 474 475
        }
    }

    return 0;
}


J
Jiri Denemark 已提交
476
/* also removes bits corresponding to vendor string from data */
477
static virCPUx86VendorPtr
J
Jiri Denemark 已提交
478
x86DataToVendor(const virCPUx86Data *data,
J
Jiri Denemark 已提交
479
                virCPUx86MapPtr map)
J
Jiri Denemark 已提交
480
{
481
    virCPUx86CPUID *cpuid;
482
    size_t i;
J
Jiri Denemark 已提交
483

484 485
    for (i = 0; i < map->nvendors; i++) {
        virCPUx86VendorPtr vendor = map->vendors[i];
486
        if ((cpuid = x86DataCpuid(data, &vendor->cpuid)) &&
J
Jiri Denemark 已提交
487 488 489 490 491 492 493 494 495 496
            x86cpuidMatchMasked(cpuid, &vendor->cpuid)) {
            x86cpuidClearBits(cpuid, &vendor->cpuid);
            return vendor;
        }
    }

    return NULL;
}


497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
static uint32_t
x86MakeSignature(unsigned int family,
                 unsigned int model)
{
    uint32_t sig = 0;

    /*
     * CPU signature (eax from 0x1 CPUID leaf):
     *
     * |31 .. 28|27 .. 20|19 .. 16|15 .. 14|13 .. 12|11 .. 8|7 .. 4|3 .. 0|
     * |   R    | extFam | extMod |   R    | PType  |  Fam  | Mod  | Step |
     *
     * R        reserved
     * extFam   extended family (valid only if Fam == 0xf)
     * extMod   extended model
     * PType    processor type
     * Fam      family
     * Mod      model
     * Step     stepping
     *
     * family = eax[27:20] + eax[11:8]
     * model = eax[19:16] << 4 + eax[7:4]
     */

    /* extFam */
    if (family > 0xf) {
        sig |= (family - 0xf) << 20;
        family = 0xf;
    }

    /* extMod */
    sig |= (model >> 4) << 16;

    /* PType is always 0 */

    /* Fam */
    sig |= family << 8;

    /* Mod */
    sig |= (model & 0xf) << 4;

    /* Step is irrelevant, it is used to distinguish different revisions
     * of the same CPU model
     */

    return sig;
}


/* Mask out irrelevant bits (R and Step) from processor signature. */
#define SIGNATURE_MASK  0x0fff3ff0

static uint32_t
x86DataToSignature(const virCPUx86Data *data)
{
    virCPUx86CPUID leaf1 = { .eax_in = 0x1 };
    virCPUx86CPUID *cpuid;

    if (!(cpuid = x86DataCpuid(data, &leaf1)))
        return 0;

    return cpuid->eax & SIGNATURE_MASK;
}


static int
x86DataAddSignature(virCPUx86Data *data,
                    uint32_t signature)
{
    virCPUx86CPUID cpuid = { .eax_in = 0x1, .eax = signature };

    return virCPUx86DataAddCPUID(data, &cpuid);
}


572
static virCPUDefPtr
573
x86DataToCPU(const virCPUx86Data *data,
J
Jiri Denemark 已提交
574
             virCPUx86ModelPtr model,
J
Jiri Denemark 已提交
575
             virCPUx86MapPtr map)
576 577
{
    virCPUDefPtr cpu;
578 579
    virCPUx86Data copy = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data modelData = VIR_CPU_X86_DATA_INIT;
580
    virCPUx86VendorPtr vendor;
581 582

    if (VIR_ALLOC(cpu) < 0 ||
583
        VIR_STRDUP(cpu->model, model->name) < 0 ||
584 585
        x86DataCopy(&copy, data) < 0 ||
        x86DataCopy(&modelData, &model->data) < 0)
586
        goto error;
587

588
    if ((vendor = x86DataToVendor(&copy, map)) &&
589
        VIR_STRDUP(cpu->vendor, vendor->name) < 0)
590
        goto error;
J
Jiri Denemark 已提交
591

592 593
    x86DataSubtract(&copy, &modelData);
    x86DataSubtract(&modelData, data);
594 595 596

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

598 599
    if (x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_REQUIRE, &copy, map) ||
        x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_DISABLE, &modelData, map))
600
        goto error;
601

602
 cleanup:
603 604
    virCPUx86DataClear(&modelData);
    virCPUx86DataClear(&copy);
605 606
    return cpu;

607
 error:
608 609 610 611 612 613
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


J
Jiri Denemark 已提交
614
static void
615
x86VendorFree(virCPUx86VendorPtr vendor)
J
Jiri Denemark 已提交
616 617 618 619 620 621
{
    if (!vendor)
        return;

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
J
Jiri Denemark 已提交
622
}
J
Jiri Denemark 已提交
623 624


625
static virCPUx86VendorPtr
J
Jiri Denemark 已提交
626
x86VendorFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
627 628
              const char *name)
{
629
    size_t i;
J
Jiri Denemark 已提交
630

631 632 633
    for (i = 0; i < map->nvendors; i++) {
        if (STREQ(map->vendors[i]->name, name))
            return map->vendors[i];
J
Jiri Denemark 已提交
634 635 636 637 638 639
    }

    return NULL;
}


J
Jiri Denemark 已提交
640 641 642
static virCPUx86VendorPtr
x86VendorParse(xmlXPathContextPtr ctxt,
               virCPUx86MapPtr map)
J
Jiri Denemark 已提交
643
{
644
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
645 646 647
    char *string = NULL;

    if (VIR_ALLOC(vendor) < 0)
J
Jiri Denemark 已提交
648
        goto error;
J
Jiri Denemark 已提交
649 650 651

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
652 653
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing CPU vendor name"));
J
Jiri Denemark 已提交
654
        goto error;
J
Jiri Denemark 已提交
655 656 657
    }

    if (x86VendorFind(map, vendor->name)) {
658 659
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU vendor %s already defined"), vendor->name);
J
Jiri Denemark 已提交
660
        goto error;
J
Jiri Denemark 已提交
661 662 663 664
    }

    string = virXPathString("string(@string)", ctxt);
    if (!string) {
665
        virReportError(VIR_ERR_INTERNAL_ERROR,
666 667
                       _("Missing vendor string for CPU vendor %s"),
                       vendor->name);
J
Jiri Denemark 已提交
668
        goto error;
J
Jiri Denemark 已提交
669 670
    }
    if (strlen(string) != VENDOR_STRING_LENGTH) {
671 672
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid CPU vendor string '%s'"), string);
J
Jiri Denemark 已提交
673
        goto error;
J
Jiri Denemark 已提交
674 675
    }

676
    vendor->cpuid.eax_in = 0;
677
    vendor->cpuid.ecx_in = 0;
E
Eric Blake 已提交
678 679 680 681
    vendor->cpuid.ebx = virReadBufInt32LE(string);
    vendor->cpuid.edx = virReadBufInt32LE(string + 4);
    vendor->cpuid.ecx = virReadBufInt32LE(string + 8);

J
Jiri Denemark 已提交
682
 cleanup:
J
Jiri Denemark 已提交
683
    VIR_FREE(string);
J
Jiri Denemark 已提交
684 685 686
    return vendor;

 error:
J
Jiri Denemark 已提交
687
    x86VendorFree(vendor);
J
Jiri Denemark 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    vendor = NULL;
    goto cleanup;
}


static int
x86VendorsLoad(virCPUx86MapPtr map,
               xmlXPathContextPtr ctxt,
               xmlNodePtr *nodes,
               int n)
{
    virCPUx86VendorPtr vendor;
    size_t i;

    if (VIR_ALLOC_N(map->vendors, n) < 0)
        return -1;

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
        if (!(vendor = x86VendorParse(ctxt, map)))
            return -1;
        map->vendors[map->nvendors++] = vendor;
    }

    return 0;
J
Jiri Denemark 已提交
713 714 715
}


716
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
717 718
x86FeatureNew(void)
{
719
    virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
720 721 722 723 724 725 726 727

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

    return feature;
}


J
Jiri Denemark 已提交
728
static void
729
x86FeatureFree(virCPUx86FeaturePtr feature)
J
Jiri Denemark 已提交
730
{
731
    if (!feature)
J
Jiri Denemark 已提交
732 733 734
        return;

    VIR_FREE(feature->name);
735
    virCPUx86DataClear(&feature->data);
J
Jiri Denemark 已提交
736 737 738 739
    VIR_FREE(feature);
}


740
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
741
x86FeatureFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
742 743
               const char *name)
{
744
    size_t i;
J
Jiri Denemark 已提交
745

746 747 748
    for (i = 0; i < map->nfeatures; i++) {
        if (STREQ(map->features[i]->name, name))
            return map->features[i];
J
Jiri Denemark 已提交
749 750 751 752 753 754
    }

    return NULL;
}


755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
static virCPUx86FeaturePtr
x86FeatureFindInternal(const char *name)
{
    size_t i;
    size_t count = ARRAY_CARDINALITY(x86_kvm_features);

    for (i = 0; i < count; i++) {
        if (STREQ(x86_kvm_features[i].name, name))
            return x86_kvm_features + i;
    }

    return NULL;
}


770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
static bool
x86FeatureIsMigratable(const char *name,
                       void *cpu_map)
{
    virCPUx86MapPtr map = cpu_map;
    size_t i;

    for (i = 0; i < map->nblockers; i++) {
        if (STREQ(name, map->migrate_blockers[i]->name))
            return false;
    }

    return true;
}


786
static char *
J
Jiri Denemark 已提交
787
x86FeatureNames(virCPUx86MapPtr map,
788
                const char *separator,
789
                virCPUx86Data *data)
790 791 792
{
    virBuffer ret = VIR_BUFFER_INITIALIZER;
    bool first = true;
793
    size_t i;
794 795 796

    virBufferAdd(&ret, "", 0);

797 798
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
799
        if (x86DataIsSubset(data, &feature->data)) {
800 801 802 803 804
            if (!first)
                virBufferAdd(&ret, separator, -1);
            else
                first = false;

805
            virBufferAdd(&ret, feature->name, -1);
806 807 808 809 810 811 812
        }
    }

    return virBufferContentAndReset(&ret);
}


813 814
static int
x86ParseCPUID(xmlXPathContextPtr ctxt,
815
              virCPUx86CPUID *cpuid)
816
{
817
    unsigned long eax_in, ecx_in;
818
    unsigned long eax, ebx, ecx, edx;
819
    int ret_eax_in, ret_ecx_in, ret_eax, ret_ebx, ret_ecx, ret_edx;
820 821 822

    memset(cpuid, 0, sizeof(*cpuid));

823
    eax_in = ecx_in = 0;
824 825
    eax = ebx = ecx = edx = 0;
    ret_eax_in = virXPathULongHex("string(@eax_in)", ctxt, &eax_in);
826
    ret_ecx_in = virXPathULongHex("string(@ecx_in)", ctxt, &ecx_in);
827 828 829 830 831
    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);

832
    if (ret_eax_in < 0 || ret_ecx_in == -2 ||
833
        ret_eax == -2 || ret_ebx == -2 || ret_ecx == -2 || ret_edx == -2)
834 835
        return -1;

836
    cpuid->eax_in = eax_in;
837
    cpuid->ecx_in = ecx_in;
838 839 840 841 842 843 844 845
    cpuid->eax = eax;
    cpuid->ebx = ebx;
    cpuid->ecx = ecx;
    cpuid->edx = edx;
    return 0;
}


J
Jiri Denemark 已提交
846 847 848
static virCPUx86FeaturePtr
x86FeatureParse(xmlXPathContextPtr ctxt,
                virCPUx86MapPtr map)
J
Jiri Denemark 已提交
849 850
{
    xmlNodePtr *nodes = NULL;
851
    virCPUx86FeaturePtr feature;
852
    virCPUx86CPUID cpuid;
853
    size_t i;
J
Jiri Denemark 已提交
854
    int n;
855
    char *str = NULL;
J
Jiri Denemark 已提交
856

J
Jiri Denemark 已提交
857
    if (!(feature = x86FeatureNew()))
J
Jiri Denemark 已提交
858
        goto error;
J
Jiri Denemark 已提交
859

J
Jiri Denemark 已提交
860
    feature->migratable = true;
861
    feature->name = virXPathString("string(@name)", ctxt);
862
    if (!feature->name) {
863 864
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU feature name"));
J
Jiri Denemark 已提交
865
        goto error;
J
Jiri Denemark 已提交
866 867 868
    }

    if (x86FeatureFind(map, feature->name)) {
869 870
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature %s already defined"), feature->name);
J
Jiri Denemark 已提交
871
        goto error;
J
Jiri Denemark 已提交
872 873
    }

874 875
    str = virXPathString("string(@migratable)", ctxt);
    if (STREQ_NULLABLE(str, "no"))
J
Jiri Denemark 已提交
876
        feature->migratable = false;
877

878
    n = virXPathNodeSet("./cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
879
    if (n < 0)
J
Jiri Denemark 已提交
880
        goto error;
J
Jiri Denemark 已提交
881 882 883

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
884
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
885
            virReportError(VIR_ERR_INTERNAL_ERROR,
886 887
                           _("Invalid cpuid[%zu] in %s feature"),
                           i, feature->name);
J
Jiri Denemark 已提交
888
            goto error;
J
Jiri Denemark 已提交
889
        }
890
        if (virCPUx86DataAddCPUID(&feature->data, &cpuid))
J
Jiri Denemark 已提交
891
            goto error;
J
Jiri Denemark 已提交
892 893
    }

J
Jiri Denemark 已提交
894
 cleanup:
895
    VIR_FREE(nodes);
896
    VIR_FREE(str);
J
Jiri Denemark 已提交
897
    return feature;
898

J
Jiri Denemark 已提交
899 900 901 902
 error:
    x86FeatureFree(feature);
    feature = NULL;
    goto cleanup;
J
Jiri Denemark 已提交
903 904 905
}


J
Jiri Denemark 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
static int
x86FeaturesLoad(virCPUx86MapPtr map,
                xmlXPathContextPtr ctxt,
                xmlNodePtr *nodes,
                int n)
{
    virCPUx86FeaturePtr feature;
    size_t i;

    if (VIR_ALLOC_N(map->features, n) < 0)
        return -1;

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
        if (!(feature = x86FeatureParse(ctxt, map)))
            return -1;
        map->features[map->nfeatures++] = feature;
        if (!feature->migratable &&
            VIR_APPEND_ELEMENT(map->migrate_blockers,
                               map->nblockers,
                               feature) < 0)
            return -1;
    }

    return 0;
}

933 934 935
static int
x86DataFromCPUFeatures(virCPUx86Data *data,
                       virCPUDefPtr cpu,
J
Jiri Denemark 已提交
936
                       virCPUx86MapPtr map)
937 938 939 940
{
    size_t i;

    for (i = 0; i < cpu->nfeatures; i++) {
941
        virCPUx86FeaturePtr feature;
942 943 944
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
945
            return -1;
946 947
        }

948 949
        if (x86DataAdd(data, &feature->data) < 0)
            return -1;
950 951
    }

952
    return 0;
953 954 955
}


J
Jiri Denemark 已提交
956
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
957 958
x86ModelNew(void)
{
J
Jiri Denemark 已提交
959
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
960 961 962 963 964 965 966 967

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

    return model;
}


J
Jiri Denemark 已提交
968
static void
J
Jiri Denemark 已提交
969
x86ModelFree(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
970
{
971
    if (!model)
J
Jiri Denemark 已提交
972 973 974
        return;

    VIR_FREE(model->name);
975
    virCPUx86DataClear(&model->data);
J
Jiri Denemark 已提交
976 977 978 979
    VIR_FREE(model);
}


J
Jiri Denemark 已提交
980 981
static virCPUx86ModelPtr
x86ModelCopy(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
982
{
J
Jiri Denemark 已提交
983
    virCPUx86ModelPtr copy;
J
Jiri Denemark 已提交
984

985 986
    if (VIR_ALLOC(copy) < 0 ||
        VIR_STRDUP(copy->name, model->name) < 0 ||
987
        x86DataCopy(&copy->data, &model->data) < 0) {
J
Jiri Denemark 已提交
988 989 990 991
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
992
    copy->vendor = model->vendor;
993
    copy->signature = model->signature;
J
Jiri Denemark 已提交
994 995 996 997 998

    return copy;
}


J
Jiri Denemark 已提交
999
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
1000
x86ModelFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
1001 1002
             const char *name)
{
1003
    size_t i;
J
Jiri Denemark 已提交
1004

1005 1006 1007
    for (i = 0; i < map->nmodels; i++) {
        if (STREQ(map->models[i]->name, name))
            return map->models[i];
J
Jiri Denemark 已提交
1008 1009 1010 1011 1012 1013
    }

    return NULL;
}


J
Jiri Denemark 已提交
1014
static virCPUx86ModelPtr
1015
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
1016
                virCPUx86MapPtr map,
J
Jiri Denemark 已提交
1017 1018
                int policy)
{
J
Jiri Denemark 已提交
1019
    virCPUx86ModelPtr model = NULL;
1020
    size_t i;
J
Jiri Denemark 已提交
1021

1022
    if (policy == VIR_CPU_FEATURE_REQUIRE) {
1023
        if (!(model = x86ModelFind(map, cpu->model))) {
1024 1025
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU model %s"), cpu->model);
J
Jiri Denemark 已提交
1026 1027 1028
            goto error;
        }

1029
        if (!(model = x86ModelCopy(model)))
1030
            goto error;
J
Jiri Denemark 已提交
1031
    } else if (!(model = x86ModelNew())) {
1032
        goto error;
J
Jiri Denemark 已提交
1033
    } else if (cpu->type == VIR_CPU_TYPE_HOST) {
1034
        return model;
J
Jiri Denemark 已提交
1035
    }
J
Jiri Denemark 已提交
1036 1037

    for (i = 0; i < cpu->nfeatures; i++) {
1038
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
1039 1040 1041 1042 1043

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

1044
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
1045 1046
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
J
Jiri Denemark 已提交
1047 1048 1049
            goto error;
        }

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

    return model;

1056
 error:
J
Jiri Denemark 已提交
1057 1058 1059 1060 1061
    x86ModelFree(model);
    return NULL;
}


1062
static int
J
Jiri Denemark 已提交
1063
x86ModelSubtractCPU(virCPUx86ModelPtr model,
1064
                    const virCPUDef *cpu,
J
Jiri Denemark 已提交
1065
                    virCPUx86MapPtr map)
1066
{
J
Jiri Denemark 已提交
1067
    virCPUx86ModelPtr cpu_model;
1068
    size_t i;
1069 1070

    if (!(cpu_model = x86ModelFind(map, cpu->model))) {
1071 1072 1073
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU model %s"),
                       cpu->model);
1074 1075 1076
        return -1;
    }

1077
    x86DataSubtract(&model->data, &cpu_model->data);
1078 1079

    for (i = 0; i < cpu->nfeatures; i++) {
1080
        virCPUx86FeaturePtr feature;
1081 1082

        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
1083 1084 1085
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"),
                           cpu->features[i].name);
1086 1087 1088
            return -1;
        }

1089
        x86DataSubtract(&model->data, &feature->data);
1090 1091 1092 1093 1094 1095
    }

    return 0;
}


1096
static virCPUx86CompareResult
J
Jiri Denemark 已提交
1097 1098
x86ModelCompare(virCPUx86ModelPtr model1,
                virCPUx86ModelPtr model2)
J
Jiri Denemark 已提交
1099
{
1100
    virCPUx86CompareResult result = EQUAL;
1101 1102
    virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(&model1->data);
    virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(&model2->data);
1103 1104
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
1105

J
Jiri Denemark 已提交
1106
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
1107
        virCPUx86CompareResult match = SUPERSET;
J
Jiri Denemark 已提交
1108

1109
        if ((cpuid2 = x86DataCpuid(&model2->data, cpuid1))) {
J
Jiri Denemark 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
            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 已提交
1122
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
1123
        virCPUx86CompareResult match = SUBSET;
J
Jiri Denemark 已提交
1124

1125
        if ((cpuid1 = x86DataCpuid(&model1->data, cpuid2))) {
J
Jiri Denemark 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
            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;
}


J
Jiri Denemark 已提交
1142 1143 1144
static virCPUx86ModelPtr
x86ModelParse(xmlXPathContextPtr ctxt,
              virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1145 1146
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
1147
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
1148
    char *vendor = NULL;
1149
    size_t i;
J
Jiri Denemark 已提交
1150 1151
    int n;

J
Jiri Denemark 已提交
1152
    if (!(model = x86ModelNew()))
J
Jiri Denemark 已提交
1153
        goto error;
J
Jiri Denemark 已提交
1154

1155
    model->name = virXPathString("string(@name)", ctxt);
1156
    if (!model->name) {
1157 1158
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
J
Jiri Denemark 已提交
1159
        goto error;
J
Jiri Denemark 已提交
1160 1161
    }

1162
    if (virXPathNode("./model", ctxt)) {
J
Jiri Denemark 已提交
1163
        virCPUx86ModelPtr ancestor;
J
Jiri Denemark 已提交
1164 1165
        char *name;

1166
        name = virXPathString("string(./model/@name)", ctxt);
1167
        if (!name) {
1168 1169 1170
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1171
            goto error;
J
Jiri Denemark 已提交
1172 1173
        }

1174
        if (!(ancestor = x86ModelFind(map, name))) {
1175 1176 1177
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
1178
            VIR_FREE(name);
J
Jiri Denemark 已提交
1179
            goto error;
J
Jiri Denemark 已提交
1180 1181 1182 1183
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1184
        model->vendor = ancestor->vendor;
1185
        if (x86DataCopy(&model->data, &ancestor->data) < 0)
J
Jiri Denemark 已提交
1186
            goto error;
J
Jiri Denemark 已提交
1187 1188
    }

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    if (virXPathBoolean("boolean(./signature)", ctxt)) {
        unsigned int sigFamily = 0;
        unsigned int sigModel = 0;
        int rc;

        rc = virXPathUInt("string(./signature/@family)", ctxt, &sigFamily);
        if (rc < 0 || sigFamily == 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid CPU signature family in model %s"),
                           model->name);
            goto cleanup;
        }

        rc = virXPathUInt("string(./signature/@model)", ctxt, &sigModel);
        if (rc < 0 || sigModel == 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid CPU signature model in model %s"),
                           model->name);
            goto cleanup;
        }

        model->signature = x86MakeSignature(sigFamily, sigModel);
    }

1213 1214 1215
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
1216 1217 1218
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1219
            goto error;
1220 1221
        }

J
Jiri Denemark 已提交
1222
        if (!(model->vendor = x86VendorFind(map, vendor))) {
1223 1224 1225
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
J
Jiri Denemark 已提交
1226
            goto error;
J
Jiri Denemark 已提交
1227 1228 1229
        }
    }

1230
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1231
    if (n < 0)
J
Jiri Denemark 已提交
1232
        goto error;
J
Jiri Denemark 已提交
1233 1234

    for (i = 0; i < n; i++) {
1235
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
1236 1237
        char *name;

1238
        if (!(name = virXMLPropString(nodes[i], "name"))) {
1239 1240
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
J
Jiri Denemark 已提交
1241
            goto error;
J
Jiri Denemark 已提交
1242 1243
        }

1244
        if (!(feature = x86FeatureFind(map, name))) {
1245 1246 1247
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1248
            VIR_FREE(name);
J
Jiri Denemark 已提交
1249
            goto error;
J
Jiri Denemark 已提交
1250 1251 1252
        }
        VIR_FREE(name);

1253
        if (x86DataAdd(&model->data, &feature->data))
J
Jiri Denemark 已提交
1254
            goto error;
J
Jiri Denemark 已提交
1255 1256
    }

J
Jiri Denemark 已提交
1257
 cleanup:
J
Jiri Denemark 已提交
1258
    VIR_FREE(vendor);
1259
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
    return model;

 error:
    x86ModelFree(model);
    model = NULL;
    goto cleanup;
}


static int
x86ModelsLoad(virCPUx86MapPtr map,
              xmlXPathContextPtr ctxt,
              xmlNodePtr *nodes,
              int n)
{
    virCPUx86ModelPtr model;
    size_t i;

    if (VIR_ALLOC_N(map->models, n) < 0)
        return -1;

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
        if (!(model = x86ModelParse(ctxt, map)))
            return -1;
        map->models[map->nmodels++] = model;
    }

    return 0;
J
Jiri Denemark 已提交
1289 1290 1291 1292
}


static void
J
Jiri Denemark 已提交
1293
x86MapFree(virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1294
{
1295 1296
    size_t i;

1297
    if (!map)
J
Jiri Denemark 已提交
1298 1299
        return;

1300 1301 1302
    for (i = 0; i < map->nfeatures; i++)
        x86FeatureFree(map->features[i]);
    VIR_FREE(map->features);
J
Jiri Denemark 已提交
1303

1304 1305 1306
    for (i = 0; i < map->nmodels; i++)
        x86ModelFree(map->models[i]);
    VIR_FREE(map->models);
J
Jiri Denemark 已提交
1307

1308 1309 1310
    for (i = 0; i < map->nvendors; i++)
        x86VendorFree(map->vendors[i]);
    VIR_FREE(map->vendors);
1311

1312 1313 1314 1315
    /* migrate_blockers only points to the features from map->features list,
     * which were already freed above
     */
    VIR_FREE(map->migrate_blockers);
1316

J
Jiri Denemark 已提交
1317 1318 1319 1320
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1321
static int
1322
x86MapLoadCallback(cpuMapElement element,
J
Jiri Denemark 已提交
1323
                   xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
1324 1325
                   xmlNodePtr *nodes,
                   int n,
J
Jiri Denemark 已提交
1326 1327
                   void *data)
{
J
Jiri Denemark 已提交
1328
    virCPUx86MapPtr map = data;
J
Jiri Denemark 已提交
1329 1330 1331

    switch (element) {
    case CPU_MAP_ELEMENT_VENDOR:
J
Jiri Denemark 已提交
1332
        return x86VendorsLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1333
    case CPU_MAP_ELEMENT_FEATURE:
J
Jiri Denemark 已提交
1334
        return x86FeaturesLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1335
    case CPU_MAP_ELEMENT_MODEL:
J
Jiri Denemark 已提交
1336
        return x86ModelsLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1337 1338 1339 1340 1341 1342 1343 1344
    case CPU_MAP_ELEMENT_LAST:
        break;
    }

    return 0;
}


J
Jiri Denemark 已提交
1345
static virCPUx86MapPtr
1346
virCPUx86LoadMap(void)
J
Jiri Denemark 已提交
1347
{
J
Jiri Denemark 已提交
1348
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1349

1350
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1351 1352
        return NULL;

J
Jiri Denemark 已提交
1353
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1354 1355 1356 1357
        goto error;

    return map;

1358
 error:
J
Jiri Denemark 已提交
1359 1360 1361 1362 1363
    x86MapFree(map);
    return NULL;
}


1364 1365 1366
int
virCPUx86MapOnceInit(void)
{
J
Jiri Denemark 已提交
1367
    if (!(cpuMap = virCPUx86LoadMap()))
1368 1369 1370 1371 1372 1373
        return -1;

    return 0;
}


J
Jiri Denemark 已提交
1374
static virCPUx86MapPtr
1375 1376 1377 1378 1379
virCPUx86GetMap(void)
{
    if (virCPUx86MapInitialize() < 0)
        return NULL;

J
Jiri Denemark 已提交
1380
    return cpuMap;
1381 1382 1383
}


1384 1385 1386
static char *
x86CPUDataFormat(const virCPUData *data)
{
1387
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(&data->data.x86);
1388
    virCPUx86CPUID *cpuid;
1389 1390 1391 1392 1393
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "<cpudata arch='x86'>\n");
    while ((cpuid = x86DataCpuidNext(&iter))) {
        virBufferAsprintf(&buf,
1394
                          "  <cpuid eax_in='0x%08x' ecx_in='0x%08x'"
1395 1396
                          " eax='0x%08x' ebx='0x%08x'"
                          " ecx='0x%08x' edx='0x%08x'/>\n",
1397
                          cpuid->eax_in, cpuid->ecx_in,
1398 1399 1400 1401
                          cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx);
    }
    virBufferAddLit(&buf, "</cpudata>\n");

1402
    if (virBufferCheckError(&buf) < 0)
1403 1404 1405 1406 1407 1408 1409
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
1410
x86CPUDataParse(xmlXPathContextPtr ctxt)
1411 1412 1413
{
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1414
    virCPUx86Data data = VIR_CPU_X86_DATA_INIT;
1415
    virCPUx86CPUID cpuid;
1416 1417 1418
    size_t i;
    int n;

1419
    n = virXPathNodeSet("/cpudata/cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
1420
    if (n <= 0) {
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
        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;
        }
1433
        if (virCPUx86DataAddCPUID(&data, &cpuid) < 0)
1434 1435 1436
            goto cleanup;
    }

1437
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1438

1439
 cleanup:
1440
    VIR_FREE(nodes);
1441
    virCPUx86DataClear(&data);
1442 1443 1444 1445
    return cpuData;
}


1446 1447 1448
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1449
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1450 1451 1452 1453 1454 1455 1456
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
#define virX86CpuIncompatible(MSG, CPU_DEF)                             \
        do {                                                            \
            char *flagsStr = NULL;                                      \
1457 1458 1459 1460
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) {  \
                virReportOOMError();                                    \
                goto error;                                             \
            }                                                           \
1461 1462 1463
            if (message &&                                              \
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
                VIR_FREE(flagsStr);                                     \
1464
                goto error;                                             \
1465 1466 1467 1468 1469 1470
            }                                                           \
            VIR_DEBUG("%s: %s", MSG, flagsStr);                         \
            VIR_FREE(flagsStr);                                         \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;                         \
        } while (0)

1471

J
Jiri Denemark 已提交
1472 1473 1474
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1475
           virCPUDataPtr *guest,
1476
           char **message)
J
Jiri Denemark 已提交
1477
{
J
Jiri Denemark 已提交
1478
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1479 1480 1481 1482 1483 1484 1485 1486
    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;
1487
    virCPUx86Data guestData = VIR_CPU_X86_DATA_INIT;
J
Jiri Denemark 已提交
1488
    virCPUCompareResult ret;
1489
    virCPUx86CompareResult result;
J
Jiri Denemark 已提交
1490
    virArch arch;
1491
    size_t i;
J
Jiri Denemark 已提交
1492

1493 1494 1495 1496 1497 1498
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

1499
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1500 1501 1502
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1503
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1504 1505 1506 1507 1508
                found = true;
                break;
            }
        }

1509
        if (!found) {
1510 1511
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1512 1513 1514
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1515
                            virArchToString(cpu->arch)) < 0)
1516
                goto error;
J
Jiri Denemark 已提交
1517
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1518
        }
J
Jiri Denemark 已提交
1519 1520 1521
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1522 1523
    }

J
Jiri Denemark 已提交
1524 1525 1526 1527
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1528 1529 1530 1531 1532
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1533
            goto error;
1534

J
Jiri Denemark 已提交
1535 1536 1537
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1538
    if (!(map = virCPUx86GetMap()) ||
1539
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1540 1541 1542 1543 1544
        !(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 已提交
1545 1546
        goto error;

1547 1548
    x86DataIntersect(&cpu_forbid->data, &host_model->data);
    if (!x86DataIsEmpty(&cpu_forbid->data)) {
1549
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
1550
                              &cpu_forbid->data);
1551
        goto cleanup;
J
Jiri Denemark 已提交
1552 1553
    }

1554 1555 1556
    /* first remove features that were inherited from the CPU model and were
     * explicitly forced, disabled, or made optional
     */
1557 1558 1559
    x86DataSubtract(&cpu_require->data, &cpu_force->data);
    x86DataSubtract(&cpu_require->data, &cpu_optional->data);
    x86DataSubtract(&cpu_require->data, &cpu_disable->data);
J
Jiri Denemark 已提交
1560 1561
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1562
        x86DataSubtract(&cpu_require->data, &host_model->data);
1563 1564
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
1565
                              &cpu_require->data);
1566
        goto cleanup;
J
Jiri Denemark 已提交
1567 1568 1569 1570
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

1571
    if (!(diff = x86ModelCopy(host_model)))
1572
        goto error;
J
Jiri Denemark 已提交
1573

1574 1575 1576 1577
    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 已提交
1578

1579
    if (!x86DataIsEmpty(&diff->data))
J
Jiri Denemark 已提交
1580
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1581 1582 1583 1584

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1585 1586
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
1587
                              &diff->data);
1588
        goto cleanup;
J
Jiri Denemark 已提交
1589 1590
    }

1591 1592
    if (guest) {
        if (!(guest_model = x86ModelCopy(host_model)))
1593
            goto error;
J
Jiri Denemark 已提交
1594

1595
        if (cpu->vendor &&
1596
            virCPUx86DataAddCPUID(&guest_model->data,
1597 1598 1599
                                  &host_model->vendor->cpuid) < 0)
            goto error;

1600 1601 1602
        if (x86DataAddSignature(&guest_model->data, host_model->signature) < 0)
            goto error;

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

1607
        if (x86DataAdd(&guest_model->data, &cpu_force->data))
1608
            goto error;
J
Jiri Denemark 已提交
1609

1610
        x86DataSubtract(&guest_model->data, &cpu_disable->data);
J
Jiri Denemark 已提交
1611

1612 1613
        if (x86DataCopy(&guestData, &guest_model->data) < 0 ||
            !(*guest = virCPUx86MakeData(arch, &guestData)))
1614
            goto error;
J
Jiri Denemark 已提交
1615 1616
    }

1617
 cleanup:
J
Jiri Denemark 已提交
1618 1619 1620 1621 1622 1623 1624 1625
    x86ModelFree(host_model);
    x86ModelFree(diff);
    x86ModelFree(cpu_force);
    x86ModelFree(cpu_require);
    x86ModelFree(cpu_optional);
    x86ModelFree(cpu_disable);
    x86ModelFree(cpu_forbid);
    x86ModelFree(guest_model);
1626
    virCPUx86DataClear(&guestData);
J
Jiri Denemark 已提交
1627 1628 1629

    return ret;

1630
 error:
J
Jiri Denemark 已提交
1631
    ret = VIR_CPU_COMPARE_ERROR;
1632
    goto cleanup;
J
Jiri Denemark 已提交
1633
}
1634
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1635 1636 1637 1638


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
1639
           virCPUDefPtr cpu,
1640
           bool failIncompatible)
J
Jiri Denemark 已提交
1641
{
1642 1643 1644 1645 1646
    virCPUCompareResult ret;
    char *message = NULL;

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

1647
    if (failIncompatible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
        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 已提交
1658 1659 1660 1661 1662 1663
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1664
             virCPUDataPtr *data,
1665
             char **message)
J
Jiri Denemark 已提交
1666
{
1667
    return x86Compute(host, guest, data, message);
J
Jiri Denemark 已提交
1668 1669
}

1670

1671
/*
1672 1673
 * Checks whether a candidate model is a better fit for the CPU data than the
 * current model.
1674
 *
1675 1676 1677
 * Returns 0 if current is better,
 *         1 if candidate is better,
 *         2 if candidate is the best one (search should stop now).
1678 1679
 */
static int
1680 1681 1682
x86DecodeUseCandidate(virCPUx86ModelPtr current,
                      virCPUDefPtr cpuCurrent,
                      virCPUx86ModelPtr candidate,
1683
                      virCPUDefPtr cpuCandidate,
1684
                      uint32_t signature,
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
                      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;

1704 1705 1706 1707 1708 1709 1710 1711 1712
    /* Ideally we want to select a model with family/model equal to
     * family/model of the real CPU. Once we found such model, we only
     * consider candidates with matching family/model.
     */
    if (signature &&
        current->signature == signature &&
        candidate->signature != signature)
        return 0;

1713 1714 1715
    if (cpuCurrent->nfeatures > cpuCandidate->nfeatures)
        return 1;

1716 1717 1718 1719 1720 1721 1722 1723
    /* Prefer a candidate with matching signature even though it would
     * result in longer list of features.
     */
    if (signature &&
        candidate->signature == signature &&
        current->signature != signature)
        return 1;

1724 1725 1726 1727
    return 0;
}


J
Jiri Denemark 已提交
1728 1729
static int
x86Decode(virCPUDefPtr cpu,
1730
          const virCPUx86Data *data,
1731
          const char **models,
1732
          unsigned int nmodels,
1733 1734
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1735 1736
{
    int ret = -1;
J
Jiri Denemark 已提交
1737
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1738
    virCPUx86ModelPtr candidate;
1739
    virCPUDefPtr cpuCandidate;
1740
    virCPUx86ModelPtr model = NULL;
1741
    virCPUDefPtr cpuModel = NULL;
1742 1743
    virCPUx86Data copy = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data features = VIR_CPU_X86_DATA_INIT;
J
Jiri Denemark 已提交
1744
    virCPUx86VendorPtr vendor;
1745
    uint32_t signature;
1746
    ssize_t i;
1747
    int rc;
J
Jiri Denemark 已提交
1748

1749 1750
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, -1);
1751

1752
    if (!data || !(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1753 1754
        return -1;

J
Jiri Denemark 已提交
1755
    vendor = x86DataToVendor(data, map);
1756
    signature = x86DataToSignature(data);
J
Jiri Denemark 已提交
1757

1758 1759 1760 1761 1762
    /* 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];
1763
        if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
1764 1765
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1766 1767 1768
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
J
Jiri Denemark 已提交
1769
                    goto cleanup;
1770 1771 1772 1773 1774 1775 1776 1777 1778
                } 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);
            }
1779
            continue;
J
Jiri Denemark 已提交
1780 1781
        }

J
Jiri Denemark 已提交
1782 1783 1784 1785
        /* 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 已提交
1786
            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
J
Jiri Denemark 已提交
1787
                      candidate->vendor->name, candidate->name, vendor->name);
1788
            continue;
J
Jiri Denemark 已提交
1789 1790
        }

J
Jiri Denemark 已提交
1791 1792 1793 1794
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto cleanup;
        cpuCandidate->type = cpu->type;

1795 1796 1797
        if ((rc = x86DecodeUseCandidate(model, cpuModel,
                                        candidate, cpuCandidate,
                                        signature, preferred,
1798
                                        cpu->type == VIR_CPU_TYPE_HOST))) {
1799 1800
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1801
            model = candidate;
1802 1803
            if (rc == 2)
                break;
1804
        } else {
1805
            virCPUDefFree(cpuCandidate);
1806
        }
J
Jiri Denemark 已提交
1807 1808
    }

1809
    if (!cpuModel) {
1810 1811
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1812
        goto cleanup;
J
Jiri Denemark 已提交
1813 1814
    }

1815 1816 1817 1818 1819
    /* 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++) {
1820 1821 1822 1823
            if (!x86FeatureIsMigratable(cpuModel->features[i].name, map)) {
                VIR_FREE(cpuModel->features[i].name);
                VIR_DELETE_ELEMENT_INPLACE(cpuModel->features, i,
                                           cpuModel->nfeatures);
1824 1825 1826 1827
            }
        }
    }

1828
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) {
1829
        if (x86DataCopy(&copy, &model->data) < 0 ||
1830
            x86DataFromCPUFeatures(&features, cpuModel, map) < 0)
J
Jiri Denemark 已提交
1831
            goto cleanup;
1832

1833
        x86DataSubtract(&copy, &features);
1834
        if (x86DataToCPUFeatures(cpuModel, VIR_CPU_FEATURE_REQUIRE,
1835
                                 &copy, map) < 0)
J
Jiri Denemark 已提交
1836
            goto cleanup;
1837 1838
    }

J
Jiri Denemark 已提交
1839 1840 1841
    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
        goto cleanup;

1842 1843 1844 1845
    cpu->model = cpuModel->model;
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1846 1847 1848

    ret = 0;

J
Jiri Denemark 已提交
1849
 cleanup:
1850
    virCPUDefFree(cpuModel);
1851 1852
    virCPUx86DataClear(&copy);
    virCPUx86DataClear(&features);
J
Jiri Denemark 已提交
1853 1854 1855
    return ret;
}

1856 1857
static int
x86DecodeCPUData(virCPUDefPtr cpu,
1858
                 const virCPUData *data,
1859 1860
                 const char **models,
                 unsigned int nmodels,
1861 1862
                 const char *preferred,
                 unsigned int flags)
1863
{
1864
    return x86Decode(cpu, &data->data.x86, models, nmodels, preferred, flags);
1865
}
J
Jiri Denemark 已提交
1866

1867

1868 1869 1870
static int
x86EncodePolicy(virCPUx86Data *data,
                const virCPUDef *cpu,
J
Jiri Denemark 已提交
1871
                virCPUx86MapPtr map,
1872
                virCPUFeaturePolicy policy)
J
Jiri Denemark 已提交
1873
{
J
Jiri Denemark 已提交
1874
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
1875 1876

    if (!(model = x86ModelFromCPU(cpu, map, policy)))
1877
        return -1;
J
Jiri Denemark 已提交
1878

1879 1880 1881
    *data = model->data;
    model->data.len = 0;
    model->data.data = NULL;
J
Jiri Denemark 已提交
1882 1883
    x86ModelFree(model);

1884
    return 0;
J
Jiri Denemark 已提交
1885 1886 1887 1888
}


static int
J
Jiri Denemark 已提交
1889
x86Encode(virArch arch,
1890
          const virCPUDef *cpu,
1891 1892 1893 1894 1895 1896
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
1897
{
J
Jiri Denemark 已提交
1898
    virCPUx86MapPtr map = NULL;
1899 1900 1901 1902 1903 1904
    virCPUx86Data data_forced = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data data_required = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data data_optional = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data data_disabled = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data data_forbidden = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data data_vendor = VIR_CPU_X86_DATA_INIT;
J
Jiri Denemark 已提交
1905

1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

1919
    if (!(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1920 1921
        goto error;

1922 1923 1924
    if (forced &&
        x86EncodePolicy(&data_forced, cpu, map, VIR_CPU_FEATURE_FORCE) < 0)
        goto error;
J
Jiri Denemark 已提交
1925

1926 1927 1928
    if (required &&
        x86EncodePolicy(&data_required, cpu, map, VIR_CPU_FEATURE_REQUIRE) < 0)
        goto error;
J
Jiri Denemark 已提交
1929

1930 1931 1932
    if (optional &&
        x86EncodePolicy(&data_optional, cpu, map, VIR_CPU_FEATURE_OPTIONAL) < 0)
        goto error;
J
Jiri Denemark 已提交
1933

1934 1935 1936
    if (disabled &&
        x86EncodePolicy(&data_disabled, cpu, map, VIR_CPU_FEATURE_DISABLE) < 0)
        goto error;
J
Jiri Denemark 已提交
1937

1938 1939 1940
    if (forbidden &&
        x86EncodePolicy(&data_forbidden, cpu, map, VIR_CPU_FEATURE_FORBID) < 0)
        goto error;
J
Jiri Denemark 已提交
1941

J
Jiri Denemark 已提交
1942
    if (vendor) {
1943
        virCPUx86VendorPtr v = NULL;
J
Jiri Denemark 已提交
1944 1945

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
1946 1947
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s not found"), cpu->vendor);
J
Jiri Denemark 已提交
1948 1949 1950
            goto error;
        }

1951
        if (v && virCPUx86DataAddCPUID(&data_vendor, &v->cpuid) < 0)
J
Jiri Denemark 已提交
1952 1953 1954
            goto error;
    }

1955
    if (forced &&
1956
        !(*forced = virCPUx86MakeData(arch, &data_forced)))
1957 1958
        goto error;
    if (required &&
1959
        !(*required = virCPUx86MakeData(arch, &data_required)))
1960 1961
        goto error;
    if (optional &&
1962
        !(*optional = virCPUx86MakeData(arch, &data_optional)))
1963 1964
        goto error;
    if (disabled &&
1965
        !(*disabled = virCPUx86MakeData(arch, &data_disabled)))
1966 1967
        goto error;
    if (forbidden &&
1968
        !(*forbidden = virCPUx86MakeData(arch, &data_forbidden)))
1969 1970
        goto error;
    if (vendor &&
1971
        !(*vendor = virCPUx86MakeData(arch, &data_vendor)))
1972
        goto error;
J
Jiri Denemark 已提交
1973

1974
    return 0;
J
Jiri Denemark 已提交
1975

1976
 error:
1977 1978 1979 1980 1981 1982
    virCPUx86DataClear(&data_forced);
    virCPUx86DataClear(&data_required);
    virCPUx86DataClear(&data_optional);
    virCPUx86DataClear(&data_disabled);
    virCPUx86DataClear(&data_forbidden);
    virCPUx86DataClear(&data_vendor);
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
    if (forced)
        x86FreeCPUData(*forced);
    if (required)
        x86FreeCPUData(*required);
    if (optional)
        x86FreeCPUData(*optional);
    if (disabled)
        x86FreeCPUData(*disabled);
    if (forbidden)
        x86FreeCPUData(*forbidden);
    if (vendor)
        x86FreeCPUData(*vendor);
1995
    return -1;
J
Jiri Denemark 已提交
1996 1997 1998 1999 2000
}


#if HAVE_CPUID
static inline void
2001
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
2002
{
2003
# if __x86_64__
2004
    asm("xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
2005
        "xor %%edx, %%edx;" /* functions may use them as additional arguments */
2006
        "cpuid;"
J
Jiri Denemark 已提交
2007 2008 2009 2010
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
2011 2012
        : "a" (cpuid->eax_in),
          "c" (cpuid->ecx_in));
2013
# else
J
Jiri Denemark 已提交
2014 2015 2016 2017
    /* 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;"
2018
        "xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
2019
        "xor %%edx, %%edx;" /* functions may use them as additional arguments */
J
Jiri Denemark 已提交
2020 2021 2022 2023 2024 2025 2026
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
2027 2028
        : "a" (cpuid->eax_in),
          "c" (cpuid->ecx_in)
J
Jiri Denemark 已提交
2029
        : "cc");
2030
# endif
J
Jiri Denemark 已提交
2031 2032 2033
}


2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
/* Leaf 0x04: deterministic cache parameters
 *
 * Sub leaf n+1 is invalid if eax[4:0] in sub leaf n equals 0.
 */
static int
cpuidSetLeaf4(virCPUx86Data *data,
              virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = *subLeaf0;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    while (cpuid.eax & 0x1f) {
        cpuid.ecx_in++;
        cpuidCall(&cpuid);
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
    }
    return 0;
}


/* Leaf 0x07: structured extended feature flags enumeration
 *
 * Sub leaf n is invalid if n > eax in sub leaf 0.
 */
static int
cpuidSetLeaf7(virCPUx86Data *data,
              virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = { .eax_in = 0x7 };
    uint32_t sub;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    for (sub = 1; sub <= subLeaf0->eax; sub++) {
        cpuid.ecx_in = sub;
        cpuidCall(&cpuid);
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
    }
    return 0;
}


/* Leaf 0x0b: extended topology enumeration
 *
 * Sub leaf n is invalid if it returns 0 in ecx[15:8].
 * Sub leaf n+1 is invalid if sub leaf n is invalid.
 * Some output values do not depend on ecx, thus sub leaf 0 provides
 * meaningful data even if it was (theoretically) considered invalid.
 */
static int
cpuidSetLeafB(virCPUx86Data *data,
              virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = *subLeaf0;

    while (cpuid.ecx & 0xff00) {
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
        cpuid.ecx_in++;
        cpuidCall(&cpuid);
    }
    return 0;
}


/* Leaf 0x0d: processor extended state enumeration
 *
 * Sub leaves 0 and 1 are valid.
 * Sub leaf n (2 <= n < 32) is invalid if eax[n] from sub leaf 0 is not set
 * and ecx[n] from sub leaf 1 is not set.
 * Sub leaf n (32 <= n < 64) is invalid if edx[n-32] from sub leaf 0 is not set
 * and edx[n-32] from sub leaf 1 is not set.
 */
static int
cpuidSetLeafD(virCPUx86Data *data,
              virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = { .eax_in = 0xd };
    virCPUx86CPUID sub0;
    virCPUx86CPUID sub1;
    uint32_t sub;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    cpuid.ecx_in = 1;
    cpuidCall(&cpuid);
    if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
        return -1;

    sub0 = *subLeaf0;
    sub1 = cpuid;
    for (sub = 2; sub < 64; sub++) {
        if (sub < 32 &&
            !(sub0.eax & (1 << sub)) &&
            !(sub1.ecx & (1 << sub)))
            continue;
        if (sub >= 32 &&
            !(sub0.edx & (1 << (sub - 32))) &&
            !(sub1.edx & (1 << (sub - 32))))
            continue;

        cpuid.ecx_in = sub;
        cpuidCall(&cpuid);
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
    }
    return 0;
}


/* Leaf 0x0f: L3 cached RDT monitoring capability enumeration
 * Leaf 0x10: RDT allocation enumeration
 *
 * res reports valid resource identification (ResID) starting at bit 1.
 * Values associated with each valid ResID are reported by ResID sub leaf.
 *
 * 0x0f: Sub leaf n is valid if edx[n] (= res[ResID]) from sub leaf 0 is set.
 * 0x10: Sub leaf n is valid if ebx[n] (= res[ResID]) from sub leaf 0 is set.
 */
static int
cpuidSetLeafResID(virCPUx86Data *data,
                  virCPUx86CPUID *subLeaf0,
                  uint32_t res)
{
    virCPUx86CPUID cpuid = { .eax_in = subLeaf0->eax_in };
    uint32_t sub;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    for (sub = 1; sub < 32; sub++) {
        if (!(res & (1 << sub)))
            continue;
        cpuid.ecx_in = sub;
        cpuidCall(&cpuid);
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
    }
    return 0;
}


/* Leaf 0x12: SGX capability enumeration
 *
 * Sub leaves 0 and 1 is supported if ebx[2] from leaf 0x7 (SGX) is set.
 * Sub leaves n >= 2 are valid as long as eax[3:0] != 0.
 */
static int
cpuidSetLeaf12(virCPUx86Data *data,
               virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = { .eax_in = 0x7 };
    virCPUx86CPUID *cpuid7;

    if (!(cpuid7 = x86DataCpuid(data, &cpuid)) ||
        !(cpuid7->ebx & (1 << 2)))
        return 0;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    cpuid.eax_in = 0x12;
    cpuid.ecx_in = 1;
    cpuidCall(&cpuid);
    if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
        return -1;

    cpuid.ecx_in = 2;
    cpuidCall(&cpuid);
    while (cpuid.eax & 0xf) {
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
        cpuid.ecx_in++;
        cpuidCall(&cpuid);
    }
    return 0;
}


/* Leaf 0x14: processor trace enumeration
 *
 * Sub leaf 0 reports the maximum supported sub leaf in eax.
 */
static int
cpuidSetLeaf14(virCPUx86Data *data,
               virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = { .eax_in = 0x14 };
    uint32_t sub;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    for (sub = 1; sub <= subLeaf0->eax; sub++) {
        cpuid.ecx_in = sub;
        cpuidCall(&cpuid);
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
    }
    return 0;
}


/* Leaf 0x17: SOC Vendor
 *
 * Sub leaf 0 is valid if eax >= 3.
 * Sub leaf 0 reports the maximum supported sub leaf in eax.
 */
static int
cpuidSetLeaf17(virCPUx86Data *data,
               virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = { .eax_in = 0x17 };
    uint32_t sub;

    if (subLeaf0->eax < 3)
        return 0;

    if (virCPUx86DataAddCPUID(data, subLeaf0) < 0)
        return -1;

    for (sub = 1; sub <= subLeaf0->eax; sub++) {
        cpuid.ecx_in = sub;
        cpuidCall(&cpuid);
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
    }
    return 0;
}


J
Jiri Denemark 已提交
2271
static int
2272
cpuidSet(uint32_t base, virCPUx86Data *data)
J
Jiri Denemark 已提交
2273
{
2274
    int rc;
J
Jiri Denemark 已提交
2275
    uint32_t max;
2276
    uint32_t leaf;
2277
    virCPUx86CPUID cpuid = { .eax_in = base };
J
Jiri Denemark 已提交
2278 2279

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

2282 2283
    for (leaf = base; leaf <= max; leaf++) {
        cpuid.eax_in = leaf;
2284
        cpuid.ecx_in = 0;
J
Jiri Denemark 已提交
2285
        cpuidCall(&cpuid);
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311

        /* Handle CPUID leaves that depend on previously queried bits or
         * which provide additional sub leaves for ecx_in > 0
         */
        if (leaf == 0x4)
            rc = cpuidSetLeaf4(data, &cpuid);
        else if (leaf == 0x7)
            rc = cpuidSetLeaf7(data, &cpuid);
        else if (leaf == 0xb)
            rc = cpuidSetLeafB(data, &cpuid);
        else if (leaf == 0xd)
            rc = cpuidSetLeafD(data, &cpuid);
        else if (leaf == 0xf)
            rc = cpuidSetLeafResID(data, &cpuid, cpuid.edx);
        else if (leaf == 0x10)
            rc = cpuidSetLeafResID(data, &cpuid, cpuid.ebx);
        else if (leaf == 0x12)
            rc = cpuidSetLeaf12(data, &cpuid);
        else if (leaf == 0x14)
            rc = cpuidSetLeaf14(data, &cpuid);
        else if (leaf == 0x17)
            rc = cpuidSetLeaf17(data, &cpuid);
        else
            rc = virCPUx86DataAddCPUID(data, &cpuid);

        if (rc < 0)
2312
            return -1;
J
Jiri Denemark 已提交
2313 2314
    }

2315
    return 0;
J
Jiri Denemark 已提交
2316 2317 2318
}


2319
static virCPUDataPtr
2320
x86NodeData(virArch arch)
J
Jiri Denemark 已提交
2321
{
2322
    virCPUDataPtr cpuData = NULL;
2323
    virCPUx86Data data = VIR_CPU_X86_DATA_INIT;
J
Jiri Denemark 已提交
2324

2325
    if (cpuidSet(CPUX86_BASIC, &data) < 0)
J
Jiri Denemark 已提交
2326 2327
        goto error;

2328
    if (cpuidSet(CPUX86_EXTENDED, &data) < 0)
J
Jiri Denemark 已提交
2329 2330
        goto error;

2331
    if (!(cpuData = virCPUx86MakeData(arch, &data)))
2332 2333 2334
        goto error;

    return cpuData;
J
Jiri Denemark 已提交
2335

2336
 error:
2337
    virCPUx86DataClear(&data);
J
Jiri Denemark 已提交
2338 2339 2340 2341 2342 2343

    return NULL;
}
#endif


2344 2345 2346 2347
static virCPUDefPtr
x86Baseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
2348 2349
            unsigned int nmodels,
            unsigned int flags)
2350
{
J
Jiri Denemark 已提交
2351
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
2352
    virCPUx86ModelPtr base_model = NULL;
2353
    virCPUDefPtr cpu = NULL;
2354
    size_t i;
2355
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
2356
    virCPUx86ModelPtr model = NULL;
2357
    bool outputVendor = true;
2358 2359
    const char *modelName;
    bool matchingNames = true;
2360

2361 2362 2363
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, NULL);

2364
    if (!(map = virCPUx86GetMap()))
2365 2366
        goto error;

2367
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
2368 2369
        goto error;

2370
    if (VIR_ALLOC(cpu) < 0)
2371
        goto error;
2372 2373

    cpu->arch = cpus[0]->arch;
2374 2375
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
2376

2377
    if (!cpus[0]->vendor) {
2378
        outputVendor = false;
2379
    } else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
2380 2381
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Unknown CPU vendor %s"), cpus[0]->vendor);
J
Jiri Denemark 已提交
2382 2383 2384
        goto error;
    }

2385
    modelName = cpus[0]->model;
2386
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
2387 2388
        const char *vn = NULL;

2389 2390 2391 2392 2393 2394 2395 2396 2397
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

2398
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
2399 2400
            goto error;

J
Jiri Denemark 已提交
2401 2402
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
2403 2404 2405
            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 已提交
2406 2407 2408
            goto error;
        }

2409
        if (cpus[i]->vendor) {
J
Jiri Denemark 已提交
2410
            vn = cpus[i]->vendor;
2411
        } else {
2412 2413 2414 2415
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
2416 2417 2418 2419

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
2420 2421
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("Unknown CPU vendor %s"), vn);
J
Jiri Denemark 已提交
2422 2423 2424
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
2425 2426
                virReportError(VIR_ERR_OPERATION_FAILED,
                               "%s", _("CPU vendors do not match"));
J
Jiri Denemark 已提交
2427 2428 2429 2430
                goto error;
            }
        }

2431
        x86DataIntersect(&base_model->data, &model->data);
2432
        x86ModelFree(model);
J
Jiri Denemark 已提交
2433
        model = NULL;
2434 2435
    }

2436
    if (x86DataIsEmpty(&base_model->data)) {
2437 2438
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
2439 2440 2441
        goto error;
    }

2442
    if (vendor && virCPUx86DataAddCPUID(&base_model->data, &vendor->cpuid) < 0)
2443
        goto error;
J
Jiri Denemark 已提交
2444

2445
    if (x86Decode(cpu, &base_model->data, models, nmodels, modelName, flags) < 0)
2446 2447
        goto error;

2448 2449 2450
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2451 2452 2453
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2454 2455
    cpu->arch = VIR_ARCH_NONE;

2456
 cleanup:
2457 2458 2459 2460
    x86ModelFree(base_model);

    return cpu;

2461
 error:
J
Jiri Denemark 已提交
2462
    x86ModelFree(model);
2463 2464 2465 2466 2467 2468
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


2469
static int
2470
x86UpdateCustom(virCPUDefPtr guest,
2471
                const virCPUDef *host)
2472 2473
{
    int ret = -1;
2474
    size_t i;
J
Jiri Denemark 已提交
2475
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
2476
    virCPUx86ModelPtr host_model = NULL;
2477

2478
    if (!(map = virCPUx86GetMap()) ||
2479
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
2480 2481 2482 2483
        goto cleanup;

    for (i = 0; i < guest->nfeatures; i++) {
        if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
2484
            virCPUx86FeaturePtr feature;
2485
            if (!(feature = x86FeatureFind(map, guest->features[i].name))) {
2486 2487 2488
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unknown CPU feature %s"),
                               guest->features[i].name);
2489 2490 2491
                goto cleanup;
            }

2492
            if (x86DataIsSubset(&host_model->data, &feature->data))
2493 2494 2495 2496 2497 2498 2499 2500
                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 已提交
2501 2502
        if (x86ModelSubtractCPU(host_model, guest, map) ||
            x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
2503
                                 &host_model->data, map))
2504 2505 2506 2507 2508
            goto cleanup;
    }

    ret = 0;

2509
 cleanup:
2510 2511 2512 2513
    x86ModelFree(host_model);
    return ret;
}

2514 2515 2516

static int
x86UpdateHostModel(virCPUDefPtr guest,
2517 2518
                   const virCPUDef *host,
                   bool passthrough)
2519
{
2520
    virCPUDefPtr oldguest = NULL;
J
Jiri Denemark 已提交
2521
    virCPUx86MapPtr map;
2522
    size_t i;
2523
    int ret = -1;
2524

2525 2526
    if (!(map = virCPUx86GetMap()))
        goto cleanup;
2527 2528 2529

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
2530
        goto cleanup;
2531 2532 2533

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
2534
        goto cleanup;
2535

2536 2537 2538 2539 2540 2541
    if (oldguest->vendor_id) {
        VIR_FREE(guest->vendor_id);
        if (VIR_STRDUP(guest->vendor_id, oldguest->vendor_id) < 0)
            goto cleanup;
    }

2542 2543 2544 2545
    /* 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++) {
2546 2547 2548
        if (!x86FeatureIsMigratable(guest->features[i].name, map)) {
            VIR_FREE(guest->features[i].name);
            VIR_DELETE_ELEMENT_INPLACE(guest->features, i, guest->nfeatures);
2549 2550
        }
    }
2551
    for (i = 0; !passthrough && i < oldguest->nfeatures; i++) {
2552 2553 2554
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
2555
            goto cleanup;
2556 2557
    }

2558 2559 2560 2561 2562
    ret = 0;

 cleanup:
    virCPUDefFree(oldguest);
    return ret;
2563 2564 2565
}


2566 2567
static int
x86Update(virCPUDefPtr guest,
2568
          const virCPUDef *host)
2569
{
2570
    switch ((virCPUMode) guest->mode) {
2571 2572 2573 2574
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2575 2576
        guest->match = VIR_CPU_MATCH_EXACT;
        return x86UpdateHostModel(guest, host, false);
2577

2578
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2579
        guest->match = VIR_CPU_MATCH_MINIMUM;
2580
        return x86UpdateHostModel(guest, host, true);
2581 2582 2583 2584 2585

    case VIR_CPU_MODE_LAST:
        break;
    }

2586 2587
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2588 2589 2590
    return -1;
}

2591 2592 2593 2594

static int
x86HasFeature(const virCPUData *data,
              const char *name)
D
Daniel P. Berrange 已提交
2595
{
J
Jiri Denemark 已提交
2596
    virCPUx86MapPtr map;
2597
    virCPUx86FeaturePtr feature;
D
Daniel P. Berrange 已提交
2598 2599
    int ret = -1;

2600
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2601 2602
        return -1;

2603 2604
    if (!(feature = x86FeatureFind(map, name)) &&
        !(feature = x86FeatureFindInternal(name)))
D
Daniel P. Berrange 已提交
2605 2606
        goto cleanup;

2607
    ret = x86DataIsSubset(&data->data.x86, &feature->data) ? 1 : 0;
D
Daniel P. Berrange 已提交
2608

2609
 cleanup:
D
Daniel P. Berrange 已提交
2610 2611
    return ret;
}
2612

2613 2614 2615
static int
x86GetModels(char ***models)
{
J
Jiri Denemark 已提交
2616
    virCPUx86MapPtr map;
2617
    size_t i;
2618 2619 2620 2621

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

2622 2623 2624
    if (models) {
        if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
            goto error;
2625

2626 2627
        for (i = 0; i < map->nmodels; i++) {
            if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
2628 2629
                goto error;
        }
2630 2631
    }

2632
    return map->nmodels;
2633 2634

 error:
2635 2636 2637 2638
    if (models) {
        virStringFreeList(*models);
        *models = NULL;
    }
2639 2640 2641
    return -1;
}

2642

J
Jiri Denemark 已提交
2643 2644 2645 2646 2647
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
    .compare    = x86Compare,
2648
    .decode     = x86DecodeCPUData,
J
Jiri Denemark 已提交
2649
    .encode     = x86Encode,
2650
    .free       = x86FreeCPUData,
J
Jiri Denemark 已提交
2651 2652 2653 2654 2655
#if HAVE_CPUID
    .nodeData   = x86NodeData,
#else
    .nodeData   = NULL,
#endif
2656
    .guestData  = x86GuestData,
2657
    .baseline   = x86Baseline,
2658
    .update     = x86Update,
D
Daniel P. Berrange 已提交
2659
    .hasFeature = x86HasFeature,
2660 2661
    .dataFormat = x86CPUDataFormat,
    .dataParse  = x86CPUDataParse,
2662
    .getModels  = x86GetModels,
J
Jiri Denemark 已提交
2663
};