cpu.c 29.5 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"
M
Martin Kletzander 已提交
29
#include "nodeinfo.h"
J
Jiri Denemark 已提交
30
#include "cpu.h"
31
#include "cpu_map.h"
J
Jiri Denemark 已提交
32
#include "cpu_x86.h"
33
#include "cpu_ppc64.h"
T
Thang Pham 已提交
34
#include "cpu_s390.h"
C
Chuck Short 已提交
35
#include "cpu_arm.h"
36
#include "util/virstring.h"
J
Jiri Denemark 已提交
37 38 39 40


#define VIR_FROM_THIS VIR_FROM_CPU

41 42
VIR_LOG_INIT("cpu.cpu");

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


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

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

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

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


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

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

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


94
/**
J
Jiri Denemark 已提交
95
 * virCPUCompareXML:
96
 *
J
Jiri Denemark 已提交
97
 * @arch: CPU architecture
98 99
 * @host: host CPU definition
 * @xml: XML description of either guest or host CPU to be compared with @host
100
 * @failIncompatible: return an error instead of VIR_CPU_COMPARE_INCOMPATIBLE
101 102 103 104 105 106
 *
 * 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
107 108 109
 * 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.
110
 */
J
Jiri Denemark 已提交
111
virCPUCompareResult
J
Jiri Denemark 已提交
112 113 114 115
virCPUCompareXML(virArch arch,
                 virCPUDefPtr host,
                 const char *xml,
                 bool failIncompatible)
J
Jiri Denemark 已提交
116 117 118 119 120 121
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR;

J
Jiri Denemark 已提交
122 123 124 125 126 127 128
    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;
    }
129

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

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

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

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

    return ret;
}


147
/**
J
Jiri Denemark 已提交
148
 * virCPUCompare:
149
 *
J
Jiri Denemark 已提交
150
 * @arch: CPU architecture
151 152
 * @host: host CPU definition
 * @cpu: either guest or host CPU to be compared with @host
153
 * @failIncompatible: return an error instead of VIR_CPU_COMPARE_INCOMPATIBLE
154 155 156 157 158 159
 *
 * 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
160 161 162
 * 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.
163
 */
J
Jiri Denemark 已提交
164
virCPUCompareResult
J
Jiri Denemark 已提交
165 166 167 168
virCPUCompare(virArch arch,
              virCPUDefPtr host,
              virCPUDefPtr cpu,
              bool failIncompatible)
J
Jiri Denemark 已提交
169 170 171
{
    struct cpuArchDriver *driver;

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

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

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

185
    return driver->compare(host, cpu, failIncompatible);
J
Jiri Denemark 已提交
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 213
/**
 * 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 已提交
214
int
215
cpuDecode(virCPUDefPtr cpu,
216
          const virCPUData *data,
217
          const char **models,
218 219
          unsigned int nmodels,
          const char *preferred)
J
Jiri Denemark 已提交
220 221 222
{
    struct cpuArchDriver *driver;

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

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

237 238 239 240
    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 已提交
241 242 243
        return -1;
    }

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

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

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


258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/**
 * 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 已提交
277
int
278
cpuEncode(virArch arch,
279
          const virCPUDef *cpu,
280 281 282 283 284 285
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
286 287 288
{
    struct cpuArchDriver *driver;

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

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

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

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

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


J
Jiri Denemark 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/**
 * 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;
}


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

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

J
Jiri Denemark 已提交
350
    if (!data)
J
Jiri Denemark 已提交
351 352
        return;

J
Jiri Denemark 已提交
353 354 355 356
    if ((driver = cpuGetSubDriver(data->arch)) && driver->dataFree)
        driver->dataFree(data);
    else
        VIR_FREE(data);
J
Jiri Denemark 已提交
357 358 359
}


360
/**
361
 * virCPUGetHost:
362 363
 *
 * @arch: CPU architecture
364
 * @type: requested type of the CPU
365
 * @nodeInfo: simplified CPU topology (optional)
366 367
 * @models: list of CPU models that can be considered for host CPU
 * @nmodels: number of CPU models in @models
368
 *
369 370 371 372 373 374 375 376 377
 * Create CPU definition describing the host's CPU.
 *
 * The @type (either VIR_CPU_TYPE_HOST or VIR_CPU_TYPE_GUEST) specifies what
 * type of CPU definition should be created. Specifically, VIR_CPU_TYPE_HOST
 * CPUs may contain only features without any policy attribute. Requesting
 * VIR_CPU_TYPE_GUEST provides better results because the CPU is allowed to
 * contain disabled features.
 *
 * If @nodeInfo is not NULL (which is only allowed for VIR_CPU_TYPE_HOST CPUs),
378 379 380 381 382 383
 * the CPU definition will have topology (sockets, cores, threads) filled in
 * according to the content of @nodeInfo. The function fails only if @nodeInfo
 * was not passed in and the assigned CPU driver was not able to detect the
 * host CPU model. In other words, a CPU definition containing just the
 * topology is a successful result even if detecting the host CPU model fails.
 *
384 385 386 387 388
 * It possible to limit the CPU model which may appear in the created CPU
 * definition by passing non-NULL @models list. This is useful when requesting
 * a CPU model usable on a specific hypervisor. If @models is NULL, any CPU
 * model known to libvirt may appear in the result.
 *
389
 * Returns host CPU definition or NULL on error.
390
 */
391 392
virCPUDefPtr
virCPUGetHost(virArch arch,
393
              virCPUType type,
394 395 396
              virNodeInfoPtr nodeInfo,
              const char **models,
              unsigned int nmodels)
J
Jiri Denemark 已提交
397 398
{
    struct cpuArchDriver *driver;
399
    virCPUDefPtr cpu = NULL;
J
Jiri Denemark 已提交
400

401 402 403
    VIR_DEBUG("arch=%s, type=%s, nodeInfo=%p, models=%p, nmodels=%u",
              virArchToString(arch), virCPUTypeToString(type), nodeInfo,
              models, nmodels);
404

405 406 407 408
    if (!(driver = cpuGetSubDriver(arch)))
        return NULL;

    if (VIR_ALLOC(cpu) < 0)
J
Jiri Denemark 已提交
409 410
        return NULL;

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    switch (type) {
    case VIR_CPU_TYPE_HOST:
        cpu->arch = arch;
        cpu->type = type;
        break;

    case VIR_CPU_TYPE_GUEST:
        if (nodeInfo) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("cannot set topology for CPU type '%s'"),
                           virCPUTypeToString(type));
            goto error;
        }
        cpu->type = type;
        break;

    case VIR_CPU_TYPE_AUTO:
    case VIR_CPU_TYPE_LAST:
        virReportError(VIR_ERR_INVALID_ARG,
                       _("unsupported CPU type: %s"),
                       virCPUTypeToString(type));
        goto error;
    }
434 435 436 437 438 439 440 441 442 443 444

    if (nodeInfo) {
        cpu->sockets = nodeInfo->sockets;
        cpu->cores = nodeInfo->cores;
        cpu->threads = nodeInfo->threads;
    }

    /* Try to get the host CPU model, but don't really fail if nodeInfo is
     * filled in.
     */
    if (driver->getHost) {
445 446
        if (driver->getHost(cpu, models, nmodels) < 0 &&
            !nodeInfo)
447 448 449 450 451
            goto error;
    } else if (nodeInfo) {
        VIR_DEBUG("cannot detect host CPU model for %s architecture",
                  virArchToString(arch));
    } else {
452
        virReportError(VIR_ERR_NO_SUPPORT,
453
                       _("cannot detect host CPU model for %s architecture"),
454
                       virArchToString(arch));
455
        goto error;
J
Jiri Denemark 已提交
456 457
    }

458 459 460 461 462
    return cpu;

 error:
    virCPUDefFree(cpu);
    return NULL;
J
Jiri Denemark 已提交
463 464 465
}


M
Martin Kletzander 已提交
466 467 468 469 470 471 472 473 474 475 476 477
virCPUDefPtr
virCPUProbeHost(virArch arch)
{
    virNodeInfo nodeinfo;

    if (nodeGetInfo(&nodeinfo))
        return NULL;

    return virCPUGetHost(arch, VIR_CPU_TYPE_HOST, &nodeinfo, NULL, 0);
}


478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/**
 * 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.
 */
499 500 501 502
char *
cpuBaselineXML(const char **xmlCPUs,
               unsigned int ncpus,
               const char **models,
503 504
               unsigned int nmodels,
               unsigned int flags)
505 506 507 508 509 510
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr;
511
    size_t i;
512

513 514 515
    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
    if (xmlCPUs) {
        for (i = 0; i < ncpus; i++)
516
            VIR_DEBUG("xmlCPUs[%zu]=%s", i, NULLSTR(xmlCPUs[i]));
517 518 519
    }
    if (models) {
        for (i = 0; i < nmodels; i++)
520
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
521 522
    }

523
    if (xmlCPUs == NULL && ncpus != 0) {
524 525
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero ncpus doesn't match with NULL xmlCPUs"));
526 527 528 529
        return NULL;
    }

    if (ncpus < 1) {
530
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given"));
531 532 533 534
        return NULL;
    }

    if (VIR_ALLOC_N(cpus, ncpus))
535
        goto error;
536 537

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

541 542 543 544 545 546 547 548 549 550
        cpus[i] = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST);
        if (cpus[i] == NULL)
            goto error;

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

551
    if (!(cpu = cpuBaseline(cpus, ncpus, models, nmodels, flags)))
552 553
        goto error;

554
    cpustr = virCPUDefFormat(cpu, NULL, false);
555

556
 cleanup:
557 558 559 560 561 562 563 564 565 566 567
    if (cpus) {
        for (i = 0; i < ncpus; i++)
            virCPUDefFree(cpus[i]);
        VIR_FREE(cpus);
    }
    virCPUDefFree(cpu);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return cpustr;

568
 error:
569 570 571 572 573
    cpustr = NULL;
    goto cleanup;
}


574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/**
 * 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.
 */
595 596 597 598
virCPUDefPtr
cpuBaseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
599 600
            unsigned int nmodels,
            unsigned int flags)
601 602
{
    struct cpuArchDriver *driver;
603
    size_t i;
604 605 606 607

    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
    if (cpus) {
        for (i = 0; i < ncpus; i++)
608
            VIR_DEBUG("cpus[%zu]=%p", i, cpus[i]);
609 610 611
    }
    if (models) {
        for (i = 0; i < nmodels; i++)
612
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
613
    }
614 615

    if (cpus == NULL && ncpus != 0) {
616 617
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero ncpus doesn't match with NULL cpus"));
618 619 620 621
        return NULL;
    }

    if (ncpus < 1) {
622
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given"));
623 624 625
        return NULL;
    }

626 627 628 629 630 631 632 633 634 635 636 637 638
    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;
        }
    }

639
    if (models == NULL && nmodels != 0) {
640 641
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero nmodels doesn't match with NULL models"));
642 643 644 645 646 647 648
        return NULL;
    }

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

    if (driver->baseline == NULL) {
649 650
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot compute baseline CPU of %s architecture"),
651
                       virArchToString(cpus[0]->arch));
652 653 654
        return NULL;
    }

655
    return driver->baseline(cpus, ncpus, models, nmodels, flags);
656
}
657 658


659
/**
J
Jiri Denemark 已提交
660
 * virCPUUpdate:
661
 *
J
Jiri Denemark 已提交
662 663
 * @arch: CPU architecture
 * @guest: guest CPU definition to be updated
664 665 666
 * @host: host CPU definition
 *
 * Updates @guest CPU definition according to @host CPU. This is required to
J
Jiri Denemark 已提交
667 668 669 670 671
 * 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.
672 673 674
 *
 * Returns 0 on success, -1 on error.
 */
675
int
J
Jiri Denemark 已提交
676 677 678
virCPUUpdate(virArch arch,
             virCPUDefPtr guest,
             const virCPUDef *host)
679 680 681
{
    struct cpuArchDriver *driver;

J
Jiri Denemark 已提交
682 683 684
    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));
685

J
Jiri Denemark 已提交
686
    if (!(driver = cpuGetSubDriver(arch)))
687 688
        return -1;

J
Jiri Denemark 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    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) {
714
        virReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
715 716
                       _("cannot update guest CPU for %s architecture"),
                       virArchToString(arch));
717 718 719
        return -1;
    }

J
Jiri Denemark 已提交
720 721 722 723 724
    if (driver->update(guest, host) < 0)
        return -1;

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

727

728 729 730 731 732 733 734 735 736 737
/**
 * virCPUUpdateLive:
 *
 * @arch: CPU architecture
 * @cpu: guest CPU definition to be updated
 * @dataEnabled: CPU data of the virtual CPU
 * @dataDisabled: CPU data with features requested by @cpu but disabled by the
 *                hypervisor
 *
 * Update custom mode CPU according to the virtual CPU created by the
738 739
 * hypervisor. The function refuses to update the CPU in case cpu->check is set
 * to VIR_CPU_CHECK_FULL.
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
 *
 * Returns -1 on error,
 *          0 when the CPU was successfully updated,
 *          1 when the operation does not make sense on the CPU or it is not
 *            supported for the given architecture.
 */
int
virCPUUpdateLive(virArch arch,
                 virCPUDefPtr cpu,
                 virCPUDataPtr dataEnabled,
                 virCPUDataPtr dataDisabled)
{
    struct cpuArchDriver *driver;

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

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

    if (!driver->updateLive)
        return 1;

    if (cpu->mode != VIR_CPU_MODE_CUSTOM)
        return 1;

    if (driver->updateLive(cpu, dataEnabled, dataDisabled) < 0)
        return -1;

    return 0;
}


773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
/**
 * 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);
}


809
/**
810
 * virCPUDataCheckFeature:
811
 *
812
 * @data: CPU data
813 814 815 816 817 818 819
 * @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 已提交
820
int
821 822
virCPUDataCheckFeature(const virCPUData *data,
                       const char *feature)
D
Daniel P. Berrange 已提交
823 824 825
{
    struct cpuArchDriver *driver;

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

829
    if (!(driver = cpuGetSubDriver(data->arch)))
D
Daniel P. Berrange 已提交
830 831
        return -1;

832
    if (!driver->dataCheckFeature) {
833
        virReportError(VIR_ERR_NO_SUPPORT,
834
                       _("cannot check guest CPU feature for %s architecture"),
J
Jiri Denemark 已提交
835
                       virArchToString(data->arch));
D
Daniel P. Berrange 已提交
836 837 838
        return -1;
    }

839
    return driver->dataCheckFeature(data, feature);
D
Daniel P. Berrange 已提交
840
}
841

842 843

/**
J
Jiri Denemark 已提交
844
 * virCPUDataFormat:
845 846 847 848 849 850 851
 *
 * @data: internal CPU representation
 *
 * Formats @data into XML for test purposes.
 *
 * Returns string representation of the XML describing @data or NULL on error.
 */
852
char *
J
Jiri Denemark 已提交
853
virCPUDataFormat(const virCPUData *data)
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
{
    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);
}

872 873

/**
J
Jiri Denemark 已提交
874
 * virCPUDataParse:
875
 *
J
Jiri Denemark 已提交
876
 * @xmlStr: XML string produced by virCPUDataFormat
877 878 879 880 881
 *
 * Parses XML representation of virCPUData structure for test purposes.
 *
 * Returns internal CPU data structure parsed from the XML or NULL on error.
 */
882
virCPUDataPtr
J
Jiri Denemark 已提交
883
virCPUDataParse(const char *xmlStr)
884 885
{
    struct cpuArchDriver *driver;
886 887 888 889
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDataPtr data = NULL;
    char *arch = NULL;
890

891
    VIR_DEBUG("xmlStr=%s", xmlStr);
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906
    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;
907 908 909

    if (!driver->dataParse) {
        virReportError(VIR_ERR_NO_SUPPORT,
910 911
                       _("cannot parse %s CPU data"), arch);
        goto cleanup;
912 913
    }

914 915 916 917 918 919 920
    data = driver->dataParse(ctxt);

 cleanup:
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    VIR_FREE(arch);
    return data;
921 922
}

923 924 925 926 927 928 929 930 931 932 933 934

/** 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.
 */
935
bool
936 937 938
virCPUModelIsAllowed(const char *model,
                     const char **models,
                     unsigned int nmodels)
939
{
940
    size_t i;
941 942 943 944 945 946 947 948 949 950

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

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

952

953
/**
J
Jiri Denemark 已提交
954
 * virCPUGetModels:
955
 *
J
Jiri Denemark 已提交
956
 * @arch: CPU architecture
957
 * @models: where to store the NULL-terminated list of supported models
958
 *
959 960 961 962
 * 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.
963
 *
964 965
 * Returns number of supported CPU models, 0 if any CPU model is supported,
 * or -1 on error.
966
 */
967
int
J
Jiri Denemark 已提交
968
virCPUGetModels(virArch arch, char ***models)
969 970 971
{
    struct cpuArchDriver *driver;

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

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

977
    if (!driver->getModels) {
978 979 980
        if (models)
            *models = NULL;
        return 0;
981
    }
982

983
    return driver->getModels(models);
984
}
J
Jiri Denemark 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005


/**
 * 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,
1006
                const char **models,
J
Jiri Denemark 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
                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;

1021
    if (virCPUModelIsAllowed(cpu->model, models, nmodels))
J
Jiri Denemark 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
        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;
    }

1038
    if (driver->translate(cpu, models, nmodels) < 0)
J
Jiri Denemark 已提交
1039 1040 1041 1042 1043
        return -1;

    VIR_DEBUG("model=%s", NULLSTR(cpu->model));
    return 0;
}
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078


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