cpu.c 20.8 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"
P
Prerna Saxena 已提交
32
#include "cpu_powerpc.h"
T
Thang Pham 已提交
33
#include "cpu_s390.h"
C
Chuck Short 已提交
34
#include "cpu_arm.h"
35
#include "cpu_aarch64.h"
J
Jiri Denemark 已提交
36
#include "cpu_generic.h"
37
#include "util/virstring.h"
J
Jiri Denemark 已提交
38 39 40 41 42


#define NR_DRIVERS ARRAY_CARDINALITY(drivers)
#define VIR_FROM_THIS VIR_FROM_CPU

43 44
VIR_LOG_INIT("cpu.cpu");

J
Jiri Denemark 已提交
45 46
static struct cpuArchDriver *drivers[] = {
    &cpuDriverX86,
P
Prerna Saxena 已提交
47
    &cpuDriverPowerPC,
T
Thang Pham 已提交
48
    &cpuDriverS390,
C
Chuck Short 已提交
49
    &cpuDriverArm,
50
    &cpuDriverAARCH64,
J
Jiri Denemark 已提交
51 52 53 54 55 56
    /* generic driver must always be the last one */
    &cpuDriverGeneric
};


static struct cpuArchDriver *
57
cpuGetSubDriver(virArch arch)
J
Jiri Denemark 已提交
58
{
59 60
    size_t i;
    size_t j;
J
Jiri Denemark 已提交
61

62
    if (arch == VIR_ARCH_NONE) {
63 64
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("undefined hardware architecture"));
J
Jiri Denemark 已提交
65 66 67 68 69
        return NULL;
    }

    for (i = 0; i < NR_DRIVERS - 1; i++) {
        for (j = 0; j < drivers[i]->narch; j++) {
70
            if (arch == drivers[i]->arch[j])
J
Jiri Denemark 已提交
71 72 73 74 75 76 77 78 79
                return drivers[i];
        }
    }

    /* use generic driver by default */
    return drivers[NR_DRIVERS - 1];
}


80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * cpuCompareXML:
 *
 * @host: host CPU definition
 * @xml: XML description of either guest or host CPU to be compared with @host
 *
 * 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
 * the @host CPU.
 */
J
Jiri Denemark 已提交
93
virCPUCompareResult
94
cpuCompareXML(virCPUDefPtr host,
95 96
              const char *xml,
              bool failIncompatible)
J
Jiri Denemark 已提交
97 98 99 100 101 102
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR;

103 104
    VIR_DEBUG("host=%p, xml=%s", host, NULLSTR(xml));

105
    if (!(doc = virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt)))
106
        goto cleanup;
J
Jiri Denemark 已提交
107

108
    cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO);
J
Jiri Denemark 已提交
109 110 111
    if (cpu == NULL)
        goto cleanup;

112
    ret = cpuCompare(host, cpu, failIncompatible);
J
Jiri Denemark 已提交
113

114
 cleanup:
J
Jiri Denemark 已提交
115 116 117 118 119 120 121 122
    virCPUDefFree(cpu);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return ret;
}


123 124 125 126 127 128 129 130 131 132 133 134 135
/**
 * cpuCompare:
 *
 * @host: host CPU definition
 * @cpu: either guest or host CPU to be compared with @host
 *
 * 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
 * the @host CPU.
 */
J
Jiri Denemark 已提交
136
virCPUCompareResult
137
cpuCompare(virCPUDefPtr host,
138 139
           virCPUDefPtr cpu,
           bool failIncompatible)
J
Jiri Denemark 已提交
140 141 142
{
    struct cpuArchDriver *driver;

143 144
    VIR_DEBUG("host=%p, cpu=%p", host, cpu);

145 146 147 148 149 150
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

151
    if ((driver = cpuGetSubDriver(host->arch)) == NULL)
J
Jiri Denemark 已提交
152 153 154
        return VIR_CPU_COMPARE_ERROR;

    if (driver->compare == NULL) {
155 156
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot compare CPUs of %s architecture"),
157
                       virArchToString(host->arch));
J
Jiri Denemark 已提交
158 159 160
        return VIR_CPU_COMPARE_ERROR;
    }

161
    return driver->compare(host, cpu, failIncompatible);
J
Jiri Denemark 已提交
162 163 164
}


165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/**
 * 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 已提交
190
int
191
cpuDecode(virCPUDefPtr cpu,
192
          const virCPUData *data,
193
          const char **models,
194 195
          unsigned int nmodels,
          const char *preferred)
J
Jiri Denemark 已提交
196 197 198
{
    struct cpuArchDriver *driver;

199 200
    VIR_DEBUG("cpu=%p, data=%p, nmodels=%u, preferred=%s",
              cpu, data, nmodels, NULLSTR(preferred));
201
    if (models) {
202
        size_t i;
203
        for (i = 0; i < nmodels; i++)
204
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
205 206
    }

207
    if (models == NULL && nmodels != 0) {
208 209
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("nonzero nmodels doesn't match with NULL models"));
210 211 212
        return -1;
    }

213 214 215 216
    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 已提交
217 218 219
        return -1;
    }

220
    if ((driver = cpuGetSubDriver(cpu->arch)) == NULL)
J
Jiri Denemark 已提交
221 222 223
        return -1;

    if (driver->decode == NULL) {
224 225
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot decode CPU data for %s architecture"),
226
                       virArchToString(cpu->arch));
J
Jiri Denemark 已提交
227 228 229
        return -1;
    }

230
    return driver->decode(cpu, data, models, nmodels, preferred, 0);
J
Jiri Denemark 已提交
231 232 233
}


234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/**
 * 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 已提交
253
int
254
cpuEncode(virArch arch,
255
          const virCPUDef *cpu,
256 257 258 259 260 261
          virCPUDataPtr *forced,
          virCPUDataPtr *required,
          virCPUDataPtr *optional,
          virCPUDataPtr *disabled,
          virCPUDataPtr *forbidden,
          virCPUDataPtr *vendor)
J
Jiri Denemark 已提交
262 263 264
{
    struct cpuArchDriver *driver;

265
    VIR_DEBUG("arch=%s, cpu=%p, forced=%p, required=%p, "
J
Jiri Denemark 已提交
266
              "optional=%p, disabled=%p, forbidden=%p, vendor=%p",
267
              virArchToString(arch), cpu, forced, required,
J
Jiri Denemark 已提交
268
              optional, disabled, forbidden, vendor);
269

270 271 272 273 274 275
    if (!cpu->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return -1;
    }

276
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
277 278 279
        return -1;

    if (driver->encode == NULL) {
280 281
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot encode CPU data for %s architecture"),
282
                       virArchToString(arch));
J
Jiri Denemark 已提交
283 284 285
        return -1;
    }

J
Jiri Denemark 已提交
286
    return driver->encode(arch, cpu, forced, required,
J
Jiri Denemark 已提交
287
                          optional, disabled, forbidden, vendor);
J
Jiri Denemark 已提交
288 289 290
}


291 292 293 294 295 296 297 298 299
/**
 * cpuDataFree:
 *
 * @data: CPU data structure to be freed
 *
 * Free internal CPU data.
 *
 * Returns nothing.
 */
J
Jiri Denemark 已提交
300
void
J
Jiri Denemark 已提交
301
cpuDataFree(virCPUDataPtr data)
J
Jiri Denemark 已提交
302 303 304
{
    struct cpuArchDriver *driver;

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

J
Jiri Denemark 已提交
307 308 309
    if (data == NULL)
        return;

J
Jiri Denemark 已提交
310
    if ((driver = cpuGetSubDriver(data->arch)) == NULL)
J
Jiri Denemark 已提交
311 312 313
        return;

    if (driver->free == NULL) {
314 315
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot free CPU data for %s architecture"),
J
Jiri Denemark 已提交
316
                       virArchToString(data->arch));
J
Jiri Denemark 已提交
317 318 319
        return;
    }

E
Eric Blake 已提交
320
    (driver->free)(data);
J
Jiri Denemark 已提交
321 322 323
}


324 325 326 327 328 329 330
/**
 * cpuNodeData:
 *
 * @arch: CPU architecture
 *
 * Returns CPU data for host CPU or NULL on error.
 */
331
virCPUDataPtr
332
cpuNodeData(virArch arch)
J
Jiri Denemark 已提交
333 334 335
{
    struct cpuArchDriver *driver;

336
    VIR_DEBUG("arch=%s", virArchToString(arch));
337

338
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
339 340 341
        return NULL;

    if (driver->nodeData == NULL) {
342 343
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot get node CPU data for %s architecture"),
344
                       virArchToString(arch));
J
Jiri Denemark 已提交
345 346 347
        return NULL;
    }

348
    return driver->nodeData(arch);
J
Jiri Denemark 已提交
349 350 351
}


352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/**
 * cpuGuestData:
 *
 * @host: host CPU definition
 * @guest: guest CPU definition
 * @data: computed guest CPU data
 * @msg: error message describing why the @guest and @host CPUs are considered
 *       incompatible
 *
 * Computes guest CPU data for the @guest CPU definition when run on the @host
 * CPU.
 *
 * Returns VIR_CPU_COMPARE_ERROR on error, VIR_CPU_COMPARE_INCOMPATIBLE when
 * the two CPUs are incompatible (@msg will describe the incompatibility),
 * VIR_CPU_COMPARE_IDENTICAL when the two CPUs are identical,
 * VIR_CPU_COMPARE_SUPERSET when the @guest CPU is a superset of the @host CPU.
 */
J
Jiri Denemark 已提交
369
virCPUCompareResult
370
cpuGuestData(virCPUDefPtr host,
J
Jiri Denemark 已提交
371
             virCPUDefPtr guest,
372
             virCPUDataPtr *data,
373
             char **msg)
J
Jiri Denemark 已提交
374 375 376
{
    struct cpuArchDriver *driver;

377
    VIR_DEBUG("host=%p, guest=%p, data=%p, msg=%p", host, guest, data, msg);
378

379 380 381 382 383 384
    if (!guest->model) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("no guest CPU model specified"));
        return VIR_CPU_COMPARE_ERROR;
    }

385
    if ((driver = cpuGetSubDriver(host->arch)) == NULL)
J
Jiri Denemark 已提交
386 387 388
        return VIR_CPU_COMPARE_ERROR;

    if (driver->guestData == NULL) {
389 390
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot compute guest CPU data for %s architecture"),
391
                       virArchToString(host->arch));
J
Jiri Denemark 已提交
392 393 394
        return VIR_CPU_COMPARE_ERROR;
    }

395
    return driver->guestData(host, guest, data, msg);
J
Jiri Denemark 已提交
396
}
397 398


399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/**
 * 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.
 */
420 421 422 423
char *
cpuBaselineXML(const char **xmlCPUs,
               unsigned int ncpus,
               const char **models,
424 425
               unsigned int nmodels,
               unsigned int flags)
426 427 428 429 430 431
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr;
432
    size_t i;
433

434 435 436
    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
    if (xmlCPUs) {
        for (i = 0; i < ncpus; i++)
437
            VIR_DEBUG("xmlCPUs[%zu]=%s", i, NULLSTR(xmlCPUs[i]));
438 439 440
    }
    if (models) {
        for (i = 0; i < nmodels; i++)
441
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
442 443
    }

444
    if (xmlCPUs == NULL && ncpus != 0) {
445 446
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero ncpus doesn't match with NULL xmlCPUs"));
447 448 449 450
        return NULL;
    }

    if (ncpus < 1) {
451
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given"));
452 453 454 455
        return NULL;
    }

    if (VIR_ALLOC_N(cpus, ncpus))
456
        goto error;
457 458

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

462 463 464 465 466 467 468 469 470 471
        cpus[i] = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST);
        if (cpus[i] == NULL)
            goto error;

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

472
    if (!(cpu = cpuBaseline(cpus, ncpus, models, nmodels, flags)))
473 474
        goto error;

475
    cpustr = virCPUDefFormat(cpu, false);
476

477
 cleanup:
478 479 480 481 482 483 484 485 486 487 488
    if (cpus) {
        for (i = 0; i < ncpus; i++)
            virCPUDefFree(cpus[i]);
        VIR_FREE(cpus);
    }
    virCPUDefFree(cpu);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return cpustr;

489
 error:
490 491 492 493 494
    cpustr = NULL;
    goto cleanup;
}


495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/**
 * 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.
 */
516 517 518 519
virCPUDefPtr
cpuBaseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
520 521
            unsigned int nmodels,
            unsigned int flags)
522 523
{
    struct cpuArchDriver *driver;
524
    size_t i;
525 526 527 528

    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
    if (cpus) {
        for (i = 0; i < ncpus; i++)
529
            VIR_DEBUG("cpus[%zu]=%p", i, cpus[i]);
530 531 532
    }
    if (models) {
        for (i = 0; i < nmodels; i++)
533
            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
534
    }
535 536

    if (cpus == NULL && ncpus != 0) {
537 538
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero ncpus doesn't match with NULL cpus"));
539 540 541 542
        return NULL;
    }

    if (ncpus < 1) {
543
        virReportError(VIR_ERR_INVALID_ARG, "%s", _("No CPUs given"));
544 545 546
        return NULL;
    }

547 548 549 550 551 552 553 554 555 556 557 558 559
    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;
        }
    }

560
    if (models == NULL && nmodels != 0) {
561 562
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("nonzero nmodels doesn't match with NULL models"));
563 564 565 566 567 568 569
        return NULL;
    }

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

    if (driver->baseline == NULL) {
570 571
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot compute baseline CPU of %s architecture"),
572
                       virArchToString(cpus[0]->arch));
573 574 575
        return NULL;
    }

576
    return driver->baseline(cpus, ncpus, models, nmodels, flags);
577
}
578 579


580 581 582 583 584 585 586 587 588 589 590 591 592 593
/**
 * cpuUpdate:
 *
 * @guest: guest CPU definition
 * @host: host CPU definition
 *
 * Updates @guest CPU definition according to @host CPU. This is required to
 * support guest CPU definition which are relative to host CPU, such as CPUs
 * with VIR_CPU_MODE_CUSTOM and optional features or VIR_CPU_MATCH_MINIMUM, or
 * CPUs with non-custom mode (VIR_CPU_MODE_HOST_MODEL,
 * VIR_CPU_MODE_HOST_PASSTHROUGH).
 *
 * Returns 0 on success, -1 on error.
 */
594 595
int
cpuUpdate(virCPUDefPtr guest,
596
          const virCPUDef *host)
597 598 599 600 601 602 603 604 605
{
    struct cpuArchDriver *driver;

    VIR_DEBUG("guest=%p, host=%p", guest, host);

    if ((driver = cpuGetSubDriver(host->arch)) == NULL)
        return -1;

    if (driver->update == NULL) {
606 607
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot update guest CPU data for %s architecture"),
608
                       virArchToString(host->arch));
609 610 611 612 613
        return -1;
    }

    return driver->update(guest, host);
}
D
Daniel P. Berrange 已提交
614

615 616 617 618 619 620 621 622 623 624 625 626

/**
 * cpuHasFeature:
 *
 * @data: internal CPU representation
 * @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 已提交
627
int
628
cpuHasFeature(const virCPUData *data,
D
Daniel P. Berrange 已提交
629 630 631 632
              const char *feature)
{
    struct cpuArchDriver *driver;

J
Jiri Denemark 已提交
633
    VIR_DEBUG("data=%p, feature=%s", data, feature);
D
Daniel P. Berrange 已提交
634

J
Jiri Denemark 已提交
635
    if ((driver = cpuGetSubDriver(data->arch)) == NULL)
D
Daniel P. Berrange 已提交
636 637 638
        return -1;

    if (driver->hasFeature == NULL) {
639 640
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("cannot check guest CPU data for %s architecture"),
J
Jiri Denemark 已提交
641
                       virArchToString(data->arch));
D
Daniel P. Berrange 已提交
642 643 644 645 646
        return -1;
    }

    return driver->hasFeature(data, feature);
}
647

648 649 650 651 652 653 654 655 656 657

/**
 * cpuDataFormat:
 *
 * @data: internal CPU representation
 *
 * Formats @data into XML for test purposes.
 *
 * Returns string representation of the XML describing @data or NULL on error.
 */
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
char *
cpuDataFormat(const virCPUData *data)
{
    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);
}

678 679 680 681 682 683 684 685 686 687 688

/**
 * cpuDataParse:
 *
 * @arch: CPU architecture
 * @xmlStr: XML string produced by cpuDataFormat
 *
 * Parses XML representation of virCPUData structure for test purposes.
 *
 * Returns internal CPU data structure parsed from the XML or NULL on error.
 */
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
virCPUDataPtr
cpuDataParse(virArch arch,
             const char *xmlStr)
{
    struct cpuArchDriver *driver;

    VIR_DEBUG("arch=%s, xmlStr=%s", virArchToString(arch), xmlStr);

    if (!(driver = cpuGetSubDriver(arch)))
        return NULL;

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

    return driver->dataParse(xmlStr);
}

710 711 712 713 714
bool
cpuModelIsAllowed(const char *model,
                  const char **models,
                  unsigned int nmodels)
{
715
    size_t i;
716 717 718 719 720 721 722 723 724 725

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

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

727 728 729 730 731 732 733 734 735 736
/**
 * cpuGetModels:
 *
 * @archName: CPU architecture string
 * @models: where to store the list of supported models
 *
 * Fetches all CPU models supported by libvirt on @archName.
 *
 * Returns number of supported CPU models or -1 on error.
 */
737 738 739 740
int
cpuGetModels(const char *archName, char ***models)
{
    struct cpuArchDriver *driver;
741 742 743
    virArch arch;

    VIR_DEBUG("arch=%s", archName);
744 745 746 747 748 749

    arch = virArchFromString(archName);
    if (arch == VIR_ARCH_NONE) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
750
        return -1;
751 752 753 754 755 756 757
    }

    driver = cpuGetSubDriver(arch);
    if (driver == NULL) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find a driver for the architecture %s"),
                       archName);
758
        return -1;
759 760
    }

761 762 763 764 765 766
    if (!driver->getModels) {
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("CPU driver for %s has no CPU model support"),
                       virArchToString(arch));
        return -1;
    }
767

768
    return driver->getModels(models);
769
}