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