cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

s390-virtio-ccw.c (34716B)


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