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 "exec/ram_addr.h" 17 #include "hw/s390x/s390-virtio-hcall.h" 18 #include "hw/s390x/sclp.h" 19 #include "hw/s390x/s390_flic.h" 20 #include "hw/s390x/ioinst.h" 21 #include "hw/s390x/css.h" 22 #include "virtio-ccw.h" 23 #include "qemu/config-file.h" 24 #include "qemu/ctype.h" 25 #include "qemu/error-report.h" 26 #include "qemu/option.h" 27 #include "qemu/qemu-print.h" 28 #include "qemu/units.h" 29 #include "hw/s390x/s390-pci-bus.h" 30 #include "sysemu/reset.h" 31 #include "hw/s390x/storage-keys.h" 32 #include "hw/s390x/storage-attributes.h" 33 #include "hw/s390x/event-facility.h" 34 #include "ipl.h" 35 #include "hw/s390x/s390-virtio-ccw.h" 36 #include "hw/s390x/css-bridge.h" 37 #include "hw/s390x/ap-bridge.h" 38 #include "migration/register.h" 39 #include "cpu_models.h" 40 #include "hw/nmi.h" 41 #include "hw/qdev-properties.h" 42 #include "hw/s390x/tod.h" 43 #include "sysemu/sysemu.h" 44 #include "sysemu/cpus.h" 45 #include "target/s390x/kvm/pv.h" 46 #include "migration/blocker.h" 47 #include "qapi/visitor.h" 48 49 static Error *pv_mig_blocker; 50 51 S390CPU *s390_cpu_addr2state(uint16_t cpu_addr) 52 { 53 static MachineState *ms; 54 55 if (!ms) { 56 ms = MACHINE(qdev_get_machine()); 57 g_assert(ms->possible_cpus); 58 } 59 60 /* CPU address corresponds to the core_id and the index */ 61 if (cpu_addr >= ms->possible_cpus->len) { 62 return NULL; 63 } 64 return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu); 65 } 66 67 static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id, 68 Error **errp) 69 { 70 S390CPU *cpu = S390_CPU(object_new(typename)); 71 S390CPU *ret = NULL; 72 73 if (!object_property_set_int(OBJECT(cpu), "core-id", core_id, errp)) { 74 goto out; 75 } 76 if (!qdev_realize(DEVICE(cpu), NULL, errp)) { 77 goto out; 78 } 79 ret = cpu; 80 81 out: 82 object_unref(OBJECT(cpu)); 83 return ret; 84 } 85 86 static void s390_init_cpus(MachineState *machine) 87 { 88 MachineClass *mc = MACHINE_GET_CLASS(machine); 89 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 90 int i; 91 92 if (machine->smp.threads > s390mc->max_threads) { 93 error_report("S390 does not support more than %d threads.", 94 s390mc->max_threads); 95 exit(1); 96 } 97 98 /* initialize possible_cpus */ 99 mc->possible_cpu_arch_ids(machine); 100 101 for (i = 0; i < machine->smp.cpus; i++) { 102 s390x_new_cpu(machine->cpu_type, i, &error_fatal); 103 } 104 } 105 106 static const char *const reset_dev_types[] = { 107 TYPE_VIRTUAL_CSS_BRIDGE, 108 "s390-sclp-event-facility", 109 "s390-flic", 110 "diag288", 111 TYPE_S390_PCI_HOST_BRIDGE, 112 TYPE_AP_BRIDGE, 113 }; 114 115 static void subsystem_reset(void) 116 { 117 DeviceState *dev; 118 int i; 119 120 for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) { 121 dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL)); 122 if (dev) { 123 device_cold_reset(dev); 124 } 125 } 126 } 127 128 static int virtio_ccw_hcall_notify(const uint64_t *args) 129 { 130 uint64_t subch_id = args[0]; 131 uint64_t queue = args[1]; 132 SubchDev *sch; 133 int cssid, ssid, schid, m; 134 135 if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) { 136 return -EINVAL; 137 } 138 sch = css_find_subch(m, cssid, ssid, schid); 139 if (!sch || !css_subch_visible(sch)) { 140 return -EINVAL; 141 } 142 if (queue >= VIRTIO_QUEUE_MAX) { 143 return -EINVAL; 144 } 145 virtio_queue_notify(virtio_ccw_get_vdev(sch), queue); 146 return 0; 147 148 } 149 150 static int virtio_ccw_hcall_early_printk(const uint64_t *args) 151 { 152 uint64_t mem = args[0]; 153 MachineState *ms = MACHINE(qdev_get_machine()); 154 155 if (mem < ms->ram_size) { 156 /* Early printk */ 157 return 0; 158 } 159 return -EINVAL; 160 } 161 162 static void virtio_ccw_register_hcalls(void) 163 { 164 s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, 165 virtio_ccw_hcall_notify); 166 /* Tolerate early printk. */ 167 s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY, 168 virtio_ccw_hcall_early_printk); 169 } 170 171 static void s390_memory_init(MemoryRegion *ram) 172 { 173 MemoryRegion *sysmem = get_system_memory(); 174 175 /* allocate RAM for core */ 176 memory_region_add_subregion(sysmem, 0, ram); 177 178 /* 179 * Configure the maximum page size. As no memory devices were created 180 * yet, this is the page size of initial memory only. 181 */ 182 s390_set_max_pagesize(qemu_maxrampagesize(), &error_fatal); 183 /* Initialize storage key device */ 184 s390_skeys_init(); 185 /* Initialize storage attributes device */ 186 s390_stattrib_init(); 187 } 188 189 static void s390_init_ipl_dev(const char *kernel_filename, 190 const char *kernel_cmdline, 191 const char *initrd_filename, const char *firmware, 192 const char *netboot_fw, bool enforce_bios) 193 { 194 Object *new = object_new(TYPE_S390_IPL); 195 DeviceState *dev = DEVICE(new); 196 char *netboot_fw_prop; 197 198 if (kernel_filename) { 199 qdev_prop_set_string(dev, "kernel", kernel_filename); 200 } 201 if (initrd_filename) { 202 qdev_prop_set_string(dev, "initrd", initrd_filename); 203 } 204 qdev_prop_set_string(dev, "cmdline", kernel_cmdline); 205 qdev_prop_set_string(dev, "firmware", firmware); 206 qdev_prop_set_bit(dev, "enforce_bios", enforce_bios); 207 netboot_fw_prop = object_property_get_str(new, "netboot_fw", &error_abort); 208 if (!strlen(netboot_fw_prop)) { 209 qdev_prop_set_string(dev, "netboot_fw", netboot_fw); 210 } 211 g_free(netboot_fw_prop); 212 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL, 213 new); 214 object_unref(new); 215 qdev_realize(dev, NULL, &error_fatal); 216 } 217 218 static void s390_create_virtio_net(BusState *bus, const char *name) 219 { 220 int i; 221 222 for (i = 0; i < nb_nics; i++) { 223 NICInfo *nd = &nd_table[i]; 224 DeviceState *dev; 225 226 qemu_check_nic_model(nd, "virtio"); 227 228 dev = qdev_new(name); 229 qdev_set_nic_properties(dev, nd); 230 qdev_realize_and_unref(dev, bus, &error_fatal); 231 } 232 } 233 234 static void s390_create_sclpconsole(const char *type, Chardev *chardev) 235 { 236 DeviceState *dev; 237 238 dev = qdev_new(type); 239 qdev_prop_set_chr(dev, "chardev", chardev); 240 qdev_realize_and_unref(dev, sclp_get_event_facility_bus(), &error_fatal); 241 } 242 243 static void ccw_init(MachineState *machine) 244 { 245 MachineClass *mc = MACHINE_GET_CLASS(machine); 246 int ret; 247 VirtualCssBus *css_bus; 248 DeviceState *dev; 249 250 s390_sclp_init(); 251 /* init memory + setup max page size. Required for the CPU model */ 252 s390_memory_init(machine->ram); 253 254 /* init CPUs (incl. CPU model) early so s390_has_feature() works */ 255 s390_init_cpus(machine); 256 257 /* Need CPU model to be determined before we can set up PV */ 258 s390_pv_init(machine->cgs, &error_fatal); 259 260 s390_flic_init(); 261 262 /* init the SIGP facility */ 263 s390_init_sigp(); 264 265 /* create AP bridge and bus(es) */ 266 s390_init_ap(); 267 268 /* get a BUS */ 269 css_bus = virtual_css_bus_init(); 270 s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline, 271 machine->initrd_filename, 272 machine->firmware ?: "s390-ccw.img", 273 "s390-netboot.img", true); 274 275 dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE); 276 object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE, 277 OBJECT(dev)); 278 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 279 280 /* register hypercalls */ 281 virtio_ccw_register_hcalls(); 282 283 s390_enable_css_support(s390_cpu_addr2state(0)); 284 285 ret = css_create_css_image(VIRTUAL_CSSID, true); 286 287 assert(ret == 0); 288 if (css_migration_enabled()) { 289 css_register_vmstate(); 290 } 291 292 /* Create VirtIO network adapters */ 293 s390_create_virtio_net(BUS(css_bus), mc->default_nic); 294 295 /* init consoles */ 296 if (serial_hd(0)) { 297 s390_create_sclpconsole("sclpconsole", serial_hd(0)); 298 } 299 if (serial_hd(1)) { 300 s390_create_sclpconsole("sclplmconsole", serial_hd(1)); 301 } 302 303 /* init the TOD clock */ 304 s390_init_tod(); 305 } 306 307 static void s390_cpu_plug(HotplugHandler *hotplug_dev, 308 DeviceState *dev, Error **errp) 309 { 310 MachineState *ms = MACHINE(hotplug_dev); 311 S390CPU *cpu = S390_CPU(dev); 312 313 g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu); 314 ms->possible_cpus->cpus[cpu->env.core_id].cpu = OBJECT(dev); 315 316 if (dev->hotplugged) { 317 raise_irq_cpu_hotplug(); 318 } 319 } 320 321 static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg) 322 { 323 S390CPU *cpu = S390_CPU(cs); 324 325 s390_ipl_prepare_cpu(cpu); 326 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); 327 } 328 329 static void s390_machine_unprotect(S390CcwMachineState *ms) 330 { 331 if (!s390_pv_vm_try_disable_async(ms)) { 332 s390_pv_vm_disable(); 333 } 334 ms->pv = false; 335 migrate_del_blocker(pv_mig_blocker); 336 error_free_or_abort(&pv_mig_blocker); 337 ram_block_discard_disable(false); 338 } 339 340 static int s390_machine_protect(S390CcwMachineState *ms) 341 { 342 Error *local_err = NULL; 343 int rc; 344 345 /* 346 * Discarding of memory in RAM blocks does not work as expected with 347 * protected VMs. Sharing and unsharing pages would be required. Disable 348 * it for now, until until we have a solution to make at least Linux 349 * guests either support it (e.g., virtio-balloon) or fail gracefully. 350 */ 351 rc = ram_block_discard_disable(true); 352 if (rc) { 353 error_report("protected VMs: cannot disable RAM discard"); 354 return rc; 355 } 356 357 error_setg(&pv_mig_blocker, 358 "protected VMs are currently not migratable."); 359 rc = migrate_add_blocker(pv_mig_blocker, &local_err); 360 if (rc) { 361 ram_block_discard_disable(false); 362 error_report_err(local_err); 363 error_free_or_abort(&pv_mig_blocker); 364 return rc; 365 } 366 367 /* Create SE VM */ 368 rc = s390_pv_vm_enable(); 369 if (rc) { 370 ram_block_discard_disable(false); 371 migrate_del_blocker(pv_mig_blocker); 372 error_free_or_abort(&pv_mig_blocker); 373 return rc; 374 } 375 376 ms->pv = true; 377 378 /* Will return 0 if API is not available since it's not vital */ 379 rc = s390_pv_query_info(); 380 if (rc) { 381 goto out_err; 382 } 383 384 /* Set SE header and unpack */ 385 rc = s390_ipl_prepare_pv_header(); 386 if (rc) { 387 goto out_err; 388 } 389 390 /* Decrypt image */ 391 rc = s390_ipl_pv_unpack(); 392 if (rc) { 393 goto out_err; 394 } 395 396 /* Verify integrity */ 397 rc = s390_pv_verify(); 398 if (rc) { 399 goto out_err; 400 } 401 return rc; 402 403 out_err: 404 s390_machine_unprotect(ms); 405 return rc; 406 } 407 408 static void s390_pv_prepare_reset(S390CcwMachineState *ms) 409 { 410 CPUState *cs; 411 412 if (!s390_is_pv()) { 413 return; 414 } 415 /* Unsharing requires all cpus to be stopped */ 416 CPU_FOREACH(cs) { 417 s390_cpu_set_state(S390_CPU_STATE_STOPPED, S390_CPU(cs)); 418 } 419 s390_pv_unshare(); 420 s390_pv_prep_reset(); 421 } 422 423 static void s390_machine_reset(MachineState *machine, ShutdownCause reason) 424 { 425 S390CcwMachineState *ms = S390_CCW_MACHINE(machine); 426 enum s390_reset reset_type; 427 CPUState *cs, *t; 428 S390CPU *cpu; 429 430 /* get the reset parameters, reset them once done */ 431 s390_ipl_get_reset_request(&cs, &reset_type); 432 433 /* all CPUs are paused and synchronized at this point */ 434 s390_cmma_reset(); 435 436 cpu = S390_CPU(cs); 437 438 switch (reset_type) { 439 case S390_RESET_EXTERNAL: 440 case S390_RESET_REIPL: 441 /* 442 * Reset the subsystem which includes a AP reset. If a PV 443 * guest had APQNs attached the AP reset is a prerequisite to 444 * unprotecting since the UV checks if all APQNs are reset. 445 */ 446 subsystem_reset(); 447 if (s390_is_pv()) { 448 s390_machine_unprotect(ms); 449 } 450 451 /* 452 * Device reset includes CPU clear resets so this has to be 453 * done AFTER the unprotect call above. 454 */ 455 qemu_devices_reset(reason); 456 s390_crypto_reset(); 457 458 /* configure and start the ipl CPU only */ 459 run_on_cpu(cs, s390_do_cpu_ipl, RUN_ON_CPU_NULL); 460 break; 461 case S390_RESET_MODIFIED_CLEAR: 462 /* 463 * Subsystem reset needs to be done before we unshare memory 464 * and lose access to VIRTIO structures in guest memory. 465 */ 466 subsystem_reset(); 467 s390_crypto_reset(); 468 s390_pv_prepare_reset(ms); 469 CPU_FOREACH(t) { 470 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 471 } 472 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 473 break; 474 case S390_RESET_LOAD_NORMAL: 475 /* 476 * Subsystem reset needs to be done before we unshare memory 477 * and lose access to VIRTIO structures in guest memory. 478 */ 479 subsystem_reset(); 480 s390_pv_prepare_reset(ms); 481 CPU_FOREACH(t) { 482 if (t == cs) { 483 continue; 484 } 485 run_on_cpu(t, s390_do_cpu_reset, RUN_ON_CPU_NULL); 486 } 487 run_on_cpu(cs, s390_do_cpu_initial_reset, RUN_ON_CPU_NULL); 488 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 489 break; 490 case S390_RESET_PV: /* Subcode 10 */ 491 subsystem_reset(); 492 s390_crypto_reset(); 493 494 CPU_FOREACH(t) { 495 if (t == cs) { 496 continue; 497 } 498 run_on_cpu(t, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 499 } 500 run_on_cpu(cs, s390_do_cpu_reset, RUN_ON_CPU_NULL); 501 502 if (s390_machine_protect(ms)) { 503 s390_pv_inject_reset_error(cs); 504 /* 505 * Continue after the diag308 so the guest knows something 506 * went wrong. 507 */ 508 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); 509 return; 510 } 511 512 run_on_cpu(cs, s390_do_cpu_load_normal, RUN_ON_CPU_NULL); 513 break; 514 default: 515 g_assert_not_reached(); 516 } 517 518 CPU_FOREACH(t) { 519 run_on_cpu(t, s390_do_cpu_set_diag318, RUN_ON_CPU_HOST_ULONG(0)); 520 } 521 s390_ipl_clear_reset_request(); 522 } 523 524 static void s390_machine_device_plug(HotplugHandler *hotplug_dev, 525 DeviceState *dev, Error **errp) 526 { 527 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 528 s390_cpu_plug(hotplug_dev, dev, errp); 529 } 530 } 531 532 static void s390_machine_device_unplug_request(HotplugHandler *hotplug_dev, 533 DeviceState *dev, Error **errp) 534 { 535 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 536 error_setg(errp, "CPU hot unplug not supported on this machine"); 537 return; 538 } 539 } 540 541 static CpuInstanceProperties s390_cpu_index_to_props(MachineState *ms, 542 unsigned cpu_index) 543 { 544 MachineClass *mc = MACHINE_GET_CLASS(ms); 545 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms); 546 547 assert(cpu_index < possible_cpus->len); 548 return possible_cpus->cpus[cpu_index].props; 549 } 550 551 static const CPUArchIdList *s390_possible_cpu_arch_ids(MachineState *ms) 552 { 553 int i; 554 unsigned int max_cpus = ms->smp.max_cpus; 555 556 if (ms->possible_cpus) { 557 g_assert(ms->possible_cpus && ms->possible_cpus->len == max_cpus); 558 return ms->possible_cpus; 559 } 560 561 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) + 562 sizeof(CPUArchId) * max_cpus); 563 ms->possible_cpus->len = max_cpus; 564 for (i = 0; i < ms->possible_cpus->len; i++) { 565 ms->possible_cpus->cpus[i].type = ms->cpu_type; 566 ms->possible_cpus->cpus[i].vcpus_count = 1; 567 ms->possible_cpus->cpus[i].arch_id = i; 568 ms->possible_cpus->cpus[i].props.has_core_id = true; 569 ms->possible_cpus->cpus[i].props.core_id = i; 570 } 571 572 return ms->possible_cpus; 573 } 574 575 static HotplugHandler *s390_get_hotplug_handler(MachineState *machine, 576 DeviceState *dev) 577 { 578 if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 579 return HOTPLUG_HANDLER(machine); 580 } 581 return NULL; 582 } 583 584 static void s390_nmi(NMIState *n, int cpu_index, Error **errp) 585 { 586 CPUState *cs = qemu_get_cpu(cpu_index); 587 588 s390_cpu_restart(S390_CPU(cs)); 589 } 590 591 static ram_addr_t s390_fixup_ram_size(ram_addr_t sz) 592 { 593 /* same logic as in sclp.c */ 594 int increment_size = 20; 595 ram_addr_t newsz; 596 597 while ((sz >> increment_size) > MAX_STORAGE_INCREMENTS) { 598 increment_size++; 599 } 600 newsz = sz >> increment_size << increment_size; 601 602 if (sz != newsz) { 603 qemu_printf("Ram size %" PRIu64 "MB was fixed up to %" PRIu64 604 "MB to match machine restrictions. Consider updating " 605 "the guest definition.\n", (uint64_t) (sz / MiB), 606 (uint64_t) (newsz / MiB)); 607 } 608 return newsz; 609 } 610 611 static inline bool machine_get_aes_key_wrap(Object *obj, Error **errp) 612 { 613 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 614 615 return ms->aes_key_wrap; 616 } 617 618 static inline void machine_set_aes_key_wrap(Object *obj, bool value, 619 Error **errp) 620 { 621 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 622 623 ms->aes_key_wrap = value; 624 } 625 626 static inline bool machine_get_dea_key_wrap(Object *obj, Error **errp) 627 { 628 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 629 630 return ms->dea_key_wrap; 631 } 632 633 static inline void machine_set_dea_key_wrap(Object *obj, bool value, 634 Error **errp) 635 { 636 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 637 638 ms->dea_key_wrap = value; 639 } 640 641 static S390CcwMachineClass *current_mc; 642 643 /* 644 * Get the class of the s390-ccw-virtio machine that is currently in use. 645 * Note: libvirt is using the "none" machine to probe for the features of the 646 * host CPU, so in case this is called with the "none" machine, the function 647 * returns the TYPE_S390_CCW_MACHINE base class. In this base class, all the 648 * various "*_allowed" variables are enabled, so that the *_allowed() wrappers 649 * below return the correct default value for the "none" machine. 650 * 651 * Attention! Do *not* add additional new wrappers for CPU features (e.g. like 652 * the ri_allowed() wrapper) via this mechanism anymore. CPU features should 653 * be handled via the CPU models, i.e. checking with cpu_model_allowed() during 654 * CPU initialization and s390_has_feat() later should be sufficient. 655 */ 656 static S390CcwMachineClass *get_machine_class(void) 657 { 658 if (unlikely(!current_mc)) { 659 /* 660 * No s390 ccw machine was instantiated, we are likely to 661 * be called for the 'none' machine. The properties will 662 * have their after-initialization values. 663 */ 664 current_mc = S390_CCW_MACHINE_CLASS( 665 object_class_by_name(TYPE_S390_CCW_MACHINE)); 666 } 667 return current_mc; 668 } 669 670 bool ri_allowed(void) 671 { 672 return get_machine_class()->ri_allowed; 673 } 674 675 bool cpu_model_allowed(void) 676 { 677 return get_machine_class()->cpu_model_allowed; 678 } 679 680 bool hpage_1m_allowed(void) 681 { 682 return get_machine_class()->hpage_1m_allowed; 683 } 684 685 static void machine_get_loadparm(Object *obj, Visitor *v, 686 const char *name, void *opaque, 687 Error **errp) 688 { 689 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 690 char *str = g_strndup((char *) ms->loadparm, sizeof(ms->loadparm)); 691 692 visit_type_str(v, name, &str, errp); 693 g_free(str); 694 } 695 696 static void machine_set_loadparm(Object *obj, Visitor *v, 697 const char *name, void *opaque, 698 Error **errp) 699 { 700 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 701 char *val; 702 int i; 703 704 if (!visit_type_str(v, name, &val, errp)) { 705 return; 706 } 707 708 for (i = 0; i < sizeof(ms->loadparm) && val[i]; i++) { 709 uint8_t c = qemu_toupper(val[i]); /* mimic HMC */ 710 711 if (('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || (c == '.') || 712 (c == ' ')) { 713 ms->loadparm[i] = c; 714 } else { 715 error_setg(errp, "LOADPARM: invalid character '%c' (ASCII 0x%02x)", 716 c, c); 717 return; 718 } 719 } 720 721 for (; i < sizeof(ms->loadparm); i++) { 722 ms->loadparm[i] = ' '; /* pad right with spaces */ 723 } 724 } 725 726 static void ccw_machine_class_init(ObjectClass *oc, void *data) 727 { 728 MachineClass *mc = MACHINE_CLASS(oc); 729 NMIClass *nc = NMI_CLASS(oc); 730 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 731 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 732 733 s390mc->ri_allowed = true; 734 s390mc->cpu_model_allowed = true; 735 s390mc->css_migration_enabled = true; 736 s390mc->hpage_1m_allowed = true; 737 s390mc->max_threads = 1; 738 mc->init = ccw_init; 739 mc->reset = s390_machine_reset; 740 mc->block_default_type = IF_VIRTIO; 741 mc->no_cdrom = 1; 742 mc->no_floppy = 1; 743 mc->no_parallel = 1; 744 mc->no_sdcard = 1; 745 mc->max_cpus = S390_MAX_CPUS; 746 mc->has_hotpluggable_cpus = true; 747 mc->smp_props.books_supported = true; 748 mc->smp_props.drawers_supported = true; 749 assert(!mc->get_hotplug_handler); 750 mc->get_hotplug_handler = s390_get_hotplug_handler; 751 mc->cpu_index_to_instance_props = s390_cpu_index_to_props; 752 mc->possible_cpu_arch_ids = s390_possible_cpu_arch_ids; 753 /* it is overridden with 'host' cpu *in kvm_arch_init* */ 754 mc->default_cpu_type = S390_CPU_TYPE_NAME("qemu"); 755 hc->plug = s390_machine_device_plug; 756 hc->unplug_request = s390_machine_device_unplug_request; 757 nc->nmi_monitor_handler = s390_nmi; 758 mc->default_ram_id = "s390.ram"; 759 mc->default_nic = "virtio-net-ccw"; 760 761 object_class_property_add_bool(oc, "aes-key-wrap", 762 machine_get_aes_key_wrap, 763 machine_set_aes_key_wrap); 764 object_class_property_set_description(oc, "aes-key-wrap", 765 "enable/disable AES key wrapping using the CPACF wrapping key"); 766 767 object_class_property_add_bool(oc, "dea-key-wrap", 768 machine_get_dea_key_wrap, 769 machine_set_dea_key_wrap); 770 object_class_property_set_description(oc, "dea-key-wrap", 771 "enable/disable DEA key wrapping using the CPACF wrapping key"); 772 773 object_class_property_add(oc, "loadparm", "loadparm", 774 machine_get_loadparm, machine_set_loadparm, 775 NULL, NULL); 776 object_class_property_set_description(oc, "loadparm", 777 "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted" 778 " to upper case) to pass to machine loader, boot manager," 779 " and guest kernel"); 780 } 781 782 static inline void s390_machine_initfn(Object *obj) 783 { 784 S390CcwMachineState *ms = S390_CCW_MACHINE(obj); 785 786 ms->aes_key_wrap = true; 787 ms->dea_key_wrap = true; 788 } 789 790 static const TypeInfo ccw_machine_info = { 791 .name = TYPE_S390_CCW_MACHINE, 792 .parent = TYPE_MACHINE, 793 .abstract = true, 794 .instance_size = sizeof(S390CcwMachineState), 795 .instance_init = s390_machine_initfn, 796 .class_size = sizeof(S390CcwMachineClass), 797 .class_init = ccw_machine_class_init, 798 .interfaces = (InterfaceInfo[]) { 799 { TYPE_NMI }, 800 { TYPE_HOTPLUG_HANDLER}, 801 { } 802 }, 803 }; 804 805 bool css_migration_enabled(void) 806 { 807 return get_machine_class()->css_migration_enabled; 808 } 809 810 #define DEFINE_CCW_MACHINE(suffix, verstr, latest) \ 811 static void ccw_machine_##suffix##_class_init(ObjectClass *oc, \ 812 void *data) \ 813 { \ 814 MachineClass *mc = MACHINE_CLASS(oc); \ 815 ccw_machine_##suffix##_class_options(mc); \ 816 mc->desc = "Virtual s390x machine (version " verstr ")"; \ 817 if (latest) { \ 818 mc->alias = "s390-ccw-virtio"; \ 819 mc->is_default = true; \ 820 } \ 821 } \ 822 static void ccw_machine_##suffix##_instance_init(Object *obj) \ 823 { \ 824 MachineState *machine = MACHINE(obj); \ 825 current_mc = S390_CCW_MACHINE_CLASS(MACHINE_GET_CLASS(machine)); \ 826 ccw_machine_##suffix##_instance_options(machine); \ 827 } \ 828 static const TypeInfo ccw_machine_##suffix##_info = { \ 829 .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr), \ 830 .parent = TYPE_S390_CCW_MACHINE, \ 831 .class_init = ccw_machine_##suffix##_class_init, \ 832 .instance_init = ccw_machine_##suffix##_instance_init, \ 833 }; \ 834 static void ccw_machine_register_##suffix(void) \ 835 { \ 836 type_register_static(&ccw_machine_##suffix##_info); \ 837 } \ 838 type_init(ccw_machine_register_##suffix) 839 840 static void ccw_machine_8_2_instance_options(MachineState *machine) 841 { 842 } 843 844 static void ccw_machine_8_2_class_options(MachineClass *mc) 845 { 846 } 847 DEFINE_CCW_MACHINE(8_2, "8.2", true); 848 849 static void ccw_machine_8_1_instance_options(MachineState *machine) 850 { 851 ccw_machine_8_2_instance_options(machine); 852 } 853 854 static void ccw_machine_8_1_class_options(MachineClass *mc) 855 { 856 ccw_machine_8_2_class_options(mc); 857 compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len); 858 mc->smp_props.drawers_supported = false; 859 mc->smp_props.books_supported = false; 860 } 861 DEFINE_CCW_MACHINE(8_1, "8.1", false); 862 863 static void ccw_machine_8_0_instance_options(MachineState *machine) 864 { 865 ccw_machine_8_1_instance_options(machine); 866 } 867 868 static void ccw_machine_8_0_class_options(MachineClass *mc) 869 { 870 ccw_machine_8_1_class_options(mc); 871 compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len); 872 } 873 DEFINE_CCW_MACHINE(8_0, "8.0", false); 874 875 static void ccw_machine_7_2_instance_options(MachineState *machine) 876 { 877 ccw_machine_8_0_instance_options(machine); 878 } 879 880 static void ccw_machine_7_2_class_options(MachineClass *mc) 881 { 882 ccw_machine_8_0_class_options(mc); 883 compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len); 884 } 885 DEFINE_CCW_MACHINE(7_2, "7.2", false); 886 887 static void ccw_machine_7_1_instance_options(MachineState *machine) 888 { 889 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_1 }; 890 891 ccw_machine_7_2_instance_options(machine); 892 s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAIE); 893 s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat); 894 } 895 896 static void ccw_machine_7_1_class_options(MachineClass *mc) 897 { 898 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 899 static GlobalProperty compat[] = { 900 { TYPE_S390_PCI_DEVICE, "interpret", "off", }, 901 { TYPE_S390_PCI_DEVICE, "forwarding-assist", "off", }, 902 }; 903 904 ccw_machine_7_2_class_options(mc); 905 compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len); 906 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 907 s390mc->max_threads = S390_MAX_CPUS; 908 } 909 DEFINE_CCW_MACHINE(7_1, "7.1", false); 910 911 static void ccw_machine_7_0_instance_options(MachineState *machine) 912 { 913 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_0 }; 914 915 ccw_machine_7_1_instance_options(machine); 916 s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat); 917 } 918 919 static void ccw_machine_7_0_class_options(MachineClass *mc) 920 { 921 ccw_machine_7_1_class_options(mc); 922 compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len); 923 } 924 DEFINE_CCW_MACHINE(7_0, "7.0", false); 925 926 static void ccw_machine_6_2_instance_options(MachineState *machine) 927 { 928 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_2 }; 929 930 ccw_machine_7_0_instance_options(machine); 931 s390_set_qemu_cpu_model(0x3906, 14, 2, qemu_cpu_feat); 932 } 933 934 static void ccw_machine_6_2_class_options(MachineClass *mc) 935 { 936 ccw_machine_7_0_class_options(mc); 937 compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len); 938 } 939 DEFINE_CCW_MACHINE(6_2, "6.2", false); 940 941 static void ccw_machine_6_1_instance_options(MachineState *machine) 942 { 943 ccw_machine_6_2_instance_options(machine); 944 s390_cpudef_featoff_greater(16, 1, S390_FEAT_NNPA); 945 s390_cpudef_featoff_greater(16, 1, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2); 946 s390_cpudef_featoff_greater(16, 1, S390_FEAT_BEAR_ENH); 947 s390_cpudef_featoff_greater(16, 1, S390_FEAT_RDP); 948 s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAI); 949 } 950 951 static void ccw_machine_6_1_class_options(MachineClass *mc) 952 { 953 ccw_machine_6_2_class_options(mc); 954 compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len); 955 mc->smp_props.prefer_sockets = true; 956 } 957 DEFINE_CCW_MACHINE(6_1, "6.1", false); 958 959 static void ccw_machine_6_0_instance_options(MachineState *machine) 960 { 961 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_0 }; 962 963 ccw_machine_6_1_instance_options(machine); 964 s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat); 965 } 966 967 static void ccw_machine_6_0_class_options(MachineClass *mc) 968 { 969 ccw_machine_6_1_class_options(mc); 970 compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len); 971 } 972 DEFINE_CCW_MACHINE(6_0, "6.0", false); 973 974 static void ccw_machine_5_2_instance_options(MachineState *machine) 975 { 976 ccw_machine_6_0_instance_options(machine); 977 } 978 979 static void ccw_machine_5_2_class_options(MachineClass *mc) 980 { 981 ccw_machine_6_0_class_options(mc); 982 compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len); 983 } 984 DEFINE_CCW_MACHINE(5_2, "5.2", false); 985 986 static void ccw_machine_5_1_instance_options(MachineState *machine) 987 { 988 ccw_machine_5_2_instance_options(machine); 989 } 990 991 static void ccw_machine_5_1_class_options(MachineClass *mc) 992 { 993 ccw_machine_5_2_class_options(mc); 994 compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len); 995 } 996 DEFINE_CCW_MACHINE(5_1, "5.1", false); 997 998 static void ccw_machine_5_0_instance_options(MachineState *machine) 999 { 1000 ccw_machine_5_1_instance_options(machine); 1001 } 1002 1003 static void ccw_machine_5_0_class_options(MachineClass *mc) 1004 { 1005 ccw_machine_5_1_class_options(mc); 1006 compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len); 1007 } 1008 DEFINE_CCW_MACHINE(5_0, "5.0", false); 1009 1010 static void ccw_machine_4_2_instance_options(MachineState *machine) 1011 { 1012 ccw_machine_5_0_instance_options(machine); 1013 } 1014 1015 static void ccw_machine_4_2_class_options(MachineClass *mc) 1016 { 1017 ccw_machine_5_0_class_options(mc); 1018 mc->fixup_ram_size = s390_fixup_ram_size; 1019 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len); 1020 } 1021 DEFINE_CCW_MACHINE(4_2, "4.2", false); 1022 1023 static void ccw_machine_4_1_instance_options(MachineState *machine) 1024 { 1025 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_1 }; 1026 ccw_machine_4_2_instance_options(machine); 1027 s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat); 1028 } 1029 1030 static void ccw_machine_4_1_class_options(MachineClass *mc) 1031 { 1032 ccw_machine_4_2_class_options(mc); 1033 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len); 1034 } 1035 DEFINE_CCW_MACHINE(4_1, "4.1", false); 1036 1037 static void ccw_machine_4_0_instance_options(MachineState *machine) 1038 { 1039 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_0 }; 1040 ccw_machine_4_1_instance_options(machine); 1041 s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat); 1042 } 1043 1044 static void ccw_machine_4_0_class_options(MachineClass *mc) 1045 { 1046 ccw_machine_4_1_class_options(mc); 1047 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len); 1048 } 1049 DEFINE_CCW_MACHINE(4_0, "4.0", false); 1050 1051 static void ccw_machine_3_1_instance_options(MachineState *machine) 1052 { 1053 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V3_1 }; 1054 ccw_machine_4_0_instance_options(machine); 1055 s390_cpudef_featoff_greater(14, 1, S390_FEAT_MULTIPLE_EPOCH); 1056 s390_cpudef_group_featoff_greater(14, 1, S390_FEAT_GROUP_MULTIPLE_EPOCH_PTFF); 1057 s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat); 1058 } 1059 1060 static void ccw_machine_3_1_class_options(MachineClass *mc) 1061 { 1062 ccw_machine_4_0_class_options(mc); 1063 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len); 1064 } 1065 DEFINE_CCW_MACHINE(3_1, "3.1", false); 1066 1067 static void ccw_machine_3_0_instance_options(MachineState *machine) 1068 { 1069 ccw_machine_3_1_instance_options(machine); 1070 } 1071 1072 static void ccw_machine_3_0_class_options(MachineClass *mc) 1073 { 1074 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 1075 1076 s390mc->hpage_1m_allowed = false; 1077 ccw_machine_3_1_class_options(mc); 1078 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len); 1079 } 1080 DEFINE_CCW_MACHINE(3_0, "3.0", false); 1081 1082 static void ccw_machine_2_12_instance_options(MachineState *machine) 1083 { 1084 ccw_machine_3_0_instance_options(machine); 1085 s390_cpudef_featoff_greater(11, 1, S390_FEAT_PPA15); 1086 s390_cpudef_featoff_greater(11, 1, S390_FEAT_BPB); 1087 } 1088 1089 static void ccw_machine_2_12_class_options(MachineClass *mc) 1090 { 1091 ccw_machine_3_0_class_options(mc); 1092 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len); 1093 } 1094 DEFINE_CCW_MACHINE(2_12, "2.12", false); 1095 1096 static void ccw_machine_2_11_instance_options(MachineState *machine) 1097 { 1098 static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V2_11 }; 1099 ccw_machine_2_12_instance_options(machine); 1100 1101 /* before 2.12 we emulated the very first z900 */ 1102 s390_set_qemu_cpu_model(0x2064, 7, 1, qemu_cpu_feat); 1103 } 1104 1105 static void ccw_machine_2_11_class_options(MachineClass *mc) 1106 { 1107 static GlobalProperty compat[] = { 1108 { TYPE_SCLP_EVENT_FACILITY, "allow_all_mask_sizes", "off", }, 1109 }; 1110 1111 ccw_machine_2_12_class_options(mc); 1112 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len); 1113 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1114 } 1115 DEFINE_CCW_MACHINE(2_11, "2.11", false); 1116 1117 static void ccw_machine_2_10_instance_options(MachineState *machine) 1118 { 1119 ccw_machine_2_11_instance_options(machine); 1120 } 1121 1122 static void ccw_machine_2_10_class_options(MachineClass *mc) 1123 { 1124 ccw_machine_2_11_class_options(mc); 1125 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len); 1126 } 1127 DEFINE_CCW_MACHINE(2_10, "2.10", false); 1128 1129 static void ccw_machine_2_9_instance_options(MachineState *machine) 1130 { 1131 ccw_machine_2_10_instance_options(machine); 1132 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP); 1133 s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2); 1134 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ZPCI); 1135 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_INT_SUPPRESSION); 1136 s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_EVENT_NOTIFICATION); 1137 } 1138 1139 static void ccw_machine_2_9_class_options(MachineClass *mc) 1140 { 1141 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 1142 static GlobalProperty compat[] = { 1143 { TYPE_S390_STATTRIB, "migration-enabled", "off", }, 1144 }; 1145 1146 ccw_machine_2_10_class_options(mc); 1147 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len); 1148 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1149 s390mc->css_migration_enabled = false; 1150 } 1151 DEFINE_CCW_MACHINE(2_9, "2.9", false); 1152 1153 static void ccw_machine_2_8_instance_options(MachineState *machine) 1154 { 1155 ccw_machine_2_9_instance_options(machine); 1156 } 1157 1158 static void ccw_machine_2_8_class_options(MachineClass *mc) 1159 { 1160 static GlobalProperty compat[] = { 1161 { TYPE_S390_FLIC_COMMON, "adapter_routes_max_batch", "64", }, 1162 }; 1163 1164 ccw_machine_2_9_class_options(mc); 1165 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len); 1166 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1167 } 1168 DEFINE_CCW_MACHINE(2_8, "2.8", false); 1169 1170 static void ccw_machine_2_7_instance_options(MachineState *machine) 1171 { 1172 ccw_machine_2_8_instance_options(machine); 1173 } 1174 1175 static void ccw_machine_2_7_class_options(MachineClass *mc) 1176 { 1177 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 1178 1179 s390mc->cpu_model_allowed = false; 1180 ccw_machine_2_8_class_options(mc); 1181 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len); 1182 } 1183 DEFINE_CCW_MACHINE(2_7, "2.7", false); 1184 1185 static void ccw_machine_2_6_instance_options(MachineState *machine) 1186 { 1187 ccw_machine_2_7_instance_options(machine); 1188 } 1189 1190 static void ccw_machine_2_6_class_options(MachineClass *mc) 1191 { 1192 S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc); 1193 static GlobalProperty compat[] = { 1194 { TYPE_S390_IPL, "iplbext_migration", "off", }, 1195 { TYPE_VIRTUAL_CSS_BRIDGE, "css_dev_path", "off", }, 1196 }; 1197 1198 s390mc->ri_allowed = false; 1199 ccw_machine_2_7_class_options(mc); 1200 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len); 1201 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1202 } 1203 DEFINE_CCW_MACHINE(2_6, "2.6", false); 1204 1205 static void ccw_machine_2_5_instance_options(MachineState *machine) 1206 { 1207 ccw_machine_2_6_instance_options(machine); 1208 } 1209 1210 static void ccw_machine_2_5_class_options(MachineClass *mc) 1211 { 1212 ccw_machine_2_6_class_options(mc); 1213 compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len); 1214 } 1215 DEFINE_CCW_MACHINE(2_5, "2.5", false); 1216 1217 static void ccw_machine_2_4_instance_options(MachineState *machine) 1218 { 1219 ccw_machine_2_5_instance_options(machine); 1220 } 1221 1222 static void ccw_machine_2_4_class_options(MachineClass *mc) 1223 { 1224 static GlobalProperty compat[] = { 1225 { TYPE_S390_SKEYS, "migration-enabled", "off", }, 1226 { "virtio-blk-ccw", "max_revision", "0", }, 1227 { "virtio-balloon-ccw", "max_revision", "0", }, 1228 { "virtio-serial-ccw", "max_revision", "0", }, 1229 { "virtio-9p-ccw", "max_revision", "0", }, 1230 { "virtio-rng-ccw", "max_revision", "0", }, 1231 { "virtio-net-ccw", "max_revision", "0", }, 1232 { "virtio-scsi-ccw", "max_revision", "0", }, 1233 { "vhost-scsi-ccw", "max_revision", "0", }, 1234 }; 1235 1236 ccw_machine_2_5_class_options(mc); 1237 compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len); 1238 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat)); 1239 } 1240 DEFINE_CCW_MACHINE(2_4, "2.4", false); 1241 1242 static void ccw_machine_register_types(void) 1243 { 1244 type_register_static(&ccw_machine_info); 1245 } 1246 1247 type_init(ccw_machine_register_types) 1248