Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752786AbdLELsL (ORCPT ); Tue, 5 Dec 2017 06:48:11 -0500 Received: from mx1.redhat.com ([209.132.183.28]:4383 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752713AbdLELr4 (ORCPT ); Tue, 5 Dec 2017 06:47:56 -0500 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: linux-kernel@vger.kernel.org Cc: qemu-devel@nongnu.org, somlo@cmu.edu, xiaolong.ye@intel.com, bhe@redhat.com, mst@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [PATCH v9 4/4] fw_cfg: write vmcoreinfo details Date: Tue, 5 Dec 2017 12:47:29 +0100 Message-Id: <20171205114729.10089-5-marcandre.lureau@redhat.com> In-Reply-To: <20171205114729.10089-1-marcandre.lureau@redhat.com> References: <20171205114729.10089-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 05 Dec 2017 11:47:55 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4291 Lines: 139 If the "etc/vmcoreinfo" fw_cfg file is present and we are not running the kdump kernel, write the addr/size of the vmcoreinfo ELF note. Signed-off-by: Marc-André Lureau Reviewed-by: Gabriel Somlo --- drivers/firmware/qemu_fw_cfg.c | 82 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c index e261d9254b29..61af1c0a640d 100644 --- a/drivers/firmware/qemu_fw_cfg.c +++ b/drivers/firmware/qemu_fw_cfg.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include MODULE_AUTHOR("Gabriel L. Somlo "); MODULE_DESCRIPTION("QEMU fw_cfg sysfs support"); @@ -58,6 +60,8 @@ MODULE_LICENSE("GPL"); /* fw_cfg "file name" is up to 56 characters (including terminating nul) */ #define FW_CFG_MAX_FILE_PATH 56 +#define VMCOREINFO_FORMAT_ELF 0x1 + /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ static u32 fw_cfg_rev; @@ -119,7 +123,8 @@ static ssize_t fw_cfg_dma_transfer(struct device *dev, dma_addr_t dma; ssize_t ret = length; enum dma_data_direction dir = - (control & FW_CFG_DMA_CTL_READ ? DMA_FROM_DEVICE : 0); + (control & FW_CFG_DMA_CTL_READ ? DMA_FROM_DEVICE : 0) | + (control & FW_CFG_DMA_CTL_WRITE ? DMA_TO_DEVICE : 0); if (address && length) { dma_addr = dma_map_single(dev, address, length, dir); @@ -216,6 +221,48 @@ static ssize_t fw_cfg_read_blob(struct device *dev, u16 key, return ret; } +#ifdef CONFIG_CRASH_CORE +/* write chunk of given fw_cfg blob (caller responsible for sanity-check) */ +static ssize_t fw_cfg_write_blob(struct device *dev, u16 key, + void *buf, loff_t pos, size_t count) +{ + u32 glk = -1U; + acpi_status status; + ssize_t ret = count; + + /* If we have ACPI, ensure mutual exclusion against any potential + * device access by the firmware, e.g. via AML methods: + */ + status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk); + if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) { + /* Should never get here */ + WARN(1, "%s: Failed to lock ACPI!\n", __func__); + memset(buf, 0, count); + return -EINVAL; + } + + mutex_lock(&fw_cfg_dev_lock); + if (pos == 0) { + ret = fw_cfg_dma_transfer(dev, buf, count, key << 16 + | FW_CFG_DMA_CTL_SELECT + | FW_CFG_DMA_CTL_WRITE); + } else { + iowrite16(fw_cfg_sel_endianness(key), fw_cfg_reg_ctrl); + ret = fw_cfg_dma_transfer(dev, NULL, pos, FW_CFG_DMA_CTL_SKIP); + if (ret < 0) + goto end; + ret = fw_cfg_dma_transfer(dev, buf, count, FW_CFG_DMA_CTL_WRITE); + } + +end: + mutex_unlock(&fw_cfg_dev_lock); + + acpi_release_global_lock(glk); + + return ret; +} +#endif /* CONFIG_CRASH_CORE */ + /* clean up fw_cfg device i/o */ static void fw_cfg_io_cleanup(void) { @@ -335,6 +382,32 @@ struct fw_cfg_sysfs_entry { struct device *dev; }; +#ifdef CONFIG_CRASH_CORE +static ssize_t write_vmcoreinfo(struct device *dev, const struct fw_cfg_file *f) +{ + struct vmci { + __le16 host_format; + __le16 guest_format; + __le32 size; + __le64 paddr; + } __packed; + static struct vmci data; + ssize_t ret; + + data = (struct vmci) { + .guest_format = cpu_to_le16(VMCOREINFO_FORMAT_ELF), + .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE), + .paddr = cpu_to_le64(paddr_vmcoreinfo_note()) + }; + /* spare ourself reading host format support for now since we + * don't know what else to format - host may ignore ours + */ + ret = fw_cfg_write_blob(dev, f->select, &data, 0, sizeof(struct vmci)); + + return ret; +} +#endif /* CONFIG_CRASH_CORE */ + /* get fw_cfg_sysfs_entry from kobject member */ static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj) { @@ -575,6 +648,13 @@ static int fw_cfg_register_file(struct device *dev, const struct fw_cfg_file *f) int err; struct fw_cfg_sysfs_entry *entry; +#ifdef CONFIG_CRASH_CORE + if (strcmp(f->name, "etc/vmcoreinfo") == 0 && !is_kdump_kernel()) { + if (write_vmcoreinfo(dev, f) < 0) + pr_warn("fw_cfg: failed to write vmcoreinfo"); + } +#endif + /* allocate new entry */ entry = kzalloc(sizeof(*entry), GFP_KERNEL); if (!entry) -- 2.15.0.277.ga3d2ad2c43