cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
9ae3a8
From 8d7f958d14fa739c64436d0926bc712eaa63fe14 Mon Sep 17 00:00:00 2001
9ae3a8
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
9ae3a8
Date: Wed, 13 Dec 2017 13:38:43 +0100
9ae3a8
Subject: [PATCH 12/41] hw/misc: add vmcoreinfo device
9ae3a8
MIME-Version: 1.0
9ae3a8
Content-Type: text/plain; charset=UTF-8
9ae3a8
Content-Transfer-Encoding: 8bit
9ae3a8
9ae3a8
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
Message-id: <20171213133912.26176-13-marcandre.lureau@redhat.com>
9ae3a8
Patchwork-id: 78362
9ae3a8
O-Subject: [RHEL-7.5 qemu-kvm PATCH v3 12/41] hw/misc: add vmcoreinfo device
9ae3a8
Bugzilla: 1411490
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
See docs/specs/vmcoreinfo.txt for details.
9ae3a8
9ae3a8
"etc/vmcoreinfo" fw_cfg entry is added when using "-device vmcoreinfo".
9ae3a8
9ae3a8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
9ae3a8
(cherry picked from commit 6e43353f10c6688060af0bc26bdfdd4cf9c96ea2)
9ae3a8
9ae3a8
RHEL: adapted to qemu 1.5 API.
9ae3a8
9ae3a8
FWCfgState is not exposed, use object_property_get_bool() instead.
9ae3a8
9ae3a8
DeviceClass.hotpluggable doesn't exist. Not important since libvirt
9ae3a8
shouldn't allow or do 'device_add vmcoreinfo'.
9ae3a8
9ae3a8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 docs/specs/vmcoreinfo.txt    | 41 +++++++++++++++++++
9ae3a8
 hw/misc/Makefile.objs        |  1 +
9ae3a8
 hw/misc/vmcoreinfo.c         | 96 ++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 include/hw/misc/vmcoreinfo.h | 46 +++++++++++++++++++++
9ae3a8
 4 files changed, 184 insertions(+)
9ae3a8
 create mode 100644 docs/specs/vmcoreinfo.txt
9ae3a8
 create mode 100644 hw/misc/vmcoreinfo.c
9ae3a8
 create mode 100644 include/hw/misc/vmcoreinfo.h
9ae3a8
9ae3a8
diff --git a/docs/specs/vmcoreinfo.txt b/docs/specs/vmcoreinfo.txt
9ae3a8
new file mode 100644
9ae3a8
index 0000000..2868a77
9ae3a8
--- /dev/null
9ae3a8
+++ b/docs/specs/vmcoreinfo.txt
9ae3a8
@@ -0,0 +1,41 @@
9ae3a8
+=================
9ae3a8
+VMCoreInfo device
9ae3a8
+=================
9ae3a8
+
9ae3a8
+The `-device vmcoreinfo` will create a fw_cfg entry for a guest to
9ae3a8
+store dump details.
9ae3a8
+
9ae3a8
+etc/vmcoreinfo
9ae3a8
+**************
9ae3a8
+
9ae3a8
+A guest may use this fw_cfg entry to add information details to qemu
9ae3a8
+dumps.
9ae3a8
+
9ae3a8
+The entry of 16 bytes has the following layout, in little-endian::
9ae3a8
+
9ae3a8
+#define VMCOREINFO_FORMAT_NONE 0x0
9ae3a8
+#define VMCOREINFO_FORMAT_ELF 0x1
9ae3a8
+
9ae3a8
+    struct FWCfgVMCoreInfo {
9ae3a8
+        uint16_t host_format;  /* formats host supports */
9ae3a8
+        uint16_t guest_format; /* format guest supplies */
9ae3a8
+        uint32_t size;         /* size of vmcoreinfo region */
9ae3a8
+        uint64_t paddr;        /* physical address of vmcoreinfo region */
9ae3a8
+    };
9ae3a8
+
9ae3a8
+Only full write (of 16 bytes) are considered valid for further
9ae3a8
+processing of entry values.
9ae3a8
+
9ae3a8
+A write of 0 in guest_format will disable further processing of
9ae3a8
+vmcoreinfo entry values & content.
9ae3a8
+
9ae3a8
+Format & content
9ae3a8
+****************
9ae3a8
+
9ae3a8
+As of qemu 2.11, only VMCOREINFO_FORMAT_ELF is supported.
9ae3a8
+
9ae3a8
+The entry gives location and size of an ELF note that is appended in
9ae3a8
+qemu dumps.
9ae3a8
+
9ae3a8
+The note format/class must be of the target bitness and the size must
9ae3a8
+be less than 1Mb.
9ae3a8
diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs
9ae3a8
index cd3123b..39b6593 100644
9ae3a8
--- a/hw/misc/Makefile.objs
9ae3a8
+++ b/hw/misc/Makefile.objs
9ae3a8
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_ISA_DEBUG) += debugexit.o
9ae3a8
 common-obj-$(CONFIG_SGA) += sga.o
9ae3a8
 common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o
9ae3a8
 common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o
9ae3a8
+common-obj-y += vmcoreinfo.o
9ae3a8
 
9ae3a8
 obj-$(CONFIG_VMPORT) += vmport.o
9ae3a8
 
9ae3a8
diff --git a/hw/misc/vmcoreinfo.c b/hw/misc/vmcoreinfo.c
9ae3a8
new file mode 100644
9ae3a8
index 0000000..1bf6735
9ae3a8
--- /dev/null
9ae3a8
+++ b/hw/misc/vmcoreinfo.c
9ae3a8
@@ -0,0 +1,96 @@
9ae3a8
+/*
9ae3a8
+ * Virtual Machine coreinfo device
9ae3a8
+ *
9ae3a8
+ * Copyright (C) 2017 Red Hat, Inc.
9ae3a8
+ *
9ae3a8
+ * Authors: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
+ *
9ae3a8
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
9ae3a8
+ * See the COPYING file in the top-level directory.
9ae3a8
+ *
9ae3a8
+ */
9ae3a8
+#include "qemu/osdep.h"
9ae3a8
+#include "qapi/error.h"
9ae3a8
+#include "hw/nvram/fw_cfg.h"
9ae3a8
+#include "hw/misc/vmcoreinfo.h"
9ae3a8
+
9ae3a8
+static void fw_cfg_vmci_write(void *dev, off_t offset, size_t len)
9ae3a8
+{
9ae3a8
+    VMCoreInfoState *s = VMCOREINFO(dev);
9ae3a8
+
9ae3a8
+    s->has_vmcoreinfo = offset == 0 && len == sizeof(s->vmcoreinfo)
9ae3a8
+        && s->vmcoreinfo.guest_format != VMCOREINFO_FORMAT_NONE;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void vmcoreinfo_reset(void *dev)
9ae3a8
+{
9ae3a8
+    VMCoreInfoState *s = VMCOREINFO(dev);
9ae3a8
+
9ae3a8
+    s->has_vmcoreinfo = false;
9ae3a8
+    memset(&s->vmcoreinfo, 0, sizeof(s->vmcoreinfo));
9ae3a8
+    s->vmcoreinfo.host_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void vmcoreinfo_realize(DeviceState *dev, Error **errp)
9ae3a8
+{
9ae3a8
+    VMCoreInfoState *s = VMCOREINFO(dev);
9ae3a8
+    FWCfgState *fw_cfg = fw_cfg_find();
9ae3a8
+
9ae3a8
+    /* Given that this function is executing, there is at least one VMCOREINFO
9ae3a8
+     * device. Check if there are several.
9ae3a8
+     */
9ae3a8
+    if (!vmcoreinfo_find()) {
9ae3a8
+        error_setg(errp, "at most one %s device is permitted",
9ae3a8
+                   VMCOREINFO_DEVICE);
9ae3a8
+        return;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (!fw_cfg || !object_property_get_bool(OBJECT(fw_cfg), "dma_enabled", NULL)) {
9ae3a8
+        error_setg(errp, "%s device requires fw_cfg with DMA",
9ae3a8
+                   VMCOREINFO_DEVICE);
9ae3a8
+        return;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    fw_cfg_add_file_callback(fw_cfg, "etc/vmcoreinfo",
9ae3a8
+                             NULL, fw_cfg_vmci_write, s,
9ae3a8
+                             &s->vmcoreinfo, sizeof(s->vmcoreinfo),
9ae3a8
+                             false);
9ae3a8
+
9ae3a8
+    qemu_register_reset(vmcoreinfo_reset, dev);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static const VMStateDescription vmstate_vmcoreinfo = {
9ae3a8
+    .name = "vmcoreinfo",
9ae3a8
+    .version_id = 1,
9ae3a8
+    .minimum_version_id = 1,
9ae3a8
+    .fields = (VMStateField[]) {
9ae3a8
+        VMSTATE_BOOL(has_vmcoreinfo, VMCoreInfoState),
9ae3a8
+        VMSTATE_UINT16(vmcoreinfo.host_format, VMCoreInfoState),
9ae3a8
+        VMSTATE_UINT16(vmcoreinfo.guest_format, VMCoreInfoState),
9ae3a8
+        VMSTATE_UINT32(vmcoreinfo.size, VMCoreInfoState),
9ae3a8
+        VMSTATE_UINT64(vmcoreinfo.paddr, VMCoreInfoState),
9ae3a8
+        VMSTATE_END_OF_LIST()
9ae3a8
+    },
9ae3a8
+};
9ae3a8
+
9ae3a8
+static void vmcoreinfo_device_class_init(ObjectClass *klass, void *data)
9ae3a8
+{
9ae3a8
+    DeviceClass *dc = DEVICE_CLASS(klass);
9ae3a8
+
9ae3a8
+    dc->vmsd = &vmstate_vmcoreinfo;
9ae3a8
+    dc->realize = vmcoreinfo_realize;
9ae3a8
+}
9ae3a8
+
9ae3a8
+static const TypeInfo vmcoreinfo_device_info = {
9ae3a8
+    .name          = VMCOREINFO_DEVICE,
9ae3a8
+    .parent        = TYPE_DEVICE,
9ae3a8
+    .instance_size = sizeof(VMCoreInfoState),
9ae3a8
+    .class_init    = vmcoreinfo_device_class_init,
9ae3a8
+};
9ae3a8
+
9ae3a8
+static void vmcoreinfo_register_types(void)
9ae3a8
+{
9ae3a8
+    type_register_static(&vmcoreinfo_device_info);
9ae3a8
+}
9ae3a8
+
9ae3a8
+type_init(vmcoreinfo_register_types)
9ae3a8
diff --git a/include/hw/misc/vmcoreinfo.h b/include/hw/misc/vmcoreinfo.h
9ae3a8
new file mode 100644
9ae3a8
index 0000000..c3aa856
9ae3a8
--- /dev/null
9ae3a8
+++ b/include/hw/misc/vmcoreinfo.h
9ae3a8
@@ -0,0 +1,46 @@
9ae3a8
+/*
9ae3a8
+ * Virtual Machine coreinfo device
9ae3a8
+ *
9ae3a8
+ * Copyright (C) 2017 Red Hat, Inc.
9ae3a8
+ *
9ae3a8
+ * Authors: Marc-André Lureau <marcandre.lureau@redhat.com>
9ae3a8
+ *
9ae3a8
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
9ae3a8
+ * See the COPYING file in the top-level directory.
9ae3a8
+ *
9ae3a8
+ */
9ae3a8
+#ifndef VMCOREINFO_H
9ae3a8
+#define VMCOREINFO_H
9ae3a8
+
9ae3a8
+#include "hw/qdev.h"
9ae3a8
+
9ae3a8
+#define VMCOREINFO_DEVICE "vmcoreinfo"
9ae3a8
+#define VMCOREINFO(obj) OBJECT_CHECK(VMCoreInfoState, (obj), VMCOREINFO_DEVICE)
9ae3a8
+
9ae3a8
+#define VMCOREINFO_FORMAT_NONE 0x0
9ae3a8
+#define VMCOREINFO_FORMAT_ELF 0x1
9ae3a8
+
9ae3a8
+/* all fields are little-endian */
9ae3a8
+typedef struct FWCfgVMCoreInfo {
9ae3a8
+    uint16_t host_format; /* set on reset */
9ae3a8
+    uint16_t guest_format;
9ae3a8
+    uint32_t size;
9ae3a8
+    uint64_t paddr;
9ae3a8
+} QEMU_PACKED FWCfgVMCoreInfo;
9ae3a8
+
9ae3a8
+typedef struct VMCoreInfoState {
9ae3a8
+    DeviceClass parent_obj;
9ae3a8
+
9ae3a8
+    bool has_vmcoreinfo;
9ae3a8
+    FWCfgVMCoreInfo vmcoreinfo;
9ae3a8
+} VMCoreInfoState;
9ae3a8
+
9ae3a8
+/* returns NULL unless there is exactly one device */
9ae3a8
+static inline VMCoreInfoState *vmcoreinfo_find(void)
9ae3a8
+{
9ae3a8
+    Object *o = object_resolve_path_type("", VMCOREINFO_DEVICE, NULL);
9ae3a8
+
9ae3a8
+    return o ? VMCOREINFO(o) : NULL;
9ae3a8
+}
9ae3a8
+
9ae3a8
+#endif
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8