Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/i386/microvm.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2018 Intel Corporation
3
 * Copyright (c) 2019 Red Hat, Inc.
4
 *
5
 * This program is free software; you can redistribute it and/or modify it
6
 * under the terms and conditions of the GNU General Public License,
7
 * version 2 or later, as published by the Free Software Foundation.
8
 *
9
 * This program is distributed in the hope it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12
 * more details.
13
 *
14
 * You should have received a copy of the GNU General Public License along with
15
 * this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include "qemu/osdep.h"
19
#include "qemu/error-report.h"
20
#include "qemu/cutils.h"
21
#include "qemu/units.h"
22
#include "qapi/error.h"
23
#include "qapi/visitor.h"
24
#include "qapi/qapi-visit-common.h"
25
#include "system/system.h"
26
#include "system/cpus.h"
27
#include "system/numa.h"
28
#include "system/reset.h"
29
#include "system/runstate.h"
30
#include "acpi-microvm.h"
31
#include "microvm-dt.h"
32
33
#include "hw/core/loader.h"
34
#include "hw/core/irq.h"
35
#include "hw/i386/kvm/clock.h"
36
#include "hw/i386/microvm.h"
37
#include "hw/i386/x86.h"
38
#include "target/i386/cpu.h"
39
#include "hw/intc/i8259.h"
40
#include "hw/timer/i8254.h"
41
#include "hw/rtc/mc146818rtc.h"
42
#include "hw/char/serial-isa.h"
43
#include "hw/display/ramfb.h"
44
#include "hw/i386/topology.h"
45
#include "hw/i386/e820_memory_layout.h"
46
#include "hw/i386/fw_cfg.h"
47
#include "hw/virtio/virtio-mmio.h"
48
#include "hw/acpi/acpi.h"
49
#include "hw/acpi/generic_event_device.h"
50
#include "hw/pci-host/gpex.h"
51
#include "hw/usb/xhci.h"
52
#include "hw/vfio/types.h"
53
54
#include "elf.h"
55
#include "kvm/kvm_i386.h"
56
#include "hw/xen/start_info.h"
57
58
0
#define MICROVM_QBOOT_FILENAME "qboot.rom"
59
0
#define MICROVM_BIOS_FILENAME  "bios-microvm.bin"
60
61
static void microvm_set_rtc(MicrovmMachineState *mms, MC146818RtcState *s)
62
0
{
63
0
    X86MachineState *x86ms = X86_MACHINE(mms);
64
0
    int val;
65
66
0
    val = MIN(x86ms->below_4g_mem_size / KiB, 640);
67
0
    mc146818rtc_set_cmos_data(s, 0x15, val);
68
0
    mc146818rtc_set_cmos_data(s, 0x16, val >> 8);
69
    /* extended memory (next 64MiB) */
70
0
    if (x86ms->below_4g_mem_size > 1 * MiB) {
71
0
        val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB;
72
0
    } else {
73
0
        val = 0;
74
0
    }
75
0
    if (val > 65535) {
76
0
        val = 65535;
77
0
    }
78
0
    mc146818rtc_set_cmos_data(s, 0x17, val);
79
0
    mc146818rtc_set_cmos_data(s, 0x18, val >> 8);
80
0
    mc146818rtc_set_cmos_data(s, 0x30, val);
81
0
    mc146818rtc_set_cmos_data(s, 0x31, val >> 8);
82
    /* memory between 16MiB and 4GiB */
83
0
    if (x86ms->below_4g_mem_size > 16 * MiB) {
84
0
        val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
85
0
    } else {
86
0
        val = 0;
87
0
    }
88
0
    if (val > 65535) {
89
0
        val = 65535;
90
0
    }
91
0
    mc146818rtc_set_cmos_data(s, 0x34, val);
92
0
    mc146818rtc_set_cmos_data(s, 0x35, val >> 8);
93
    /* memory above 4GiB */
94
0
    val = x86ms->above_4g_mem_size / 65536;
95
0
    mc146818rtc_set_cmos_data(s, 0x5b, val);
96
0
    mc146818rtc_set_cmos_data(s, 0x5c, val >> 8);
97
0
    mc146818rtc_set_cmos_data(s, 0x5d, val >> 16);
98
0
}
99
100
static void create_gpex(MicrovmMachineState *mms)
101
0
{
102
0
    X86MachineState *x86ms = X86_MACHINE(mms);
103
0
    MemoryRegion *mmio32_alias;
104
0
    MemoryRegion *mmio64_alias;
105
0
    MemoryRegion *mmio_reg;
106
0
    MemoryRegion *ecam_alias;
107
0
    MemoryRegion *ecam_reg;
108
0
    DeviceState *dev;
109
0
    int i;
110
111
0
    dev = qdev_new(TYPE_GPEX_HOST);
112
0
    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
113
114
    /* Map only the first size_ecam bytes of ECAM space */
115
0
    ecam_alias = g_new0(MemoryRegion, 1);
116
0
    ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
117
0
    memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
118
0
                             ecam_reg, 0, mms->gpex.ecam.size);
119
0
    memory_region_add_subregion(get_system_memory(),
120
0
                                mms->gpex.ecam.base, ecam_alias);
121
122
    /* Map the MMIO window into system address space so as to expose
123
     * the section of PCI MMIO space which starts at the same base address
124
     * (ie 1:1 mapping for that part of PCI MMIO space visible through
125
     * the window).
126
     */
127
0
    mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
128
0
    if (mms->gpex.mmio32.size) {
129
0
        mmio32_alias = g_new0(MemoryRegion, 1);
130
0
        memory_region_init_alias(mmio32_alias, OBJECT(dev), "pcie-mmio32", mmio_reg,
131
0
                                 mms->gpex.mmio32.base, mms->gpex.mmio32.size);
132
0
        memory_region_add_subregion(get_system_memory(),
133
0
                                    mms->gpex.mmio32.base, mmio32_alias);
134
0
    }
135
0
    if (mms->gpex.mmio64.size) {
136
0
        mmio64_alias = g_new0(MemoryRegion, 1);
137
0
        memory_region_init_alias(mmio64_alias, OBJECT(dev), "pcie-mmio64", mmio_reg,
138
0
                                 mms->gpex.mmio64.base, mms->gpex.mmio64.size);
139
0
        memory_region_add_subregion(get_system_memory(),
140
0
                                    mms->gpex.mmio64.base, mmio64_alias);
141
0
    }
142
143
0
    for (i = 0; i < PCI_NUM_PINS; i++) {
144
0
        sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
145
0
                           x86ms->gsi[mms->gpex.irq + i]);
146
0
    }
147
0
}
148
149
static int microvm_ioapics(MicrovmMachineState *mms)
150
0
{
151
0
    if (!x86_machine_is_acpi_enabled(X86_MACHINE(mms))) {
152
0
        return 1;
153
0
    }
154
0
    if (mms->ioapic2 == ON_OFF_AUTO_OFF) {
155
0
        return 1;
156
0
    }
157
0
    return 2;
158
0
}
159
160
static void microvm_devices_init(MicrovmMachineState *mms)
161
0
{
162
0
    X86MachineState *x86ms = X86_MACHINE(mms);
163
0
    ISABus *isa_bus;
164
0
    GSIState *gsi_state;
165
0
    int ioapics;
166
0
    int i;
167
168
    /* Core components */
169
0
    ioapics = microvm_ioapics(mms);
170
0
    gsi_state = g_malloc0(sizeof(*gsi_state));
171
0
    x86ms->gsi = qemu_allocate_irqs(gsi_handler, gsi_state,
172
0
                                    IOAPIC_NUM_PINS * ioapics);
173
174
0
    isa_bus = isa_bus_new(NULL, get_system_memory(), get_system_io(),
175
0
                          &error_abort);
176
0
    isa_bus_register_input_irqs(isa_bus, x86ms->gsi);
177
178
0
    ioapic_init_gsi(gsi_state, OBJECT(mms));
179
0
    if (ioapics > 1) {
180
0
        x86ms->ioapic2 = ioapic_init_secondary(gsi_state);
181
0
    }
182
183
0
    if (kvm_enabled()) {
184
0
        kvmclock_create(true);
185
0
    }
186
187
0
    mms->virtio_irq_base = 5;
188
0
    mms->virtio_num_transports = 8;
189
0
    if (x86ms->ioapic2) {
190
0
        mms->pcie_irq_base = 16;    /* 16 -> 19 */
191
        /* use second ioapic (24 -> 47) for virtio-mmio irq lines */
192
0
        mms->virtio_irq_base = IO_APIC_SECONDARY_IRQBASE;
193
0
        mms->virtio_num_transports = IOAPIC_NUM_PINS;
194
0
    } else if (x86_machine_is_acpi_enabled(x86ms)) {
195
0
        mms->pcie_irq_base = 12;    /* 12 -> 15 */
196
0
        mms->virtio_irq_base = 16;  /* 16 -> 23 */
197
0
    }
198
199
0
    for (i = 0; i < mms->virtio_num_transports; i++) {
200
0
        sysbus_create_simple("virtio-mmio",
201
0
                             VIRTIO_MMIO_BASE + i * 512,
202
0
                             x86ms->gsi[mms->virtio_irq_base + i]);
203
0
    }
204
205
    /* Optional and legacy devices */
206
0
    if (x86_machine_is_acpi_enabled(x86ms)) {
207
0
        DeviceState *dev = qdev_new(TYPE_ACPI_GED);
208
0
        qdev_prop_set_uint32(dev, "ged-event", ACPI_GED_PWR_DOWN_EVT);
209
0
        sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
210
0
        sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, GED_MMIO_BASE);
211
        /* sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, GED_MMIO_BASE_MEMHP); */
212
0
        sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, GED_MMIO_BASE_REGS);
213
0
        sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
214
0
                           x86ms->gsi[GED_MMIO_IRQ]);
215
0
        x86ms->acpi_dev = HOTPLUG_HANDLER(dev);
216
0
    }
217
218
0
    if (x86_machine_is_acpi_enabled(x86ms) && machine_usb(MACHINE(mms))) {
219
0
        DeviceState *dev = qdev_new(TYPE_XHCI_SYSBUS);
220
0
        qdev_prop_set_uint32(dev, "intrs", 1);
221
0
        qdev_prop_set_uint32(dev, "slots", XHCI_MAXSLOTS);
222
0
        qdev_prop_set_uint32(dev, "p2", 8);
223
0
        qdev_prop_set_uint32(dev, "p3", 8);
224
0
        sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
225
0
        sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, MICROVM_XHCI_BASE);
226
0
        sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
227
0
                           x86ms->gsi[MICROVM_XHCI_IRQ]);
228
0
    }
229
230
0
    if (x86_machine_is_acpi_enabled(x86ms) && mms->pcie == ON_OFF_AUTO_ON) {
231
        /* use topmost 25% of the address space available */
232
0
        hwaddr phys_size = (hwaddr)1 << X86_CPU(first_cpu)->phys_bits;
233
0
        if (phys_size > 0x1000000ll) {
234
0
            mms->gpex.mmio64.size = phys_size / 4;
235
0
            mms->gpex.mmio64.base = phys_size - mms->gpex.mmio64.size;
236
0
        }
237
0
        mms->gpex.mmio32.base = PCIE_MMIO_BASE;
238
0
        mms->gpex.mmio32.size = PCIE_MMIO_SIZE;
239
0
        mms->gpex.ecam.base   = PCIE_ECAM_BASE;
240
0
        mms->gpex.ecam.size   = PCIE_ECAM_SIZE;
241
0
        mms->gpex.irq         = mms->pcie_irq_base;
242
0
        create_gpex(mms);
243
0
        x86ms->pci_irq_mask = ((1 << (mms->pcie_irq_base + 0)) |
244
0
                               (1 << (mms->pcie_irq_base + 1)) |
245
0
                               (1 << (mms->pcie_irq_base + 2)) |
246
0
                               (1 << (mms->pcie_irq_base + 3)));
247
0
    } else {
248
0
        x86ms->pci_irq_mask = 0;
249
0
    }
250
251
0
    if (x86ms->pic == ON_OFF_AUTO_ON || x86ms->pic == ON_OFF_AUTO_AUTO) {
252
0
        qemu_irq *i8259;
253
254
0
        i8259 = i8259_init(isa_bus, x86_allocate_cpu_irq());
255
0
        for (i = 0; i < ISA_NUM_IRQS; i++) {
256
0
            gsi_state->i8259_irq[i] = i8259[i];
257
0
        }
258
0
        g_free(i8259);
259
0
    }
260
261
0
    if (x86ms->pit == ON_OFF_AUTO_ON || x86ms->pit == ON_OFF_AUTO_AUTO) {
262
0
        if (kvm_pit_in_kernel()) {
263
0
            kvm_pit_init(isa_bus, 0x40);
264
0
        } else {
265
0
            i8254_pit_init(isa_bus, 0x40, 0, NULL);
266
0
        }
267
0
    }
268
269
0
    if (mms->rtc == ON_OFF_AUTO_ON ||
270
0
        (mms->rtc == ON_OFF_AUTO_AUTO && !kvm_enabled())) {
271
0
        microvm_set_rtc(mms, mc146818_rtc_init(isa_bus, 2000, NULL));
272
0
    }
273
274
0
    if (mms->isa_serial) {
275
0
        serial_hds_isa_init(isa_bus, 0, 1);
276
0
    }
277
278
0
    if (!x86ms->igvm) {
279
0
        const char *default_firmware = x86_machine_is_acpi_enabled(x86ms)
280
0
                ? MICROVM_BIOS_FILENAME
281
0
                : MICROVM_QBOOT_FILENAME;
282
0
        x86_bios_rom_init(x86ms, default_firmware, get_system_memory(), true);
283
0
    }
284
0
}
285
286
static void microvm_memory_init(MicrovmMachineState *mms)
287
0
{
288
0
    MicrovmMachineClass *mmc = MICROVM_MACHINE_GET_CLASS(mms);
289
0
    MachineState *machine = MACHINE(mms);
290
0
    X86MachineState *x86ms = X86_MACHINE(mms);
291
0
    MemoryRegion *ram_below_4g, *ram_above_4g;
292
0
    MemoryRegion *system_memory = get_system_memory();
293
0
    FWCfgState *fw_cfg;
294
0
    ram_addr_t lowmem = 0xc0000000; /* 3G */
295
0
    int i;
296
297
0
    if (machine->ram_size > lowmem) {
298
0
        x86ms->above_4g_mem_size = machine->ram_size - lowmem;
299
0
        x86ms->below_4g_mem_size = lowmem;
300
0
    } else {
301
0
        x86ms->above_4g_mem_size = 0;
302
0
        x86ms->below_4g_mem_size = machine->ram_size;
303
0
    }
304
305
0
    ram_below_4g = g_malloc(sizeof(*ram_below_4g));
306
0
    memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
307
0
                             0, x86ms->below_4g_mem_size);
308
0
    memory_region_add_subregion(system_memory, 0, ram_below_4g);
309
310
0
    e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
311
312
0
    if (x86ms->above_4g_mem_size > 0) {
313
0
        ram_above_4g = g_malloc(sizeof(*ram_above_4g));
314
0
        memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
315
0
                                 machine->ram,
316
0
                                 x86ms->below_4g_mem_size,
317
0
                                 x86ms->above_4g_mem_size);
318
0
        memory_region_add_subregion(system_memory, 0x100000000ULL,
319
0
                                    ram_above_4g);
320
0
        e820_add_entry(0x100000000ULL, x86ms->above_4g_mem_size, E820_RAM);
321
0
    }
322
323
0
    fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, &address_space_memory);
324
325
0
    fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, machine->smp.cpus);
326
0
    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, machine->smp.max_cpus);
327
0
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size);
328
0
    fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1);
329
330
0
    rom_set_fw(fw_cfg);
331
332
0
    if (machine->kernel_filename != NULL) {
333
0
        mmc->x86_load_linux(x86ms, fw_cfg, 0);
334
0
    }
335
336
0
    if (mms->option_roms) {
337
0
        for (i = 0; i < nb_option_roms; i++) {
338
0
            rom_add_option(option_rom[i].name, option_rom[i].bootindex);
339
0
        }
340
0
    }
341
342
0
    x86ms->fw_cfg = fw_cfg;
343
0
    x86ms->ioapic_as = &address_space_memory;
344
0
}
345
346
static gchar *microvm_get_mmio_cmdline(gchar *name, uint32_t virtio_irq_base)
347
0
{
348
0
    gchar *cmdline;
349
0
    gchar *separator;
350
0
    long int index;
351
0
    int ret;
352
353
0
    separator = g_strrstr(name, ".");
354
0
    if (!separator) {
355
0
        return NULL;
356
0
    }
357
358
0
    if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) {
359
0
        return NULL;
360
0
    }
361
362
0
    cmdline = g_malloc0(VIRTIO_CMDLINE_MAXLEN);
363
0
    ret = g_snprintf(cmdline, VIRTIO_CMDLINE_MAXLEN,
364
0
                     " virtio_mmio.device=512@0x%lx:%ld",
365
0
                     VIRTIO_MMIO_BASE + index * 512,
366
0
                     virtio_irq_base + index);
367
0
    if (ret < 0 || ret >= VIRTIO_CMDLINE_MAXLEN) {
368
0
        g_free(cmdline);
369
0
        return NULL;
370
0
    }
371
372
0
    return cmdline;
373
0
}
374
375
static void microvm_fix_kernel_cmdline(MachineState *machine)
376
0
{
377
0
    X86MachineState *x86ms = X86_MACHINE(machine);
378
0
    MicrovmMachineState *mms = MICROVM_MACHINE(machine);
379
0
    BusState *bus;
380
0
    BusChild *kid;
381
0
    char *cmdline;
382
383
    /*
384
     * Find MMIO transports with attached devices, and add them to the kernel
385
     * command line.
386
     *
387
     * Yes, this is a hack, but one that heavily improves the UX without
388
     * introducing any significant issues.
389
     */
390
0
    cmdline = g_strdup(machine->kernel_cmdline);
391
0
    bus = sysbus_get_default();
392
0
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
393
0
        DeviceState *dev = kid->child;
394
395
0
        if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MMIO)) {
396
0
            VirtIOMMIOProxy *mmio = VIRTIO_MMIO(OBJECT(dev));
397
0
            VirtioBusState *mmio_virtio_bus = &mmio->bus;
398
0
            BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
399
400
0
            if (!QTAILQ_EMPTY(&mmio_bus->children)) {
401
0
                gchar *mmio_cmdline = microvm_get_mmio_cmdline
402
0
                    (mmio_bus->name, mms->virtio_irq_base);
403
0
                if (mmio_cmdline) {
404
0
                    char *newcmd = g_strjoin(NULL, cmdline, mmio_cmdline, NULL);
405
0
                    g_free(mmio_cmdline);
406
0
                    g_free(cmdline);
407
0
                    cmdline = newcmd;
408
0
                }
409
0
            }
410
0
        }
411
0
    }
412
413
0
    fw_cfg_modify_i32(x86ms->fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(cmdline) + 1);
414
0
    fw_cfg_modify_string(x86ms->fw_cfg, FW_CFG_CMDLINE_DATA, cmdline);
415
416
0
    g_free(cmdline);
417
0
}
418
419
static void microvm_device_pre_plug_cb(const HotplugHandler *hotplug_dev,
420
                                       DeviceState *dev, Error **errp)
421
0
{
422
0
    X86CPU *cpu = X86_CPU(dev);
423
424
0
    cpu->host_phys_bits = true; /* need reliable phys-bits */
425
0
    x86_cpu_pre_plug(hotplug_dev, dev, errp);
426
0
}
427
428
static void microvm_device_plug_cb(const HotplugHandler *hotplug_dev,
429
                                   DeviceState *dev, Error **errp)
430
0
{
431
0
    x86_cpu_plug(hotplug_dev, dev, errp);
432
0
}
433
434
static void microvm_device_unplug_request_cb(const HotplugHandler *hotplug_dev,
435
                                             DeviceState *dev, Error **errp)
436
0
{
437
0
    error_setg(errp, "unplug not supported by microvm");
438
0
}
439
440
static void microvm_device_unplug_cb(const HotplugHandler *hotplug_dev,
441
                                     DeviceState *dev, Error **errp)
442
0
{
443
0
    error_setg(errp, "unplug not supported by microvm");
444
0
}
445
446
static const HotplugHandler *microvm_get_hotplug_handler(MachineState *machine,
447
                                                         DeviceState *dev)
448
0
{
449
0
    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
450
0
        return HOTPLUG_HANDLER(machine);
451
0
    }
452
0
    return NULL;
453
0
}
454
455
static void microvm_machine_done(Notifier *notifier, void *data)
456
0
{
457
0
    MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState,
458
0
                                            machine_done);
459
0
    X86MachineState *x86ms = X86_MACHINE(mms);
460
461
0
    acpi_setup_microvm(mms);
462
0
    dt_setup_microvm(mms);
463
0
    fw_cfg_add_e820(x86ms->fw_cfg);
464
0
}
465
466
static void microvm_powerdown_req(Notifier *notifier, void *data)
467
0
{
468
0
    MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState,
469
0
                                            powerdown_req);
470
0
    X86MachineState *x86ms = X86_MACHINE(mms);
471
472
0
    if (x86ms->acpi_dev) {
473
0
        Object *obj = OBJECT(x86ms->acpi_dev);
474
0
        AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
475
0
        adevc->send_event(ACPI_DEVICE_IF(x86ms->acpi_dev),
476
0
                          ACPI_POWER_DOWN_STATUS);
477
0
    }
478
0
}
479
480
static void microvm_machine_state_init(MachineState *machine)
481
0
{
482
0
    MicrovmMachineState *mms = MICROVM_MACHINE(machine);
483
0
    X86MachineState *x86ms = X86_MACHINE(machine);
484
485
    /* State */
486
0
    mms->kernel_cmdline_fixed = false;
487
488
0
    mms->machine_done.notify = microvm_machine_done;
489
0
    qemu_add_machine_init_done_notifier(&mms->machine_done);
490
0
    mms->powerdown_req.notify = microvm_powerdown_req;
491
0
    qemu_register_powerdown_notifier(&mms->powerdown_req);
492
493
0
    microvm_memory_init(mms);
494
495
0
    x86_cpus_init(x86ms, CPU_VERSION_LATEST);
496
497
0
    microvm_devices_init(mms);
498
0
}
499
500
static void microvm_machine_reset(MachineState *machine, ResetType type)
501
0
{
502
0
    MicrovmMachineState *mms = MICROVM_MACHINE(machine);
503
0
    CPUState *cs;
504
0
    X86CPU *cpu;
505
506
0
    if (!x86_machine_is_acpi_enabled(X86_MACHINE(machine)) &&
507
0
        machine->kernel_filename != NULL &&
508
0
        mms->auto_kernel_cmdline && !mms->kernel_cmdline_fixed) {
509
0
        microvm_fix_kernel_cmdline(machine);
510
0
        mms->kernel_cmdline_fixed = true;
511
0
    }
512
513
0
    qemu_devices_reset(type);
514
515
0
    CPU_FOREACH(cs) {
516
0
        cpu = X86_CPU(cs);
517
518
0
        x86_cpu_after_reset(cpu);
519
0
    }
520
0
}
521
522
static void microvm_machine_get_rtc(Object *obj, Visitor *v, const char *name,
523
                                    void *opaque, Error **errp)
524
0
{
525
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
526
0
    OnOffAuto rtc = mms->rtc;
527
528
0
    visit_type_OnOffAuto(v, name, &rtc, errp);
529
0
}
530
531
static void microvm_machine_set_rtc(Object *obj, Visitor *v, const char *name,
532
                                    void *opaque, Error **errp)
533
0
{
534
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
535
536
0
    visit_type_OnOffAuto(v, name, &mms->rtc, errp);
537
0
}
538
539
static void microvm_machine_get_pcie(Object *obj, Visitor *v, const char *name,
540
                                     void *opaque, Error **errp)
541
0
{
542
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
543
0
    OnOffAuto pcie = mms->pcie;
544
545
0
    visit_type_OnOffAuto(v, name, &pcie, errp);
546
0
}
547
548
static void microvm_machine_set_pcie(Object *obj, Visitor *v, const char *name,
549
                                     void *opaque, Error **errp)
550
0
{
551
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
552
553
0
    visit_type_OnOffAuto(v, name, &mms->pcie, errp);
554
0
}
555
556
static void microvm_machine_get_ioapic2(Object *obj, Visitor *v, const char *name,
557
                                        void *opaque, Error **errp)
558
0
{
559
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
560
0
    OnOffAuto ioapic2 = mms->ioapic2;
561
562
0
    visit_type_OnOffAuto(v, name, &ioapic2, errp);
563
0
}
564
565
static void microvm_machine_set_ioapic2(Object *obj, Visitor *v, const char *name,
566
                                        void *opaque, Error **errp)
567
0
{
568
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
569
570
0
    visit_type_OnOffAuto(v, name, &mms->ioapic2, errp);
571
0
}
572
573
static bool microvm_machine_get_isa_serial(Object *obj, Error **errp)
574
0
{
575
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
576
577
0
    return mms->isa_serial;
578
0
}
579
580
static void microvm_machine_set_isa_serial(Object *obj, bool value,
581
                                           Error **errp)
582
0
{
583
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
584
585
0
    mms->isa_serial = value;
586
0
}
587
588
static bool microvm_machine_get_option_roms(Object *obj, Error **errp)
589
0
{
590
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
591
592
0
    return mms->option_roms;
593
0
}
594
595
static void microvm_machine_set_option_roms(Object *obj, bool value,
596
                                            Error **errp)
597
0
{
598
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
599
600
0
    mms->option_roms = value;
601
0
}
602
603
static bool microvm_machine_get_auto_kernel_cmdline(Object *obj, Error **errp)
604
0
{
605
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
606
607
0
    return mms->auto_kernel_cmdline;
608
0
}
609
610
static void microvm_machine_set_auto_kernel_cmdline(Object *obj, bool value,
611
                                                    Error **errp)
612
0
{
613
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
614
615
0
    mms->auto_kernel_cmdline = value;
616
0
}
617
618
static void microvm_machine_initfn(Object *obj)
619
0
{
620
0
    MicrovmMachineState *mms = MICROVM_MACHINE(obj);
621
622
    /* Configuration */
623
0
    mms->rtc = ON_OFF_AUTO_AUTO;
624
0
    mms->pcie = ON_OFF_AUTO_AUTO;
625
0
    mms->ioapic2 = ON_OFF_AUTO_AUTO;
626
0
    mms->isa_serial = true;
627
0
    mms->option_roms = true;
628
0
    mms->auto_kernel_cmdline = true;
629
0
}
630
631
GlobalProperty microvm_properties[] = {
632
    /*
633
     * pcie host bridge (gpex) on microvm has no io address window,
634
     * so reserving io space is not going to work.  Turn it off.
635
     */
636
    { "pcie-root-port", "io-reserve", "0" },
637
    { TYPE_RAMFB_DEVICE, "use-legacy-x86-rom", "true" },
638
    { TYPE_VFIO_PCI_NOHOTPLUG, "use-legacy-x86-rom", "true" },
639
};
640
641
static void microvm_class_init(ObjectClass *oc, const void *data)
642
61
{
643
61
    MicrovmMachineClass *mmc = MICROVM_MACHINE_CLASS(oc);
644
61
    MachineClass *mc = MACHINE_CLASS(oc);
645
61
    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
646
647
61
    mmc->x86_load_linux = x86_load_linux;
648
649
61
    mc->init = microvm_machine_state_init;
650
651
61
    mc->family = "microvm_i386";
652
61
    mc->desc = "microvm (i386)";
653
61
    mc->units_per_default_bus = 1;
654
61
    mc->no_floppy = 1;
655
61
    mc->max_cpus = 288;
656
61
    mc->has_hotpluggable_cpus = false;
657
61
    mc->auto_enable_numa_with_memhp = false;
658
61
    mc->auto_enable_numa_with_memdev = false;
659
61
    mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
660
61
    mc->nvdimm_supported = false;
661
61
    mc->default_ram_id = "microvm.ram";
662
663
    /* Avoid relying too much on kernel components */
664
61
    mc->default_kernel_irqchip_split = true;
665
666
    /* Machine class handlers */
667
61
    mc->reset = microvm_machine_reset;
668
669
    /* hotplug (for cpu coldplug) */
670
61
    mc->get_hotplug_handler = microvm_get_hotplug_handler;
671
61
    hc->pre_plug = microvm_device_pre_plug_cb;
672
61
    hc->plug = microvm_device_plug_cb;
673
61
    hc->unplug_request = microvm_device_unplug_request_cb;
674
61
    hc->unplug = microvm_device_unplug_cb;
675
676
61
    object_class_property_add(oc, MICROVM_MACHINE_RTC, "OnOffAuto",
677
61
                              microvm_machine_get_rtc,
678
61
                              microvm_machine_set_rtc,
679
61
                              NULL, NULL);
680
61
    object_class_property_set_description(oc, MICROVM_MACHINE_RTC,
681
61
        "Enable MC146818 RTC");
682
683
61
    object_class_property_add(oc, MICROVM_MACHINE_PCIE, "OnOffAuto",
684
61
                              microvm_machine_get_pcie,
685
61
                              microvm_machine_set_pcie,
686
61
                              NULL, NULL);
687
61
    object_class_property_set_description(oc, MICROVM_MACHINE_PCIE,
688
61
        "Enable PCIe");
689
690
61
    object_class_property_add(oc, MICROVM_MACHINE_IOAPIC2, "OnOffAuto",
691
61
                              microvm_machine_get_ioapic2,
692
61
                              microvm_machine_set_ioapic2,
693
61
                              NULL, NULL);
694
61
    object_class_property_set_description(oc, MICROVM_MACHINE_IOAPIC2,
695
61
        "Enable second IO-APIC");
696
697
61
    object_class_property_add_bool(oc, MICROVM_MACHINE_ISA_SERIAL,
698
61
                                   microvm_machine_get_isa_serial,
699
61
                                   microvm_machine_set_isa_serial);
700
61
    object_class_property_set_description(oc, MICROVM_MACHINE_ISA_SERIAL,
701
61
        "Set off to disable the instantiation an ISA serial port");
702
703
61
    object_class_property_add_bool(oc, MICROVM_MACHINE_OPTION_ROMS,
704
61
                                   microvm_machine_get_option_roms,
705
61
                                   microvm_machine_set_option_roms);
706
61
    object_class_property_set_description(oc, MICROVM_MACHINE_OPTION_ROMS,
707
61
        "Set off to disable loading option ROMs");
708
709
61
    object_class_property_add_bool(oc, MICROVM_MACHINE_AUTO_KERNEL_CMDLINE,
710
61
                                   microvm_machine_get_auto_kernel_cmdline,
711
61
                                   microvm_machine_set_auto_kernel_cmdline);
712
61
    object_class_property_set_description(oc,
713
61
        MICROVM_MACHINE_AUTO_KERNEL_CMDLINE,
714
61
        "Set off to disable adding virtio-mmio devices to the kernel cmdline");
715
716
61
    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
717
718
61
    compat_props_add(mc->compat_props, microvm_properties,
719
61
                     G_N_ELEMENTS(microvm_properties));
720
721
#if defined(CONFIG_IGVM)
722
    object_class_property_add_link(oc, "igvm-cfg",
723
                                   TYPE_IGVM_CFG,
724
                                   offsetof(X86MachineState, igvm),
725
                                   object_property_allow_set_link,
726
                                   OBJ_PROP_LINK_STRONG);
727
    object_class_property_set_description(oc, "igvm-cfg",
728
                                          "Set IGVM configuration");
729
#endif
730
61
}
731
732
static const TypeInfo microvm_machine_info = {
733
    .name          = TYPE_MICROVM_MACHINE,
734
    .parent        = TYPE_X86_MACHINE,
735
    .instance_size = sizeof(MicrovmMachineState),
736
    .instance_init = microvm_machine_initfn,
737
    .class_size    = sizeof(MicrovmMachineClass),
738
    .class_init    = microvm_class_init,
739
    .interfaces = (const InterfaceInfo[]) {
740
         { TYPE_HOTPLUG_HANDLER },
741
         { }
742
    },
743
};
744
745
static void microvm_machine_init(void)
746
61
{
747
61
    type_register_static(&microvm_machine_info);
748
61
}
749
type_init(microvm_machine_init);