xref: /openbmc/qemu/hw/s390x/s390-virtio-ccw.c (revision 5f88dd43)
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 };
113 
114 static void subsystem_reset(void)
115 {
116     DeviceState *dev;
117     int i;
118 
119     for (i = 0; i < ARRAY_SIZE(reset_dev_types); i++) {
120         dev = DEVICE(object_resolve_path_type("", reset_dev_types[i], NULL));
121         if (dev) {
122             device_cold_reset(dev);
123         }
124     }
125 }
126 
127 static int virtio_ccw_hcall_notify(const uint64_t *args)
128 {
129     uint64_t subch_id = args[0];
130     uint64_t queue = args[1];
131     SubchDev *sch;
132     int cssid, ssid, schid, m;
133 
134     if (ioinst_disassemble_sch_ident(subch_id, &m, &cssid, &ssid, &schid)) {
135         return -EINVAL;
136     }
137     sch = css_find_subch(m, cssid, ssid, schid);
138     if (!sch || !css_subch_visible(sch)) {
139         return -EINVAL;
140     }
141     if (queue >= VIRTIO_QUEUE_MAX) {
142         return -EINVAL;
143     }
144     virtio_queue_notify(virtio_ccw_get_vdev(sch), queue);
145     return 0;
146 
147 }
148 
149 static int virtio_ccw_hcall_early_printk(const uint64_t *args)
150 {
151     uint64_t mem = args[0];
152     MachineState *ms = MACHINE(qdev_get_machine());
153 
154     if (mem < ms->ram_size) {
155         /* Early printk */
156         return 0;
157     }
158     return -EINVAL;
159 }
160 
161 static void virtio_ccw_register_hcalls(void)
162 {
163     s390_register_virtio_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY,
164                                    virtio_ccw_hcall_notify);
165     /* Tolerate early printk. */
166     s390_register_virtio_hypercall(KVM_S390_VIRTIO_NOTIFY,
167                                    virtio_ccw_hcall_early_printk);
168 }
169 
170 static void s390_memory_init(MemoryRegion *ram)
171 {
172     MemoryRegion *sysmem = get_system_memory();
173 
174     /* allocate RAM for core */
175     memory_region_add_subregion(sysmem, 0, ram);
176 
177     /*
178      * Configure the maximum page size. As no memory devices were created
179      * yet, this is the page size of initial memory only.
180      */
181     s390_set_max_pagesize(qemu_maxrampagesize(), &error_fatal);
182     /* Initialize storage key device */
183     s390_skeys_init();
184     /* Initialize storage attributes device */
185     s390_stattrib_init();
186 }
187 
188 static void s390_init_ipl_dev(const char *kernel_filename,
189                               const char *kernel_cmdline,
190                               const char *initrd_filename, const char *firmware,
191                               const char *netboot_fw, bool enforce_bios)
192 {
193     Object *new = object_new(TYPE_S390_IPL);
194     DeviceState *dev = DEVICE(new);
195     char *netboot_fw_prop;
196 
197     if (kernel_filename) {
198         qdev_prop_set_string(dev, "kernel", kernel_filename);
199     }
200     if (initrd_filename) {
201         qdev_prop_set_string(dev, "initrd", initrd_filename);
202     }
203     qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
204     qdev_prop_set_string(dev, "firmware", firmware);
205     qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
206     netboot_fw_prop = object_property_get_str(new, "netboot_fw", &error_abort);
207     if (!strlen(netboot_fw_prop)) {
208         qdev_prop_set_string(dev, "netboot_fw", netboot_fw);
209     }
210     g_free(netboot_fw_prop);
211     object_property_add_child(qdev_get_machine(), TYPE_S390_IPL,
212                               new);
213     object_unref(new);
214     qdev_realize(dev, NULL, &error_fatal);
215 }
216 
217 static void s390_create_virtio_net(BusState *bus, const char *name)
218 {
219     int i;
220 
221     for (i = 0; i < nb_nics; i++) {
222         NICInfo *nd = &nd_table[i];
223         DeviceState *dev;
224 
225         qemu_check_nic_model(nd, "virtio");
226 
227         dev = qdev_new(name);
228         qdev_set_nic_properties(dev, nd);
229         qdev_realize_and_unref(dev, bus, &error_fatal);
230     }
231 }
232 
233 static void s390_create_sclpconsole(const char *type, Chardev *chardev)
234 {
235     DeviceState *dev;
236 
237     dev = qdev_new(type);
238     qdev_prop_set_chr(dev, "chardev", chardev);
239     qdev_realize_and_unref(dev, sclp_get_event_facility_bus(), &error_fatal);
240 }
241 
242 static void ccw_init(MachineState *machine)
243 {
244     MachineClass *mc = MACHINE_GET_CLASS(machine);
245     int ret;
246     VirtualCssBus *css_bus;
247     DeviceState *dev;
248 
249     s390_sclp_init();
250     /* init memory + setup max page size. Required for the CPU model */
251     s390_memory_init(machine->ram);
252 
253     /* init CPUs (incl. CPU model) early so s390_has_feature() works */
254     s390_init_cpus(machine);
255 
256     /* Need CPU model to be determined before we can set up PV */
257     s390_pv_init(machine->cgs, &error_fatal);
258 
259     s390_flic_init();
260 
261     /* init the SIGP facility */
262     s390_init_sigp();
263 
264     /* create AP bridge and bus(es) */
265     s390_init_ap();
266 
267     /* get a BUS */
268     css_bus = virtual_css_bus_init();
269     s390_init_ipl_dev(machine->kernel_filename, machine->kernel_cmdline,
270                       machine->initrd_filename,
271                       machine->firmware ?: "s390-ccw.img",
272                       "s390-netboot.img", true);
273 
274     dev = qdev_new(TYPE_S390_PCI_HOST_BRIDGE);
275     object_property_add_child(qdev_get_machine(), TYPE_S390_PCI_HOST_BRIDGE,
276                               OBJECT(dev));
277     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
278 
279     /* register hypercalls */
280     virtio_ccw_register_hcalls();
281 
282     s390_enable_css_support(s390_cpu_addr2state(0));
283 
284     ret = css_create_css_image(VIRTUAL_CSSID, true);
285 
286     assert(ret == 0);
287     if (css_migration_enabled()) {
288         css_register_vmstate();
289     }
290 
291     /* Create VirtIO network adapters */
292     s390_create_virtio_net(BUS(css_bus), mc->default_nic);
293 
294     /* init consoles */
295     if (serial_hd(0)) {
296         s390_create_sclpconsole("sclpconsole", serial_hd(0));
297     }
298     if (serial_hd(1)) {
299         s390_create_sclpconsole("sclplmconsole", serial_hd(1));
300     }
301 
302     /* init the TOD clock */
303     s390_init_tod();
304 }
305 
306 static void s390_cpu_plug(HotplugHandler *hotplug_dev,
307                         DeviceState *dev, Error **errp)
308 {
309     MachineState *ms = MACHINE(hotplug_dev);
310     S390CPU *cpu = S390_CPU(dev);
311 
312     g_assert(!ms->possible_cpus->cpus[cpu->env.core_id].cpu);
313     ms->possible_cpus->cpus[cpu->env.core_id].cpu = OBJECT(dev);
314 
315     if (dev->hotplugged) {
316         raise_irq_cpu_hotplug();
317     }
318 }
319 
320 static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg)
321 {
322     S390CPU *cpu = S390_CPU(cs);
323 
324     s390_ipl_prepare_cpu(cpu);
325     s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
326 }
327 
328 static void s390_machine_unprotect(S390CcwMachineState *ms)
329 {
330     if (!s390_pv_vm_try_disable_async(ms)) {
331         s390_pv_vm_disable();
332     }
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     mc->default_nic = "virtio-net-ccw";
747 
748     object_class_property_add_bool(oc, "aes-key-wrap",
749                                    machine_get_aes_key_wrap,
750                                    machine_set_aes_key_wrap);
751     object_class_property_set_description(oc, "aes-key-wrap",
752             "enable/disable AES key wrapping using the CPACF wrapping key");
753 
754     object_class_property_add_bool(oc, "dea-key-wrap",
755                                    machine_get_dea_key_wrap,
756                                    machine_set_dea_key_wrap);
757     object_class_property_set_description(oc, "dea-key-wrap",
758             "enable/disable DEA key wrapping using the CPACF wrapping key");
759 
760     object_class_property_add(oc, "loadparm", "loadparm",
761                               machine_get_loadparm, machine_set_loadparm,
762                               NULL, NULL);
763     object_class_property_set_description(oc, "loadparm",
764             "Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted"
765             " to upper case) to pass to machine loader, boot manager,"
766             " and guest kernel");
767 }
768 
769 static inline void s390_machine_initfn(Object *obj)
770 {
771     S390CcwMachineState *ms = S390_CCW_MACHINE(obj);
772 
773     ms->aes_key_wrap = true;
774     ms->dea_key_wrap = true;
775 }
776 
777 static const TypeInfo ccw_machine_info = {
778     .name          = TYPE_S390_CCW_MACHINE,
779     .parent        = TYPE_MACHINE,
780     .abstract      = true,
781     .instance_size = sizeof(S390CcwMachineState),
782     .instance_init = s390_machine_initfn,
783     .class_size = sizeof(S390CcwMachineClass),
784     .class_init    = ccw_machine_class_init,
785     .interfaces = (InterfaceInfo[]) {
786         { TYPE_NMI },
787         { TYPE_HOTPLUG_HANDLER},
788         { }
789     },
790 };
791 
792 bool css_migration_enabled(void)
793 {
794     return get_machine_class()->css_migration_enabled;
795 }
796 
797 #define DEFINE_CCW_MACHINE(suffix, verstr, latest)                            \
798     static void ccw_machine_##suffix##_class_init(ObjectClass *oc,            \
799                                                   void *data)                 \
800     {                                                                         \
801         MachineClass *mc = MACHINE_CLASS(oc);                                 \
802         ccw_machine_##suffix##_class_options(mc);                             \
803         mc->desc = "Virtual s390x machine (version " verstr ")";              \
804         if (latest) {                                                         \
805             mc->alias = "s390-ccw-virtio";                                    \
806             mc->is_default = true;                                            \
807         }                                                                     \
808     }                                                                         \
809     static void ccw_machine_##suffix##_instance_init(Object *obj)             \
810     {                                                                         \
811         MachineState *machine = MACHINE(obj);                                 \
812         current_mc = S390_CCW_MACHINE_CLASS(MACHINE_GET_CLASS(machine));          \
813         ccw_machine_##suffix##_instance_options(machine);                     \
814     }                                                                         \
815     static const TypeInfo ccw_machine_##suffix##_info = {                     \
816         .name = MACHINE_TYPE_NAME("s390-ccw-virtio-" verstr),                 \
817         .parent = TYPE_S390_CCW_MACHINE,                                      \
818         .class_init = ccw_machine_##suffix##_class_init,                      \
819         .instance_init = ccw_machine_##suffix##_instance_init,                \
820     };                                                                        \
821     static void ccw_machine_register_##suffix(void)                           \
822     {                                                                         \
823         type_register_static(&ccw_machine_##suffix##_info);                   \
824     }                                                                         \
825     type_init(ccw_machine_register_##suffix)
826 
827 static void ccw_machine_8_2_instance_options(MachineState *machine)
828 {
829 }
830 
831 static void ccw_machine_8_2_class_options(MachineClass *mc)
832 {
833 }
834 DEFINE_CCW_MACHINE(8_2, "8.2", true);
835 
836 static void ccw_machine_8_1_instance_options(MachineState *machine)
837 {
838     ccw_machine_8_2_instance_options(machine);
839 }
840 
841 static void ccw_machine_8_1_class_options(MachineClass *mc)
842 {
843     ccw_machine_8_2_class_options(mc);
844     compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len);
845 }
846 DEFINE_CCW_MACHINE(8_1, "8.1", false);
847 
848 static void ccw_machine_8_0_instance_options(MachineState *machine)
849 {
850     ccw_machine_8_1_instance_options(machine);
851 }
852 
853 static void ccw_machine_8_0_class_options(MachineClass *mc)
854 {
855     ccw_machine_8_1_class_options(mc);
856     compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len);
857 }
858 DEFINE_CCW_MACHINE(8_0, "8.0", false);
859 
860 static void ccw_machine_7_2_instance_options(MachineState *machine)
861 {
862     ccw_machine_8_0_instance_options(machine);
863 }
864 
865 static void ccw_machine_7_2_class_options(MachineClass *mc)
866 {
867     ccw_machine_8_0_class_options(mc);
868     compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len);
869 }
870 DEFINE_CCW_MACHINE(7_2, "7.2", false);
871 
872 static void ccw_machine_7_1_instance_options(MachineState *machine)
873 {
874     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_1 };
875 
876     ccw_machine_7_2_instance_options(machine);
877     s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAIE);
878     s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat);
879 }
880 
881 static void ccw_machine_7_1_class_options(MachineClass *mc)
882 {
883     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
884     static GlobalProperty compat[] = {
885         { TYPE_S390_PCI_DEVICE, "interpret", "off", },
886         { TYPE_S390_PCI_DEVICE, "forwarding-assist", "off", },
887     };
888 
889     ccw_machine_7_2_class_options(mc);
890     compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len);
891     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
892     s390mc->max_threads = S390_MAX_CPUS;
893 }
894 DEFINE_CCW_MACHINE(7_1, "7.1", false);
895 
896 static void ccw_machine_7_0_instance_options(MachineState *machine)
897 {
898     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V7_0 };
899 
900     ccw_machine_7_1_instance_options(machine);
901     s390_set_qemu_cpu_model(0x8561, 15, 1, qemu_cpu_feat);
902 }
903 
904 static void ccw_machine_7_0_class_options(MachineClass *mc)
905 {
906     ccw_machine_7_1_class_options(mc);
907     compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len);
908 }
909 DEFINE_CCW_MACHINE(7_0, "7.0", false);
910 
911 static void ccw_machine_6_2_instance_options(MachineState *machine)
912 {
913     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_2 };
914 
915     ccw_machine_7_0_instance_options(machine);
916     s390_set_qemu_cpu_model(0x3906, 14, 2, qemu_cpu_feat);
917 }
918 
919 static void ccw_machine_6_2_class_options(MachineClass *mc)
920 {
921     ccw_machine_7_0_class_options(mc);
922     compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len);
923 }
924 DEFINE_CCW_MACHINE(6_2, "6.2", false);
925 
926 static void ccw_machine_6_1_instance_options(MachineState *machine)
927 {
928     ccw_machine_6_2_instance_options(machine);
929     s390_cpudef_featoff_greater(16, 1, S390_FEAT_NNPA);
930     s390_cpudef_featoff_greater(16, 1, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2);
931     s390_cpudef_featoff_greater(16, 1, S390_FEAT_BEAR_ENH);
932     s390_cpudef_featoff_greater(16, 1, S390_FEAT_RDP);
933     s390_cpudef_featoff_greater(16, 1, S390_FEAT_PAI);
934 }
935 
936 static void ccw_machine_6_1_class_options(MachineClass *mc)
937 {
938     ccw_machine_6_2_class_options(mc);
939     compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len);
940     mc->smp_props.prefer_sockets = true;
941 }
942 DEFINE_CCW_MACHINE(6_1, "6.1", false);
943 
944 static void ccw_machine_6_0_instance_options(MachineState *machine)
945 {
946     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V6_0 };
947 
948     ccw_machine_6_1_instance_options(machine);
949     s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
950 }
951 
952 static void ccw_machine_6_0_class_options(MachineClass *mc)
953 {
954     ccw_machine_6_1_class_options(mc);
955     compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len);
956 }
957 DEFINE_CCW_MACHINE(6_0, "6.0", false);
958 
959 static void ccw_machine_5_2_instance_options(MachineState *machine)
960 {
961     ccw_machine_6_0_instance_options(machine);
962 }
963 
964 static void ccw_machine_5_2_class_options(MachineClass *mc)
965 {
966     ccw_machine_6_0_class_options(mc);
967     compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len);
968 }
969 DEFINE_CCW_MACHINE(5_2, "5.2", false);
970 
971 static void ccw_machine_5_1_instance_options(MachineState *machine)
972 {
973     ccw_machine_5_2_instance_options(machine);
974 }
975 
976 static void ccw_machine_5_1_class_options(MachineClass *mc)
977 {
978     ccw_machine_5_2_class_options(mc);
979     compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len);
980 }
981 DEFINE_CCW_MACHINE(5_1, "5.1", false);
982 
983 static void ccw_machine_5_0_instance_options(MachineState *machine)
984 {
985     ccw_machine_5_1_instance_options(machine);
986 }
987 
988 static void ccw_machine_5_0_class_options(MachineClass *mc)
989 {
990     ccw_machine_5_1_class_options(mc);
991     compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
992 }
993 DEFINE_CCW_MACHINE(5_0, "5.0", false);
994 
995 static void ccw_machine_4_2_instance_options(MachineState *machine)
996 {
997     ccw_machine_5_0_instance_options(machine);
998 }
999 
1000 static void ccw_machine_4_2_class_options(MachineClass *mc)
1001 {
1002     ccw_machine_5_0_class_options(mc);
1003     mc->fixup_ram_size = s390_fixup_ram_size;
1004     compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
1005 }
1006 DEFINE_CCW_MACHINE(4_2, "4.2", false);
1007 
1008 static void ccw_machine_4_1_instance_options(MachineState *machine)
1009 {
1010     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_1 };
1011     ccw_machine_4_2_instance_options(machine);
1012     s390_set_qemu_cpu_model(0x2964, 13, 2, qemu_cpu_feat);
1013 }
1014 
1015 static void ccw_machine_4_1_class_options(MachineClass *mc)
1016 {
1017     ccw_machine_4_2_class_options(mc);
1018     compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
1019 }
1020 DEFINE_CCW_MACHINE(4_1, "4.1", false);
1021 
1022 static void ccw_machine_4_0_instance_options(MachineState *machine)
1023 {
1024     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V4_0 };
1025     ccw_machine_4_1_instance_options(machine);
1026     s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
1027 }
1028 
1029 static void ccw_machine_4_0_class_options(MachineClass *mc)
1030 {
1031     ccw_machine_4_1_class_options(mc);
1032     compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
1033 }
1034 DEFINE_CCW_MACHINE(4_0, "4.0", false);
1035 
1036 static void ccw_machine_3_1_instance_options(MachineState *machine)
1037 {
1038     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V3_1 };
1039     ccw_machine_4_0_instance_options(machine);
1040     s390_cpudef_featoff_greater(14, 1, S390_FEAT_MULTIPLE_EPOCH);
1041     s390_cpudef_group_featoff_greater(14, 1, S390_FEAT_GROUP_MULTIPLE_EPOCH_PTFF);
1042     s390_set_qemu_cpu_model(0x2827, 12, 2, qemu_cpu_feat);
1043 }
1044 
1045 static void ccw_machine_3_1_class_options(MachineClass *mc)
1046 {
1047     ccw_machine_4_0_class_options(mc);
1048     compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
1049 }
1050 DEFINE_CCW_MACHINE(3_1, "3.1", false);
1051 
1052 static void ccw_machine_3_0_instance_options(MachineState *machine)
1053 {
1054     ccw_machine_3_1_instance_options(machine);
1055 }
1056 
1057 static void ccw_machine_3_0_class_options(MachineClass *mc)
1058 {
1059     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1060 
1061     s390mc->hpage_1m_allowed = false;
1062     ccw_machine_3_1_class_options(mc);
1063     compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
1064 }
1065 DEFINE_CCW_MACHINE(3_0, "3.0", false);
1066 
1067 static void ccw_machine_2_12_instance_options(MachineState *machine)
1068 {
1069     ccw_machine_3_0_instance_options(machine);
1070     s390_cpudef_featoff_greater(11, 1, S390_FEAT_PPA15);
1071     s390_cpudef_featoff_greater(11, 1, S390_FEAT_BPB);
1072 }
1073 
1074 static void ccw_machine_2_12_class_options(MachineClass *mc)
1075 {
1076     ccw_machine_3_0_class_options(mc);
1077     compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
1078 }
1079 DEFINE_CCW_MACHINE(2_12, "2.12", false);
1080 
1081 static void ccw_machine_2_11_instance_options(MachineState *machine)
1082 {
1083     static const S390FeatInit qemu_cpu_feat = { S390_FEAT_LIST_QEMU_V2_11 };
1084     ccw_machine_2_12_instance_options(machine);
1085 
1086     /* before 2.12 we emulated the very first z900 */
1087     s390_set_qemu_cpu_model(0x2064, 7, 1, qemu_cpu_feat);
1088 }
1089 
1090 static void ccw_machine_2_11_class_options(MachineClass *mc)
1091 {
1092     static GlobalProperty compat[] = {
1093         { TYPE_SCLP_EVENT_FACILITY, "allow_all_mask_sizes", "off", },
1094     };
1095 
1096     ccw_machine_2_12_class_options(mc);
1097     compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
1098     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1099 }
1100 DEFINE_CCW_MACHINE(2_11, "2.11", false);
1101 
1102 static void ccw_machine_2_10_instance_options(MachineState *machine)
1103 {
1104     ccw_machine_2_11_instance_options(machine);
1105 }
1106 
1107 static void ccw_machine_2_10_class_options(MachineClass *mc)
1108 {
1109     ccw_machine_2_11_class_options(mc);
1110     compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
1111 }
1112 DEFINE_CCW_MACHINE(2_10, "2.10", false);
1113 
1114 static void ccw_machine_2_9_instance_options(MachineState *machine)
1115 {
1116     ccw_machine_2_10_instance_options(machine);
1117     s390_cpudef_featoff_greater(12, 1, S390_FEAT_ESOP);
1118     s390_cpudef_featoff_greater(12, 1, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2);
1119     s390_cpudef_featoff_greater(12, 1, S390_FEAT_ZPCI);
1120     s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_INT_SUPPRESSION);
1121     s390_cpudef_featoff_greater(12, 1, S390_FEAT_ADAPTER_EVENT_NOTIFICATION);
1122 }
1123 
1124 static void ccw_machine_2_9_class_options(MachineClass *mc)
1125 {
1126     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1127     static GlobalProperty compat[] = {
1128         { TYPE_S390_STATTRIB, "migration-enabled", "off", },
1129     };
1130 
1131     ccw_machine_2_10_class_options(mc);
1132     compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
1133     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1134     s390mc->css_migration_enabled = false;
1135 }
1136 DEFINE_CCW_MACHINE(2_9, "2.9", false);
1137 
1138 static void ccw_machine_2_8_instance_options(MachineState *machine)
1139 {
1140     ccw_machine_2_9_instance_options(machine);
1141 }
1142 
1143 static void ccw_machine_2_8_class_options(MachineClass *mc)
1144 {
1145     static GlobalProperty compat[] = {
1146         { TYPE_S390_FLIC_COMMON, "adapter_routes_max_batch", "64", },
1147     };
1148 
1149     ccw_machine_2_9_class_options(mc);
1150     compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
1151     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1152 }
1153 DEFINE_CCW_MACHINE(2_8, "2.8", false);
1154 
1155 static void ccw_machine_2_7_instance_options(MachineState *machine)
1156 {
1157     ccw_machine_2_8_instance_options(machine);
1158 }
1159 
1160 static void ccw_machine_2_7_class_options(MachineClass *mc)
1161 {
1162     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1163 
1164     s390mc->cpu_model_allowed = false;
1165     ccw_machine_2_8_class_options(mc);
1166     compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
1167 }
1168 DEFINE_CCW_MACHINE(2_7, "2.7", false);
1169 
1170 static void ccw_machine_2_6_instance_options(MachineState *machine)
1171 {
1172     ccw_machine_2_7_instance_options(machine);
1173 }
1174 
1175 static void ccw_machine_2_6_class_options(MachineClass *mc)
1176 {
1177     S390CcwMachineClass *s390mc = S390_CCW_MACHINE_CLASS(mc);
1178     static GlobalProperty compat[] = {
1179         { TYPE_S390_IPL, "iplbext_migration", "off", },
1180          { TYPE_VIRTUAL_CSS_BRIDGE, "css_dev_path", "off", },
1181     };
1182 
1183     s390mc->ri_allowed = false;
1184     ccw_machine_2_7_class_options(mc);
1185     compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
1186     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1187 }
1188 DEFINE_CCW_MACHINE(2_6, "2.6", false);
1189 
1190 static void ccw_machine_2_5_instance_options(MachineState *machine)
1191 {
1192     ccw_machine_2_6_instance_options(machine);
1193 }
1194 
1195 static void ccw_machine_2_5_class_options(MachineClass *mc)
1196 {
1197     ccw_machine_2_6_class_options(mc);
1198     compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len);
1199 }
1200 DEFINE_CCW_MACHINE(2_5, "2.5", false);
1201 
1202 static void ccw_machine_2_4_instance_options(MachineState *machine)
1203 {
1204     ccw_machine_2_5_instance_options(machine);
1205 }
1206 
1207 static void ccw_machine_2_4_class_options(MachineClass *mc)
1208 {
1209     static GlobalProperty compat[] = {
1210         { TYPE_S390_SKEYS, "migration-enabled", "off", },
1211         { "virtio-blk-ccw", "max_revision", "0", },
1212         { "virtio-balloon-ccw", "max_revision", "0", },
1213         { "virtio-serial-ccw", "max_revision", "0", },
1214         { "virtio-9p-ccw", "max_revision", "0", },
1215         { "virtio-rng-ccw", "max_revision", "0", },
1216         { "virtio-net-ccw", "max_revision", "0", },
1217         { "virtio-scsi-ccw", "max_revision", "0", },
1218         { "vhost-scsi-ccw", "max_revision", "0", },
1219     };
1220 
1221     ccw_machine_2_5_class_options(mc);
1222     compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len);
1223     compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1224 }
1225 DEFINE_CCW_MACHINE(2_4, "2.4", false);
1226 
1227 static void ccw_machine_register_types(void)
1228 {
1229     type_register_static(&ccw_machine_info);
1230 }
1231 
1232 type_init(ccw_machine_register_types)
1233