machine.c 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * QEMU Machine
 *
 * Copyright (C) 2014 Red Hat Inc
 *
 * Authors:
 *   Marcel Apfelbaum <marcel.a@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "hw/boards.h"
14
#include "qapi/visitor.h"
15 16 17
#include "hw/sysbus.h"
#include "sysemu/sysemu.h"
#include "qemu/error-report.h"
18 19 20 21 22 23 24 25 26 27 28 29

static char *machine_get_accel(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->accel);
}

static void machine_set_accel(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

30
    g_free(ms->accel);
31 32 33 34 35 36 37
    ms->accel = g_strdup(value);
}

static void machine_set_kernel_irqchip(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

38 39
    ms->kernel_irqchip_allowed = value;
    ms->kernel_irqchip_required = value;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}

static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
                                       void *opaque, const char *name,
                                       Error **errp)
{
    MachineState *ms = MACHINE(obj);
    int64_t value = ms->kvm_shadow_mem;

    visit_type_int(v, &value, name, errp);
}

static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
                                       void *opaque, const char *name,
                                       Error **errp)
{
    MachineState *ms = MACHINE(obj);
    Error *error = NULL;
    int64_t value;

    visit_type_int(v, &value, name, &error);
    if (error) {
        error_propagate(errp, error);
        return;
    }

    ms->kvm_shadow_mem = value;
}

static char *machine_get_kernel(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->kernel_filename);
}

static void machine_set_kernel(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

80
    g_free(ms->kernel_filename);
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    ms->kernel_filename = g_strdup(value);
}

static char *machine_get_initrd(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->initrd_filename);
}

static void machine_set_initrd(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

95
    g_free(ms->initrd_filename);
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    ms->initrd_filename = g_strdup(value);
}

static char *machine_get_append(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->kernel_cmdline);
}

static void machine_set_append(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

110
    g_free(ms->kernel_cmdline);
111 112 113 114 115 116 117 118 119 120 121 122 123 124
    ms->kernel_cmdline = g_strdup(value);
}

static char *machine_get_dtb(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->dtb);
}

static void machine_set_dtb(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

125
    g_free(ms->dtb);
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    ms->dtb = g_strdup(value);
}

static char *machine_get_dumpdtb(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->dumpdtb);
}

static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

140
    g_free(ms->dumpdtb);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    ms->dumpdtb = g_strdup(value);
}

static void machine_get_phandle_start(Object *obj, Visitor *v,
                                       void *opaque, const char *name,
                                       Error **errp)
{
    MachineState *ms = MACHINE(obj);
    int64_t value = ms->phandle_start;

    visit_type_int(v, &value, name, errp);
}

static void machine_set_phandle_start(Object *obj, Visitor *v,
                                       void *opaque, const char *name,
                                       Error **errp)
{
    MachineState *ms = MACHINE(obj);
    Error *error = NULL;
    int64_t value;

    visit_type_int(v, &value, name, &error);
    if (error) {
        error_propagate(errp, error);
        return;
    }

    ms->phandle_start = value;
}

static char *machine_get_dt_compatible(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->dt_compatible);
}

static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

182
    g_free(ms->dt_compatible);
183 184 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 213 214 215 216 217 218 219 220 221 222 223 224 225
    ms->dt_compatible = g_strdup(value);
}

static bool machine_get_dump_guest_core(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return ms->dump_guest_core;
}

static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    ms->dump_guest_core = value;
}

static bool machine_get_mem_merge(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return ms->mem_merge;
}

static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    ms->mem_merge = value;
}

static bool machine_get_usb(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return ms->usb;
}

static void machine_set_usb(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    ms->usb = value;
226
    ms->usb_disabled = !value;
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242
static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return ms->igd_gfx_passthru;
}

static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    ms->igd_gfx_passthru = value;
}

243 244 245 246 247 248 249 250 251 252 253
static char *machine_get_firmware(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return g_strdup(ms->firmware);
}

static void machine_set_firmware(Object *obj, const char *value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

254
    g_free(ms->firmware);
255 256 257
    ms->firmware = g_strdup(value);
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271
static bool machine_get_iommu(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return ms->iommu;
}

static void machine_set_iommu(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    ms->iommu = value;
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285
static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    ms->suppress_vmdesc = value;
}

static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
{
    MachineState *ms = MACHINE(obj);

    return ms->suppress_vmdesc;
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
static int error_on_sysbus_device(SysBusDevice *sbdev, void *opaque)
{
    error_report("Option '-device %s' cannot be handled by this machine",
                 object_class_get_name(object_get_class(OBJECT(sbdev))));
    exit(1);
}

static void machine_init_notify(Notifier *notifier, void *data)
{
    Object *machine = qdev_get_machine();
    ObjectClass *oc = object_get_class(machine);
    MachineClass *mc = MACHINE_CLASS(oc);

    if (mc->has_dynamic_sysbus) {
        /* Our machine can handle dynamic sysbus devices, we're all good */
        return;
    }

    /*
     * Loop through all dynamically created devices and check whether there
     * are sysbus devices among them. If there are, error out.
     */
    foreach_dynamic_sysbus_device(error_on_sysbus_device, NULL);
}

311 312 313 314 315 316 317 318
static void machine_class_init(ObjectClass *oc, void *data)
{
    MachineClass *mc = MACHINE_CLASS(oc);

    /* Default 128 MB as guest ram size */
    mc->default_ram_size = 128 * M_BYTE;
}

319 320 321 322 323 324 325 326
static void machine_class_base_init(ObjectClass *oc, void *data)
{
    if (!object_class_is_abstract(oc)) {
        const char *cname = object_class_get_name(oc);
        assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
    }
}

327 328
static void machine_initfn(Object *obj)
{
329 330
    MachineState *ms = MACHINE(obj);

331
    ms->kernel_irqchip_allowed = true;
332
    ms->kvm_shadow_mem = -1;
333
    ms->dump_guest_core = true;
334
    ms->mem_merge = true;
335

336 337
    object_property_add_str(obj, "accel",
                            machine_get_accel, machine_set_accel, NULL);
338 339 340
    object_property_set_description(obj, "accel",
                                    "Accelerator list",
                                    NULL);
341
    object_property_add_bool(obj, "kernel-irqchip",
342
                             NULL,
343 344
                             machine_set_kernel_irqchip,
                             NULL);
345 346 347
    object_property_set_description(obj, "kernel-irqchip",
                                    "Use KVM in-kernel irqchip",
                                    NULL);
348
    object_property_add(obj, "kvm-shadow-mem", "int",
349 350 351
                        machine_get_kvm_shadow_mem,
                        machine_set_kvm_shadow_mem,
                        NULL, NULL, NULL);
352 353 354
    object_property_set_description(obj, "kvm-shadow-mem",
                                    "KVM shadow MMU size",
                                    NULL);
355 356
    object_property_add_str(obj, "kernel",
                            machine_get_kernel, machine_set_kernel, NULL);
357 358 359
    object_property_set_description(obj, "kernel",
                                    "Linux kernel image file",
                                    NULL);
360 361
    object_property_add_str(obj, "initrd",
                            machine_get_initrd, machine_set_initrd, NULL);
362 363 364
    object_property_set_description(obj, "initrd",
                                    "Linux initial ramdisk file",
                                    NULL);
365 366
    object_property_add_str(obj, "append",
                            machine_get_append, machine_set_append, NULL);
367 368 369
    object_property_set_description(obj, "append",
                                    "Linux kernel command line",
                                    NULL);
370 371
    object_property_add_str(obj, "dtb",
                            machine_get_dtb, machine_set_dtb, NULL);
372 373 374
    object_property_set_description(obj, "dtb",
                                    "Linux kernel device tree file",
                                    NULL);
375 376
    object_property_add_str(obj, "dumpdtb",
                            machine_get_dumpdtb, machine_set_dumpdtb, NULL);
377 378 379
    object_property_set_description(obj, "dumpdtb",
                                    "Dump current dtb to a file and quit",
                                    NULL);
380
    object_property_add(obj, "phandle-start", "int",
381 382 383
                        machine_get_phandle_start,
                        machine_set_phandle_start,
                        NULL, NULL, NULL);
384 385 386
    object_property_set_description(obj, "phandle-start",
                                    "The first phandle ID we may generate dynamically",
                                    NULL);
387
    object_property_add_str(obj, "dt-compatible",
388 389 390
                            machine_get_dt_compatible,
                            machine_set_dt_compatible,
                            NULL);
391 392 393
    object_property_set_description(obj, "dt-compatible",
                                    "Overrides the \"compatible\" property of the dt root node",
                                    NULL);
394 395 396 397
    object_property_add_bool(obj, "dump-guest-core",
                             machine_get_dump_guest_core,
                             machine_set_dump_guest_core,
                             NULL);
398 399 400
    object_property_set_description(obj, "dump-guest-core",
                                    "Include guest memory in  a core dump",
                                    NULL);
401
    object_property_add_bool(obj, "mem-merge",
402 403
                             machine_get_mem_merge,
                             machine_set_mem_merge, NULL);
404 405 406
    object_property_set_description(obj, "mem-merge",
                                    "Enable/disable memory merge support",
                                    NULL);
407 408 409
    object_property_add_bool(obj, "usb",
                             machine_get_usb,
                             machine_set_usb, NULL);
410 411 412
    object_property_set_description(obj, "usb",
                                    "Set on/off to enable/disable usb",
                                    NULL);
413 414 415 416 417 418
    object_property_add_bool(obj, "igd-passthru",
                             machine_get_igd_gfx_passthru,
                             machine_set_igd_gfx_passthru, NULL);
    object_property_set_description(obj, "igd-passthru",
                                    "Set on/off to enable/disable igd passthrou",
                                    NULL);
419
    object_property_add_str(obj, "firmware",
420 421
                            machine_get_firmware,
                            machine_set_firmware, NULL);
422 423 424
    object_property_set_description(obj, "firmware",
                                    "Firmware image",
                                    NULL);
425 426 427
    object_property_add_bool(obj, "iommu",
                             machine_get_iommu,
                             machine_set_iommu, NULL);
428 429 430
    object_property_set_description(obj, "iommu",
                                    "Set on/off to enable/disable Intel IOMMU (VT-d)",
                                    NULL);
431 432 433 434 435 436
    object_property_add_bool(obj, "suppress-vmdesc",
                             machine_get_suppress_vmdesc,
                             machine_set_suppress_vmdesc, NULL);
    object_property_set_description(obj, "suppress-vmdesc",
                                    "Set on to disable self-describing migration",
                                    NULL);
437 438 439 440

    /* Register notifier when init is done for sysbus sanity checks */
    ms->sysbus_notifier.notify = machine_init_notify;
    qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}

static void machine_finalize(Object *obj)
{
    MachineState *ms = MACHINE(obj);

    g_free(ms->accel);
    g_free(ms->kernel_filename);
    g_free(ms->initrd_filename);
    g_free(ms->kernel_cmdline);
    g_free(ms->dtb);
    g_free(ms->dumpdtb);
    g_free(ms->dt_compatible);
    g_free(ms->firmware);
}
456

457 458 459 460 461
bool machine_usb(MachineState *machine)
{
    return machine->usb;
}

462 463 464 465 466
bool machine_iommu(MachineState *machine)
{
    return machine->iommu;
}

467 468 469 470 471 472 473 474 475 476
bool machine_kernel_irqchip_allowed(MachineState *machine)
{
    return machine->kernel_irqchip_allowed;
}

bool machine_kernel_irqchip_required(MachineState *machine)
{
    return machine->kernel_irqchip_required;
}

477 478 479 480 481
int machine_kvm_shadow_mem(MachineState *machine)
{
    return machine->kvm_shadow_mem;
}

482 483 484 485 486
int machine_phandle_start(MachineState *machine)
{
    return machine->phandle_start;
}

487 488 489 490 491
bool machine_dump_guest_core(MachineState *machine)
{
    return machine->dump_guest_core;
}

492 493 494 495 496
bool machine_mem_merge(MachineState *machine)
{
    return machine->mem_merge;
}

497 498 499 500 501
static const TypeInfo machine_info = {
    .name = TYPE_MACHINE,
    .parent = TYPE_OBJECT,
    .abstract = true,
    .class_size = sizeof(MachineClass),
502
    .class_init    = machine_class_init,
503
    .class_base_init = machine_class_base_init,
504
    .instance_size = sizeof(MachineState),
505 506
    .instance_init = machine_initfn,
    .instance_finalize = machine_finalize,
507 508 509 510 511 512 513 514
};

static void machine_register_types(void)
{
    type_register_static(&machine_info);
}

type_init(machine_register_types)