From: Julien Thierry Subject: [PATCH kvmtool v2 6/6] arm: Support non-volatile memory Date: Thu, 10 Jan 2019 14:20:46 +0000 Message-ID: <1547130046-14729-7-git-send-email-julien.thierry@arm.com> References: <1547130046-14729-1-git-send-email-julien.thierry@arm.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Cc: andre.przywara@arm.com, Sami.Mujawar@arm.com, will.deacon@arm.com To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu Return-path: In-Reply-To: <1547130046-14729-1-git-send-email-julien.thierry@arm.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu List-Id: kvm.vger.kernel.org Add an option to let user load files as non-volatile memory. An additional range of addresses is reserved for nv memory. Loaded files must be a multiple of the system page size. Users can chose whether the data written by the guest modifies the original file. Signed-off-by: Julien Thierry --- arm/fdt.c | 34 ++++++++ arm/include/arm-common/kvm-arch.h | 5 +- arm/include/arm-common/kvm-config-arch.h | 18 ++++- arm/kvm.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 2 deletions(-) diff --git a/arm/fdt.c b/arm/fdt.c index 2936986..e3a7a64 100644 --- a/arm/fdt.c +++ b/arm/fdt.c @@ -72,6 +72,37 @@ static void generate_irq_prop(void *fdt, u8 irq, enum irq_type irq_type) _FDT(fdt_property(fdt, "interrupts", irq_prop, sizeof(irq_prop))); } +static void generate_nvmem_node(void *fdt, struct kvm_nv_mem *nvmem) +{ + char buf[22]; // 22 >= len("flash@" + 16 hex char + '\0') + const u64 reg_prop[] = { + cpu_to_fdt64(nvmem->map_addr), + cpu_to_fdt64(nvmem->size) + }; + + snprintf(buf, sizeof(buf), "flash@%llx", nvmem->map_addr); + _FDT(fdt_begin_node(fdt, buf)); + + _FDT(fdt_property_string(fdt, "compatible", "kvmtool,flash")); + + _FDT(fdt_property(fdt, "reg", reg_prop, sizeof(reg_prop))); + + _FDT(fdt_property_string(fdt, "label", nvmem->name)); + + _FDT(fdt_end_node(fdt)); +} + +static void generate_nvmem_nodes(void *fdt, struct kvm *kvm) +{ + struct list_head *nvmem_node; + + list_for_each(nvmem_node, &kvm->cfg.arch.nvmem_list) + generate_nvmem_node(fdt, + container_of(nvmem_node, + struct kvm_nv_mem, + node)); +} + struct psci_fns { u32 cpu_suspend; u32 cpu_off; @@ -210,6 +241,9 @@ static int setup_fdt(struct kvm *kvm) _FDT(fdt_property_cell(fdt, "migrate", fns->migrate)); _FDT(fdt_end_node(fdt)); + /* Non volatile memories */ + generate_nvmem_nodes(fdt, kvm); + /* Finalise. */ _FDT(fdt_end_node(fdt)); _FDT(fdt_finish(fdt)); diff --git a/arm/include/arm-common/kvm-arch.h b/arm/include/arm-common/kvm-arch.h index b9d486d..f425d86 100644 --- a/arm/include/arm-common/kvm-arch.h +++ b/arm/include/arm-common/kvm-arch.h @@ -10,6 +10,7 @@ #define ARM_IOPORT_AREA _AC(0x0000000000000000, UL) #define ARM_MMIO_AREA _AC(0x0000000000010000, UL) #define ARM_AXI_AREA _AC(0x0000000040000000, UL) +#define ARM_NVRAM_AREA _AC(0x000000007f000000, UL) #define ARM_MEMORY_AREA _AC(0x0000000080000000, UL) #define ARM_LOMAP_MAX_MEMORY ((1ULL << 32) - ARM_MEMORY_AREA) @@ -24,9 +25,11 @@ #define ARM_IOPORT_SIZE (ARM_MMIO_AREA - ARM_IOPORT_AREA) #define ARM_VIRTIO_MMIO_SIZE (ARM_AXI_AREA - (ARM_MMIO_AREA + ARM_GIC_SIZE)) #define ARM_PCI_CFG_SIZE (1ULL << 24) -#define ARM_PCI_MMIO_SIZE (ARM_MEMORY_AREA - \ +#define ARM_PCI_MMIO_SIZE (ARM_NVRAM_AREA - \ (ARM_AXI_AREA + ARM_PCI_CFG_SIZE)) +#define ARM_NVRAM_SIZE (ARM_MEMORY_AREA - ARM_NVRAM_AREA) + #define KVM_IOPORT_AREA ARM_IOPORT_AREA #define KVM_PCI_CFG_AREA ARM_AXI_AREA #define KVM_PCI_MMIO_AREA (KVM_PCI_CFG_AREA + ARM_PCI_CFG_SIZE) diff --git a/arm/include/arm-common/kvm-config-arch.h b/arm/include/arm-common/kvm-config-arch.h index 5734c46..0f1fa11 100644 --- a/arm/include/arm-common/kvm-config-arch.h +++ b/arm/include/arm-common/kvm-config-arch.h @@ -1,8 +1,18 @@ #ifndef ARM_COMMON__KVM_CONFIG_ARCH_H #define ARM_COMMON__KVM_CONFIG_ARCH_H +#include #include "kvm/parse-options.h" +struct kvm_nv_mem { + char *data_file; + char *name; + ssize_t size; + u64 map_addr; + bool write_back; + struct list_head node; +}; + struct kvm_config_arch { const char *dump_dtb_filename; unsigned int force_cntfrq; @@ -12,9 +22,11 @@ struct kvm_config_arch { u64 kaslr_seed; enum irqchip_type irqchip; u64 fw_addr; + struct list_head nvmem_list; }; int irqchip_parser(const struct option *opt, const char *arg, int unset); +int nvmem_parser(const struct option *opt, const char *arg, int unset); #define OPT_ARCH_RUN(pfx, cfg) \ pfx, \ @@ -33,6 +45,10 @@ int irqchip_parser(const struct option *opt, const char *arg, int unset); "Type of interrupt controller to emulate in the guest", \ irqchip_parser, NULL), \ OPT_U64('\0', "firmware-address", &(cfg)->fw_addr, \ - "Address where firmware should be loaded"), + "Address where firmware should be loaded"), \ + OPT_CALLBACK('\0', "nvmem", NULL, \ + ",