Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Physical memory management |
| 3 | * |
| 4 | * Copyright 2011 Red Hat, Inc. and/or its affiliates |
| 5 | * |
| 6 | * Authors: |
| 7 | * Avi Kivity <avi@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 14 | */ |
| 15 | |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 16 | #include "exec/memory.h" |
| 17 | #include "exec/address-spaces.h" |
| 18 | #include "exec/ioport.h" |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 19 | #include "qapi/visitor.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 20 | #include "qemu/bitops.h" |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 21 | #include "qom/object.h" |
Paolo Bonzini | 55d5d04 | 2013-07-28 14:57:22 +0200 | [diff] [blame] | 22 | #include "trace.h" |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 25 | #include "exec/memory-internal.h" |
Juan Quintela | 220c3eb | 2013-10-14 17:13:59 +0200 | [diff] [blame] | 26 | #include "exec/ram_addr.h" |
Paolo Bonzini | e1c57ab | 2014-05-14 17:43:18 +0800 | [diff] [blame] | 27 | #include "sysemu/sysemu.h" |
Avi Kivity | 67d95c1 | 2011-12-15 15:25:22 +0200 | [diff] [blame] | 28 | |
Paolo Bonzini | d197063 | 2013-05-24 13:23:38 +0200 | [diff] [blame] | 29 | //#define DEBUG_UNASSIGNED |
| 30 | |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 31 | static unsigned memory_region_transaction_depth; |
| 32 | static bool memory_region_update_pending; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 33 | static bool ioeventfd_update_pending; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 34 | static bool global_dirty_log = false; |
| 35 | |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 36 | static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners |
| 37 | = QTAILQ_HEAD_INITIALIZER(memory_listeners); |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 38 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 39 | static QTAILQ_HEAD(, AddressSpace) address_spaces |
| 40 | = QTAILQ_HEAD_INITIALIZER(address_spaces); |
| 41 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 42 | typedef struct AddrRange AddrRange; |
| 43 | |
Avi Kivity | 8417ceb | 2011-08-03 11:56:14 +0300 | [diff] [blame] | 44 | /* |
Fam Zheng | c9cdaa3 | 2014-08-11 10:18:31 +0800 | [diff] [blame] | 45 | * Note that signed integers are needed for negative offsetting in aliases |
Avi Kivity | 8417ceb | 2011-08-03 11:56:14 +0300 | [diff] [blame] | 46 | * (large MemoryRegion::alias_offset). |
| 47 | */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 48 | struct AddrRange { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 49 | Int128 start; |
| 50 | Int128 size; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 51 | }; |
| 52 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 53 | static AddrRange addrrange_make(Int128 start, Int128 size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 54 | { |
| 55 | return (AddrRange) { start, size }; |
| 56 | } |
| 57 | |
| 58 | static bool addrrange_equal(AddrRange r1, AddrRange r2) |
| 59 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 60 | return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 61 | } |
| 62 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 63 | static Int128 addrrange_end(AddrRange r) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 64 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 65 | return int128_add(r.start, r.size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 66 | } |
| 67 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 68 | static AddrRange addrrange_shift(AddrRange range, Int128 delta) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 69 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 70 | int128_addto(&range.start, delta); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 71 | return range; |
| 72 | } |
| 73 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 74 | static bool addrrange_contains(AddrRange range, Int128 addr) |
| 75 | { |
| 76 | return int128_ge(addr, range.start) |
| 77 | && int128_lt(addr, addrrange_end(range)); |
| 78 | } |
| 79 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 80 | static bool addrrange_intersects(AddrRange r1, AddrRange r2) |
| 81 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 82 | return addrrange_contains(r1, r2.start) |
| 83 | || addrrange_contains(r2, r1.start); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2) |
| 87 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 88 | Int128 start = int128_max(r1.start, r2.start); |
| 89 | Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2)); |
| 90 | return addrrange_make(start, int128_sub(end, start)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 91 | } |
| 92 | |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 93 | enum ListenerDirection { Forward, Reverse }; |
| 94 | |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 95 | static bool memory_listener_match(MemoryListener *listener, |
| 96 | MemoryRegionSection *section) |
| 97 | { |
| 98 | return !listener->address_space_filter |
| 99 | || listener->address_space_filter == section->address_space; |
| 100 | } |
| 101 | |
| 102 | #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 103 | do { \ |
| 104 | MemoryListener *_listener; \ |
| 105 | \ |
| 106 | switch (_direction) { \ |
| 107 | case Forward: \ |
| 108 | QTAILQ_FOREACH(_listener, &memory_listeners, link) { \ |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 109 | if (_listener->_callback) { \ |
| 110 | _listener->_callback(_listener, ##_args); \ |
| 111 | } \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 112 | } \ |
| 113 | break; \ |
| 114 | case Reverse: \ |
| 115 | QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \ |
| 116 | memory_listeners, link) { \ |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 117 | if (_listener->_callback) { \ |
| 118 | _listener->_callback(_listener, ##_args); \ |
| 119 | } \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 120 | } \ |
| 121 | break; \ |
| 122 | default: \ |
| 123 | abort(); \ |
| 124 | } \ |
| 125 | } while (0) |
| 126 | |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 127 | #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \ |
| 128 | do { \ |
| 129 | MemoryListener *_listener; \ |
| 130 | \ |
| 131 | switch (_direction) { \ |
| 132 | case Forward: \ |
| 133 | QTAILQ_FOREACH(_listener, &memory_listeners, link) { \ |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 134 | if (_listener->_callback \ |
| 135 | && memory_listener_match(_listener, _section)) { \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 136 | _listener->_callback(_listener, _section, ##_args); \ |
| 137 | } \ |
| 138 | } \ |
| 139 | break; \ |
| 140 | case Reverse: \ |
| 141 | QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \ |
| 142 | memory_listeners, link) { \ |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 143 | if (_listener->_callback \ |
| 144 | && memory_listener_match(_listener, _section)) { \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 145 | _listener->_callback(_listener, _section, ##_args); \ |
| 146 | } \ |
| 147 | } \ |
| 148 | break; \ |
| 149 | default: \ |
| 150 | abort(); \ |
| 151 | } \ |
| 152 | } while (0) |
| 153 | |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 154 | /* No need to ref/unref .mr, the FlatRange keeps it alive. */ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 155 | #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 156 | MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 157 | .mr = (fr)->mr, \ |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 158 | .address_space = (as), \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 159 | .offset_within_region = (fr)->offset_in_region, \ |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 160 | .size = (fr)->addr.size, \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 161 | .offset_within_address_space = int128_get64((fr)->addr.start), \ |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 162 | .readonly = (fr)->readonly, \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 163 | })) |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 164 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 165 | struct CoalescedMemoryRange { |
| 166 | AddrRange addr; |
| 167 | QTAILQ_ENTRY(CoalescedMemoryRange) link; |
| 168 | }; |
| 169 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 170 | struct MemoryRegionIoeventfd { |
| 171 | AddrRange addr; |
| 172 | bool match_data; |
| 173 | uint64_t data; |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 174 | EventNotifier *e; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a, |
| 178 | MemoryRegionIoeventfd b) |
| 179 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 180 | if (int128_lt(a.addr.start, b.addr.start)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 181 | return true; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 182 | } else if (int128_gt(a.addr.start, b.addr.start)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 183 | return false; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 184 | } else if (int128_lt(a.addr.size, b.addr.size)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 185 | return true; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 186 | } else if (int128_gt(a.addr.size, b.addr.size)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 187 | return false; |
| 188 | } else if (a.match_data < b.match_data) { |
| 189 | return true; |
| 190 | } else if (a.match_data > b.match_data) { |
| 191 | return false; |
| 192 | } else if (a.match_data) { |
| 193 | if (a.data < b.data) { |
| 194 | return true; |
| 195 | } else if (a.data > b.data) { |
| 196 | return false; |
| 197 | } |
| 198 | } |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 199 | if (a.e < b.e) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 200 | return true; |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 201 | } else if (a.e > b.e) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a, |
| 208 | MemoryRegionIoeventfd b) |
| 209 | { |
| 210 | return !memory_region_ioeventfd_before(a, b) |
| 211 | && !memory_region_ioeventfd_before(b, a); |
| 212 | } |
| 213 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 214 | typedef struct FlatRange FlatRange; |
| 215 | typedef struct FlatView FlatView; |
| 216 | |
| 217 | /* Range of memory in the global map. Addresses are absolute. */ |
| 218 | struct FlatRange { |
| 219 | MemoryRegion *mr; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 220 | hwaddr offset_in_region; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 221 | AddrRange addr; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 222 | uint8_t dirty_log_mask; |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 223 | bool romd_mode; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 224 | bool readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | /* Flattened global view of current active memory hierarchy. Kept in sorted |
| 228 | * order. |
| 229 | */ |
| 230 | struct FlatView { |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 231 | struct rcu_head rcu; |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 232 | unsigned ref; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 233 | FlatRange *ranges; |
| 234 | unsigned nr; |
| 235 | unsigned nr_allocated; |
| 236 | }; |
| 237 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 238 | typedef struct AddressSpaceOps AddressSpaceOps; |
| 239 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 240 | #define FOR_EACH_FLAT_RANGE(var, view) \ |
| 241 | for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var) |
| 242 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 243 | static bool flatrange_equal(FlatRange *a, FlatRange *b) |
| 244 | { |
| 245 | return a->mr == b->mr |
| 246 | && addrrange_equal(a->addr, b->addr) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 247 | && a->offset_in_region == b->offset_in_region |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 248 | && a->romd_mode == b->romd_mode |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 249 | && a->readonly == b->readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void flatview_init(FlatView *view) |
| 253 | { |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 254 | view->ref = 1; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 255 | view->ranges = NULL; |
| 256 | view->nr = 0; |
| 257 | view->nr_allocated = 0; |
| 258 | } |
| 259 | |
| 260 | /* Insert a range into a given position. Caller is responsible for maintaining |
| 261 | * sorting order. |
| 262 | */ |
| 263 | static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range) |
| 264 | { |
| 265 | if (view->nr == view->nr_allocated) { |
| 266 | view->nr_allocated = MAX(2 * view->nr, 10); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 267 | view->ranges = g_realloc(view->ranges, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 268 | view->nr_allocated * sizeof(*view->ranges)); |
| 269 | } |
| 270 | memmove(view->ranges + pos + 1, view->ranges + pos, |
| 271 | (view->nr - pos) * sizeof(FlatRange)); |
| 272 | view->ranges[pos] = *range; |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 273 | memory_region_ref(range->mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 274 | ++view->nr; |
| 275 | } |
| 276 | |
| 277 | static void flatview_destroy(FlatView *view) |
| 278 | { |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 279 | int i; |
| 280 | |
| 281 | for (i = 0; i < view->nr; i++) { |
| 282 | memory_region_unref(view->ranges[i].mr); |
| 283 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 284 | g_free(view->ranges); |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 285 | g_free(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 286 | } |
| 287 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 288 | static void flatview_ref(FlatView *view) |
| 289 | { |
| 290 | atomic_inc(&view->ref); |
| 291 | } |
| 292 | |
| 293 | static void flatview_unref(FlatView *view) |
| 294 | { |
| 295 | if (atomic_fetch_dec(&view->ref) == 1) { |
| 296 | flatview_destroy(view); |
| 297 | } |
| 298 | } |
| 299 | |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 300 | static bool can_merge(FlatRange *r1, FlatRange *r2) |
| 301 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 302 | return int128_eq(addrrange_end(r1->addr), r2->addr.start) |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 303 | && r1->mr == r2->mr |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 304 | && int128_eq(int128_add(int128_make64(r1->offset_in_region), |
| 305 | r1->addr.size), |
| 306 | int128_make64(r2->offset_in_region)) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 307 | && r1->dirty_log_mask == r2->dirty_log_mask |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 308 | && r1->romd_mode == r2->romd_mode |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 309 | && r1->readonly == r2->readonly; |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 310 | } |
| 311 | |
Peter Crosthwaite | 8508e02 | 2013-06-03 15:31:56 +1000 | [diff] [blame] | 312 | /* Attempt to simplify a view by merging adjacent ranges */ |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 313 | static void flatview_simplify(FlatView *view) |
| 314 | { |
| 315 | unsigned i, j; |
| 316 | |
| 317 | i = 0; |
| 318 | while (i < view->nr) { |
| 319 | j = i + 1; |
| 320 | while (j < view->nr |
| 321 | && can_merge(&view->ranges[j-1], &view->ranges[j])) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 322 | int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size); |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 323 | ++j; |
| 324 | } |
| 325 | ++i; |
| 326 | memmove(&view->ranges[i], &view->ranges[j], |
| 327 | (view->nr - j) * sizeof(view->ranges[j])); |
| 328 | view->nr -= j - i; |
| 329 | } |
| 330 | } |
| 331 | |
Paolo Bonzini | e7342aa | 2013-07-22 15:54:37 +0200 | [diff] [blame] | 332 | static bool memory_region_big_endian(MemoryRegion *mr) |
| 333 | { |
| 334 | #ifdef TARGET_WORDS_BIGENDIAN |
| 335 | return mr->ops->endianness != DEVICE_LITTLE_ENDIAN; |
| 336 | #else |
| 337 | return mr->ops->endianness == DEVICE_BIG_ENDIAN; |
| 338 | #endif |
| 339 | } |
| 340 | |
Paolo Bonzini | e11ef3d | 2013-07-22 15:54:35 +0200 | [diff] [blame] | 341 | static bool memory_region_wrong_endianness(MemoryRegion *mr) |
| 342 | { |
| 343 | #ifdef TARGET_WORDS_BIGENDIAN |
| 344 | return mr->ops->endianness == DEVICE_LITTLE_ENDIAN; |
| 345 | #else |
| 346 | return mr->ops->endianness == DEVICE_BIG_ENDIAN; |
| 347 | #endif |
| 348 | } |
| 349 | |
| 350 | static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size) |
| 351 | { |
| 352 | if (memory_region_wrong_endianness(mr)) { |
| 353 | switch (size) { |
| 354 | case 1: |
| 355 | break; |
| 356 | case 2: |
| 357 | *data = bswap16(*data); |
| 358 | break; |
| 359 | case 4: |
| 360 | *data = bswap32(*data); |
| 361 | break; |
| 362 | case 8: |
| 363 | *data = bswap64(*data); |
| 364 | break; |
| 365 | default: |
| 366 | abort(); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
Paolo Bonzini | 547e920 | 2013-07-22 15:54:36 +0200 | [diff] [blame] | 371 | static void memory_region_oldmmio_read_accessor(MemoryRegion *mr, |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 372 | hwaddr addr, |
| 373 | uint64_t *value, |
| 374 | unsigned size, |
| 375 | unsigned shift, |
| 376 | uint64_t mask) |
| 377 | { |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 378 | uint64_t tmp; |
| 379 | |
| 380 | tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr); |
Paolo Bonzini | 55d5d04 | 2013-07-28 14:57:22 +0200 | [diff] [blame] | 381 | trace_memory_region_ops_read(mr, addr, tmp, size); |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 382 | *value |= (tmp & mask) << shift; |
| 383 | } |
| 384 | |
Paolo Bonzini | 547e920 | 2013-07-22 15:54:36 +0200 | [diff] [blame] | 385 | static void memory_region_read_accessor(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 386 | hwaddr addr, |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 387 | uint64_t *value, |
| 388 | unsigned size, |
| 389 | unsigned shift, |
| 390 | uint64_t mask) |
| 391 | { |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 392 | uint64_t tmp; |
| 393 | |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 394 | if (mr->flush_coalesced_mmio) { |
| 395 | qemu_flush_coalesced_mmio_buffer(); |
| 396 | } |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 397 | tmp = mr->ops->read(mr->opaque, addr, size); |
Paolo Bonzini | 55d5d04 | 2013-07-28 14:57:22 +0200 | [diff] [blame] | 398 | trace_memory_region_ops_read(mr, addr, tmp, size); |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 399 | *value |= (tmp & mask) << shift; |
| 400 | } |
| 401 | |
Paolo Bonzini | 547e920 | 2013-07-22 15:54:36 +0200 | [diff] [blame] | 402 | static void memory_region_oldmmio_write_accessor(MemoryRegion *mr, |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 403 | hwaddr addr, |
| 404 | uint64_t *value, |
| 405 | unsigned size, |
| 406 | unsigned shift, |
| 407 | uint64_t mask) |
| 408 | { |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 409 | uint64_t tmp; |
| 410 | |
| 411 | tmp = (*value >> shift) & mask; |
Paolo Bonzini | 55d5d04 | 2013-07-28 14:57:22 +0200 | [diff] [blame] | 412 | trace_memory_region_ops_write(mr, addr, tmp, size); |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 413 | mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp); |
| 414 | } |
| 415 | |
Paolo Bonzini | 547e920 | 2013-07-22 15:54:36 +0200 | [diff] [blame] | 416 | static void memory_region_write_accessor(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 417 | hwaddr addr, |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 418 | uint64_t *value, |
| 419 | unsigned size, |
| 420 | unsigned shift, |
| 421 | uint64_t mask) |
| 422 | { |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 423 | uint64_t tmp; |
| 424 | |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 425 | if (mr->flush_coalesced_mmio) { |
| 426 | qemu_flush_coalesced_mmio_buffer(); |
| 427 | } |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 428 | tmp = (*value >> shift) & mask; |
Paolo Bonzini | 55d5d04 | 2013-07-28 14:57:22 +0200 | [diff] [blame] | 429 | trace_memory_region_ops_write(mr, addr, tmp, size); |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 430 | mr->ops->write(mr->opaque, addr, tmp, size); |
| 431 | } |
| 432 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 433 | static void access_with_adjusted_size(hwaddr addr, |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 434 | uint64_t *value, |
| 435 | unsigned size, |
| 436 | unsigned access_size_min, |
| 437 | unsigned access_size_max, |
Paolo Bonzini | 547e920 | 2013-07-22 15:54:36 +0200 | [diff] [blame] | 438 | void (*access)(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 439 | hwaddr addr, |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 440 | uint64_t *value, |
| 441 | unsigned size, |
| 442 | unsigned shift, |
| 443 | uint64_t mask), |
Paolo Bonzini | 547e920 | 2013-07-22 15:54:36 +0200 | [diff] [blame] | 444 | MemoryRegion *mr) |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 445 | { |
| 446 | uint64_t access_mask; |
| 447 | unsigned access_size; |
| 448 | unsigned i; |
| 449 | |
| 450 | if (!access_size_min) { |
| 451 | access_size_min = 1; |
| 452 | } |
| 453 | if (!access_size_max) { |
| 454 | access_size_max = 4; |
| 455 | } |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 456 | |
| 457 | /* FIXME: support unaligned access? */ |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 458 | access_size = MAX(MIN(size, access_size_max), access_size_min); |
| 459 | access_mask = -1ULL >> (64 - access_size * 8); |
Paolo Bonzini | e7342aa | 2013-07-22 15:54:37 +0200 | [diff] [blame] | 460 | if (memory_region_big_endian(mr)) { |
| 461 | for (i = 0; i < size; i += access_size) { |
| 462 | access(mr, addr + i, value, access_size, |
| 463 | (size - access_size - i) * 8, access_mask); |
| 464 | } |
| 465 | } else { |
| 466 | for (i = 0; i < size; i += access_size) { |
| 467 | access(mr, addr + i, value, access_size, i * 8, access_mask); |
| 468 | } |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 472 | static AddressSpace *memory_region_to_address_space(MemoryRegion *mr) |
| 473 | { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 474 | AddressSpace *as; |
| 475 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 476 | while (mr->container) { |
| 477 | mr = mr->container; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 478 | } |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 479 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 480 | if (mr == as->root) { |
| 481 | return as; |
| 482 | } |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 483 | } |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 484 | return NULL; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 487 | /* Render a memory region into the global view. Ranges in @view obscure |
| 488 | * ranges in @mr. |
| 489 | */ |
| 490 | static void render_memory_region(FlatView *view, |
| 491 | MemoryRegion *mr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 492 | Int128 base, |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 493 | AddrRange clip, |
| 494 | bool readonly) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 495 | { |
| 496 | MemoryRegion *subregion; |
| 497 | unsigned i; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 498 | hwaddr offset_in_region; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 499 | Int128 remain; |
| 500 | Int128 now; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 501 | FlatRange fr; |
| 502 | AddrRange tmp; |
| 503 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 504 | if (!mr->enabled) { |
| 505 | return; |
| 506 | } |
| 507 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 508 | int128_addto(&base, int128_make64(mr->addr)); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 509 | readonly |= mr->readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 510 | |
| 511 | tmp = addrrange_make(base, mr->size); |
| 512 | |
| 513 | if (!addrrange_intersects(tmp, clip)) { |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | clip = addrrange_intersection(tmp, clip); |
| 518 | |
| 519 | if (mr->alias) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 520 | int128_subfrom(&base, int128_make64(mr->alias->addr)); |
| 521 | int128_subfrom(&base, int128_make64(mr->alias_offset)); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 522 | render_memory_region(view, mr->alias, base, clip, readonly); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
| 526 | /* Render subregions in priority order. */ |
| 527 | QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) { |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 528 | render_memory_region(view, subregion, base, clip, readonly); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 529 | } |
| 530 | |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 531 | if (!mr->terminates) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 535 | offset_in_region = int128_get64(int128_sub(clip.start, base)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 536 | base = clip.start; |
| 537 | remain = clip.size; |
| 538 | |
Peter Crosthwaite | 2eb74e1 | 2013-06-03 15:33:29 +1000 | [diff] [blame] | 539 | fr.mr = mr; |
| 540 | fr.dirty_log_mask = mr->dirty_log_mask; |
| 541 | fr.romd_mode = mr->romd_mode; |
| 542 | fr.readonly = readonly; |
| 543 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 544 | /* Render the region itself into any gaps left by the current view. */ |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 545 | for (i = 0; i < view->nr && int128_nz(remain); ++i) { |
| 546 | if (int128_ge(base, addrrange_end(view->ranges[i].addr))) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 547 | continue; |
| 548 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 549 | if (int128_lt(base, view->ranges[i].addr.start)) { |
| 550 | now = int128_min(remain, |
| 551 | int128_sub(view->ranges[i].addr.start, base)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 552 | fr.offset_in_region = offset_in_region; |
| 553 | fr.addr = addrrange_make(base, now); |
| 554 | flatview_insert(view, i, &fr); |
| 555 | ++i; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 556 | int128_addto(&base, now); |
| 557 | offset_in_region += int128_get64(now); |
| 558 | int128_subfrom(&remain, now); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 559 | } |
Avi Kivity | d26a8ca | 2012-10-29 18:22:36 +0200 | [diff] [blame] | 560 | now = int128_sub(int128_min(int128_add(base, remain), |
| 561 | addrrange_end(view->ranges[i].addr)), |
| 562 | base); |
| 563 | int128_addto(&base, now); |
| 564 | offset_in_region += int128_get64(now); |
| 565 | int128_subfrom(&remain, now); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 566 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 567 | if (int128_nz(remain)) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 568 | fr.offset_in_region = offset_in_region; |
| 569 | fr.addr = addrrange_make(base, remain); |
| 570 | flatview_insert(view, i, &fr); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* Render a memory topology into a list of disjoint absolute ranges. */ |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 575 | static FlatView *generate_memory_topology(MemoryRegion *mr) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 576 | { |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 577 | FlatView *view; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 578 | |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 579 | view = g_new(FlatView, 1); |
| 580 | flatview_init(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 581 | |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 582 | if (mr) { |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 583 | render_memory_region(view, mr, int128_zero(), |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 584 | addrrange_make(int128_zero(), int128_2_64()), false); |
| 585 | } |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 586 | flatview_simplify(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 587 | |
| 588 | return view; |
| 589 | } |
| 590 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 591 | static void address_space_add_del_ioeventfds(AddressSpace *as, |
| 592 | MemoryRegionIoeventfd *fds_new, |
| 593 | unsigned fds_new_nb, |
| 594 | MemoryRegionIoeventfd *fds_old, |
| 595 | unsigned fds_old_nb) |
| 596 | { |
| 597 | unsigned iold, inew; |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 598 | MemoryRegionIoeventfd *fd; |
| 599 | MemoryRegionSection section; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 600 | |
| 601 | /* Generate a symmetric difference of the old and new fd sets, adding |
| 602 | * and deleting as necessary. |
| 603 | */ |
| 604 | |
| 605 | iold = inew = 0; |
| 606 | while (iold < fds_old_nb || inew < fds_new_nb) { |
| 607 | if (iold < fds_old_nb |
| 608 | && (inew == fds_new_nb |
| 609 | || memory_region_ioeventfd_before(fds_old[iold], |
| 610 | fds_new[inew]))) { |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 611 | fd = &fds_old[iold]; |
| 612 | section = (MemoryRegionSection) { |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 613 | .address_space = as, |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 614 | .offset_within_address_space = int128_get64(fd->addr.start), |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 615 | .size = fd->addr.size, |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 616 | }; |
| 617 | MEMORY_LISTENER_CALL(eventfd_del, Forward, §ion, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 618 | fd->match_data, fd->data, fd->e); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 619 | ++iold; |
| 620 | } else if (inew < fds_new_nb |
| 621 | && (iold == fds_old_nb |
| 622 | || memory_region_ioeventfd_before(fds_new[inew], |
| 623 | fds_old[iold]))) { |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 624 | fd = &fds_new[inew]; |
| 625 | section = (MemoryRegionSection) { |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 626 | .address_space = as, |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 627 | .offset_within_address_space = int128_get64(fd->addr.start), |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 628 | .size = fd->addr.size, |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 629 | }; |
| 630 | MEMORY_LISTENER_CALL(eventfd_add, Reverse, §ion, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 631 | fd->match_data, fd->data, fd->e); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 632 | ++inew; |
| 633 | } else { |
| 634 | ++iold; |
| 635 | ++inew; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 640 | static FlatView *address_space_get_flatview(AddressSpace *as) |
| 641 | { |
| 642 | FlatView *view; |
| 643 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 644 | rcu_read_lock(); |
| 645 | view = atomic_rcu_read(&as->current_map); |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 646 | flatview_ref(view); |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 647 | rcu_read_unlock(); |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 648 | return view; |
| 649 | } |
| 650 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 651 | static void address_space_update_ioeventfds(AddressSpace *as) |
| 652 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 653 | FlatView *view; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 654 | FlatRange *fr; |
| 655 | unsigned ioeventfd_nb = 0; |
| 656 | MemoryRegionIoeventfd *ioeventfds = NULL; |
| 657 | AddrRange tmp; |
| 658 | unsigned i; |
| 659 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 660 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 661 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 662 | for (i = 0; i < fr->mr->ioeventfd_nb; ++i) { |
| 663 | tmp = addrrange_shift(fr->mr->ioeventfds[i].addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 664 | int128_sub(fr->addr.start, |
| 665 | int128_make64(fr->offset_in_region))); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 666 | if (addrrange_intersects(fr->addr, tmp)) { |
| 667 | ++ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 668 | ioeventfds = g_realloc(ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 669 | ioeventfd_nb * sizeof(*ioeventfds)); |
| 670 | ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i]; |
| 671 | ioeventfds[ioeventfd_nb-1].addr = tmp; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb, |
| 677 | as->ioeventfds, as->ioeventfd_nb); |
| 678 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 679 | g_free(as->ioeventfds); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 680 | as->ioeventfds = ioeventfds; |
| 681 | as->ioeventfd_nb = ioeventfd_nb; |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 682 | flatview_unref(view); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 683 | } |
| 684 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 685 | static void address_space_update_topology_pass(AddressSpace *as, |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 686 | const FlatView *old_view, |
| 687 | const FlatView *new_view, |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 688 | bool adding) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 689 | { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 690 | unsigned iold, inew; |
| 691 | FlatRange *frold, *frnew; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 692 | |
| 693 | /* Generate a symmetric difference of the old and new memory maps. |
| 694 | * Kill ranges in the old map, and instantiate ranges in the new map. |
| 695 | */ |
| 696 | iold = inew = 0; |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 697 | while (iold < old_view->nr || inew < new_view->nr) { |
| 698 | if (iold < old_view->nr) { |
| 699 | frold = &old_view->ranges[iold]; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 700 | } else { |
| 701 | frold = NULL; |
| 702 | } |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 703 | if (inew < new_view->nr) { |
| 704 | frnew = &new_view->ranges[inew]; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 705 | } else { |
| 706 | frnew = NULL; |
| 707 | } |
| 708 | |
| 709 | if (frold |
| 710 | && (!frnew |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 711 | || int128_lt(frold->addr.start, frnew->addr.start) |
| 712 | || (int128_eq(frold->addr.start, frnew->addr.start) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 713 | && !flatrange_equal(frold, frnew)))) { |
Peter Crosthwaite | 41a6e47 | 2013-06-03 15:32:42 +1000 | [diff] [blame] | 714 | /* In old but not in new, or in both but attributes changed. */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 715 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 716 | if (!adding) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 717 | MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 718 | } |
| 719 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 720 | ++iold; |
| 721 | } else if (frold && frnew && flatrange_equal(frold, frnew)) { |
Peter Crosthwaite | 41a6e47 | 2013-06-03 15:32:42 +1000 | [diff] [blame] | 722 | /* In both and unchanged (except logging may have changed) */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 723 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 724 | if (adding) { |
Avi Kivity | 50c1e14 | 2012-02-08 21:36:02 +0200 | [diff] [blame] | 725 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 726 | if (frold->dirty_log_mask && !frnew->dirty_log_mask) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 727 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 728 | } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 729 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 730 | } |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 731 | } |
| 732 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 733 | ++iold; |
| 734 | ++inew; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 735 | } else { |
| 736 | /* In new */ |
| 737 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 738 | if (adding) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 739 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 740 | } |
| 741 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 742 | ++inew; |
| 743 | } |
| 744 | } |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | |
| 748 | static void address_space_update_topology(AddressSpace *as) |
| 749 | { |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 750 | FlatView *old_view = address_space_get_flatview(as); |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 751 | FlatView *new_view = generate_memory_topology(as->root); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 752 | |
| 753 | address_space_update_topology_pass(as, old_view, new_view, false); |
| 754 | address_space_update_topology_pass(as, old_view, new_view, true); |
| 755 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 756 | /* Writes are protected by the BQL. */ |
| 757 | atomic_rcu_set(&as->current_map, new_view); |
| 758 | call_rcu(old_view, flatview_unref, rcu); |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 759 | |
| 760 | /* Note that all the old MemoryRegions are still alive up to this |
| 761 | * point. This relieves most MemoryListeners from the need to |
| 762 | * ref/unref the MemoryRegions they get---unless they use them |
| 763 | * outside the iothread mutex, in which case precise reference |
| 764 | * counting is necessary. |
| 765 | */ |
| 766 | flatview_unref(old_view); |
| 767 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 768 | address_space_update_ioeventfds(as); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 769 | } |
| 770 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 771 | void memory_region_transaction_begin(void) |
| 772 | { |
Jan Kiszka | bb880de | 2012-08-23 13:02:32 +0200 | [diff] [blame] | 773 | qemu_flush_coalesced_mmio_buffer(); |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 774 | ++memory_region_transaction_depth; |
| 775 | } |
| 776 | |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 777 | static void memory_region_clear_pending(void) |
| 778 | { |
| 779 | memory_region_update_pending = false; |
| 780 | ioeventfd_update_pending = false; |
| 781 | } |
| 782 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 783 | void memory_region_transaction_commit(void) |
| 784 | { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 785 | AddressSpace *as; |
| 786 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 787 | assert(memory_region_transaction_depth); |
| 788 | --memory_region_transaction_depth; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 789 | if (!memory_region_transaction_depth) { |
| 790 | if (memory_region_update_pending) { |
| 791 | MEMORY_LISTENER_CALL_GLOBAL(begin, Forward); |
Jan Kiszka | 02e2b95 | 2012-08-23 13:02:31 +0200 | [diff] [blame] | 792 | |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 793 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 794 | address_space_update_topology(as); |
| 795 | } |
| 796 | |
| 797 | MEMORY_LISTENER_CALL_GLOBAL(commit, Forward); |
| 798 | } else if (ioeventfd_update_pending) { |
| 799 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 800 | address_space_update_ioeventfds(as); |
| 801 | } |
Jan Kiszka | 02e2b95 | 2012-08-23 13:02:31 +0200 | [diff] [blame] | 802 | } |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 803 | memory_region_clear_pending(); |
| 804 | } |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 805 | } |
| 806 | |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 807 | static void memory_region_destructor_none(MemoryRegion *mr) |
| 808 | { |
| 809 | } |
| 810 | |
| 811 | static void memory_region_destructor_ram(MemoryRegion *mr) |
| 812 | { |
| 813 | qemu_ram_free(mr->ram_addr); |
| 814 | } |
| 815 | |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 816 | static void memory_region_destructor_alias(MemoryRegion *mr) |
| 817 | { |
| 818 | memory_region_unref(mr->alias); |
| 819 | } |
| 820 | |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 821 | static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr) |
| 822 | { |
| 823 | qemu_ram_free_from_ptr(mr->ram_addr); |
| 824 | } |
| 825 | |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 826 | static void memory_region_destructor_rom_device(MemoryRegion *mr) |
| 827 | { |
| 828 | qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 829 | } |
| 830 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 831 | static bool memory_region_need_escape(char c) |
| 832 | { |
| 833 | return c == '/' || c == '[' || c == '\\' || c == ']'; |
| 834 | } |
| 835 | |
| 836 | static char *memory_region_escape_name(const char *name) |
| 837 | { |
| 838 | const char *p; |
| 839 | char *escaped, *q; |
| 840 | uint8_t c; |
| 841 | size_t bytes = 0; |
| 842 | |
| 843 | for (p = name; *p; p++) { |
| 844 | bytes += memory_region_need_escape(*p) ? 4 : 1; |
| 845 | } |
| 846 | if (bytes == p - name) { |
| 847 | return g_memdup(name, bytes + 1); |
| 848 | } |
| 849 | |
| 850 | escaped = g_malloc(bytes + 1); |
| 851 | for (p = name, q = escaped; *p; p++) { |
| 852 | c = *p; |
| 853 | if (unlikely(memory_region_need_escape(c))) { |
| 854 | *q++ = '\\'; |
| 855 | *q++ = 'x'; |
| 856 | *q++ = "0123456789abcdef"[c >> 4]; |
| 857 | c = "0123456789abcdef"[c & 15]; |
| 858 | } |
| 859 | *q++ = c; |
| 860 | } |
| 861 | *q = 0; |
| 862 | return escaped; |
| 863 | } |
| 864 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 865 | void memory_region_init(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 866 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 867 | const char *name, |
| 868 | uint64_t size) |
| 869 | { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 870 | if (!owner) { |
| 871 | owner = qdev_get_machine(); |
| 872 | } |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 873 | |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 874 | object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION); |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 875 | mr->size = int128_make64(size); |
| 876 | if (size == UINT64_MAX) { |
| 877 | mr->size = int128_2_64(); |
| 878 | } |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 879 | mr->name = g_strdup(name); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 880 | |
| 881 | if (name) { |
Peter Crosthwaite | 843ef73 | 2014-08-19 23:56:26 -0700 | [diff] [blame] | 882 | char *escaped_name = memory_region_escape_name(name); |
| 883 | char *name_array = g_strdup_printf("%s[*]", escaped_name); |
| 884 | object_property_add_child(owner, name_array, OBJECT(mr), &error_abort); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 885 | object_unref(OBJECT(mr)); |
Peter Crosthwaite | 843ef73 | 2014-08-19 23:56:26 -0700 | [diff] [blame] | 886 | g_free(name_array); |
| 887 | g_free(escaped_name); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 891 | static void memory_region_get_addr(Object *obj, Visitor *v, void *opaque, |
| 892 | const char *name, Error **errp) |
| 893 | { |
| 894 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 895 | uint64_t value = mr->addr; |
| 896 | |
| 897 | visit_type_uint64(v, &value, name, errp); |
| 898 | } |
| 899 | |
| 900 | static void memory_region_get_container(Object *obj, Visitor *v, void *opaque, |
| 901 | const char *name, Error **errp) |
| 902 | { |
| 903 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 904 | gchar *path = (gchar *)""; |
| 905 | |
| 906 | if (mr->container) { |
| 907 | path = object_get_canonical_path(OBJECT(mr->container)); |
| 908 | } |
| 909 | visit_type_str(v, &path, name, errp); |
| 910 | if (mr->container) { |
| 911 | g_free(path); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | static Object *memory_region_resolve_container(Object *obj, void *opaque, |
| 916 | const char *part) |
| 917 | { |
| 918 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 919 | |
| 920 | return OBJECT(mr->container); |
| 921 | } |
| 922 | |
Peter Crosthwaite | d33382d | 2014-06-05 23:17:01 -0700 | [diff] [blame] | 923 | static void memory_region_get_priority(Object *obj, Visitor *v, void *opaque, |
| 924 | const char *name, Error **errp) |
| 925 | { |
| 926 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 927 | int32_t value = mr->priority; |
| 928 | |
| 929 | visit_type_int32(v, &value, name, errp); |
| 930 | } |
| 931 | |
| 932 | static bool memory_region_get_may_overlap(Object *obj, Error **errp) |
| 933 | { |
| 934 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 935 | |
| 936 | return mr->may_overlap; |
| 937 | } |
| 938 | |
Peter Crosthwaite | 52aef7b | 2014-06-05 23:17:35 -0700 | [diff] [blame] | 939 | static void memory_region_get_size(Object *obj, Visitor *v, void *opaque, |
| 940 | const char *name, Error **errp) |
| 941 | { |
| 942 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 943 | uint64_t value = memory_region_size(mr); |
| 944 | |
| 945 | visit_type_uint64(v, &value, name, errp); |
| 946 | } |
| 947 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 948 | static void memory_region_initfn(Object *obj) |
| 949 | { |
| 950 | MemoryRegion *mr = MEMORY_REGION(obj); |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 951 | ObjectProperty *op; |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 952 | |
| 953 | mr->ops = &unassigned_mem_ops; |
| 954 | mr->enabled = true; |
| 955 | mr->romd_mode = true; |
| 956 | mr->destructor = memory_region_destructor_none; |
| 957 | QTAILQ_INIT(&mr->subregions); |
| 958 | QTAILQ_INIT(&mr->coalesced); |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 959 | |
| 960 | op = object_property_add(OBJECT(mr), "container", |
| 961 | "link<" TYPE_MEMORY_REGION ">", |
| 962 | memory_region_get_container, |
| 963 | NULL, /* memory_region_set_container */ |
| 964 | NULL, NULL, &error_abort); |
| 965 | op->resolve = memory_region_resolve_container; |
| 966 | |
| 967 | object_property_add(OBJECT(mr), "addr", "uint64", |
| 968 | memory_region_get_addr, |
| 969 | NULL, /* memory_region_set_addr */ |
| 970 | NULL, NULL, &error_abort); |
Peter Crosthwaite | d33382d | 2014-06-05 23:17:01 -0700 | [diff] [blame] | 971 | object_property_add(OBJECT(mr), "priority", "uint32", |
| 972 | memory_region_get_priority, |
| 973 | NULL, /* memory_region_set_priority */ |
| 974 | NULL, NULL, &error_abort); |
| 975 | object_property_add_bool(OBJECT(mr), "may-overlap", |
| 976 | memory_region_get_may_overlap, |
| 977 | NULL, /* memory_region_set_may_overlap */ |
| 978 | &error_abort); |
Peter Crosthwaite | 52aef7b | 2014-06-05 23:17:35 -0700 | [diff] [blame] | 979 | object_property_add(OBJECT(mr), "size", "uint64", |
| 980 | memory_region_get_size, |
| 981 | NULL, /* memory_region_set_size, */ |
| 982 | NULL, NULL, &error_abort); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 983 | } |
| 984 | |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 985 | static uint64_t unassigned_mem_read(void *opaque, hwaddr addr, |
| 986 | unsigned size) |
| 987 | { |
| 988 | #ifdef DEBUG_UNASSIGNED |
| 989 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
| 990 | #endif |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 991 | if (current_cpu != NULL) { |
| 992 | cpu_unassigned_access(current_cpu, addr, false, false, 0, size); |
Andreas Färber | c658b94 | 2013-05-27 06:49:53 +0200 | [diff] [blame] | 993 | } |
Jan Kiszka | 68a7439 | 2013-09-02 18:43:31 +0200 | [diff] [blame] | 994 | return 0; |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | static void unassigned_mem_write(void *opaque, hwaddr addr, |
| 998 | uint64_t val, unsigned size) |
| 999 | { |
| 1000 | #ifdef DEBUG_UNASSIGNED |
| 1001 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val); |
| 1002 | #endif |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1003 | if (current_cpu != NULL) { |
| 1004 | cpu_unassigned_access(current_cpu, addr, true, false, 0, size); |
Andreas Färber | c658b94 | 2013-05-27 06:49:53 +0200 | [diff] [blame] | 1005 | } |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Paolo Bonzini | d197063 | 2013-05-24 13:23:38 +0200 | [diff] [blame] | 1008 | static bool unassigned_mem_accepts(void *opaque, hwaddr addr, |
| 1009 | unsigned size, bool is_write) |
| 1010 | { |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
| 1014 | const MemoryRegionOps unassigned_mem_ops = { |
| 1015 | .valid.accepts = unassigned_mem_accepts, |
| 1016 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 1017 | }; |
| 1018 | |
Paolo Bonzini | d270203 | 2013-05-24 11:55:06 +0200 | [diff] [blame] | 1019 | bool memory_region_access_valid(MemoryRegion *mr, |
| 1020 | hwaddr addr, |
| 1021 | unsigned size, |
| 1022 | bool is_write) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1023 | { |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1024 | int access_size_min, access_size_max; |
| 1025 | int access_size, i; |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 1026 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1027 | if (!mr->ops->valid.unaligned && (addr & (size - 1))) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1031 | if (!mr->ops->valid.accepts) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1032 | return true; |
| 1033 | } |
| 1034 | |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1035 | access_size_min = mr->ops->valid.min_access_size; |
| 1036 | if (!mr->ops->valid.min_access_size) { |
| 1037 | access_size_min = 1; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1038 | } |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1039 | |
| 1040 | access_size_max = mr->ops->valid.max_access_size; |
| 1041 | if (!mr->ops->valid.max_access_size) { |
| 1042 | access_size_max = 4; |
| 1043 | } |
| 1044 | |
| 1045 | access_size = MAX(MIN(size, access_size_max), access_size_min); |
| 1046 | for (i = 0; i < size; i += access_size) { |
| 1047 | if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size, |
| 1048 | is_write)) { |
| 1049 | return false; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1053 | return true; |
| 1054 | } |
| 1055 | |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1056 | static uint64_t memory_region_dispatch_read1(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1057 | hwaddr addr, |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1058 | unsigned size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1059 | { |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 1060 | uint64_t data = 0; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1061 | |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 1062 | if (mr->ops->read) { |
| 1063 | access_with_adjusted_size(addr, &data, size, |
| 1064 | mr->ops->impl.min_access_size, |
| 1065 | mr->ops->impl.max_access_size, |
| 1066 | memory_region_read_accessor, mr); |
| 1067 | } else { |
| 1068 | access_with_adjusted_size(addr, &data, size, 1, 4, |
| 1069 | memory_region_oldmmio_read_accessor, mr); |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 1070 | } |
| 1071 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1072 | return data; |
| 1073 | } |
| 1074 | |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1075 | static bool memory_region_dispatch_read(MemoryRegion *mr, |
| 1076 | hwaddr addr, |
| 1077 | uint64_t *pval, |
| 1078 | unsigned size) |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1079 | { |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1080 | if (!memory_region_access_valid(mr, addr, size, false)) { |
| 1081 | *pval = unassigned_mem_read(mr, addr, size); |
| 1082 | return true; |
| 1083 | } |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1084 | |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1085 | *pval = memory_region_dispatch_read1(mr, addr, size); |
| 1086 | adjust_endianness(mr, pval, size); |
| 1087 | return false; |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1090 | static bool memory_region_dispatch_write(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1091 | hwaddr addr, |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1092 | uint64_t data, |
| 1093 | unsigned size) |
| 1094 | { |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 1095 | if (!memory_region_access_valid(mr, addr, size, true)) { |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 1096 | unassigned_mem_write(mr, addr, data, size); |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1097 | return true; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1098 | } |
| 1099 | |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1100 | adjust_endianness(mr, &data, size); |
| 1101 | |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 1102 | if (mr->ops->write) { |
| 1103 | access_with_adjusted_size(addr, &data, size, |
| 1104 | mr->ops->impl.min_access_size, |
| 1105 | mr->ops->impl.max_access_size, |
| 1106 | memory_region_write_accessor, mr); |
| 1107 | } else { |
| 1108 | access_with_adjusted_size(addr, &data, size, 1, 4, |
| 1109 | memory_region_oldmmio_write_accessor, mr); |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 1110 | } |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1111 | return false; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1112 | } |
| 1113 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1114 | void memory_region_init_io(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1115 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1116 | const MemoryRegionOps *ops, |
| 1117 | void *opaque, |
| 1118 | const char *name, |
| 1119 | uint64_t size) |
| 1120 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1121 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1122 | mr->ops = ops; |
| 1123 | mr->opaque = opaque; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1124 | mr->terminates = true; |
Avi Kivity | 97161e1 | 2012-03-08 19:16:39 +0200 | [diff] [blame] | 1125 | mr->ram_addr = ~(ram_addr_t)0; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | void memory_region_init_ram(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1129 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1130 | const char *name, |
Hu Tao | 4994653 | 2014-09-09 13:27:55 +0800 | [diff] [blame] | 1131 | uint64_t size, |
| 1132 | Error **errp) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1133 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1134 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 1135 | mr->ram = true; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1136 | mr->terminates = true; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1137 | mr->destructor = memory_region_destructor_ram; |
Hu Tao | 4994653 | 2014-09-09 13:27:55 +0800 | [diff] [blame] | 1138 | mr->ram_addr = qemu_ram_alloc(size, mr, errp); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1139 | } |
| 1140 | |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 1141 | void memory_region_init_resizeable_ram(MemoryRegion *mr, |
| 1142 | Object *owner, |
| 1143 | const char *name, |
| 1144 | uint64_t size, |
| 1145 | uint64_t max_size, |
| 1146 | void (*resized)(const char*, |
| 1147 | uint64_t length, |
| 1148 | void *host), |
| 1149 | Error **errp) |
| 1150 | { |
| 1151 | memory_region_init(mr, owner, name, size); |
| 1152 | mr->ram = true; |
| 1153 | mr->terminates = true; |
| 1154 | mr->destructor = memory_region_destructor_ram; |
| 1155 | mr->ram_addr = qemu_ram_alloc_resizeable(size, max_size, resized, mr, errp); |
| 1156 | } |
| 1157 | |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1158 | #ifdef __linux__ |
| 1159 | void memory_region_init_ram_from_file(MemoryRegion *mr, |
| 1160 | struct Object *owner, |
| 1161 | const char *name, |
| 1162 | uint64_t size, |
Paolo Bonzini | dbcb898 | 2014-06-10 19:15:24 +0800 | [diff] [blame] | 1163 | bool share, |
Paolo Bonzini | 7f56e74 | 2014-05-14 17:43:20 +0800 | [diff] [blame] | 1164 | const char *path, |
| 1165 | Error **errp) |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1166 | { |
| 1167 | memory_region_init(mr, owner, name, size); |
| 1168 | mr->ram = true; |
| 1169 | mr->terminates = true; |
| 1170 | mr->destructor = memory_region_destructor_ram; |
Paolo Bonzini | dbcb898 | 2014-06-10 19:15:24 +0800 | [diff] [blame] | 1171 | mr->ram_addr = qemu_ram_alloc_from_file(size, mr, share, path, errp); |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1172 | } |
| 1173 | #endif |
| 1174 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1175 | void memory_region_init_ram_ptr(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1176 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1177 | const char *name, |
| 1178 | uint64_t size, |
| 1179 | void *ptr) |
| 1180 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1181 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 1182 | mr->ram = true; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1183 | mr->terminates = true; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1184 | mr->destructor = memory_region_destructor_ram_from_ptr; |
Hu Tao | ef701d7 | 2014-09-09 13:27:54 +0800 | [diff] [blame] | 1185 | |
| 1186 | /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */ |
| 1187 | assert(ptr != NULL); |
| 1188 | mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_abort); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1189 | } |
| 1190 | |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 1191 | void memory_region_set_skip_dump(MemoryRegion *mr) |
| 1192 | { |
| 1193 | mr->skip_dump = true; |
| 1194 | } |
| 1195 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1196 | void memory_region_init_alias(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1197 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1198 | const char *name, |
| 1199 | MemoryRegion *orig, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1200 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1201 | uint64_t size) |
| 1202 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1203 | memory_region_init(mr, owner, name, size); |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 1204 | memory_region_ref(orig); |
| 1205 | mr->destructor = memory_region_destructor_alias; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1206 | mr->alias = orig; |
| 1207 | mr->alias_offset = offset; |
| 1208 | } |
| 1209 | |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1210 | void memory_region_init_rom_device(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1211 | Object *owner, |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1212 | const MemoryRegionOps *ops, |
Avi Kivity | 75f5941 | 2011-08-26 00:35:15 +0300 | [diff] [blame] | 1213 | void *opaque, |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1214 | const char *name, |
Hu Tao | 33e0eb5 | 2014-09-09 13:27:57 +0800 | [diff] [blame] | 1215 | uint64_t size, |
| 1216 | Error **errp) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1217 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1218 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 7bc2b9c | 2011-08-25 14:56:14 +0300 | [diff] [blame] | 1219 | mr->ops = ops; |
Avi Kivity | 75f5941 | 2011-08-26 00:35:15 +0300 | [diff] [blame] | 1220 | mr->opaque = opaque; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1221 | mr->terminates = true; |
Avi Kivity | 75c578d | 2012-01-02 15:40:52 +0200 | [diff] [blame] | 1222 | mr->rom_device = true; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1223 | mr->destructor = memory_region_destructor_rom_device; |
Hu Tao | 33e0eb5 | 2014-09-09 13:27:57 +0800 | [diff] [blame] | 1224 | mr->ram_addr = qemu_ram_alloc(size, mr, errp); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1225 | } |
| 1226 | |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1227 | void memory_region_init_iommu(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1228 | Object *owner, |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1229 | const MemoryRegionIOMMUOps *ops, |
| 1230 | const char *name, |
| 1231 | uint64_t size) |
| 1232 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1233 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1234 | mr->iommu_ops = ops, |
| 1235 | mr->terminates = true; /* then re-forwards */ |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1236 | notifier_list_init(&mr->iommu_notify); |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 1239 | void memory_region_init_reservation(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1240 | Object *owner, |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 1241 | const char *name, |
| 1242 | uint64_t size) |
| 1243 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1244 | memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size); |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1247 | static void memory_region_finalize(Object *obj) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1248 | { |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1249 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1250 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1251 | assert(QTAILQ_EMPTY(&mr->subregions)); |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1252 | mr->destructor(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1253 | memory_region_clear_coalescing(mr); |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 1254 | g_free((char *)mr->name); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1255 | g_free(mr->ioeventfds); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1256 | } |
| 1257 | |
Paolo Bonzini | 803c081 | 2013-05-07 06:59:09 +0200 | [diff] [blame] | 1258 | Object *memory_region_owner(MemoryRegion *mr) |
| 1259 | { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1260 | Object *obj = OBJECT(mr); |
| 1261 | return obj->parent; |
Paolo Bonzini | 803c081 | 2013-05-07 06:59:09 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 1264 | void memory_region_ref(MemoryRegion *mr) |
| 1265 | { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1266 | /* MMIO callbacks most likely will access data that belongs |
| 1267 | * to the owner, hence the need to ref/unref the owner whenever |
| 1268 | * the memory region is in use. |
| 1269 | * |
| 1270 | * The memory region is a child of its owner. As long as the |
| 1271 | * owner doesn't call unparent itself on the memory region, |
| 1272 | * ref-ing the owner will also keep the memory region alive. |
| 1273 | * Memory regions without an owner are supposed to never go away, |
| 1274 | * but we still ref/unref them for debugging purposes. |
| 1275 | */ |
| 1276 | Object *obj = OBJECT(mr); |
| 1277 | if (obj && obj->parent) { |
| 1278 | object_ref(obj->parent); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1279 | } else { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1280 | object_ref(obj); |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | void memory_region_unref(MemoryRegion *mr) |
| 1285 | { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1286 | Object *obj = OBJECT(mr); |
| 1287 | if (obj && obj->parent) { |
| 1288 | object_unref(obj->parent); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1289 | } else { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1290 | object_unref(obj); |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1294 | uint64_t memory_region_size(MemoryRegion *mr) |
| 1295 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1296 | if (int128_eq(mr->size, int128_2_64())) { |
| 1297 | return UINT64_MAX; |
| 1298 | } |
| 1299 | return int128_get64(mr->size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1300 | } |
| 1301 | |
Peter Crosthwaite | 5d546d4 | 2014-08-14 23:55:03 -0700 | [diff] [blame] | 1302 | const char *memory_region_name(const MemoryRegion *mr) |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 1303 | { |
Peter Crosthwaite | d1dd32a | 2014-08-25 20:10:24 -0700 | [diff] [blame] | 1304 | if (!mr->name) { |
| 1305 | ((MemoryRegion *)mr)->name = |
| 1306 | object_get_canonical_path_component(OBJECT(mr)); |
| 1307 | } |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 1308 | return mr->name; |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 1311 | bool memory_region_is_ram(MemoryRegion *mr) |
| 1312 | { |
| 1313 | return mr->ram; |
| 1314 | } |
| 1315 | |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 1316 | bool memory_region_is_skip_dump(MemoryRegion *mr) |
| 1317 | { |
| 1318 | return mr->skip_dump; |
| 1319 | } |
| 1320 | |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 1321 | bool memory_region_is_logging(MemoryRegion *mr) |
| 1322 | { |
| 1323 | return mr->dirty_log_mask; |
| 1324 | } |
| 1325 | |
Avi Kivity | ce7923d | 2011-12-08 16:05:11 +0200 | [diff] [blame] | 1326 | bool memory_region_is_rom(MemoryRegion *mr) |
| 1327 | { |
| 1328 | return mr->ram && mr->readonly; |
| 1329 | } |
| 1330 | |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1331 | bool memory_region_is_iommu(MemoryRegion *mr) |
| 1332 | { |
| 1333 | return mr->iommu_ops; |
| 1334 | } |
| 1335 | |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1336 | void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n) |
| 1337 | { |
| 1338 | notifier_list_add(&mr->iommu_notify, n); |
| 1339 | } |
| 1340 | |
| 1341 | void memory_region_unregister_iommu_notifier(Notifier *n) |
| 1342 | { |
| 1343 | notifier_remove(n); |
| 1344 | } |
| 1345 | |
| 1346 | void memory_region_notify_iommu(MemoryRegion *mr, |
| 1347 | IOMMUTLBEntry entry) |
| 1348 | { |
| 1349 | assert(memory_region_is_iommu(mr)); |
| 1350 | notifier_list_notify(&mr->iommu_notify, &entry); |
| 1351 | } |
| 1352 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1353 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client) |
| 1354 | { |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1355 | uint8_t mask = 1 << client; |
| 1356 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1357 | memory_region_transaction_begin(); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1358 | mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask); |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1359 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1360 | memory_region_transaction_commit(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1361 | } |
| 1362 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1363 | bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, |
| 1364 | hwaddr size, unsigned client) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1365 | { |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1366 | assert(mr->terminates); |
Juan Quintela | 5215919 | 2013-10-08 12:44:04 +0200 | [diff] [blame] | 1367 | return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1368 | } |
| 1369 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1370 | void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, |
| 1371 | hwaddr size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1372 | { |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1373 | assert(mr->terminates); |
Juan Quintela | 75218e7 | 2013-10-08 12:31:54 +0200 | [diff] [blame] | 1374 | cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1375 | } |
| 1376 | |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1377 | bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, |
| 1378 | hwaddr size, unsigned client) |
| 1379 | { |
| 1380 | bool ret; |
| 1381 | assert(mr->terminates); |
Juan Quintela | 5215919 | 2013-10-08 12:44:04 +0200 | [diff] [blame] | 1382 | ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client); |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1383 | if (ret) { |
Juan Quintela | a2f4d5b | 2013-10-10 11:49:53 +0200 | [diff] [blame] | 1384 | cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client); |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1385 | } |
| 1386 | return ret; |
| 1387 | } |
| 1388 | |
| 1389 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1390 | void memory_region_sync_dirty_bitmap(MemoryRegion *mr) |
| 1391 | { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1392 | AddressSpace *as; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1393 | FlatRange *fr; |
| 1394 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1395 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1396 | FlatView *view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1397 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1398 | if (fr->mr == mr) { |
| 1399 | MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync); |
| 1400 | } |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1401 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1402 | flatview_unref(view); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1403 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly) |
| 1407 | { |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 1408 | if (mr->readonly != readonly) { |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1409 | memory_region_transaction_begin(); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 1410 | mr->readonly = readonly; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1411 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1412 | memory_region_transaction_commit(); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 1413 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1414 | } |
| 1415 | |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1416 | void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1417 | { |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1418 | if (mr->romd_mode != romd_mode) { |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1419 | memory_region_transaction_begin(); |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1420 | mr->romd_mode = romd_mode; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1421 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1422 | memory_region_transaction_commit(); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1426 | void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, |
| 1427 | hwaddr size, unsigned client) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1428 | { |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1429 | assert(mr->terminates); |
Juan Quintela | a2f4d5b | 2013-10-10 11:49:53 +0200 | [diff] [blame] | 1430 | cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1431 | } |
| 1432 | |
Paolo Bonzini | a35ba7b | 2014-06-10 19:15:23 +0800 | [diff] [blame] | 1433 | int memory_region_get_fd(MemoryRegion *mr) |
| 1434 | { |
| 1435 | if (mr->alias) { |
| 1436 | return memory_region_get_fd(mr->alias); |
| 1437 | } |
| 1438 | |
| 1439 | assert(mr->terminates); |
| 1440 | |
| 1441 | return qemu_get_ram_fd(mr->ram_addr & TARGET_PAGE_MASK); |
| 1442 | } |
| 1443 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1444 | void *memory_region_get_ram_ptr(MemoryRegion *mr) |
| 1445 | { |
| 1446 | if (mr->alias) { |
| 1447 | return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset; |
| 1448 | } |
| 1449 | |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1450 | assert(mr->terminates); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1451 | |
Jan Kiszka | 021d26d | 2011-08-30 00:38:24 +0200 | [diff] [blame] | 1452 | return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1453 | } |
| 1454 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1455 | static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1456 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1457 | FlatView *view; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1458 | FlatRange *fr; |
| 1459 | CoalescedMemoryRange *cmr; |
| 1460 | AddrRange tmp; |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 1461 | MemoryRegionSection section; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1462 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1463 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1464 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1465 | if (fr->mr == mr) { |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 1466 | section = (MemoryRegionSection) { |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 1467 | .address_space = as, |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 1468 | .offset_within_address_space = int128_get64(fr->addr.start), |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 1469 | .size = fr->addr.size, |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 1470 | }; |
| 1471 | |
| 1472 | MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, §ion, |
| 1473 | int128_get64(fr->addr.start), |
| 1474 | int128_get64(fr->addr.size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1475 | QTAILQ_FOREACH(cmr, &mr->coalesced, link) { |
| 1476 | tmp = addrrange_shift(cmr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1477 | int128_sub(fr->addr.start, |
| 1478 | int128_make64(fr->offset_in_region))); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1479 | if (!addrrange_intersects(tmp, fr->addr)) { |
| 1480 | continue; |
| 1481 | } |
| 1482 | tmp = addrrange_intersection(tmp, fr->addr); |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 1483 | MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, §ion, |
| 1484 | int128_get64(tmp.start), |
| 1485 | int128_get64(tmp.size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1489 | flatview_unref(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1490 | } |
| 1491 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1492 | static void memory_region_update_coalesced_range(MemoryRegion *mr) |
| 1493 | { |
| 1494 | AddressSpace *as; |
| 1495 | |
| 1496 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 1497 | memory_region_update_coalesced_range_as(mr, as); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1501 | void memory_region_set_coalescing(MemoryRegion *mr) |
| 1502 | { |
| 1503 | memory_region_clear_coalescing(mr); |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1504 | memory_region_add_coalescing(mr, 0, int128_get64(mr->size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void memory_region_add_coalescing(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1508 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1509 | uint64_t size) |
| 1510 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1511 | CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1512 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1513 | cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1514 | QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link); |
| 1515 | memory_region_update_coalesced_range(mr); |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 1516 | memory_region_set_flush_coalesced(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | void memory_region_clear_coalescing(MemoryRegion *mr) |
| 1520 | { |
| 1521 | CoalescedMemoryRange *cmr; |
Fam Zheng | ab5b3db | 2014-06-13 14:34:41 +0800 | [diff] [blame] | 1522 | bool updated = false; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1523 | |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 1524 | qemu_flush_coalesced_mmio_buffer(); |
| 1525 | mr->flush_coalesced_mmio = false; |
| 1526 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1527 | while (!QTAILQ_EMPTY(&mr->coalesced)) { |
| 1528 | cmr = QTAILQ_FIRST(&mr->coalesced); |
| 1529 | QTAILQ_REMOVE(&mr->coalesced, cmr, link); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1530 | g_free(cmr); |
Fam Zheng | ab5b3db | 2014-06-13 14:34:41 +0800 | [diff] [blame] | 1531 | updated = true; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1532 | } |
Fam Zheng | ab5b3db | 2014-06-13 14:34:41 +0800 | [diff] [blame] | 1533 | |
| 1534 | if (updated) { |
| 1535 | memory_region_update_coalesced_range(mr); |
| 1536 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1537 | } |
| 1538 | |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 1539 | void memory_region_set_flush_coalesced(MemoryRegion *mr) |
| 1540 | { |
| 1541 | mr->flush_coalesced_mmio = true; |
| 1542 | } |
| 1543 | |
| 1544 | void memory_region_clear_flush_coalesced(MemoryRegion *mr) |
| 1545 | { |
| 1546 | qemu_flush_coalesced_mmio_buffer(); |
| 1547 | if (QTAILQ_EMPTY(&mr->coalesced)) { |
| 1548 | mr->flush_coalesced_mmio = false; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1552 | void memory_region_add_eventfd(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1553 | hwaddr addr, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1554 | unsigned size, |
| 1555 | bool match_data, |
| 1556 | uint64_t data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 1557 | EventNotifier *e) |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1558 | { |
| 1559 | MemoryRegionIoeventfd mrfd = { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1560 | .addr.start = int128_make64(addr), |
| 1561 | .addr.size = int128_make64(size), |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1562 | .match_data = match_data, |
| 1563 | .data = data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 1564 | .e = e, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1565 | }; |
| 1566 | unsigned i; |
| 1567 | |
Alexander Graf | 28f362b | 2012-10-15 20:30:28 +0200 | [diff] [blame] | 1568 | adjust_endianness(mr, &mrfd.data, size); |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1569 | memory_region_transaction_begin(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1570 | for (i = 0; i < mr->ioeventfd_nb; ++i) { |
| 1571 | if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) { |
| 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | ++mr->ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1576 | mr->ioeventfds = g_realloc(mr->ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1577 | sizeof(*mr->ioeventfds) * mr->ioeventfd_nb); |
| 1578 | memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i], |
| 1579 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i)); |
| 1580 | mr->ioeventfds[i] = mrfd; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1581 | ioeventfd_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1582 | memory_region_transaction_commit(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | void memory_region_del_eventfd(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1586 | hwaddr addr, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1587 | unsigned size, |
| 1588 | bool match_data, |
| 1589 | uint64_t data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 1590 | EventNotifier *e) |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1591 | { |
| 1592 | MemoryRegionIoeventfd mrfd = { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1593 | .addr.start = int128_make64(addr), |
| 1594 | .addr.size = int128_make64(size), |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1595 | .match_data = match_data, |
| 1596 | .data = data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 1597 | .e = e, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1598 | }; |
| 1599 | unsigned i; |
| 1600 | |
Alexander Graf | 28f362b | 2012-10-15 20:30:28 +0200 | [diff] [blame] | 1601 | adjust_endianness(mr, &mrfd.data, size); |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1602 | memory_region_transaction_begin(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1603 | for (i = 0; i < mr->ioeventfd_nb; ++i) { |
| 1604 | if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) { |
| 1605 | break; |
| 1606 | } |
| 1607 | } |
| 1608 | assert(i != mr->ioeventfd_nb); |
| 1609 | memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1], |
| 1610 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1))); |
| 1611 | --mr->ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1612 | mr->ioeventfds = g_realloc(mr->ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1613 | sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1); |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1614 | ioeventfd_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1615 | memory_region_transaction_commit(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1616 | } |
| 1617 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1618 | static void memory_region_update_container_subregions(MemoryRegion *subregion) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1619 | { |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 1620 | hwaddr offset = subregion->addr; |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1621 | MemoryRegion *mr = subregion->container; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1622 | MemoryRegion *other; |
| 1623 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1624 | memory_region_transaction_begin(); |
| 1625 | |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 1626 | memory_region_ref(subregion); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1627 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link) { |
| 1628 | if (subregion->may_overlap || other->may_overlap) { |
| 1629 | continue; |
| 1630 | } |
Hu Tao | 2c7cfd6 | 2013-03-15 14:32:01 +0800 | [diff] [blame] | 1631 | if (int128_ge(int128_make64(offset), |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1632 | int128_add(int128_make64(other->addr), other->size)) |
| 1633 | || int128_le(int128_add(int128_make64(offset), subregion->size), |
| 1634 | int128_make64(other->addr))) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1635 | continue; |
| 1636 | } |
Anthony Liguori | a5e1cbc | 2011-08-22 11:14:56 -0500 | [diff] [blame] | 1637 | #if 0 |
Michael Walle | 860329b | 2011-09-15 23:16:49 +0200 | [diff] [blame] | 1638 | printf("warning: subregion collision %llx/%llx (%s) " |
| 1639 | "vs %llx/%llx (%s)\n", |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1640 | (unsigned long long)offset, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1641 | (unsigned long long)int128_get64(subregion->size), |
Michael Walle | 860329b | 2011-09-15 23:16:49 +0200 | [diff] [blame] | 1642 | subregion->name, |
| 1643 | (unsigned long long)other->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1644 | (unsigned long long)int128_get64(other->size), |
Michael Walle | 860329b | 2011-09-15 23:16:49 +0200 | [diff] [blame] | 1645 | other->name); |
Anthony Liguori | a5e1cbc | 2011-08-22 11:14:56 -0500 | [diff] [blame] | 1646 | #endif |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1647 | } |
| 1648 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link) { |
| 1649 | if (subregion->priority >= other->priority) { |
| 1650 | QTAILQ_INSERT_BEFORE(other, subregion, subregions_link); |
| 1651 | goto done; |
| 1652 | } |
| 1653 | } |
| 1654 | QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link); |
| 1655 | done: |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1656 | memory_region_update_pending |= mr->enabled && subregion->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1657 | memory_region_transaction_commit(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1658 | } |
| 1659 | |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 1660 | static void memory_region_add_subregion_common(MemoryRegion *mr, |
| 1661 | hwaddr offset, |
| 1662 | MemoryRegion *subregion) |
| 1663 | { |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1664 | assert(!subregion->container); |
| 1665 | subregion->container = mr; |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 1666 | subregion->addr = offset; |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1667 | memory_region_update_container_subregions(subregion); |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 1668 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1669 | |
| 1670 | void memory_region_add_subregion(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1671 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1672 | MemoryRegion *subregion) |
| 1673 | { |
| 1674 | subregion->may_overlap = false; |
| 1675 | subregion->priority = 0; |
| 1676 | memory_region_add_subregion_common(mr, offset, subregion); |
| 1677 | } |
| 1678 | |
| 1679 | void memory_region_add_subregion_overlap(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1680 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1681 | MemoryRegion *subregion, |
Marcel Apfelbaum | a1ff8ae | 2013-09-16 11:21:14 +0300 | [diff] [blame] | 1682 | int priority) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1683 | { |
| 1684 | subregion->may_overlap = true; |
| 1685 | subregion->priority = priority; |
| 1686 | memory_region_add_subregion_common(mr, offset, subregion); |
| 1687 | } |
| 1688 | |
| 1689 | void memory_region_del_subregion(MemoryRegion *mr, |
| 1690 | MemoryRegion *subregion) |
| 1691 | { |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1692 | memory_region_transaction_begin(); |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1693 | assert(subregion->container == mr); |
| 1694 | subregion->container = NULL; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1695 | QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link); |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 1696 | memory_region_unref(subregion); |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1697 | memory_region_update_pending |= mr->enabled && subregion->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1698 | memory_region_transaction_commit(); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled) |
| 1702 | { |
| 1703 | if (enabled == mr->enabled) { |
| 1704 | return; |
| 1705 | } |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1706 | memory_region_transaction_begin(); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1707 | mr->enabled = enabled; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1708 | memory_region_update_pending = true; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1709 | memory_region_transaction_commit(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1710 | } |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 1711 | |
Michael S. Tsirkin | e7af4c6 | 2014-12-16 11:21:23 +0200 | [diff] [blame] | 1712 | void memory_region_set_size(MemoryRegion *mr, uint64_t size) |
| 1713 | { |
| 1714 | Int128 s = int128_make64(size); |
| 1715 | |
| 1716 | if (size == UINT64_MAX) { |
| 1717 | s = int128_2_64(); |
| 1718 | } |
| 1719 | if (int128_eq(s, mr->size)) { |
| 1720 | return; |
| 1721 | } |
| 1722 | memory_region_transaction_begin(); |
| 1723 | mr->size = s; |
| 1724 | memory_region_update_pending = true; |
| 1725 | memory_region_transaction_commit(); |
| 1726 | } |
| 1727 | |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 1728 | static void memory_region_readd_subregion(MemoryRegion *mr) |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1729 | { |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1730 | MemoryRegion *container = mr->container; |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1731 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1732 | if (container) { |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 1733 | memory_region_transaction_begin(); |
| 1734 | memory_region_ref(mr); |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1735 | memory_region_del_subregion(container, mr); |
| 1736 | mr->container = container; |
| 1737 | memory_region_update_container_subregions(mr); |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 1738 | memory_region_unref(mr); |
| 1739 | memory_region_transaction_commit(); |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1740 | } |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 1741 | } |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1742 | |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 1743 | void memory_region_set_address(MemoryRegion *mr, hwaddr addr) |
| 1744 | { |
| 1745 | if (addr != mr->addr) { |
| 1746 | mr->addr = addr; |
| 1747 | memory_region_readd_subregion(mr); |
| 1748 | } |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1749 | } |
| 1750 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1751 | void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset) |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1752 | { |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1753 | assert(mr->alias); |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1754 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1755 | if (offset == mr->alias_offset) { |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1756 | return; |
| 1757 | } |
| 1758 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1759 | memory_region_transaction_begin(); |
| 1760 | mr->alias_offset = offset; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1761 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1762 | memory_region_transaction_commit(); |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1763 | } |
| 1764 | |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 1765 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr) |
| 1766 | { |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 1767 | return mr->ram_addr; |
| 1768 | } |
| 1769 | |
Igor Mammedov | a2b257d | 2014-10-31 16:38:37 +0000 | [diff] [blame] | 1770 | uint64_t memory_region_get_alignment(const MemoryRegion *mr) |
| 1771 | { |
| 1772 | return mr->align; |
| 1773 | } |
| 1774 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1775 | static int cmp_flatrange_addr(const void *addr_, const void *fr_) |
| 1776 | { |
| 1777 | const AddrRange *addr = addr_; |
| 1778 | const FlatRange *fr = fr_; |
| 1779 | |
| 1780 | if (int128_le(addrrange_end(*addr), fr->addr.start)) { |
| 1781 | return -1; |
| 1782 | } else if (int128_ge(addr->start, addrrange_end(fr->addr))) { |
| 1783 | return 1; |
| 1784 | } |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1788 | static FlatRange *flatview_lookup(FlatView *view, AddrRange addr) |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1789 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1790 | return bsearch(&addr, view->ranges, view->nr, |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1791 | sizeof(FlatRange), cmp_flatrange_addr); |
| 1792 | } |
| 1793 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1794 | bool memory_region_present(MemoryRegion *container, hwaddr addr) |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1795 | { |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1796 | MemoryRegion *mr = memory_region_find(container, addr, 1).mr; |
| 1797 | if (!mr || (mr == container)) { |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1798 | return false; |
| 1799 | } |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 1800 | memory_region_unref(mr); |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1801 | return true; |
| 1802 | } |
| 1803 | |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 1804 | bool memory_region_is_mapped(MemoryRegion *mr) |
| 1805 | { |
| 1806 | return mr->container ? true : false; |
| 1807 | } |
| 1808 | |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1809 | MemoryRegionSection memory_region_find(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1810 | hwaddr addr, uint64_t size) |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1811 | { |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 1812 | MemoryRegionSection ret = { .mr = NULL }; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1813 | MemoryRegion *root; |
| 1814 | AddressSpace *as; |
| 1815 | AddrRange range; |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1816 | FlatView *view; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1817 | FlatRange *fr; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1818 | |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1819 | addr += mr->addr; |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1820 | for (root = mr; root->container; ) { |
| 1821 | root = root->container; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1822 | addr += root->addr; |
| 1823 | } |
| 1824 | |
| 1825 | as = memory_region_to_address_space(root); |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 1826 | if (!as) { |
| 1827 | return ret; |
| 1828 | } |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1829 | range = addrrange_make(int128_make64(addr), int128_make64(size)); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1830 | |
Paolo Bonzini | 2b64766 | 2013-05-17 12:40:44 +0200 | [diff] [blame] | 1831 | rcu_read_lock(); |
| 1832 | view = atomic_rcu_read(&as->current_map); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1833 | fr = flatview_lookup(view, range); |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1834 | if (!fr) { |
Paolo Bonzini | 2b64766 | 2013-05-17 12:40:44 +0200 | [diff] [blame] | 1835 | goto out; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1836 | } |
| 1837 | |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1838 | while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) { |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1839 | --fr; |
| 1840 | } |
| 1841 | |
| 1842 | ret.mr = fr->mr; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1843 | ret.address_space = as; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1844 | range = addrrange_intersection(range, fr->addr); |
| 1845 | ret.offset_within_region = fr->offset_in_region; |
| 1846 | ret.offset_within_region += int128_get64(int128_sub(range.start, |
| 1847 | fr->addr.start)); |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 1848 | ret.size = range.size; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1849 | ret.offset_within_address_space = int128_get64(range.start); |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 1850 | ret.readonly = fr->readonly; |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 1851 | memory_region_ref(ret.mr); |
Paolo Bonzini | 2b64766 | 2013-05-17 12:40:44 +0200 | [diff] [blame] | 1852 | out: |
| 1853 | rcu_read_unlock(); |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1854 | return ret; |
| 1855 | } |
| 1856 | |
Paolo Bonzini | 1d67136 | 2013-04-24 10:46:55 +0200 | [diff] [blame] | 1857 | void address_space_sync_dirty_bitmap(AddressSpace *as) |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 1858 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1859 | FlatView *view; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1860 | FlatRange *fr; |
| 1861 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1862 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1863 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 1864 | MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1865 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1866 | flatview_unref(view); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | void memory_global_dirty_log_start(void) |
| 1870 | { |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1871 | global_dirty_log = true; |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 1872 | MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | void memory_global_dirty_log_stop(void) |
| 1876 | { |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1877 | global_dirty_log = false; |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 1878 | MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static void listener_add_address_space(MemoryListener *listener, |
| 1882 | AddressSpace *as) |
| 1883 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1884 | FlatView *view; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1885 | FlatRange *fr; |
| 1886 | |
Julien Grall | 221b3a3 | 2012-04-04 15:15:41 +0100 | [diff] [blame] | 1887 | if (listener->address_space_filter |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 1888 | && listener->address_space_filter != as) { |
Julien Grall | 221b3a3 | 2012-04-04 15:15:41 +0100 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1892 | if (global_dirty_log) { |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 1893 | if (listener->log_global_start) { |
| 1894 | listener->log_global_start(listener); |
| 1895 | } |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1896 | } |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 1897 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1898 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 1899 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1900 | MemoryRegionSection section = { |
| 1901 | .mr = fr->mr, |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 1902 | .address_space = as, |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1903 | .offset_within_region = fr->offset_in_region, |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 1904 | .size = fr->addr.size, |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1905 | .offset_within_address_space = int128_get64(fr->addr.start), |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 1906 | .readonly = fr->readonly, |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1907 | }; |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 1908 | if (listener->region_add) { |
| 1909 | listener->region_add(listener, §ion); |
| 1910 | } |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1911 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1912 | flatview_unref(view); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1913 | } |
| 1914 | |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 1915 | void memory_listener_register(MemoryListener *listener, AddressSpace *filter) |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1916 | { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 1917 | MemoryListener *other = NULL; |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1918 | AddressSpace *as; |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 1919 | |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 1920 | listener->address_space_filter = filter; |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 1921 | if (QTAILQ_EMPTY(&memory_listeners) |
| 1922 | || listener->priority >= QTAILQ_LAST(&memory_listeners, |
| 1923 | memory_listeners)->priority) { |
| 1924 | QTAILQ_INSERT_TAIL(&memory_listeners, listener, link); |
| 1925 | } else { |
| 1926 | QTAILQ_FOREACH(other, &memory_listeners, link) { |
| 1927 | if (listener->priority < other->priority) { |
| 1928 | break; |
| 1929 | } |
| 1930 | } |
| 1931 | QTAILQ_INSERT_BEFORE(other, listener, link); |
| 1932 | } |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1933 | |
| 1934 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 1935 | listener_add_address_space(listener, as); |
| 1936 | } |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | void memory_listener_unregister(MemoryListener *listener) |
| 1940 | { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 1941 | QTAILQ_REMOVE(&memory_listeners, listener, link); |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 1942 | } |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1943 | |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 1944 | void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 1945 | { |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 1946 | memory_region_ref(root); |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1947 | memory_region_transaction_begin(); |
Avi Kivity | 8786db7 | 2012-10-02 13:53:41 +0200 | [diff] [blame] | 1948 | as->root = root; |
| 1949 | as->current_map = g_new(FlatView, 1); |
| 1950 | flatview_init(as->current_map); |
Avi Kivity | 4c19eb7 | 2012-10-30 13:47:44 +0200 | [diff] [blame] | 1951 | as->ioeventfd_nb = 0; |
| 1952 | as->ioeventfds = NULL; |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1953 | QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link); |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 1954 | as->name = g_strdup(name ? name : "anonymous"); |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1955 | address_space_init_dispatch(as); |
Paolo Bonzini | f43793c | 2013-04-16 15:39:51 +0200 | [diff] [blame] | 1956 | memory_region_update_pending |= root->enabled; |
| 1957 | memory_region_transaction_commit(); |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 1958 | } |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 1959 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 1960 | static void do_address_space_destroy(AddressSpace *as) |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 1961 | { |
David Gibson | 078c44f | 2014-05-30 12:59:00 -0600 | [diff] [blame] | 1962 | MemoryListener *listener; |
| 1963 | |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 1964 | address_space_destroy_dispatch(as); |
David Gibson | 078c44f | 2014-05-30 12:59:00 -0600 | [diff] [blame] | 1965 | |
| 1966 | QTAILQ_FOREACH(listener, &memory_listeners, link) { |
| 1967 | assert(listener->address_space_filter != as); |
| 1968 | } |
| 1969 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1970 | flatview_unref(as->current_map); |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 1971 | g_free(as->name); |
Avi Kivity | 4c19eb7 | 2012-10-30 13:47:44 +0200 | [diff] [blame] | 1972 | g_free(as->ioeventfds); |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 1973 | memory_region_unref(as->root); |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 1976 | void address_space_destroy(AddressSpace *as) |
| 1977 | { |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 1978 | MemoryRegion *root = as->root; |
| 1979 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 1980 | /* Flush out anything from MemoryListeners listening in on this */ |
| 1981 | memory_region_transaction_begin(); |
| 1982 | as->root = NULL; |
| 1983 | memory_region_transaction_commit(); |
| 1984 | QTAILQ_REMOVE(&address_spaces, as, address_spaces_link); |
Paolo Bonzini | 6e48e8f | 2015-02-10 10:25:44 -0700 | [diff] [blame] | 1985 | address_space_unregister(as); |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 1986 | |
| 1987 | /* At this point, as->dispatch and as->current_map are dummy |
| 1988 | * entries that the guest should never use. Wait for the old |
| 1989 | * values to expire before freeing the data. |
| 1990 | */ |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 1991 | as->root = root; |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 1992 | call_rcu(as, do_address_space_destroy, rcu); |
| 1993 | } |
| 1994 | |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1995 | bool io_mem_read(MemoryRegion *mr, hwaddr addr, uint64_t *pval, unsigned size) |
Avi Kivity | acbbec5 | 2011-11-21 12:27:03 +0200 | [diff] [blame] | 1996 | { |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1997 | return memory_region_dispatch_read(mr, addr, pval, size); |
Avi Kivity | acbbec5 | 2011-11-21 12:27:03 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 2000 | bool io_mem_write(MemoryRegion *mr, hwaddr addr, |
Avi Kivity | acbbec5 | 2011-11-21 12:27:03 +0200 | [diff] [blame] | 2001 | uint64_t val, unsigned size) |
| 2002 | { |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 2003 | return memory_region_dispatch_write(mr, addr, val, size); |
Avi Kivity | acbbec5 | 2011-11-21 12:27:03 +0200 | [diff] [blame] | 2004 | } |
| 2005 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2006 | typedef struct MemoryRegionList MemoryRegionList; |
| 2007 | |
| 2008 | struct MemoryRegionList { |
| 2009 | const MemoryRegion *mr; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2010 | QTAILQ_ENTRY(MemoryRegionList) queue; |
| 2011 | }; |
| 2012 | |
| 2013 | typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead; |
| 2014 | |
| 2015 | static void mtree_print_mr(fprintf_function mon_printf, void *f, |
| 2016 | const MemoryRegion *mr, unsigned int level, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2017 | hwaddr base, |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2018 | MemoryRegionListHead *alias_print_queue) |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2019 | { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2020 | MemoryRegionList *new_ml, *ml, *next_ml; |
| 2021 | MemoryRegionListHead submr_print_queue; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2022 | const MemoryRegion *submr; |
| 2023 | unsigned int i; |
| 2024 | |
Jan Kiszka | 7ea692b | 2012-10-31 10:49:02 +0100 | [diff] [blame] | 2025 | if (!mr || !mr->enabled) { |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2026 | return; |
| 2027 | } |
| 2028 | |
| 2029 | for (i = 0; i < level; i++) { |
| 2030 | mon_printf(f, " "); |
| 2031 | } |
| 2032 | |
| 2033 | if (mr->alias) { |
| 2034 | MemoryRegionList *ml; |
| 2035 | bool found = false; |
| 2036 | |
| 2037 | /* check if the alias is already in the queue */ |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2038 | QTAILQ_FOREACH(ml, alias_print_queue, queue) { |
Paolo Bonzini | f54bb15 | 2013-12-11 12:51:46 +0100 | [diff] [blame] | 2039 | if (ml->mr == mr->alias) { |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2040 | found = true; |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | if (!found) { |
| 2045 | ml = g_new(MemoryRegionList, 1); |
| 2046 | ml->mr = mr->alias; |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2047 | QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2048 | } |
Jan Kiszka | 4896d74 | 2012-02-04 16:25:42 +0100 | [diff] [blame] | 2049 | mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx |
| 2050 | " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx |
| 2051 | "-" TARGET_FMT_plx "\n", |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2052 | base + mr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2053 | base + mr->addr |
Alex Williamson | fd1d992 | 2013-07-19 12:42:12 -0600 | [diff] [blame] | 2054 | + (int128_nz(mr->size) ? |
| 2055 | (hwaddr)int128_get64(int128_sub(mr->size, |
| 2056 | int128_one())) : 0), |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 2057 | mr->priority, |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 2058 | mr->romd_mode ? 'R' : '-', |
| 2059 | !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' |
| 2060 | : '-', |
Peter Crosthwaite | 3fb18b4 | 2014-08-14 23:55:36 -0700 | [diff] [blame] | 2061 | memory_region_name(mr), |
| 2062 | memory_region_name(mr->alias), |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2063 | mr->alias_offset, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2064 | mr->alias_offset |
Alexey Kardashevskiy | a66670c | 2013-08-30 18:10:38 +1000 | [diff] [blame] | 2065 | + (int128_nz(mr->size) ? |
| 2066 | (hwaddr)int128_get64(int128_sub(mr->size, |
| 2067 | int128_one())) : 0)); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2068 | } else { |
Jan Kiszka | 4896d74 | 2012-02-04 16:25:42 +0100 | [diff] [blame] | 2069 | mon_printf(f, |
| 2070 | TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n", |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2071 | base + mr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2072 | base + mr->addr |
Alex Williamson | fd1d992 | 2013-07-19 12:42:12 -0600 | [diff] [blame] | 2073 | + (int128_nz(mr->size) ? |
| 2074 | (hwaddr)int128_get64(int128_sub(mr->size, |
| 2075 | int128_one())) : 0), |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 2076 | mr->priority, |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 2077 | mr->romd_mode ? 'R' : '-', |
| 2078 | !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' |
| 2079 | : '-', |
Peter Crosthwaite | 3fb18b4 | 2014-08-14 23:55:36 -0700 | [diff] [blame] | 2080 | memory_region_name(mr)); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2081 | } |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2082 | |
| 2083 | QTAILQ_INIT(&submr_print_queue); |
| 2084 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2085 | QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2086 | new_ml = g_new(MemoryRegionList, 1); |
| 2087 | new_ml->mr = submr; |
| 2088 | QTAILQ_FOREACH(ml, &submr_print_queue, queue) { |
| 2089 | if (new_ml->mr->addr < ml->mr->addr || |
| 2090 | (new_ml->mr->addr == ml->mr->addr && |
| 2091 | new_ml->mr->priority > ml->mr->priority)) { |
| 2092 | QTAILQ_INSERT_BEFORE(ml, new_ml, queue); |
| 2093 | new_ml = NULL; |
| 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | if (new_ml) { |
| 2098 | QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue); |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | QTAILQ_FOREACH(ml, &submr_print_queue, queue) { |
| 2103 | mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr, |
| 2104 | alias_print_queue); |
| 2105 | } |
| 2106 | |
Avi Kivity | 88365e4 | 2011-11-13 12:00:55 +0200 | [diff] [blame] | 2107 | QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2108 | g_free(ml); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | void mtree_info(fprintf_function mon_printf, void *f) |
| 2113 | { |
| 2114 | MemoryRegionListHead ml_head; |
| 2115 | MemoryRegionList *ml, *ml2; |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2116 | AddressSpace *as; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2117 | |
| 2118 | QTAILQ_INIT(&ml_head); |
| 2119 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2120 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2121 | mon_printf(f, "%s\n", as->name); |
| 2122 | mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head); |
Blue Swirl | b9f9be8 | 2012-03-10 16:58:35 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | mon_printf(f, "aliases\n"); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2126 | /* print aliased regions */ |
| 2127 | QTAILQ_FOREACH(ml, &ml_head, queue) { |
Paolo Bonzini | f54bb15 | 2013-12-11 12:51:46 +0100 | [diff] [blame] | 2128 | mon_printf(f, "%s\n", memory_region_name(ml->mr)); |
| 2129 | mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) { |
Avi Kivity | 88365e4 | 2011-11-13 12:00:55 +0200 | [diff] [blame] | 2133 | g_free(ml); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2134 | } |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2135 | } |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 2136 | |
| 2137 | static const TypeInfo memory_region_info = { |
| 2138 | .parent = TYPE_OBJECT, |
| 2139 | .name = TYPE_MEMORY_REGION, |
| 2140 | .instance_size = sizeof(MemoryRegion), |
| 2141 | .instance_init = memory_region_initfn, |
| 2142 | .instance_finalize = memory_region_finalize, |
| 2143 | }; |
| 2144 | |
| 2145 | static void memory_register_types(void) |
| 2146 | { |
| 2147 | type_register_static(&memory_region_info); |
| 2148 | } |
| 2149 | |
| 2150 | type_init(memory_register_types) |