Skip to contentSkip to conversation

kvm-i386: QEMU support for SMRAM

PATCH New Paolo Bonzini 1 participant 8 messages May 18, 2015

lore
Share & tools
lore
#0 lore raw

cover kvm-i386: QEMU support for SMRAM

These are the new patches for SMRAM support, using multiple address spaces and, thus, multiple KVMMemoryListeners. No more layering violations, yay!

The meat is in patches 4 and 5, which parameterize KVM's memory listener so that: 1) each memory listener handles its own array of memory slots;
2) each memory listener specifies an address space id for use in KVM_SET_USER_MEMORY_REGION and KVM_GET_DIRTY_LOG.

Andrew Jones (1):
kvm-all: put kvm_mem_flags to more work

Paolo Bonzini (6):
kvm-all: remove useless typedef
kvm-all: move internal types to kvm_int.h
kvm-all: make KVM's memory listener more generic
kvm-all: add support for multiple address spaces
kvm-all: kvm_irqchip_create is not expected to fail
target-i386: register a separate KVM address space including SMRAM regions

 include/sysemu/kvm_int.h |  40 ++++++++
 kvm-all.c                | 259 +++++++++++++++++++++++++----------------------
 target-i386/kvm.c        |  40 +++++++-
 3 files changed, 215 insertions(+), 124 deletions(-)
 create mode 100644 include/sysemu/kvm_int.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/7 kvm-all: put kvm_mem_flags to more work

From: Andrew Jones <[email protected]>

Currently kvm_mem_flags just translates bools to bits, let's make it also determine the bools first. This avoids its parameter list growing each time we add a flag.

---
 kvm-all.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
kvm-all.c +15 -10
index 17a3771..b459855 100644
@@ -295,10 +295,14 @@ err:
  * dirty pages logging control
  */
 
-static int kvm_mem_flags(KVMState *s, bool log_dirty, bool readonly)
+static int kvm_mem_flags(MemoryRegion *mr)
 {
+    bool readonly = mr->readonly || memory_region_is_romd(mr);
     int flags = 0;
-    flags = log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
+
+    if (memory_region_is_logging(mr)) {
+        flags |= KVM_MEM_LOG_DIRTY_PAGES;
+    }
     if (readonly && kvm_readonly_mem_allowed) {
         flags |= KVM_MEM_READONLY;
     }
@@ -313,7 +317,10 @@ 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, false);
+    flags = mem->flags & ~mask;
+    if (log_dirty) {
+        flags |= KVM_MEM_LOG_DIRTY_PAGES;
+    }
     mem->flags = flags;
 
     /* If nothing changed effectively, no need to issue ioctl */
@@ -663,9 +670,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
     KVMSlot *mem, old;
     int err;
     MemoryRegion *mr = section->mr;
-    bool log_dirty = memory_region_is_logging(mr);
     bool writeable = !mr->readonly && !mr->rom_device;
-    bool readonly_flag = mr->readonly || memory_region_is_romd(mr);
     hwaddr start_addr = section->offset_within_address_space;
     ram_addr_t size = int128_get64(section->size);
     void *ram = NULL;
@@ -709,7 +714,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
             (ram - start_addr == mem->ram - mem->start_addr)) {
             /* The new slot fits into the existing one and comes with
              * identical parameters - update flags and done. */
-            kvm_slot_dirty_pages_log_change(mem, log_dirty);
+            kvm_slot_dirty_pages_log_change(mem, memory_region_is_logging(mr));
             return;
         }
 
@@ -742,7 +747,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, readonly_flag);
+            mem->flags = kvm_mem_flags(mr);
 
             err = kvm_set_user_memory_region(s, mem);
             if (err) {
@@ -763,7 +768,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, readonly_flag);
+            mem->flags =  kvm_mem_flags(mr);
 
             err = kvm_set_user_memory_region(s, mem);
             if (err) {
@@ -787,7 +792,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, readonly_flag);
+            mem->flags = kvm_mem_flags(mr);
 
             err = kvm_set_user_memory_region(s, mem);
             if (err) {
@@ -809,7 +814,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, readonly_flag);
+    mem->flags = kvm_mem_flags(mr);
 
     err = kvm_set_user_memory_region(s, mem);
     if (err) {
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/7 kvm-all: remove useless typedef

In reply to Paolo Bonzini
---
 kvm-all.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kvm-all.c +1 -3
index b459855..bb9477c 100644
@@ -69,8 +69,6 @@ typedef struct KVMSlot
     int flags;
 } KVMSlot;
 
-typedef struct kvm_dirty_log KVMDirtyLog;
-
 struct KVMState
 {
     AccelState parent_obj;
@@ -425,7 +423,7 @@ static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
 {
     KVMState *s = kvm_state;
     unsigned long size, allocated_size = 0;
-    KVMDirtyLog d = {};
+    struct kvm_dirty_log d = {};
     KVMSlot *mem;
     int ret = 0;
     hwaddr start_addr = section->offset_within_address_space;
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/7 kvm-all: move internal types to kvm_int.h

In reply to Paolo Bonzini

i386 code will have to define a different KVMMemoryListener. Create an internal header so that KVMSlot is not exposed outside.

---
 include/sysemu/kvm_int.h | 30 ++++++++++++++++++++++++++++++
 kvm-all.c                | 18 +-----------------
 2 files changed, 31 insertions(+), 17 deletions(-)
 create mode 100644 include/sysemu/kvm_int.h
include/sysemu/kvm_int.h new file +30 -0
new file mode 100644
index 0000000..e8dcbd7
@@ -0,0 +1,30 @@
+/*
+ * Internal definitions for a target's KVM support
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_KVM_INT_H
+#define QEMU_KVM_INT_H
+
+#include "sysemu/sysemu.h"
+#include "sysemu/accel.h"
+#include "sysemu/kvm.h"
+
+typedef struct KVMSlot
+{
+    hwaddr start_addr;
+    ram_addr_t memory_size;
+    void *ram;
+    int slot;
+    int flags;
+} KVMSlot;
+
+#define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm")
+
+#define KVM_STATE(obj) \
+    OBJECT_CHECK(KVMState, (obj), TYPE_KVM_ACCEL)
+
+#endif
kvm-all.c +1 -17
index bb9477c..4499ff8 100644
@@ -24,13 +24,11 @@
 #include "qemu/atomic.h"
 #include "qemu/option.h"
 #include "qemu/config-file.h"
-#include "sysemu/sysemu.h"
-#include "sysemu/accel.h"
 #include "hw/hw.h"
 #include "hw/pci/msi.h"
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
-#include "sysemu/kvm.h"
+#include "sysemu/kvm_int.h"
 #include "qemu/bswap.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
@@ -60,15 +58,6 @@
 
 #define KVM_MSI_HASHTAB_SIZE    256
 
-typedef struct KVMSlot
-{
-    hwaddr start_addr;
-    ram_addr_t memory_size;
-    void *ram;
-    int slot;
-    int flags;
-} KVMSlot;
-
 struct KVMState
 {
     AccelState parent_obj;
@@ -107,11 +96,6 @@ struct KVMState
 #endif
 };
 
-#define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm")
-
-#define KVM_STATE(obj) \
-    OBJECT_CHECK(KVMState, (obj), TYPE_KVM_ACCEL)
-
 KVMState *kvm_state;
 bool kvm_kernel_irqchip;
 bool kvm_async_interrupts_allowed;
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/7 kvm-all: make KVM's memory listener more generic

In reply to Paolo Bonzini

No semantic change, but s->slots and s->migration_log move into a new struct KVMMemoryListener. KVM's memory listener becomes a member of struct KVMState, and becomes of type KVMMemoryListener.

---
 include/sysemu/kvm_int.h |   6 ++
 kvm-all.c                | 174 +++++++++++++++++++++++++++--------------------
 2 files changed, 106 insertions(+), 74 deletions(-)
include/sysemu/kvm_int.h +6 -0
index e8dcbd7..f2698fa 100644
@@ -22,6 +22,12 @@ typedef struct KVMSlot
     int flags;
 } KVMSlot;
 
+typedef struct KVMMemoryListener {
+    MemoryListener listener;
+    KVMSlot *slots;
+    int migration_log;
+} KVMMemoryListener;
+
 #define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm")
 
 #define KVM_STATE(obj) \
kvm-all.c +100 -74

464 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 5/7 kvm-all: add support for multiple address spaces

In reply to Paolo Bonzini

Make kvm_memory_listener_register public, and assign a kernel address space id to each KVMMemoryListener.

---
 include/sysemu/kvm_int.h |  4 ++++
 kvm-all.c                | 13 ++++++-------
 2 files changed, 10 insertions(+), 7 deletions(-)
include/sysemu/kvm_int.h +4 -0
index f2698fa..9d67a2a 100644
@@ -26,6 +26,7 @@ typedef struct KVMMemoryListener {
     MemoryListener listener;
     KVMSlot *slots;
     int migration_log;
+    int as_id;
 } KVMMemoryListener;
 
 #define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm")
@@ -33,4 +34,7 @@ typedef struct KVMMemoryListener {
 #define KVM_STATE(obj) \
     OBJECT_CHECK(KVMState, (obj), TYPE_KVM_ACCEL)
 
+void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
+                                  AddressSpace *as, int as_id);
+
 #endif
kvm-all.c +6 -7
index 2700182..f8552a9 100644
@@ -217,7 +217,7 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot)
     KVMState *s = kvm_state;
     struct kvm_userspace_memory_region mem;
 
-    mem.slot = slot->slot;
+    mem.slot = slot->slot | (kml->as_id << 16);
     mem.guest_phys_addr = slot->start_addr;
     mem.userspace_addr = (unsigned long)slot->ram;
     mem.flags = slot->flags;
@@ -449,8 +449,7 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml,
         allocated_size = size;
         memset(d.dirty_bitmap, 0, allocated_size);
 
-        d.slot = mem->slot;
-
+        d.slot = mem->slot | (kml->as_id << 16);
         if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) {
             DPRINTF("ioctl failed %d\n", errno);
             ret = -1;
@@ -931,13 +930,13 @@ static void kvm_io_ioeventfd_del(MemoryListener *listener,
     }
 }
 
-static void kvm_memory_listener_register(KVMState *s,
-                                         KVMMemoryListener *kml,
-                                         AddressSpace *as)
+void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
+                                  AddressSpace *as, int as_id)
 {
     int i;
 
     kml->slots = g_malloc0(s->nr_slots * sizeof(KVMSlot));
+    kml->as_id = as_id;
 
     for (i = 0; i < s->nr_slots; i++) {
         kml->slots[i].slot = i;
@@ -1654,7 +1653,7 @@ static int kvm_init(MachineState *ms)
     s->memory_listener.listener.coalesced_mmio_del = kvm_uncoalesce_mmio_region;
 
     kvm_memory_listener_register(s, &s->memory_listener,
-                                 &address_space_memory);
+                                 &address_space_memory, 0);
     memory_listener_register(&kvm_io_listener,
                              &address_space_io);
 
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH 6/7 kvm-all: kvm_irqchip_create is not expected to fail

In reply to Paolo Bonzini

KVM_CREATE_IRQCHIP should never fail, and so should its userspace wrapper kvm_irqchip_create. The function does not do anything if the irqchip capability is not available, as is the case for PPC.

With this patch, kvm_arch_init can allocate memory and it will not be leaked.

---
 kvm-all.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
kvm-all.c +18 -17
index f8552a9..ce18b40 100644
@@ -1391,27 +1391,31 @@ int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, int virq)
            false);
 }
 
-static int kvm_irqchip_create(MachineState *machine, KVMState *s)
+static void kvm_irqchip_create(MachineState *machine, KVMState *s)
 {
     int ret;
 
-    if (!machine_kernel_irqchip_allowed(machine) ||
-        (!kvm_check_extension(s, KVM_CAP_IRQCHIP) &&
-         (kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0) < 0))) {
-        return 0;
+    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
+        ;
+    } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
+        ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
+        if (ret < 0) {
+            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
+            exit(1);
+        }
+    } else {
+        return;
     }
 
     /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
     ret = kvm_arch_irqchip_create(s);
-    if (ret < 0) {
-        return ret;
-    } else if (ret == 0) {
+    if (ret == 0) {
         ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
-        if (ret < 0) {
-            fprintf(stderr, "Create kernel irqchip failed\n");
-            return ret;
-        }
+    }
+    if (ret < 0) {
+        fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret));
+        exit(1);
     }
 
     kvm_kernel_irqchip = true;
@@ -1422,8 +1426,6 @@ static int kvm_irqchip_create(MachineState *machine, KVMState *s)
     kvm_halt_in_kernel_allowed = true;
 
     kvm_init_irq_routing(s);
-
-    return 0;
 }
 
 /* Find number of supported CPUs using the recommended
@@ -1640,9 +1642,8 @@ static int kvm_init(MachineState *ms)
         goto err;
     }
 
-    ret = kvm_irqchip_create(ms, s);
-    if (ret < 0) {
-        goto err;
+    if (machine_kernel_irqchip_allowed(ms)) {
+        kvm_irqchip_create(ms, s);
     }
 
     kvm_state = s;
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH 7/7 target-i386: register a separate KVM address space including SMRAM regions

In reply to Paolo Bonzini
---
 target-i386/kvm.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
target-i386/kvm.c +39 -1
index 09b4fc7..19ccc08 100644
@@ -22,7 +22,7 @@
 
 #include "qemu-common.h"
 #include "sysemu/sysemu.h"
-#include "sysemu/kvm.h"
+#include "sysemu/kvm_int.h"
 #include "kvm_i386.h"
 #include "cpu.h"
 #include "exec/gdbstub.h"
@@ -846,6 +846,39 @@ static int kvm_get_supported_msrs(KVMState *s)
     return ret;
 }
 
+static Notifier smram_machine_done;
+static KVMMemoryListener smram_listener;
+static AddressSpace smram_address_space;
+static MemoryRegion smram_as_root;
+static MemoryRegion smram_as_mem;
+
+static void register_smram_listener(Notifier *n, void *unused)
+{
+    MemoryRegion *smram =
+        (MemoryRegion *) object_resolve_path("/machine/smram", NULL);
+
+    /* Outer container... */
+    memory_region_init(&smram_as_root, OBJECT(kvm_state), "mem-container-smram", ~0ull);
+    memory_region_set_enabled(&smram_as_root, true);
+
+    /* ... with two regions inside: normal system memory with low
+     * priority, and...
+     */
+    memory_region_init_alias(&smram_as_mem, OBJECT(kvm_state), "mem-smram",
+			     get_system_memory(), 0, ~0ull);
+    memory_region_add_subregion_overlap(&smram_as_root, 0, &smram_as_mem, 0);
+    memory_region_set_enabled(&smram_as_mem, true);
+
+    if (smram) {
+	    memory_region_add_subregion_overlap(&smram_as_root, 0, smram, 10);
+            memory_region_set_enabled(smram, true);
+    }
+
+    address_space_init(&smram_address_space, &smram_as_root, "KVM-SMRAM");
+    kvm_memory_listener_register(kvm_state, &smram_listener,
+				 &smram_address_space, 1);
+}
+
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
     uint64_t identity_base = 0xfffbc000;
@@ -904,6 +937,11 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
             return ret;
         }
     }
+
+    if (kvm_check_extension(s, KVM_CAP_X86_SMM)) {
+        smram_machine_done.notify = register_smram_listener;
+        qemu_add_machine_init_done_notifier(&smram_machine_done);
+    }
     return 0;
 }
 
Signature · 1 line
1.8.3.1