cpu.c 5.7 KB
Newer Older
J
Jiri Denemark 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * cpu.c: internal functions for CPU manipulation
 *
 * Copyright (C) 2009 Red Hat, Inc.
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Authors:
 *      Jiri Denemark <jdenemar@redhat.com>
 */

#include <config.h>

#include "xml.h"
#include "cpu.h"
#include "cpu_x86.h"
#include "cpu_generic.h"


#define NR_DRIVERS ARRAY_CARDINALITY(drivers)
#define VIR_FROM_THIS VIR_FROM_CPU

static struct cpuArchDriver *drivers[] = {
    &cpuDriverX86,
    /* generic driver must always be the last one */
    &cpuDriverGeneric
};


static struct cpuArchDriver *
43
cpuGetSubDriver(const char *arch)
J
Jiri Denemark 已提交
44 45 46 47 48
{
    unsigned int i;
    unsigned int j;

    if (arch == NULL) {
49
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
50
                          "%s", _("undefined hardware architecture"));
J
Jiri Denemark 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        return NULL;
    }

    for (i = 0; i < NR_DRIVERS - 1; i++) {
        for (j = 0; j < drivers[i]->narch; j++) {
            if (STREQ(arch, drivers[i]->arch[j]))
                return drivers[i];
        }
    }

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


virCPUCompareResult
67
cpuCompareXML(virCPUDefPtr host,
J
Jiri Denemark 已提交
68 69 70 71 72 73 74 75 76 77
              const char *xml)
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    virCPUDefPtr cpu = NULL;
    virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR;

    doc = xmlParseMemory(xml, strlen(xml));

    if (doc == NULL || (ctxt = xmlXPathNewContext(doc)) == NULL) {
78
        virReportOOMError();
J
Jiri Denemark 已提交
79 80 81 82 83
        goto cleanup;
    }

    ctxt->node = xmlDocGetRootElement(doc);

84
    cpu = virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO);
J
Jiri Denemark 已提交
85 86 87
    if (cpu == NULL)
        goto cleanup;

88
    ret = cpuCompare(host, cpu);
J
Jiri Denemark 已提交
89 90 91 92 93 94 95 96 97 98 99

cleanup:
    virCPUDefFree(cpu);
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);

    return ret;
}


virCPUCompareResult
100
cpuCompare(virCPUDefPtr host,
J
Jiri Denemark 已提交
101 102 103 104
           virCPUDefPtr cpu)
{
    struct cpuArchDriver *driver;

105
    if ((driver = cpuGetSubDriver(host->arch)) == NULL)
J
Jiri Denemark 已提交
106 107 108
        return VIR_CPU_COMPARE_ERROR;

    if (driver->compare == NULL) {
109
        virCPUReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
110 111 112 113 114 115 116 117 118 119
                _("cannot compare CPUs of %s architecture"),
                host->arch);
        return VIR_CPU_COMPARE_ERROR;
    }

    return driver->compare(host, cpu);
}


int
120
cpuDecode(virCPUDefPtr cpu,
J
Jiri Denemark 已提交
121 122 123 124 125 126
          const union cpuData *data,
          unsigned int nmodels,
          const char **models)
{
    struct cpuArchDriver *driver;

127
    if (models == NULL && nmodels != 0) {
128
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
129 130 131 132
                "%s", _("nonzero nmodels doesn't match with NULL models"));
        return -1;
    }

J
Jiri Denemark 已提交
133
    if (cpu == NULL) {
134
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
135
                          "%s", _("invalid CPU definition"));
J
Jiri Denemark 已提交
136 137 138
        return -1;
    }

139
    if ((driver = cpuGetSubDriver(cpu->arch)) == NULL)
J
Jiri Denemark 已提交
140 141 142
        return -1;

    if (driver->decode == NULL) {
143
        virCPUReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
144 145 146 147 148 149 150 151 152 153
                _("cannot decode CPU data for %s architecture"),
                cpu->arch);
        return -1;
    }

    return driver->decode(cpu, data, nmodels, models);
}


int
154
cpuEncode(const char *arch,
J
Jiri Denemark 已提交
155 156 157 158 159 160 161 162 163
          const virCPUDefPtr cpu,
          union cpuData **forced,
          union cpuData **required,
          union cpuData **optional,
          union cpuData **disabled,
          union cpuData **forbidden)
{
    struct cpuArchDriver *driver;

164
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
165 166 167
        return -1;

    if (driver->encode == NULL) {
168
        virCPUReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
169 170 171 172 173 174 175 176 177 178 179
                _("cannot encode CPU data for %s architecture"),
                arch);
        return -1;
    }

    return driver->encode(cpu, forced, required,
                          optional, disabled, forbidden);
}


void
180
cpuDataFree(const char *arch,
J
Jiri Denemark 已提交
181 182 183 184 185 186 187
            union cpuData *data)
{
    struct cpuArchDriver *driver;

    if (data == NULL)
        return;

188
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
189 190 191
        return;

    if (driver->free == NULL) {
192
        virCPUReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
193 194 195 196 197 198 199 200 201 202
                _("cannot free CPU data for %s architecture"),
                arch);
        return;
    }

    driver->free(data);
}


union cpuData *
203
cpuNodeData(const char *arch)
J
Jiri Denemark 已提交
204 205 206
{
    struct cpuArchDriver *driver;

207
    if ((driver = cpuGetSubDriver(arch)) == NULL)
J
Jiri Denemark 已提交
208 209 210
        return NULL;

    if (driver->nodeData == NULL) {
211
        virCPUReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
212 213 214 215 216 217 218 219 220 221
                _("cannot get node CPU data for %s architecture"),
                arch);
        return NULL;
    }

    return driver->nodeData();
}


virCPUCompareResult
222
cpuGuestData(virCPUDefPtr host,
J
Jiri Denemark 已提交
223 224 225 226 227
             virCPUDefPtr guest,
             union cpuData **data)
{
    struct cpuArchDriver *driver;

228
    if ((driver = cpuGetSubDriver(host->arch)) == NULL)
J
Jiri Denemark 已提交
229 230 231
        return VIR_CPU_COMPARE_ERROR;

    if (driver->guestData == NULL) {
232
        virCPUReportError(VIR_ERR_NO_SUPPORT,
J
Jiri Denemark 已提交
233 234 235 236 237 238 239
                _("cannot compute guest CPU data for %s architecture"),
                host->arch);
        return VIR_CPU_COMPARE_ERROR;
    }

    return driver->guestData(host, guest, data);
}