qemu/hw/core/machine.c
<<
>>
Prefs
   1/*
   2 * QEMU Machine
   3 *
   4 * Copyright (C) 2014 Red Hat Inc
   5 *
   6 * Authors:
   7 *   Marcel Apfelbaum <marcel.a@redhat.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10 * See the COPYING file in the top-level directory.
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "hw/boards.h"
  15#include "qapi/error.h"
  16#include "qapi-visit.h"
  17#include "qapi/visitor.h"
  18#include "hw/sysbus.h"
  19#include "sysemu/sysemu.h"
  20#include "qemu/error-report.h"
  21#include "qemu/cutils.h"
  22
  23static char *machine_get_accel(Object *obj, Error **errp)
  24{
  25    MachineState *ms = MACHINE(obj);
  26
  27    return g_strdup(ms->accel);
  28}
  29
  30static void machine_set_accel(Object *obj, const char *value, Error **errp)
  31{
  32    MachineState *ms = MACHINE(obj);
  33
  34    g_free(ms->accel);
  35    ms->accel = g_strdup(value);
  36}
  37
  38static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
  39                                       const char *name, void *opaque,
  40                                       Error **errp)
  41{
  42    Error *err = NULL;
  43    MachineState *ms = MACHINE(obj);
  44    OnOffSplit mode;
  45
  46    visit_type_OnOffSplit(v, name, &mode, &err);
  47    if (err) {
  48        error_propagate(errp, err);
  49        return;
  50    } else {
  51        switch (mode) {
  52        case ON_OFF_SPLIT_ON:
  53            ms->kernel_irqchip_allowed = true;
  54            ms->kernel_irqchip_required = true;
  55            ms->kernel_irqchip_split = false;
  56            break;
  57        case ON_OFF_SPLIT_OFF:
  58            ms->kernel_irqchip_allowed = false;
  59            ms->kernel_irqchip_required = false;
  60            ms->kernel_irqchip_split = false;
  61            break;
  62        case ON_OFF_SPLIT_SPLIT:
  63            ms->kernel_irqchip_allowed = true;
  64            ms->kernel_irqchip_required = true;
  65            ms->kernel_irqchip_split = true;
  66            break;
  67        default:
  68            /* The value was checked in visit_type_OnOffSplit() above. If
  69             * we get here, then something is wrong in QEMU.
  70             */
  71            abort();
  72        }
  73    }
  74}
  75
  76static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
  77                                       const char *name, void *opaque,
  78                                       Error **errp)
  79{
  80    MachineState *ms = MACHINE(obj);
  81    int64_t value = ms->kvm_shadow_mem;
  82
  83    visit_type_int(v, name, &value, errp);
  84}
  85
  86static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
  87                                       const char *name, void *opaque,
  88                                       Error **errp)
  89{
  90    MachineState *ms = MACHINE(obj);
  91    Error *error = NULL;
  92    int64_t value;
  93
  94    visit_type_int(v, name, &value, &error);
  95    if (error) {
  96        error_propagate(errp, error);
  97        return;
  98    }
  99
 100    ms->kvm_shadow_mem = value;
 101}
 102
 103static char *machine_get_kernel(Object *obj, Error **errp)
 104{
 105    MachineState *ms = MACHINE(obj);
 106
 107    return g_strdup(ms->kernel_filename);
 108}
 109
 110static void machine_set_kernel(Object *obj, const char *value, Error **errp)
 111{
 112    MachineState *ms = MACHINE(obj);
 113
 114    g_free(ms->kernel_filename);
 115    ms->kernel_filename = g_strdup(value);
 116}
 117
 118static char *machine_get_initrd(Object *obj, Error **errp)
 119{
 120    MachineState *ms = MACHINE(obj);
 121
 122    return g_strdup(ms->initrd_filename);
 123}
 124
 125static void machine_set_initrd(Object *obj, const char *value, Error **errp)
 126{
 127    MachineState *ms = MACHINE(obj);
 128
 129    g_free(ms->initrd_filename);
 130    ms->initrd_filename = g_strdup(value);
 131}
 132
 133static char *machine_get_append(Object *obj, Error **errp)
 134{
 135    MachineState *ms = MACHINE(obj);
 136
 137    return g_strdup(ms->kernel_cmdline);
 138}
 139
 140static void machine_set_append(Object *obj, const char *value, Error **errp)
 141{
 142    MachineState *ms = MACHINE(obj);
 143
 144    g_free(ms->kernel_cmdline);
 145    ms->kernel_cmdline = g_strdup(value);
 146}
 147
 148static char *machine_get_dtb(Object *obj, Error **errp)
 149{
 150    MachineState *ms = MACHINE(obj);
 151
 152    return g_strdup(ms->dtb);
 153}
 154
 155static void machine_set_dtb(Object *obj, const char *value, Error **errp)
 156{
 157    MachineState *ms = MACHINE(obj);
 158
 159    g_free(ms->dtb);
 160    ms->dtb = g_strdup(value);
 161}
 162
 163static char *machine_get_dumpdtb(Object *obj, Error **errp)
 164{
 165    MachineState *ms = MACHINE(obj);
 166
 167    return g_strdup(ms->dumpdtb);
 168}
 169
 170static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
 171{
 172    MachineState *ms = MACHINE(obj);
 173
 174    g_free(ms->dumpdtb);
 175    ms->dumpdtb = g_strdup(value);
 176}
 177
 178static void machine_get_phandle_start(Object *obj, Visitor *v,
 179                                      const char *name, void *opaque,
 180                                      Error **errp)
 181{
 182    MachineState *ms = MACHINE(obj);
 183    int64_t value = ms->phandle_start;
 184
 185    visit_type_int(v, name, &value, errp);
 186}
 187
 188static void machine_set_phandle_start(Object *obj, Visitor *v,
 189                                      const char *name, void *opaque,
 190                                      Error **errp)
 191{
 192    MachineState *ms = MACHINE(obj);
 193    Error *error = NULL;
 194    int64_t value;
 195
 196    visit_type_int(v, name, &value, &error);
 197    if (error) {
 198        error_propagate(errp, error);
 199        return;
 200    }
 201
 202    ms->phandle_start = value;
 203}
 204
 205static char *machine_get_dt_compatible(Object *obj, Error **errp)
 206{
 207    MachineState *ms = MACHINE(obj);
 208
 209    return g_strdup(ms->dt_compatible);
 210}
 211
 212static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
 213{
 214    MachineState *ms = MACHINE(obj);
 215
 216    g_free(ms->dt_compatible);
 217    ms->dt_compatible = g_strdup(value);
 218}
 219
 220static bool machine_get_dump_guest_core(Object *obj, Error **errp)
 221{
 222    MachineState *ms = MACHINE(obj);
 223
 224    return ms->dump_guest_core;
 225}
 226
 227static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
 228{
 229    MachineState *ms = MACHINE(obj);
 230
 231    ms->dump_guest_core = value;
 232}
 233
 234static bool machine_get_mem_merge(Object *obj, Error **errp)
 235{
 236    MachineState *ms = MACHINE(obj);
 237
 238    return ms->mem_merge;
 239}
 240
 241static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
 242{
 243    MachineState *ms = MACHINE(obj);
 244
 245    ms->mem_merge = value;
 246}
 247
 248static bool machine_get_usb(Object *obj, Error **errp)
 249{
 250    MachineState *ms = MACHINE(obj);
 251
 252    return ms->usb;
 253}
 254
 255static void machine_set_usb(Object *obj, bool value, Error **errp)
 256{
 257    MachineState *ms = MACHINE(obj);
 258
 259    ms->usb = value;
 260    ms->usb_disabled = !value;
 261}
 262
 263static bool machine_get_graphics(Object *obj, Error **errp)
 264{
 265    MachineState *ms = MACHINE(obj);
 266
 267    return ms->enable_graphics;
 268}
 269
 270static void machine_set_graphics(Object *obj, bool value, Error **errp)
 271{
 272    MachineState *ms = MACHINE(obj);
 273
 274    ms->enable_graphics = value;
 275}
 276
 277static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
 278{
 279    MachineState *ms = MACHINE(obj);
 280
 281    return ms->igd_gfx_passthru;
 282}
 283
 284static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
 285{
 286    MachineState *ms = MACHINE(obj);
 287
 288    ms->igd_gfx_passthru = value;
 289}
 290
 291static char *machine_get_firmware(Object *obj, Error **errp)
 292{
 293    MachineState *ms = MACHINE(obj);
 294
 295    return g_strdup(ms->firmware);
 296}
 297
 298static void machine_set_firmware(Object *obj, const char *value, Error **errp)
 299{
 300    MachineState *ms = MACHINE(obj);
 301
 302    g_free(ms->firmware);
 303    ms->firmware = g_strdup(value);
 304}
 305
 306static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
 307{
 308    MachineState *ms = MACHINE(obj);
 309
 310    ms->suppress_vmdesc = value;
 311}
 312
 313static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
 314{
 315    MachineState *ms = MACHINE(obj);
 316
 317    return ms->suppress_vmdesc;
 318}
 319
 320static void machine_set_enforce_config_section(Object *obj, bool value,
 321                                             Error **errp)
 322{
 323    MachineState *ms = MACHINE(obj);
 324
 325    ms->enforce_config_section = value;
 326}
 327
 328static bool machine_get_enforce_config_section(Object *obj, Error **errp)
 329{
 330    MachineState *ms = MACHINE(obj);
 331
 332    return ms->enforce_config_section;
 333}
 334
 335static void error_on_sysbus_device(SysBusDevice *sbdev, void *opaque)
 336{
 337    error_report("Option '-device %s' cannot be handled by this machine",
 338                 object_class_get_name(object_get_class(OBJECT(sbdev))));
 339    exit(1);
 340}
 341
 342static void machine_init_notify(Notifier *notifier, void *data)
 343{
 344    Object *machine = qdev_get_machine();
 345    ObjectClass *oc = object_get_class(machine);
 346    MachineClass *mc = MACHINE_CLASS(oc);
 347
 348    if (mc->has_dynamic_sysbus) {
 349        /* Our machine can handle dynamic sysbus devices, we're all good */
 350        return;
 351    }
 352
 353    /*
 354     * Loop through all dynamically created devices and check whether there
 355     * are sysbus devices among them. If there are, error out.
 356     */
 357    foreach_dynamic_sysbus_device(error_on_sysbus_device, NULL);
 358}
 359
 360HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
 361{
 362    int i;
 363    Object *cpu;
 364    HotpluggableCPUList *head = NULL;
 365    const char *cpu_type;
 366
 367    cpu = machine->possible_cpus->cpus[0].cpu;
 368    assert(cpu); /* Boot cpu is always present */
 369    cpu_type = object_get_typename(cpu);
 370    for (i = 0; i < machine->possible_cpus->len; i++) {
 371        HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
 372        HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
 373
 374        cpu_item->type = g_strdup(cpu_type);
 375        cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
 376        cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
 377                                   sizeof(*cpu_item->props));
 378
 379        cpu = machine->possible_cpus->cpus[i].cpu;
 380        if (cpu) {
 381            cpu_item->has_qom_path = true;
 382            cpu_item->qom_path = object_get_canonical_path(cpu);
 383        }
 384        list_item->value = cpu_item;
 385        list_item->next = head;
 386        head = list_item;
 387    }
 388    return head;
 389}
 390
 391static void machine_class_init(ObjectClass *oc, void *data)
 392{
 393    MachineClass *mc = MACHINE_CLASS(oc);
 394
 395    /* Default 128 MB as guest ram size */
 396    mc->default_ram_size = 128 * M_BYTE;
 397    mc->rom_file_has_mr = true;
 398
 399    /* numa node memory size aligned on 8MB by default.
 400     * On Linux, each node's border has to be 8MB aligned
 401     */
 402    mc->numa_mem_align_shift = 23;
 403
 404    object_class_property_add_str(oc, "accel",
 405        machine_get_accel, machine_set_accel, &error_abort);
 406    object_class_property_set_description(oc, "accel",
 407        "Accelerator list", &error_abort);
 408
 409    object_class_property_add(oc, "kernel-irqchip", "OnOffSplit",
 410        NULL, machine_set_kernel_irqchip,
 411        NULL, NULL, &error_abort);
 412    object_class_property_set_description(oc, "kernel-irqchip",
 413        "Configure KVM in-kernel irqchip", &error_abort);
 414
 415    object_class_property_add(oc, "kvm-shadow-mem", "int",
 416        machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
 417        NULL, NULL, &error_abort);
 418    object_class_property_set_description(oc, "kvm-shadow-mem",
 419        "KVM shadow MMU size", &error_abort);
 420
 421    object_class_property_add_str(oc, "kernel",
 422        machine_get_kernel, machine_set_kernel, &error_abort);
 423    object_class_property_set_description(oc, "kernel",
 424        "Linux kernel image file", &error_abort);
 425
 426    object_class_property_add_str(oc, "initrd",
 427        machine_get_initrd, machine_set_initrd, &error_abort);
 428    object_class_property_set_description(oc, "initrd",
 429        "Linux initial ramdisk file", &error_abort);
 430
 431    object_class_property_add_str(oc, "append",
 432        machine_get_append, machine_set_append, &error_abort);
 433    object_class_property_set_description(oc, "append",
 434        "Linux kernel command line", &error_abort);
 435
 436    object_class_property_add_str(oc, "dtb",
 437        machine_get_dtb, machine_set_dtb, &error_abort);
 438    object_class_property_set_description(oc, "dtb",
 439        "Linux kernel device tree file", &error_abort);
 440
 441    object_class_property_add_str(oc, "dumpdtb",
 442        machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
 443    object_class_property_set_description(oc, "dumpdtb",
 444        "Dump current dtb to a file and quit", &error_abort);
 445
 446    object_class_property_add(oc, "phandle-start", "int",
 447        machine_get_phandle_start, machine_set_phandle_start,
 448        NULL, NULL, &error_abort);
 449    object_class_property_set_description(oc, "phandle-start",
 450            "The first phandle ID we may generate dynamically", &error_abort);
 451
 452    object_class_property_add_str(oc, "dt-compatible",
 453        machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
 454    object_class_property_set_description(oc, "dt-compatible",
 455        "Overrides the \"compatible\" property of the dt root node",
 456        &error_abort);
 457
 458    object_class_property_add_bool(oc, "dump-guest-core",
 459        machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
 460    object_class_property_set_description(oc, "dump-guest-core",
 461        "Include guest memory in  a core dump", &error_abort);
 462
 463    object_class_property_add_bool(oc, "mem-merge",
 464        machine_get_mem_merge, machine_set_mem_merge, &error_abort);
 465    object_class_property_set_description(oc, "mem-merge",
 466        "Enable/disable memory merge support", &error_abort);
 467
 468    object_class_property_add_bool(oc, "usb",
 469        machine_get_usb, machine_set_usb, &error_abort);
 470    object_class_property_set_description(oc, "usb",
 471        "Set on/off to enable/disable usb", &error_abort);
 472
 473    object_class_property_add_bool(oc, "graphics",
 474        machine_get_graphics, machine_set_graphics, &error_abort);
 475    object_class_property_set_description(oc, "graphics",
 476        "Set on/off to enable/disable graphics emulation", &error_abort);
 477
 478    object_class_property_add_bool(oc, "igd-passthru",
 479        machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
 480        &error_abort);
 481    object_class_property_set_description(oc, "igd-passthru",
 482        "Set on/off to enable/disable igd passthrou", &error_abort);
 483
 484    object_class_property_add_str(oc, "firmware",
 485        machine_get_firmware, machine_set_firmware,
 486        &error_abort);
 487    object_class_property_set_description(oc, "firmware",
 488        "Firmware image", &error_abort);
 489
 490    object_class_property_add_bool(oc, "suppress-vmdesc",
 491        machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
 492        &error_abort);
 493    object_class_property_set_description(oc, "suppress-vmdesc",
 494        "Set on to disable self-describing migration", &error_abort);
 495
 496    object_class_property_add_bool(oc, "enforce-config-section",
 497        machine_get_enforce_config_section, machine_set_enforce_config_section,
 498        &error_abort);
 499    object_class_property_set_description(oc, "enforce-config-section",
 500        "Set on to enforce configuration section migration", &error_abort);
 501}
 502
 503static void machine_class_base_init(ObjectClass *oc, void *data)
 504{
 505    if (!object_class_is_abstract(oc)) {
 506        MachineClass *mc = MACHINE_CLASS(oc);
 507        const char *cname = object_class_get_name(oc);
 508        assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
 509        mc->name = g_strndup(cname,
 510                            strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
 511    }
 512}
 513
 514static void machine_initfn(Object *obj)
 515{
 516    MachineState *ms = MACHINE(obj);
 517
 518    ms->kernel_irqchip_allowed = true;
 519    ms->kvm_shadow_mem = -1;
 520    ms->dump_guest_core = true;
 521    ms->mem_merge = true;
 522    ms->enable_graphics = true;
 523
 524    /* Register notifier when init is done for sysbus sanity checks */
 525    ms->sysbus_notifier.notify = machine_init_notify;
 526    qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
 527}
 528
 529static void machine_finalize(Object *obj)
 530{
 531    MachineState *ms = MACHINE(obj);
 532
 533    g_free(ms->accel);
 534    g_free(ms->kernel_filename);
 535    g_free(ms->initrd_filename);
 536    g_free(ms->kernel_cmdline);
 537    g_free(ms->dtb);
 538    g_free(ms->dumpdtb);
 539    g_free(ms->dt_compatible);
 540    g_free(ms->firmware);
 541}
 542
 543bool machine_usb(MachineState *machine)
 544{
 545    return machine->usb;
 546}
 547
 548bool machine_kernel_irqchip_allowed(MachineState *machine)
 549{
 550    return machine->kernel_irqchip_allowed;
 551}
 552
 553bool machine_kernel_irqchip_required(MachineState *machine)
 554{
 555    return machine->kernel_irqchip_required;
 556}
 557
 558bool machine_kernel_irqchip_split(MachineState *machine)
 559{
 560    return machine->kernel_irqchip_split;
 561}
 562
 563int machine_kvm_shadow_mem(MachineState *machine)
 564{
 565    return machine->kvm_shadow_mem;
 566}
 567
 568int machine_phandle_start(MachineState *machine)
 569{
 570    return machine->phandle_start;
 571}
 572
 573bool machine_dump_guest_core(MachineState *machine)
 574{
 575    return machine->dump_guest_core;
 576}
 577
 578bool machine_mem_merge(MachineState *machine)
 579{
 580    return machine->mem_merge;
 581}
 582
 583static void machine_class_finalize(ObjectClass *klass, void *data)
 584{
 585    MachineClass *mc = MACHINE_CLASS(klass);
 586
 587    if (mc->compat_props) {
 588        g_array_free(mc->compat_props, true);
 589    }
 590    g_free(mc->name);
 591}
 592
 593static void register_compat_prop(const char *driver,
 594                                 const char *property,
 595                                 const char *value)
 596{
 597    GlobalProperty *p = g_new0(GlobalProperty, 1);
 598    /* Machine compat_props must never cause errors: */
 599    p->errp = &error_abort;
 600    p->driver = driver;
 601    p->property = property;
 602    p->value = value;
 603    qdev_prop_register_global(p);
 604}
 605
 606static void machine_register_compat_for_subclass(ObjectClass *oc, void *opaque)
 607{
 608    GlobalProperty *p = opaque;
 609    register_compat_prop(object_class_get_name(oc), p->property, p->value);
 610}
 611
 612void machine_register_compat_props(MachineState *machine)
 613{
 614    MachineClass *mc = MACHINE_GET_CLASS(machine);
 615    int i;
 616    GlobalProperty *p;
 617    ObjectClass *oc;
 618
 619    if (!mc->compat_props) {
 620        return;
 621    }
 622
 623    for (i = 0; i < mc->compat_props->len; i++) {
 624        p = g_array_index(mc->compat_props, GlobalProperty *, i);
 625        oc = object_class_by_name(p->driver);
 626        if (oc && object_class_is_abstract(oc)) {
 627            /* temporary hack to make sure we do not override
 628             * globals set explicitly on -global: if an abstract class
 629             * is on compat_props, register globals for all its
 630             * non-abstract subtypes instead.
 631             *
 632             * This doesn't solve the problem for cases where
 633             * a non-abstract typename mentioned on compat_props
 634             * has subclasses, like spapr-pci-host-bridge.
 635             */
 636            object_class_foreach(machine_register_compat_for_subclass,
 637                                 p->driver, false, p);
 638        } else {
 639            register_compat_prop(p->driver, p->property, p->value);
 640        }
 641    }
 642}
 643
 644static const TypeInfo machine_info = {
 645    .name = TYPE_MACHINE,
 646    .parent = TYPE_OBJECT,
 647    .abstract = true,
 648    .class_size = sizeof(MachineClass),
 649    .class_init    = machine_class_init,
 650    .class_base_init = machine_class_base_init,
 651    .class_finalize = machine_class_finalize,
 652    .instance_size = sizeof(MachineState),
 653    .instance_init = machine_initfn,
 654    .instance_finalize = machine_finalize,
 655};
 656
 657static void machine_register_types(void)
 658{
 659    type_register_static(&machine_info);
 660}
 661
 662type_init(machine_register_types)
 663