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 "sysemu/numa.h" 21 #include "qemu/error-report.h" 22 #include "qemu/cutils.h" 23 #include "sysemu/qtest.h" 24 25 static char *machine_get_accel(Object *obj, Error **errp) 26 { 27 MachineState *ms = MACHINE(obj); 28 29 return g_strdup(ms->accel); 30 } 31 32 static void machine_set_accel(Object *obj, const char *value, Error **errp) 33 { 34 MachineState *ms = MACHINE(obj); 35 36 g_free(ms->accel); 37 ms->accel = g_strdup(value); 38 } 39 40 static void machine_set_kernel_irqchip(Object *obj, Visitor *v, 41 const char *name, void *opaque, 42 Error **errp) 43 { 44 Error *err = NULL; 45 MachineState *ms = MACHINE(obj); 46 OnOffSplit mode; 47 48 visit_type_OnOffSplit(v, name, &mode, &err); 49 if (err) { 50 error_propagate(errp, err); 51 return; 52 } else { 53 switch (mode) { 54 case ON_OFF_SPLIT_ON: 55 ms->kernel_irqchip_allowed = true; 56 ms->kernel_irqchip_required = true; 57 ms->kernel_irqchip_split = false; 58 break; 59 case ON_OFF_SPLIT_OFF: 60 ms->kernel_irqchip_allowed = false; 61 ms->kernel_irqchip_required = false; 62 ms->kernel_irqchip_split = false; 63 break; 64 case ON_OFF_SPLIT_SPLIT: 65 ms->kernel_irqchip_allowed = true; 66 ms->kernel_irqchip_required = true; 67 ms->kernel_irqchip_split = true; 68 break; 69 default: 70 /* The value was checked in visit_type_OnOffSplit() above. If 71 * we get here, then something is wrong in QEMU. 72 */ 73 abort(); 74 } 75 } 76 } 77 78 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v, 79 const char *name, void *opaque, 80 Error **errp) 81 { 82 MachineState *ms = MACHINE(obj); 83 int64_t value = ms->kvm_shadow_mem; 84 85 visit_type_int(v, name, &value, errp); 86 } 87 88 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v, 89 const char *name, void *opaque, 90 Error **errp) 91 { 92 MachineState *ms = MACHINE(obj); 93 Error *error = NULL; 94 int64_t value; 95 96 visit_type_int(v, name, &value, &error); 97 if (error) { 98 error_propagate(errp, error); 99 return; 100 } 101 102 ms->kvm_shadow_mem = value; 103 } 104 105 static char *machine_get_kernel(Object *obj, Error **errp) 106 { 107 MachineState *ms = MACHINE(obj); 108 109 return g_strdup(ms->kernel_filename); 110 } 111 112 static void machine_set_kernel(Object *obj, const char *value, Error **errp) 113 { 114 MachineState *ms = MACHINE(obj); 115 116 g_free(ms->kernel_filename); 117 ms->kernel_filename = g_strdup(value); 118 } 119 120 static char *machine_get_initrd(Object *obj, Error **errp) 121 { 122 MachineState *ms = MACHINE(obj); 123 124 return g_strdup(ms->initrd_filename); 125 } 126 127 static void machine_set_initrd(Object *obj, const char *value, Error **errp) 128 { 129 MachineState *ms = MACHINE(obj); 130 131 g_free(ms->initrd_filename); 132 ms->initrd_filename = g_strdup(value); 133 } 134 135 static char *machine_get_append(Object *obj, Error **errp) 136 { 137 MachineState *ms = MACHINE(obj); 138 139 return g_strdup(ms->kernel_cmdline); 140 } 141 142 static void machine_set_append(Object *obj, const char *value, Error **errp) 143 { 144 MachineState *ms = MACHINE(obj); 145 146 g_free(ms->kernel_cmdline); 147 ms->kernel_cmdline = g_strdup(value); 148 } 149 150 static char *machine_get_dtb(Object *obj, Error **errp) 151 { 152 MachineState *ms = MACHINE(obj); 153 154 return g_strdup(ms->dtb); 155 } 156 157 static void machine_set_dtb(Object *obj, const char *value, Error **errp) 158 { 159 MachineState *ms = MACHINE(obj); 160 161 g_free(ms->dtb); 162 ms->dtb = g_strdup(value); 163 } 164 165 static char *machine_get_dumpdtb(Object *obj, Error **errp) 166 { 167 MachineState *ms = MACHINE(obj); 168 169 return g_strdup(ms->dumpdtb); 170 } 171 172 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp) 173 { 174 MachineState *ms = MACHINE(obj); 175 176 g_free(ms->dumpdtb); 177 ms->dumpdtb = g_strdup(value); 178 } 179 180 static void machine_get_phandle_start(Object *obj, Visitor *v, 181 const char *name, void *opaque, 182 Error **errp) 183 { 184 MachineState *ms = MACHINE(obj); 185 int64_t value = ms->phandle_start; 186 187 visit_type_int(v, name, &value, errp); 188 } 189 190 static void machine_set_phandle_start(Object *obj, Visitor *v, 191 const char *name, void *opaque, 192 Error **errp) 193 { 194 MachineState *ms = MACHINE(obj); 195 Error *error = NULL; 196 int64_t value; 197 198 visit_type_int(v, name, &value, &error); 199 if (error) { 200 error_propagate(errp, error); 201 return; 202 } 203 204 ms->phandle_start = value; 205 } 206 207 static char *machine_get_dt_compatible(Object *obj, Error **errp) 208 { 209 MachineState *ms = MACHINE(obj); 210 211 return g_strdup(ms->dt_compatible); 212 } 213 214 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp) 215 { 216 MachineState *ms = MACHINE(obj); 217 218 g_free(ms->dt_compatible); 219 ms->dt_compatible = g_strdup(value); 220 } 221 222 static bool machine_get_dump_guest_core(Object *obj, Error **errp) 223 { 224 MachineState *ms = MACHINE(obj); 225 226 return ms->dump_guest_core; 227 } 228 229 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp) 230 { 231 MachineState *ms = MACHINE(obj); 232 233 ms->dump_guest_core = value; 234 } 235 236 static bool machine_get_mem_merge(Object *obj, Error **errp) 237 { 238 MachineState *ms = MACHINE(obj); 239 240 return ms->mem_merge; 241 } 242 243 static void machine_set_mem_merge(Object *obj, bool value, Error **errp) 244 { 245 MachineState *ms = MACHINE(obj); 246 247 ms->mem_merge = value; 248 } 249 250 static bool machine_get_usb(Object *obj, Error **errp) 251 { 252 MachineState *ms = MACHINE(obj); 253 254 return ms->usb; 255 } 256 257 static void machine_set_usb(Object *obj, bool value, Error **errp) 258 { 259 MachineState *ms = MACHINE(obj); 260 261 ms->usb = value; 262 ms->usb_disabled = !value; 263 } 264 265 static bool machine_get_graphics(Object *obj, Error **errp) 266 { 267 MachineState *ms = MACHINE(obj); 268 269 return ms->enable_graphics; 270 } 271 272 static void machine_set_graphics(Object *obj, bool value, Error **errp) 273 { 274 MachineState *ms = MACHINE(obj); 275 276 ms->enable_graphics = value; 277 } 278 279 static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp) 280 { 281 MachineState *ms = MACHINE(obj); 282 283 return ms->igd_gfx_passthru; 284 } 285 286 static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp) 287 { 288 MachineState *ms = MACHINE(obj); 289 290 ms->igd_gfx_passthru = value; 291 } 292 293 static char *machine_get_firmware(Object *obj, Error **errp) 294 { 295 MachineState *ms = MACHINE(obj); 296 297 return g_strdup(ms->firmware); 298 } 299 300 static void machine_set_firmware(Object *obj, const char *value, Error **errp) 301 { 302 MachineState *ms = MACHINE(obj); 303 304 g_free(ms->firmware); 305 ms->firmware = g_strdup(value); 306 } 307 308 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp) 309 { 310 MachineState *ms = MACHINE(obj); 311 312 ms->suppress_vmdesc = value; 313 } 314 315 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp) 316 { 317 MachineState *ms = MACHINE(obj); 318 319 return ms->suppress_vmdesc; 320 } 321 322 static void machine_set_enforce_config_section(Object *obj, bool value, 323 Error **errp) 324 { 325 MachineState *ms = MACHINE(obj); 326 327 ms->enforce_config_section = value; 328 } 329 330 static bool machine_get_enforce_config_section(Object *obj, Error **errp) 331 { 332 MachineState *ms = MACHINE(obj); 333 334 return ms->enforce_config_section; 335 } 336 337 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type) 338 { 339 strList *item = g_new0(strList, 1); 340 341 item->value = g_strdup(type); 342 item->next = mc->allowed_dynamic_sysbus_devices; 343 mc->allowed_dynamic_sysbus_devices = item; 344 } 345 346 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque) 347 { 348 MachineState *machine = opaque; 349 MachineClass *mc = MACHINE_GET_CLASS(machine); 350 bool allowed = false; 351 strList *wl; 352 353 for (wl = mc->allowed_dynamic_sysbus_devices; 354 !allowed && wl; 355 wl = wl->next) { 356 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value); 357 } 358 359 if (!allowed) { 360 error_report("Option '-device %s' cannot be handled by this machine", 361 object_class_get_name(object_get_class(OBJECT(sbdev)))); 362 exit(1); 363 } 364 } 365 366 static void machine_init_notify(Notifier *notifier, void *data) 367 { 368 MachineState *machine = MACHINE(qdev_get_machine()); 369 370 /* 371 * Loop through all dynamically created sysbus devices and check if they are 372 * all allowed. If a device is not allowed, error out. 373 */ 374 foreach_dynamic_sysbus_device(validate_sysbus_device, machine); 375 } 376 377 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine) 378 { 379 int i; 380 HotpluggableCPUList *head = NULL; 381 MachineClass *mc = MACHINE_GET_CLASS(machine); 382 383 /* force board to initialize possible_cpus if it hasn't been done yet */ 384 mc->possible_cpu_arch_ids(machine); 385 386 for (i = 0; i < machine->possible_cpus->len; i++) { 387 Object *cpu; 388 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1); 389 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1); 390 391 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type); 392 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count; 393 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props, 394 sizeof(*cpu_item->props)); 395 396 cpu = machine->possible_cpus->cpus[i].cpu; 397 if (cpu) { 398 cpu_item->has_qom_path = true; 399 cpu_item->qom_path = object_get_canonical_path(cpu); 400 } 401 list_item->value = cpu_item; 402 list_item->next = head; 403 head = list_item; 404 } 405 return head; 406 } 407 408 /** 409 * machine_set_cpu_numa_node: 410 * @machine: machine object to modify 411 * @props: specifies which cpu objects to assign to 412 * numa node specified by @props.node_id 413 * @errp: if an error occurs, a pointer to an area to store the error 414 * 415 * Associate NUMA node specified by @props.node_id with cpu slots that 416 * match socket/core/thread-ids specified by @props. It's recommended to use 417 * query-hotpluggable-cpus.props values to specify affected cpu slots, 418 * which would lead to exact 1:1 mapping of cpu slots to NUMA node. 419 * 420 * However for CLI convenience it's possible to pass in subset of properties, 421 * which would affect all cpu slots that match it. 422 * Ex for pc machine: 423 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \ 424 * -numa cpu,node-id=0,socket_id=0 \ 425 * -numa cpu,node-id=1,socket_id=1 426 * will assign all child cores of socket 0 to node 0 and 427 * of socket 1 to node 1. 428 * 429 * On attempt of reassigning (already assigned) cpu slot to another NUMA node, 430 * return error. 431 * Empty subset is disallowed and function will return with error in this case. 432 */ 433 void machine_set_cpu_numa_node(MachineState *machine, 434 const CpuInstanceProperties *props, Error **errp) 435 { 436 MachineClass *mc = MACHINE_GET_CLASS(machine); 437 bool match = false; 438 int i; 439 440 if (!mc->possible_cpu_arch_ids) { 441 error_setg(errp, "mapping of CPUs to NUMA node is not supported"); 442 return; 443 } 444 445 /* disabling node mapping is not supported, forbid it */ 446 assert(props->has_node_id); 447 448 /* force board to initialize possible_cpus if it hasn't been done yet */ 449 mc->possible_cpu_arch_ids(machine); 450 451 for (i = 0; i < machine->possible_cpus->len; i++) { 452 CPUArchId *slot = &machine->possible_cpus->cpus[i]; 453 454 /* reject unsupported by board properties */ 455 if (props->has_thread_id && !slot->props.has_thread_id) { 456 error_setg(errp, "thread-id is not supported"); 457 return; 458 } 459 460 if (props->has_core_id && !slot->props.has_core_id) { 461 error_setg(errp, "core-id is not supported"); 462 return; 463 } 464 465 if (props->has_socket_id && !slot->props.has_socket_id) { 466 error_setg(errp, "socket-id is not supported"); 467 return; 468 } 469 470 /* skip slots with explicit mismatch */ 471 if (props->has_thread_id && props->thread_id != slot->props.thread_id) { 472 continue; 473 } 474 475 if (props->has_core_id && props->core_id != slot->props.core_id) { 476 continue; 477 } 478 479 if (props->has_socket_id && props->socket_id != slot->props.socket_id) { 480 continue; 481 } 482 483 /* reject assignment if slot is already assigned, for compatibility 484 * of legacy cpu_index mapping with SPAPR core based mapping do not 485 * error out if cpu thread and matched core have the same node-id */ 486 if (slot->props.has_node_id && 487 slot->props.node_id != props->node_id) { 488 error_setg(errp, "CPU is already assigned to node-id: %" PRId64, 489 slot->props.node_id); 490 return; 491 } 492 493 /* assign slot to node as it's matched '-numa cpu' key */ 494 match = true; 495 slot->props.node_id = props->node_id; 496 slot->props.has_node_id = props->has_node_id; 497 } 498 499 if (!match) { 500 error_setg(errp, "no match found"); 501 } 502 } 503 504 static void machine_class_init(ObjectClass *oc, void *data) 505 { 506 MachineClass *mc = MACHINE_CLASS(oc); 507 508 /* Default 128 MB as guest ram size */ 509 mc->default_ram_size = 128 * M_BYTE; 510 mc->rom_file_has_mr = true; 511 512 /* numa node memory size aligned on 8MB by default. 513 * On Linux, each node's border has to be 8MB aligned 514 */ 515 mc->numa_mem_align_shift = 23; 516 mc->numa_auto_assign_ram = numa_default_auto_assign_ram; 517 518 object_class_property_add_str(oc, "accel", 519 machine_get_accel, machine_set_accel, &error_abort); 520 object_class_property_set_description(oc, "accel", 521 "Accelerator list", &error_abort); 522 523 object_class_property_add(oc, "kernel-irqchip", "OnOffSplit", 524 NULL, machine_set_kernel_irqchip, 525 NULL, NULL, &error_abort); 526 object_class_property_set_description(oc, "kernel-irqchip", 527 "Configure KVM in-kernel irqchip", &error_abort); 528 529 object_class_property_add(oc, "kvm-shadow-mem", "int", 530 machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem, 531 NULL, NULL, &error_abort); 532 object_class_property_set_description(oc, "kvm-shadow-mem", 533 "KVM shadow MMU size", &error_abort); 534 535 object_class_property_add_str(oc, "kernel", 536 machine_get_kernel, machine_set_kernel, &error_abort); 537 object_class_property_set_description(oc, "kernel", 538 "Linux kernel image file", &error_abort); 539 540 object_class_property_add_str(oc, "initrd", 541 machine_get_initrd, machine_set_initrd, &error_abort); 542 object_class_property_set_description(oc, "initrd", 543 "Linux initial ramdisk file", &error_abort); 544 545 object_class_property_add_str(oc, "append", 546 machine_get_append, machine_set_append, &error_abort); 547 object_class_property_set_description(oc, "append", 548 "Linux kernel command line", &error_abort); 549 550 object_class_property_add_str(oc, "dtb", 551 machine_get_dtb, machine_set_dtb, &error_abort); 552 object_class_property_set_description(oc, "dtb", 553 "Linux kernel device tree file", &error_abort); 554 555 object_class_property_add_str(oc, "dumpdtb", 556 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort); 557 object_class_property_set_description(oc, "dumpdtb", 558 "Dump current dtb to a file and quit", &error_abort); 559 560 object_class_property_add(oc, "phandle-start", "int", 561 machine_get_phandle_start, machine_set_phandle_start, 562 NULL, NULL, &error_abort); 563 object_class_property_set_description(oc, "phandle-start", 564 "The first phandle ID we may generate dynamically", &error_abort); 565 566 object_class_property_add_str(oc, "dt-compatible", 567 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort); 568 object_class_property_set_description(oc, "dt-compatible", 569 "Overrides the \"compatible\" property of the dt root node", 570 &error_abort); 571 572 object_class_property_add_bool(oc, "dump-guest-core", 573 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort); 574 object_class_property_set_description(oc, "dump-guest-core", 575 "Include guest memory in a core dump", &error_abort); 576 577 object_class_property_add_bool(oc, "mem-merge", 578 machine_get_mem_merge, machine_set_mem_merge, &error_abort); 579 object_class_property_set_description(oc, "mem-merge", 580 "Enable/disable memory merge support", &error_abort); 581 582 object_class_property_add_bool(oc, "usb", 583 machine_get_usb, machine_set_usb, &error_abort); 584 object_class_property_set_description(oc, "usb", 585 "Set on/off to enable/disable usb", &error_abort); 586 587 object_class_property_add_bool(oc, "graphics", 588 machine_get_graphics, machine_set_graphics, &error_abort); 589 object_class_property_set_description(oc, "graphics", 590 "Set on/off to enable/disable graphics emulation", &error_abort); 591 592 object_class_property_add_bool(oc, "igd-passthru", 593 machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru, 594 &error_abort); 595 object_class_property_set_description(oc, "igd-passthru", 596 "Set on/off to enable/disable igd passthrou", &error_abort); 597 598 object_class_property_add_str(oc, "firmware", 599 machine_get_firmware, machine_set_firmware, 600 &error_abort); 601 object_class_property_set_description(oc, "firmware", 602 "Firmware image", &error_abort); 603 604 object_class_property_add_bool(oc, "suppress-vmdesc", 605 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc, 606 &error_abort); 607 object_class_property_set_description(oc, "suppress-vmdesc", 608 "Set on to disable self-describing migration", &error_abort); 609 610 object_class_property_add_bool(oc, "enforce-config-section", 611 machine_get_enforce_config_section, machine_set_enforce_config_section, 612 &error_abort); 613 object_class_property_set_description(oc, "enforce-config-section", 614 "Set on to enforce configuration section migration", &error_abort); 615 } 616 617 static void machine_class_base_init(ObjectClass *oc, void *data) 618 { 619 if (!object_class_is_abstract(oc)) { 620 MachineClass *mc = MACHINE_CLASS(oc); 621 const char *cname = object_class_get_name(oc); 622 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX)); 623 mc->name = g_strndup(cname, 624 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX)); 625 } 626 } 627 628 static void machine_initfn(Object *obj) 629 { 630 MachineState *ms = MACHINE(obj); 631 632 ms->kernel_irqchip_allowed = true; 633 ms->kvm_shadow_mem = -1; 634 ms->dump_guest_core = true; 635 ms->mem_merge = true; 636 ms->enable_graphics = true; 637 638 /* Register notifier when init is done for sysbus sanity checks */ 639 ms->sysbus_notifier.notify = machine_init_notify; 640 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier); 641 } 642 643 static void machine_finalize(Object *obj) 644 { 645 MachineState *ms = MACHINE(obj); 646 647 g_free(ms->accel); 648 g_free(ms->kernel_filename); 649 g_free(ms->initrd_filename); 650 g_free(ms->kernel_cmdline); 651 g_free(ms->dtb); 652 g_free(ms->dumpdtb); 653 g_free(ms->dt_compatible); 654 g_free(ms->firmware); 655 } 656 657 bool machine_usb(MachineState *machine) 658 { 659 return machine->usb; 660 } 661 662 bool machine_kernel_irqchip_allowed(MachineState *machine) 663 { 664 return machine->kernel_irqchip_allowed; 665 } 666 667 bool machine_kernel_irqchip_required(MachineState *machine) 668 { 669 return machine->kernel_irqchip_required; 670 } 671 672 bool machine_kernel_irqchip_split(MachineState *machine) 673 { 674 return machine->kernel_irqchip_split; 675 } 676 677 int machine_kvm_shadow_mem(MachineState *machine) 678 { 679 return machine->kvm_shadow_mem; 680 } 681 682 int machine_phandle_start(MachineState *machine) 683 { 684 return machine->phandle_start; 685 } 686 687 bool machine_dump_guest_core(MachineState *machine) 688 { 689 return machine->dump_guest_core; 690 } 691 692 bool machine_mem_merge(MachineState *machine) 693 { 694 return machine->mem_merge; 695 } 696 697 static char *cpu_slot_to_string(const CPUArchId *cpu) 698 { 699 GString *s = g_string_new(NULL); 700 if (cpu->props.has_socket_id) { 701 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id); 702 } 703 if (cpu->props.has_core_id) { 704 if (s->len) { 705 g_string_append_printf(s, ", "); 706 } 707 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id); 708 } 709 if (cpu->props.has_thread_id) { 710 if (s->len) { 711 g_string_append_printf(s, ", "); 712 } 713 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id); 714 } 715 return g_string_free(s, false); 716 } 717 718 static void machine_numa_finish_init(MachineState *machine) 719 { 720 int i; 721 bool default_mapping; 722 GString *s = g_string_new(NULL); 723 MachineClass *mc = MACHINE_GET_CLASS(machine); 724 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine); 725 726 assert(nb_numa_nodes); 727 for (i = 0; i < possible_cpus->len; i++) { 728 if (possible_cpus->cpus[i].props.has_node_id) { 729 break; 730 } 731 } 732 default_mapping = (i == possible_cpus->len); 733 734 for (i = 0; i < possible_cpus->len; i++) { 735 const CPUArchId *cpu_slot = &possible_cpus->cpus[i]; 736 737 if (!cpu_slot->props.has_node_id) { 738 /* fetch default mapping from board and enable it */ 739 CpuInstanceProperties props = cpu_slot->props; 740 741 props.node_id = mc->get_default_cpu_node_id(machine, i); 742 if (!default_mapping) { 743 /* record slots with not set mapping, 744 * TODO: make it hard error in future */ 745 char *cpu_str = cpu_slot_to_string(cpu_slot); 746 g_string_append_printf(s, "%sCPU %d [%s]", 747 s->len ? ", " : "", i, cpu_str); 748 g_free(cpu_str); 749 750 /* non mapped cpus used to fallback to node 0 */ 751 props.node_id = 0; 752 } 753 754 props.has_node_id = true; 755 machine_set_cpu_numa_node(machine, &props, &error_fatal); 756 } 757 } 758 if (s->len && !qtest_enabled()) { 759 warn_report("CPU(s) not present in any NUMA nodes: %s", 760 s->str); 761 warn_report("All CPU(s) up to maxcpus should be described " 762 "in NUMA config, ability to start up with partial NUMA " 763 "mappings is obsoleted and will be removed in future"); 764 } 765 g_string_free(s, true); 766 } 767 768 void machine_run_board_init(MachineState *machine) 769 { 770 MachineClass *machine_class = MACHINE_GET_CLASS(machine); 771 772 if (nb_numa_nodes) { 773 machine_numa_finish_init(machine); 774 } 775 776 /* If the machine supports the valid_cpu_types check and the user 777 * specified a CPU with -cpu check here that the user CPU is supported. 778 */ 779 if (machine_class->valid_cpu_types && machine->cpu_type) { 780 ObjectClass *class = object_class_by_name(machine->cpu_type); 781 int i; 782 783 for (i = 0; machine_class->valid_cpu_types[i]; i++) { 784 if (object_class_dynamic_cast(class, 785 machine_class->valid_cpu_types[i])) { 786 /* The user specificed CPU is in the valid field, we are 787 * good to go. 788 */ 789 break; 790 } 791 } 792 793 if (!machine_class->valid_cpu_types[i]) { 794 /* The user specified CPU is not valid */ 795 error_report("Invalid CPU type: %s", machine->cpu_type); 796 error_printf("The valid types are: %s", 797 machine_class->valid_cpu_types[0]); 798 for (i = 1; machine_class->valid_cpu_types[i]; i++) { 799 error_printf(", %s", machine_class->valid_cpu_types[i]); 800 } 801 error_printf("\n"); 802 803 exit(1); 804 } 805 } 806 807 machine_class->init(machine); 808 } 809 810 static void machine_class_finalize(ObjectClass *klass, void *data) 811 { 812 MachineClass *mc = MACHINE_CLASS(klass); 813 814 if (mc->compat_props) { 815 g_array_free(mc->compat_props, true); 816 } 817 g_free(mc->name); 818 } 819 820 void machine_register_compat_props(MachineState *machine) 821 { 822 MachineClass *mc = MACHINE_GET_CLASS(machine); 823 int i; 824 GlobalProperty *p; 825 826 if (!mc->compat_props) { 827 return; 828 } 829 830 for (i = 0; i < mc->compat_props->len; i++) { 831 p = g_array_index(mc->compat_props, GlobalProperty *, i); 832 /* Machine compat_props must never cause errors: */ 833 p->errp = &error_abort; 834 qdev_prop_register_global(p); 835 } 836 } 837 838 static const TypeInfo machine_info = { 839 .name = TYPE_MACHINE, 840 .parent = TYPE_OBJECT, 841 .abstract = true, 842 .class_size = sizeof(MachineClass), 843 .class_init = machine_class_init, 844 .class_base_init = machine_class_base_init, 845 .class_finalize = machine_class_finalize, 846 .instance_size = sizeof(MachineState), 847 .instance_init = machine_initfn, 848 .instance_finalize = machine_finalize, 849 }; 850 851 static void machine_register_types(void) 852 { 853 type_register_static(&machine_info); 854 } 855 856 type_init(machine_register_types) 857