cpu_x86.c 79.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"
36
#include "virhostcpu.h"
J
Jiri Denemark 已提交
37 38 39

#define VIR_FROM_THIS VIR_FROM_CPU

40 41
VIR_LOG_INIT("cpu.cpu_x86");

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

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

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

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

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

63

64 65 66
#define KVM_FEATURE_DEF(Name, Eax_in, Eax) \
    static virCPUx86CPUID Name ## _cpuid[] = { \
        { .eax_in = Eax_in, .eax = Eax }, \
67 68
    }

69 70 71 72 73 74 75
#define KVM_FEATURE(Name) \
    { \
        .name = (char *) Name, \
        .data = { \
            .len = ARRAY_CARDINALITY(Name ## _cpuid), \
            .data = Name ## _cpuid \
        } \
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
    }

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);
104
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_SPINLOCKS,
105 106 107 108 109 110 111
                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);
112 113
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_FREQUENCIES,
                0x40000003, 0x00000800);
114 115
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_REENLIGHTENMENT,
                0x40000003, 0x00002000);
116 117
KVM_FEATURE_DEF(VIR_CPU_x86_KVM_HV_TLBFLUSH,
                0x40000004, 0x00000004);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

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),
134
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_SPINLOCKS),
135 136 137
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_VAPIC),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_VPINDEX),
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_RESET),
138
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_FREQUENCIES),
139
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_REENLIGHTENMENT),
140
    KVM_FEATURE(VIR_CPU_x86_KVM_HV_TLBFLUSH),
141 142
};

J
Jiri Denemark 已提交
143 144 145
typedef struct _virCPUx86Model virCPUx86Model;
typedef virCPUx86Model *virCPUx86ModelPtr;
struct _virCPUx86Model {
J
Jiri Denemark 已提交
146
    char *name;
147
    virCPUx86VendorPtr vendor;
148
    uint32_t signature;
149
    virCPUx86Data data;
J
Jiri Denemark 已提交
150 151
};

J
Jiri Denemark 已提交
152 153 154
typedef struct _virCPUx86Map virCPUx86Map;
typedef virCPUx86Map *virCPUx86MapPtr;
struct _virCPUx86Map {
155 156
    size_t nvendors;
    virCPUx86VendorPtr *vendors;
157 158
    size_t nfeatures;
    virCPUx86FeaturePtr *features;
159 160
    size_t nmodels;
    virCPUx86ModelPtr *models;
161 162
    size_t nblockers;
    virCPUx86FeaturePtr *migrate_blockers;
J
Jiri Denemark 已提交
163 164
};

J
Jiri Denemark 已提交
165
static virCPUx86MapPtr cpuMap;
166 167
static unsigned int microcodeVersion;

168 169
int virCPUx86DriverOnceInit(void);
VIR_ONCE_GLOBAL_INIT(virCPUx86Driver);
170

J
Jiri Denemark 已提交
171

172
typedef enum {
J
Jiri Denemark 已提交
173 174 175 176
    SUBSET,
    EQUAL,
    SUPERSET,
    UNRELATED
177
} virCPUx86CompareResult;
J
Jiri Denemark 已提交
178 179


180 181 182
typedef struct _virCPUx86DataIterator virCPUx86DataIterator;
typedef virCPUx86DataIterator *virCPUx86DataIteratorPtr;
struct _virCPUx86DataIterator {
183
    const virCPUx86Data *data;
J
Jiri Denemark 已提交
184 185
    int pos;
};
J
Jiri Denemark 已提交
186 187


188
#define virCPUx86DataIteratorInit(data) \
189
    { data, -1 }
J
Jiri Denemark 已提交
190 191


192
static bool
193 194
x86cpuidMatch(const virCPUx86CPUID *cpuid1,
              const virCPUx86CPUID *cpuid2)
J
Jiri Denemark 已提交
195 196 197 198 199 200 201 202
{
    return (cpuid1->eax == cpuid2->eax &&
            cpuid1->ebx == cpuid2->ebx &&
            cpuid1->ecx == cpuid2->ecx &&
            cpuid1->edx == cpuid2->edx);
}


203
static bool
204 205
x86cpuidMatchMasked(const virCPUx86CPUID *cpuid,
                    const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
206 207 208 209 210 211 212 213
{
    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 已提交
214
static void
215 216
x86cpuidSetBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
217
{
218 219 220
    if (!mask)
        return;

J
Jiri Denemark 已提交
221 222 223 224 225 226 227
    cpuid->eax |= mask->eax;
    cpuid->ebx |= mask->ebx;
    cpuid->ecx |= mask->ecx;
    cpuid->edx |= mask->edx;
}


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

J
Jiri Denemark 已提交
235 236 237 238 239 240 241
    cpuid->eax &= ~mask->eax;
    cpuid->ebx &= ~mask->ebx;
    cpuid->ecx &= ~mask->ecx;
    cpuid->edx &= ~mask->edx;
}


J
Jiri Denemark 已提交
242
static void
243 244
x86cpuidAndBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
245
{
246 247 248
    if (!mask)
        return;

249 250 251 252 253 254
    cpuid->eax &= mask->eax;
    cpuid->ebx &= mask->ebx;
    cpuid->ecx &= mask->ecx;
    cpuid->edx &= mask->edx;
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

static virCPUx86FeaturePtr
x86FeatureFind(virCPUx86MapPtr map,
               const char *name)
{
    size_t i;

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

    return NULL;
}


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


286 287 288 289 290 291
static int
virCPUx86CPUIDSorter(const void *a, const void *b)
{
    virCPUx86CPUID *da = (virCPUx86CPUID *) a;
    virCPUx86CPUID *db = (virCPUx86CPUID *) b;

292
    if (da->eax_in > db->eax_in)
293
        return 1;
294
    else if (da->eax_in < db->eax_in)
295 296
        return -1;

297 298 299 300 301
    if (da->ecx_in > db->ecx_in)
        return 1;
    else if (da->ecx_in < db->ecx_in)
        return -1;

302 303 304
    return 0;
}

305

306
/* skips all zero CPUID leaves */
307
static virCPUx86CPUID *
308
x86DataCpuidNext(virCPUx86DataIteratorPtr iterator)
J
Jiri Denemark 已提交
309
{
310
    const virCPUx86Data *data = iterator->data;
J
Jiri Denemark 已提交
311

312
    if (!data)
J
Jiri Denemark 已提交
313 314
        return NULL;

315 316 317 318
    while (++iterator->pos < data->len) {
        if (!x86cpuidMatch(data->data + iterator->pos, &cpuidNull))
            return data->data + iterator->pos;
    }
J
Jiri Denemark 已提交
319

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


324
static virCPUx86CPUID *
325
x86DataCpuid(const virCPUx86Data *data,
326
             const virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
327
{
328
    size_t i;
J
Jiri Denemark 已提交
329

330
    for (i = 0; i < data->len; i++) {
331 332
        if (data->data[i].eax_in == cpuid->eax_in &&
            data->data[i].ecx_in == cpuid->ecx_in)
333
            return data->data + i;
J
Jiri Denemark 已提交
334 335
    }

336
    return NULL;
J
Jiri Denemark 已提交
337 338
}

339
static void
340
virCPUx86DataClear(virCPUx86Data *data)
J
Jiri Denemark 已提交
341
{
342
    if (!data)
J
Jiri Denemark 已提交
343 344
        return;

345
    VIR_FREE(data->data);
J
Jiri Denemark 已提交
346 347 348
}


349
static void
J
Jiri Denemark 已提交
350
virCPUx86DataFree(virCPUDataPtr data)
351 352 353 354
{
    if (!data)
        return;

355
    virCPUx86DataClear(&data->data.x86);
356 357 358 359
    VIR_FREE(data);
}


360 361
static int
x86DataCopy(virCPUx86Data *dst, const virCPUx86Data *src)
J
Jiri Denemark 已提交
362
{
363
    size_t i;
J
Jiri Denemark 已提交
364

365 366
    if (VIR_ALLOC_N(dst->data, src->len) < 0)
        return -1;
J
Jiri Denemark 已提交
367

368 369 370
    dst->len = src->len;
    for (i = 0; i < src->len; i++)
        dst->data[i] = src->data[i];
J
Jiri Denemark 已提交
371

372
    return 0;
J
Jiri Denemark 已提交
373 374 375
}


376 377 378
static int
virCPUx86DataAddCPUIDInt(virCPUx86Data *data,
                         const virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
379
{
380
    virCPUx86CPUID *existing;
J
Jiri Denemark 已提交
381

382
    if ((existing = x86DataCpuid(data, cpuid))) {
383 384 385 386 387
        x86cpuidSetBits(existing, cpuid);
    } else {
        if (VIR_APPEND_ELEMENT_COPY(data->data, data->len,
                                    *((virCPUx86CPUID *)cpuid)) < 0)
            return -1;
J
Jiri Denemark 已提交
388

389 390 391
        qsort(data->data, data->len,
              sizeof(virCPUx86CPUID), virCPUx86CPUIDSorter);
    }
J
Jiri Denemark 已提交
392

J
Jiri Denemark 已提交
393 394 395 396 397
    return 0;
}


static int
398 399
x86DataAdd(virCPUx86Data *data1,
           const virCPUx86Data *data2)
J
Jiri Denemark 已提交
400
{
401
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data2);
402 403
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
404

405
    while ((cpuid2 = x86DataCpuidNext(&iter))) {
406
        cpuid1 = x86DataCpuid(data1, cpuid2);
J
Jiri Denemark 已提交
407

408 409 410
        if (cpuid1) {
            x86cpuidSetBits(cpuid1, cpuid2);
        } else {
411
            if (virCPUx86DataAddCPUIDInt(data1, cpuid2) < 0)
412 413
                return -1;
        }
J
Jiri Denemark 已提交
414
    }
J
Jiri Denemark 已提交
415 416 417 418 419

    return 0;
}


420
static void
421 422
x86DataSubtract(virCPUx86Data *data1,
                const virCPUx86Data *data2)
423
{
424
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
425 426
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
427

428
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
429
        cpuid2 = x86DataCpuid(data2, cpuid1);
430
        x86cpuidClearBits(cpuid1, cpuid2);
431 432 433 434
    }
}


J
Jiri Denemark 已提交
435
static void
436 437
x86DataIntersect(virCPUx86Data *data1,
                 const virCPUx86Data *data2)
438
{
439
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data1);
440 441
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
442

J
Jiri Denemark 已提交
443
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
444
        cpuid2 = x86DataCpuid(data2, cpuid1);
J
Jiri Denemark 已提交
445 446 447 448
        if (cpuid2)
            x86cpuidAndBits(cpuid1, cpuid2);
        else
            x86cpuidClearBits(cpuid1, cpuid1);
449 450 451 452
    }
}


J
Jiri Denemark 已提交
453
static bool
454
x86DataIsEmpty(virCPUx86Data *data)
J
Jiri Denemark 已提交
455
{
456
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data);
J
Jiri Denemark 已提交
457

458
    return !x86DataCpuidNext(&iter);
J
Jiri Denemark 已提交
459
}
J
Jiri Denemark 已提交
460 461


J
Jiri Denemark 已提交
462
static bool
463 464
x86DataIsSubset(const virCPUx86Data *data,
                const virCPUx86Data *subset)
J
Jiri Denemark 已提交
465 466
{

467
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit((virCPUx86Data *)subset);
468 469
    const virCPUx86CPUID *cpuid;
    const virCPUx86CPUID *cpuidSubset;
J
Jiri Denemark 已提交
470

J
Jiri Denemark 已提交
471
    while ((cpuidSubset = x86DataCpuidNext(&iter))) {
472
        if (!(cpuid = x86DataCpuid(data, cpuidSubset)) ||
J
Jiri Denemark 已提交
473 474
            !x86cpuidMatchMasked(cpuid, cpuidSubset))
            return false;
J
Jiri Denemark 已提交
475 476
    }

J
Jiri Denemark 已提交
477
    return true;
J
Jiri Denemark 已提交
478 479 480
}


481 482 483 484
/* also removes all detected features from data */
static int
x86DataToCPUFeatures(virCPUDefPtr cpu,
                     int policy,
485
                     virCPUx86Data *data,
J
Jiri Denemark 已提交
486
                     virCPUx86MapPtr map)
487
{
488
    size_t i;
489

490 491
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
492 493
        if (x86DataIsSubset(data, &feature->data)) {
            x86DataSubtract(data, &feature->data);
J
Jiri Denemark 已提交
494 495
            if (virCPUDefAddFeature(cpu, feature->name, policy) < 0)
                return -1;
496 497 498 499 500 501 502
        }
    }

    return 0;
}


J
Jiri Denemark 已提交
503
/* also removes bits corresponding to vendor string from data */
504
static virCPUx86VendorPtr
J
Jiri Denemark 已提交
505
x86DataToVendor(const virCPUx86Data *data,
J
Jiri Denemark 已提交
506
                virCPUx86MapPtr map)
J
Jiri Denemark 已提交
507
{
508
    virCPUx86CPUID *cpuid;
509
    size_t i;
J
Jiri Denemark 已提交
510

511 512
    for (i = 0; i < map->nvendors; i++) {
        virCPUx86VendorPtr vendor = map->vendors[i];
513
        if ((cpuid = x86DataCpuid(data, &vendor->cpuid)) &&
J
Jiri Denemark 已提交
514 515 516 517 518 519 520 521 522 523
            x86cpuidMatchMasked(cpuid, &vendor->cpuid)) {
            x86cpuidClearBits(cpuid, &vendor->cpuid);
            return vendor;
        }
    }

    return NULL;
}


524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
static int
virCPUx86VendorToCPUID(const char *vendor,
                       virCPUx86CPUID *cpuid)
{
    if (strlen(vendor) != VENDOR_STRING_LENGTH) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid CPU vendor string '%s'"), vendor);
        return -1;
    }

    cpuid->eax_in = 0;
    cpuid->ecx_in = 0;
    cpuid->ebx = virReadBufInt32LE(vendor);
    cpuid->edx = virReadBufInt32LE(vendor + 4);
    cpuid->ecx = virReadBufInt32LE(vendor + 8);

    return 0;
}


544 545
static uint32_t
x86MakeSignature(unsigned int family,
546 547
                 unsigned int model,
                 unsigned int stepping)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
{
    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]
567
     * stepping = eax[3:0]
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
     */

    /* 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;

587 588
    /* Step */
    sig |= stepping & 0xf;
589 590 591 592 593

    return sig;
}


594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static void
x86DataToSignatureFull(const virCPUx86Data *data,
                       unsigned int *family,
                       unsigned int *model,
                       unsigned int *stepping)
{
    virCPUx86CPUID leaf1 = { .eax_in = 0x1 };
    virCPUx86CPUID *cpuid;

    *family = *model = *stepping = 0;

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

    *family = ((cpuid->eax >> 20) & 0xff) + ((cpuid->eax >> 8) & 0xf);
    *model = ((cpuid->eax >> 12) & 0xf0) + ((cpuid->eax >> 4) & 0xf);
    *stepping = cpuid->eax & 0xf;
}


614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
/* 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 };

636
    return virCPUx86DataAddCPUIDInt(data, &cpuid);
637 638 639
}


640
static virCPUDefPtr
641
x86DataToCPU(const virCPUx86Data *data,
J
Jiri Denemark 已提交
642
             virCPUx86ModelPtr model,
643 644
             virCPUx86MapPtr map,
             virDomainCapsCPUModelPtr hvModel)
645 646
{
    virCPUDefPtr cpu;
647 648
    virCPUx86Data copy = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data modelData = VIR_CPU_X86_DATA_INIT;
649
    virCPUx86VendorPtr vendor;
650 651

    if (VIR_ALLOC(cpu) < 0 ||
652
        VIR_STRDUP(cpu->model, model->name) < 0 ||
653 654
        x86DataCopy(&copy, data) < 0 ||
        x86DataCopy(&modelData, &model->data) < 0)
655
        goto error;
656

657
    if ((vendor = x86DataToVendor(&copy, map)) &&
658
        VIR_STRDUP(cpu->vendor, vendor->name) < 0)
659
        goto error;
J
Jiri Denemark 已提交
660

661 662
    x86DataSubtract(&copy, &modelData);
    x86DataSubtract(&modelData, data);
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    /* The hypervisor's version of the CPU model (hvModel) may contain
     * additional features which may be currently unavailable. Such features
     * block usage of the CPU model and we need to explicitly disable them.
     */
    if (hvModel && hvModel->blockers) {
        char **blocker;
        virCPUx86FeaturePtr feature;

        for (blocker = hvModel->blockers; *blocker; blocker++) {
            if ((feature = x86FeatureFind(map, *blocker)) &&
                !x86DataIsSubset(&copy, &feature->data))
                x86DataAdd(&modelData, &feature->data);
        }
    }

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

682 683
    if (x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_REQUIRE, &copy, map) ||
        x86DataToCPUFeatures(cpu, VIR_CPU_FEATURE_DISABLE, &modelData, map))
684
        goto error;
685

686
 cleanup:
687 688
    virCPUx86DataClear(&modelData);
    virCPUx86DataClear(&copy);
689 690
    return cpu;

691
 error:
692 693 694 695 696 697
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


J
Jiri Denemark 已提交
698
static void
699
x86VendorFree(virCPUx86VendorPtr vendor)
J
Jiri Denemark 已提交
700 701 702 703 704 705
{
    if (!vendor)
        return;

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
J
Jiri Denemark 已提交
706
}
J
Jiri Denemark 已提交
707 708


709
static virCPUx86VendorPtr
J
Jiri Denemark 已提交
710
x86VendorFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
711 712
              const char *name)
{
713
    size_t i;
J
Jiri Denemark 已提交
714

715 716 717
    for (i = 0; i < map->nvendors; i++) {
        if (STREQ(map->vendors[i]->name, name))
            return map->vendors[i];
J
Jiri Denemark 已提交
718 719 720 721 722 723
    }

    return NULL;
}


J
Jiri Denemark 已提交
724 725 726
static virCPUx86VendorPtr
x86VendorParse(xmlXPathContextPtr ctxt,
               virCPUx86MapPtr map)
J
Jiri Denemark 已提交
727
{
728
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
729 730 731
    char *string = NULL;

    if (VIR_ALLOC(vendor) < 0)
J
Jiri Denemark 已提交
732
        goto error;
J
Jiri Denemark 已提交
733 734 735

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
736 737
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing CPU vendor name"));
J
Jiri Denemark 已提交
738
        goto error;
J
Jiri Denemark 已提交
739 740 741
    }

    if (x86VendorFind(map, vendor->name)) {
742 743
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU vendor %s already defined"), vendor->name);
J
Jiri Denemark 已提交
744
        goto error;
J
Jiri Denemark 已提交
745 746 747 748
    }

    string = virXPathString("string(@string)", ctxt);
    if (!string) {
749
        virReportError(VIR_ERR_INTERNAL_ERROR,
750 751
                       _("Missing vendor string for CPU vendor %s"),
                       vendor->name);
J
Jiri Denemark 已提交
752
        goto error;
J
Jiri Denemark 已提交
753 754
    }

755 756
    if (virCPUx86VendorToCPUID(string, &vendor->cpuid) < 0)
        goto error;
E
Eric Blake 已提交
757

J
Jiri Denemark 已提交
758
 cleanup:
J
Jiri Denemark 已提交
759
    VIR_FREE(string);
J
Jiri Denemark 已提交
760 761 762
    return vendor;

 error:
J
Jiri Denemark 已提交
763
    x86VendorFree(vendor);
J
Jiri Denemark 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
    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 已提交
789 790 791
}


792
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
793 794
x86FeatureNew(void)
{
795
    virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
796 797 798 799 800 801 802 803

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

    return feature;
}


J
Jiri Denemark 已提交
804
static void
805
x86FeatureFree(virCPUx86FeaturePtr feature)
J
Jiri Denemark 已提交
806
{
807
    if (!feature)
J
Jiri Denemark 已提交
808 809 810
        return;

    VIR_FREE(feature->name);
811
    virCPUx86DataClear(&feature->data);
J
Jiri Denemark 已提交
812 813 814 815
    VIR_FREE(feature);
}


J
Jiri Denemark 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
static int
x86FeatureInData(const char *name,
                 const virCPUx86Data *data,
                 virCPUx86MapPtr map)
{
    virCPUx86FeaturePtr feature;

    if (!(feature = x86FeatureFind(map, name)) &&
        !(feature = x86FeatureFindInternal(name))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown CPU feature %s"), name);
        return -1;
    }

    if (x86DataIsSubset(data, &feature->data))
        return 1;
    else
        return 0;
}


837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
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;
}


853
static char *
J
Jiri Denemark 已提交
854
x86FeatureNames(virCPUx86MapPtr map,
855
                const char *separator,
856
                virCPUx86Data *data)
857 858 859
{
    virBuffer ret = VIR_BUFFER_INITIALIZER;
    bool first = true;
860
    size_t i;
861 862 863

    virBufferAdd(&ret, "", 0);

864 865
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
866
        if (x86DataIsSubset(data, &feature->data)) {
867 868 869 870 871
            if (!first)
                virBufferAdd(&ret, separator, -1);
            else
                first = false;

872
            virBufferAdd(&ret, feature->name, -1);
873 874 875 876 877 878 879
        }
    }

    return virBufferContentAndReset(&ret);
}


880 881
static int
x86ParseCPUID(xmlXPathContextPtr ctxt,
882
              virCPUx86CPUID *cpuid)
883
{
884
    unsigned long eax_in, ecx_in;
885
    unsigned long eax, ebx, ecx, edx;
886
    int ret_eax_in, ret_ecx_in, ret_eax, ret_ebx, ret_ecx, ret_edx;
887 888 889

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

890
    eax_in = ecx_in = 0;
891 892
    eax = ebx = ecx = edx = 0;
    ret_eax_in = virXPathULongHex("string(@eax_in)", ctxt, &eax_in);
893
    ret_ecx_in = virXPathULongHex("string(@ecx_in)", ctxt, &ecx_in);
894 895 896 897 898
    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);

899
    if (ret_eax_in < 0 || ret_ecx_in == -2 ||
900
        ret_eax == -2 || ret_ebx == -2 || ret_ecx == -2 || ret_edx == -2)
901 902
        return -1;

903
    cpuid->eax_in = eax_in;
904
    cpuid->ecx_in = ecx_in;
905 906 907 908 909 910 911 912
    cpuid->eax = eax;
    cpuid->ebx = ebx;
    cpuid->ecx = ecx;
    cpuid->edx = edx;
    return 0;
}


J
Jiri Denemark 已提交
913 914 915
static virCPUx86FeaturePtr
x86FeatureParse(xmlXPathContextPtr ctxt,
                virCPUx86MapPtr map)
J
Jiri Denemark 已提交
916 917
{
    xmlNodePtr *nodes = NULL;
918
    virCPUx86FeaturePtr feature;
919
    virCPUx86CPUID cpuid;
920
    size_t i;
J
Jiri Denemark 已提交
921
    int n;
922
    char *str = NULL;
J
Jiri Denemark 已提交
923

J
Jiri Denemark 已提交
924
    if (!(feature = x86FeatureNew()))
J
Jiri Denemark 已提交
925
        goto error;
J
Jiri Denemark 已提交
926

J
Jiri Denemark 已提交
927
    feature->migratable = true;
928
    feature->name = virXPathString("string(@name)", ctxt);
929
    if (!feature->name) {
930 931
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU feature name"));
J
Jiri Denemark 已提交
932
        goto error;
J
Jiri Denemark 已提交
933 934 935
    }

    if (x86FeatureFind(map, feature->name)) {
936 937
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature %s already defined"), feature->name);
J
Jiri Denemark 已提交
938
        goto error;
J
Jiri Denemark 已提交
939 940
    }

941 942
    str = virXPathString("string(@migratable)", ctxt);
    if (STREQ_NULLABLE(str, "no"))
J
Jiri Denemark 已提交
943
        feature->migratable = false;
944

945
    n = virXPathNodeSet("./cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
946
    if (n < 0)
J
Jiri Denemark 已提交
947
        goto error;
J
Jiri Denemark 已提交
948 949 950

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
951
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
952
            virReportError(VIR_ERR_INTERNAL_ERROR,
953 954
                           _("Invalid cpuid[%zu] in %s feature"),
                           i, feature->name);
J
Jiri Denemark 已提交
955
            goto error;
J
Jiri Denemark 已提交
956
        }
957
        if (virCPUx86DataAddCPUIDInt(&feature->data, &cpuid))
J
Jiri Denemark 已提交
958
            goto error;
J
Jiri Denemark 已提交
959 960
    }

J
Jiri Denemark 已提交
961
 cleanup:
962
    VIR_FREE(nodes);
963
    VIR_FREE(str);
J
Jiri Denemark 已提交
964
    return feature;
965

J
Jiri Denemark 已提交
966 967 968 969
 error:
    x86FeatureFree(feature);
    feature = NULL;
    goto cleanup;
J
Jiri Denemark 已提交
970 971 972
}


J
Jiri Denemark 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
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;
}

1000

J
Jiri Denemark 已提交
1001
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
1002 1003
x86ModelNew(void)
{
J
Jiri Denemark 已提交
1004
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
1005 1006 1007 1008 1009 1010 1011 1012

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

    return model;
}


J
Jiri Denemark 已提交
1013
static void
J
Jiri Denemark 已提交
1014
x86ModelFree(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
1015
{
1016
    if (!model)
J
Jiri Denemark 已提交
1017 1018 1019
        return;

    VIR_FREE(model->name);
1020
    virCPUx86DataClear(&model->data);
J
Jiri Denemark 已提交
1021 1022 1023 1024
    VIR_FREE(model);
}


J
Jiri Denemark 已提交
1025 1026
static virCPUx86ModelPtr
x86ModelCopy(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
1027
{
J
Jiri Denemark 已提交
1028
    virCPUx86ModelPtr copy;
J
Jiri Denemark 已提交
1029

1030 1031
    if (VIR_ALLOC(copy) < 0 ||
        VIR_STRDUP(copy->name, model->name) < 0 ||
1032
        x86DataCopy(&copy->data, &model->data) < 0) {
J
Jiri Denemark 已提交
1033 1034 1035 1036
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
1037
    copy->vendor = model->vendor;
1038
    copy->signature = model->signature;
J
Jiri Denemark 已提交
1039 1040 1041 1042 1043

    return copy;
}


J
Jiri Denemark 已提交
1044
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
1045
x86ModelFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
1046 1047
             const char *name)
{
1048
    size_t i;
J
Jiri Denemark 已提交
1049

1050 1051 1052
    for (i = 0; i < map->nmodels; i++) {
        if (STREQ(map->models[i]->name, name))
            return map->models[i];
J
Jiri Denemark 已提交
1053 1054 1055 1056 1057 1058
    }

    return NULL;
}


1059 1060 1061 1062 1063 1064 1065 1066 1067
/*
 * Computes CPU model data from a CPU definition associated with features
 * matching @policy. If @policy equals -1, the computed model will describe
 * all CPU features, i.e., it will contain:
 *
 *      features from model
 *      + required and forced features
 *      - disabled and forbidden features
 */
J
Jiri Denemark 已提交
1068
static virCPUx86ModelPtr
1069
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
1070
                virCPUx86MapPtr map,
J
Jiri Denemark 已提交
1071 1072
                int policy)
{
J
Jiri Denemark 已提交
1073
    virCPUx86ModelPtr model = NULL;
1074
    size_t i;
J
Jiri Denemark 已提交
1075

1076 1077 1078 1079
    /* host CPU only contains required features; requesting other features
     * just returns an empty model
     */
    if (cpu->type == VIR_CPU_TYPE_HOST &&
1080 1081
        policy != VIR_CPU_FEATURE_REQUIRE &&
        policy != -1)
1082 1083
        return x86ModelNew();

J
Jiri Denemark 已提交
1084 1085
    if (cpu->model &&
        (policy == VIR_CPU_FEATURE_REQUIRE || policy == -1)) {
1086
        if (!(model = x86ModelFind(map, cpu->model))) {
1087 1088
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU model %s"), cpu->model);
1089
            return NULL;
J
Jiri Denemark 已提交
1090 1091
        }

1092 1093 1094
        model = x86ModelCopy(model);
    } else {
        model = x86ModelNew();
J
Jiri Denemark 已提交
1095
    }
J
Jiri Denemark 已提交
1096

1097 1098 1099
    if (!model)
        return NULL;

J
Jiri Denemark 已提交
1100
    for (i = 0; i < cpu->nfeatures; i++) {
1101
        virCPUx86FeaturePtr feature;
1102
        virCPUFeaturePolicy fpol;
J
Jiri Denemark 已提交
1103

1104 1105 1106 1107 1108 1109 1110
        if (cpu->features[i].policy == -1)
            fpol = VIR_CPU_FEATURE_REQUIRE;
        else
            fpol = cpu->features[i].policy;

        if ((policy == -1 && fpol == VIR_CPU_FEATURE_OPTIONAL) ||
            (policy != -1 && fpol != policy))
J
Jiri Denemark 已提交
1111 1112
            continue;

1113
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
1114 1115
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
J
Jiri Denemark 已提交
1116 1117 1118
            goto error;
        }

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
        if (policy == -1) {
            switch (fpol) {
            case VIR_CPU_FEATURE_FORCE:
            case VIR_CPU_FEATURE_REQUIRE:
                if (x86DataAdd(&model->data, &feature->data) < 0)
                    goto error;
                break;

            case VIR_CPU_FEATURE_DISABLE:
            case VIR_CPU_FEATURE_FORBID:
                x86DataSubtract(&model->data, &feature->data);
                break;

            /* coverity[dead_error_condition] */
            case VIR_CPU_FEATURE_OPTIONAL:
            case VIR_CPU_FEATURE_LAST:
                break;
            }
        } else if (x86DataAdd(&model->data, &feature->data) < 0) {
1138
            goto error;
1139
        }
J
Jiri Denemark 已提交
1140 1141 1142 1143
    }

    return model;

1144
 error:
J
Jiri Denemark 已提交
1145 1146 1147 1148 1149
    x86ModelFree(model);
    return NULL;
}


1150
static virCPUx86CompareResult
J
Jiri Denemark 已提交
1151 1152
x86ModelCompare(virCPUx86ModelPtr model1,
                virCPUx86ModelPtr model2)
J
Jiri Denemark 已提交
1153
{
1154
    virCPUx86CompareResult result = EQUAL;
1155 1156
    virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(&model1->data);
    virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(&model2->data);
1157 1158
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
1159

J
Jiri Denemark 已提交
1160
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
1161
        virCPUx86CompareResult match = SUPERSET;
J
Jiri Denemark 已提交
1162

1163
        if ((cpuid2 = x86DataCpuid(&model2->data, cpuid1))) {
J
Jiri Denemark 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
            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 已提交
1176
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
1177
        virCPUx86CompareResult match = SUBSET;
J
Jiri Denemark 已提交
1178

1179
        if ((cpuid1 = x86DataCpuid(&model1->data, cpuid2))) {
J
Jiri Denemark 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
            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 已提交
1196 1197 1198
static virCPUx86ModelPtr
x86ModelParse(xmlXPathContextPtr ctxt,
              virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1199 1200
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
1201
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
1202
    char *vendor = NULL;
1203
    size_t i;
J
Jiri Denemark 已提交
1204 1205
    int n;

J
Jiri Denemark 已提交
1206
    if (!(model = x86ModelNew()))
J
Jiri Denemark 已提交
1207
        goto error;
J
Jiri Denemark 已提交
1208

1209
    model->name = virXPathString("string(@name)", ctxt);
1210
    if (!model->name) {
1211 1212
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
J
Jiri Denemark 已提交
1213
        goto error;
J
Jiri Denemark 已提交
1214 1215
    }

1216
    if (virXPathNode("./model", ctxt)) {
J
Jiri Denemark 已提交
1217
        virCPUx86ModelPtr ancestor;
J
Jiri Denemark 已提交
1218 1219
        char *name;

1220
        name = virXPathString("string(./model/@name)", ctxt);
1221
        if (!name) {
1222 1223 1224
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1225
            goto error;
J
Jiri Denemark 已提交
1226 1227
        }

1228
        if (!(ancestor = x86ModelFind(map, name))) {
1229 1230 1231
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
1232
            VIR_FREE(name);
J
Jiri Denemark 已提交
1233
            goto error;
J
Jiri Denemark 已提交
1234 1235 1236 1237
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1238
        model->vendor = ancestor->vendor;
1239
        model->signature = ancestor->signature;
1240
        if (x86DataCopy(&model->data, &ancestor->data) < 0)
J
Jiri Denemark 已提交
1241
            goto error;
J
Jiri Denemark 已提交
1242 1243
    }

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    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;
        }

1265
        model->signature = x86MakeSignature(sigFamily, sigModel, 0);
1266 1267
    }

1268 1269 1270
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
1271 1272 1273
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1274
            goto error;
1275 1276
        }

J
Jiri Denemark 已提交
1277
        if (!(model->vendor = x86VendorFind(map, vendor))) {
1278 1279 1280
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
J
Jiri Denemark 已提交
1281
            goto error;
J
Jiri Denemark 已提交
1282 1283 1284
        }
    }

1285
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1286
    if (n < 0)
J
Jiri Denemark 已提交
1287
        goto error;
J
Jiri Denemark 已提交
1288 1289

    for (i = 0; i < n; i++) {
1290
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
1291 1292
        char *name;

1293
        if (!(name = virXMLPropString(nodes[i], "name"))) {
1294 1295
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
J
Jiri Denemark 已提交
1296
            goto error;
J
Jiri Denemark 已提交
1297 1298
        }

1299
        if (!(feature = x86FeatureFind(map, name))) {
1300 1301 1302
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1303
            VIR_FREE(name);
J
Jiri Denemark 已提交
1304
            goto error;
J
Jiri Denemark 已提交
1305 1306 1307
        }
        VIR_FREE(name);

1308
        if (x86DataAdd(&model->data, &feature->data))
J
Jiri Denemark 已提交
1309
            goto error;
J
Jiri Denemark 已提交
1310 1311
    }

J
Jiri Denemark 已提交
1312
 cleanup:
J
Jiri Denemark 已提交
1313
    VIR_FREE(vendor);
1314
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    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 已提交
1344 1345 1346 1347
}


static void
J
Jiri Denemark 已提交
1348
x86MapFree(virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1349
{
1350 1351
    size_t i;

1352
    if (!map)
J
Jiri Denemark 已提交
1353 1354
        return;

1355 1356 1357
    for (i = 0; i < map->nfeatures; i++)
        x86FeatureFree(map->features[i]);
    VIR_FREE(map->features);
J
Jiri Denemark 已提交
1358

1359 1360 1361
    for (i = 0; i < map->nmodels; i++)
        x86ModelFree(map->models[i]);
    VIR_FREE(map->models);
J
Jiri Denemark 已提交
1362

1363 1364 1365
    for (i = 0; i < map->nvendors; i++)
        x86VendorFree(map->vendors[i]);
    VIR_FREE(map->vendors);
1366

1367 1368 1369 1370
    /* migrate_blockers only points to the features from map->features list,
     * which were already freed above
     */
    VIR_FREE(map->migrate_blockers);
1371

J
Jiri Denemark 已提交
1372 1373 1374 1375
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1376
static int
1377
x86MapLoadCallback(cpuMapElement element,
J
Jiri Denemark 已提交
1378
                   xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
1379 1380
                   xmlNodePtr *nodes,
                   int n,
J
Jiri Denemark 已提交
1381 1382
                   void *data)
{
J
Jiri Denemark 已提交
1383
    virCPUx86MapPtr map = data;
J
Jiri Denemark 已提交
1384 1385 1386

    switch (element) {
    case CPU_MAP_ELEMENT_VENDOR:
J
Jiri Denemark 已提交
1387
        return x86VendorsLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1388
    case CPU_MAP_ELEMENT_FEATURE:
J
Jiri Denemark 已提交
1389
        return x86FeaturesLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1390
    case CPU_MAP_ELEMENT_MODEL:
J
Jiri Denemark 已提交
1391
        return x86ModelsLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1392 1393 1394 1395 1396 1397 1398 1399
    case CPU_MAP_ELEMENT_LAST:
        break;
    }

    return 0;
}


J
Jiri Denemark 已提交
1400
static virCPUx86MapPtr
1401
virCPUx86LoadMap(void)
J
Jiri Denemark 已提交
1402
{
J
Jiri Denemark 已提交
1403
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1404

1405
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1406 1407
        return NULL;

J
Jiri Denemark 已提交
1408
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1409 1410 1411 1412
        goto error;

    return map;

1413
 error:
J
Jiri Denemark 已提交
1414 1415 1416 1417 1418
    x86MapFree(map);
    return NULL;
}


1419
int
1420
virCPUx86DriverOnceInit(void)
1421
{
J
Jiri Denemark 已提交
1422
    if (!(cpuMap = virCPUx86LoadMap()))
1423 1424
        return -1;

1425 1426
    microcodeVersion = virHostCPUGetMicrocodeVersion();

1427 1428 1429 1430
    return 0;
}


J
Jiri Denemark 已提交
1431
static virCPUx86MapPtr
1432 1433
virCPUx86GetMap(void)
{
1434
    if (virCPUx86DriverInitialize() < 0)
1435 1436
        return NULL;

J
Jiri Denemark 已提交
1437
    return cpuMap;
1438 1439 1440
}


1441
static char *
J
Jiri Denemark 已提交
1442
virCPUx86DataFormat(const virCPUData *data)
1443
{
1444
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(&data->data.x86);
1445
    virCPUx86CPUID *cpuid;
1446 1447 1448 1449 1450
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "<cpudata arch='x86'>\n");
    while ((cpuid = x86DataCpuidNext(&iter))) {
        virBufferAsprintf(&buf,
1451
                          "  <cpuid eax_in='0x%08x' ecx_in='0x%08x'"
1452 1453
                          " eax='0x%08x' ebx='0x%08x'"
                          " ecx='0x%08x' edx='0x%08x'/>\n",
1454
                          cpuid->eax_in, cpuid->ecx_in,
1455 1456 1457 1458
                          cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx);
    }
    virBufferAddLit(&buf, "</cpudata>\n");

1459
    if (virBufferCheckError(&buf) < 0)
1460 1461 1462 1463 1464 1465 1466
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
J
Jiri Denemark 已提交
1467
virCPUx86DataParse(xmlXPathContextPtr ctxt)
1468 1469 1470
{
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1471
    virCPUx86CPUID cpuid;
1472 1473 1474
    size_t i;
    int n;

1475
    n = virXPathNodeSet("/cpudata/cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
1476
    if (n <= 0) {
1477 1478
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no x86 CPU data found"));
1479
        goto error;
1480 1481
    }

1482 1483 1484
    if (!(cpuData = virCPUDataNew(VIR_ARCH_X86_64)))
        goto error;

1485 1486 1487 1488 1489
    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);
1490
            goto error;
1491
        }
1492
        if (virCPUx86DataAddCPUID(cpuData, &cpuid) < 0)
1493
            goto error;
1494 1495
    }

1496
 cleanup:
1497 1498
    VIR_FREE(nodes);
    return cpuData;
1499 1500

 error:
J
Jiri Denemark 已提交
1501
    virCPUx86DataFree(cpuData);
1502 1503
    cpuData = NULL;
    goto cleanup;
1504 1505 1506
}


1507 1508 1509
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1510
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1511 1512 1513 1514
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
1515 1516 1517 1518 1519 1520 1521 1522
#define virX86CpuIncompatible(MSG, CPU_DEF) \
        do { \
            char *flagsStr = NULL; \
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) { \
                virReportOOMError(); \
                goto error; \
            } \
            if (message && \
1523
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
1524 1525 1526 1527 1528 1529
                VIR_FREE(flagsStr); \
                goto error; \
            } \
            VIR_DEBUG("%s: %s", MSG, flagsStr); \
            VIR_FREE(flagsStr); \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE; \
1530 1531
        } while (0)

1532

J
Jiri Denemark 已提交
1533 1534 1535
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1536
           virCPUDataPtr *guest,
1537
           char **message)
J
Jiri Denemark 已提交
1538
{
J
Jiri Denemark 已提交
1539
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1540 1541 1542 1543 1544 1545 1546 1547
    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;
1548
    virCPUDataPtr guestData = NULL;
J
Jiri Denemark 已提交
1549
    virCPUCompareResult ret;
1550
    virCPUx86CompareResult result;
J
Jiri Denemark 已提交
1551
    virArch arch;
1552
    size_t i;
J
Jiri Denemark 已提交
1553

1554
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1555 1556 1557
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1558
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1559 1560 1561 1562 1563
                found = true;
                break;
            }
        }

1564
        if (!found) {
1565 1566
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1567 1568 1569
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1570
                            virArchToString(cpu->arch)) < 0)
1571
                goto error;
J
Jiri Denemark 已提交
1572
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1573
        }
J
Jiri Denemark 已提交
1574 1575 1576
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1577 1578
    }

J
Jiri Denemark 已提交
1579 1580 1581 1582
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1583 1584 1585 1586 1587
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1588
            goto error;
1589

J
Jiri Denemark 已提交
1590 1591 1592
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1593
    if (!(map = virCPUx86GetMap()) ||
J
Jiri Denemark 已提交
1594
        !(host_model = x86ModelFromCPU(host, map, -1)) ||
J
Jiri Denemark 已提交
1595 1596 1597 1598 1599
        !(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 已提交
1600 1601
        goto error;

1602 1603
    x86DataIntersect(&cpu_forbid->data, &host_model->data);
    if (!x86DataIsEmpty(&cpu_forbid->data)) {
1604
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
1605
                              &cpu_forbid->data);
1606
        goto cleanup;
J
Jiri Denemark 已提交
1607 1608
    }

1609 1610 1611
    /* first remove features that were inherited from the CPU model and were
     * explicitly forced, disabled, or made optional
     */
1612 1613 1614
    x86DataSubtract(&cpu_require->data, &cpu_force->data);
    x86DataSubtract(&cpu_require->data, &cpu_optional->data);
    x86DataSubtract(&cpu_require->data, &cpu_disable->data);
J
Jiri Denemark 已提交
1615 1616
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1617
        x86DataSubtract(&cpu_require->data, &host_model->data);
1618 1619
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
1620
                              &cpu_require->data);
1621
        goto cleanup;
J
Jiri Denemark 已提交
1622 1623 1624 1625
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

1626
    if (!(diff = x86ModelCopy(host_model)))
1627
        goto error;
J
Jiri Denemark 已提交
1628

1629 1630 1631 1632
    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 已提交
1633

1634
    if (!x86DataIsEmpty(&diff->data))
J
Jiri Denemark 已提交
1635
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1636 1637 1638 1639

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1640 1641
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
1642
                              &diff->data);
1643
        goto cleanup;
J
Jiri Denemark 已提交
1644 1645
    }

1646 1647
    if (guest) {
        if (!(guest_model = x86ModelCopy(host_model)))
1648
            goto error;
J
Jiri Denemark 已提交
1649

1650
        if (cpu->vendor && host_model->vendor &&
1651 1652
            virCPUx86DataAddCPUIDInt(&guest_model->data,
                                     &host_model->vendor->cpuid) < 0)
1653 1654
            goto error;

1655 1656 1657
        if (x86DataAddSignature(&guest_model->data, host_model->signature) < 0)
            goto error;

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

1662
        if (x86DataAdd(&guest_model->data, &cpu_force->data))
1663
            goto error;
J
Jiri Denemark 已提交
1664

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

1667 1668
        if (!(guestData = virCPUDataNew(arch)) ||
            x86DataCopy(&guestData->data.x86, &guest_model->data) < 0)
1669
            goto error;
1670 1671

        *guest = guestData;
J
Jiri Denemark 已提交
1672 1673
    }

1674
 cleanup:
J
Jiri Denemark 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
    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;

1686
 error:
J
Jiri Denemark 已提交
1687
    virCPUx86DataFree(guestData);
J
Jiri Denemark 已提交
1688
    ret = VIR_CPU_COMPARE_ERROR;
1689
    goto cleanup;
J
Jiri Denemark 已提交
1690
}
1691
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1692 1693 1694


static virCPUCompareResult
J
Jiri Denemark 已提交
1695 1696 1697
virCPUx86Compare(virCPUDefPtr host,
                 virCPUDefPtr cpu,
                 bool failIncompatible)
J
Jiri Denemark 已提交
1698
{
J
Jiri Denemark 已提交
1699 1700 1701
    virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR;
    virCPUx86MapPtr map;
    virCPUx86ModelPtr model = NULL;
1702 1703
    char *message = NULL;

J
Jiri Denemark 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
    if (!host || !host->model) {
        if (failIncompatible) {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s",
                           _("unknown host CPU"));
        } else {
            VIR_WARN("unknown host CPU");
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;
        }
        goto cleanup;
    }

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

J
Jiri Denemark 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 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
    if (ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
        bool noTSX = false;

        if (STREQ_NULLABLE(cpu->model, "Haswell") ||
            STREQ_NULLABLE(cpu->model, "Broadwell")) {
            if (!(map = virCPUx86GetMap()))
                goto cleanup;

            if (!(model = x86ModelFromCPU(cpu, map, -1)))
                goto cleanup;

            noTSX = !x86FeatureInData("hle", &model->data, map) ||
                    !x86FeatureInData("rtm", &model->data, map);
        }

        if (failIncompatible) {
            ret = VIR_CPU_COMPARE_ERROR;
            if (message) {
                if (noTSX) {
                    virReportError(VIR_ERR_CPU_INCOMPATIBLE,
                                   _("%s; try using '%s-noTSX' CPU model"),
                                   message, cpu->model);
                } else {
                    virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s", message);
                }
            } else {
                if (noTSX) {
                    virReportError(VIR_ERR_CPU_INCOMPATIBLE,
                                   _("try using '%s-noTSX' CPU model"),
                                   cpu->model);
                } else {
                    virReportError(VIR_ERR_CPU_INCOMPATIBLE, NULL);
                }
            }
1751 1752 1753
        }
    }

J
Jiri Denemark 已提交
1754 1755 1756
 cleanup:
    VIR_FREE(message);
    x86ModelFree(model);
1757
    return ret;
J
Jiri Denemark 已提交
1758 1759 1760
}


1761
/*
1762 1763
 * Checks whether a candidate model is a better fit for the CPU data than the
 * current model.
1764
 *
1765 1766 1767
 * Returns 0 if current is better,
 *         1 if candidate is better,
 *         2 if candidate is the best one (search should stop now).
1768 1769
 */
static int
1770 1771 1772
x86DecodeUseCandidate(virCPUx86ModelPtr current,
                      virCPUDefPtr cpuCurrent,
                      virCPUx86ModelPtr candidate,
1773
                      virCPUDefPtr cpuCandidate,
1774
                      uint32_t signature,
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
                      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;
        }
    }

1787 1788
    if (preferred && STREQ(cpuCandidate->model, preferred)) {
        VIR_DEBUG("%s is the preferred model", cpuCandidate->model);
1789
        return 2;
1790
    }
1791

1792 1793
    if (!cpuCurrent) {
        VIR_DEBUG("%s is better than nothing", cpuCandidate->model);
1794
        return 1;
1795
    }
1796

1797 1798 1799 1800 1801 1802
    /* 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 &&
1803 1804 1805
        candidate->signature != signature) {
        VIR_DEBUG("%s differs in signature from matching %s",
                  cpuCandidate->model, cpuCurrent->model);
1806
        return 0;
1807
    }
1808

1809 1810 1811
    if (cpuCurrent->nfeatures > cpuCandidate->nfeatures) {
        VIR_DEBUG("%s results in shorter feature list than %s",
                  cpuCandidate->model, cpuCurrent->model);
1812
        return 1;
1813
    }
1814

1815 1816 1817 1818 1819
    /* Prefer a candidate with matching signature even though it would
     * result in longer list of features.
     */
    if (signature &&
        candidate->signature == signature &&
1820 1821
        current->signature != signature) {
        VIR_DEBUG("%s provides matching signature", cpuCandidate->model);
1822
        return 1;
1823
    }
1824

1825 1826
    VIR_DEBUG("%s does not result in shorter feature list than %s",
              cpuCandidate->model, cpuCurrent->model);
1827 1828 1829 1830
    return 0;
}


1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
/**
 * Drop broken TSX features.
 */
static void
x86DataFilterTSX(virCPUx86Data *data,
                 virCPUx86VendorPtr vendor,
                 virCPUx86MapPtr map)
{
    unsigned int family;
    unsigned int model;
    unsigned int stepping;

    if (!vendor || STRNEQ(vendor->name, "Intel"))
        return;

    x86DataToSignatureFull(data, &family, &model, &stepping);

    if (family == 6 &&
        ((model == 63 && stepping < 4) ||
         model == 60 ||
         model == 69 ||
         model == 70)) {
        virCPUx86FeaturePtr feature;

        VIR_DEBUG("Dropping broken TSX");

        if ((feature = x86FeatureFind(map, "hle")))
            x86DataSubtract(data, &feature->data);

        if ((feature = x86FeatureFind(map, "rtm")))
            x86DataSubtract(data, &feature->data);
    }
}


J
Jiri Denemark 已提交
1866 1867
static int
x86Decode(virCPUDefPtr cpu,
1868
          const virCPUx86Data *cpuData,
1869
          virDomainCapsCPUModelsPtr models,
1870
          const char *preferred,
1871
          bool migratable)
J
Jiri Denemark 已提交
1872 1873
{
    int ret = -1;
J
Jiri Denemark 已提交
1874
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1875
    virCPUx86ModelPtr candidate;
1876
    virCPUDefPtr cpuCandidate;
1877
    virCPUx86ModelPtr model = NULL;
1878
    virCPUDefPtr cpuModel = NULL;
1879
    virCPUx86Data data = VIR_CPU_X86_DATA_INIT;
1880 1881
    virCPUx86Data copy = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data features = VIR_CPU_X86_DATA_INIT;
J
Jiri Denemark 已提交
1882
    virCPUx86VendorPtr vendor;
1883
    virDomainCapsCPUModelPtr hvModel = NULL;
1884
    uint32_t signature;
1885
    ssize_t i;
1886
    int rc;
J
Jiri Denemark 已提交
1887

1888
    if (!cpuData || x86DataCopy(&data, cpuData) < 0)
J
Jiri Denemark 已提交
1889 1890
        return -1;

1891 1892 1893 1894 1895 1896 1897
    if (!(map = virCPUx86GetMap()))
        goto cleanup;

    vendor = x86DataToVendor(&data, map);
    signature = x86DataToSignature(&data);

    x86DataFilterTSX(&data, vendor, map);
J
Jiri Denemark 已提交
1898

1899 1900 1901 1902 1903
    /* 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];
1904 1905
        if (models &&
            !(hvModel = virDomainCapsCPUModelsGet(models, candidate->name))) {
1906 1907
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1908 1909 1910
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
J
Jiri Denemark 已提交
1911
                    goto cleanup;
1912 1913 1914 1915 1916 1917 1918 1919 1920
                } 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);
            }
1921
            continue;
J
Jiri Denemark 已提交
1922 1923
        }

J
Jiri Denemark 已提交
1924 1925 1926 1927
        /* 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 已提交
1928
            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
J
Jiri Denemark 已提交
1929
                      candidate->vendor->name, candidate->name, vendor->name);
1930
            continue;
J
Jiri Denemark 已提交
1931 1932
        }

1933
        if (!(cpuCandidate = x86DataToCPU(&data, candidate, map, hvModel)))
J
Jiri Denemark 已提交
1934 1935 1936
            goto cleanup;
        cpuCandidate->type = cpu->type;

1937 1938 1939
        if ((rc = x86DecodeUseCandidate(model, cpuModel,
                                        candidate, cpuCandidate,
                                        signature, preferred,
1940
                                        cpu->type == VIR_CPU_TYPE_HOST))) {
1941 1942
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1943
            model = candidate;
1944 1945
            if (rc == 2)
                break;
1946
        } else {
1947
            virCPUDefFree(cpuCandidate);
1948
        }
J
Jiri Denemark 已提交
1949 1950
    }

1951
    if (!cpuModel) {
1952 1953
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1954
        goto cleanup;
J
Jiri Denemark 已提交
1955 1956
    }

1957 1958 1959
    /* Remove non-migratable features if requested
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
1960
    if (migratable) {
1961 1962 1963 1964 1965
        i = 0;
        while (i < cpuModel->nfeatures) {
            if (x86FeatureIsMigratable(cpuModel->features[i].name, map)) {
                i++;
            } else {
1966 1967 1968
                VIR_FREE(cpuModel->features[i].name);
                VIR_DELETE_ELEMENT_INPLACE(cpuModel->features, i,
                                           cpuModel->nfeatures);
1969 1970 1971 1972
            }
        }
    }

J
Jiri Denemark 已提交
1973 1974 1975
    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
        goto cleanup;

1976 1977
    VIR_STEAL_PTR(cpu->model, cpuModel->model);
    VIR_STEAL_PTR(cpu->features, cpuModel->features);
1978
    cpu->nfeatures = cpuModel->nfeatures;
1979 1980 1981
    cpuModel->nfeatures = 0;
    cpu->nfeatures_max = cpuModel->nfeatures_max;
    cpuModel->nfeatures_max = 0;
J
Jiri Denemark 已提交
1982 1983 1984

    ret = 0;

J
Jiri Denemark 已提交
1985
 cleanup:
1986
    virCPUDefFree(cpuModel);
1987
    virCPUx86DataClear(&data);
1988 1989
    virCPUx86DataClear(&copy);
    virCPUx86DataClear(&features);
J
Jiri Denemark 已提交
1990 1991 1992
    return ret;
}

1993 1994
static int
x86DecodeCPUData(virCPUDefPtr cpu,
1995
                 const virCPUData *data,
1996
                 virDomainCapsCPUModelsPtr models)
1997
{
1998
    return x86Decode(cpu, &data->data.x86, models, NULL, false);
1999
}
J
Jiri Denemark 已提交
2000

2001

2002 2003 2004
static int
x86EncodePolicy(virCPUx86Data *data,
                const virCPUDef *cpu,
J
Jiri Denemark 已提交
2005
                virCPUx86MapPtr map,
2006
                virCPUFeaturePolicy policy)
J
Jiri Denemark 已提交
2007
{
J
Jiri Denemark 已提交
2008
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
2009 2010

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

2013 2014 2015
    *data = model->data;
    model->data.len = 0;
    model->data.data = NULL;
J
Jiri Denemark 已提交
2016 2017
    x86ModelFree(model);

2018
    return 0;
J
Jiri Denemark 已提交
2019 2020 2021 2022
}


static int
J
Jiri Denemark 已提交
2023
x86Encode(virArch arch,
2024
          const virCPUDef *cpu,
2025 2026 2027 2028 2029 2030
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
2031
{
J
Jiri Denemark 已提交
2032
    virCPUx86MapPtr map = NULL;
2033 2034 2035 2036 2037 2038
    virCPUDataPtr data_forced = NULL;
    virCPUDataPtr data_required = NULL;
    virCPUDataPtr data_optional = NULL;
    virCPUDataPtr data_disabled = NULL;
    virCPUDataPtr data_forbidden = NULL;
    virCPUDataPtr data_vendor = NULL;
J
Jiri Denemark 已提交
2039

2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

2053
    if (!(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
2054 2055
        goto error;

2056
    if (forced &&
2057 2058 2059
        (!(data_forced = virCPUDataNew(arch)) ||
         x86EncodePolicy(&data_forced->data.x86, cpu, map,
                         VIR_CPU_FEATURE_FORCE) < 0))
2060
        goto error;
J
Jiri Denemark 已提交
2061

2062
    if (required &&
2063 2064 2065
        (!(data_required = virCPUDataNew(arch)) ||
         x86EncodePolicy(&data_required->data.x86, cpu, map,
                         VIR_CPU_FEATURE_REQUIRE) < 0))
2066
        goto error;
J
Jiri Denemark 已提交
2067

2068
    if (optional &&
2069 2070 2071
        (!(data_optional = virCPUDataNew(arch)) ||
         x86EncodePolicy(&data_optional->data.x86, cpu, map,
                         VIR_CPU_FEATURE_OPTIONAL) < 0))
2072
        goto error;
J
Jiri Denemark 已提交
2073

2074
    if (disabled &&
2075 2076 2077
        (!(data_disabled = virCPUDataNew(arch)) ||
         x86EncodePolicy(&data_disabled->data.x86, cpu, map,
                         VIR_CPU_FEATURE_DISABLE) < 0))
2078
        goto error;
J
Jiri Denemark 已提交
2079

2080
    if (forbidden &&
2081 2082 2083
        (!(data_forbidden = virCPUDataNew(arch)) ||
         x86EncodePolicy(&data_forbidden->data.x86, cpu, map,
                         VIR_CPU_FEATURE_FORBID) < 0))
2084
        goto error;
J
Jiri Denemark 已提交
2085

J
Jiri Denemark 已提交
2086
    if (vendor) {
2087
        virCPUx86VendorPtr v = NULL;
J
Jiri Denemark 已提交
2088 2089

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
2090 2091
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s not found"), cpu->vendor);
J
Jiri Denemark 已提交
2092 2093 2094
            goto error;
        }

2095
        if (!(data_vendor = virCPUDataNew(arch)))
J
Jiri Denemark 已提交
2096 2097
            goto error;

2098
        if (v && virCPUx86DataAddCPUID(data_vendor, &v->cpuid) < 0)
2099 2100
            goto error;
    }
J
Jiri Denemark 已提交
2101

2102
    if (forced)
2103
        *forced = data_forced;
2104
    if (required)
2105
        *required = data_required;
2106
    if (optional)
2107
        *optional = data_optional;
2108
    if (disabled)
2109
        *disabled = data_disabled;
2110
    if (forbidden)
2111
        *forbidden = data_forbidden;
2112
    if (vendor)
2113 2114 2115 2116 2117
        *vendor = data_vendor;

    return 0;

 error:
J
Jiri Denemark 已提交
2118 2119 2120 2121 2122 2123
    virCPUx86DataFree(data_forced);
    virCPUx86DataFree(data_required);
    virCPUx86DataFree(data_optional);
    virCPUx86DataFree(data_disabled);
    virCPUx86DataFree(data_forbidden);
    virCPUx86DataFree(data_vendor);
2124
    return -1;
J
Jiri Denemark 已提交
2125 2126 2127
}


P
Pavel Hrdina 已提交
2128
#if defined(__i386__) || defined(__x86_64__)
J
Jiri Denemark 已提交
2129
static inline void
2130
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
2131
{
2132
# if __x86_64__
2133
    asm("xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
2134
        "xor %%edx, %%edx;" /* functions may use them as additional arguments */
2135
        "cpuid;"
J
Jiri Denemark 已提交
2136 2137 2138 2139
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
2140 2141
        : "a" (cpuid->eax_in),
          "c" (cpuid->ecx_in));
2142
# else
J
Jiri Denemark 已提交
2143 2144 2145 2146
    /* 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;"
2147
        "xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
2148
        "xor %%edx, %%edx;" /* functions may use them as additional arguments */
J
Jiri Denemark 已提交
2149 2150 2151 2152 2153 2154 2155
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
2156 2157
        : "a" (cpuid->eax_in),
          "c" (cpuid->ecx_in)
J
Jiri Denemark 已提交
2158
        : "cc");
2159
# endif
J
Jiri Denemark 已提交
2160 2161 2162
}


2163 2164 2165 2166 2167
/* Leaf 0x04: deterministic cache parameters
 *
 * Sub leaf n+1 is invalid if eax[4:0] in sub leaf n equals 0.
 */
static int
2168
cpuidSetLeaf4(virCPUDataPtr data,
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
              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
2191
cpuidSetLeaf7(virCPUDataPtr data,
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
              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
2218
cpuidSetLeafB(virCPUDataPtr data,
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
              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
2242
cpuidSetLeafD(virCPUDataPtr data,
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 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
              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
2289
cpuidSetLeafResID(virCPUDataPtr data,
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
                  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
2317
cpuidSetLeaf12(virCPUDataPtr data,
2318 2319 2320 2321 2322
               virCPUx86CPUID *subLeaf0)
{
    virCPUx86CPUID cpuid = { .eax_in = 0x7 };
    virCPUx86CPUID *cpuid7;

2323
    if (!(cpuid7 = x86DataCpuid(&data->data.x86, &cpuid)) ||
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
        !(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
2353
cpuidSetLeaf14(virCPUDataPtr data,
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
               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
2378
cpuidSetLeaf17(virCPUDataPtr data,
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
               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 已提交
2400
static int
2401
cpuidSet(uint32_t base, virCPUDataPtr data)
J
Jiri Denemark 已提交
2402
{
2403
    int rc;
J
Jiri Denemark 已提交
2404
    uint32_t max;
2405
    uint32_t leaf;
2406
    virCPUx86CPUID cpuid = { .eax_in = base };
J
Jiri Denemark 已提交
2407 2408

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

2411 2412
    for (leaf = base; leaf <= max; leaf++) {
        cpuid.eax_in = leaf;
2413
        cpuid.ecx_in = 0;
J
Jiri Denemark 已提交
2414
        cpuidCall(&cpuid);
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440

        /* 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)
2441
            return -1;
J
Jiri Denemark 已提交
2442 2443
    }

2444
    return 0;
J
Jiri Denemark 已提交
2445 2446 2447
}


2448
static int
2449
virCPUx86GetHost(virCPUDefPtr cpu,
2450
                 virDomainCapsCPUModelsPtr models)
J
Jiri Denemark 已提交
2451
{
2452
    virCPUDataPtr cpuData = NULL;
2453
    int ret = -1;
J
Jiri Denemark 已提交
2454

2455 2456 2457
    if (virCPUx86DriverInitialize() < 0)
        goto cleanup;

2458 2459
    if (!(cpuData = virCPUDataNew(archs[0])))
        goto cleanup;
J
Jiri Denemark 已提交
2460

2461 2462 2463
    if (cpuidSet(CPUX86_BASIC, cpuData) < 0 ||
        cpuidSet(CPUX86_EXTENDED, cpuData) < 0)
        goto cleanup;
2464

2465
    ret = x86DecodeCPUData(cpu, cpuData, models);
2466
    cpu->microcodeVersion = microcodeVersion;
J
Jiri Denemark 已提交
2467

2468
 cleanup:
J
Jiri Denemark 已提交
2469
    virCPUx86DataFree(cpuData);
2470
    return ret;
J
Jiri Denemark 已提交
2471 2472 2473 2474
}
#endif


2475
static virCPUDefPtr
2476 2477 2478
virCPUx86Baseline(virCPUDefPtr *cpus,
                  unsigned int ncpus,
                  virDomainCapsCPUModelsPtr models,
2479
                  const char **features,
2480
                  bool migratable)
2481
{
J
Jiri Denemark 已提交
2482
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
2483
    virCPUx86ModelPtr base_model = NULL;
2484
    virCPUDefPtr cpu = NULL;
2485
    size_t i;
2486
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
2487
    virCPUx86ModelPtr model = NULL;
2488
    bool outputVendor = true;
2489 2490
    const char *modelName;
    bool matchingNames = true;
2491
    virCPUDataPtr featData = NULL;
2492

2493
    if (!(map = virCPUx86GetMap()))
2494 2495
        goto error;

2496
    if (!(base_model = x86ModelFromCPU(cpus[0], map, -1)))
2497 2498
        goto error;

2499
    if (VIR_ALLOC(cpu) < 0)
2500
        goto error;
2501

2502 2503
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
2504

2505
    if (!cpus[0]->vendor) {
2506
        outputVendor = false;
2507
    } else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
2508 2509
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Unknown CPU vendor %s"), cpus[0]->vendor);
J
Jiri Denemark 已提交
2510 2511 2512
        goto error;
    }

2513
    modelName = cpus[0]->model;
2514
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
2515 2516
        const char *vn = NULL;

2517 2518 2519 2520 2521 2522 2523 2524 2525
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

2526
        if (!(model = x86ModelFromCPU(cpus[i], map, -1)))
2527 2528
            goto error;

J
Jiri Denemark 已提交
2529 2530
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
2531 2532 2533
            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 已提交
2534 2535 2536
            goto error;
        }

2537
        if (cpus[i]->vendor) {
J
Jiri Denemark 已提交
2538
            vn = cpus[i]->vendor;
2539
        } else {
2540 2541 2542 2543
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
2544 2545 2546 2547

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
2548 2549
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("Unknown CPU vendor %s"), vn);
J
Jiri Denemark 已提交
2550 2551 2552
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
2553 2554
                virReportError(VIR_ERR_OPERATION_FAILED,
                               "%s", _("CPU vendors do not match"));
J
Jiri Denemark 已提交
2555 2556 2557 2558
                goto error;
            }
        }

2559
        x86DataIntersect(&base_model->data, &model->data);
2560
        x86ModelFree(model);
J
Jiri Denemark 已提交
2561
        model = NULL;
2562 2563
    }

2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    if (features) {
        virCPUx86FeaturePtr feat;

        if (!(featData = virCPUDataNew(archs[0])))
            goto cleanup;

        for (i = 0; features[i]; i++) {
            if ((feat = x86FeatureFind(map, features[i])) &&
                x86DataAdd(&featData->data.x86, &feat->data) < 0)
                goto cleanup;
        }

        x86DataIntersect(&base_model->data, &featData->data.x86);
    }

2579
    if (x86DataIsEmpty(&base_model->data)) {
2580 2581
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
2582 2583 2584
        goto error;
    }

2585 2586
    if (vendor &&
        virCPUx86DataAddCPUIDInt(&base_model->data, &vendor->cpuid) < 0)
2587
        goto error;
J
Jiri Denemark 已提交
2588

2589
    if (x86Decode(cpu, &base_model->data, models, modelName, migratable) < 0)
2590 2591
        goto error;

2592 2593 2594
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2595 2596 2597
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2598
 cleanup:
2599
    x86ModelFree(base_model);
2600
    virCPUx86DataFree(featData);
2601 2602 2603

    return cpu;

2604
 error:
J
Jiri Denemark 已提交
2605
    x86ModelFree(model);
2606 2607 2608 2609 2610 2611
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


2612
static int
J
Jiri Denemark 已提交
2613
x86UpdateHostModel(virCPUDefPtr guest,
2614
                   const virCPUDef *host)
2615
{
J
Jiri Denemark 已提交
2616
    virCPUDefPtr updated = NULL;
2617
    size_t i;
J
Jiri Denemark 已提交
2618
    int ret = -1;
2619

J
Jiri Denemark 已提交
2620
    if (!(updated = virCPUDefCopyWithoutModel(host)))
2621 2622
        goto cleanup;

J
Jiri Denemark 已提交
2623 2624
    updated->type = VIR_CPU_TYPE_GUEST;
    updated->mode = VIR_CPU_MODE_CUSTOM;
2625
    if (virCPUDefCopyModel(updated, host, true) < 0)
J
Jiri Denemark 已提交
2626
        goto cleanup;
2627

J
Jiri Denemark 已提交
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
    if (guest->vendor_id) {
        VIR_FREE(updated->vendor_id);
        if (VIR_STRDUP(updated->vendor_id, guest->vendor_id) < 0)
            goto cleanup;
    }

    for (i = 0; i < guest->nfeatures; i++) {
        if (virCPUDefUpdateFeature(updated,
                                   guest->features[i].name,
                                   guest->features[i].policy) < 0)
2638 2639 2640
            goto cleanup;
    }

2641 2642
    virCPUDefStealModel(guest, updated,
                        guest->mode == VIR_CPU_MODE_CUSTOM);
J
Jiri Denemark 已提交
2643 2644
    guest->mode = VIR_CPU_MODE_CUSTOM;
    guest->match = VIR_CPU_MATCH_EXACT;
2645 2646
    ret = 0;

2647
 cleanup:
J
Jiri Denemark 已提交
2648
    virCPUDefFree(updated);
2649 2650 2651
    return ret;
}

2652 2653

static int
J
Jiri Denemark 已提交
2654 2655
virCPUx86Update(virCPUDefPtr guest,
                const virCPUDef *host)
2656
{
J
Jiri Denemark 已提交
2657
    virCPUx86ModelPtr model = NULL;
J
Jiri Denemark 已提交
2658
    virCPUx86MapPtr map;
2659
    int ret = -1;
J
Jiri Denemark 已提交
2660
    size_t i;
2661

J
Jiri Denemark 已提交
2662 2663 2664 2665 2666
    if (!host) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unknown host CPU model"));
        return -1;
    }
2667

J
Jiri Denemark 已提交
2668 2669
    if (!(map = virCPUx86GetMap()))
        return -1;
2670

J
Jiri Denemark 已提交
2671
    if (!(model = x86ModelFromCPU(host, map, -1)))
2672
        goto cleanup;
2673

J
Jiri Denemark 已提交
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
    for (i = 0; i < guest->nfeatures; i++) {
        if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
            int supported = x86FeatureInData(guest->features[i].name,
                                             &model->data, map);
            if (supported < 0)
                goto cleanup;
            else if (supported)
                guest->features[i].policy = VIR_CPU_FEATURE_REQUIRE;
            else
                guest->features[i].policy = VIR_CPU_FEATURE_DISABLE;
2684 2685
        }
    }
2686

J
Jiri Denemark 已提交
2687 2688
    if (guest->mode == VIR_CPU_MODE_HOST_MODEL ||
        guest->match == VIR_CPU_MATCH_MINIMUM)
2689
        ret = x86UpdateHostModel(guest, host);
J
Jiri Denemark 已提交
2690 2691
    else
        ret = 0;
2692 2693

 cleanup:
J
Jiri Denemark 已提交
2694
    x86ModelFree(model);
2695
    return ret;
2696 2697 2698
}


2699 2700 2701 2702 2703 2704 2705 2706 2707
static int
virCPUx86UpdateLive(virCPUDefPtr cpu,
                    virCPUDataPtr dataEnabled,
                    virCPUDataPtr dataDisabled)
{
    virCPUx86MapPtr map;
    virCPUx86ModelPtr model = NULL;
    virCPUx86Data enabled = VIR_CPU_X86_DATA_INIT;
    virCPUx86Data disabled = VIR_CPU_X86_DATA_INIT;
2708 2709 2710 2711
    virBuffer bufAdded = VIR_BUFFER_INITIALIZER;
    virBuffer bufRemoved = VIR_BUFFER_INITIALIZER;
    char *added = NULL;
    char *removed = NULL;
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
    size_t i;
    int ret = -1;

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

    if (!(model = x86ModelFromCPU(cpu, map, -1)))
        goto cleanup;

    if (dataEnabled &&
        x86DataCopy(&enabled, &dataEnabled->data.x86) < 0)
        goto cleanup;

    if (dataDisabled &&
        x86DataCopy(&disabled, &dataDisabled->data.x86) < 0)
        goto cleanup;

    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];

2732 2733
        if (x86DataIsSubset(&enabled, &feature->data) &&
            !x86DataIsSubset(&model->data, &feature->data)) {
2734 2735 2736 2737 2738
            VIR_DEBUG("Feature '%s' enabled by the hypervisor", feature->name);
            if (cpu->check == VIR_CPU_CHECK_FULL)
                virBufferAsprintf(&bufAdded, "%s,", feature->name);
            else if (virCPUDefUpdateFeature(cpu, feature->name,
                                            VIR_CPU_FEATURE_REQUIRE) < 0)
2739 2740 2741
                goto cleanup;
        }

2742 2743 2744
        if (x86DataIsSubset(&disabled, &feature->data) ||
            (x86DataIsSubset(&model->data, &feature->data) &&
             !x86DataIsSubset(&enabled, &feature->data))) {
2745 2746 2747 2748 2749
            VIR_DEBUG("Feature '%s' disabled by the hypervisor", feature->name);
            if (cpu->check == VIR_CPU_CHECK_FULL)
                virBufferAsprintf(&bufRemoved, "%s,", feature->name);
            else if (virCPUDefUpdateFeature(cpu, feature->name,
                                            VIR_CPU_FEATURE_DISABLE) < 0)
2750 2751 2752 2753
                goto cleanup;
        }
    }

2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
    virBufferTrim(&bufAdded, ",", -1);
    virBufferTrim(&bufRemoved, ",", -1);

    if (virBufferCheckError(&bufAdded) < 0 ||
        virBufferCheckError(&bufRemoved) < 0)
        goto cleanup;

    added = virBufferContentAndReset(&bufAdded);
    removed = virBufferContentAndReset(&bufRemoved);

    if (added || removed) {
        if (added && removed)
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("guest CPU doesn't match specification: "
                             "extra features: %s, missing features: %s"),
                           added, removed);
        else if (added)
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("guest CPU doesn't match specification: "
                             "extra features: %s"),
                           added);
        else
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("guest CPU doesn't match specification: "
                             "missing features: %s"),
                           removed);
        goto cleanup;
    }

    if (cpu->check == VIR_CPU_CHECK_FULL &&
        !x86DataIsEmpty(&disabled)) {
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("guest CPU doesn't match specification"));
        goto cleanup;
    }

2790 2791 2792 2793 2794 2795
    ret = 0;

 cleanup:
    x86ModelFree(model);
    virCPUx86DataClear(&enabled);
    virCPUx86DataClear(&disabled);
2796 2797 2798 2799
    VIR_FREE(added);
    VIR_FREE(removed);
    virBufferFreeAndReset(&bufAdded);
    virBufferFreeAndReset(&bufRemoved);
2800 2801 2802 2803
    return ret;
}


2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
static int
virCPUx86CheckFeature(const virCPUDef *cpu,
                      const char *name)
{
    int ret = -1;
    virCPUx86MapPtr map;
    virCPUx86ModelPtr model = NULL;

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

    if (!(model = x86ModelFromCPU(cpu, map, -1)))
        goto cleanup;

    ret = x86FeatureInData(name, &model->data, map);

 cleanup:
    x86ModelFree(model);
    return ret;
}


2826
static int
2827 2828
virCPUx86DataCheckFeature(const virCPUData *data,
                          const char *name)
D
Daniel P. Berrange 已提交
2829
{
J
Jiri Denemark 已提交
2830
    virCPUx86MapPtr map;
D
Daniel P. Berrange 已提交
2831

2832
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2833 2834
        return -1;

J
Jiri Denemark 已提交
2835
    return x86FeatureInData(name, &data->data.x86, map);
D
Daniel P. Berrange 已提交
2836
}
2837

2838
static int
J
Jiri Denemark 已提交
2839
virCPUx86GetModels(char ***models)
2840
{
J
Jiri Denemark 已提交
2841
    virCPUx86MapPtr map;
2842
    size_t i;
2843 2844 2845 2846

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

2847 2848 2849
    if (models) {
        if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
            goto error;
2850

2851 2852
        for (i = 0; i < map->nmodels; i++) {
            if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
2853 2854
                goto error;
        }
2855 2856
    }

2857
    return map->nmodels;
2858 2859

 error:
2860
    if (models) {
2861
        virStringListFree(*models);
2862 2863
        *models = NULL;
    }
2864 2865 2866
    return -1;
}

2867

J
Jiri Denemark 已提交
2868 2869
static int
virCPUx86Translate(virCPUDefPtr cpu,
2870
                   virDomainCapsCPUModelsPtr models)
J
Jiri Denemark 已提交
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
{
    virCPUDefPtr translated = NULL;
    virCPUx86MapPtr map;
    virCPUx86ModelPtr model = NULL;
    size_t i;
    int ret = -1;

    if (!(map = virCPUx86GetMap()))
        goto cleanup;

    if (!(model = x86ModelFromCPU(cpu, map, -1)))
        goto cleanup;

    if (model->vendor &&
2885
        virCPUx86DataAddCPUIDInt(&model->data, &model->vendor->cpuid) < 0)
J
Jiri Denemark 已提交
2886 2887 2888 2889 2890 2891 2892 2893
        goto cleanup;

    if (x86DataAddSignature(&model->data, model->signature) < 0)
        goto cleanup;

    if (!(translated = virCPUDefCopyWithoutModel(cpu)))
        goto cleanup;

2894
    if (x86Decode(translated, &model->data, models, NULL, false) < 0)
J
Jiri Denemark 已提交
2895 2896 2897 2898 2899 2900 2901 2902
        goto cleanup;

    for (i = 0; i < cpu->nfeatures; i++) {
        virCPUFeatureDefPtr f = cpu->features + i;
        if (virCPUDefUpdateFeature(translated, f->name, f->policy) < 0)
            goto cleanup;
    }

2903
    virCPUDefStealModel(cpu, translated, true);
J
Jiri Denemark 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912
    ret = 0;

 cleanup:
    virCPUDefFree(translated);
    x86ModelFree(model);
    return ret;
}


2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
static int
virCPUx86ExpandFeatures(virCPUDefPtr cpu)
{
    virCPUx86MapPtr map;
    virCPUDefPtr expanded = NULL;
    virCPUx86ModelPtr model = NULL;
    bool host = cpu->type == VIR_CPU_TYPE_HOST;
    size_t i;
    int ret = -1;

    if (!(map = virCPUx86GetMap()))
        goto cleanup;

    if (!(expanded = virCPUDefCopy(cpu)))
        goto cleanup;

    virCPUDefFreeFeatures(expanded);

    if (!(model = x86ModelFind(map, cpu->model))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown CPU model %s"), cpu->model);
        goto cleanup;
    }

    if (!(model = x86ModelCopy(model)) ||
        x86DataToCPUFeatures(expanded, host ? -1 : VIR_CPU_FEATURE_REQUIRE,
                             &model->data, map) < 0)
        goto cleanup;

    for (i = 0; i < cpu->nfeatures; i++) {
        virCPUFeatureDefPtr f = cpu->features + i;

        if (!host &&
            f->policy != VIR_CPU_FEATURE_REQUIRE &&
            f->policy != VIR_CPU_FEATURE_DISABLE)
            continue;

        if (virCPUDefUpdateFeature(expanded, f->name, f->policy) < 0)
            goto cleanup;
    }

    virCPUDefFreeModel(cpu);

    ret = virCPUDefCopyModel(cpu, expanded, false);

 cleanup:
    virCPUDefFree(expanded);
    x86ModelFree(model);
    return ret;
}


2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
static virCPUDefPtr
virCPUx86CopyMigratable(virCPUDefPtr cpu)
{
    virCPUDefPtr copy;
    virCPUx86MapPtr map;

    if (!(map = virCPUx86GetMap()))
        return NULL;

    if (!(copy = virCPUDefCopyWithoutModel(cpu)))
        return NULL;

    if (virCPUDefCopyModelFilter(copy, cpu, false,
                                 x86FeatureIsMigratable, map) < 0)
        goto error;

    return copy;

 error:
    virCPUDefFree(copy);
    return NULL;
}


2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
static int
virCPUx86ValidateFeatures(virCPUDefPtr cpu)
{
    virCPUx86MapPtr map;
    size_t i;

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

    for (i = 0; i < cpu->nfeatures; i++) {
        if (!x86FeatureFind(map, cpu->features[i].name)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unknown CPU feature: %s"),
                           cpu->features[i].name);
            return -1;
        }
    }

    return 0;
}


3011 3012 3013 3014 3015 3016 3017 3018
int
virCPUx86DataAddCPUID(virCPUDataPtr cpuData,
                      const virCPUx86CPUID *cpuid)
{
    return virCPUx86DataAddCPUIDInt(&cpuData->data.x86, cpuid);
}


3019 3020 3021
int
virCPUx86DataSetSignature(virCPUDataPtr cpuData,
                          unsigned int family,
3022 3023
                          unsigned int model,
                          unsigned int stepping)
3024
{
3025
    uint32_t signature = x86MakeSignature(family, model, stepping);
3026 3027 3028 3029 3030

    return x86DataAddSignature(&cpuData->data.x86, signature);
}


3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
int
virCPUx86DataSetVendor(virCPUDataPtr cpuData,
                       const char *vendor)
{
    virCPUx86CPUID cpuid = { 0 };

    if (virCPUx86VendorToCPUID(vendor, &cpuid) < 0)
        return -1;

    return virCPUx86DataAddCPUID(cpuData, &cpuid);
}


3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
int
virCPUx86DataAddFeature(virCPUDataPtr cpuData,
                        const char *name)
{
    virCPUx86FeaturePtr feature;
    virCPUx86MapPtr map;

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

    /* ignore unknown features */
    if (!(feature = x86FeatureFind(map, name)) &&
        !(feature = x86FeatureFindInternal(name)))
        return 0;

    if (x86DataAdd(&cpuData->data.x86, &feature->data) < 0)
        return -1;

    return 0;
}


J
Jiri Denemark 已提交
3066 3067 3068 3069
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
J
Jiri Denemark 已提交
3070
    .compare    = virCPUx86Compare,
3071
    .decode     = x86DecodeCPUData,
J
Jiri Denemark 已提交
3072
    .encode     = x86Encode,
J
Jiri Denemark 已提交
3073
    .dataFree   = virCPUx86DataFree,
P
Pavel Hrdina 已提交
3074
#if defined(__i386__) || defined(__x86_64__)
3075
    .getHost    = virCPUx86GetHost,
J
Jiri Denemark 已提交
3076
#endif
3077
    .baseline   = virCPUx86Baseline,
J
Jiri Denemark 已提交
3078
    .update     = virCPUx86Update,
3079
    .updateLive = virCPUx86UpdateLive,
3080
    .checkFeature = virCPUx86CheckFeature,
3081
    .dataCheckFeature = virCPUx86DataCheckFeature,
J
Jiri Denemark 已提交
3082
    .dataFormat = virCPUx86DataFormat,
J
Jiri Denemark 已提交
3083
    .dataParse  = virCPUx86DataParse,
J
Jiri Denemark 已提交
3084
    .getModels  = virCPUx86GetModels,
J
Jiri Denemark 已提交
3085
    .translate  = virCPUx86Translate,
3086
    .expandFeatures = virCPUx86ExpandFeatures,
3087
    .copyMigratable = virCPUx86CopyMigratable,
3088
    .validateFeatures = virCPUx86ValidateFeatures,
J
Jiri Denemark 已提交
3089
};