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

#include <config.h>

#include <stdint.h>

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

#define VIR_FROM_THIS VIR_FROM_CPU

39 40
VIR_LOG_INIT("cpu.cpu_x86");

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

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

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

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

54 55 56
typedef struct _virCPUx86Feature virCPUx86Feature;
typedef virCPUx86Feature *virCPUx86FeaturePtr;
struct _virCPUx86Feature {
J
Jiri Denemark 已提交
57
    char *name;
58
    virCPUx86Data *data;
J
Jiri Denemark 已提交
59
    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
    {VIR_CPU_x86_KVM_CLOCKSOURCE,  { .eax_in = 0x40000001, .eax = 0x00000001 }},
    {VIR_CPU_x86_KVM_NOP_IO_DELAY, { .eax_in = 0x40000001, .eax = 0x00000002 }},
    {VIR_CPU_x86_KVM_MMU_OP,       { .eax_in = 0x40000001, .eax = 0x00000004 }},
    {VIR_CPU_x86_KVM_CLOCKSOURCE2, { .eax_in = 0x40000001, .eax = 0x00000008 }},
    {VIR_CPU_x86_KVM_ASYNC_PF,     { .eax_in = 0x40000001, .eax = 0x00000010 }},
    {VIR_CPU_x86_KVM_STEAL_TIME,   { .eax_in = 0x40000001, .eax = 0x00000020 }},
    {VIR_CPU_x86_KVM_PV_EOI,       { .eax_in = 0x40000001, .eax = 0x00000040 }},
    {VIR_CPU_x86_KVM_PV_UNHALT,    { .eax_in = 0x40000001, .eax = 0x00000080 }},
79
    {VIR_CPU_x86_KVM_CLOCKSOURCE_STABLE_BIT,
80 81 82 83 84 85 86 87 88
                                   { .eax_in = 0x40000001, .eax = 0x01000000 }},
    {VIR_CPU_x86_KVM_HV_RUNTIME,   { .eax_in = 0x40000003, .eax = 0x00000001 }},
    {VIR_CPU_x86_KVM_HV_SYNIC,     { .eax_in = 0x40000003, .eax = 0x00000004 }},
    {VIR_CPU_x86_KVM_HV_STIMER,    { .eax_in = 0x40000003, .eax = 0x00000008 }},
    {VIR_CPU_x86_KVM_HV_RELAXED,   { .eax_in = 0x40000003, .eax = 0x00000020 }},
    {VIR_CPU_x86_KVM_HV_SPINLOCK,  { .eax_in = 0x40000003, .eax = 0x00000022 }},
    {VIR_CPU_x86_KVM_HV_VAPIC,     { .eax_in = 0x40000003, .eax = 0x00000030 }},
    {VIR_CPU_x86_KVM_HV_VPINDEX,   { .eax_in = 0x40000003, .eax = 0x00000040 }},
    {VIR_CPU_x86_KVM_HV_RESET,     { .eax_in = 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
static int
virCPUx86CPUIDSorter(const void *a, const void *b)
{
    virCPUx86CPUID *da = (virCPUx86CPUID *) a;
    virCPUx86CPUID *db = (virCPUx86CPUID *) b;

206
    if (da->eax_in > db->eax_in)
207
        return 1;
208
    else if (da->eax_in < db->eax_in)
209 210 211 212 213
        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,
235
             uint32_t eax_in)
J
Jiri Denemark 已提交
236
{
237
    size_t i;
J
Jiri Denemark 已提交
238

239
    for (i = 0; i < data->len; i++) {
240
        if (data->data[i].eax_in == eax_in)
241
            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
    if ((existing = x86DataCpuid(data, cpuid->eax_in))) {
311 312 313 314 315
        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
    while ((cpuid2 = x86DataCpuidNext(&iter))) {
334
        cpuid1 = x86DataCpuid(data1, cpuid2->eax_in);
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
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
357
        cpuid2 = x86DataCpuid(data2, cpuid1->eax_in);
358
        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
    while ((cpuid1 = x86DataCpuidNext(&iter))) {
372
        cpuid2 = x86DataCpuid(data2, cpuid1->eax_in);
J
Jiri Denemark 已提交
373 374 375 376
        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
    while ((cpuidSubset = x86DataCpuidNext(&iter))) {
400
        if (!(cpuid = x86DataCpuid(data, cpuidSubset->eax_in)) ||
J
Jiri Denemark 已提交
401 402
            !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];
441
        if ((cpuid = x86DataCpuid(data, vendor->cpuid.eax_in)) &&
J
Jiri Denemark 已提交
442 443 444 445 446 447 448 449 450 451
            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.eax_in = 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
    unsigned long eax_in;
    unsigned long eax, ebx, ecx, edx;
    int ret_eax_in, ret_eax, ret_ebx, ret_ecx, ret_edx;
673 674 675

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

676 677 678
    eax_in = 0;
    eax = ebx = ecx = edx = 0;
    ret_eax_in = virXPathULongHex("string(@eax_in)", ctxt, &eax_in);
679 680 681 682 683
    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);

684 685
    if (ret_eax_in < 0 ||
        ret_eax == -2 || ret_ebx == -2 || ret_ecx == -2 || ret_edx == -2)
686 687
        return -1;

688
    cpuid->eax_in = eax_in;
689 690 691 692 693 694 695 696
    cpuid->eax = eax;
    cpuid->ebx = ebx;
    cpuid->ecx = ecx;
    cpuid->edx = edx;
    return 0;
}


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

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

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

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

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

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

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

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

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


J
Jiri Denemark 已提交
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 782 783
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;
}

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

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

    for (i = 0; i < cpu->nfeatures; i++) {
795
        virCPUx86FeaturePtr feature;
796 797 798 799 800 801 802 803 804 805 806 807
        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;

808
 error:
809 810 811 812 813
    virCPUx86DataFree(data);
    return NULL;
}


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

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

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

    return model;
}


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

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


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

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

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

    return copy;
}


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

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

    return NULL;
}


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

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

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

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

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

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

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

    return model;

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


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

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

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

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

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

        VIR_FREE(name);

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

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

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

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

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

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

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

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

J
Jiri Denemark 已提交
1096
 cleanup:
J
Jiri Denemark 已提交
1097
    VIR_FREE(vendor);
1098
    VIR_FREE(nodes);
J
Jiri Denemark 已提交
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 1126 1127
    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 已提交
1128 1129 1130 1131
}


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

1136
    if (!map)
J
Jiri Denemark 已提交
1137 1138
        return;

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

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

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

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

J
Jiri Denemark 已提交
1156 1157 1158 1159
    VIR_FREE(map);
}


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

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

    return 0;
}


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

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

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

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

        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;

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

    return 0;

1219
 error:
1220 1221 1222 1223 1224
    x86FeatureFree(feature);
    return -1;
}


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

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

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

1236 1237 1238
    if (x86MapLoadInternalFeatures(map) < 0)
        goto error;

J
Jiri Denemark 已提交
1239 1240
    return map;

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


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

    return 0;
}


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

J
Jiri Denemark 已提交
1263
    return cpuMap;
1264 1265 1266
}


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

    virBufferAddLit(&buf, "<cpudata arch='x86'>\n");
    while ((cpuid = x86DataCpuidNext(&iter))) {
        virBufferAsprintf(&buf,
1277
                          "  <cpuid eax_in='0x%08x'"
1278 1279
                          " eax='0x%08x' ebx='0x%08x'"
                          " ecx='0x%08x' edx='0x%08x'/>\n",
1280
                          cpuid->eax_in,
1281 1282 1283 1284
                          cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx);
    }
    virBufferAddLit(&buf, "</cpudata>\n");

1285
    if (virBufferCheckError(&buf) < 0)
1286 1287 1288 1289 1290 1291 1292
        return NULL;

    return virBufferContentAndReset(&buf);
}


static virCPUDataPtr
1293
x86CPUDataParse(xmlXPathContextPtr ctxt)
1294 1295 1296
{
    xmlNodePtr *nodes = NULL;
    virCPUDataPtr cpuData = NULL;
1297
    virCPUx86Data *data = NULL;
1298
    virCPUx86CPUID cpuid;
1299 1300 1301 1302 1303 1304
    size_t i;
    int n;

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

1305
    n = virXPathNodeSet("/cpudata/cpuid", ctxt, &nodes);
J
Jiri Denemark 已提交
1306
    if (n <= 0) {
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
        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;
        }
1319
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
1320 1321 1322
            goto cleanup;
    }

1323
    cpuData = virCPUx86MakeData(VIR_ARCH_X86_64, &data);
1324

1325
 cleanup:
1326
    VIR_FREE(nodes);
1327
    virCPUx86DataFree(data);
1328 1329 1330 1331
    return cpuData;
}


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

1357

J
Jiri Denemark 已提交
1358 1359 1360
static virCPUCompareResult
x86Compute(virCPUDefPtr host,
           virCPUDefPtr cpu,
1361
           virCPUDataPtr *guest,
1362
           char **message)
J
Jiri Denemark 已提交
1363
{
J
Jiri Denemark 已提交
1364
    virCPUx86MapPtr map = NULL;
J
Jiri Denemark 已提交
1365 1366 1367 1368 1369 1370 1371 1372
    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 已提交
1373
    virCPUCompareResult ret;
1374
    virCPUx86CompareResult result;
J
Jiri Denemark 已提交
1375
    virArch arch;
1376
    size_t i;
J
Jiri Denemark 已提交
1377

1378 1379 1380 1381 1382 1383
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

1384
    if (cpu->arch != VIR_ARCH_NONE) {
J
Jiri Denemark 已提交
1385 1386 1387
        bool found = false;

        for (i = 0; i < ARRAY_CARDINALITY(archs); i++) {
1388
            if (archs[i] == cpu->arch) {
J
Jiri Denemark 已提交
1389 1390 1391 1392 1393
                found = true;
                break;
            }
        }

1394
        if (!found) {
1395 1396
            VIR_DEBUG("CPU arch %s does not match host arch",
                      virArchToString(cpu->arch));
1397 1398 1399
            if (message &&
                virAsprintf(message,
                            _("CPU arch %s does not match host arch"),
1400
                            virArchToString(cpu->arch)) < 0)
1401
                goto error;
J
Jiri Denemark 已提交
1402
            return VIR_CPU_COMPARE_INCOMPATIBLE;
1403
        }
J
Jiri Denemark 已提交
1404 1405 1406
        arch = cpu->arch;
    } else {
        arch = host->arch;
J
Jiri Denemark 已提交
1407 1408
    }

J
Jiri Denemark 已提交
1409 1410 1411 1412
    if (cpu->vendor &&
        (!host->vendor || STRNEQ(cpu->vendor, host->vendor))) {
        VIR_DEBUG("host CPU vendor does not match required CPU vendor %s",
                  cpu->vendor);
1413 1414 1415 1416 1417
        if (message &&
            virAsprintf(message,
                        _("host CPU vendor does not match required "
                          "CPU vendor %s"),
                        cpu->vendor) < 0)
1418
            goto error;
1419

J
Jiri Denemark 已提交
1420 1421 1422
        return VIR_CPU_COMPARE_INCOMPATIBLE;
    }

1423
    if (!(map = virCPUx86GetMap()) ||
1424
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)) ||
J
Jiri Denemark 已提交
1425 1426 1427 1428 1429
        !(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 已提交
1430 1431
        goto error;

1432 1433 1434 1435
    x86DataIntersect(cpu_forbid->data, host_model->data);
    if (!x86DataIsEmpty(cpu_forbid->data)) {
        virX86CpuIncompatible(N_("Host CPU provides forbidden features"),
                              cpu_forbid->data);
1436
        goto cleanup;
J
Jiri Denemark 已提交
1437 1438
    }

1439 1440 1441 1442 1443
    /* 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 已提交
1444
    x86DataSubtract(cpu_require->data, cpu_disable->data);
J
Jiri Denemark 已提交
1445 1446
    result = x86ModelCompare(host_model, cpu_require);
    if (result == SUBSET || result == UNRELATED) {
1447 1448 1449 1450
        x86DataSubtract(cpu_require->data, host_model->data);
        virX86CpuIncompatible(N_("Host CPU does not provide required "
                                 "features"),
                              cpu_require->data);
1451
        goto cleanup;
J
Jiri Denemark 已提交
1452 1453 1454 1455
    }

    ret = VIR_CPU_COMPARE_IDENTICAL;

1456
    if (!(diff = x86ModelCopy(host_model)))
1457
        goto error;
J
Jiri Denemark 已提交
1458

J
Jiri Denemark 已提交
1459 1460 1461 1462
    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 已提交
1463

J
Jiri Denemark 已提交
1464 1465
    if (!x86DataIsEmpty(diff->data))
        ret = VIR_CPU_COMPARE_SUPERSET;
J
Jiri Denemark 已提交
1466 1467 1468 1469

    if (ret == VIR_CPU_COMPARE_SUPERSET
        && cpu->type == VIR_CPU_TYPE_GUEST
        && cpu->match == VIR_CPU_MATCH_STRICT) {
1470 1471 1472
        virX86CpuIncompatible(N_("Host CPU does not strictly match guest CPU: "
                                 "Extra features"),
                              diff->data);
1473
        goto cleanup;
J
Jiri Denemark 已提交
1474 1475
    }

1476
    if (guest) {
1477
        virCPUx86Data *guestData;
1478

1479
        if (!(guest_model = x86ModelCopy(host_model)))
1480
            goto error;
J
Jiri Denemark 已提交
1481

1482 1483 1484 1485 1486
        if (cpu->vendor &&
            virCPUx86DataAddCPUID(guest_model->data,
                                  &host_model->vendor->cpuid) < 0)
            goto error;

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

J
Jiri Denemark 已提交
1491
        if (x86DataAdd(guest_model->data, cpu_force->data))
1492
            goto error;
J
Jiri Denemark 已提交
1493

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

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

1503
 cleanup:
J
Jiri Denemark 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
    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;

1515
 error:
J
Jiri Denemark 已提交
1516
    ret = VIR_CPU_COMPARE_ERROR;
1517
    goto cleanup;
J
Jiri Denemark 已提交
1518
}
1519
#undef virX86CpuIncompatible
J
Jiri Denemark 已提交
1520 1521 1522 1523


static virCPUCompareResult
x86Compare(virCPUDefPtr host,
1524
           virCPUDefPtr cpu,
1525
           bool failIncompatible)
J
Jiri Denemark 已提交
1526
{
1527 1528 1529 1530 1531
    virCPUCompareResult ret;
    char *message = NULL;

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

1532
    if (failIncompatible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) {
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
        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 已提交
1543 1544 1545 1546 1547 1548
}


static virCPUCompareResult
x86GuestData(virCPUDefPtr host,
             virCPUDefPtr guest,
1549
             virCPUDataPtr *data,
1550
             char **message)
J
Jiri Denemark 已提交
1551
{
1552
    return x86Compute(host, guest, data, message);
J
Jiri Denemark 已提交
1553 1554
}

1555

1556 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
/*
 * 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 已提交
1593 1594
static int
x86Decode(virCPUDefPtr cpu,
1595
          const virCPUx86Data *data,
1596
          const char **models,
1597
          unsigned int nmodels,
1598 1599
          const char *preferred,
          unsigned int flags)
J
Jiri Denemark 已提交
1600 1601
{
    int ret = -1;
J
Jiri Denemark 已提交
1602
    virCPUx86MapPtr map;
J
Jiri Denemark 已提交
1603
    virCPUx86ModelPtr candidate;
1604 1605
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
1606 1607 1608
    virCPUx86Data *copy = NULL;
    virCPUx86Data *features = NULL;
    const virCPUx86Data *cpuData = NULL;
J
Jiri Denemark 已提交
1609
    virCPUx86VendorPtr vendor;
1610
    ssize_t i;
1611
    int rc;
J
Jiri Denemark 已提交
1612

1613 1614
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, -1);
1615

1616
    if (!data || !(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1617 1618
        return -1;

J
Jiri Denemark 已提交
1619 1620
    vendor = x86DataToVendor(data, map);

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

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

J
Jiri Denemark 已提交
1654 1655 1656 1657
        if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
            goto cleanup;
        cpuCandidate->type = cpu->type;

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

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

1676 1677 1678 1679 1680
    /* 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++) {
1681 1682 1683 1684
            size_t j;
            for (j = 0; j < map->nblockers; j++) {
                if (STREQ(map->migrate_blockers[j]->name,
                          cpuModel->features[i].name)) {
1685
                    VIR_FREE(cpuModel->features[i].name);
1686 1687
                    VIR_DELETE_ELEMENT_INPLACE(cpuModel->features, i,
                                               cpuModel->nfeatures);
1688 1689 1690 1691 1692
                }
            }
        }
    }

1693 1694 1695
    if (flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) {
        if (!(copy = x86DataCopy(cpuData)) ||
            !(features = x86DataFromCPUFeatures(cpuModel, map)))
J
Jiri Denemark 已提交
1696
            goto cleanup;
1697 1698 1699 1700

        x86DataSubtract(copy, features);
        if (x86DataToCPUFeatures(cpuModel, VIR_CPU_FEATURE_REQUIRE,
                                 copy, map) < 0)
J
Jiri Denemark 已提交
1701
            goto cleanup;
1702 1703
    }

J
Jiri Denemark 已提交
1704 1705 1706
    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
        goto cleanup;

1707 1708 1709 1710
    cpu->model = cpuModel->model;
    cpu->nfeatures = cpuModel->nfeatures;
    cpu->features = cpuModel->features;
    VIR_FREE(cpuModel);
J
Jiri Denemark 已提交
1711 1712 1713

    ret = 0;

J
Jiri Denemark 已提交
1714
 cleanup:
1715
    virCPUDefFree(cpuModel);
1716 1717
    virCPUx86DataFree(copy);
    virCPUx86DataFree(features);
J
Jiri Denemark 已提交
1718 1719 1720
    return ret;
}

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

1732

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

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

J
Jiri Denemark 已提交
1744 1745
    data = model->data;
    model->data = NULL;
J
Jiri Denemark 已提交
1746 1747 1748 1749 1750 1751 1752
    x86ModelFree(model);

    return data;
}


static int
J
Jiri Denemark 已提交
1753
x86Encode(virArch arch,
1754
          const virCPUDef *cpu,
1755 1756 1757 1758 1759 1760
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
1761
{
J
Jiri Denemark 已提交
1762
    virCPUx86MapPtr map = NULL;
1763 1764 1765 1766 1767 1768
    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 已提交
1769

1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
    if (forced)
        *forced = NULL;
    if (required)
        *required = NULL;
    if (optional)
        *optional = NULL;
    if (disabled)
        *disabled = NULL;
    if (forbidden)
        *forbidden = NULL;
    if (vendor)
        *vendor = NULL;

1783
    if (!(map = virCPUx86GetMap()))
J
Jiri Denemark 已提交
1784 1785 1786 1787 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
        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 已提交
1816
    if (vendor) {
1817
        virCPUx86VendorPtr v = NULL;
J
Jiri Denemark 已提交
1818 1819

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

        if (v &&
            (VIR_ALLOC(data_vendor) < 0 ||
1827
             virCPUx86DataAddCPUID(data_vendor, &v->cpuid) < 0)) {
J
Jiri Denemark 已提交
1828 1829 1830 1831
            goto error;
        }
    }

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

1851
    return 0;
J
Jiri Denemark 已提交
1852

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


#if HAVE_CPUID
static inline void
1878
cpuidCall(virCPUx86CPUID *cpuid)
J
Jiri Denemark 已提交
1879
{
1880
# if __x86_64__
1881 1882 1883 1884
    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 已提交
1885 1886 1887 1888
        : "=a" (cpuid->eax),
          "=b" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
1889
        : "a" (cpuid->eax_in));
1890
# else
J
Jiri Denemark 已提交
1891 1892 1893 1894
    /* 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;"
1895 1896 1897
        "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 已提交
1898 1899 1900 1901 1902 1903 1904
        "cpuid;"
        "mov %%ebx, %1;"
        "pop %%ebx;"
        : "=a" (cpuid->eax),
          "=r" (cpuid->ebx),
          "=c" (cpuid->ecx),
          "=d" (cpuid->edx)
1905
        : "a" (cpuid->eax_in)
J
Jiri Denemark 已提交
1906
        : "cc");
1907
# endif
J
Jiri Denemark 已提交
1908 1909 1910 1911
}


static int
1912
cpuidSet(uint32_t base, virCPUx86Data *data)
J
Jiri Denemark 已提交
1913 1914 1915
{
    uint32_t max;
    uint32_t i;
1916
    virCPUx86CPUID cpuid = { base, 0, 0, 0, 0 };
J
Jiri Denemark 已提交
1917 1918

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

1921
    for (i = base; i <= max; i++) {
1922
        cpuid.eax_in = i;
J
Jiri Denemark 已提交
1923
        cpuidCall(&cpuid);
1924 1925
        if (virCPUx86DataAddCPUID(data, &cpuid) < 0)
            return -1;
J
Jiri Denemark 已提交
1926 1927
    }

1928
    return 0;
J
Jiri Denemark 已提交
1929 1930 1931
}


1932
static virCPUDataPtr
1933
x86NodeData(virArch arch)
J
Jiri Denemark 已提交
1934
{
1935
    virCPUDataPtr cpuData = NULL;
1936
    virCPUx86Data *data;
J
Jiri Denemark 已提交
1937

1938
    if (VIR_ALLOC(data) < 0)
J
Jiri Denemark 已提交
1939 1940
        return NULL;

1941
    if (cpuidSet(CPUX86_BASIC, data) < 0)
J
Jiri Denemark 已提交
1942 1943
        goto error;

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

1947
    if (!(cpuData = virCPUx86MakeData(arch, &data)))
1948 1949 1950
        goto error;

    return cpuData;
J
Jiri Denemark 已提交
1951

1952
 error:
1953
    virCPUx86DataFree(data);
J
Jiri Denemark 已提交
1954 1955 1956 1957 1958 1959

    return NULL;
}
#endif


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

1977 1978 1979
    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES |
                  VIR_CONNECT_BASELINE_CPU_MIGRATABLE, NULL);

1980
    if (!(map = virCPUx86GetMap()))
1981 1982
        goto error;

1983
    if (!(base_model = x86ModelFromCPU(cpus[0], map, VIR_CPU_FEATURE_REQUIRE)))
1984 1985
        goto error;

1986
    if (VIR_ALLOC(cpu) < 0)
1987
        goto error;
1988 1989

    cpu->arch = cpus[0]->arch;
1990 1991
    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
1992

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

2001
    modelName = cpus[0]->model;
2002
    for (i = 1; i < ncpus; i++) {
J
Jiri Denemark 已提交
2003 2004
        const char *vn = NULL;

2005 2006 2007 2008 2009 2010 2011 2012 2013
        if (matchingNames && cpus[i]->model) {
            if (!modelName) {
                modelName = cpus[i]->model;
            } else if (STRNEQ(modelName, cpus[i]->model)) {
                modelName = NULL;
                matchingNames = false;
            }
        }

2014
        if (!(model = x86ModelFromCPU(cpus[i], map, VIR_CPU_FEATURE_REQUIRE)))
2015 2016
            goto error;

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

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

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

J
Jiri Denemark 已提交
2047
        x86DataIntersect(base_model->data, model->data);
2048
        x86ModelFree(model);
J
Jiri Denemark 已提交
2049
        model = NULL;
2050 2051
    }

J
Jiri Denemark 已提交
2052
    if (x86DataIsEmpty(base_model->data)) {
2053 2054
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("CPUs are incompatible"));
2055 2056 2057
        goto error;
    }

2058
    if (vendor && virCPUx86DataAddCPUID(base_model->data, &vendor->cpuid) < 0)
2059
        goto error;
J
Jiri Denemark 已提交
2060

2061
    if (x86Decode(cpu, base_model->data, models, nmodels, modelName, flags) < 0)
2062 2063
        goto error;

2064 2065 2066
    if (STREQ_NULLABLE(cpu->model, modelName))
        cpu->fallback = VIR_CPU_FALLBACK_FORBID;

2067 2068 2069
    if (!outputVendor)
        VIR_FREE(cpu->vendor);

2070 2071
    cpu->arch = VIR_ARCH_NONE;

2072
 cleanup:
2073 2074 2075 2076
    x86ModelFree(base_model);

    return cpu;

2077
 error:
J
Jiri Denemark 已提交
2078
    x86ModelFree(model);
2079 2080 2081 2082 2083 2084
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
}


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

2094
    if (!(map = virCPUx86GetMap()) ||
2095
        !(host_model = x86ModelFromCPU(host, map, VIR_CPU_FEATURE_REQUIRE)))
2096 2097 2098 2099
        goto cleanup;

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

J
Jiri Denemark 已提交
2108
            if (x86DataIsSubset(host_model->data, feature->data))
2109 2110 2111 2112 2113 2114 2115 2116
                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 已提交
2117 2118 2119
        if (x86ModelSubtractCPU(host_model, guest, map) ||
            x86DataToCPUFeatures(guest, VIR_CPU_FEATURE_REQUIRE,
                                 host_model->data, map))
2120 2121 2122 2123 2124
            goto cleanup;
    }

    ret = 0;

2125
 cleanup:
2126 2127 2128 2129
    x86ModelFree(host_model);
    return ret;
}

2130 2131 2132

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

2141 2142
    if (!(map = virCPUx86GetMap()))
        goto cleanup;
2143 2144 2145

    /* update the host model according to the desired configuration */
    if (!(oldguest = virCPUDefCopy(guest)))
2146
        goto cleanup;
2147 2148 2149

    virCPUDefFreeModel(guest);
    if (virCPUDefCopyModel(guest, host, true) < 0)
2150
        goto cleanup;
2151

2152 2153 2154 2155 2156 2157
    if (oldguest->vendor_id) {
        VIR_FREE(guest->vendor_id);
        if (VIR_STRDUP(guest->vendor_id, oldguest->vendor_id) < 0)
            goto cleanup;
    }

2158 2159 2160 2161
    /* 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++) {
2162 2163 2164
        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 已提交
2165
                VIR_FREE(guest->features[i].name);
2166
                VIR_DELETE_ELEMENT_INPLACE(guest->features, i, guest->nfeatures);
J
Ján Tomko 已提交
2167
            }
2168 2169
        }
    }
2170
    for (i = 0; !passthrough && i < oldguest->nfeatures; i++) {
2171 2172 2173
        if (virCPUDefUpdateFeature(guest,
                                   oldguest->features[i].name,
                                   oldguest->features[i].policy) < 0)
2174
            goto cleanup;
2175 2176
    }

2177 2178 2179 2180 2181
    ret = 0;

 cleanup:
    virCPUDefFree(oldguest);
    return ret;
2182 2183 2184
}


2185 2186
static int
x86Update(virCPUDefPtr guest,
2187
          const virCPUDef *host)
2188
{
2189
    switch ((virCPUMode) guest->mode) {
2190 2191 2192 2193
    case VIR_CPU_MODE_CUSTOM:
        return x86UpdateCustom(guest, host);

    case VIR_CPU_MODE_HOST_MODEL:
2194 2195
        guest->match = VIR_CPU_MATCH_EXACT;
        return x86UpdateHostModel(guest, host, false);
2196

2197
    case VIR_CPU_MODE_HOST_PASSTHROUGH:
2198
        guest->match = VIR_CPU_MATCH_MINIMUM;
2199
        return x86UpdateHostModel(guest, host, true);
2200 2201 2202 2203 2204

    case VIR_CPU_MODE_LAST:
        break;
    }

2205 2206
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Unexpected CPU mode: %d"), guest->mode);
2207 2208 2209
    return -1;
}

2210 2211 2212 2213

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

2219
    if (!(map = virCPUx86GetMap()))
D
Daniel P. Berrange 已提交
2220 2221 2222 2223 2224
        return -1;

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

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

2227
 cleanup:
D
Daniel P. Berrange 已提交
2228 2229
    return ret;
}
2230

2231 2232 2233
static int
x86GetModels(char ***models)
{
J
Jiri Denemark 已提交
2234
    virCPUx86MapPtr map;
2235
    size_t i;
2236 2237 2238 2239

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

2240 2241 2242
    if (models) {
        if (VIR_ALLOC_N(*models, map->nmodels + 1) < 0)
            goto error;
2243

2244 2245
        for (i = 0; i < map->nmodels; i++) {
            if (VIR_STRDUP((*models)[i], map->models[i]->name) < 0)
2246 2247
                goto error;
        }
2248 2249
    }

2250
    return map->nmodels;
2251 2252

 error:
2253 2254 2255 2256
    if (models) {
        virStringFreeList(*models);
        *models = NULL;
    }
2257 2258 2259
    return -1;
}

2260

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