cpu.c 25.4 KB
Newer Older
J
Jiri Denemark 已提交
1 2 3
/*
 * cpu.c: internal functions for CPU manipulation
 *
4
 * Copyright (C) 2009-2013 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
 *
 * Authors:
 *      Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

26
#include "virlog.h"
27
#include "viralloc.h"
28
#include "virxml.h"
J
Jiri Denemark 已提交
29
#include "cpu.h"
30
#include "cpu_map.h"
J
Jiri Denemark 已提交
31
#include "cpu_x86.h"
32
#include "cpu_ppc64.h"
T
Thang Pham 已提交
33
#include "cpu_s390.h"
C
Chuck Short 已提交
34
#include "cpu_arm.h"
35
#include "util/virstring.h"
J
Jiri Denemark 已提交
36 37 38 39


#define VIR_FROM_THIS VIR_FROM_CPU

40 41
VIR_LOG_INIT("cpu.cpu");

J
Jiri Denemark 已提交
42 43
static struct cpuArchDriver *drivers[] = {
    &cpuDriverX86,
44
    &cpuDriverPPC64,
T
Thang Pham 已提交
45
    &cpuDriverS390,
C
Chuck Short 已提交
46
    &cpuDriverArm,
J
Jiri Denemark 已提交
47 48 49 50
};


static struct cpuArchDriver *
51
cpuGetSubDriver(virArch arch)
J
Jiri Denemark 已提交
52
{
53 54
    size_t i;
    size_t j;
J
Jiri Denemark 已提交
55

56
    if (arch == VIR_ARCH_NONE) {
57 58
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("undefined hardware architecture"));
J
Jiri Denemark 已提交
59 60 61
        return NULL;
    }

J
Jiri Denemark 已提交
62
    for (i = 0; i < ARRAY_CARDINALITY(drivers); i++) {
J
Jiri Denemark 已提交
63
        for (j = 0; j < drivers[i]->narch; j++) {
64
            if (arch == drivers[i]->arch[j])
J
Jiri Denemark 已提交
65 66 67 68
                return drivers[i];
        }
    }

J
Jiri Denemark 已提交
69 70 71 72
    virReportError(VIR_ERR_NO_SUPPORT,
                   _("'%s' architecture is not supported by CPU driver"),
                   virArchToString(arch));
    return NULL;
J
Jiri Denemark 已提交
73 74 75
}


76 77 78 79 80
static struct cpuArchDriver *
cpuGetSubDriverByName(const char *name)
{
    size_t i;

J
Jiri Denemark 已提交
81
    for (i = 0; i < ARRAY_CARDINALITY(drivers); i++) {
82 83 84 85 86 87 88 89 90 91 92
        if (STREQ_NULLABLE(name, drivers[i]->name))
            return drivers[i];
    }

    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("CPU driver '%s' does not exist"),
                   name);
    return NULL;
}


93
/**
J
Jiri Denemark 已提交
94
 * virCPUCompareXML:
95
 *
J
Jiri Denemark 已提交
96
 * @arch: CPU architecture
97 98
 * @host: host CPU definition
 * @xml: XML description of either guest or host CPU to be compared with @host
99
 * @failIncompatible: return an error instead of VIR_CPU_COMPARE_INCOMPATIBLE
100 101 102 103 104 105
 *
 * Compares the CPU described by @xml with @host CPU.
 *
 * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when
 * the two CPUs are incompatible, VIR_CPU_COMPARE_IDENTICAL when the two CPUs
 * are identical, VIR_CPU_COMPARE_SUPERSET when the @xml CPU is a superset of
106 107 108
 * the @host CPU. If @failIncompatible is true, the function will return
 * VIR_CPU_COMPARE_ERROR (and set VIR_ERR_CPU_INCOMPATIBLE error) when the
 * two CPUs are incompatible.
109
 */
J
Jiri Denemark 已提交
110
virCPUCompareResult
J
Jiri Denemark 已提交
111 112 113 114
virCPUCompareXML(virArch arch,
                 virCPUDefPtr host,
                 const char *xml,
                 bool failIncompatible)
J
Jiri Denemark 已提交
115 116 117 118 119 120
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR;

J
Jiri Denemark 已提交
121 122 123 124 125 126 127
    VIR_DEBUG("arch=%s, host=%p, xml=%s",
              virArchToString(arch), host, NULLSTR(xml));

    if (!xml) {
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("missing CPU definition"));
        goto cleanup;
    }
128

129
    if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt)))
130
        goto cleanup;
J
Jiri Denemark 已提交
131

J
Jiri Denemark 已提交
132
    if (!(cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO)))
J
Jiri Denemark 已提交
133 134
        goto cleanup;

J
Jiri Denemark 已提交
135
    ret = virCPUCompare(arch, host, cpu, failIncompatible);
J
Jiri Denemark 已提交
136

137
 cleanup:
J
Jiri Denemark 已提交
138 139 140 141 142 143 144 145
    virCPUDefFree(cpu);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return ret;
}


146
/**
J
Jiri Denemark 已提交
147
 * virCPUCompare:
148
 *
J
Jiri Denemark 已提交
149
 * @arch: CPU architecture
150 151
 * @host: host CPU definition
 * @cpu: either guest or host CPU to be compared with @host
152
 * @failIncompatible: return an error instead of VIR_CPU_COMPARE_INCOMPATIBLE
153 154 155 156 157 158
 *
 * Compares the CPU described by @cpu with @host CPU.
 *
 * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when
 * the two CPUs are incompatible, VIR_CPU_COMPARE_IDENTICAL when the two CPUs
 * are identical, VIR_CPU_COMPARE_SUPERSET when the @cpu CPU is a superset of
159 160 161
 * the @host CPU. If @failIncompatible is true, the function will return
 * VIR_CPU_COMPARE_ERROR (and set VIR_ERR_CPU_INCOMPATIBLE error) when the
 * two CPUs are incompatible.
162
 */
J
Jiri Denemark 已提交
163
virCPUCompareResult
J
Jiri Denemark 已提交
164 165 166 167
virCPUCompare(virArch arch,
              virCPUDefPtr host,
              virCPUDefPtr cpu,
              bool failIncompatible)
J
Jiri Denemark 已提交
168 169 170
{
    struct cpuArchDriver *driver;

J
Jiri Denemark 已提交
171 172
    VIR_DEBUG("arch=%s, host=%p, cpu=%p",
              virArchToString(arch), host, cpu);
173

J
Jiri Denemark 已提交
174
    if (!(driver = cpuGetSubDriver(arch)))
J
Jiri Denemark 已提交
175 176
        return VIR_CPU_COMPARE_ERROR;

J
Jiri Denemark 已提交
177
    if (!driver->compare) {
178 179
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot compare CPUs of %s architecture"),
J
Jiri Denemark 已提交
180
                       virArchToString(arch));
J
Jiri Denemark 已提交
181 182 183
        return VIR_CPU_COMPARE_ERROR;
    }

184
    return driver->compare(host, cpu, failIncompatible);
J
Jiri Denemark 已提交
185 186 187
}


188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/**
 * cpuDecode:
 *
 * @cpu: CPU definition stub to be filled in
 * @data: internal CPU data to be decoded into @cpu definition
 * @models: list of CPU models that can be considered when decoding @data
 * @nmodels: number of CPU models in @models
 * @preferred: CPU models that should be used if possible
 *
 * Decodes internal CPU data into a CPU definition consisting of a CPU model
 * and a list of CPU features. The @cpu model stub is supposed to have arch,
 * type, match and fallback members set, this function will add the rest. If
 * @models list is NULL, all models supported by libvirt will be considered
 * when decoding the data. In general, this function will select the model
 * closest to the CPU specified by @data unless @preferred is non-NULL, in
 * which case the @preferred model will be used as long as it is compatible
 * with @data.
 *
 * For VIR_ARCH_I686 and VIR_ARCH_X86_64 architectures this means the computed
 * CPU definition will have the shortest possible list of additional features.
 * When @preferred is non-NULL, the @preferred model will be used even if
 * other models would result in a shorter list of additional features.
 *
 * Returns 0 on success, -1 on error.
 */
J
Jiri Denemark 已提交
213
int
214
cpuDecode(virCPUDefPtr cpu,
215
          const virCPUData *data,
216
          const char **models,
217 218
          unsigned int nmodels,
          const char *preferred)
J
Jiri Denemark 已提交
219 220 221
{
    struct cpuArchDriver *driver;

222 223
    VIR_DEBUG("cpu=%p, data=%p, nmodels=%u, preferred=%s",
              cpu, data, nmodels, NULLSTR(preferred));
224
    if (models) {
225
        size_t i;
226
        for (i = 0; i < nmodels; i++)
227
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
228 229
    }

230
    if (models == NULL && nmodels != 0) {
231 232
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("nonzero nmodels doesn't match with NULL models"));
233 234 235
        return -1;
    }

236 237 238 239
    if (cpu->type > VIR_CPU_TYPE_GUEST ||
        cpu->mode != VIR_CPU_MODE_CUSTOM) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("invalid CPU definition stub"));
J
Jiri Denemark 已提交
240 241 242
        return -1;
    }

243
    if ((driver = cpuGetSubDriver(cpu->arch)) == NULL)
J
Jiri Denemark 已提交
244 245 246
        return -1;

    if (driver->decode == NULL) {
247 248
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot decode CPU data for %s architecture"),
249
                       virArchToString(cpu->arch));
J
Jiri Denemark 已提交
250 251 252
        return -1;
    }

253
    return driver->decode(cpu, data, models, nmodels, preferred, 0);
J
Jiri Denemark 已提交
254 255 256
}


257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
/**
 * cpuEncode:
 *
 * @arch: CPU architecture
 * @cpu: CPU definition to be encoded into internal CPU driver representation
 * @forced: where to store CPU data corresponding to forced features
 * @required: where to store CPU data corresponding to required features
 * @optional: where to store CPU data corresponding to optional features
 * @disabled: where to store CPU data corresponding to disabled features
 * @forbidden: where to store CPU data corresponding to forbidden features
 * @vendor: where to store CPU data corresponding to CPU vendor
 *
 * Encode CPU definition from @cpu into internal CPU driver representation.
 * Any of @forced, @required, @optional, @disabled, @forbidden and @vendor
 * arguments can be NULL in case the caller is not interested in the
 * corresponding data.
 *
 * Returns 0 on success, -1 on error.
 */
J
Jiri Denemark 已提交
276
int
277
cpuEncode(virArch arch,
278
          const virCPUDef *cpu,
279 280 281 282 283 284
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
285 286 287
{
    struct cpuArchDriver *driver;

288
    VIR_DEBUG("arch=%s, cpu=%p, forced=%p, required=%p, "
J
Jiri Denemark 已提交
289
              "optional=%p, disabled=%p, forbidden=%p, vendor=%p",
290
              virArchToString(arch), cpu, forced, required,
J
Jiri Denemark 已提交
291
              optional, disabled, forbidden, vendor);
292

293 294 295 296 297 298
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return -1;
    }

299
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
300 301 302
        return -1;

    if (driver->encode == NULL) {
303 304
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot encode CPU data for %s architecture"),
305
                       virArchToString(arch));
J
Jiri Denemark 已提交
306 307 308
        return -1;
    }

J
Jiri Denemark 已提交
309
    return driver->encode(arch, cpu, forced, required,
J
Jiri Denemark 已提交
310
                          optional, disabled, forbidden, vendor);
J
Jiri Denemark 已提交
311 312 313
}


J
Jiri Denemark 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/**
 * virCPUDataNew:
 *
 * Returns an allocated memory for virCPUData or NULL on error.
 */
virCPUDataPtr
virCPUDataNew(virArch arch)
{
    virCPUDataPtr data;

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

    data->arch = arch;

    return data;
}


333 334 335 336 337 338 339 340 341
/**
 * cpuDataFree:
 *
 * @data: CPU data structure to be freed
 *
 * Free internal CPU data.
 *
 * Returns nothing.
 */
J
Jiri Denemark 已提交
342
void
J
Jiri Denemark 已提交
343
cpuDataFree(virCPUDataPtr data)
J
Jiri Denemark 已提交
344 345 346
{
    struct cpuArchDriver *driver;

J
Jiri Denemark 已提交
347
    VIR_DEBUG("data=%p", data);
348

J
Jiri Denemark 已提交
349 350 351
    if (data == NULL)
        return;

J
Jiri Denemark 已提交
352
    if ((driver = cpuGetSubDriver(data->arch)) == NULL)
J
Jiri Denemark 已提交
353 354 355
        return;

    if (driver->free == NULL) {
356 357
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot free CPU data for %s architecture"),
J
Jiri Denemark 已提交
358
                       virArchToString(data->arch));
J
Jiri Denemark 已提交
359 360 361
        return;
    }

E
Eric Blake 已提交
362
    (driver->free)(data);
J
Jiri Denemark 已提交
363 364 365
}


366 367 368 369 370 371 372
/**
 * cpuNodeData:
 *
 * @arch: CPU architecture
 *
 * Returns CPU data for host CPU or NULL on error.
 */
373
virCPUDataPtr
374
cpuNodeData(virArch arch)
J
Jiri Denemark 已提交
375 376 377
{
    struct cpuArchDriver *driver;

378
    VIR_DEBUG("arch=%s", virArchToString(arch));
379

380
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
381 382 383
        return NULL;

    if (driver->nodeData == NULL) {
384 385
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot get node CPU data for %s architecture"),
386
                       virArchToString(arch));
J
Jiri Denemark 已提交
387 388 389
        return NULL;
    }

390
    return driver->nodeData(arch);
J
Jiri Denemark 已提交
391 392 393
}


394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/**
 * cpuBaselineXML:
 *
 * @xmlCPUs: list of host CPU XML descriptions
 * @ncpus: number of CPUs in @xmlCPUs
 * @models: list of CPU models that can be considered for the baseline CPU
 * @nmodels: number of CPU models in @models
 * @flags: bitwise-OR of virConnectBaselineCPUFlags
 *
 * Computes the most feature-rich CPU which is compatible with all given
 * host CPUs. If @models array is NULL, all models supported by libvirt will
 * be considered when computing the baseline CPU model, otherwise the baseline
 * CPU model will be one of the provided CPU @models.
 *
 * If @flags includes VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES then libvirt
 * will explicitly list all CPU features that are part of the host CPU,
 * without this flag features that are part of the CPU model will not be
 * listed.
 *
 * Returns XML description of the baseline CPU or NULL on error.
 */
415 416 417 418
char *
cpuBaselineXML(const char **xmlCPUs,
               unsigned int ncpus,
               const char **models,
419 420
               unsigned int nmodels,
               unsigned int flags)
421 422 423 424 425 426
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr;
427
    size_t i;
428

429 430 431
    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
    if (xmlCPUs) {
        for (i = 0; i < ncpus; i++)
432
            VIR_DEBUG("xmlCPUs[%zu]=%s", i, NULLSTR(xmlCPUs[i]));
433 434 435
    }
    if (models) {
        for (i = 0; i < nmodels; i++)
436
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
437 438
    }

439
    if (xmlCPUs == NULL && ncpus != 0) {
440 441
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero ncpus doesn't match with NULL xmlCPUs"));
442 443 444 445
        return NULL;
    }

    if (ncpus < 1) {
446
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given"));
447 448 449 450
        return NULL;
    }

    if (VIR_ALLOC_N(cpus, ncpus))
451
        goto error;
452 453

    for (i = 0; i < ncpus; i++) {
454
        if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), &ctxt)))
455 456
            goto error;

457 458 459 460 461 462 463 464 465 466
        cpus[i] = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST);
        if (cpus[i] == NULL)
            goto error;

        xmlXPathFreeContext(ctxt);
        xmlFreeDoc(doc);
        ctxt = NULL;
        doc = NULL;
    }

467
    if (!(cpu = cpuBaseline(cpus, ncpus, models, nmodels, flags)))
468 469
        goto error;

470
    cpustr = virCPUDefFormat(cpu, NULL, false);
471

472
 cleanup:
473 474 475 476 477 478 479 480 481 482 483
    if (cpus) {
        for (i = 0; i < ncpus; i++)
            virCPUDefFree(cpus[i]);
        VIR_FREE(cpus);
    }
    virCPUDefFree(cpu);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return cpustr;

484
 error:
485 486 487 488 489
    cpustr = NULL;
    goto cleanup;
}


490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/**
 * cpuBaseline:
 *
 * @cpus: list of host CPU definitions
 * @ncpus: number of CPUs in @cpus
 * @models: list of CPU models that can be considered for the baseline CPU
 * @nmodels: number of CPU models in @models
 * @flags: bitwise-OR of virConnectBaselineCPUFlags
 *
 * Computes the most feature-rich CPU which is compatible with all given
 * host CPUs. If @models array is NULL, all models supported by libvirt will
 * be considered when computing the baseline CPU model, otherwise the baseline
 * CPU model will be one of the provided CPU @models.
 *
 * If @flags includes VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES then libvirt
 * will explicitly list all CPU features that are part of the host CPU,
 * without this flag features that are part of the CPU model will not be
 * listed.
 *
 * Returns baseline CPU definition or NULL on error.
 */
511 512 513 514
virCPUDefPtr
cpuBaseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
515 516
            unsigned int nmodels,
            unsigned int flags)
517 518
{
    struct cpuArchDriver *driver;
519
    size_t i;
520 521 522 523

    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
    if (cpus) {
        for (i = 0; i < ncpus; i++)
524
            VIR_DEBUG("cpus[%zu]=%p", i, cpus[i]);
525 526 527
    }
    if (models) {
        for (i = 0; i < nmodels; i++)
528
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
529
    }
530 531

    if (cpus == NULL && ncpus != 0) {
532 533
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero ncpus doesn't match with NULL cpus"));
534 535 536 537
        return NULL;
    }

    if (ncpus < 1) {
538
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given"));
539 540 541
        return NULL;
    }

542 543 544 545 546 547 548 549 550 551 552 553 554
    for (i = 0; i < ncpus; i++) {
        if (!cpus[i]) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("invalid CPU definition at index %zu"), i);
            return NULL;
        }
        if (!cpus[i]->model) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("no CPU model specified at index %zu"), i);
            return NULL;
        }
    }

555
    if (models == NULL && nmodels != 0) {
556 557
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero nmodels doesn't match with NULL models"));
558 559 560 561 562 563 564
        return NULL;
    }

    if ((driver = cpuGetSubDriver(cpus[0]->arch)) == NULL)
        return NULL;

    if (driver->baseline == NULL) {
565 566
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot compute baseline CPU of %s architecture"),
567
                       virArchToString(cpus[0]->arch));
568 569 570
        return NULL;
    }

571
    return driver->baseline(cpus, ncpus, models, nmodels, flags);
572
}
573 574


575
/**
J
Jiri Denemark 已提交
576
 * virCPUUpdate:
577
 *
J
Jiri Denemark 已提交
578 579
 * @arch: CPU architecture
 * @guest: guest CPU definition to be updated
580 581 582
 * @host: host CPU definition
 *
 * Updates @guest CPU definition according to @host CPU. This is required to
J
Jiri Denemark 已提交
583 584 585 586 587
 * support guest CPU definitions specified relatively to host CPU, such as
 * CPUs with VIR_CPU_MODE_CUSTOM and optional features or
 * VIR_CPU_MATCH_MINIMUM, or CPUs with VIR_CPU_MODE_HOST_MODEL.
 * When the guest CPU was not specified relatively, the function does nothing
 * and returns success.
588 589 590
 *
 * Returns 0 on success, -1 on error.
 */
591
int
J
Jiri Denemark 已提交
592 593 594
virCPUUpdate(virArch arch,
             virCPUDefPtr guest,
             const virCPUDef *host)
595 596 597
{
    struct cpuArchDriver *driver;

J
Jiri Denemark 已提交
598 599 600
    VIR_DEBUG("arch=%s, guest=%p mode=%s model=%s, host=%p model=%s",
              virArchToString(arch), guest, virCPUModeTypeToString(guest->mode),
              NULLSTR(guest->model), host, NULLSTR(host ? host->model : NULL));
601

J
Jiri Denemark 已提交
602
    if (!(driver = cpuGetSubDriver(arch)))
603 604
        return -1;

J
Jiri Denemark 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    if (guest->mode == VIR_CPU_MODE_HOST_PASSTHROUGH)
        return 0;

    if (guest->mode == VIR_CPU_MODE_CUSTOM &&
        guest->match != VIR_CPU_MATCH_MINIMUM) {
        size_t i;
        bool optional = false;

        for (i = 0; i < guest->nfeatures; i++) {
            if (guest->features[i].policy == VIR_CPU_FEATURE_OPTIONAL) {
                optional = true;
                break;
            }
        }

        if (!optional)
            return 0;
    }

    /* We get here if guest CPU is either
     *  - host-model
     *  - custom with minimum match
     *  - custom with optional features
     */
    if (!driver->update) {
630
        virReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
631 632
                       _("cannot update guest CPU for %s architecture"),
                       virArchToString(arch));
633 634 635
        return -1;
    }

J
Jiri Denemark 已提交
636 637 638 639 640
    if (driver->update(guest, host) < 0)
        return -1;

    VIR_DEBUG("model=%s", NULLSTR(guest->model));
    return 0;
641
}
D
Daniel P. Berrange 已提交
642

643

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
/**
 * virCPUCheckFeature:
 *
 * @arch: CPU architecture
 * @cpu: CPU definition
 * @feature: feature to be checked for
 *
 * Checks whether @feature is supported by the CPU described by @cpu.
 *
 * Returns 1 if the feature is supported, 0 if it's not supported, or
 * -1 on error.
 */
int
virCPUCheckFeature(virArch arch,
                   const virCPUDef *cpu,
                   const char *feature)
{
    struct cpuArchDriver *driver;

    VIR_DEBUG("arch=%s, cpu=%p, feature=%s",
              virArchToString(arch), cpu, feature);

    if (!(driver = cpuGetSubDriver(arch)))
        return -1;

    if (!driver->checkFeature) {
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot check guest CPU feature for %s architecture"),
                       virArchToString(arch));
        return -1;
    }

    return driver->checkFeature(cpu, feature);
}


680
/**
681
 * virCPUDataCheckFeature:
682
 *
683
 * @data: CPU data
684 685 686 687 688 689 690
 * @feature: feature to be checked for
 *
 * Checks whether @feature is supported by the CPU described by @data.
 *
 * Returns 1 if the feature is supported, 0 if it's not supported, or
 * -1 on error.
 */
D
Daniel P. Berrange 已提交
691
int
692 693
virCPUDataCheckFeature(const virCPUData *data,
                       const char *feature)
D
Daniel P. Berrange 已提交
694 695 696
{
    struct cpuArchDriver *driver;

697 698
    VIR_DEBUG("arch=%s, data=%p, feature=%s",
              virArchToString(data->arch), data, feature);
D
Daniel P. Berrange 已提交
699

700
    if (!(driver = cpuGetSubDriver(data->arch)))
D
Daniel P. Berrange 已提交
701 702
        return -1;

703
    if (!driver->dataCheckFeature) {
704
        virReportError(VIR_ERR_NO_SUPPORT,
705
                       _("cannot check guest CPU feature for %s architecture"),
J
Jiri Denemark 已提交
706
                       virArchToString(data->arch));
D
Daniel P. Berrange 已提交
707 708 709
        return -1;
    }

710
    return driver->dataCheckFeature(data, feature);
D
Daniel P. Berrange 已提交
711
}
712

713 714

/**
J
Jiri Denemark 已提交
715
 * virCPUDataFormat:
716 717 718 719 720 721 722
 *
 * @data: internal CPU representation
 *
 * Formats @data into XML for test purposes.
 *
 * Returns string representation of the XML describing @data or NULL on error.
 */
723
char *
J
Jiri Denemark 已提交
724
virCPUDataFormat(const virCPUData *data)
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
{
    struct cpuArchDriver *driver;

    VIR_DEBUG("data=%p", data);

    if (!(driver = cpuGetSubDriver(data->arch)))
        return NULL;

    if (!driver->dataFormat) {
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot format %s CPU data"),
                       virArchToString(data->arch));
        return NULL;
    }

    return driver->dataFormat(data);
}

743 744

/**
J
Jiri Denemark 已提交
745
 * virCPUDataParse:
746
 *
J
Jiri Denemark 已提交
747
 * @xmlStr: XML string produced by virCPUDataFormat
748 749 750 751 752
 *
 * Parses XML representation of virCPUData structure for test purposes.
 *
 * Returns internal CPU data structure parsed from the XML or NULL on error.
 */
753
virCPUDataPtr
J
Jiri Denemark 已提交
754
virCPUDataParse(const char *xmlStr)
755 756
{
    struct cpuArchDriver *driver;
757 758 759 760
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDataPtr data = NULL;
    char *arch = NULL;
761

762
    VIR_DEBUG("xmlStr=%s", xmlStr);
763

764 765 766 767 768 769 770 771 772 773 774 775 776 777
    if (!(xml = virXMLParseStringCtxt(xmlStr, _("CPU data"), &ctxt))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot parse CPU data"));
        goto cleanup;
    }

    if (!(arch = virXPathString("string(/cpudata/@arch)", ctxt))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("missing CPU data architecture"));
        goto cleanup;
    }

    if (!(driver = cpuGetSubDriverByName(arch)))
        goto cleanup;
778 779 780

    if (!driver->dataParse) {
        virReportError(VIR_ERR_NO_SUPPORT,
781 782
                       _("cannot parse %s CPU data"), arch);
        goto cleanup;
783 784
    }

785 786 787 788 789 790 791
    data = driver->dataParse(ctxt);

 cleanup:
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    VIR_FREE(arch);
    return data;
792 793
}

794 795 796 797 798 799 800 801 802 803 804 805

/** virCPUModelIsAllowed:
 *
 * @model: CPU model to be checked
 * @models: list of supported CPU models
 * @nmodels: number of models in @models
 *
 * Checks whether @model can be found in the list of supported @models.
 * If @models is empty, all models are supported.
 *
 * Returns true if @model is supported, false otherwise.
 */
806
bool
807 808 809
virCPUModelIsAllowed(const char *model,
                     const char **models,
                     unsigned int nmodels)
810
{
811
    size_t i;
812 813 814 815 816 817 818 819 820 821

    if (!models || !nmodels)
        return true;

    for (i = 0; i < nmodels; i++) {
        if (models[i] && STREQ(models[i], model))
            return true;
    }
    return false;
}
822

823

824
/**
J
Jiri Denemark 已提交
825
 * virCPUGetModels:
826
 *
J
Jiri Denemark 已提交
827
 * @arch: CPU architecture
828
 * @models: where to store the NULL-terminated list of supported models
829
 *
830 831 832 833
 * Fetches all CPU models supported by libvirt on @archName. If there are
 * no restrictions on CPU models on @archName (i.e., the CPU model is just
 * passed directly to a hypervisor), this function returns 0 and sets
 * @models to NULL.
834
 *
835 836
 * Returns number of supported CPU models, 0 if any CPU model is supported,
 * or -1 on error.
837
 */
838
int
J
Jiri Denemark 已提交
839
virCPUGetModels(virArch arch, char ***models)
840 841 842
{
    struct cpuArchDriver *driver;

J
Jiri Denemark 已提交
843
    VIR_DEBUG("arch=%s", virArchToString(arch));
844

845
    if (!(driver = cpuGetSubDriver(arch)))
846
        return -1;
847

848
    if (!driver->getModels) {
849 850 851
        if (models)
            *models = NULL;
        return 0;
852
    }
853

854
    return driver->getModels(models);
855
}
J
Jiri Denemark 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876


/**
 * virCPUTranslate:
 *
 * @arch: CPU architecture
 * @cpu: CPU definition to be translated
 * @models: NULL-terminated list of allowed CPU models (NULL if all are allowed)
 * @nmodels: number of CPU models in @models
 *
 * Translates @cpu model (if allowed by @cpu->fallback) to a closest CPU model
 * from @models list.
 *
 * The function does nothing (and returns 0) if @cpu does not have to be
 * translated.
 *
 * Returns -1 on error, 0 on success.
 */
int
virCPUTranslate(virArch arch,
                virCPUDefPtr cpu,
877
                const char **models,
J
Jiri Denemark 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891
                unsigned int nmodels)
{
    struct cpuArchDriver *driver;

    VIR_DEBUG("arch=%s, cpu=%p, model=%s, models=%p, nmodels=%u",
              virArchToString(arch), cpu, NULLSTR(cpu->model), models, nmodels);

    if (!(driver = cpuGetSubDriver(arch)))
        return -1;

    if (cpu->mode == VIR_CPU_MODE_HOST_MODEL ||
        cpu->mode == VIR_CPU_MODE_HOST_PASSTHROUGH)
        return 0;

892
    if (virCPUModelIsAllowed(cpu->model, models, nmodels))
J
Jiri Denemark 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
        return 0;

    if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("CPU model %s is not supported by hypervisor"),
                       cpu->model);
        return -1;
    }

    if (!driver->translate) {
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot translate CPU model %s to a supported model"),
                       cpu->model);
        return -1;
    }

909
    if (driver->translate(cpu, models, nmodels) < 0)
J
Jiri Denemark 已提交
910 911 912 913 914
        return -1;

    VIR_DEBUG("model=%s", NULLSTR(cpu->model));
    return 0;
}
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949


/**
 * virCPUConvertLegacy:
 *
 * @arch: CPU architecture
 * @cpu: CPU definition to be converted
 *
 * Convert legacy CPU definition into one that the corresponding cpu driver
 * will be able to work with. Currently this is only implemented by the PPC
 * driver, which needs to convert legacy POWERx_v* names into POWERx.
 *
 * Returns -1 on error, 0 on success.
 */
int
virCPUConvertLegacy(virArch arch,
                    virCPUDefPtr cpu)
{
    struct cpuArchDriver *driver;

    VIR_DEBUG("arch=%s, cpu=%p, model=%s",
              virArchToString(arch), cpu, NULLSTR(cpu->model));

    if (!(driver = cpuGetSubDriver(arch)))
        return -1;

    if (!driver->convertLegacy)
        return 0;

    if (driver->convertLegacy(cpu) < 0)
        return -1;

    VIR_DEBUG("model=%s", NULLSTR(cpu->model));
    return 0;
}