Skip to contentSkip to conversation

KVM flash memory support

PATCH Superseded Jordan Justen 1 participant 7 messages May 1, 2013

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM flash memory support

git://github.com/jljusten/qemu.git kvm-flash-v2

Utilize KVM_CAP_READONLY_MEM to support PC system flash emulation with KVM.

v2:
* Remove rom_only from PC_COMPAT_1_4
* Only enable flash when a pflash drive is created.

Jordan Justen (6):
kvm: add kvm_readonly_mem_enabled
kvm: support using KVM_MEM_READONLY flag for readonly regions
kvm: workaround a possible KVM bug when using KVM_MEM_READONLY
pflash_cfi01: memory region should be set to enable readonly mode
pc_sysfw: allow flash (-pflash) memory to be used with KVM
pc_sysfw: change rom_only default to 0

 hw/block/pc_sysfw.c     |   52 +++++++++++++++++++++++++++++------------------
 hw/block/pflash_cfi01.c |    2 ++
 include/hw/i386/pc.h    |    4 ----
 include/sysemu/kvm.h    |   10 +++++++++
 kvm-all.c               |   41 +++++++++++++++++++++++++++++--------
 kvm-stub.c              |    1 +
 6 files changed, 78 insertions(+), 32 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v2 1/6 kvm: add kvm_readonly_mem_enabled

---
 include/sysemu/kvm.h |   10 ++++++++++
 kvm-all.c            |    6 ++++++
 kvm-stub.c           |    1 +
 3 files changed, 17 insertions(+)
include/sysemu/kvm.h +10 -0
index 75bd7d9..c83f51c 100644
@@ -45,6 +45,7 @@ extern bool kvm_async_interrupts_allowed;
 extern bool kvm_irqfds_allowed;
 extern bool kvm_msi_via_irqfd_allowed;
 extern bool kvm_gsi_routing_allowed;
+extern bool kvm_readonly_mem_allowed;
 
 #if defined CONFIG_KVM || !defined NEED_CPU_H
 #define kvm_enabled()           (kvm_allowed)
@@ -97,6 +98,14 @@ extern bool kvm_gsi_routing_allowed;
  */
 #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
 
+/**
+ * kvm_readonly_mem_enabled:
+ *
+ * Returns: true if KVM readonly memory is enabled (ie the kernel
+ * supports it and we're running in a configuration that permits it).
+ */
+#define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed)
+
 #else
 #define kvm_enabled()           (0)
 #define kvm_irqchip_in_kernel() (false)
@@ -104,6 +113,7 @@ extern bool kvm_gsi_routing_allowed;
 #define kvm_irqfds_enabled() (false)
 #define kvm_msi_via_irqfd_enabled() (false)
 #define kvm_gsi_routing_allowed() (false)
+#define kvm_readonly_mem_enabled() (false)
 #endif
 
 struct kvm_run;
kvm-all.c +6 -0
index 2d92721..f634c41 100644
@@ -109,6 +109,7 @@ bool kvm_async_interrupts_allowed;
 bool kvm_irqfds_allowed;
 bool kvm_msi_via_irqfd_allowed;
 bool kvm_gsi_routing_allowed;
+bool kvm_readonly_mem_allowed;
 
 static const KVMCapabilityInfo kvm_required_capabilites[] = {
     KVM_CAP_INFO(USER_MEMORY),
@@ -1423,6 +1424,11 @@ int kvm_init(void)
         s->irq_set_ioctl = KVM_IRQ_LINE_STATUS;
     }
 
+#ifdef KVM_CAP_READONLY_MEM
+    kvm_readonly_mem_allowed =
+        (kvm_check_extension(s, KVM_CAP_READONLY_MEM) > 0);
+#endif
+
     ret = kvm_arch_init(s);
     if (ret < 0) {
         goto err;
kvm-stub.c +1 -0
index 5f52186..40dc368 100644
@@ -22,6 +22,7 @@ bool kvm_async_interrupts_allowed;
 bool kvm_irqfds_allowed;
 bool kvm_msi_via_irqfd_allowed;
 bool kvm_gsi_routing_allowed;
+bool kvm_readonly_mem_allowed;
 
 int kvm_init_vcpu(CPUState *cpu)
 {
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v2 2/6 kvm: support using KVM_MEM_READONLY flag for readonly regions

In reply to Jordan Justen

A slot that uses KVM_MEM_READONLY can be read from and code can execute from the region, but writes will trap.

For regions that are readonly and also not writeable, we force the slot to be removed so reads or writes to the region will trap. (A memory region in this state is not executable within kvm.)

---
 kvm-all.c |   28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
kvm-all.c +20 -8
index f634c41..95e6bf2 100644
@@ -266,9 +266,14 @@ err:
  * dirty pages logging control
  */
 
-static int kvm_mem_flags(KVMState *s, bool log_dirty)
+static int kvm_mem_flags(KVMState *s, bool log_dirty, bool readonly)
 {
-    return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
+    int flags = 0;
+    flags = log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
+    if (readonly && kvm_readonly_mem_allowed) {
+        flags |= KVM_MEM_READONLY;
+    }
+    return flags;
 }
 
 static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
@@ -279,7 +284,7 @@ static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
 
     old_flags = mem->flags;
 
-    flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
+    flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty, false);
     mem->flags = flags;
 
     /* If nothing changed effectively, no need to issue ioctl */
@@ -636,7 +641,14 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
     }
 
     if (!memory_region_is_ram(mr)) {
-        return;
+        if (!mr->readonly || !kvm_readonly_mem_allowed) {
+            return;
+        } else if (!mr->readable && add) {
+            /* If the memory range is not readable, then we actually want
+             * to remove the kvm memory slot so all accesses will trap. */
+            assert(mr->readonly && kvm_readonly_mem_allowed);
+            add = false;
+        }
     }
 
     ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
@@ -685,7 +697,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
             mem->memory_size = old.memory_size;
             mem->start_addr = old.start_addr;
             mem->ram = old.ram;
-            mem->flags = kvm_mem_flags(s, log_dirty);
+            mem->flags = kvm_mem_flags(s, log_dirty, mr->readonly);
 
             err = kvm_set_user_memory_region(s, mem);
             if (err) {
@@ -706,7 +718,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
             mem->memory_size = start_addr - old.start_addr;
             mem->start_addr = old.start_addr;
             mem->ram = old.ram;
-            mem->flags =  kvm_mem_flags(s, log_dirty);
+            mem->flags =  kvm_mem_flags(s, log_dirty, mr->readonly);
 
             err = kvm_set_user_memory_region(s, mem);
             if (err) {
@@ -730,7 +742,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
             size_delta = mem->start_addr - old.start_addr;
             mem->memory_size = old.memory_size - size_delta;
             mem->ram = old.ram + size_delta;
-            mem->flags = kvm_mem_flags(s, log_dirty);
+            mem->flags = kvm_mem_flags(s, log_dirty, mr->readonly);
 
             err = kvm_set_user_memory_region(s, mem);
             if (err) {
@@ -752,7 +764,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
     mem->memory_size = size;
     mem->start_addr = start_addr;
     mem->ram = ram;
-    mem->flags = kvm_mem_flags(s, log_dirty);
+    mem->flags = kvm_mem_flags(s, log_dirty, mr->readonly);
 
     err = kvm_set_user_memory_region(s, mem);
     if (err) {
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v2 3/6 kvm: workaround a possible KVM bug when using KVM_MEM_READONLY

In reply to Jordan Justen

On a Linux 3.8.0 based kernel, I occasionally saw a situation where the memory region would continue to trap on memory read even though KVM_MEM_READONLY was set.

I found that if I set the slot to a size of 0, and before setting the slot, it would then behave as expected.

---
 kvm-all.c |    7 +++++++
 1 file changed, 7 insertions(+)
kvm-all.c +7 -0
index 95e6bf2..e2ddbcb 100644
@@ -205,6 +205,13 @@ static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
     if (s->migration_log) {
         mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
     }
+    if (mem.flags & KVM_MEM_READONLY && mem.memory_size != 0) {
+        /* Workaround an issue with setting a READONLY slot. Set the
+         * slot size to 0 before setting the slot to the desired value. */
+        mem.memory_size = 0;
+        kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+        mem.memory_size = slot->memory_size;
+    }
     return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
 }
 
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH v2 4/6 pflash_cfi01: memory region should be set to enable readonly mode

In reply to Jordan Justen

This causes any writes to the memory region to trap to the device handler.

This is also important for KVM, because this allows the memory region to be set using KVM_MEM_READONLY, which allows the memory region to be read & executed. (Without this, KVM will not support executing from the memory region.)

---
 hw/block/pflash_cfi01.c |    2 ++
 1 file changed, 2 insertions(+)
hw/block/pflash_cfi01.c +2 -0
index 3ff20e0..b65225e 100644
@@ -596,6 +596,8 @@ static int pflash_cfi01_init(SysBusDevice *dev)
         }
     }
 
+    memory_region_set_readonly(&pfl->mem, true);
+
     if (pfl->bs) {
         pfl->ro = bdrv_is_read_only(pfl->bs);
     } else {
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v2 5/6 pc_sysfw: allow flash (-pflash) memory to be used with KVM

In reply to Jordan Justen

When pc-sysfw.rom_only != 0, flash memory will be usable with kvm. In order to enable flash memory mode, a pflash device must be created. (For example, by using the -pflash command line parameter.)

Usage of a flash memory device with kvm requires the KVM READONLY memory capability, and kvm will abort if a flash device is used with an older kvm which does not support this capability.

If a flash device is not used, then qemu/kvm will operate in the original rom-mode.

---
 hw/block/pc_sysfw.c |   50 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 19 deletions(-)
hw/block/pc_sysfw.c +31 -19
index aad8614..301eb96 100644
@@ -215,28 +215,40 @@ void pc_system_firmware_init(MemoryRegion *rom_memory)
 
     qdev_init_nofail(DEVICE(sysfw_dev));
 
-    if (sysfw_dev->rom_only) {
-        old_pc_system_rom_init(rom_memory);
-        return;
-    }
-
     pflash_drv = drive_get(IF_PFLASH, 0, 0);
 
-    /* Currently KVM cannot execute from device memory.
-       Use old rom based firmware initialization for KVM. */
-    /*
-     * This is a Bad Idea, because it makes enabling/disabling KVM
-     * guest-visible.  Do it only in bug-compatibility mode.
-     */
-    if (pc_sysfw_flash_vs_rom_bug_compatible && kvm_enabled()) {
-        if (pflash_drv != NULL) {
-            fprintf(stderr, "qemu: pflash cannot be used with kvm enabled\n");
-            exit(1);
-        } else {
-            sysfw_dev->rom_only = 1;
-            old_pc_system_rom_init(rom_memory);
-            return;
+    if (pc_sysfw_flash_vs_rom_bug_compatible) {
+        /*
+         * This is a Bad Idea, because it makes enabling/disabling KVM
+         * guest-visible.  Do it only in bug-compatibility mode.
+         */
+        if (kvm_enabled()) {
+            if (pflash_drv != NULL) {
+                fprintf(stderr, "qemu: pflash cannot be used with kvm enabled\n");
+                exit(1);
+            } else {
+                /* In old pc_sysfw_flash_vs_rom_bug_compatible mode, we assume
+                 * that KVM cannot execute from device memory. In this case, we
+                 * use old rom based firmware initialization for KVM. But, since
+                 * this is different from non-kvm mode, this behavior is
+                 * undesirable */
+                sysfw_dev->rom_only = 1;
+            }
         }
+    } else if (pflash_drv == NULL) {
+        /* When a pflash drive is not found, use rom-mode */
+        sysfw_dev->rom_only = 1;
+    } else if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
+        /* Older KVM cannot execute from device memory. So, flash memory
+         * cannot be used unless the readonly memory kvm capability is present. */
+        fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
+        exit(1);
+    }
+
+    /* If rom-mode is active, use the old pc system rom initialization. */
+    if (sysfw_dev->rom_only) {
+        old_pc_system_rom_init(rom_memory);
+        return;
     }
 
     /* If a pflash drive is not found, then create one using
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v2 6/6 pc_sysfw: change rom_only default to 0

In reply to Jordan Justen

Now KVM can support a flash memory. This feature depends on KVM_CAP_READONLY_MEM, which was introduced in Linux 3.7.

Flash memory will only be enabled if a pflash device is created. (For example, by using the -pflash command line parameter.)

---
 hw/block/pc_sysfw.c  |    2 +-
 include/hw/i386/pc.h |    4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)
hw/block/pc_sysfw.c +1 -1
index 301eb96..46e794e 100644
@@ -267,7 +267,7 @@ void pc_system_firmware_init(MemoryRegion *rom_memory)
 }
 
 static Property pcsysfw_properties[] = {
-    DEFINE_PROP_UINT8("rom_only", PcSysFwDevice, rom_only, 1),
+    DEFINE_PROP_UINT8("rom_only", PcSysFwDevice, rom_only, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
include/hw/i386/pc.h +0 -4
index dd6bc24..16ee970 100644
@@ -237,10 +237,6 @@ int e820_add_entry(uint64_t, uint64_t, uint32_t);
             .driver   = "virtio-net-pci",\
             .property = "romfile",\
             .value    = "pxe-virtio.rom",\
-        },{\
-            .driver   = "pc-sysfw",\
-            .property = "rom_only",\
-            .value    = stringify(0),\
         }
 
 #endif
Signature · 1 line
1.7.10.4