1 /* 2 * virtio ccw machine 3 * 4 * Copyright 2012, 2020 IBM Corp. 5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 6 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> 7 * Janosch Frank <frankja@linux.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or (at 10 * your option) any later version. See the COPYING file in the top-level 11 * directory. 12 */ 13 14 #include "qemu/osdep.h" 15 #include "qapi/error.h" 16 #include "system/ram_addr.h" 17 #include "system/confidential-guest-support.h" 18 #include "hw/boards.h" 19 #include "hw/s390x/sclp.h" 20 #include "hw/s390x/s390_flic.h" 21 #include "virtio-ccw.h" 22 #include "qemu/config-file.h" 23 #include "qemu/ctype.h" 24 #include "qemu/error-report.h" 25 #include "qemu/option.h" 26 #include "qemu/qemu-print.h" 27 #include "qemu/units.h" 28 #include "hw/s390x/s390-pci-bus.h" 29 #include "system/reset.h" 30 #include "hw/s390x/storage-keys.h" 31 #include "hw/s390x/storage-attributes.h" 32 #include "hw/s390x/event-facility.h" 33 #include "ipl.h" 34 #include "hw/s390x/s390-virtio-ccw.h" 35 #include "hw/s390x/css-bridge.h" 36 #include "hw/s390x/ap-bridge.h" 37 #include "migration/register.h" 38 #include "target/s390x/cpu_models.h" 39 #include "hw/nmi.h" 40 #include "hw/qdev-properties.h" 41 #include "hw/s390x/tod.h" 42 #include "system/system.h" 43 #include "system/cpus.h" 44 #include "system/hostmem.h" 45 #include "target/s390x/kvm/pv.h" 46 #include "migration/blocker.h" 47 #include "qapi/visitor.h" 48 #include "hw/s390x/cpu-topology.h" 49 #include "kvm/kvm_s390x.h" 50 #include "hw/virtio/virtio-md-pci.h" 51 #include "hw/s390x/virtio-ccw-md.h" 52 #include "system/replay.h" 53 #include CONFIG_DEVICES 54 55 static Error *pv_mig_blocker; 56 57 static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id, 58 Error **errp) 59 { 60 S390CPU *cpu = S390_CPU(object_new(typename)); 61 S390CPU *ret = NULL; 62 63 if (!object_property_set_int(OBJECT(cpu), "core-id", core_id, errp)) { 64 goto out; 65 } 66 if (!qdev_realize(DEVICE(cpu), NULL, errp)) { 67 goto out; 68 } 69 ret = cpu; 70 71 out: 72 object_unref(OBJECT(cpu)); 73 return ret; 74 } 75 76 static void s390_init_cpus(MachineState *machine) 77 { 78 MachineClass *mc = MACHINE_GET_CLASS(machine); 79 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 80 int i; 81 82 if (machine->smp.threads > s390mc->max_threads) { 83 error_report("S390 does not support more than %d threads.", 84 s390mc->max_threads); 85 exit(1); 86 } 87 88 /* initialize possible_cpus */ 89 mc->possible_cpu_arch_ids(machine); 90 91 for (i = 0; i < machine->smp.cpus; i++) { 92 s390x_new_cpu(machine->cpu_type, i, &error_fatal); 93 } 94 } 95 96 static const char *const reset_dev_types[] = { 97 TYPE_VIRTUAL_CSS_BRIDGE, 98 "s390-sclp-event-facility", 99 "s390-flic", 100 "diag288", 101 TYPE_S390_PCI_HOST_BRIDGE, 102 TYPE_AP_BRIDGE, 103 }; 104 105 static void subsystem_reset(void) 106 { 107 DeviceState *dev; 108 int i; 109 110 /* 111 * ISM firmware is sensitive to unexpected changes to the IOMMU, which can 112 * occur during reset of the vfio-pci device (unmap of entire aperture). 113 * Ensure any passthrough ISM devices are reset now, while CPUs are paused 114 * but before vfio-pci cleanup occurs. 115 */ 116 s390_pci_ism_reset(); 117 118 for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) { 119 dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL)); 120 if (dev) { 121 device_cold_reset(dev); 122 } 123 } 124 if (s390_has_topology()) { 125 s390_topology_reset(); 126 } 127 } 128 129 static void s390_set_memory_limit(S390CcwMachineState *s390ms, 130 uint64_t new_limit) 131 { 132 uint64_t hw_limit = 0; 133 int ret = 0; 134 135 assert(!s390ms->memory_limit && new_limit); 136 if (kvm_enabled()) { 137 ret = kvm_s390_set_mem_limit(new_limit, &hw_limit); 138 } 139 if (ret == -E2BIG) { 140 error_report("host supports a maximum of %" PRIu64 " GB", 141 hw_limit / GiB); 142 exit(EXIT_FAILURE); 143 } else if (ret) { 144 error_report("setting the guest size failed"); 145 exit(EXIT_FAILURE); 146 } 147 s390ms->memory_limit = new_limit; 148 } 149 150 static void s390_set_max_pagesize(S390CcwMachineState *s390ms, 151 uint64_t pagesize) 152 { 153 assert(!s390ms->max_pagesize && pagesize); 154 if (kvm_enabled()) { 155 kvm_s390_set_max_pagesize(pagesize, &error_fatal); 156 } 157 s390ms->max_pagesize = pagesize; 158 } 159 160 static void s390_memory_init(MachineState *machine) 161 { 162 S390CcwMachineState *s390ms = S390_CCW_MACHINE(machine); 163 MemoryRegion *sysmem = get_system_memory(); 164 MemoryRegion *ram = machine->ram; 165 uint64_t ram_size = memory_region_size(ram); 166 uint64_t devmem_base, devmem_size; 167 168 if (!QEMU_IS_ALIGNED(ram_size, 1 * MiB)) { 169 /* 170 * SCLP cannot possibly expose smaller granularity right now and KVM 171 * cannot handle smaller granularity. As we don't support NUMA, the 172 * region size directly corresponds to machine->ram_size, and the region 173 * is a single RAM memory region. 174 */ 175 error_report("ram size must be multiples of 1 MiB"); 176 exit(EXIT_FAILURE); 177 } 178 179 devmem_size = 0; 180 devmem_base = ram_size; 181 #ifdef CONFIG_MEM_DEVICE 182 if (machine->ram_size < machine->maxram_size) { 183 184 /* 185 * Make sure memory devices have a sane default alignment, even 186 * when weird initial memory sizes are specified. 187 */ 188 devmem_base = QEMU_ALIGN_UP(devmem_base, 1 * GiB); 189 devmem_size = machine->maxram_size - machine->ram_size; 190 } 191 #endif 192 s390_set_memory_limit(s390ms, devmem_base + devmem_size); 193 194 /* Map the initial memory. Must happen after setting the memory limit. */ 195 memory_region_add_subregion(sysmem, 0, ram); 196 197 /* Initialize address space for memory devices. */ 198 #ifdef CONFIG_MEM_DEVICE 199 if (devmem_size) { 200 machine_memory_devices_init(machine, devmem_base, devmem_size); 201 } 202 #endif /* CONFIG_MEM_DEVICE */ 203 204 /* 205 * Configure the maximum page size. As no memory devices were created 206 * yet, this is the page size of initial memory only. 207 */ 208 s390_set_max_pagesize(s390ms, qemu_maxrampagesize()); 209 /* Initialize storage key device */ 210 s390_skeys_init(); 211 /* Initialize storage attributes device */ 212 s390_stattrib_init(); 213 } 214 215 static void s390_init_ipl_dev(const char *kernel_filename, 216 const char *kernel_cmdline, 217 const char *initrd_filename, const char *firmware, 218 bool enforce_bios) 219 { 220 Object *new = object_new(TYPE_S390_IPL); 221 DeviceState *dev = DEVICE(new); 222 223 if (kernel_filename) { 224 qdev_prop_set_string(dev, "kernel", kernel_filename); 225 } 226 if (initrd_filename) { 227 qdev_prop_set_string(dev, "initrd", initrd_filename); 228 } 229 qdev_prop_set_string(dev, "cmdline", kernel_cmdline); 230 qdev_prop_set_string(dev, "firmware", firmware); 231 qdev_prop_set_bit(dev, "enforce_bios", enforce_bios); 232 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL, 233 new); 234 object_unref(new); 235 qdev_realize(dev, NULL, &error_fatal); 236 } 237 238 static void s390_create_virtio_net(BusState *bus, const char *name) 239 { 240 DeviceState *dev; 241 int cnt = 0; 242 243 while ((dev = qemu_create_nic_device(name, true, "virtio"))) { 244 g_autofree char *childname = g_strdup_printf("%s[%d]", name, cnt++); 245 object_property_add_child(OBJECT(bus), childname, OBJECT(dev)); 246 qdev_realize_and_unref(dev, bus, &error_fatal); 247 } 248 } 249 250 static void s390_create_sclpconsole(SCLPDevice *sclp, 251 const char *type, Chardev *chardev) 252 { 253 SCLPEventFacility *ef = sclp->event_facility; 254 BusState *ev_fac_bus = sclp_get_event_facility_bus(ef); 255 DeviceState *dev; 256 257 dev = qdev_new(type); 258 object_property_add_child(OBJECT(ef), type, OBJECT(dev)); 259 qdev_prop_set_chr(dev, "chardev", chardev); 260 qdev_realize_and_unref(dev, ev_fac_bus, &error_fatal); 261 } 262 263 static void ccw_init(MachineState *machine) 264 { 265 MachineClass *mc = MACHINE_GET_CLASS(machine); 266 S390CcwMachineState *ms = S390_CCW_MACHINE(machine); 267 int ret; 268 VirtualCssBus *css_bus; 269 DeviceState *dev; 270 271 ms->sclp = SCLP(object_new(TYPE_SCLP)); 272 object_property_add_child(OBJECT(machine), TYPE_SCLP, OBJECT(ms->sclp)); 273 qdev_realize_and_unref(DEVICE(ms->sclp), NULL, &error_fatal); 274 275 /* init memory + setup max page size. Required for the CPU model */ 276 s390_memory_init(machine); 277 278 /* init CPUs (incl. CPU model) early so s390_has_feature() works */ 279 s390_init_cpus(machine); 280 281 /* Need CPU model to be determined before we can set up PV */ 282 if (machine->cgs) { 283 confidential_guest_kvm_init(machine->cgs, &error_fatal); 284 } 285 286 s390_flic_init(); 287 288 /* init the SIGP facility */ 289 s390_init_sigp(); 290 291 /* create AP bridge and bus(es) */ 292 s390_init_ap(); 293 294 /* get a BUS */ 295 css_bus = virtual_css_bus_init(); 296 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline, 297 machine->initrd_filename, 298 machine->firmware ?: "s390-ccw.img", 299 true); 300 301 dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE); 302 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE, 303 OBJECT(dev)); 304 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 305 306 s390_enable_css_support(s390_cpu_addr2state(0)); 307 308 ret = css_create_css_image(VIRTUAL_CSSID, true); 309 assert(ret == 0); 310 311 css_register_vmstate(); 312 313 /* Create VirtIO network adapters */ 314 s390_create_virtio_net(BUS(css_bus), mc->default_nic); 315 316 /* init consoles */ 317 if (serial_hd(0)) { 318 s390_create_sclpconsole(ms->sclp, "sclpconsole", serial_hd(0)); 319 } 320 if (serial_hd(1)) { 321 s390_create_sclpconsole(ms->sclp, "sclplmconsole", serial_hd(1)); 322 } 323 324 /* init the TOD clock */ 325 s390_init_tod(); 326 } 327 328 static void s390_cpu_plug(HotplugHandler *hotplug_dev, 329 DeviceState *dev, Error **errp) 330 { 331 ERRP_GUARD(); 332 MachineState *ms = MACHINE(hotplug_dev); 333 S390CPU *cpu = S390_CPU(dev); 334 335 g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu); 336 ms->possible_cpus->cpus[cpu->env.core_id].cpu = CPU(dev); 337 338 if (s390_has_topology()) { 339 s390_topology_setup_cpu(ms, cpu, errp); 340 if (*errp) { 341 return; 342 } 343 } 344 345 if (dev->hotplugged) { 346 raise_irq_cpu_hotplug(); 347 } 348 } 349 350 static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg) 351 { 352 S390CPU *cpu = S390_CPU(cs); 353 354 s390_ipl_prepare_cpu(cpu); 355 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); 356 } 357 358 static void s390_machine_unprotect(S390CcwMachineState *ms) 359 { 360 if (!s390_pv_vm_try_disable_async(ms)) { 361 s390_pv_vm_disable(); 362 } 363 ms->pv = false; 364 migrate_del_blocker(&pv_mig_blocker); 365 ram_block_discard_disable(false); 366 } 367 368 static int s390_machine_protect(S390CcwMachineState *ms, 369 struct S390PVResponse *pv_resp) 370 { 371 Error *local_err = NULL; 372 int rc; 373 374 /* 375 * Discarding of memory in RAM blocks does not work as expected with 376 * protected VMs. Sharing and unsharing pages would be required. Disable 377 * it for now, until until we have a solution to make at least Linux 378 * guests either support it (e.g., virtio-balloon) or fail gracefully. 379 */ 380 rc = ram_block_discard_disable(true); 381 if (rc) { 382 error_report("protected VMs: cannot disable RAM discard"); 383 return rc; 384 } 385 386 error_setg(&pv_mig_blocker, 387 "protected VMs are currently not migratable."); 388 rc = migrate_add_blocker(&pv_mig_blocker, &local_err); 389 if (rc) { 390 ram_block_discard_disable(false); 391 error_report_err(local_err); 392 return rc; 393 } 394 395 /* Create SE VM */ 396 rc = s390_pv_vm_enable(); 397 if (rc) { 398 ram_block_discard_disable(false); 399 migrate_del_blocker(&pv_mig_blocker); 400 return rc; 401 } 402 403 ms->pv = true; 404 405 /* Will return 0 if API is not available since it's not vital */ 406 rc = s390_pv_query_info(); 407 if (rc) { 408 goto out_err; 409 } 410 411 /* Set SE header and unpack */ 412 rc = s390_ipl_prepare_pv_header(pv_resp, &local_err); 413 if (rc) { 414 goto out_err; 415 } 416 417 /* Decrypt image */ 418 rc = s390_ipl_pv_unpack(pv_resp); 419 if (rc) { 420 goto out_err; 421 } 422 423 /* Verify integrity */ 424 rc = s390_pv_verify(pv_resp); 425 if (rc) { 426 goto out_err; 427 } 428 return rc; 429 430 out_err: 431 if (local_err) { 432 error_report_err(local_err); 433 } 434 s390_machine_unprotect(ms); 435 return rc; 436 } 437 438 static void s390_pv_prepare_reset(S390CcwMachineState *ms) 439 { 440 CPUState *cs; 441 442 if (!s390_is_pv()) { 443 return; 444 } 445 /* Unsharing requires all cpus to be stopped */ 446 CPU_FOREACH(cs) { 447 s390_cpu_set_state(S390_CPU_STATE_STOPPED, S390_CPU(cs)); 448 } 449 s390_pv_unshare(); 450 s390_pv_prep_reset(); 451 } 452 453 static void s390_machine_reset(MachineState *machine, ResetType type) 454 { 455 S390CcwMachineState *ms = S390_CCW_MACHINE(machine); 456 struct S390PVResponse pv_resp; 457 enum s390_reset reset_type; 458 CPUState *cs, *t; 459 S390CPU *cpu; 460 461 /* 462 * Temporarily drop the record/replay mutex to let rr_cpu_thread_fn() 463 * process the run_on_cpu() requests below. This is safe, because at this 464 * point one of the following is true: 465 * - All CPU threads are not running, either because the machine is being 466 * initialized, or because the guest requested a reset using diag 308. 467 * There is no risk to desync the record/replay state. 468 * - A snapshot is about to be loaded. The record/replay state consistency 469 * is not important. 470 */ 471 replay_mutex_unlock(); 472 473 /* get the reset parameters, reset them once done */ 474 s390_ipl_get_reset_request(&cs, &reset_type); 475 476 /* all CPUs are paused and synchronized at this point */ 477 s390_cmma_reset(); 478 479 cpu = S390_CPU(cs); 480 481 switch (reset_type) { 482 case S390_RESET_EXTERNAL: 483 case S390_RESET_REIPL: 484 /* 485 * Reset the subsystem which includes a AP reset. If a PV 486 * guest had APQNs attached the AP reset is a prerequisite to 487 * unprotecting since the UV checks if all APQNs are reset. 488 */ 489 subsystem_reset(); 490 if (s390_is_pv()) { 491 s390_machine_unprotect(ms); 492 } 493 494 /* 495 * Device reset includes CPU clear resets so this has to be 496 * done AFTER the unprotect call above. 497 */ 498 qemu_devices_reset(type); 499 s390_crypto_reset(); 500 501 /* configure and start the ipl CPU only */ 502 run_on_cpu(cs, s390_do_cpu_ipl, RUN_ON_CPU_NULL); 503 break; 504 case S390_RESET_MODIFIED_CLEAR: 505 /* 506 * Subsystem reset needs to be done before we unshare memory 507 * and lose access to VIRTIO structures in guest memory. 508 */ 509 subsystem_reset(); 510 s390_crypto_reset(); 511 s390_pv_prepare_reset(ms); 512 CPU_FOREACH(t) { 513 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 514 } 515 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 516 break; 517 case S390_RESET_LOAD_NORMAL: 518 /* 519 * Subsystem reset needs to be done before we unshare memory 520 * and lose access to VIRTIO structures in guest memory. 521 */ 522 subsystem_reset(); 523 s390_pv_prepare_reset(ms); 524 CPU_FOREACH(t) { 525 if (t == cs) { 526 continue; 527 } 528 run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL); 529 } 530 run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL); 531 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 532 break; 533 case S390_RESET_PV: /* Subcode 10 */ 534 subsystem_reset(); 535 s390_crypto_reset(); 536 537 CPU_FOREACH(t) { 538 if (t == cs) { 539 continue; 540 } 541 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 542 } 543 run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL); 544 545 if (s390_machine_protect(ms, &pv_resp)) { 546 s390_pv_inject_reset_error(cs, pv_resp); 547 /* 548 * Continue after the diag308 so the guest knows something 549 * went wrong. 550 */ 551 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); 552 goto out_lock; 553 } 554 555 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 556 break; 557 default: 558 g_assert_not_reached(); 559 } 560 561 CPU_FOREACH(t) { 562 run_on_cpu(t, s390_do_cpu_set_diag318, RUN_ON_CPU_HOST_ULONG(0)); 563 } 564 s390_ipl_clear_reset_request(); 565 566 out_lock: 567 /* 568 * Re-take the record/replay mutex, temporarily dropping the BQL in order 569 * to satisfy the ordering requirements. 570 */ 571 bql_unlock(); 572 replay_mutex_lock(); 573 bql_lock(); 574 } 575 576 static void s390_machine_device_pre_plug(HotplugHandler *hotplug_dev, 577 DeviceState *dev, Error **errp) 578 { 579 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 580 virtio_ccw_md_pre_plug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp); 581 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 582 virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); 583 } 584 } 585 586 static void s390_machine_device_plug(HotplugHandler *hotplug_dev, 587 DeviceState *dev, Error **errp) 588 { 589 S390CcwMachineState *s390ms = S390_CCW_MACHINE(hotplug_dev); 590 591 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 592 s390_cpu_plug(hotplug_dev, dev, errp); 593 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW) || 594 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 595 /* 596 * At this point, the device is realized and set all memdevs mapped, so 597 * qemu_maxrampagesize() will pick up the page sizes of these memdevs 598 * as well. Before we plug the device and expose any RAM memory regions 599 * to the system, make sure we don't exceed the previously set max page 600 * size. While only relevant for KVM, there is not really any use case 601 * for this with TCG, so we'll unconditionally reject it. 602 */ 603 if (qemu_maxrampagesize() != s390ms->max_pagesize) { 604 error_setg(errp, "Memory device uses a bigger page size than" 605 " initial memory"); 606 return; 607 } 608 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 609 virtio_ccw_md_plug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp); 610 } else { 611 virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); 612 } 613 } 614 } 615 616 static void s390_machine_device_unplug_request(HotplugHandler *hotplug_dev, 617 DeviceState *dev, Error **errp) 618 { 619 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 620 error_setg(errp, "CPU hot unplug not supported on this machine"); 621 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 622 virtio_ccw_md_unplug_request(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), 623 errp); 624 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 625 virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), 626 errp); 627 } 628 } 629 630 static void s390_machine_device_unplug(HotplugHandler *hotplug_dev, 631 DeviceState *dev, Error **errp) 632 { 633 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 634 virtio_ccw_md_unplug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp); 635 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 636 virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); 637 } 638 } 639 640 static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms, 641 unsigned cpu_index) 642 { 643 MachineClass *mc = MACHINE_GET_CLASS(ms); 644 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 645 646 assert(cpu_index < possible_cpus->len); 647 return possible_cpus->cpus[cpu_index].props; 648 } 649 650 static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms) 651 { 652 int i; 653 unsigned int max_cpus = ms->smp.max_cpus; 654 655 if (ms->possible_cpus) { 656 g_assert(ms->possible_cpus && ms->possible_cpus->len == max_cpus); 657 return ms->possible_cpus; 658 } 659 660 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 661 sizeof(CPUArchId) * max_cpus); 662 ms->possible_cpus->len = max_cpus; 663 for (i = 0; i < ms->possible_cpus->len; i++) { 664 CpuInstanceProperties *props = &ms->possible_cpus->cpus[i].props; 665 666 ms->possible_cpus->cpus[i].type = ms->cpu_type; 667 ms->possible_cpus->cpus[i].vcpus_count = 1; 668 ms->possible_cpus->cpus[i].arch_id = i; 669 670 props->has_core_id = true; 671 props->core_id = i; 672 props->has_socket_id = true; 673 props->socket_id = s390_std_socket(i, &ms->smp); 674 props->has_book_id = true; 675 props->book_id = s390_std_book(i, &ms->smp); 676 props->has_drawer_id = true; 677 props->drawer_id = s390_std_drawer(i, &ms->smp); 678 } 679 680 return ms->possible_cpus; 681 } 682 683 static HotplugHandler *s390_get_hotplug_handler(MachineState *machine, 684 DeviceState *dev) 685 { 686 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU) || 687 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW) || 688 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 689 return HOTPLUG_HANDLER(machine); 690 } 691 return NULL; 692 } 693 694 static void s390_nmi(NMIState *n, int cpu_index, Error **errp) 695 { 696 CPUState *cs = qemu_get_cpu(cpu_index); 697 698 s390_cpu_restart(S390_CPU(cs)); 699 } 700 701 static ram_addr_t s390_fixup_ram_size(ram_addr_t sz) 702 { 703 /* same logic as in sclp.c */ 704 int increment_size = 20; 705 ram_addr_t newsz; 706 707 while ((sz >> increment_size) > MAX_STORAGE_INCREMENTS) { 708 increment_size++; 709 } 710 newsz = sz >> increment_size << increment_size; 711 712 if (sz != newsz) { 713 qemu_printf("Ram size %" PRIu64 "MB was fixed up to %" PRIu64 714 "MB to match machine restrictions. Consider updating " 715 "the guest definition.\n", (uint64_t) (sz / MiB), 716 (uint64_t) (newsz / MiB)); 717 } 718 return newsz; 719 } 720 721 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp) 722 { 723 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 724 725 return ms->aes_key_wrap; 726 } 727 728 static inline void machine_set_aes_key_wrap(Object *obj, bool value, 729 Error **errp) 730 { 731 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 732 733 ms->aes_key_wrap = value; 734 } 735 736 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp) 737 { 738 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 739 740 return ms->dea_key_wrap; 741 } 742 743 static inline void machine_set_dea_key_wrap(Object *obj, bool value, 744 Error **errp) 745 { 746 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 747 748 ms->dea_key_wrap = value; 749 } 750 751 static void machine_get_loadparm(Object *obj, Visitor *v, 752 const char *name, void *opaque, 753 Error **errp) 754 { 755 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 756 char *str = g_strndup((char *) ms->loadparm, sizeof(ms->loadparm)); 757 758 visit_type_str(v, name, &str, errp); 759 g_free(str); 760 } 761 762 static void machine_set_loadparm(Object *obj, Visitor *v, 763 const char *name, void *opaque, 764 Error **errp) 765 { 766 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 767 char *val; 768 769 if (!visit_type_str(v, name, &val, errp)) { 770 return; 771 } 772 773 s390_ipl_fmt_loadparm(ms->loadparm, val, errp); 774 g_free(val); 775 } 776 777 static void ccw_machine_class_init(ObjectClass *oc, const void *data) 778 { 779 MachineClass *mc = MACHINE_CLASS(oc); 780 NMIClass *nc = NMI_CLASS(oc); 781 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 782 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 783 DumpSKeysInterface *dsi = DUMP_SKEYS_INTERFACE_CLASS(oc); 784 785 s390mc->max_threads = 1; 786 mc->reset = s390_machine_reset; 787 mc->block_default_type = IF_VIRTIO; 788 mc->no_cdrom = 1; 789 mc->no_floppy = 1; 790 mc->no_parallel = 1; 791 mc->max_cpus = S390_MAX_CPUS; 792 mc->has_hotpluggable_cpus = true; 793 mc->smp_props.books_supported = true; 794 mc->smp_props.drawers_supported = true; 795 assert(!mc->get_hotplug_handler); 796 mc->get_hotplug_handler = s390_get_hotplug_handler; 797 mc->cpu_index_to_instance_props = s390_cpu_index_to_props; 798 mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids; 799 /* it is overridden with 'host' cpu *in kvm_arch_init* */ 800 mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu"); 801 hc->pre_plug = s390_machine_device_pre_plug; 802 hc->plug = s390_machine_device_plug; 803 hc->unplug_request = s390_machine_device_unplug_request; 804 hc->unplug = s390_machine_device_unplug; 805 nc->nmi_monitor_handler = s390_nmi; 806 mc->default_ram_id = "s390.ram"; 807 mc->default_nic = "virtio-net-ccw"; 808 dsi->qmp_dump_skeys = s390_qmp_dump_skeys; 809 810 object_class_property_add_bool(oc, "aes-key-wrap", 811 machine_get_aes_key_wrap, 812 machine_set_aes_key_wrap); 813 object_class_property_set_description(oc, "aes-key-wrap", 814 "enable/disable AES key wrapping using the CPACF wrapping key"); 815 816 object_class_property_add_bool(oc, "dea-key-wrap", 817 machine_get_dea_key_wrap, 818 machine_set_dea_key_wrap); 819 object_class_property_set_description(oc, "dea-key-wrap", 820 "enable/disable DEA key wrapping using the CPACF wrapping key"); 821 822 object_class_property_add(oc, "loadparm", "loadparm", 823 machine_get_loadparm, machine_set_loadparm, 824 NULL, NULL); 825 object_class_property_set_description(oc, "loadparm", 826 "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted" 827 " to upper case) to pass to machine loader, boot manager," 828 " and guest kernel"); 829 } 830 831 static inline void s390_machine_initfn(Object *obj) 832 { 833 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 834 835 ms->aes_key_wrap = true; 836 ms->dea_key_wrap = true; 837 } 838 839 static const TypeInfo ccw_machine_info = { 840 .name = TYPE_S390_CCW_MACHINE, 841 .parent = TYPE_MACHINE, 842 .abstract = true, 843 .instance_size = sizeof(S390CcwMachineState), 844 .instance_init = s390_machine_initfn, 845 .class_size = sizeof(S390CcwMachineClass), 846 .class_init = ccw_machine_class_init, 847 .interfaces = (const InterfaceInfo[]) { 848 { TYPE_NMI }, 849 { TYPE_HOTPLUG_HANDLER}, 850 { TYPE_DUMP_SKEYS_INTERFACE}, 851 { } 852 }, 853 }; 854 855 #define DEFINE_CCW_MACHINE_IMPL(latest, ...) \ 856 static void MACHINE_VER_SYM(mach_init, ccw, __VA_ARGS__)(MachineState *mach) \ 857 { \ 858 MACHINE_VER_SYM(instance_options, ccw, __VA_ARGS__)(mach); \ 859 ccw_init(mach); \ 860 } \ 861 static void MACHINE_VER_SYM(class_init, ccw, __VA_ARGS__)( \ 862 ObjectClass *oc, \ 863 const void *data) \ 864 { \ 865 MachineClass *mc = MACHINE_CLASS(oc); \ 866 MACHINE_VER_SYM(class_options, ccw, __VA_ARGS__)(mc); \ 867 mc->desc = "Virtual s390x machine (version " MACHINE_VER_STR(__VA_ARGS__) ")"; \ 868 mc->init = MACHINE_VER_SYM(mach_init, ccw, __VA_ARGS__); \ 869 MACHINE_VER_DEPRECATION(__VA_ARGS__); \ 870 if (latest) { \ 871 mc->alias = "s390-ccw-virtio"; \ 872 mc->is_default = true; \ 873 } \ 874 } \ 875 static const TypeInfo MACHINE_VER_SYM(info, ccw, __VA_ARGS__) = \ 876 { \ 877 .name = MACHINE_VER_TYPE_NAME("s390-ccw-virtio", __VA_ARGS__), \ 878 .parent = TYPE_S390_CCW_MACHINE, \ 879 .class_init = MACHINE_VER_SYM(class_init, ccw, __VA_ARGS__), \ 880 }; \ 881 static void MACHINE_VER_SYM(register, ccw, __VA_ARGS__)(void) \ 882 { \ 883 MACHINE_VER_DELETION(__VA_ARGS__); \ 884 type_register_static(&MACHINE_VER_SYM(info, ccw, __VA_ARGS__)); \ 885 } \ 886 type_init(MACHINE_VER_SYM(register, ccw, __VA_ARGS__)) 887 888 #define DEFINE_CCW_MACHINE_AS_LATEST(major, minor) \ 889 DEFINE_CCW_MACHINE_IMPL(true, major, minor) 890 891 #define DEFINE_CCW_MACHINE(major, minor) \ 892 DEFINE_CCW_MACHINE_IMPL(false, major, minor) 893 894 895 static void ccw_machine_10_1_instance_options(MachineState *machine) 896 { 897 } 898 899 static void ccw_machine_10_1_class_options(MachineClass *mc) 900 { 901 } 902 DEFINE_CCW_MACHINE_AS_LATEST(10, 1); 903 904 static void ccw_machine_10_0_instance_options(MachineState *machine) 905 { 906 ccw_machine_10_1_instance_options(machine); 907 } 908 909 static void ccw_machine_10_0_class_options(MachineClass *mc) 910 { 911 ccw_machine_10_1_class_options(mc); 912 compat_props_add(mc->compat_props, hw_compat_10_0, hw_compat_10_0_len); 913 } 914 DEFINE_CCW_MACHINE(10, 0); 915 916 static void ccw_machine_9_2_instance_options(MachineState *machine) 917 { 918 ccw_machine_10_0_instance_options(machine); 919 } 920 921 static void ccw_machine_9_2_class_options(MachineClass *mc) 922 { 923 static GlobalProperty compat[] = { 924 { TYPE_S390_PCI_DEVICE, "relaxed-translation", "off", }, 925 }; 926 927 ccw_machine_10_0_class_options(mc); 928 compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len); 929 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 930 } 931 DEFINE_CCW_MACHINE(9, 2); 932 933 static void ccw_machine_9_1_instance_options(MachineState *machine) 934 { 935 ccw_machine_9_2_instance_options(machine); 936 } 937 938 static void ccw_machine_9_1_class_options(MachineClass *mc) 939 { 940 ccw_machine_9_2_class_options(mc); 941 compat_props_add(mc->compat_props, hw_compat_9_1, hw_compat_9_1_len); 942 } 943 DEFINE_CCW_MACHINE(9, 1); 944 945 static void ccw_machine_9_0_instance_options(MachineState *machine) 946 { 947 ccw_machine_9_1_instance_options(machine); 948 } 949 950 static void ccw_machine_9_0_class_options(MachineClass *mc) 951 { 952 static GlobalProperty compat[] = { 953 { TYPE_QEMU_S390_FLIC, "migrate-all-state", "off", }, 954 }; 955 956 ccw_machine_9_1_class_options(mc); 957 compat_props_add(mc->compat_props, hw_compat_9_0, hw_compat_9_0_len); 958 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 959 } 960 DEFINE_CCW_MACHINE(9, 0); 961 962 static void ccw_machine_8_2_instance_options(MachineState *machine) 963 { 964 ccw_machine_9_0_instance_options(machine); 965 } 966 967 static void ccw_machine_8_2_class_options(MachineClass *mc) 968 { 969 ccw_machine_9_0_class_options(mc); 970 compat_props_add(mc->compat_props, hw_compat_8_2, hw_compat_8_2_len); 971 } 972 DEFINE_CCW_MACHINE(8, 2); 973 974 static void ccw_machine_8_1_instance_options(MachineState *machine) 975 { 976 ccw_machine_8_2_instance_options(machine); 977 } 978 979 static void ccw_machine_8_1_class_options(MachineClass *mc) 980 { 981 ccw_machine_8_2_class_options(mc); 982 compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len); 983 mc->smp_props.drawers_supported = false; 984 mc->smp_props.books_supported = false; 985 } 986 DEFINE_CCW_MACHINE(8, 1); 987 988 static void ccw_machine_8_0_instance_options(MachineState *machine) 989 { 990 ccw_machine_8_1_instance_options(machine); 991 } 992 993 static void ccw_machine_8_0_class_options(MachineClass *mc) 994 { 995 ccw_machine_8_1_class_options(mc); 996 compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len); 997 } 998 DEFINE_CCW_MACHINE(8, 0); 999 1000 static void ccw_machine_7_2_instance_options(MachineState *machine) 1001 { 1002 ccw_machine_8_0_instance_options(machine); 1003 } 1004 1005 static void ccw_machine_7_2_class_options(MachineClass *mc) 1006 { 1007 ccw_machine_8_0_class_options(mc); 1008 compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len); 1009 } 1010 DEFINE_CCW_MACHINE(7, 2); 1011 1012 static void ccw_machine_7_1_instance_options(MachineState *machine) 1013 { 1014 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_1 }; 1015 1016 ccw_machine_7_2_instance_options(machine); 1017 s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAIE); 1018 s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat); 1019 } 1020 1021 static void ccw_machine_7_1_class_options(MachineClass *mc) 1022 { 1023 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 1024 static GlobalProperty compat[] = { 1025 { TYPE_S390_PCI_DEVICE, "interpret", "off", }, 1026 { TYPE_S390_PCI_DEVICE, "forwarding-assist", "off", }, 1027 }; 1028 1029 ccw_machine_7_2_class_options(mc); 1030 compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len); 1031 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1032 s390mc->max_threads = S390_MAX_CPUS; 1033 } 1034 DEFINE_CCW_MACHINE(7, 1); 1035 1036 static void ccw_machine_7_0_instance_options(MachineState *machine) 1037 { 1038 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_0 }; 1039 1040 ccw_machine_7_1_instance_options(machine); 1041 s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat); 1042 } 1043 1044 static void ccw_machine_7_0_class_options(MachineClass *mc) 1045 { 1046 ccw_machine_7_1_class_options(mc); 1047 compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len); 1048 } 1049 DEFINE_CCW_MACHINE(7, 0); 1050 1051 static void ccw_machine_6_2_instance_options(MachineState *machine) 1052 { 1053 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_2 }; 1054 1055 ccw_machine_7_0_instance_options(machine); 1056 s390_set_qemu_cpu_model(0x3906, 14, 2, qemu_cpu_feat); 1057 } 1058 1059 static void ccw_machine_6_2_class_options(MachineClass *mc) 1060 { 1061 ccw_machine_7_0_class_options(mc); 1062 compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len); 1063 } 1064 DEFINE_CCW_MACHINE(6, 2); 1065 1066 static void ccw_machine_6_1_instance_options(MachineState *machine) 1067 { 1068 ccw_machine_6_2_instance_options(machine); 1069 s390_cpudef_featoff_greater(16, 1, S390_FEAT_NNPA); 1070 s390_cpudef_featoff_greater(16, 1, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2); 1071 s390_cpudef_featoff_greater(16, 1, S390_FEAT_BEAR_ENH); 1072 s390_cpudef_featoff_greater(16, 1, S390_FEAT_RDP); 1073 s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAI); 1074 } 1075 1076 static void ccw_machine_6_1_class_options(MachineClass *mc) 1077 { 1078 ccw_machine_6_2_class_options(mc); 1079 compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len); 1080 mc->smp_props.prefer_sockets = true; 1081 } 1082 DEFINE_CCW_MACHINE(6, 1); 1083 1084 static void ccw_machine_6_0_instance_options(MachineState *machine) 1085 { 1086 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_0 }; 1087 1088 ccw_machine_6_1_instance_options(machine); 1089 s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat); 1090 } 1091 1092 static void ccw_machine_6_0_class_options(MachineClass *mc) 1093 { 1094 ccw_machine_6_1_class_options(mc); 1095 compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len); 1096 } 1097 DEFINE_CCW_MACHINE(6, 0); 1098 1099 static void ccw_machine_5_2_instance_options(MachineState *machine) 1100 { 1101 ccw_machine_6_0_instance_options(machine); 1102 } 1103 1104 static void ccw_machine_5_2_class_options(MachineClass *mc) 1105 { 1106 ccw_machine_6_0_class_options(mc); 1107 compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len); 1108 } 1109 DEFINE_CCW_MACHINE(5, 2); 1110 1111 static void ccw_machine_5_1_instance_options(MachineState *machine) 1112 { 1113 ccw_machine_5_2_instance_options(machine); 1114 } 1115 1116 static void ccw_machine_5_1_class_options(MachineClass *mc) 1117 { 1118 ccw_machine_5_2_class_options(mc); 1119 compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len); 1120 } 1121 DEFINE_CCW_MACHINE(5, 1); 1122 1123 static void ccw_machine_5_0_instance_options(MachineState *machine) 1124 { 1125 ccw_machine_5_1_instance_options(machine); 1126 } 1127 1128 static void ccw_machine_5_0_class_options(MachineClass *mc) 1129 { 1130 ccw_machine_5_1_class_options(mc); 1131 compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len); 1132 } 1133 DEFINE_CCW_MACHINE(5, 0); 1134 1135 static void ccw_machine_4_2_instance_options(MachineState *machine) 1136 { 1137 ccw_machine_5_0_instance_options(machine); 1138 } 1139 1140 static void ccw_machine_4_2_class_options(MachineClass *mc) 1141 { 1142 ccw_machine_5_0_class_options(mc); 1143 mc->fixup_ram_size = s390_fixup_ram_size; 1144 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len); 1145 } 1146 DEFINE_CCW_MACHINE(4, 2); 1147 1148 static void ccw_machine_register_types(void) 1149 { 1150 type_register_static(&ccw_machine_info); 1151 } 1152 1153 type_init(ccw_machine_register_types) 1154