Import QEMU upstream snapshot d2e570c

Upstream: https://gitlab.com/qemu-project/qemu.git

Upstream-Commit: d2e570cc0f97b936902a5b1b86b73c0f5998b475
This commit is contained in:
2026-08-31 02:15:30 +02:00
commit cf256aa081
11315 changed files with 3598369 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
config NITRO_VSOCK_BUS
bool
config NITRO_SERIAL_VSOCK
bool
depends on NITRO_VSOCK_BUS
config NITRO_HEARTBEAT
bool
depends on NITRO_VSOCK_BUS
config NITRO_MACHINE
bool
default y
depends on NITRO
select NITRO_VSOCK_BUS
select NITRO_HEARTBEAT
select NITRO_SERIAL_VSOCK
+115
View File
@@ -0,0 +1,115 @@
/*
* Nitro Enclave Heartbeat device
*
* Copyright © 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Authors:
* Alexander Graf <[email protected]>
*
* The Nitro Enclave init process sends a heartbeat byte (0xB7) to
* CID 3 (parent) port 9000 on boot to signal it reached initramfs.
* The parent must accept the connection, read the byte, and echo it
* back. If the enclave init cannot reach the listener, it exits.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "chardev/char.h"
#include "chardev/char-fe.h"
#include "hw/nitro/heartbeat.h"
#include "trace.h"
#define HEARTBEAT_PORT 9000
#define VMADDR_CID_ANY_STR "4294967295"
static int nitro_heartbeat_can_read(void *opaque)
{
NitroHeartbeatState *s = opaque;
/* One-shot protocol: stop reading after the first heartbeat */
return s->done ? 0 : 1;
}
static void nitro_heartbeat_read(void *opaque, const uint8_t *buf, int size)
{
NitroHeartbeatState *s = opaque;
if (s->done || size < 1) {
return;
}
/* Echo the heartbeat byte back and disconnect */
qemu_chr_fe_write_all(&s->vsock, buf, 1);
s->done = true;
qemu_chr_fe_deinit(&s->vsock, true);
trace_nitro_heartbeat_done();
}
static void nitro_heartbeat_event(void *opaque, QEMUChrEvent event)
{
trace_nitro_heartbeat_event(event);
}
static void nitro_heartbeat_realize(DeviceState *dev, Error **errp)
{
NitroHeartbeatState *s = NITRO_HEARTBEAT(dev);
g_autofree char *chardev_id = NULL;
Chardev *chr;
ChardevBackend *backend;
ChardevSocket *sock;
chardev_id = g_strdup_printf("nitro-heartbeat");
backend = g_new0(ChardevBackend, 1);
backend->type = CHARDEV_BACKEND_KIND_SOCKET;
sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
sock->addr = g_new0(SocketAddressLegacy, 1);
sock->addr->type = SOCKET_ADDRESS_TYPE_VSOCK;
sock->addr->u.vsock.data = g_new0(VsockSocketAddress, 1);
sock->addr->u.vsock.data->cid = g_strdup(VMADDR_CID_ANY_STR);
sock->addr->u.vsock.data->port = g_strdup_printf("%u", HEARTBEAT_PORT);
sock->server = true;
sock->has_server = true;
sock->wait = false;
sock->has_wait = true;
chr = qemu_chardev_new(chardev_id, TYPE_CHARDEV_SOCKET,
backend, NULL, errp);
if (!chr) {
return;
}
if (!qemu_chr_fe_init(&s->vsock, chr, errp)) {
return;
}
qemu_chr_fe_set_handlers(&s->vsock,
nitro_heartbeat_can_read,
nitro_heartbeat_read,
nitro_heartbeat_event,
NULL, s, NULL, true);
}
static void nitro_heartbeat_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
dc->realize = nitro_heartbeat_realize;
}
static const TypeInfo nitro_heartbeat_info = {
.name = TYPE_NITRO_HEARTBEAT,
.parent = TYPE_NITRO_VSOCK_DEVICE,
.instance_size = sizeof(NitroHeartbeatState),
.class_init = nitro_heartbeat_class_init,
};
static void nitro_heartbeat_register(void)
{
type_register_static(&nitro_heartbeat_info);
}
type_init(nitro_heartbeat_register);
+277
View File
@@ -0,0 +1,277 @@
/*
* Nitro Enclaves (accel) machine
*
* Copyright © 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Authors:
* Alexander Graf <[email protected]>
*
* Nitro Enclaves machine model for -accel nitro. This machine behaves
* like the nitro-enclave machine, but uses the real Nitro Enclaves
* backend to launch the virtual machine. It requires use of the -accel
* nitro.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "qom/object_interfaces.h"
#include "chardev/char.h"
#include "hw/core/boards.h"
#include "hw/core/cpu.h"
#include "hw/core/qdev-properties-system.h"
#include "hw/nitro/heartbeat.h"
#include "hw/nitro/machine.h"
#include "hw/nitro/nitro-vsock-bus.h"
#include "hw/nitro/serial-vsock.h"
#include "system/address-spaces.h"
#include "system/hostmem.h"
#include "system/system.h"
#include "system/nitro-accel.h"
#include "qemu/accel.h"
#include "hw/arm/machines-qom.h"
#include "hw/core/eif.h"
#include <zlib.h> /* for crc32 */
#define EIF_LOAD_ADDR (8 * 1024 * 1024)
static bool is_eif(char *eif, gsize len)
{
const char eif_magic[] = EIF_MAGIC;
return len >= sizeof(eif_magic) &&
!memcmp(eif, eif_magic, sizeof(eif_magic));
}
static void build_eif_section(EifHeader *hdr, GByteArray *buf, uint16_t type,
const char *data, uint64_t size)
{
uint16_t section = be16_to_cpu(hdr->section_cnt);
EifSectionHeader shdr = {
.section_type = cpu_to_be16(type),
.flags = 0,
.section_size = cpu_to_be64(size),
};
hdr->section_offsets[section] = cpu_to_be64(buf->len);
hdr->section_sizes[section] = cpu_to_be64(size);
g_byte_array_append(buf, (const uint8_t *)&shdr, sizeof(shdr));
if (size) {
g_byte_array_append(buf, (const uint8_t *)data, size);
}
hdr->section_cnt = cpu_to_be16(section + 1);
}
/*
* Nitro Enclaves only support loading EIF files. When the user provides
* a Linux kernel, initrd and cmdline, convert them into EIF format.
*/
static char *build_eif(const char *kernel_data, gsize kernel_size,
const char *initrd_path, const char *cmdline,
gsize *out_size, Error **errp)
{
g_autofree char *initrd_data = NULL;
static const char metadata[] = "{}";
size_t metadata_len = sizeof(metadata) - 1;
gsize initrd_size = 0;
GByteArray *buf;
EifHeader hdr;
uint32_t crc = 0;
size_t cmdline_len;
if (initrd_path) {
if (!g_file_get_contents(initrd_path, &initrd_data,
&initrd_size, NULL)) {
error_setg(errp, "Failed to read initrd '%s'", initrd_path);
return NULL;
}
}
buf = g_byte_array_new();
cmdline_len = cmdline ? strlen(cmdline) : 0;
hdr = (EifHeader) {
.magic = EIF_MAGIC,
.version = cpu_to_be16(4),
.flags = cpu_to_be16(target_aarch64() ? EIF_HDR_ARCH_ARM64 : 0),
};
g_byte_array_append(buf, (const uint8_t *)&hdr, sizeof(hdr));
/* Kernel */
build_eif_section(&hdr, buf, EIF_SECTION_KERNEL, kernel_data, kernel_size);
/* Command line */
build_eif_section(&hdr, buf, EIF_SECTION_CMDLINE, cmdline, cmdline_len);
/* Initramfs */
build_eif_section(&hdr, buf, EIF_SECTION_RAMDISK, initrd_data, initrd_size);
/* Metadata */
build_eif_section(&hdr, buf, EIF_SECTION_METADATA, metadata, metadata_len);
/*
* Patch the header into the buffer first (with real section offsets
* and sizes), then compute CRC over everything except the CRC field.
*/
memcpy(buf->data, &hdr, sizeof(hdr));
crc = crc32(crc, buf->data, offsetof(EifHeader, eif_crc32));
crc = crc32(crc, &buf->data[sizeof(hdr)], buf->len - sizeof(hdr));
/* Finally write the CRC into the in-buffer header */
((EifHeader *)buf->data)->eif_crc32 = cpu_to_be32(crc);
*out_size = buf->len;
return (char *)g_byte_array_free(buf, false);
}
static void nitro_machine_init(MachineState *machine)
{
const char *eif_path = machine->kernel_filename;
const char *cpu_type = machine->cpu_type;
g_autofree char *eif_data = NULL;
gsize eif_size;
if (!nitro_enabled()) {
error_report("The 'nitro' machine requires -accel nitro");
exit(1);
}
if (!cpu_type) {
ObjectClass *oc = cpu_class_by_name(target_cpu_type(), "host");
if (!oc) {
error_report("nitro: no 'host' CPU available");
exit(1);
}
cpu_type = object_class_get_name(oc);
}
if (!eif_path) {
error_report("nitro: -kernel <eif-file> is required");
exit(1);
}
/* Expose memory as normal QEMU RAM. Needs to be huge page backed. */
memory_region_add_subregion(get_system_memory(), 0, machine->ram);
/*
* Load EIF (-kernel) as raw blob at the EIF_LOAD_ADDR into guest RAM.
* The Nitro Hypervisor will extract its contents and bootstrap the
* Enclave from it.
*/
if (!g_file_get_contents(eif_path, &eif_data, &eif_size, NULL)) {
error_report("nitro: failed to read EIF '%s'", eif_path);
exit(1);
}
if (!is_eif(eif_data, eif_size)) {
char *kernel_data = eif_data;
gsize kernel_size = eif_size;
Error *err = NULL;
/*
* The user gave us a non-EIF kernel, likely a Linux kernel image.
* Assemble an EIF file from it, the -initrd and the -append arguments,
* so that users can perform a natural direct kernel boot.
*/
eif_data = build_eif(kernel_data, kernel_size, machine->initrd_filename,
machine->kernel_cmdline, &eif_size, &err);
if (!eif_data) {
error_report_err(err);
exit(1);
}
g_free(kernel_data);
}
address_space_write(&address_space_memory, EIF_LOAD_ADDR,
MEMTXATTRS_UNSPECIFIED, eif_data, eif_size);
if (defaults_enabled()) {
NitroVsockBridge *bridge = nitro_vsock_bridge_create();
/* Nitro Enclaves require a heartbeat device. Provide one. */
qdev_realize(qdev_new(TYPE_NITRO_HEARTBEAT),
BUS(&bridge->bus), &error_fatal);
/*
* In debug mode, Nitro Enclaves expose the guest's serial output via
* vsock. When the accel is in debug mode, wire the vsock serial to
* the machine's serial port so that -nographic automatically works
*/
if (object_property_get_bool(OBJECT(current_accel()), "debug-mode", NULL)) {
Chardev *chr = serial_hd(0);
if (chr) {
DeviceState *dev = qdev_new(TYPE_NITRO_SERIAL_VSOCK);
qdev_prop_set_chr(dev, "chardev", chr);
qdev_realize(dev, BUS(&bridge->bus), &error_fatal);
}
}
}
}
static bool nitro_create_memfd_backend(MachineState *ms, const char *path,
Error **errp)
{
MachineClass *mc = MACHINE_GET_CLASS(ms);
Object *root = object_get_objects_root();
Object *obj;
bool r = false;
obj = object_new(TYPE_MEMORY_BACKEND_MEMFD);
/* Nitro Enclaves require huge page backing */
if (!object_property_set_int(obj, "size", ms->ram_size, errp) ||
!object_property_set_bool(obj, "hugetlb", true, errp)) {
goto out;
}
object_property_add_child(root, mc->default_ram_id, obj);
if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
goto out;
}
r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
out:
object_unref(obj);
return r;
}
static void nitro_machine_class_init(ObjectClass *oc, const void *data)
{
MachineClass *mc = MACHINE_CLASS(oc);
mc->desc = "Nitro Enclave";
mc->init = nitro_machine_init;
mc->create_default_memdev = nitro_create_memfd_backend;
mc->default_ram_id = "ram";
mc->max_cpus = 4096;
}
static const TypeInfo nitro_machine_info = {
.name = TYPE_NITRO_MACHINE,
.parent = TYPE_MACHINE,
.instance_size = sizeof(NitroMachineState),
.class_init = nitro_machine_class_init,
.interfaces = (const InterfaceInfo[]) {
/* x86_64 and aarch64 only */
{ TYPE_TARGET_AARCH64_MACHINE },
{ }
},
};
static void nitro_machine_register(void)
{
type_register_static(&nitro_machine_info);
}
type_init(nitro_machine_register);
+4
View File
@@ -0,0 +1,4 @@
system_ss.add(when: 'CONFIG_NITRO_VSOCK_BUS', if_true: files('nitro-vsock-bus.c'))
system_ss.add(when: 'CONFIG_NITRO_SERIAL_VSOCK', if_true: files('serial-vsock.c'))
system_ss.add(when: 'CONFIG_NITRO_HEARTBEAT', if_true: files('heartbeat.c'))
system_ss.add(when: 'CONFIG_NITRO_MACHINE', if_true: [files('machine.c'), zlib])
+98
View File
@@ -0,0 +1,98 @@
/*
* Nitro Enclave Vsock Bus
*
* Copyright © 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Authors:
* Alexander Graf <[email protected]>
*
* A bus for Nitro Enclave vsock devices. In Nitro Enclaves, communication
* between parent and enclave/hypervisor happens almost exclusively through
* vsock. The nitro-vsock-bus models this dependency in QEMU, which allows
* devices in this bus to implement individual services on top of vsock.
*
* The nitro accel advertises the Enclave's CID to the bus by calling
* nitro_vsock_bridge_start_enclave() on the bridge device as soon as it
* knows the CID.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "monitor/qdev.h"
#include "hw/core/sysbus.h"
#include "hw/nitro/nitro-vsock-bus.h"
void nitro_vsock_bridge_start_enclave(NitroVsockBridge *bridge,
uint32_t enclave_cid, Error **errp)
{
ERRP_GUARD();
BusState *qbus = BUS(&bridge->bus);
BusChild *kid;
bridge->enclave_cid = enclave_cid;
QTAILQ_FOREACH(kid, &qbus->children, sibling) {
NitroVsockDevice *ndev = NITRO_VSOCK_DEVICE(kid->child);
NitroVsockDeviceClass *ndc = NITRO_VSOCK_DEVICE_GET_CLASS(ndev);
if (ndc->enclave_started) {
ndc->enclave_started(ndev, enclave_cid, errp);
if (*errp) {
return;
}
}
}
}
NitroVsockBridge *nitro_vsock_bridge_create(void)
{
DeviceState *dev = qdev_new(TYPE_NITRO_VSOCK_BRIDGE);
qdev_set_id(dev, g_strdup("nitro-vsock"), &error_fatal);
sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
return NITRO_VSOCK_BRIDGE(dev);
}
static void nitro_vsock_bridge_init(Object *obj)
{
NitroVsockBridge *s = NITRO_VSOCK_BRIDGE(obj);
qbus_init(&s->bus, sizeof(s->bus), TYPE_NITRO_VSOCK_BUS,
DEVICE(s), "nitro-vsock");
object_property_add_uint32_ptr(obj, "enclave-cid",
&s->enclave_cid, OBJ_PROP_FLAG_READ);
}
static void nitro_vsock_device_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
dc->bus_type = TYPE_NITRO_VSOCK_BUS;
}
static const TypeInfo nitro_vsock_bus_types[] = {
{
.name = TYPE_NITRO_VSOCK_BUS,
.parent = TYPE_BUS,
.instance_size = sizeof(NitroVsockBus),
},
{
.name = TYPE_NITRO_VSOCK_BRIDGE,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(NitroVsockBridge),
.instance_init = nitro_vsock_bridge_init,
},
{
.name = TYPE_NITRO_VSOCK_DEVICE,
.parent = TYPE_DEVICE,
.instance_size = sizeof(NitroVsockDevice),
.class_size = sizeof(NitroVsockDeviceClass),
.class_init = nitro_vsock_device_class_init,
.abstract = true,
},
};
DEFINE_TYPES(nitro_vsock_bus_types);
+123
View File
@@ -0,0 +1,123 @@
/*
* Nitro Enclave Vsock Serial
*
* Copyright © 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Authors:
* Alexander Graf <[email protected]>
*
* With Nitro Enclaves in debug mode, the Nitro Hypervisor provides a vsock
* port that the parent can connect to to receive serial console output of
* the Enclave. This driver implements short-circuit logic to establish the
* vsock connection to that port and feed its data into a chardev, so that
* a machine model can use it as serial device.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "chardev/char.h"
#include "chardev/char-fe.h"
#include "hw/core/qdev-properties.h"
#include "hw/core/qdev-properties-system.h"
#include "hw/nitro/serial-vsock.h"
#include "trace.h"
#define CONSOLE_PORT_START 10000
#define VMADDR_CID_HYPERVISOR_STR "0"
static int nitro_serial_vsock_can_read(void *opaque)
{
NitroSerialVsockState *s = opaque;
/* Refuse vsock input until the output backend is ready */
return qemu_chr_fe_backend_open(&s->output) ? 4096 : 0;
}
static void nitro_serial_vsock_read(void *opaque, const uint8_t *buf, int size)
{
NitroSerialVsockState *s = opaque;
/* Forward all vsock data to the output chardev */
qemu_chr_fe_write_all(&s->output, buf, size);
}
static void nitro_serial_vsock_event(void *opaque, QEMUChrEvent event)
{
/* No need to action on connect/disconnect events, but trace for debug */
trace_nitro_serial_vsock_event(event);
}
static void nitro_serial_vsock_enclave_started(NitroVsockDevice *dev,
uint32_t enclave_cid,
Error **errp)
{
NitroSerialVsockState *s = NITRO_SERIAL_VSOCK(dev);
uint32_t port = enclave_cid + CONSOLE_PORT_START;
g_autofree char *chardev_id = NULL;
Chardev *chr;
ChardevBackend *backend;
ChardevSocket *sock;
/*
* We know the Enclave CID to connect to now. Create a vsock
* client chardev that connects to the Enclave's console.
*/
chardev_id = g_strdup_printf("nitro-console-%u", enclave_cid);
backend = g_new0(ChardevBackend, 1);
backend->type = CHARDEV_BACKEND_KIND_SOCKET;
sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
sock->addr = g_new0(SocketAddressLegacy, 1);
sock->addr->type = SOCKET_ADDRESS_TYPE_VSOCK;
sock->addr->u.vsock.data = g_new0(VsockSocketAddress, 1);
sock->addr->u.vsock.data->cid = g_strdup(VMADDR_CID_HYPERVISOR_STR);
sock->addr->u.vsock.data->port = g_strdup_printf("%u", port);
sock->server = false;
sock->has_server = true;
chr = qemu_chardev_new(chardev_id, TYPE_CHARDEV_SOCKET,
backend, NULL, errp);
if (!chr) {
return;
}
if (!qemu_chr_fe_init(&s->vsock, chr, errp)) {
return;
}
qemu_chr_fe_set_handlers(&s->vsock,
nitro_serial_vsock_can_read,
nitro_serial_vsock_read,
nitro_serial_vsock_event,
NULL, s, NULL, true);
}
static const Property nitro_serial_vsock_props[] = {
DEFINE_PROP_CHR("chardev", NitroSerialVsockState, output),
};
static void nitro_serial_vsock_class_init(ObjectClass *oc, const void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
NitroVsockDeviceClass *ndc = NITRO_VSOCK_DEVICE_CLASS(oc);
device_class_set_props(dc, nitro_serial_vsock_props);
ndc->enclave_started = nitro_serial_vsock_enclave_started;
}
static const TypeInfo nitro_serial_vsock_info = {
.name = TYPE_NITRO_SERIAL_VSOCK,
.parent = TYPE_NITRO_VSOCK_DEVICE,
.instance_size = sizeof(NitroSerialVsockState),
.class_init = nitro_serial_vsock_class_init,
};
static void nitro_serial_vsock_register(void)
{
type_register_static(&nitro_serial_vsock_info);
}
type_init(nitro_serial_vsock_register);
+8
View File
@@ -0,0 +1,8 @@
# See docs/devel/tracing.rst for syntax documentation.
# serial-vsock.c
nitro_serial_vsock_event(int event) "event %d"
# heartbeat.c
nitro_heartbeat_event(int event) "event %d"
nitro_heartbeat_done(void) "enclave heartbeat received"
+4
View File
@@ -0,0 +1,4 @@
/*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "trace/trace-hw_nitro.h"