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

#include <config.h>

#include <stdint.h>

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

#define VIR_FROM_THIS VIR_FROM_CPU

39 40
VIR_LOG_INIT("cpu.cpu_x86");

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

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

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

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

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

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

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

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

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

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

J
Jiri Denemark 已提交
116

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


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


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


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


148
static bool
149 150
x86cpuidMatchMasked(const virCPUx86CPUID *cpuid,
                    const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
151 152 153 154 155 156 157 158
{
    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 已提交
159
static void
160 161
x86cpuidSetBits(virCPUx86CPUID *cpuid,
                const virCPUx86CPUID *mask)
J
Jiri Denemark 已提交
162
{
163 164 165
    if (!mask)
        return;

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


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

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


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

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

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

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

    return 0;
}

214

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

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

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

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


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

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

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

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

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


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

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

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

    return cpuData;
}

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

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


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

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

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

    return copy;
}


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

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

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

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


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

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

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

    return 0;
}


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

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


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

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


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

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


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

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

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

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


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

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

    return 0;
}


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

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

    return NULL;
}


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

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

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

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

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

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

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

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


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

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


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

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

    return NULL;
}


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

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

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

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

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

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

J
Jiri Denemark 已提交
561
 cleanup:
J
Jiri Denemark 已提交
562
    VIR_FREE(string);
J
Jiri Denemark 已提交
563 564 565
    return vendor;

 error:
J
Jiri Denemark 已提交
566
    x86VendorFree(vendor);
J
Jiri Denemark 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    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 已提交
592 593 594
}


595
static virCPUx86FeaturePtr
J
Jiri Denemark 已提交
596 597
x86FeatureNew(void)
{
598
    virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611

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

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

    return feature;
}


J
Jiri Denemark 已提交
612
static void
613
x86FeatureFree(virCPUx86FeaturePtr feature)
J
Jiri Denemark 已提交
614
{
615
    if (!feature)
J
Jiri Denemark 已提交
616 617 618
        return;

    VIR_FREE(feature->name);
619
    virCPUx86DataFree(feature->data);
J
Jiri Denemark 已提交
620 621 622 623
    VIR_FREE(feature);
}


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

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

    return NULL;
}


639
static char *
J
Jiri Denemark 已提交
640
x86FeatureNames(virCPUx86MapPtr map,
641
                const char *separator,
642
                virCPUx86Data *data)
643 644 645
{
    virBuffer ret = VIR_BUFFER_INITIALIZER;
    bool first = true;
646
    size_t i;
647 648 649

    virBufferAdd(&ret, "", 0);

650 651 652
    for (i = 0; i < map->nfeatures; i++) {
        virCPUx86FeaturePtr feature = map->features[i];
        if (x86DataIsSubset(data, feature->data)) {
653 654 655 656 657
            if (!first)
                virBufferAdd(&ret, separator, -1);
            else
                first = false;

658
            virBufferAdd(&ret, feature->name, -1);
659 660 661 662 663 664 665
        }
    }

    return virBufferContentAndReset(&ret);
}


666 667
static int
x86ParseCPUID(xmlXPathContextPtr ctxt,
668
              virCPUx86CPUID *cpuid)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
{
    unsigned long fun, eax, ebx, ecx, edx;
    int ret_fun, ret_eax, ret_ebx, ret_ecx, ret_edx;

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

    fun = eax = ebx = ecx = edx = 0;
    ret_fun = virXPathULongHex("string(@function)", ctxt, &fun);
    ret_eax = virXPathULongHex("string(@eax)", ctxt, &eax);
    ret_ebx = virXPathULongHex("string(@ebx)", ctxt, &ebx);
    ret_ecx = virXPathULongHex("string(@ecx)", ctxt, &ecx);
    ret_edx = virXPathULongHex("string(@edx)", ctxt, &edx);

    if (ret_fun < 0 || ret_eax == -2 || ret_ebx == -2
        || ret_ecx == -2 || ret_edx == -2)
        return -1;

    cpuid->function = fun;
    cpuid->eax = eax;
    cpuid->ebx = ebx;
    cpuid->ecx = ecx;
    cpuid->edx = edx;
    return 0;
}


J
Jiri Denemark 已提交
695 696 697
static virCPUx86FeaturePtr
x86FeatureParse(xmlXPathContextPtr ctxt,
                virCPUx86MapPtr map)
J
Jiri Denemark 已提交
698 699
{
    xmlNodePtr *nodes = NULL;
700
    virCPUx86FeaturePtr feature;
701
    virCPUx86CPUID cpuid;
702
    size_t i;
J
Jiri Denemark 已提交
703
    int n;
704
    char *str = NULL;
J
Jiri Denemark 已提交
705

J
Jiri Denemark 已提交
706
    if (!(feature = x86FeatureNew()))
J
Jiri Denemark 已提交
707
        goto error;
J
Jiri Denemark 已提交
708

J
Jiri Denemark 已提交
709
    feature->migratable = true;
710
    feature->name = virXPathString("string(@name)", ctxt);
711
    if (!feature->name) {
712 713
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU feature name"));
J
Jiri Denemark 已提交
714
        goto error;
J
Jiri Denemark 已提交
715 716 717
    }

    if (x86FeatureFind(map, feature->name)) {
718 719
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU feature %s already defined"), feature->name);
J
Jiri Denemark 已提交
720
        goto error;
J
Jiri Denemark 已提交
721 722
    }

723 724
    str = virXPathString("string(@migratable)", ctxt);
    if (STREQ_NULLABLE(str, "no"))
J
Jiri Denemark 已提交
725
        feature->migratable = false;
726

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

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

J
Jiri Denemark 已提交
743
 cleanup:
744
    VIR_FREE(nodes);
745
    VIR_FREE(str);
J
Jiri Denemark 已提交
746
    return feature;
747

J
Jiri Denemark 已提交
748 749 750 751
 error:
    x86FeatureFree(feature);
    feature = NULL;
    goto cleanup;
J
Jiri Denemark 已提交
752 753 754
}


J
Jiri Denemark 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
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;
}

782 783
static virCPUx86Data *
x86DataFromCPUFeatures(virCPUDefPtr cpu,
J
Jiri Denemark 已提交
784
                       virCPUx86MapPtr map)
785 786 787 788 789 790 791 792
{
    virCPUx86Data *data;
    size_t i;

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

    for (i = 0; i < cpu->nfeatures; i++) {
793
        virCPUx86FeaturePtr feature;
794 795 796 797 798 799 800 801 802 803 804 805
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
            goto error;
        }

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

    return data;

806
 error:
807 808 809 810 811
    virCPUx86DataFree(data);
    return NULL;
}


J
Jiri Denemark 已提交
812
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
813 814
x86ModelNew(void)
{
J
Jiri Denemark 已提交
815
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828

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

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

    return model;
}


J
Jiri Denemark 已提交
829
static void
J
Jiri Denemark 已提交
830
x86ModelFree(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
831
{
832
    if (!model)
J
Jiri Denemark 已提交
833 834 835
        return;

    VIR_FREE(model->name);
836
    virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
837 838 839 840
    VIR_FREE(model);
}


J
Jiri Denemark 已提交
841 842
static virCPUx86ModelPtr
x86ModelCopy(virCPUx86ModelPtr model)
J
Jiri Denemark 已提交
843
{
J
Jiri Denemark 已提交
844
    virCPUx86ModelPtr copy;
J
Jiri Denemark 已提交
845

846 847 848
    if (VIR_ALLOC(copy) < 0 ||
        VIR_STRDUP(copy->name, model->name) < 0 ||
        !(copy->data = x86DataCopy(model->data))) {
J
Jiri Denemark 已提交
849 850 851 852
        x86ModelFree(copy);
        return NULL;
    }

J
Jiri Denemark 已提交
853
    copy->vendor = model->vendor;
J
Jiri Denemark 已提交
854 855 856 857 858

    return copy;
}


J
Jiri Denemark 已提交
859
static virCPUx86ModelPtr
J
Jiri Denemark 已提交
860
x86ModelFind(virCPUx86MapPtr map,
J
Jiri Denemark 已提交
861 862
             const char *name)
{
863
    size_t i;
J
Jiri Denemark 已提交
864

865 866 867
    for (i = 0; i < map->nmodels; i++) {
        if (STREQ(map->models[i]->name, name))
            return map->models[i];
J
Jiri Denemark 已提交
868 869 870 871 872 873
    }

    return NULL;
}


J
Jiri Denemark 已提交
874
static virCPUx86ModelPtr
875
x86ModelFromCPU(const virCPUDef *cpu,
J
Jiri Denemark 已提交
876
                virCPUx86MapPtr map,
J
Jiri Denemark 已提交
877 878
                int policy)
{
J
Jiri Denemark 已提交
879
    virCPUx86ModelPtr model = NULL;
880
    size_t i;
J
Jiri Denemark 已提交
881

882
    if (policy == VIR_CPU_FEATURE_REQUIRE) {
883
        if (!(model = x86ModelFind(map, cpu->model))) {
884 885
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU model %s"), cpu->model);
J
Jiri Denemark 已提交
886 887 888
            goto error;
        }

889
        if (!(model = x86ModelCopy(model)))
890
            goto error;
J
Jiri Denemark 已提交
891
    } else if (!(model = x86ModelNew())) {
892
        goto error;
J
Jiri Denemark 已提交
893
    } else if (cpu->type == VIR_CPU_TYPE_HOST) {
894
        return model;
J
Jiri Denemark 已提交
895
    }
J
Jiri Denemark 已提交
896 897

    for (i = 0; i < cpu->nfeatures; i++) {
898
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
899 900 901 902 903

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

904
        if (!(feature = x86FeatureFind(map, cpu->features[i].name))) {
905 906
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown CPU feature %s"), cpu->features[i].name);
J
Jiri Denemark 已提交
907 908 909
            goto error;
        }

J
Jiri Denemark 已提交
910
        if (x86DataAdd(model->data, feature->data))
911
            goto error;
J
Jiri Denemark 已提交
912 913 914 915
    }

    return model;

916
 error:
J
Jiri Denemark 已提交
917 918 919 920 921
    x86ModelFree(model);
    return NULL;
}


922
static int
J
Jiri Denemark 已提交
923
x86ModelSubtractCPU(virCPUx86ModelPtr model,
924
                    const virCPUDef *cpu,
J
Jiri Denemark 已提交
925
                    virCPUx86MapPtr map)
926
{
J
Jiri Denemark 已提交
927
    virCPUx86ModelPtr cpu_model;
928
    size_t i;
929 930

    if (!(cpu_model = x86ModelFind(map, cpu->model))) {
931 932 933
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU model %s"),
                       cpu->model);
934 935 936
        return -1;
    }

J
Jiri Denemark 已提交
937
    x86DataSubtract(model->data, cpu_model->data);
938 939

    for (i = 0; i < cpu->nfeatures; i++) {
940
        virCPUx86FeaturePtr feature;
941 942

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

J
Jiri Denemark 已提交
949
        x86DataSubtract(model->data, feature->data);
950 951 952 953 954 955
    }

    return 0;
}


956
static virCPUx86CompareResult
J
Jiri Denemark 已提交
957 958
x86ModelCompare(virCPUx86ModelPtr model1,
                virCPUx86ModelPtr model2)
J
Jiri Denemark 已提交
959
{
960
    virCPUx86CompareResult result = EQUAL;
961 962
    virCPUx86DataIterator iter1 = virCPUx86DataIteratorInit(model1->data);
    virCPUx86DataIterator iter2 = virCPUx86DataIteratorInit(model2->data);
963 964
    virCPUx86CPUID *cpuid1;
    virCPUx86CPUID *cpuid2;
J
Jiri Denemark 已提交
965

J
Jiri Denemark 已提交
966
    while ((cpuid1 = x86DataCpuidNext(&iter1))) {
967
        virCPUx86CompareResult match = SUPERSET;
J
Jiri Denemark 已提交
968

J
Jiri Denemark 已提交
969
        if ((cpuid2 = x86DataCpuid(model2->data, cpuid1->function))) {
J
Jiri Denemark 已提交
970 971 972 973 974 975 976 977 978 979 980 981
            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 已提交
982
    while ((cpuid2 = x86DataCpuidNext(&iter2))) {
983
        virCPUx86CompareResult match = SUBSET;
J
Jiri Denemark 已提交
984

J
Jiri Denemark 已提交
985
        if ((cpuid1 = x86DataCpuid(model1->data, cpuid2->function))) {
J
Jiri Denemark 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
            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 已提交
1002 1003 1004
static virCPUx86ModelPtr
x86ModelParse(xmlXPathContextPtr ctxt,
              virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1005 1006
{
    xmlNodePtr *nodes = NULL;
J
Jiri Denemark 已提交
1007
    virCPUx86ModelPtr model;
J
Jiri Denemark 已提交
1008
    char *vendor = NULL;
1009
    size_t i;
J
Jiri Denemark 已提交
1010 1011
    int n;

J
Jiri Denemark 已提交
1012
    if (!(model = x86ModelNew()))
J
Jiri Denemark 已提交
1013
        goto error;
J
Jiri Denemark 已提交
1014

1015
    model->name = virXPathString("string(@name)", ctxt);
1016
    if (!model->name) {
1017 1018
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
J
Jiri Denemark 已提交
1019
        goto error;
J
Jiri Denemark 已提交
1020 1021
    }

1022
    if (virXPathNode("./model", ctxt)) {
J
Jiri Denemark 已提交
1023
        virCPUx86ModelPtr ancestor;
J
Jiri Denemark 已提交
1024 1025
        char *name;

1026
        name = virXPathString("string(./model/@name)", ctxt);
1027
        if (!name) {
1028 1029 1030
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing ancestor's name in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1031
            goto error;
J
Jiri Denemark 已提交
1032 1033
        }

1034
        if (!(ancestor = x86ModelFind(map, name))) {
1035 1036 1037
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Ancestor model %s not found for CPU model %s"),
                           name, model->name);
J
Jiri Denemark 已提交
1038
            VIR_FREE(name);
J
Jiri Denemark 已提交
1039
            goto error;
J
Jiri Denemark 已提交
1040 1041 1042 1043
        }

        VIR_FREE(name);

J
Jiri Denemark 已提交
1044
        model->vendor = ancestor->vendor;
1045
        virCPUx86DataFree(model->data);
J
Jiri Denemark 已提交
1046
        if (!(model->data = x86DataCopy(ancestor->data)))
J
Jiri Denemark 已提交
1047
            goto error;
J
Jiri Denemark 已提交
1048 1049
    }

1050 1051 1052
    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
1053 1054 1055
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
J
Jiri Denemark 已提交
1056
            goto error;
1057 1058
        }

J
Jiri Denemark 已提交
1059
        if (!(model->vendor = x86VendorFind(map, vendor))) {
1060 1061 1062
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
J
Jiri Denemark 已提交
1063
            goto error;
J
Jiri Denemark 已提交
1064 1065 1066
        }
    }

1067
    n = virXPathNodeSet("./feature", ctxt, &nodes);
J
Jiri Denemark 已提交
1068
    if (n < 0)
J
Jiri Denemark 已提交
1069
        goto error;
J
Jiri Denemark 已提交
1070 1071

    for (i = 0; i < n; i++) {
1072
        virCPUx86FeaturePtr feature;
J
Jiri Denemark 已提交
1073 1074
        char *name;

1075
        if (!(name = virXMLPropString(nodes[i], "name"))) {
1076 1077
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing feature name for CPU model %s"), model->name);
J
Jiri Denemark 已提交
1078
            goto error;
J
Jiri Denemark 已提交
1079 1080
        }

1081
        if (!(feature = x86FeatureFind(map, name))) {
1082 1083 1084
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Feature %s required by CPU model %s not found"),
                           name, model->name);
J
Jiri Denemark 已提交
1085
            VIR_FREE(name);
J
Jiri Denemark 已提交
1086
            goto error;
J
Jiri Denemark 已提交
1087 1088 1089
        }
        VIR_FREE(name);

J
Jiri Denemark 已提交
1090
        if (x86DataAdd(model->data, feature->data))
J
Jiri Denemark 已提交
1091
            goto error;
J
Jiri Denemark 已提交
1092 1093
    }

J
Jiri Denemark 已提交
1094
 cleanup:
J
Jiri Denemark 已提交
1095
    VIR_FREE(vendor);
1096
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    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 已提交
1126 1127 1128 1129
}


static void
J
Jiri Denemark 已提交
1130
x86MapFree(virCPUx86MapPtr map)
J
Jiri Denemark 已提交
1131
{
1132 1133
    size_t i;

1134
    if (!map)
J
Jiri Denemark 已提交
1135 1136
        return;

1137 1138 1139
    for (i = 0; i < map->nfeatures; i++)
        x86FeatureFree(map->features[i]);
    VIR_FREE(map->features);
J
Jiri Denemark 已提交
1140

1141 1142 1143
    for (i = 0; i < map->nmodels; i++)
        x86ModelFree(map->models[i]);
    VIR_FREE(map->models);
J
Jiri Denemark 已提交
1144

1145 1146 1147
    for (i = 0; i < map->nvendors; i++)
        x86VendorFree(map->vendors[i]);
    VIR_FREE(map->vendors);
1148

1149 1150 1151 1152
    /* migrate_blockers only points to the features from map->features list,
     * which were already freed above
     */
    VIR_FREE(map->migrate_blockers);
1153

J
Jiri Denemark 已提交
1154 1155 1156 1157
    VIR_FREE(map);
}


J
Jiri Denemark 已提交
1158
static int
1159
x86MapLoadCallback(cpuMapElement element,
J
Jiri Denemark 已提交
1160
                   xmlXPathContextPtr ctxt,
J
Jiri Denemark 已提交
1161 1162
                   xmlNodePtr *nodes,
                   int n,
J
Jiri Denemark 已提交
1163 1164
                   void *data)
{
J
Jiri Denemark 已提交
1165
    virCPUx86MapPtr map = data;
J
Jiri Denemark 已提交
1166 1167 1168

    switch (element) {
    case CPU_MAP_ELEMENT_VENDOR:
J
Jiri Denemark 已提交
1169
        return x86VendorsLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1170
    case CPU_MAP_ELEMENT_FEATURE:
J
Jiri Denemark 已提交
1171
        return x86FeaturesLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1172
    case CPU_MAP_ELEMENT_MODEL:
J
Jiri Denemark 已提交
1173
        return x86ModelsLoad(map, ctxt, nodes, n);
J
Jiri Denemark 已提交
1174 1175 1176 1177 1178 1179 1180 1181
    case CPU_MAP_ELEMENT_LAST:
        break;
    }

    return 0;
}


1182
static int
J
Jiri Denemark 已提交
1183
x86MapLoadInternalFeatures(virCPUx86MapPtr map)
1184 1185
{
    size_t i;
1186
    virCPUx86FeaturePtr feature = NULL;
1187 1188 1189 1190 1191
    size_t nfeatures = map->nfeatures;
    size_t count = ARRAY_CARDINALITY(x86_kvm_features);

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

1193
    for (i = 0; i < count; i++) {
1194 1195 1196 1197 1198
        const char *name = x86_kvm_features[i].name;

        if (x86FeatureFind(map, name)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("CPU feature %s already defined"), name);
1199
            goto error;
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
        }

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

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

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

1211
        map->features[map->nfeatures++] = feature;
1212 1213 1214 1215 1216
        feature = NULL;
    }

    return 0;

1217
 error:
1218 1219 1220 1221 1222
    x86FeatureFree(feature);
    return -1;
}


J
Jiri Denemark 已提交
1223
static virCPUx86MapPtr
1224
virCPUx86LoadMap(void)
J
Jiri Denemark 已提交
1225
{
J
Jiri Denemark 已提交
1226
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1227

1228
    if (VIR_ALLOC(map) < 0)
J
Jiri Denemark 已提交
1229 1230
        return NULL;

J
Jiri Denemark 已提交
1231
    if (cpuMapLoad("x86", x86MapLoadCallback, map) < 0)
J
Jiri Denemark 已提交
1232 1233
        goto error;

1234 1235 1236
    if (x86MapLoadInternalFeatures(map) < 0)
        goto error;

J
Jiri Denemark 已提交
1237 1238
    return map;

1239
 error:
J
Jiri Denemark 已提交
1240 1241 1242 1243 1244
    x86MapFree(map);
    return NULL;
}


1245 1246 1247
int
virCPUx86MapOnceInit(void)
{
J
Jiri Denemark 已提交
1248
    if (!(cpuMap = virCPUx86LoadMap()))
1249 1250 1251 1252 1253 1254
        return -1;

    return 0;
}


J
Jiri Denemark 已提交
1255
static virCPUx86MapPtr
1256 1257 1258 1259 1260
virCPUx86GetMap(void)
{
    if (virCPUx86MapInitialize() < 0)
        return NULL;

J
Jiri Denemark 已提交
1261
    return cpuMap;
1262 1263 1264
}


1265 1266 1267
static char *
x86CPUDataFormat(const virCPUData *data)
{
1268
    virCPUx86DataIterator iter = virCPUx86DataIteratorInit(data->data.x86);
1269
    virCPUx86CPUID *cpuid;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAddLit(&buf, "<cpudata arch='x86'>\n");
    while ((cpuid = x86DataCpuidNext(&iter))) {
        virBufferAsprintf(&buf,
                          "  <cpuid function='0x%08x'"
                          " eax='0x%08x' ebx='0x%08x'"
                          " ecx='0x%08x' edx='0x%08x'/>\n",
                          cpuid->function,
                          cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx);
    }
    virBufferAddLit(&buf, "</cpudata>\n");

1283
    if (virBufferCheckError(&buf) < 0)
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
x86CPUDataParse(const char *xmlStr)
{
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1297
    virCPUx86Data *data = NULL;
1298
    virCPUx86CPUID cpuid;
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
    size_t i;
    int n;

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

    if (!(xml = virXMLParseStringCtxt(xmlStr, _("CPU data"), &ctxt))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot parse CPU data"));
        goto cleanup;
    }
    ctxt->node = xmlDocGetRootElement(xml);

    n = virXPathNodeSet("/cpudata[@arch='x86']/data", ctxt, &nodes);
    if (n < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no x86 CPU data found"));
        goto cleanup;
    }

    for (i = 0; i < n; i++) {
        ctxt->node = nodes[i];
        if (x86ParseCPUID(ctxt, &cpuid) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse cpuid[%zu]"), i);
            goto cleanup;
        }
1326
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
1327 1328 1329
            goto cleanup;
    }

1330
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1331

1332
 cleanup:
1333 1334 1335
    VIR_FREE(nodes);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1336
    virCPUx86DataFree(data);
1337 1338 1339 1340
    return cpuData;
}


1341 1342 1343
/* A helper macro to exit the cpu computation function without writing
 * redundant code:
 * MSG: error message
1344
 * CPU_DEF: a virCPUx86Data pointer with flags that are conflicting
1345 1346 1347 1348 1349 1350 1351
 * RET: return code to set
 *
 * This macro generates the error string outputs it into logs.
 */
#define virX86CpuIncompatible(MSG, CPU_DEF)                             \
        do {                                                            \
            char *flagsStr = NULL;                                      \
1352 1353 1354 1355
            if (!(flagsStr = x86FeatureNames(map, ", ", (CPU_DEF)))) {  \
                virReportOOMError();                                    \
                goto error;                                             \
            }                                                           \
1356 1357 1358
            if (message &&                                              \
                virAsprintf(message, "%s: %s", _(MSG), flagsStr) < 0) { \
                VIR_FREE(flagsStr);                                     \
1359
                goto error;                                             \
1360 1361 1362 1363 1364 1365
            }                                                           \
            VIR_DEBUG("%s: %s", MSG, flagsStr);                         \
            VIR_FREE(flagsStr);                                         \
            ret = VIR_CPU_COMPARE_INCOMPATIBLE;                         \
        } while (0)

1366

J
Jiri Denemark 已提交
1367 1368 1369
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1370
           virCPUDataPtr *guest,
1371
           char **message)
J
Jiri Denemark 已提交
1372
{
J
Jiri Denemark 已提交
1373
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1374 1375 1376 1377 1378 1379 1380 1381
    virCPUx86ModelPtr host_model = NULL;
    virCPUx86ModelPtr cpu_force = NULL;
    virCPUx86ModelPtr cpu_require = NULL;
    virCPUx86ModelPtr cpu_optional = NULL;
    virCPUx86ModelPtr cpu_disable = NULL;
    virCPUx86ModelPtr cpu_forbid = NULL;
    virCPUx86ModelPtr diff = NULL;
    virCPUx86ModelPtr guest_model = NULL;
J
Jiri Denemark 已提交
1382
    virCPUCompareResult ret;
1383
    virCPUx86CompareResult result;
J
Jiri Denemark 已提交
1384
    virArch arch;
1385
    size_t i;
J
Jiri Denemark 已提交
1386

1387 1388 1389 1390 1391 1392
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

1393
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1394 1395 1396
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1397
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1398 1399 1400 1401 1402
                found = true;
                break;
            }
        }

1403
        if (!found) {
1404 1405
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1406 1407 1408
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1409
                            virArchToString(cpu->arch)) < 0)
1410
                goto error;
J
Jiri Denemark 已提交
1411
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1412
        }
J
Jiri Denemark 已提交
1413 1414 1415
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1416 1417
    }

J
Jiri Denemark 已提交
1418 1419 1420 1421
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1422 1423 1424 1425 1426
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1427
            goto error;
1428

J
Jiri Denemark 已提交
1429 1430 1431
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1432
    if (!(map = virCPUx86GetMap()) ||
1433
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1434 1435 1436 1437 1438
        !(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 已提交
1439 1440
        goto error;

1441 1442 1443 1444
    x86DataIntersect(cpu_forbid->data, host_model->data);
    if (!x86DataIsEmpty(cpu_forbid->data)) {
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
                              cpu_forbid->data);
1445
        goto cleanup;
J
Jiri Denemark 已提交
1446 1447
    }

1448 1449 1450 1451 1452
    /* first remove features that were inherited from the CPU model and were
     * explicitly forced, disabled, or made optional
     */
    x86DataSubtract(cpu_require->data, cpu_force->data);
    x86DataSubtract(cpu_require->data, cpu_optional->data);
J
Jiri Denemark 已提交
1453
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1454 1455
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1456 1457 1458 1459
        x86DataSubtract(cpu_require->data, host_model->data);
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
                              cpu_require->data);
1460
        goto cleanup;
J
Jiri Denemark 已提交
1461 1462 1463 1464
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

1465
    if (!(diff = x86ModelCopy(host_model)))
1466
        goto error;
J
Jiri Denemark 已提交
1467

J
Jiri Denemark 已提交
1468 1469 1470 1471
    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 已提交
1472

J
Jiri Denemark 已提交
1473 1474
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1475 1476 1477 1478

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1479 1480 1481
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
                              diff->data);
1482
        goto cleanup;
J
Jiri Denemark 已提交
1483 1484
    }

1485
    if (guest) {
1486
        virCPUx86Data *guestData;
1487

1488
        if (!(guest_model = x86ModelCopy(host_model)))
1489
            goto error;
J
Jiri Denemark 已提交
1490 1491 1492

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

J
Jiri Denemark 已提交
1495
        if (x86DataAdd(guest_model->data, cpu_force->data))
1496
            goto error;
J
Jiri Denemark 已提交
1497

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

1500
        if (!(guestData = x86DataCopy(guest_model->data)) ||
1501
            !(*guest = virCPUx86MakeData(arch, &guestData))) {
1502
            virCPUx86DataFree(guestData);
1503
            goto error;
1504
        }
J
Jiri Denemark 已提交
1505 1506
    }

1507
 cleanup:
J
Jiri Denemark 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
    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;

1519
 error:
J
Jiri Denemark 已提交
1520
    ret = VIR_CPU_COMPARE_ERROR;
1521
    goto cleanup;
J
Jiri Denemark 已提交
1522
}
1523
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1524 1525 1526 1527


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
1528
           virCPUDefPtr cpu,
1529
           bool failIncompatible)
J
Jiri Denemark 已提交
1530
{
1531 1532 1533 1534 1535
    virCPUCompareResult ret;
    char *message = NULL;

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

1536
    if (failIncompatible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
        ret = VIR_CPU_COMPARE_ERROR;
        if (message) {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s", message);
        } else {
            virReportError(VIR_ERR_CPU_INCOMPATIBLE, NULL);
        }
    }
    VIR_FREE(message);

    return ret;
J
Jiri Denemark 已提交
1547 1548 1549 1550 1551 1552
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1553
             virCPUDataPtr *data,
1554
             char **message)
J
Jiri Denemark 已提交
1555
{
1556
    return x86Compute(host, guest, data, message);
J
Jiri Denemark 已提交
1557 1558
}

1559

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
/*
 * Checks whether cpuCandidate is a better fit for the CPU data than the
 * currently selected one from cpuCurrent.
 *
 * Returns 0 if cpuCurrent is better,
 *         1 if cpuCandidate is better,
 *         2 if cpuCandidate is the best one (search should stop now).
 */
static int
x86DecodeUseCandidate(virCPUDefPtr cpuCurrent,
                      virCPUDefPtr cpuCandidate,
                      const char *preferred,
                      bool checkPolicy)
{
    if (checkPolicy) {
        size_t i;
        for (i = 0; i < cpuCandidate->nfeatures; i++) {
            if (cpuCandidate->features[i].policy == VIR_CPU_FEATURE_DISABLE)
                return 0;
            cpuCandidate->features[i].policy = -1;
        }
    }

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

    if (!cpuCurrent)
        return 1;

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

    return 0;
}


J
Jiri Denemark 已提交
1597 1598
static int
x86Decode(virCPUDefPtr cpu,
1599
          const virCPUx86Data *data,
1600
          const char **models,
1601
          unsigned int nmodels,
1602 1603
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1604 1605
{
    int ret = -1;
J
Jiri Denemark 已提交
1606
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1607
    virCPUx86ModelPtr candidate;
1608 1609
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1610 1611 1612
    virCPUx86Data *copy = NULL;
    virCPUx86Data *features = NULL;
    const virCPUx86Data *cpuData = NULL;
J
Jiri Denemark 已提交
1613
    virCPUx86VendorPtr vendor;
1614
    ssize_t i;
1615
    int rc;
J
Jiri Denemark 已提交
1616

1617 1618
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, -1);
1619

1620
    if (!data || !(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1621 1622
        return -1;

J
Jiri Denemark 已提交
1623 1624
    vendor = x86DataToVendor(data, map);

1625 1626 1627 1628 1629
    /* 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];
1630
        if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
1631 1632
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
1633 1634 1635
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
J
Jiri Denemark 已提交
1636
                    goto cleanup;
1637 1638 1639 1640 1641 1642 1643 1644 1645
                } 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);
            }
1646
            continue;
J
Jiri Denemark 已提交
1647 1648
        }

J
Jiri Denemark 已提交
1649 1650 1651 1652
        /* 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 已提交
1653
            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
J
Jiri Denemark 已提交
1654
                      candidate->vendor->name, candidate->name, vendor->name);
1655
            continue;
J
Jiri Denemark 已提交
1656 1657
        }

J
Jiri Denemark 已提交
1658 1659 1660 1661
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto cleanup;
        cpuCandidate->type = cpu->type;

1662 1663
        if ((rc = x86DecodeUseCandidate(cpuModel, cpuCandidate, preferred,
                                        cpu->type == VIR_CPU_TYPE_HOST))) {
1664 1665
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
1666
            cpuData = candidate->data;
1667 1668
            if (rc == 2)
                break;
1669
        } else {
1670
            virCPUDefFree(cpuCandidate);
1671
        }
J
Jiri Denemark 已提交
1672 1673
    }

1674
    if (!cpuModel) {
1675 1676
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
J
Jiri Denemark 已提交
1677
        goto cleanup;
J
Jiri Denemark 已提交
1678 1679
    }

1680 1681 1682 1683 1684
    /* Remove non-migratable features if requested
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
    if (flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE) {
        for (i = 0; i < cpuModel->nfeatures; i++) {
1685 1686 1687 1688
            size_t j;
            for (j = 0; j < map->nblockers; j++) {
                if (STREQ(map->migrate_blockers[j]->name,
                          cpuModel->features[i].name)) {
1689
                    VIR_FREE(cpuModel->features[i].name);
1690 1691
                    VIR_DELETE_ELEMENT_INPLACE(cpuModel->features, i,
                                               cpuModel->nfeatures);
1692 1693 1694 1695 1696
                }
            }
        }
    }

1697 1698 1699
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) {
        if (!(copy = x86DataCopy(cpuData)) ||
            !(features = x86DataFromCPUFeatures(cpuModel, map)))
J
Jiri Denemark 已提交
1700
            goto cleanup;
1701 1702 1703 1704

        x86DataSubtract(copy, features);
        if (x86DataToCPUFeatures(cpuModel, VIR_CPU_FEATURE_REQUIRE,
                                 copy, map) < 0)
J
Jiri Denemark 已提交
1705
            goto cleanup;
1706 1707
    }

J
Jiri Denemark 已提交
1708 1709 1710
    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
        goto cleanup;

1711 1712 1713 1714
    cpu->model = cpuModel->model;
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1715 1716 1717

    ret = 0;

J
Jiri Denemark 已提交
1718
 cleanup:
1719
    virCPUDefFree(cpuModel);
1720 1721
    virCPUx86DataFree(copy);
    virCPUx86DataFree(features);
J
Jiri Denemark 已提交
1722 1723 1724
    return ret;
}

1725 1726
static int
x86DecodeCPUData(virCPUDefPtr cpu,
1727
                 const virCPUData *data,
1728 1729
                 const char **models,
                 unsigned int nmodels,
1730 1731
                 const char *preferred,
                 unsigned int flags)
1732
{
1733
    return x86Decode(cpu, data->data.x86, models, nmodels, preferred, flags);
1734
}
J
Jiri Denemark 已提交
1735

1736

1737
static virCPUx86Data *
1738
x86EncodePolicy(const virCPUDef *cpu,
J
Jiri Denemark 已提交
1739
                virCPUx86MapPtr map,
1740
                virCPUFeaturePolicy policy)
J
Jiri Denemark 已提交
1741
{
J
Jiri Denemark 已提交
1742
    virCPUx86ModelPtr model;
1743
    virCPUx86Data *data = NULL;
J
Jiri Denemark 已提交
1744 1745 1746 1747

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

J
Jiri Denemark 已提交
1748 1749
    data = model->data;
    model->data = NULL;
J
Jiri Denemark 已提交
1750 1751 1752 1753 1754 1755 1756
    x86ModelFree(model);

    return data;
}


static int
J
Jiri Denemark 已提交
1757
x86Encode(virArch arch,
1758
          const virCPUDef *cpu,
1759 1760 1761 1762 1763 1764
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
1765
{
J
Jiri Denemark 已提交
1766
    virCPUx86MapPtr map = NULL;
1767 1768 1769 1770 1771 1772
    virCPUx86Data *data_forced = NULL;
    virCPUx86Data *data_required = NULL;
    virCPUx86Data *data_optional = NULL;
    virCPUx86Data *data_disabled = NULL;
    virCPUx86Data *data_forbidden = NULL;
    virCPUx86Data *data_vendor = NULL;
J
Jiri Denemark 已提交
1773

1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

1787
    if (!(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
        goto error;

    if (forced) {
        data_forced = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_FORCE);
        if (!data_forced)
            goto error;
    }

    if (required) {
        data_required = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_REQUIRE);
        if (!data_required)
            goto error;
    }

    if (optional) {
        data_optional = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_OPTIONAL);
        if (!data_optional)
            goto error;
    }

    if (disabled) {
        data_disabled = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_DISABLE);
        if (!data_disabled)
            goto error;
    }

    if (forbidden) {
        data_forbidden = x86EncodePolicy(cpu, map, VIR_CPU_FEATURE_FORBID);
        if (!data_forbidden)
            goto error;
    }

J
Jiri Denemark 已提交
1820
    if (vendor) {
1821
        virCPUx86VendorPtr v = NULL;
J
Jiri Denemark 已提交
1822 1823

        if (cpu->vendor && !(v = x86VendorFind(map, cpu->vendor))) {
1824 1825
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("CPU vendor %s not found"), cpu->vendor);
J
Jiri Denemark 已提交
1826 1827 1828 1829 1830
            goto error;
        }

        if (v &&
            (VIR_ALLOC(data_vendor) < 0 ||
1831
             virCPUx86DataAddCPUID(data_vendor, &v->cpuid) < 0)) {
J
Jiri Denemark 已提交
1832 1833 1834 1835
            goto error;
        }
    }

1836
    if (forced &&
1837
        !(*forced = virCPUx86MakeData(arch, &data_forced)))
1838 1839
        goto error;
    if (required &&
1840
        !(*required = virCPUx86MakeData(arch, &data_required)))
1841 1842
        goto error;
    if (optional &&
1843
        !(*optional = virCPUx86MakeData(arch, &data_optional)))
1844 1845
        goto error;
    if (disabled &&
1846
        !(*disabled = virCPUx86MakeData(arch, &data_disabled)))
1847 1848
        goto error;
    if (forbidden &&
1849
        !(*forbidden = virCPUx86MakeData(arch, &data_forbidden)))
1850 1851
        goto error;
    if (vendor &&
1852
        !(*vendor = virCPUx86MakeData(arch, &data_vendor)))
1853
        goto error;
J
Jiri Denemark 已提交
1854

1855
    return 0;
J
Jiri Denemark 已提交
1856

1857
 error:
1858 1859 1860 1861 1862 1863
    virCPUx86DataFree(data_forced);
    virCPUx86DataFree(data_required);
    virCPUx86DataFree(data_optional);
    virCPUx86DataFree(data_disabled);
    virCPUx86DataFree(data_forbidden);
    virCPUx86DataFree(data_vendor);
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
    if (forced)
        x86FreeCPUData(*forced);
    if (required)
        x86FreeCPUData(*required);
    if (optional)
        x86FreeCPUData(*optional);
    if (disabled)
        x86FreeCPUData(*disabled);
    if (forbidden)
        x86FreeCPUData(*forbidden);
    if (vendor)
        x86FreeCPUData(*vendor);
1876
    return -1;
J
Jiri Denemark 已提交
1877 1878 1879 1880 1881
}


#if HAVE_CPUID
static inline void
1882
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
1883
{
1884
# if __x86_64__
1885 1886 1887 1888
    asm("xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
        "xor %%ecx, %%ecx;" /* functions may use them as additional */
        "xor %%edx, %%edx;" /* arguments */
        "cpuid;"
J
Jiri Denemark 已提交
1889 1890 1891 1892 1893
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function));
1894
# else
J
Jiri Denemark 已提交
1895 1896 1897 1898
    /* 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;"
1899 1900 1901
        "xor %%ebx, %%ebx;" /* clear the other registers as some cpuid */
        "xor %%ecx, %%ecx;" /* functions may use them as additional */
        "xor %%edx, %%edx;" /* arguments */
J
Jiri Denemark 已提交
1902 1903 1904 1905 1906 1907 1908 1909 1910
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
        : "a" (cpuid->function)
        : "cc");
1911
# endif
J
Jiri Denemark 已提交
1912 1913 1914 1915
}


static int
1916
cpuidSet(uint32_t base, virCPUx86Data *data)
J
Jiri Denemark 已提交
1917 1918 1919
{
    uint32_t max;
    uint32_t i;
1920
    virCPUx86CPUID cpuid = { base, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
1921 1922

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

1925 1926
    for (i = base; i <= max; i++) {
        cpuid.function = i;
J
Jiri Denemark 已提交
1927
        cpuidCall(&cpuid);
1928 1929
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
J
Jiri Denemark 已提交
1930 1931
    }

1932
    return 0;
J
Jiri Denemark 已提交
1933 1934 1935
}


1936
static virCPUDataPtr
1937
x86NodeData(virArch arch)
J
Jiri Denemark 已提交
1938
{
1939
    virCPUDataPtr cpuData = NULL;
1940
    virCPUx86Data *data;
J
Jiri Denemark 已提交
1941

1942
    if (VIR_ALLOC(data) < 0)
J
Jiri Denemark 已提交
1943 1944
        return NULL;

1945
    if (cpuidSet(CPUX86_BASIC, data) < 0)
J
Jiri Denemark 已提交
1946 1947
        goto error;

1948
    if (cpuidSet(CPUX86_EXTENDED, data) < 0)
J
Jiri Denemark 已提交
1949 1950
        goto error;

1951
    if (!(cpuData = virCPUx86MakeData(arch, &data)))
1952 1953 1954
        goto error;

    return cpuData;
J
Jiri Denemark 已提交
1955

1956
 error:
1957
    virCPUx86DataFree(data);
J
Jiri Denemark 已提交
1958 1959 1960 1961 1962 1963

    return NULL;
}
#endif


1964 1965 1966 1967
static virCPUDefPtr
x86Baseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
1968 1969
            unsigned int nmodels,
            unsigned int flags)
1970
{
J
Jiri Denemark 已提交
1971
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1972
    virCPUx86ModelPtr base_model = NULL;
1973
    virCPUDefPtr cpu = NULL;
1974
    size_t i;
1975
    virCPUx86VendorPtr vendor = NULL;
J
Jiri Denemark 已提交
1976
    virCPUx86ModelPtr model = NULL;
1977
    bool outputVendor = true;
1978 1979
    const char *modelName;
    bool matchingNames = true;
1980

1981 1982 1983
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, NULL);

1984
    if (!(map = virCPUx86GetMap()))
1985 1986
        goto error;

1987
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1988 1989
        goto error;

1990
    if (VIR_ALLOC(cpu) < 0)
1991
        goto error;
1992 1993

    cpu->arch = cpus[0]->arch;
1994 1995
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1996

1997
    if (!cpus[0]->vendor) {
1998
        outputVendor = false;
1999
    } else if (!(vendor = x86VendorFind(map, cpus[0]->vendor))) {
2000 2001
        virReportError(VIR_ERR_OPERATION_FAILED,
                       _("Unknown CPU vendor %s"), cpus[0]->vendor);
J
Jiri Denemark 已提交
2002 2003 2004
        goto error;
    }

2005
    modelName = cpus[0]->model;
2006
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
2007 2008
        const char *vn = NULL;

2009 2010 2011 2012 2013 2014 2015 2016 2017
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

2018
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
2019 2020
            goto error;

J
Jiri Denemark 已提交
2021 2022
        if (cpus[i]->vendor && model->vendor &&
            STRNEQ(cpus[i]->vendor, model->vendor->name)) {
2023 2024 2025
            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 已提交
2026 2027 2028
            goto error;
        }

2029
        if (cpus[i]->vendor) {
J
Jiri Denemark 已提交
2030
            vn = cpus[i]->vendor;
2031
        } else {
2032 2033 2034 2035
            outputVendor = false;
            if (model->vendor)
                vn = model->vendor->name;
        }
J
Jiri Denemark 已提交
2036 2037 2038 2039

        if (vn) {
            if (!vendor) {
                if (!(vendor = x86VendorFind(map, vn))) {
2040 2041
                    virReportError(VIR_ERR_OPERATION_FAILED,
                                   _("Unknown CPU vendor %s"), vn);
J
Jiri Denemark 已提交
2042 2043 2044
                    goto error;
                }
            } else if (STRNEQ(vendor->name, vn)) {
2045 2046
                virReportError(VIR_ERR_OPERATION_FAILED,
                               "%s", _("CPU vendors do not match"));
J
Jiri Denemark 已提交
2047 2048 2049 2050
                goto error;
            }
        }

J
Jiri Denemark 已提交
2051
        x86DataIntersect(base_model->data, model->data);
2052
        x86ModelFree(model);
J
Jiri Denemark 已提交
2053
        model = NULL;
2054 2055
    }

J
Jiri Denemark 已提交
2056
    if (x86DataIsEmpty(base_model->data)) {
2057 2058
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
2059 2060 2061
        goto error;
    }

2062
    if (vendor && virCPUx86DataAddCPUID(base_model->data, &vendor->cpuid) < 0)
2063
        goto error;
J
Jiri Denemark 已提交
2064

2065
    if (x86Decode(cpu, base_model->data, models, nmodels, modelName, flags) < 0)
2066 2067
        goto error;

2068 2069 2070
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2071 2072 2073
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2074 2075
    cpu->arch = VIR_ARCH_NONE;

2076
 cleanup:
2077 2078 2079 2080
    x86ModelFree(base_model);

    return cpu;

2081
 error:
J
Jiri Denemark 已提交
2082
    x86ModelFree(model);
2083 2084 2085 2086 2087 2088
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


2089
static int
2090
x86UpdateCustom(virCPUDefPtr guest,
2091
                const virCPUDef *host)
2092 2093
{
    int ret = -1;
2094
    size_t i;
J
Jiri Denemark 已提交
2095
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
2096
    virCPUx86ModelPtr host_model = NULL;
2097

2098
    if (!(map = virCPUx86GetMap()) ||
2099
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
2100 2101 2102 2103
        goto cleanup;

    for (i = 0; i < guest->nfeatures; i++) {
        if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
2104
            virCPUx86FeaturePtr feature;
2105
            if (!(feature = x86FeatureFind(map, guest->features[i].name))) {
2106 2107 2108
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unknown CPU feature %s"),
                               guest->features[i].name);
2109 2110 2111
                goto cleanup;
            }

J
Jiri Denemark 已提交
2112
            if (x86DataIsSubset(host_model->data, feature->data))
2113 2114 2115 2116 2117 2118 2119 2120
                guest->features[i].policy = VIR_CPU_FEATURE_REQUIRE;
            else
                guest->features[i].policy = VIR_CPU_FEATURE_DISABLE;
        }
    }

    if (guest->match == VIR_CPU_MATCH_MINIMUM) {
        guest->match = VIR_CPU_MATCH_EXACT;
P
Peter Krempa 已提交
2121 2122 2123
        if (x86ModelSubtractCPU(host_model, guest, map) ||
            x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
                                 host_model->data, map))
2124 2125 2126 2127 2128
            goto cleanup;
    }

    ret = 0;

2129
 cleanup:
2130 2131 2132 2133
    x86ModelFree(host_model);
    return ret;
}

2134 2135 2136

static int
x86UpdateHostModel(virCPUDefPtr guest,
2137 2138
                   const virCPUDef *host,
                   bool passthrough)
2139
{
2140
    virCPUDefPtr oldguest = NULL;
J
Jiri Denemark 已提交
2141
    virCPUx86MapPtr map;
2142
    size_t i;
2143
    int ret = -1;
2144

2145 2146
    if (!(map = virCPUx86GetMap()))
        goto cleanup;
2147 2148 2149

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
2150
        goto cleanup;
2151 2152 2153

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
2154
        goto cleanup;
2155

2156 2157 2158 2159 2160 2161
    if (oldguest->vendor_id) {
        VIR_FREE(guest->vendor_id);
        if (VIR_STRDUP(guest->vendor_id, oldguest->vendor_id) < 0)
            goto cleanup;
    }

2162 2163 2164 2165
    /* Remove non-migratable features by default
     * Note: this only works as long as no CPU model contains non-migratable
     * features directly */
    for (i = 0; i < guest->nfeatures; i++) {
2166 2167 2168
        size_t j;
        for (j = 0; j < map->nblockers; j++) {
            if (STREQ(map->migrate_blockers[j]->name, guest->features[i].name)) {
J
Ján Tomko 已提交
2169
                VIR_FREE(guest->features[i].name);
2170
                VIR_DELETE_ELEMENT_INPLACE(guest->features, i, guest->nfeatures);
J
Ján Tomko 已提交
2171
            }
2172 2173
        }
    }
2174
    for (i = 0; !passthrough && i < oldguest->nfeatures; i++) {
2175 2176 2177
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
2178
            goto cleanup;
2179 2180
    }

2181 2182 2183 2184 2185
    ret = 0;

 cleanup:
    virCPUDefFree(oldguest);
    return ret;
2186 2187 2188
}


2189 2190
static int
x86Update(virCPUDefPtr guest,
2191
          const virCPUDef *host)
2192
{
2193
    switch ((virCPUMode) guest->mode) {
2194 2195 2196 2197
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2198 2199
        guest->match = VIR_CPU_MATCH_EXACT;
        return x86UpdateHostModel(guest, host, false);
2200

2201
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2202
        guest->match = VIR_CPU_MATCH_MINIMUM;
2203
        return x86UpdateHostModel(guest, host, true);
2204 2205 2206 2207 2208

    case VIR_CPU_MODE_LAST:
        break;
    }

2209 2210
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2211 2212 2213
    return -1;
}

2214 2215 2216 2217

static int
x86HasFeature(const virCPUData *data,
              const char *name)
D
Daniel P. Berrange 已提交
2218
{
J
Jiri Denemark 已提交
2219
    virCPUx86MapPtr map;
2220
    virCPUx86FeaturePtr feature;
D
Daniel P. Berrange 已提交
2221 2222
    int ret = -1;

2223
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2224 2225 2226 2227 2228
        return -1;

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

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

2231
 cleanup:
D
Daniel P. Berrange 已提交
2232 2233
    return ret;
}
2234

2235 2236 2237
static int
x86GetModels(char ***models)
{
J
Jiri Denemark 已提交
2238
    virCPUx86MapPtr map;
2239
    size_t i;
2240 2241 2242 2243

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

2244 2245 2246
    if (models) {
        if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
            goto error;
2247

2248 2249
        for (i = 0; i < map->nmodels; i++) {
            if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
2250 2251
                goto error;
        }
2252 2253
    }

2254
    return map->nmodels;
2255 2256

 error:
2257 2258 2259 2260
    if (models) {
        virStringFreeList(*models);
        *models = NULL;
    }
2261 2262 2263
    return -1;
}

2264

J
Jiri Denemark 已提交
2265 2266 2267 2268 2269
struct cpuArchDriver cpuDriverX86 = {
    .name = "x86",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
    .compare    = x86Compare,
2270
    .decode     = x86DecodeCPUData,
J
Jiri Denemark 已提交
2271
    .encode     = x86Encode,
2272
    .free       = x86FreeCPUData,
J
Jiri Denemark 已提交
2273 2274 2275 2276 2277
#if HAVE_CPUID
    .nodeData   = x86NodeData,
#else
    .nodeData   = NULL,
#endif
2278
    .guestData  = x86GuestData,
2279
    .baseline   = x86Baseline,
2280
    .update     = x86Update,
D
Daniel P. Berrange 已提交
2281
    .hasFeature = x86HasFeature,
2282 2283
    .dataFormat = x86CPUDataFormat,
    .dataParse  = x86CPUDataParse,
2284
    .getModels  = x86GetModels,
J
Jiri Denemark 已提交
2285
};