Skip to contentSkip to conversation

Remove AddressSpaceOps

PATCH In review Avi Kivity 3 participants 15 messages Feb 8, 2012 – Feb 9, 2012

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Remove AddressSpaceOps

This patchset makes the memory core (memory.c) talk to the backend (in exec.c) via a MemoryListener instead of named functions.

While the motivation for this is to simplify the memory core, it also enables optimizing accelerators some more (by having a tcg MemoryListener to do tcg specific core) and allows unit testing of memory.c (by adding a testing MemoryListener and seeing what it outputs as various inputs are fed into the core).

Avi Kivity (10):
ioport: change portio_list not to use memory_region_set_offset()
memory: remove memory_region_set_offset()
memory: add shorthand for invoking a callback on all listeners
memory: switch memory listeners to a QTAILQ
memory: code motion: move MEMORY_LISTENER_CALL()
memory: move ioeventfd ops to MemoryListener
memory: add a readonly attribute to MemoryRegionSection
memory: don't pass ->readable attribute to
cpu_register_physical_memory_log
memory: use a MemoryListener for core memory map updates too
memory: drop AddressSpaceOps

 exec-obsolete.h |    5 +-
 exec.c          |   77 +++++++++++++++-
 hw/vhost.c      |   15 +++
 ioport.c        |   25 ++++-
 ioport.h        |    1 +
 kvm-all.c       |   79 +++++++++++++++
 memory.c        |  288 ++++++++++++++++++-------------------------------------
 memory.h        |   19 ++--
 xen-all.c       |   15 +++
 9 files changed, 310 insertions(+), 214 deletions(-)
Signature · 1 line
1.7.9
author
#1 lore raw

PATCH 1/10 ioport: change portio_list not to use memory_region_set_offset()

memory_region_set_offset() will be going away soon, so don't use it. Use an alias instead.

---
 ioport.c |   25 +++++++++++++++++++------
 ioport.h |    1 +
 2 files changed, 20 insertions(+), 6 deletions(-)
ioport.c +19 -6
index 36fa3a4..505b252 100644
@@ -328,6 +328,7 @@ void portio_list_init(PortioList *piolist,
     piolist->ports = callbacks;
     piolist->nr = 0;
     piolist->regions = g_new0(MemoryRegion *, n);
+    piolist->aliases = g_new0(MemoryRegion *, n);
     piolist->address_space = NULL;
     piolist->opaque = opaque;
     piolist->name = name;
@@ -336,6 +337,7 @@ void portio_list_init(PortioList *piolist,
 void portio_list_destroy(PortioList *piolist)
 {
     g_free(piolist->regions);
+    g_free(piolist->aliases);
 }
 
 static void portio_list_add_1(PortioList *piolist,
@@ -345,7 +347,7 @@ static void portio_list_add_1(PortioList *piolist,
 {
     MemoryRegionPortio *pio;
     MemoryRegionOps *ops;
-    MemoryRegion *region;
+    MemoryRegion *region, *alias;
     unsigned i;
 
     /* Copy the sub-list and null-terminate it.  */
@@ -362,12 +364,19 @@ static void portio_list_add_1(PortioList *piolist,
     ops->old_portio = pio;
 
     region = g_new(MemoryRegion, 1);
+    alias = g_new(MemoryRegion, 1);
+    /*
+     * Use an alias so that the callback is called with an absolute address,
+     * rather than an offset relative to to start + off_low.
+     */
     memory_region_init_io(region, ops, piolist->opaque, piolist->name,
-                          off_high - off_low);
-    memory_region_set_offset(region, start + off_low);
+                          UINT64_MAX);
+    memory_region_init_alias(alias, piolist->name,
+                             region, start + off_low, off_high - off_low);
     memory_region_add_subregion(piolist->address_space,
-                                start + off_low, region);
+                                start + off_low, alias);
     piolist->regions[piolist->nr++] = region;
+    piolist->aliases[piolist->nr++] = alias;
 }
 
 void portio_list_add(PortioList *piolist,
@@ -409,15 +418,19 @@ void portio_list_add(PortioList *piolist,
 
 void portio_list_del(PortioList *piolist)
 {
-    MemoryRegion *mr;
+    MemoryRegion *mr, *alias;
     unsigned i;
 
     for (i = 0; i < piolist->nr; ++i) {
         mr = piolist->regions[i];
-        memory_region_del_subregion(piolist->address_space, mr);
+        alias = piolist->aliases[i];
+        memory_region_del_subregion(piolist->address_space, alias);
+        memory_region_destroy(alias);
         memory_region_destroy(mr);
         g_free((MemoryRegionOps *)mr->ops);
         g_free(mr);
+        g_free(alias);
         piolist->regions[i] = NULL;
+        piolist->aliases[i] = NULL;
     }
 }
ioport.h +1 -0
index ae3e9da..ab29c89 100644
@@ -60,6 +60,7 @@ typedef struct PortioList {
     struct MemoryRegion *address_space;
     unsigned nr;
     struct MemoryRegion **regions;
+    struct MemoryRegion **aliases;
     void *opaque;
     const char *name;
 } PortioList;
Signature · 1 line
1.7.9
author
#2 lore raw

PATCH 2/10 memory: remove memory_region_set_offset()

In reply to Avi Kivity

memory_region_set_offset() complicates the API, and has been deprecated since its introduction. Now that it is no longer used, remove it.

---
 memory.c |   26 ++++++++++----------------
 memory.h |    9 ---------
 2 files changed, 10 insertions(+), 25 deletions(-)
memory.c +10 -16
index 5e77d8a..382dded 100644
@@ -401,17 +401,17 @@ static void memory_region_iorange_read(IORange *iorange,
 
         *data = ((uint64_t)1 << (width * 8)) - 1;
         if (mrp) {
-            *data = mrp->read(mr->opaque, offset + mr->offset);
+            *data = mrp->read(mr->opaque, offset);
         } else if (width == 2) {
             mrp = find_portio(mr, offset, 1, false);
             assert(mrp);
-            *data = mrp->read(mr->opaque, offset + mr->offset) |
-                    (mrp->read(mr->opaque, offset + mr->offset + 1) << 8);
+            *data = mrp->read(mr->opaque, offset) |
+                    (mrp->read(mr->opaque, offset + 1) << 8);
         }
         return;
     }
     *data = 0;
-    access_with_adjusted_size(offset + mr->offset, data, width,
+    access_with_adjusted_size(offset, data, width,
                               mr->ops->impl.min_access_size,
                               mr->ops->impl.max_access_size,
                               memory_region_read_accessor, mr);
@@ -428,16 +428,16 @@ static void memory_region_iorange_write(IORange *iorange,
         const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true);
 
         if (mrp) {
-            mrp->write(mr->opaque, offset + mr->offset, data);
+            mrp->write(mr->opaque, offset, data);
         } else if (width == 2) {
             mrp = find_portio(mr, offset, 1, false);
             assert(mrp);
-            mrp->write(mr->opaque, offset + mr->offset, data & 0xff);
-            mrp->write(mr->opaque, offset + mr->offset + 1, data >> 8);
+            mrp->write(mr->opaque, offset, data & 0xff);
+            mrp->write(mr->opaque, offset + 1, data >> 8);
         }
         return;
     }
-    access_with_adjusted_size(offset + mr->offset, &data, width,
+    access_with_adjusted_size(offset, &data, width,
                               mr->ops->impl.min_access_size,
                               mr->ops->impl.max_access_size,
                               memory_region_write_accessor, mr);
@@ -863,7 +863,6 @@ void memory_region_init(MemoryRegion *mr,
         mr->size = int128_2_64();
     }
     mr->addr = 0;
-    mr->offset = 0;
     mr->subpage = false;
     mr->enabled = true;
     mr->terminates = false;
@@ -925,7 +924,7 @@ static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
     }
 
     /* FIXME: support unaligned access */
-    access_with_adjusted_size(addr + mr->offset, &data, size,
+    access_with_adjusted_size(addr, &data, size,
                               mr->ops->impl.min_access_size,
                               mr->ops->impl.max_access_size,
                               memory_region_read_accessor, mr);
@@ -979,7 +978,7 @@ static void memory_region_dispatch_write(MemoryRegion *mr,
     }
 
     /* FIXME: support unaligned access */
-    access_with_adjusted_size(addr + mr->offset, &data, size,
+    access_with_adjusted_size(addr, &data, size,
                               mr->ops->impl.min_access_size,
                               mr->ops->impl.max_access_size,
                               memory_region_write_accessor, mr);
@@ -1122,11 +1121,6 @@ bool memory_region_is_rom(MemoryRegion *mr)
     return mr->ram && mr->readonly;
 }
 
-void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset)
-{
-    mr->offset = offset;
-}
-
 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
 {
     uint8_t mask = 1 << client;
memory.h +0 -9
index 4cf8d2f..4763286 100644
@@ -115,7 +115,6 @@ struct MemoryRegion {
     MemoryRegion *parent;
     Int128 size;
     target_phys_addr_t addr;
-    target_phys_addr_t offset;
     void (*destructor)(MemoryRegion *mr);
     ram_addr_t ram_addr;
     IORange iorange;
@@ -359,14 +358,6 @@ bool memory_region_is_rom(MemoryRegion *mr);
 void *memory_region_get_ram_ptr(MemoryRegion *mr);
 
 /**
- * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
- *                           callbacks.
- *
- * This function is deprecated and should not be used in new code.
- */
-void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset);
-
-/**
  * memory_region_set_log: Turn dirty logging on or off for a region.
  *
  * Turns dirty logging on or off for a specified client (display, migration).
Signature · 1 line
1.7.9
author
#3 lore raw

PATCH 3/10 memory: add shorthand for invoking a callback on all listeners

In reply to Avi Kivity
---
 memory.c |   54 +++++++++++++++++++-----------------------------------
 1 files changed, 19 insertions(+), 35 deletions(-)
memory.c +19 -35
index 382dded..6afe414 100644
@@ -678,31 +678,23 @@ static void address_space_update_ioeventfds(AddressSpace *as)
     as->ioeventfd_nb = ioeventfd_nb;
 }
 
-typedef void ListenerCallback(MemoryListener *listener,
-                              MemoryRegionSection *mrs);
-
-/* Want "void (&MemoryListener::*callback)(const MemoryRegionSection& s)" */
-static void memory_listener_update_region(FlatRange *fr, AddressSpace *as,
-                                          size_t callback_offset)
-{
-    MemoryRegionSection section = {
-        .mr = fr->mr,
-        .address_space = as->root,
-        .offset_within_region = fr->offset_in_region,
-        .size = int128_get64(fr->addr.size),
-        .offset_within_address_space = int128_get64(fr->addr.start),
-    };
-    MemoryListener *listener;
-
-    QLIST_FOREACH(listener, &memory_listeners, link) {
-        ListenerCallback *callback
-            = *(ListenerCallback **)((void *)listener + callback_offset);
-        callback(listener, &section);
-    }
-}
-
-#define MEMORY_LISTENER_UPDATE_REGION(fr, as, callback) \
-    memory_listener_update_region(fr, as, offsetof(MemoryListener, callback))
+#define MEMORY_LISTENER_CALL(_callback, _args...)               \
+    do {                                                        \
+        MemoryListener *_listener;                              \
+                                                                \
+        QLIST_FOREACH(_listener, &memory_listeners, link) {     \
+            _listener->_callback(_listener, ##_args);           \
+        }                                                       \
+    } while (0)
+
+#define MEMORY_LISTENER_UPDATE_REGION(fr, as, callback)                 \
+    MEMORY_LISTENER_CALL(callback, &(MemoryRegionSection) {             \
+        .mr = (fr)->mr,                                                 \
+        .address_space = (as)->root,                                    \
+        .offset_within_region = (fr)->offset_in_region,                 \
+        .size = int128_get64((fr)->addr.size),                          \
+        .offset_within_address_space = int128_get64((fr)->addr.start),  \
+                })
 
 static void address_space_update_topology_pass(AddressSpace *as,
                                                FlatView old_view,
@@ -1483,23 +1475,15 @@ void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
 
 void memory_global_dirty_log_start(void)
 {
-    MemoryListener *listener;
-
     cpu_physical_memory_set_dirty_tracking(1);
     global_dirty_log = true;
-    QLIST_FOREACH(listener, &memory_listeners, link) {
-        listener->log_global_start(listener);
-    }
+    MEMORY_LISTENER_CALL(log_global_start);
 }
 
 void memory_global_dirty_log_stop(void)
 {
-    MemoryListener *listener;
-
     global_dirty_log = false;
-    QLIST_FOREACH(listener, &memory_listeners, link) {
-        listener->log_global_stop(listener);
-    }
+    MEMORY_LISTENER_CALL(log_global_stop);
     cpu_physical_memory_set_dirty_tracking(0);
 }
 
Signature · 1 line
1.7.9
author
#4 lore raw

PATCH 4/10 memory: switch memory listeners to a QTAILQ

In reply to Avi Kivity

This allows reverse iteration, which in turns allows consistent ordering among multiple listeners:

l1->add
l2->add
l2->del
l1->del

---
 hw/vhost.c |    1 +
 kvm-all.c  |    1 +
 memory.c   |   70 ++++++++++++++++++++++++++++++++++++++++++------------------
 memory.h   |    4 ++-
 xen-all.c  |    1 +
 5 files changed, 55 insertions(+), 22 deletions(-)
hw/vhost.c +1 -0
index 5ece659..4737145 100644
@@ -751,6 +751,7 @@ int vhost_dev_init(struct vhost_dev *hdev, int devfd, bool force)
         .log_sync = vhost_log_sync,
         .log_global_start = vhost_log_global_start,
         .log_global_stop = vhost_log_global_stop,
+        .priority = 10
     };
     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
     hdev->n_mem_sections = 0;
kvm-all.c +1 -0
index 0b87658..6835fd4 100644
@@ -726,6 +726,7 @@ static void kvm_log_global_stop(struct MemoryListener *listener)
     .log_sync = kvm_log_sync,
     .log_global_start = kvm_log_global_start,
     .log_global_stop = kvm_log_global_stop,
+    .priority = 10,
 };
 
 static void kvm_handle_interrupt(CPUState *env, int mask)
memory.c +49 -21

140 lines not on this page · raw message

memory.h +3 -1
index 4763286..954dc86 100644
@@ -185,7 +185,9 @@ struct MemoryListener {
     void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
     void (*log_global_start)(MemoryListener *listener);
     void (*log_global_stop)(MemoryListener *listener);
-    QLIST_ENTRY(MemoryListener) link;
+    /* Lower = earlier (during add), later (during del) */
+    unsigned priority;
+    QTAILQ_ENTRY(MemoryListener) link;
 };
 
 /**
xen-all.c +1 -0
index fd39168..8cb84ef 100644
@@ -495,6 +495,7 @@ static void xen_log_global_stop(MemoryListener *listener)
     .log_sync = xen_log_sync,
     .log_global_start = xen_log_global_start,
     .log_global_stop = xen_log_global_stop,
+    .priority = 10,
 };
 
 /* VCPU Operations, MMIO, IO ring ... */
Signature · 1 line
1.7.9
author
#5 lore raw

PATCH 5/10 memory: code motion: move MEMORY_LISTENER_CALL()

In reply to Avi Kivity

So it can be used in earlier code.

---
 memory.c |   64 +++++++++++++++++++++++++++++++-------------------------------
 1 files changed, 32 insertions(+), 32 deletions(-)
memory.c +32 -32
index cb2b4f1..a1013bc 100644
@@ -82,6 +82,38 @@ static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
     return addrrange_make(start, int128_sub(end, start));
 }
 
+enum ListenerDirection { Forward, Reverse };
+
+#define MEMORY_LISTENER_CALL(_callback, _direction, _args...)           \
+    do {                                                                \
+        MemoryListener *_listener;                                      \
+                                                                        \
+        switch (_direction) {                                           \
+        case Forward:                                                   \
+            QTAILQ_FOREACH(_listener, &memory_listeners, link) {        \
+                _listener->_callback(_listener, ##_args);               \
+            }                                                           \
+            break;                                                      \
+        case Reverse:                                                   \
+            QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners,        \
+                                   memory_listeners, link) {            \
+                _listener->_callback(_listener, ##_args);               \
+            }                                                           \
+            break;                                                      \
+        default:                                                        \
+            abort();                                                    \
+        }                                                               \
+    } while (0)
+
+#define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback)            \
+    MEMORY_LISTENER_CALL(callback, dir, &(MemoryRegionSection) {        \
+        .mr = (fr)->mr,                                                 \
+        .address_space = (as)->root,                                    \
+        .offset_within_region = (fr)->offset_in_region,                 \
+        .size = int128_get64((fr)->addr.size),                          \
+        .offset_within_address_space = int128_get64((fr)->addr.start),  \
+                })
+
 struct CoalescedMemoryRange {
     AddrRange addr;
     QTAILQ_ENTRY(CoalescedMemoryRange) link;
@@ -678,38 +710,6 @@ static void address_space_update_ioeventfds(AddressSpace *as)
     as->ioeventfd_nb = ioeventfd_nb;
 }
 
-enum ListenerDirection { Forward, Reverse };
-
-#define MEMORY_LISTENER_CALL(_callback, _direction, _args...)           \
-    do {                                                                \
-        MemoryListener *_listener;                                      \
-                                                                        \
-        switch (_direction) {                                           \
-        case Forward:                                                   \
-            QTAILQ_FOREACH(_listener, &memory_listeners, link) {        \
-                _listener->_callback(_listener, ##_args);               \
-            }                                                           \
-            break;                                                      \
-        case Reverse:                                                   \
-            QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners,        \
-                                   memory_listeners, link) {            \
-                _listener->_callback(_listener, ##_args);               \
-            }                                                           \
-            break;                                                      \
-        default:                                                        \
-            abort();                                                    \
-        }                                                               \
-    } while (0)
-
-#define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback)            \
-    MEMORY_LISTENER_CALL(callback, dir, &(MemoryRegionSection) {        \
-        .mr = (fr)->mr,                                                 \
-        .address_space = (as)->root,                                    \
-        .offset_within_region = (fr)->offset_in_region,                 \
-        .size = int128_get64((fr)->addr.size),                          \
-        .offset_within_address_space = int128_get64((fr)->addr.start),  \
-                })
-
 static void address_space_update_topology_pass(AddressSpace *as,
                                                FlatView old_view,
                                                FlatView new_view,
Signature · 1 line
1.7.9
author
#6 lore raw

PATCH 6/10 memory: move ioeventfd ops to MemoryListener

In reply to Avi Kivity

This way the accelerator (kvm) can handle them directly.

---
 hw/vhost.c |   14 ++++++++++
 kvm-all.c  |   78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 memory.c   |   74 ++++++++++++++-------------------------------------------
 memory.h   |    4 +++
 xen-all.c  |   14 ++++++++++
 5 files changed, 128 insertions(+), 56 deletions(-)
hw/vhost.c +14 -0
index 4737145..e1e7e01 100644
@@ -720,6 +720,18 @@ static void vhost_virtqueue_cleanup(struct vhost_dev *dev,
                               0, virtio_queue_get_desc_size(vdev, idx));
 }
 
+static void vhost_eventfd_add(MemoryListener *listener,
+                              MemoryRegionSection *section,
+                              bool match_data, uint64_t data, int fd)
+{
+}
+
+static void vhost_eventfd_del(MemoryListener *listener,
+                              MemoryRegionSection *section,
+                              bool match_data, uint64_t data, int fd)
+{
+}
+
 int vhost_dev_init(struct vhost_dev *hdev, int devfd, bool force)
 {
     uint64_t features;
@@ -751,6 +763,8 @@ int vhost_dev_init(struct vhost_dev *hdev, int devfd, bool force)
         .log_sync = vhost_log_sync,
         .log_global_start = vhost_log_global_start,
         .log_global_stop = vhost_log_global_stop,
+        .eventfd_add = vhost_eventfd_add,
+        .eventfd_del = vhost_eventfd_del,
         .priority = 10
     };
     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
kvm-all.c +78 -0

96 lines not on this page · raw message

memory.c +18 -56

117 lines not on this page · raw message

memory.h +4 -0
index 954dc86..84bb67c 100644
@@ -185,6 +185,10 @@ struct MemoryListener {
     void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
     void (*log_global_start)(MemoryListener *listener);
     void (*log_global_stop)(MemoryListener *listener);
+    void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
+                        bool match_data, uint64_t data, int fd);
+    void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
+                        bool match_data, uint64_t data, int fd);
     /* Lower = earlier (during add), later (during del) */
     unsigned priority;
     QTAILQ_ENTRY(MemoryListener) link;
xen-all.c +14 -0
index 8cb84ef..e005b63 100644
@@ -487,6 +487,18 @@ static void xen_log_global_stop(MemoryListener *listener)
 {
 }
 
+static void xen_eventfd_add(MemoryListener *listener,
+                            MemoryRegionSection *section,
+                            bool match_data, uint64_t data, int fd)
+{
+}
+
+static void xen_eventfd_del(MemoryListener *listener,
+                            MemoryRegionSection *section,
+                            bool match_data, uint64_t data, int fd)
+{
+}
+
 static MemoryListener xen_memory_listener = {
     .region_add = xen_region_add,
     .region_del = xen_region_del,
@@ -495,6 +507,8 @@ static void xen_log_global_stop(MemoryListener *listener)
     .log_sync = xen_log_sync,
     .log_global_start = xen_log_global_start,
     .log_global_stop = xen_log_global_stop,
+    .eventfd_add = xen_eventfd_add,
+    .eventfd_del = xen_eventfd_del,
     .priority = 10,
 };
 
Signature · 1 line
1.7.9
author
#7 lore raw

PATCH 7/10 memory: add a readonly attribute to MemoryRegionSection

In reply to Avi Kivity

.readonly cannot be obtained from the MemoryRegion, since it is inherited from aliases (so you can have a MemoryRegion mapped RW at one address and RO at another). Record it in a MemoryRegionSection for listeners.

---
 memory.c |    5 +++++
 memory.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
memory.c +5 -0
index e1a31d4..4f37152 100644
@@ -112,6 +112,7 @@ static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
         .offset_within_region = (fr)->offset_in_region,                 \
         .size = int128_get64((fr)->addr.size),                          \
         .offset_within_address_space = int128_get64((fr)->addr.start),  \
+        .readonly = (fr)->readonly,                                     \
                 })
 
 struct CoalescedMemoryRange {
@@ -342,6 +343,7 @@ static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
         .offset_within_address_space = int128_get64(fr->addr.start),
         .offset_within_region = fr->offset_in_region,
         .size = int128_get64(fr->addr.size),
+        .readonly = fr->readonly,
     };
 
     cpu_register_physical_memory_log(&section, fr->readable, fr->readonly);
@@ -354,6 +356,7 @@ static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
         .offset_within_address_space = int128_get64(fr->addr.start),
         .offset_within_region = int128_get64(fr->addr.start),
         .size = int128_get64(fr->addr.size),
+        .readonly = fr->readonly,
     };
 
     cpu_register_physical_memory_log(&section, true, false);
@@ -1437,6 +1440,7 @@ MemoryRegionSection memory_region_find(MemoryRegion *address_space,
                                                         fr->addr.start));
     ret.size = int128_get64(range.size);
     ret.offset_within_address_space = int128_get64(range.start);
+    ret.readonly = fr->readonly;
     return ret;
 }
 
@@ -1479,6 +1483,7 @@ static void listener_add_address_space(MemoryListener *listener,
             .offset_within_region = fr->offset_in_region,
             .size = int128_get64(fr->addr.size),
             .offset_within_address_space = int128_get64(fr->addr.start),
+            .readonly = fr->readonly,
         };
         listener->region_add(listener, &section);
     }
memory.h +2 -0
index 84bb67c..1d99cee 100644
@@ -160,6 +160,7 @@ typedef struct MemoryRegionSection MemoryRegionSection;
  * @size: the size of the section; will not exceed @mr's boundaries
  * @offset_within_address_space: the address of the first byte of the section
  *     relative to the region's address space
+ * @readonly: writes to this section are ignored
  */
 struct MemoryRegionSection {
     MemoryRegion *mr;
@@ -167,6 +168,7 @@ struct MemoryRegionSection {
     target_phys_addr_t offset_within_region;
     uint64_t size;
     target_phys_addr_t offset_within_address_space;
+    bool readonly;
 };
 
 typedef struct MemoryListener MemoryListener;
Signature · 1 line
1.7.9
author
#8 lore raw

PATCH 8/10 memory: don't pass ->readable attribute to cpu_register_physical_memory_log

In reply to Avi Kivity

It can be derived from the MemoryRegion itself (which is why it is not used there).

---
 exec-obsolete.h |    2 +-
 exec.c          |    2 +-
 memory.c        |    4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
exec-obsolete.h +1 -1
index 94c23d0..23ffbaa 100644
@@ -37,7 +37,7 @@ void cpu_unregister_io_memory(int table_address);
 
 struct MemoryRegionSection;
 void cpu_register_physical_memory_log(struct MemoryRegionSection *section,
-                                      bool readable, bool readonly);
+                                      bool readonly);
 
 void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size);
 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size);
exec.c +1 -1
index b81677a..d3020ab 100644
@@ -2529,7 +2529,7 @@ static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
    before calculating this offset.  This should not be a problem unless
    the low bits of start_addr and region_offset differ.  */
 void cpu_register_physical_memory_log(MemoryRegionSection *section,
-                                      bool readable, bool readonly)
+                                      bool readonly)
 {
     target_phys_addr_t start_addr = section->offset_within_address_space;
     ram_addr_t size = section->size;
memory.c +2 -2
index 4f37152..71039c4 100644
@@ -346,7 +346,7 @@ static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
         .readonly = fr->readonly,
     };
 
-    cpu_register_physical_memory_log(&section, fr->readable, fr->readonly);
+    cpu_register_physical_memory_log(&section, fr->readonly);
 }
 
 static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
@@ -359,7 +359,7 @@ static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
         .readonly = fr->readonly,
     };
 
-    cpu_register_physical_memory_log(&section, true, false);
+    cpu_register_physical_memory_log(&section, false);
 }
 
 static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
Signature · 1 line
1.7.9
author
#9 lore raw

PATCH 9/10 memory: use a MemoryListener for core memory map updates too

In reply to Avi Kivity

This transforms memory.c into a library which can then be unit tested easily, by feeding it inputs and listening to its outputs.

---
 exec-obsolete.h |    3 ++
 exec.c          |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 memory.c        |   27 +-------------------
 3 files changed, 79 insertions(+), 26 deletions(-)
exec-obsolete.h +3 -0
index 23ffbaa..4dbe476 100644
@@ -121,6 +121,9 @@ static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
 
 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
                                      int dirty_flags);
+
+extern const IORangeOps memory_region_iorange_ops;
+
 #endif
 
 #endif
exec.c +75 -0

87 lines not on this page · raw message

memory.c +1 -26
index 71039c4..4e7a90b 100644
@@ -338,28 +338,10 @@ static void access_with_adjusted_size(target_phys_addr_t addr,
 
 static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
 {
-    MemoryRegionSection section = {
-        .mr = fr->mr,
-        .offset_within_address_space = int128_get64(fr->addr.start),
-        .offset_within_region = fr->offset_in_region,
-        .size = int128_get64(fr->addr.size),
-        .readonly = fr->readonly,
-    };
-
-    cpu_register_physical_memory_log(&section, fr->readonly);
 }
 
 static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
 {
-    MemoryRegionSection section = {
-        .mr = &io_mem_unassigned,
-        .offset_within_address_space = int128_get64(fr->addr.start),
-        .offset_within_region = int128_get64(fr->addr.start),
-        .size = int128_get64(fr->addr.size),
-        .readonly = fr->readonly,
-    };
-
-    cpu_register_physical_memory_log(&section, false);
 }
 
 static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
@@ -450,22 +432,17 @@ static void memory_region_iorange_write(IORange *iorange,
                               memory_region_write_accessor, mr);
 }
 
-static const IORangeOps memory_region_iorange_ops = {
+const IORangeOps memory_region_iorange_ops = {
     .read = memory_region_iorange_read,
     .write = memory_region_iorange_write,
 };
 
 static void as_io_range_add(AddressSpace *as, FlatRange *fr)
 {
-    iorange_init(&fr->mr->iorange, &memory_region_iorange_ops,
-                 int128_get64(fr->addr.start), int128_get64(fr->addr.size));
-    ioport_register(&fr->mr->iorange);
 }
 
 static void as_io_range_del(AddressSpace *as, FlatRange *fr)
 {
-    isa_unassign_ioport(int128_get64(fr->addr.start),
-                        int128_get64(fr->addr.size));
 }
 
 static const AddressSpaceOps address_space_ops_io = {
@@ -1456,7 +1433,6 @@ void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
 
 void memory_global_dirty_log_start(void)
 {
-    cpu_physical_memory_set_dirty_tracking(1);
     global_dirty_log = true;
     MEMORY_LISTENER_CALL(log_global_start, Forward);
 }
@@ -1465,7 +1441,6 @@ void memory_global_dirty_log_stop(void)
 {
     global_dirty_log = false;
     MEMORY_LISTENER_CALL(log_global_stop, Reverse);
-    cpu_physical_memory_set_dirty_tracking(0);
 }
 
 static void listener_add_address_space(MemoryListener *listener,
Signature · 1 line
1.7.9
#10 lore raw

Re: [Qemu-devel] [PATCH 09/10] memory: use a MemoryListener for core memory map updates too

exec.c static void io_mem_init(void) · line 3528

Quoted 10 lines · Avi Kivity
exec.c +10 -0
+static void core_log_stop(MemoryListener *listener,
+                          MemoryRegionSection *section)
+{
+}
+
+static void core_log_sync(MemoryListener *listener,
+                          MemoryRegionSection *section)
+{
+}
+

Why not wrapping the calls inside an "if" so that these dummy functions need not be there? Can be done in a follow-up, though.

Paolo

author
#11 lore raw

Re: [Qemu-devel] [PATCH 09/10] memory: use a MemoryListener for core memory map updates too

Quoted 14 lines · Paolo Bonzini

exec.c static void io_mem_init(void) · line 3528

Quoted 10 lines · Avi Kivity
exec.c +10 -0
+static void core_log_stop(MemoryListener *listener,
+                          MemoryRegionSection *section)
+{
+}
+
+static void core_log_sync(MemoryListener *listener,
+                          MemoryRegionSection *section)
+{
+}
+

Why not wrapping the calls inside an "if" so that these dummy functions need not be there? Can be done in a follow-up, though.

I dislike that style. We could have memory.c provide default versions for users to put into their listener structures.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#12 lore raw

PATCH 10/10 memory: drop AddressSpaceOps

In reply to Avi Kivity

All functionality has been moved to various MemoryListeners.

---
 memory.c |   56 ++------------------------------------------------------
 1 files changed, 2 insertions(+), 54 deletions(-)
memory.c +2 -54
index 4e7a90b..4f854d4 100644
@@ -191,20 +191,12 @@ struct FlatView {
 
 /* A system address space - I/O, memory, etc. */
 struct AddressSpace {
-    const AddressSpaceOps *ops;
     MemoryRegion *root;
     FlatView current_map;
     int ioeventfd_nb;
     MemoryRegionIoeventfd *ioeventfds;
 };
 
-struct AddressSpaceOps {
-    void (*range_add)(AddressSpace *as, FlatRange *fr);
-    void (*range_del)(AddressSpace *as, FlatRange *fr);
-    void (*log_start)(AddressSpace *as, FlatRange *fr);
-    void (*log_stop)(AddressSpace *as, FlatRange *fr);
-};
-
 #define FOR_EACH_FLAT_RANGE(var, view)          \
     for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
 
@@ -336,32 +328,7 @@ static void access_with_adjusted_size(target_phys_addr_t addr,
     }
 }
 
-static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
-{
-}
-
-static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
-{
-}
-
-static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
-{
-}
-
-static void as_memory_log_stop(AddressSpace *as, FlatRange *fr)
-{
-}
-
-static const AddressSpaceOps address_space_ops_memory = {
-    .range_add = as_memory_range_add,
-    .range_del = as_memory_range_del,
-    .log_start = as_memory_log_start,
-    .log_stop = as_memory_log_stop,
-};
-
-static AddressSpace address_space_memory = {
-    .ops = &address_space_ops_memory,
-};
+static AddressSpace address_space_memory;
 
 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
                                              unsigned width, bool write)
@@ -437,22 +404,7 @@ static void memory_region_iorange_write(IORange *iorange,
     .write = memory_region_iorange_write,
 };
 
-static void as_io_range_add(AddressSpace *as, FlatRange *fr)
-{
-}
-
-static void as_io_range_del(AddressSpace *as, FlatRange *fr)
-{
-}
-
-static const AddressSpaceOps address_space_ops_io = {
-    .range_add = as_io_range_add,
-    .range_del = as_io_range_del,
-};
-
-static AddressSpace address_space_io = {
-    .ops = &address_space_ops_io,
-};
+static AddressSpace address_space_io;
 
 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
 {
@@ -685,7 +637,6 @@ static void address_space_update_topology_pass(AddressSpace *as,
 
             if (!adding) {
                 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
-                as->ops->range_del(as, frold);
             }
 
             ++iold;
@@ -695,9 +646,7 @@ static void address_space_update_topology_pass(AddressSpace *as,
             if (adding) {
                 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
                     MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
-                    as->ops->log_stop(as, frnew);
                 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
-                    as->ops->log_start(as, frnew);
                     MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
                 }
             }
@@ -708,7 +657,6 @@ static void address_space_update_topology_pass(AddressSpace *as,
             /* In new */
 
             if (adding) {
-                as->ops->range_add(as, frnew);
                 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
             }
 
Signature · 1 line
1.7.9
author
#13 lore raw
In reply to Avi Kivity
Quoted 9 lines · Avi Kivity

This patchset makes the memory core (memory.c) talk to the backend (in exec.c) via a MemoryListener instead of named functions.

While the motivation for this is to simplify the memory core, it also enables optimizing accelerators some more (by having a tcg MemoryListener to do tcg specific core) and allows unit testing of memory.c (by adding a testing MemoryListener and seeing what it outputs as various inputs are fed into the core).

Something that is very visible here is that MemoryListeners switch quite a lot on the address space. This suggests that we need to allow observing a specific address space. I'll address that in a future patch.

Signature · 1 line
error compiling committee.c: too many arguments to function
Reviewed-by on the v1 series
#14 lore raw
In reply to Avi Kivity
Quoted 9 lines · Avi Kivity

This patchset makes the memory core (memory.c) talk to the backend (in exec.c) via a MemoryListener instead of named functions.

While the motivation for this is to simplify the memory core, it also enables optimizing accelerators some more (by having a tcg MemoryListener to do tcg specific core) and allows unit testing of memory.c (by adding a testing MemoryListener and seeing what it outputs as various inputs are fed into the core).

Quoted 12 lines

Avi Kivity (10):
ioport: change portio_list not to use memory_region_set_offset()
memory: remove memory_region_set_offset()
memory: add shorthand for invoking a callback on all listeners
memory: switch memory listeners to a QTAILQ
memory: code motion: move MEMORY_LISTENER_CALL()
memory: move ioeventfd ops to MemoryListener
memory: add a readonly attribute to MemoryRegionSection
memory: don't pass ->readable attribute to
cpu_register_physical_memory_log
memory: use a MemoryListener for core memory map updates too
memory: drop AddressSpaceOps

Looks good.

r~