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