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 s390_create_sclpcpi(SCLPDevice *sclp) 264 { 265 SCLPEventFacility *ef = sclp->event_facility; 266 BusState *ev_fac_bus = sclp_get_event_facility_bus(ef); 267 DeviceState *dev; 268 269 dev = qdev_new(TYPE_SCLP_EVENT_CPI); 270 object_property_add_child(OBJECT(ef), "sclpcpi", OBJECT(dev)); 271 qdev_realize_and_unref(dev, ev_fac_bus, &error_fatal); 272 } 273 274 static void ccw_init(MachineState *machine) 275 { 276 MachineClass *mc = MACHINE_GET_CLASS(machine); 277 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 278 S390CcwMachineState *ms = S390_CCW_MACHINE(machine); 279 int ret; 280 VirtualCssBus *css_bus; 281 DeviceState *dev; 282 283 ms->sclp = SCLP(object_new(TYPE_SCLP)); 284 object_property_add_child(OBJECT(machine), TYPE_SCLP, OBJECT(ms->sclp)); 285 qdev_realize_and_unref(DEVICE(ms->sclp), NULL, &error_fatal); 286 287 /* init memory + setup max page size. Required for the CPU model */ 288 s390_memory_init(machine); 289 290 /* init CPUs (incl. CPU model) early so s390_has_feature() works */ 291 s390_init_cpus(machine); 292 293 /* Need CPU model to be determined before we can set up PV */ 294 if (machine->cgs) { 295 confidential_guest_kvm_init(machine->cgs, &error_fatal); 296 } 297 298 s390_flic_init(); 299 300 /* init the SIGP facility */ 301 s390_init_sigp(); 302 303 /* create AP bridge and bus(es) */ 304 s390_init_ap(); 305 306 /* get a BUS */ 307 css_bus = virtual_css_bus_init(); 308 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline, 309 machine->initrd_filename, 310 machine->firmware ?: "s390-ccw.img", 311 true); 312 313 dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE); 314 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE, 315 OBJECT(dev)); 316 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 317 318 s390_enable_css_support(s390_cpu_addr2state(0)); 319 320 ret = css_create_css_image(VIRTUAL_CSSID, true); 321 assert(ret == 0); 322 323 css_register_vmstate(); 324 325 /* Create VirtIO network adapters */ 326 s390_create_virtio_net(BUS(css_bus), mc->default_nic); 327 328 /* init consoles */ 329 if (serial_hd(0)) { 330 s390_create_sclpconsole(ms->sclp, "sclpconsole", serial_hd(0)); 331 } 332 if (serial_hd(1)) { 333 s390_create_sclpconsole(ms->sclp, "sclplmconsole", serial_hd(1)); 334 } 335 336 /* init the TOD clock */ 337 s390_init_tod(); 338 339 /* init SCLP event Control-Program Identification */ 340 if (s390mc->use_cpi) { 341 s390_create_sclpcpi(ms->sclp); 342 } 343 344 } 345 346 static void s390_cpu_plug(HotplugHandler *hotplug_dev, 347 DeviceState *dev, Error **errp) 348 { 349 ERRP_GUARD(); 350 MachineState *ms = MACHINE(hotplug_dev); 351 S390CPU *cpu = S390_CPU(dev); 352 353 g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu); 354 ms->possible_cpus->cpus[cpu->env.core_id].cpu = CPU(dev); 355 356 if (s390_has_topology()) { 357 s390_topology_setup_cpu(ms, cpu, errp); 358 if (*errp) { 359 return; 360 } 361 } 362 363 if (dev->hotplugged) { 364 raise_irq_cpu_hotplug(); 365 } 366 } 367 368 static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg) 369 { 370 S390CPU *cpu = S390_CPU(cs); 371 372 s390_ipl_prepare_cpu(cpu); 373 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); 374 } 375 376 static void s390_machine_unprotect(S390CcwMachineState *ms) 377 { 378 if (!s390_pv_vm_try_disable_async(ms)) { 379 s390_pv_vm_disable(); 380 } 381 ms->pv = false; 382 migrate_del_blocker(&pv_mig_blocker); 383 ram_block_discard_disable(false); 384 } 385 386 static int s390_machine_protect(S390CcwMachineState *ms, 387 struct S390PVResponse *pv_resp) 388 { 389 Error *local_err = NULL; 390 int rc; 391 392 /* 393 * Discarding of memory in RAM blocks does not work as expected with 394 * protected VMs. Sharing and unsharing pages would be required. Disable 395 * it for now, until until we have a solution to make at least Linux 396 * guests either support it (e.g., virtio-balloon) or fail gracefully. 397 */ 398 rc = ram_block_discard_disable(true); 399 if (rc) { 400 error_report("protected VMs: cannot disable RAM discard"); 401 return rc; 402 } 403 404 error_setg(&pv_mig_blocker, 405 "protected VMs are currently not migratable."); 406 rc = migrate_add_blocker(&pv_mig_blocker, &local_err); 407 if (rc) { 408 ram_block_discard_disable(false); 409 error_report_err(local_err); 410 return rc; 411 } 412 413 /* Create SE VM */ 414 rc = s390_pv_vm_enable(); 415 if (rc) { 416 ram_block_discard_disable(false); 417 migrate_del_blocker(&pv_mig_blocker); 418 return rc; 419 } 420 421 ms->pv = true; 422 423 /* Will return 0 if API is not available since it's not vital */ 424 rc = s390_pv_query_info(); 425 if (rc) { 426 goto out_err; 427 } 428 429 /* Set SE header and unpack */ 430 rc = s390_ipl_prepare_pv_header(pv_resp, &local_err); 431 if (rc) { 432 goto out_err; 433 } 434 435 /* Decrypt image */ 436 rc = s390_ipl_pv_unpack(pv_resp); 437 if (rc) { 438 goto out_err; 439 } 440 441 /* Verify integrity */ 442 rc = s390_pv_verify(pv_resp); 443 if (rc) { 444 goto out_err; 445 } 446 return rc; 447 448 out_err: 449 if (local_err) { 450 error_report_err(local_err); 451 } 452 s390_machine_unprotect(ms); 453 return rc; 454 } 455 456 static void s390_pv_prepare_reset(S390CcwMachineState *ms) 457 { 458 CPUState *cs; 459 460 if (!s390_is_pv()) { 461 return; 462 } 463 /* Unsharing requires all cpus to be stopped */ 464 CPU_FOREACH(cs) { 465 s390_cpu_set_state(S390_CPU_STATE_STOPPED, S390_CPU(cs)); 466 } 467 s390_pv_unshare(); 468 s390_pv_prep_reset(); 469 } 470 471 static void s390_machine_reset(MachineState *machine, ResetType type) 472 { 473 S390CcwMachineState *ms = S390_CCW_MACHINE(machine); 474 struct S390PVResponse pv_resp; 475 enum s390_reset reset_type; 476 CPUState *cs, *t; 477 S390CPU *cpu; 478 479 /* 480 * Temporarily drop the record/replay mutex to let rr_cpu_thread_fn() 481 * process the run_on_cpu() requests below. This is safe, because at this 482 * point one of the following is true: 483 * - All CPU threads are not running, either because the machine is being 484 * initialized, or because the guest requested a reset using diag 308. 485 * There is no risk to desync the record/replay state. 486 * - A snapshot is about to be loaded. The record/replay state consistency 487 * is not important. 488 */ 489 replay_mutex_unlock(); 490 491 /* get the reset parameters, reset them once done */ 492 s390_ipl_get_reset_request(&cs, &reset_type); 493 494 /* all CPUs are paused and synchronized at this point */ 495 s390_cmma_reset(); 496 497 cpu = S390_CPU(cs); 498 499 switch (reset_type) { 500 case S390_RESET_EXTERNAL: 501 case S390_RESET_REIPL: 502 /* 503 * Reset the subsystem which includes a AP reset. If a PV 504 * guest had APQNs attached the AP reset is a prerequisite to 505 * unprotecting since the UV checks if all APQNs are reset. 506 */ 507 subsystem_reset(); 508 if (s390_is_pv()) { 509 s390_machine_unprotect(ms); 510 } 511 512 /* 513 * Device reset includes CPU clear resets so this has to be 514 * done AFTER the unprotect call above. 515 */ 516 qemu_devices_reset(type); 517 s390_crypto_reset(); 518 519 /* configure and start the ipl CPU only */ 520 run_on_cpu(cs, s390_do_cpu_ipl, RUN_ON_CPU_NULL); 521 break; 522 case S390_RESET_MODIFIED_CLEAR: 523 /* 524 * Subsystem reset needs to be done before we unshare memory 525 * and lose access to VIRTIO structures in guest memory. 526 */ 527 subsystem_reset(); 528 s390_crypto_reset(); 529 s390_pv_prepare_reset(ms); 530 CPU_FOREACH(t) { 531 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 532 } 533 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 534 break; 535 case S390_RESET_LOAD_NORMAL: 536 /* 537 * Subsystem reset needs to be done before we unshare memory 538 * and lose access to VIRTIO structures in guest memory. 539 */ 540 subsystem_reset(); 541 s390_pv_prepare_reset(ms); 542 CPU_FOREACH(t) { 543 if (t == cs) { 544 continue; 545 } 546 run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL); 547 } 548 run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL); 549 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 550 break; 551 case S390_RESET_PV: /* Subcode 10 */ 552 subsystem_reset(); 553 s390_crypto_reset(); 554 555 CPU_FOREACH(t) { 556 if (t == cs) { 557 continue; 558 } 559 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 560 } 561 run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL); 562 563 if (s390_machine_protect(ms, &pv_resp)) { 564 s390_pv_inject_reset_error(cs, pv_resp); 565 /* 566 * Continue after the diag308 so the guest knows something 567 * went wrong. 568 */ 569 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); 570 goto out_lock; 571 } 572 573 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 574 break; 575 default: 576 g_assert_not_reached(); 577 } 578 579 CPU_FOREACH(t) { 580 run_on_cpu(t, s390_do_cpu_set_diag318, RUN_ON_CPU_HOST_ULONG(0)); 581 } 582 s390_ipl_clear_reset_request(); 583 584 out_lock: 585 /* 586 * Re-take the record/replay mutex, temporarily dropping the BQL in order 587 * to satisfy the ordering requirements. 588 */ 589 bql_unlock(); 590 replay_mutex_lock(); 591 bql_lock(); 592 } 593 594 static void s390_machine_device_pre_plug(HotplugHandler *hotplug_dev, 595 DeviceState *dev, Error **errp) 596 { 597 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 598 virtio_ccw_md_pre_plug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp); 599 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 600 virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); 601 } 602 } 603 604 static void s390_machine_device_plug(HotplugHandler *hotplug_dev, 605 DeviceState *dev, Error **errp) 606 { 607 S390CcwMachineState *s390ms = S390_CCW_MACHINE(hotplug_dev); 608 609 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 610 s390_cpu_plug(hotplug_dev, dev, errp); 611 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW) || 612 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 613 /* 614 * At this point, the device is realized and set all memdevs mapped, so 615 * qemu_maxrampagesize() will pick up the page sizes of these memdevs 616 * as well. Before we plug the device and expose any RAM memory regions 617 * to the system, make sure we don't exceed the previously set max page 618 * size. While only relevant for KVM, there is not really any use case 619 * for this with TCG, so we'll unconditionally reject it. 620 */ 621 if (qemu_maxrampagesize() != s390ms->max_pagesize) { 622 error_setg(errp, "Memory device uses a bigger page size than" 623 " initial memory"); 624 return; 625 } 626 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 627 virtio_ccw_md_plug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp); 628 } else { 629 virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); 630 } 631 } 632 } 633 634 static void s390_machine_device_unplug_request(HotplugHandler *hotplug_dev, 635 DeviceState *dev, Error **errp) 636 { 637 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 638 error_setg(errp, "CPU hot unplug not supported on this machine"); 639 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 640 virtio_ccw_md_unplug_request(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), 641 errp); 642 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 643 virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), 644 errp); 645 } 646 } 647 648 static void s390_machine_device_unplug(HotplugHandler *hotplug_dev, 649 DeviceState *dev, Error **errp) 650 { 651 if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW)) { 652 virtio_ccw_md_unplug(VIRTIO_MD_CCW(dev), MACHINE(hotplug_dev), errp); 653 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 654 virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp); 655 } 656 } 657 658 static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms, 659 unsigned cpu_index) 660 { 661 MachineClass *mc = MACHINE_GET_CLASS(ms); 662 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 663 664 assert(cpu_index < possible_cpus->len); 665 return possible_cpus->cpus[cpu_index].props; 666 } 667 668 static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms) 669 { 670 int i; 671 unsigned int max_cpus = ms->smp.max_cpus; 672 673 if (ms->possible_cpus) { 674 g_assert(ms->possible_cpus && ms->possible_cpus->len == max_cpus); 675 return ms->possible_cpus; 676 } 677 678 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 679 sizeof(CPUArchId) * max_cpus); 680 ms->possible_cpus->len = max_cpus; 681 for (i = 0; i < ms->possible_cpus->len; i++) { 682 CpuInstanceProperties *props = &ms->possible_cpus->cpus[i].props; 683 684 ms->possible_cpus->cpus[i].type = ms->cpu_type; 685 ms->possible_cpus->cpus[i].vcpus_count = 1; 686 ms->possible_cpus->cpus[i].arch_id = i; 687 688 props->has_core_id = true; 689 props->core_id = i; 690 props->has_socket_id = true; 691 props->socket_id = s390_std_socket(i, &ms->smp); 692 props->has_book_id = true; 693 props->book_id = s390_std_book(i, &ms->smp); 694 props->has_drawer_id = true; 695 props->drawer_id = s390_std_drawer(i, &ms->smp); 696 } 697 698 return ms->possible_cpus; 699 } 700 701 static HotplugHandler *s390_get_hotplug_handler(MachineState *machine, 702 DeviceState *dev) 703 { 704 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU) || 705 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_CCW) || 706 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) { 707 return HOTPLUG_HANDLER(machine); 708 } 709 return NULL; 710 } 711 712 static void s390_nmi(NMIState *n, int cpu_index, Error **errp) 713 { 714 CPUState *cs = qemu_get_cpu(cpu_index); 715 716 s390_cpu_restart(S390_CPU(cs)); 717 } 718 719 static ram_addr_t s390_fixup_ram_size(ram_addr_t sz) 720 { 721 /* same logic as in sclp.c */ 722 int increment_size = 20; 723 ram_addr_t newsz; 724 725 while ((sz >> increment_size) > MAX_STORAGE_INCREMENTS) { 726 increment_size++; 727 } 728 newsz = sz >> increment_size << increment_size; 729 730 if (sz != newsz) { 731 qemu_printf("Ram size %" PRIu64 "MB was fixed up to %" PRIu64 732 "MB to match machine restrictions. Consider updating " 733 "the guest definition.\n", (uint64_t) (sz / MiB), 734 (uint64_t) (newsz / MiB)); 735 } 736 return newsz; 737 } 738 739 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp) 740 { 741 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 742 743 return ms->aes_key_wrap; 744 } 745 746 static inline void machine_set_aes_key_wrap(Object *obj, bool value, 747 Error **errp) 748 { 749 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 750 751 ms->aes_key_wrap = value; 752 } 753 754 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp) 755 { 756 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 757 758 return ms->dea_key_wrap; 759 } 760 761 static inline void machine_set_dea_key_wrap(Object *obj, bool value, 762 Error **errp) 763 { 764 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 765 766 ms->dea_key_wrap = value; 767 } 768 769 static void machine_get_loadparm(Object *obj, Visitor *v, 770 const char *name, void *opaque, 771 Error **errp) 772 { 773 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 774 char *str = g_strndup((char *) ms->loadparm, sizeof(ms->loadparm)); 775 776 visit_type_str(v, name, &str, errp); 777 g_free(str); 778 } 779 780 static void machine_set_loadparm(Object *obj, Visitor *v, 781 const char *name, void *opaque, 782 Error **errp) 783 { 784 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 785 char *val; 786 787 if (!visit_type_str(v, name, &val, errp)) { 788 return; 789 } 790 791 s390_ipl_fmt_loadparm(ms->loadparm, val, errp); 792 g_free(val); 793 } 794 795 static void ccw_machine_class_init(ObjectClass *oc, const void *data) 796 { 797 MachineClass *mc = MACHINE_CLASS(oc); 798 NMIClass *nc = NMI_CLASS(oc); 799 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 800 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 801 DumpSKeysInterface *dsi = DUMP_SKEYS_INTERFACE_CLASS(oc); 802 803 s390mc->max_threads = 1; 804 s390mc->use_cpi = true; 805 mc->reset = s390_machine_reset; 806 mc->block_default_type = IF_VIRTIO; 807 mc->no_cdrom = 1; 808 mc->no_floppy = 1; 809 mc->no_parallel = 1; 810 mc->max_cpus = S390_MAX_CPUS; 811 mc->has_hotpluggable_cpus = true; 812 mc->smp_props.books_supported = true; 813 mc->smp_props.drawers_supported = true; 814 assert(!mc->get_hotplug_handler); 815 mc->get_hotplug_handler = s390_get_hotplug_handler; 816 mc->cpu_index_to_instance_props = s390_cpu_index_to_props; 817 mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids; 818 /* it is overridden with 'host' cpu *in kvm_arch_init* */ 819 mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu"); 820 hc->pre_plug = s390_machine_device_pre_plug; 821 hc->plug = s390_machine_device_plug; 822 hc->unplug_request = s390_machine_device_unplug_request; 823 hc->unplug = s390_machine_device_unplug; 824 nc->nmi_monitor_handler = s390_nmi; 825 mc->default_ram_id = "s390.ram"; 826 mc->default_nic = "virtio-net-ccw"; 827 dsi->qmp_dump_skeys = s390_qmp_dump_skeys; 828 829 object_class_property_add_bool(oc, "aes-key-wrap", 830 machine_get_aes_key_wrap, 831 machine_set_aes_key_wrap); 832 object_class_property_set_description(oc, "aes-key-wrap", 833 "enable/disable AES key wrapping using the CPACF wrapping key"); 834 835 object_class_property_add_bool(oc, "dea-key-wrap", 836 machine_get_dea_key_wrap, 837 machine_set_dea_key_wrap); 838 object_class_property_set_description(oc, "dea-key-wrap", 839 "enable/disable DEA key wrapping using the CPACF wrapping key"); 840 841 object_class_property_add(oc, "loadparm", "loadparm", 842 machine_get_loadparm, machine_set_loadparm, 843 NULL, NULL); 844 object_class_property_set_description(oc, "loadparm", 845 "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted" 846 " to upper case) to pass to machine loader, boot manager," 847 " and guest kernel"); 848 } 849 850 static inline void s390_machine_initfn(Object *obj) 851 { 852 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 853 854 ms->aes_key_wrap = true; 855 ms->dea_key_wrap = true; 856 } 857 858 static const TypeInfo ccw_machine_info = { 859 .name = TYPE_S390_CCW_MACHINE, 860 .parent = TYPE_MACHINE, 861 .abstract = true, 862 .instance_size = sizeof(S390CcwMachineState), 863 .instance_init = s390_machine_initfn, 864 .class_size = sizeof(S390CcwMachineClass), 865 .class_init = ccw_machine_class_init, 866 .interfaces = (const InterfaceInfo[]) { 867 { TYPE_NMI }, 868 { TYPE_HOTPLUG_HANDLER}, 869 { TYPE_DUMP_SKEYS_INTERFACE}, 870 { } 871 }, 872 }; 873 874 #define DEFINE_CCW_MACHINE_IMPL(latest, ...) \ 875 static void MACHINE_VER_SYM(mach_init, ccw, __VA_ARGS__)(MachineState *mach) \ 876 { \ 877 MACHINE_VER_SYM(instance_options, ccw, __VA_ARGS__)(mach); \ 878 ccw_init(mach); \ 879 } \ 880 static void MACHINE_VER_SYM(class_init, ccw, __VA_ARGS__)( \ 881 ObjectClass *oc, \ 882 const void *data) \ 883 { \ 884 MachineClass *mc = MACHINE_CLASS(oc); \ 885 MACHINE_VER_SYM(class_options, ccw, __VA_ARGS__)(mc); \ 886 mc->desc = "Virtual s390x machine (version " MACHINE_VER_STR(__VA_ARGS__) ")"; \ 887 mc->init = MACHINE_VER_SYM(mach_init, ccw, __VA_ARGS__); \ 888 MACHINE_VER_DEPRECATION(__VA_ARGS__); \ 889 if (latest) { \ 890 mc->alias = "s390-ccw-virtio"; \ 891 mc->is_default = true; \ 892 } \ 893 } \ 894 static const TypeInfo MACHINE_VER_SYM(info, ccw, __VA_ARGS__) = \ 895 { \ 896 .name = MACHINE_VER_TYPE_NAME("s390-ccw-virtio", __VA_ARGS__), \ 897 .parent = TYPE_S390_CCW_MACHINE, \ 898 .class_init = MACHINE_VER_SYM(class_init, ccw, __VA_ARGS__), \ 899 }; \ 900 static void MACHINE_VER_SYM(register, ccw, __VA_ARGS__)(void) \ 901 { \ 902 MACHINE_VER_DELETION(__VA_ARGS__); \ 903 type_register_static(&MACHINE_VER_SYM(info, ccw, __VA_ARGS__)); \ 904 } \ 905 type_init(MACHINE_VER_SYM(register, ccw, __VA_ARGS__)) 906 907 #define DEFINE_CCW_MACHINE_AS_LATEST(major, minor) \ 908 DEFINE_CCW_MACHINE_IMPL(true, major, minor) 909 910 #define DEFINE_CCW_MACHINE(major, minor) \ 911 DEFINE_CCW_MACHINE_IMPL(false, major, minor) 912 913 914 static void ccw_machine_10_1_instance_options(MachineState *machine) 915 { 916 } 917 918 static void ccw_machine_10_1_class_options(MachineClass *mc) 919 { 920 } 921 DEFINE_CCW_MACHINE_AS_LATEST(10, 1); 922 923 static void ccw_machine_10_0_instance_options(MachineState *machine) 924 { 925 ccw_machine_10_1_instance_options(machine); 926 } 927 928 static void ccw_machine_10_0_class_options(MachineClass *mc) 929 { 930 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 931 s390mc->use_cpi = false; 932 933 ccw_machine_10_1_class_options(mc); 934 compat_props_add(mc->compat_props, hw_compat_10_0, hw_compat_10_0_len); 935 } 936 DEFINE_CCW_MACHINE(10, 0); 937 938 static void ccw_machine_9_2_instance_options(MachineState *machine) 939 { 940 ccw_machine_10_0_instance_options(machine); 941 } 942 943 static void ccw_machine_9_2_class_options(MachineClass *mc) 944 { 945 static GlobalProperty compat[] = { 946 { TYPE_S390_PCI_DEVICE, "relaxed-translation", "off", }, 947 }; 948 949 ccw_machine_10_0_class_options(mc); 950 compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len); 951 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 952 } 953 DEFINE_CCW_MACHINE(9, 2); 954 955 static void ccw_machine_9_1_instance_options(MachineState *machine) 956 { 957 ccw_machine_9_2_instance_options(machine); 958 } 959 960 static void ccw_machine_9_1_class_options(MachineClass *mc) 961 { 962 ccw_machine_9_2_class_options(mc); 963 compat_props_add(mc->compat_props, hw_compat_9_1, hw_compat_9_1_len); 964 } 965 DEFINE_CCW_MACHINE(9, 1); 966 967 static void ccw_machine_9_0_instance_options(MachineState *machine) 968 { 969 ccw_machine_9_1_instance_options(machine); 970 } 971 972 static void ccw_machine_9_0_class_options(MachineClass *mc) 973 { 974 static GlobalProperty compat[] = { 975 { TYPE_QEMU_S390_FLIC, "migrate-all-state", "off", }, 976 }; 977 978 ccw_machine_9_1_class_options(mc); 979 compat_props_add(mc->compat_props, hw_compat_9_0, hw_compat_9_0_len); 980 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 981 } 982 DEFINE_CCW_MACHINE(9, 0); 983 984 static void ccw_machine_8_2_instance_options(MachineState *machine) 985 { 986 ccw_machine_9_0_instance_options(machine); 987 } 988 989 static void ccw_machine_8_2_class_options(MachineClass *mc) 990 { 991 ccw_machine_9_0_class_options(mc); 992 compat_props_add(mc->compat_props, hw_compat_8_2, hw_compat_8_2_len); 993 } 994 DEFINE_CCW_MACHINE(8, 2); 995 996 static void ccw_machine_8_1_instance_options(MachineState *machine) 997 { 998 ccw_machine_8_2_instance_options(machine); 999 } 1000 1001 static void ccw_machine_8_1_class_options(MachineClass *mc) 1002 { 1003 ccw_machine_8_2_class_options(mc); 1004 compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len); 1005 mc->smp_props.drawers_supported = false; 1006 mc->smp_props.books_supported = false; 1007 } 1008 DEFINE_CCW_MACHINE(8, 1); 1009 1010 static void ccw_machine_8_0_instance_options(MachineState *machine) 1011 { 1012 ccw_machine_8_1_instance_options(machine); 1013 } 1014 1015 static void ccw_machine_8_0_class_options(MachineClass *mc) 1016 { 1017 ccw_machine_8_1_class_options(mc); 1018 compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len); 1019 } 1020 DEFINE_CCW_MACHINE(8, 0); 1021 1022 static void ccw_machine_7_2_instance_options(MachineState *machine) 1023 { 1024 ccw_machine_8_0_instance_options(machine); 1025 } 1026 1027 static void ccw_machine_7_2_class_options(MachineClass *mc) 1028 { 1029 ccw_machine_8_0_class_options(mc); 1030 compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len); 1031 } 1032 DEFINE_CCW_MACHINE(7, 2); 1033 1034 static void ccw_machine_7_1_instance_options(MachineState *machine) 1035 { 1036 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_1 }; 1037 1038 ccw_machine_7_2_instance_options(machine); 1039 s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAIE); 1040 s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat); 1041 } 1042 1043 static void ccw_machine_7_1_class_options(MachineClass *mc) 1044 { 1045 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 1046 static GlobalProperty compat[] = { 1047 { TYPE_S390_PCI_DEVICE, "interpret", "off", }, 1048 { TYPE_S390_PCI_DEVICE, "forwarding-assist", "off", }, 1049 }; 1050 1051 ccw_machine_7_2_class_options(mc); 1052 compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len); 1053 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1054 s390mc->max_threads = S390_MAX_CPUS; 1055 } 1056 DEFINE_CCW_MACHINE(7, 1); 1057 1058 static void ccw_machine_7_0_instance_options(MachineState *machine) 1059 { 1060 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_0 }; 1061 1062 ccw_machine_7_1_instance_options(machine); 1063 s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat); 1064 } 1065 1066 static void ccw_machine_7_0_class_options(MachineClass *mc) 1067 { 1068 ccw_machine_7_1_class_options(mc); 1069 compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len); 1070 } 1071 DEFINE_CCW_MACHINE(7, 0); 1072 1073 static void ccw_machine_6_2_instance_options(MachineState *machine) 1074 { 1075 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_2 }; 1076 1077 ccw_machine_7_0_instance_options(machine); 1078 s390_set_qemu_cpu_model(0x3906, 14, 2, qemu_cpu_feat); 1079 } 1080 1081 static void ccw_machine_6_2_class_options(MachineClass *mc) 1082 { 1083 ccw_machine_7_0_class_options(mc); 1084 compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len); 1085 } 1086 DEFINE_CCW_MACHINE(6, 2); 1087 1088 static void ccw_machine_6_1_instance_options(MachineState *machine) 1089 { 1090 ccw_machine_6_2_instance_options(machine); 1091 s390_cpudef_featoff_greater(16, 1, S390_FEAT_NNPA); 1092 s390_cpudef_featoff_greater(16, 1, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2); 1093 s390_cpudef_featoff_greater(16, 1, S390_FEAT_BEAR_ENH); 1094 s390_cpudef_featoff_greater(16, 1, S390_FEAT_RDP); 1095 s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAI); 1096 } 1097 1098 static void ccw_machine_6_1_class_options(MachineClass *mc) 1099 { 1100 ccw_machine_6_2_class_options(mc); 1101 compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len); 1102 mc->smp_props.prefer_sockets = true; 1103 } 1104 DEFINE_CCW_MACHINE(6, 1); 1105 1106 static void ccw_machine_6_0_instance_options(MachineState *machine) 1107 { 1108 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_0 }; 1109 1110 ccw_machine_6_1_instance_options(machine); 1111 s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat); 1112 } 1113 1114 static void ccw_machine_6_0_class_options(MachineClass *mc) 1115 { 1116 ccw_machine_6_1_class_options(mc); 1117 compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len); 1118 } 1119 DEFINE_CCW_MACHINE(6, 0); 1120 1121 static void ccw_machine_5_2_instance_options(MachineState *machine) 1122 { 1123 ccw_machine_6_0_instance_options(machine); 1124 } 1125 1126 static void ccw_machine_5_2_class_options(MachineClass *mc) 1127 { 1128 ccw_machine_6_0_class_options(mc); 1129 compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len); 1130 } 1131 DEFINE_CCW_MACHINE(5, 2); 1132 1133 static void ccw_machine_5_1_instance_options(MachineState *machine) 1134 { 1135 ccw_machine_5_2_instance_options(machine); 1136 } 1137 1138 static void ccw_machine_5_1_class_options(MachineClass *mc) 1139 { 1140 ccw_machine_5_2_class_options(mc); 1141 compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len); 1142 } 1143 DEFINE_CCW_MACHINE(5, 1); 1144 1145 static void ccw_machine_5_0_instance_options(MachineState *machine) 1146 { 1147 ccw_machine_5_1_instance_options(machine); 1148 } 1149 1150 static void ccw_machine_5_0_class_options(MachineClass *mc) 1151 { 1152 ccw_machine_5_1_class_options(mc); 1153 compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len); 1154 } 1155 DEFINE_CCW_MACHINE(5, 0); 1156 1157 static void ccw_machine_4_2_instance_options(MachineState *machine) 1158 { 1159 ccw_machine_5_0_instance_options(machine); 1160 } 1161 1162 static void ccw_machine_4_2_class_options(MachineClass *mc) 1163 { 1164 ccw_machine_5_0_class_options(mc); 1165 mc->fixup_ram_size = s390_fixup_ram_size; 1166 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len); 1167 } 1168 DEFINE_CCW_MACHINE(4, 2); 1169 1170 static void ccw_machine_register_types(void) 1171 { 1172 type_register_static(&ccw_machine_info); 1173 } 1174 1175 type_init(ccw_machine_register_types) 1176