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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 18 | #include "qemu-common.h" |
| 19 | #include "cpu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 20 | #include "exec/memory.h" |
| 21 | #include "exec/address-spaces.h" |
| 22 | #include "exec/ioport.h" |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 23 | #include "qapi/visitor.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 24 | #include "qemu/bitops.h" |
Pavel Fedin | 8c56c1a | 2015-11-20 12:37:16 +0300 | [diff] [blame] | 25 | #include "qemu/error-report.h" |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 26 | #include "qom/object.h" |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 27 | #include "trace-root.h" |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 28 | |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 29 | #include "exec/memory-internal.h" |
Juan Quintela | 220c3eb | 2013-10-14 17:13:59 +0200 | [diff] [blame] | 30 | #include "exec/ram_addr.h" |
Pavel Fedin | 8c56c1a | 2015-11-20 12:37:16 +0300 | [diff] [blame] | 31 | #include "sysemu/kvm.h" |
Paolo Bonzini | e1c57ab | 2014-05-14 17:43:18 +0800 | [diff] [blame] | 32 | #include "sysemu/sysemu.h" |
KONRAD Frederic | c935674 | 2016-10-19 15:06:49 +0200 | [diff] [blame] | 33 | #include "hw/misc/mmio_interface.h" |
| 34 | #include "hw/qdev-properties.h" |
Peter Maydell | b08199c | 2017-07-07 15:42:51 +0100 | [diff] [blame] | 35 | #include "migration/vmstate.h" |
Avi Kivity | 67d95c1 | 2011-12-15 15:25:22 +0200 | [diff] [blame] | 36 | |
Paolo Bonzini | d197063 | 2013-05-24 13:23:38 +0200 | [diff] [blame] | 37 | //#define DEBUG_UNASSIGNED |
| 38 | |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 39 | static unsigned memory_region_transaction_depth; |
| 40 | static bool memory_region_update_pending; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 41 | static bool ioeventfd_update_pending; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 42 | static bool global_dirty_log = false; |
| 43 | |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 44 | static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners |
| 45 | = QTAILQ_HEAD_INITIALIZER(memory_listeners); |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 46 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 47 | static QTAILQ_HEAD(, AddressSpace) address_spaces |
| 48 | = QTAILQ_HEAD_INITIALIZER(address_spaces); |
| 49 | |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 50 | static GHashTable *flat_views; |
| 51 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 52 | typedef struct AddrRange AddrRange; |
| 53 | |
Avi Kivity | 8417ceb | 2011-08-03 11:56:14 +0300 | [diff] [blame] | 54 | /* |
Fam Zheng | c9cdaa3 | 2014-08-11 10:18:31 +0800 | [diff] [blame] | 55 | * Note that signed integers are needed for negative offsetting in aliases |
Avi Kivity | 8417ceb | 2011-08-03 11:56:14 +0300 | [diff] [blame] | 56 | * (large MemoryRegion::alias_offset). |
| 57 | */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 58 | struct AddrRange { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 59 | Int128 start; |
| 60 | Int128 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 AddrRange addrrange_make(Int128 start, Int128 size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 64 | { |
| 65 | return (AddrRange) { start, size }; |
| 66 | } |
| 67 | |
| 68 | static bool addrrange_equal(AddrRange r1, AddrRange r2) |
| 69 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 70 | 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] | 71 | } |
| 72 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 73 | static Int128 addrrange_end(AddrRange r) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 74 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 75 | return int128_add(r.start, r.size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 76 | } |
| 77 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 78 | static AddrRange addrrange_shift(AddrRange range, Int128 delta) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 79 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 80 | int128_addto(&range.start, delta); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 81 | return range; |
| 82 | } |
| 83 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 84 | static bool addrrange_contains(AddrRange range, Int128 addr) |
| 85 | { |
| 86 | return int128_ge(addr, range.start) |
| 87 | && int128_lt(addr, addrrange_end(range)); |
| 88 | } |
| 89 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 90 | static bool addrrange_intersects(AddrRange r1, AddrRange r2) |
| 91 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 92 | return addrrange_contains(r1, r2.start) |
| 93 | || addrrange_contains(r2, r1.start); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2) |
| 97 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 98 | Int128 start = int128_max(r1.start, r2.start); |
| 99 | Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2)); |
| 100 | return addrrange_make(start, int128_sub(end, start)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 101 | } |
| 102 | |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 103 | enum ListenerDirection { Forward, Reverse }; |
| 104 | |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 105 | #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 106 | do { \ |
| 107 | MemoryListener *_listener; \ |
| 108 | \ |
| 109 | switch (_direction) { \ |
| 110 | case Forward: \ |
| 111 | QTAILQ_FOREACH(_listener, &memory_listeners, link) { \ |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 112 | if (_listener->_callback) { \ |
| 113 | _listener->_callback(_listener, ##_args); \ |
| 114 | } \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 115 | } \ |
| 116 | break; \ |
| 117 | case Reverse: \ |
| 118 | QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \ |
| 119 | memory_listeners, link) { \ |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 120 | if (_listener->_callback) { \ |
| 121 | _listener->_callback(_listener, ##_args); \ |
| 122 | } \ |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 123 | } \ |
| 124 | break; \ |
| 125 | default: \ |
| 126 | abort(); \ |
| 127 | } \ |
| 128 | } while (0) |
| 129 | |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 130 | #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 131 | do { \ |
| 132 | MemoryListener *_listener; \ |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 133 | struct memory_listeners_as *list = &(_as)->listeners; \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 134 | \ |
| 135 | switch (_direction) { \ |
| 136 | case Forward: \ |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 137 | QTAILQ_FOREACH(_listener, list, link_as) { \ |
| 138 | if (_listener->_callback) { \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 139 | _listener->_callback(_listener, _section, ##_args); \ |
| 140 | } \ |
| 141 | } \ |
| 142 | break; \ |
| 143 | case Reverse: \ |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 144 | QTAILQ_FOREACH_REVERSE(_listener, list, memory_listeners_as, \ |
| 145 | link_as) { \ |
| 146 | if (_listener->_callback) { \ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 147 | _listener->_callback(_listener, _section, ##_args); \ |
| 148 | } \ |
| 149 | } \ |
| 150 | break; \ |
| 151 | default: \ |
| 152 | abort(); \ |
| 153 | } \ |
| 154 | } while (0) |
| 155 | |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 156 | /* No need to ref/unref .mr, the FlatRange keeps it alive. */ |
Paolo Bonzini | b2dfd71 | 2015-04-25 14:38:30 +0200 | [diff] [blame] | 157 | #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \ |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 158 | do { \ |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 159 | MemoryRegionSection mrs = section_from_flat_range(fr, \ |
| 160 | address_space_to_flatview(as)); \ |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 161 | MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \ |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 162 | } while(0) |
Avi Kivity | 0e0d36b | 2012-02-08 16:39:45 +0200 | [diff] [blame] | 163 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 164 | struct CoalescedMemoryRange { |
| 165 | AddrRange addr; |
| 166 | QTAILQ_ENTRY(CoalescedMemoryRange) link; |
| 167 | }; |
| 168 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 169 | struct MemoryRegionIoeventfd { |
| 170 | AddrRange addr; |
| 171 | bool match_data; |
| 172 | uint64_t data; |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 173 | EventNotifier *e; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a, |
| 177 | MemoryRegionIoeventfd b) |
| 178 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 179 | if (int128_lt(a.addr.start, b.addr.start)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 180 | return true; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 181 | } else if (int128_gt(a.addr.start, b.addr.start)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 182 | return false; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 183 | } else if (int128_lt(a.addr.size, b.addr.size)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 184 | return true; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 185 | } else if (int128_gt(a.addr.size, b.addr.size)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 186 | return false; |
| 187 | } else if (a.match_data < b.match_data) { |
| 188 | return true; |
| 189 | } else if (a.match_data > b.match_data) { |
| 190 | return false; |
| 191 | } else if (a.match_data) { |
| 192 | if (a.data < b.data) { |
| 193 | return true; |
| 194 | } else if (a.data > b.data) { |
| 195 | return false; |
| 196 | } |
| 197 | } |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 198 | if (a.e < b.e) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 199 | return true; |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 200 | } else if (a.e > b.e) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a, |
| 207 | MemoryRegionIoeventfd b) |
| 208 | { |
| 209 | return !memory_region_ioeventfd_before(a, b) |
| 210 | && !memory_region_ioeventfd_before(b, a); |
| 211 | } |
| 212 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 213 | typedef struct FlatRange FlatRange; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 214 | |
| 215 | /* Range of memory in the global map. Addresses are absolute. */ |
| 216 | struct FlatRange { |
| 217 | MemoryRegion *mr; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 218 | hwaddr offset_in_region; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 219 | AddrRange addr; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 220 | uint8_t dirty_log_mask; |
Paolo Bonzini | b138e65 | 2016-05-24 21:26:28 +0200 | [diff] [blame] | 221 | bool romd_mode; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 222 | bool readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | /* Flattened global view of current active memory hierarchy. Kept in sorted |
| 226 | * order. |
| 227 | */ |
| 228 | struct FlatView { |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 229 | struct rcu_head rcu; |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 230 | unsigned ref; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 231 | FlatRange *ranges; |
| 232 | unsigned nr; |
| 233 | unsigned nr_allocated; |
Alexey Kardashevskiy | 66a6df1 | 2017-09-21 18:50:56 +1000 | [diff] [blame] | 234 | struct AddressSpaceDispatch *dispatch; |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 235 | MemoryRegion *root; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 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 | |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 243 | static inline MemoryRegionSection |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 244 | section_from_flat_range(FlatRange *fr, FlatView *fv) |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 245 | { |
| 246 | return (MemoryRegionSection) { |
| 247 | .mr = fr->mr, |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 248 | .fv = fv, |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 249 | .offset_within_region = fr->offset_in_region, |
| 250 | .size = fr->addr.size, |
| 251 | .offset_within_address_space = int128_get64(fr->addr.start), |
| 252 | .readonly = fr->readonly, |
| 253 | }; |
| 254 | } |
| 255 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 256 | static bool flatrange_equal(FlatRange *a, FlatRange *b) |
| 257 | { |
| 258 | return a->mr == b->mr |
| 259 | && addrrange_equal(a->addr, b->addr) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 260 | && a->offset_in_region == b->offset_in_region |
Paolo Bonzini | b138e65 | 2016-05-24 21:26:28 +0200 | [diff] [blame] | 261 | && a->romd_mode == b->romd_mode |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 262 | && a->readonly == b->readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 263 | } |
| 264 | |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 265 | static FlatView *flatview_new(MemoryRegion *mr_root) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 266 | { |
Alexey Kardashevskiy | cc94cd6 | 2017-09-21 18:50:55 +1000 | [diff] [blame] | 267 | FlatView *view; |
| 268 | |
| 269 | view = g_new0(FlatView, 1); |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 270 | view->ref = 1; |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 271 | view->root = mr_root; |
| 272 | memory_region_ref(mr_root); |
Paolo Bonzini | 02d9651 | 2017-09-21 12:34:00 +0200 | [diff] [blame] | 273 | trace_flatview_new(view, mr_root); |
Alexey Kardashevskiy | cc94cd6 | 2017-09-21 18:50:55 +1000 | [diff] [blame] | 274 | |
| 275 | return view; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* Insert a range into a given position. Caller is responsible for maintaining |
| 279 | * sorting order. |
| 280 | */ |
| 281 | static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range) |
| 282 | { |
| 283 | if (view->nr == view->nr_allocated) { |
| 284 | view->nr_allocated = MAX(2 * view->nr, 10); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 285 | view->ranges = g_realloc(view->ranges, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 286 | view->nr_allocated * sizeof(*view->ranges)); |
| 287 | } |
| 288 | memmove(view->ranges + pos + 1, view->ranges + pos, |
| 289 | (view->nr - pos) * sizeof(FlatRange)); |
| 290 | view->ranges[pos] = *range; |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 291 | memory_region_ref(range->mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 292 | ++view->nr; |
| 293 | } |
| 294 | |
| 295 | static void flatview_destroy(FlatView *view) |
| 296 | { |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 297 | int i; |
| 298 | |
Paolo Bonzini | 02d9651 | 2017-09-21 12:34:00 +0200 | [diff] [blame] | 299 | trace_flatview_destroy(view, view->root); |
Alexey Kardashevskiy | 66a6df1 | 2017-09-21 18:50:56 +1000 | [diff] [blame] | 300 | if (view->dispatch) { |
| 301 | address_space_dispatch_free(view->dispatch); |
| 302 | } |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 303 | for (i = 0; i < view->nr; i++) { |
| 304 | memory_region_unref(view->ranges[i].mr); |
| 305 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 306 | g_free(view->ranges); |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 307 | memory_region_unref(view->root); |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 308 | g_free(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 309 | } |
| 310 | |
Paolo Bonzini | 447b0d0 | 2017-09-21 14:32:47 +0200 | [diff] [blame] | 311 | static bool flatview_ref(FlatView *view) |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 312 | { |
Paolo Bonzini | 447b0d0 | 2017-09-21 14:32:47 +0200 | [diff] [blame] | 313 | return atomic_fetch_inc_nonzero(&view->ref) > 0; |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void flatview_unref(FlatView *view) |
| 317 | { |
| 318 | if (atomic_fetch_dec(&view->ref) == 1) { |
Paolo Bonzini | 02d9651 | 2017-09-21 12:34:00 +0200 | [diff] [blame] | 319 | trace_flatview_destroy_rcu(view, view->root); |
Alexey Kardashevskiy | 092aa2f | 2017-09-21 18:51:07 +1000 | [diff] [blame] | 320 | assert(view->root); |
Alexey Kardashevskiy | 66a6df1 | 2017-09-21 18:50:56 +1000 | [diff] [blame] | 321 | call_rcu(view, flatview_destroy, rcu); |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 325 | FlatView *address_space_to_flatview(AddressSpace *as) |
Alexey Kardashevskiy | 66a6df1 | 2017-09-21 18:50:56 +1000 | [diff] [blame] | 326 | { |
| 327 | return atomic_rcu_read(&as->current_map); |
| 328 | } |
| 329 | |
| 330 | AddressSpaceDispatch *flatview_to_dispatch(FlatView *fv) |
| 331 | { |
| 332 | return fv->dispatch; |
| 333 | } |
| 334 | |
| 335 | AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as) |
| 336 | { |
| 337 | return flatview_to_dispatch(address_space_to_flatview(as)); |
| 338 | } |
| 339 | |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 340 | static bool can_merge(FlatRange *r1, FlatRange *r2) |
| 341 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 342 | return int128_eq(addrrange_end(r1->addr), r2->addr.start) |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 343 | && r1->mr == r2->mr |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 344 | && int128_eq(int128_add(int128_make64(r1->offset_in_region), |
| 345 | r1->addr.size), |
| 346 | int128_make64(r2->offset_in_region)) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 347 | && r1->dirty_log_mask == r2->dirty_log_mask |
Paolo Bonzini | b138e65 | 2016-05-24 21:26:28 +0200 | [diff] [blame] | 348 | && r1->romd_mode == r2->romd_mode |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 349 | && r1->readonly == r2->readonly; |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 350 | } |
| 351 | |
Peter Crosthwaite | 8508e02 | 2013-06-03 15:31:56 +1000 | [diff] [blame] | 352 | /* Attempt to simplify a view by merging adjacent ranges */ |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 353 | static void flatview_simplify(FlatView *view) |
| 354 | { |
| 355 | unsigned i, j; |
| 356 | |
| 357 | i = 0; |
| 358 | while (i < view->nr) { |
| 359 | j = i + 1; |
| 360 | while (j < view->nr |
| 361 | && can_merge(&view->ranges[j-1], &view->ranges[j])) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 362 | int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size); |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 363 | ++j; |
| 364 | } |
| 365 | ++i; |
| 366 | memmove(&view->ranges[i], &view->ranges[j], |
| 367 | (view->nr - j) * sizeof(view->ranges[j])); |
| 368 | view->nr -= j - i; |
| 369 | } |
| 370 | } |
| 371 | |
Paolo Bonzini | e7342aa | 2013-07-22 15:54:37 +0200 | [diff] [blame] | 372 | static bool memory_region_big_endian(MemoryRegion *mr) |
| 373 | { |
| 374 | #ifdef TARGET_WORDS_BIGENDIAN |
| 375 | return mr->ops->endianness != DEVICE_LITTLE_ENDIAN; |
| 376 | #else |
| 377 | return mr->ops->endianness == DEVICE_BIG_ENDIAN; |
| 378 | #endif |
| 379 | } |
| 380 | |
Paolo Bonzini | e11ef3d | 2013-07-22 15:54:35 +0200 | [diff] [blame] | 381 | static bool memory_region_wrong_endianness(MemoryRegion *mr) |
| 382 | { |
| 383 | #ifdef TARGET_WORDS_BIGENDIAN |
| 384 | return mr->ops->endianness == DEVICE_LITTLE_ENDIAN; |
| 385 | #else |
| 386 | return mr->ops->endianness == DEVICE_BIG_ENDIAN; |
| 387 | #endif |
| 388 | } |
| 389 | |
| 390 | static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size) |
| 391 | { |
| 392 | if (memory_region_wrong_endianness(mr)) { |
| 393 | switch (size) { |
| 394 | case 1: |
| 395 | break; |
| 396 | case 2: |
| 397 | *data = bswap16(*data); |
| 398 | break; |
| 399 | case 4: |
| 400 | *data = bswap32(*data); |
| 401 | break; |
| 402 | case 8: |
| 403 | *data = bswap64(*data); |
| 404 | break; |
| 405 | default: |
| 406 | abort(); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 411 | static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset) |
| 412 | { |
| 413 | MemoryRegion *root; |
| 414 | hwaddr abs_addr = offset; |
| 415 | |
| 416 | abs_addr += mr->addr; |
| 417 | for (root = mr; root->container; ) { |
| 418 | root = root->container; |
| 419 | abs_addr += root->addr; |
| 420 | } |
| 421 | |
| 422 | return abs_addr; |
| 423 | } |
| 424 | |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 425 | static int get_cpu_index(void) |
| 426 | { |
| 427 | if (current_cpu) { |
| 428 | return current_cpu->cpu_index; |
| 429 | } |
| 430 | return -1; |
| 431 | } |
| 432 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 433 | static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr, |
| 434 | hwaddr addr, |
| 435 | uint64_t *value, |
| 436 | unsigned size, |
| 437 | unsigned shift, |
| 438 | uint64_t mask, |
| 439 | MemTxAttrs attrs) |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 440 | { |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 441 | uint64_t tmp; |
| 442 | |
| 443 | tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 444 | if (mr->subpage) { |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 445 | trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size); |
Hollis Blanchard | f2d0894 | 2016-03-02 12:12:55 -0800 | [diff] [blame] | 446 | } else if (mr == &io_mem_notdirty) { |
| 447 | /* Accesses to code which has previously been translated into a TB show |
| 448 | * up in the MMIO path, as accesses to the io_mem_notdirty |
| 449 | * MemoryRegion. */ |
| 450 | trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size); |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 451 | } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) { |
| 452 | hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr); |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 453 | trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 454 | } |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 455 | *value |= (tmp & mask) << shift; |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 456 | return MEMTX_OK; |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 459 | static MemTxResult memory_region_read_accessor(MemoryRegion *mr, |
| 460 | hwaddr addr, |
| 461 | uint64_t *value, |
| 462 | unsigned size, |
| 463 | unsigned shift, |
| 464 | uint64_t mask, |
| 465 | MemTxAttrs attrs) |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 466 | { |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 467 | uint64_t tmp; |
| 468 | |
| 469 | tmp = mr->ops->read(mr->opaque, addr, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 470 | if (mr->subpage) { |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 471 | trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size); |
Hollis Blanchard | f2d0894 | 2016-03-02 12:12:55 -0800 | [diff] [blame] | 472 | } else if (mr == &io_mem_notdirty) { |
| 473 | /* Accesses to code which has previously been translated into a TB show |
| 474 | * up in the MMIO path, as accesses to the io_mem_notdirty |
| 475 | * MemoryRegion. */ |
| 476 | trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size); |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 477 | } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) { |
| 478 | hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr); |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 479 | trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 480 | } |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 481 | *value |= (tmp & mask) << shift; |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 482 | return MEMTX_OK; |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 483 | } |
| 484 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 485 | static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr, |
| 486 | hwaddr addr, |
| 487 | uint64_t *value, |
| 488 | unsigned size, |
| 489 | unsigned shift, |
| 490 | uint64_t mask, |
| 491 | MemTxAttrs attrs) |
| 492 | { |
| 493 | uint64_t tmp = 0; |
| 494 | MemTxResult r; |
| 495 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 496 | r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 497 | if (mr->subpage) { |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 498 | trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size); |
Hollis Blanchard | f2d0894 | 2016-03-02 12:12:55 -0800 | [diff] [blame] | 499 | } else if (mr == &io_mem_notdirty) { |
| 500 | /* Accesses to code which has previously been translated into a TB show |
| 501 | * up in the MMIO path, as accesses to the io_mem_notdirty |
| 502 | * MemoryRegion. */ |
| 503 | trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size); |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 504 | } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) { |
| 505 | hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr); |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 506 | trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 507 | } |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 508 | *value |= (tmp & mask) << shift; |
| 509 | return r; |
| 510 | } |
| 511 | |
| 512 | static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr, |
| 513 | hwaddr addr, |
| 514 | uint64_t *value, |
| 515 | unsigned size, |
| 516 | unsigned shift, |
| 517 | uint64_t mask, |
| 518 | MemTxAttrs attrs) |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 519 | { |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 520 | uint64_t tmp; |
| 521 | |
| 522 | tmp = (*value >> shift) & mask; |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 523 | if (mr->subpage) { |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 524 | trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size); |
Hollis Blanchard | f2d0894 | 2016-03-02 12:12:55 -0800 | [diff] [blame] | 525 | } else if (mr == &io_mem_notdirty) { |
| 526 | /* Accesses to code which has previously been translated into a TB show |
| 527 | * up in the MMIO path, as accesses to the io_mem_notdirty |
| 528 | * MemoryRegion. */ |
| 529 | trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size); |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 530 | } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) { |
| 531 | hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr); |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 532 | trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 533 | } |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 534 | mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp); |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 535 | return MEMTX_OK; |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 536 | } |
| 537 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 538 | static MemTxResult memory_region_write_accessor(MemoryRegion *mr, |
| 539 | hwaddr addr, |
| 540 | uint64_t *value, |
| 541 | unsigned size, |
| 542 | unsigned shift, |
| 543 | uint64_t mask, |
| 544 | MemTxAttrs attrs) |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 545 | { |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 546 | uint64_t tmp; |
| 547 | |
| 548 | tmp = (*value >> shift) & mask; |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 549 | if (mr->subpage) { |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 550 | trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size); |
Hollis Blanchard | f2d0894 | 2016-03-02 12:12:55 -0800 | [diff] [blame] | 551 | } else if (mr == &io_mem_notdirty) { |
| 552 | /* Accesses to code which has previously been translated into a TB show |
| 553 | * up in the MMIO path, as accesses to the io_mem_notdirty |
| 554 | * MemoryRegion. */ |
| 555 | trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size); |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 556 | } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) { |
| 557 | hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr); |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 558 | trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 559 | } |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 560 | mr->ops->write(mr->opaque, addr, tmp, size); |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 561 | return MEMTX_OK; |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 562 | } |
| 563 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 564 | static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr, |
| 565 | hwaddr addr, |
| 566 | uint64_t *value, |
| 567 | unsigned size, |
| 568 | unsigned shift, |
| 569 | uint64_t mask, |
| 570 | MemTxAttrs attrs) |
| 571 | { |
| 572 | uint64_t tmp; |
| 573 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 574 | tmp = (*value >> shift) & mask; |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 575 | if (mr->subpage) { |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 576 | trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size); |
Hollis Blanchard | f2d0894 | 2016-03-02 12:12:55 -0800 | [diff] [blame] | 577 | } else if (mr == &io_mem_notdirty) { |
| 578 | /* Accesses to code which has previously been translated into a TB show |
| 579 | * up in the MMIO path, as accesses to the io_mem_notdirty |
| 580 | * MemoryRegion. */ |
| 581 | trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size); |
Hollis Blanchard | 4779dc1 | 2016-02-08 16:03:05 -0800 | [diff] [blame] | 582 | } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) { |
| 583 | hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr); |
Hollis Blanchard | 5a68be9 | 2016-03-02 12:12:54 -0800 | [diff] [blame] | 584 | trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size); |
Hollis Blanchard | 23d92d6 | 2016-02-08 16:03:04 -0800 | [diff] [blame] | 585 | } |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 586 | return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs); |
| 587 | } |
| 588 | |
| 589 | static MemTxResult access_with_adjusted_size(hwaddr addr, |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 590 | uint64_t *value, |
| 591 | unsigned size, |
| 592 | unsigned access_size_min, |
| 593 | unsigned access_size_max, |
KONRAD Frederic | 05e015f | 2017-09-21 12:04:20 +0200 | [diff] [blame] | 594 | MemTxResult (*access_fn) |
| 595 | (MemoryRegion *mr, |
| 596 | hwaddr addr, |
| 597 | uint64_t *value, |
| 598 | unsigned size, |
| 599 | unsigned shift, |
| 600 | uint64_t mask, |
| 601 | MemTxAttrs attrs), |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 602 | MemoryRegion *mr, |
| 603 | MemTxAttrs attrs) |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 604 | { |
| 605 | uint64_t access_mask; |
| 606 | unsigned access_size; |
| 607 | unsigned i; |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 608 | MemTxResult r = MEMTX_OK; |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 609 | |
| 610 | if (!access_size_min) { |
| 611 | access_size_min = 1; |
| 612 | } |
| 613 | if (!access_size_max) { |
| 614 | access_size_max = 4; |
| 615 | } |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 616 | |
| 617 | /* FIXME: support unaligned access? */ |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 618 | access_size = MAX(MIN(size, access_size_max), access_size_min); |
| 619 | access_mask = -1ULL >> (64 - access_size * 8); |
Paolo Bonzini | e7342aa | 2013-07-22 15:54:37 +0200 | [diff] [blame] | 620 | if (memory_region_big_endian(mr)) { |
| 621 | for (i = 0; i < size; i += access_size) { |
KONRAD Frederic | 05e015f | 2017-09-21 12:04:20 +0200 | [diff] [blame] | 622 | r |= access_fn(mr, addr + i, value, access_size, |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 623 | (size - access_size - i) * 8, access_mask, attrs); |
Paolo Bonzini | e7342aa | 2013-07-22 15:54:37 +0200 | [diff] [blame] | 624 | } |
| 625 | } else { |
| 626 | for (i = 0; i < size; i += access_size) { |
KONRAD Frederic | 05e015f | 2017-09-21 12:04:20 +0200 | [diff] [blame] | 627 | r |= access_fn(mr, addr + i, value, access_size, i * 8, |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 628 | access_mask, attrs); |
Paolo Bonzini | e7342aa | 2013-07-22 15:54:37 +0200 | [diff] [blame] | 629 | } |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 630 | } |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 631 | return r; |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 632 | } |
| 633 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 634 | static AddressSpace *memory_region_to_address_space(MemoryRegion *mr) |
| 635 | { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 636 | AddressSpace *as; |
| 637 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 638 | while (mr->container) { |
| 639 | mr = mr->container; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 640 | } |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 641 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 642 | if (mr == as->root) { |
| 643 | return as; |
| 644 | } |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 645 | } |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 646 | return NULL; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 649 | /* Render a memory region into the global view. Ranges in @view obscure |
| 650 | * ranges in @mr. |
| 651 | */ |
| 652 | static void render_memory_region(FlatView *view, |
| 653 | MemoryRegion *mr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 654 | Int128 base, |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 655 | AddrRange clip, |
| 656 | bool readonly) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 657 | { |
| 658 | MemoryRegion *subregion; |
| 659 | unsigned i; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 660 | hwaddr offset_in_region; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 661 | Int128 remain; |
| 662 | Int128 now; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 663 | FlatRange fr; |
| 664 | AddrRange tmp; |
| 665 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 666 | if (!mr->enabled) { |
| 667 | return; |
| 668 | } |
| 669 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 670 | int128_addto(&base, int128_make64(mr->addr)); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 671 | readonly |= mr->readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 672 | |
| 673 | tmp = addrrange_make(base, mr->size); |
| 674 | |
| 675 | if (!addrrange_intersects(tmp, clip)) { |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | clip = addrrange_intersection(tmp, clip); |
| 680 | |
| 681 | if (mr->alias) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 682 | int128_subfrom(&base, int128_make64(mr->alias->addr)); |
| 683 | int128_subfrom(&base, int128_make64(mr->alias_offset)); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 684 | render_memory_region(view, mr->alias, base, clip, readonly); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | |
| 688 | /* Render subregions in priority order. */ |
| 689 | QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) { |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 690 | render_memory_region(view, subregion, base, clip, readonly); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 691 | } |
| 692 | |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 693 | if (!mr->terminates) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 694 | return; |
| 695 | } |
| 696 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 697 | offset_in_region = int128_get64(int128_sub(clip.start, base)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 698 | base = clip.start; |
| 699 | remain = clip.size; |
| 700 | |
Peter Crosthwaite | 2eb74e1 | 2013-06-03 15:33:29 +1000 | [diff] [blame] | 701 | fr.mr = mr; |
Paolo Bonzini | 6f6a5ef | 2015-03-23 10:57:21 +0100 | [diff] [blame] | 702 | fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr); |
Paolo Bonzini | b138e65 | 2016-05-24 21:26:28 +0200 | [diff] [blame] | 703 | fr.romd_mode = mr->romd_mode; |
Peter Crosthwaite | 2eb74e1 | 2013-06-03 15:33:29 +1000 | [diff] [blame] | 704 | fr.readonly = readonly; |
| 705 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 706 | /* Render the region itself into any gaps left by the current view. */ |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 707 | for (i = 0; i < view->nr && int128_nz(remain); ++i) { |
| 708 | if (int128_ge(base, addrrange_end(view->ranges[i].addr))) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 709 | continue; |
| 710 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 711 | if (int128_lt(base, view->ranges[i].addr.start)) { |
| 712 | now = int128_min(remain, |
| 713 | int128_sub(view->ranges[i].addr.start, base)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 714 | fr.offset_in_region = offset_in_region; |
| 715 | fr.addr = addrrange_make(base, now); |
| 716 | flatview_insert(view, i, &fr); |
| 717 | ++i; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 718 | int128_addto(&base, now); |
| 719 | offset_in_region += int128_get64(now); |
| 720 | int128_subfrom(&remain, now); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 721 | } |
Avi Kivity | d26a8ca | 2012-10-29 18:22:36 +0200 | [diff] [blame] | 722 | now = int128_sub(int128_min(int128_add(base, remain), |
| 723 | addrrange_end(view->ranges[i].addr)), |
| 724 | base); |
| 725 | int128_addto(&base, now); |
| 726 | offset_in_region += int128_get64(now); |
| 727 | int128_subfrom(&remain, now); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 728 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 729 | if (int128_nz(remain)) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 730 | fr.offset_in_region = offset_in_region; |
| 731 | fr.addr = addrrange_make(base, remain); |
| 732 | flatview_insert(view, i, &fr); |
| 733 | } |
| 734 | } |
| 735 | |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 736 | static MemoryRegion *memory_region_get_flatview_root(MemoryRegion *mr) |
| 737 | { |
Paolo Bonzini | e673ba9 | 2017-09-21 12:28:16 +0200 | [diff] [blame] | 738 | while (mr->enabled) { |
| 739 | if (mr->alias) { |
| 740 | if (!mr->alias_offset && int128_ge(mr->size, mr->alias->size)) { |
| 741 | /* The alias is included in its entirety. Use it as |
| 742 | * the "real" root, so that we can share more FlatViews. |
| 743 | */ |
| 744 | mr = mr->alias; |
| 745 | continue; |
| 746 | } |
| 747 | } else if (!mr->terminates) { |
| 748 | unsigned int found = 0; |
| 749 | MemoryRegion *child, *next = NULL; |
| 750 | QTAILQ_FOREACH(child, &mr->subregions, subregions_link) { |
| 751 | if (child->enabled) { |
| 752 | if (++found > 1) { |
| 753 | next = NULL; |
| 754 | break; |
| 755 | } |
| 756 | if (!child->addr && int128_ge(mr->size, child->size)) { |
| 757 | /* A child is included in its entirety. If it's the only |
| 758 | * enabled one, use it in the hope of finding an alias down the |
| 759 | * way. This will also let us share FlatViews. |
| 760 | */ |
| 761 | next = child; |
| 762 | } |
| 763 | } |
| 764 | } |
Alexey Kardashevskiy | 092aa2f | 2017-09-21 18:51:07 +1000 | [diff] [blame] | 765 | if (found == 0) { |
| 766 | return NULL; |
| 767 | } |
Paolo Bonzini | e673ba9 | 2017-09-21 12:28:16 +0200 | [diff] [blame] | 768 | if (next) { |
| 769 | mr = next; |
| 770 | continue; |
| 771 | } |
| 772 | } |
| 773 | |
Alexey Kardashevskiy | 092aa2f | 2017-09-21 18:51:07 +1000 | [diff] [blame] | 774 | return mr; |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 775 | } |
| 776 | |
Alexey Kardashevskiy | 092aa2f | 2017-09-21 18:51:07 +1000 | [diff] [blame] | 777 | return NULL; |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 778 | } |
| 779 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 780 | /* Render a memory topology into a list of disjoint absolute ranges. */ |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 781 | static FlatView *generate_memory_topology(MemoryRegion *mr) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 782 | { |
Alexey Kardashevskiy | 9bf561e | 2017-09-21 18:51:02 +1000 | [diff] [blame] | 783 | int i; |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 784 | FlatView *view; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 785 | |
Alexey Kardashevskiy | 89c177b | 2017-09-21 18:51:01 +1000 | [diff] [blame] | 786 | view = flatview_new(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 787 | |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 788 | if (mr) { |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 789 | render_memory_region(view, mr, int128_zero(), |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 790 | addrrange_make(int128_zero(), int128_2_64()), false); |
| 791 | } |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 792 | flatview_simplify(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 793 | |
Alexey Kardashevskiy | 9bf561e | 2017-09-21 18:51:02 +1000 | [diff] [blame] | 794 | view->dispatch = address_space_dispatch_new(view); |
| 795 | for (i = 0; i < view->nr; i++) { |
| 796 | MemoryRegionSection mrs = |
| 797 | section_from_flat_range(&view->ranges[i], view); |
| 798 | flatview_add_to_dispatch(view, &mrs); |
| 799 | } |
| 800 | address_space_dispatch_compact(view->dispatch); |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 801 | g_hash_table_replace(flat_views, mr, view); |
Alexey Kardashevskiy | 9bf561e | 2017-09-21 18:51:02 +1000 | [diff] [blame] | 802 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 803 | return view; |
| 804 | } |
| 805 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 806 | static void address_space_add_del_ioeventfds(AddressSpace *as, |
| 807 | MemoryRegionIoeventfd *fds_new, |
| 808 | unsigned fds_new_nb, |
| 809 | MemoryRegionIoeventfd *fds_old, |
| 810 | unsigned fds_old_nb) |
| 811 | { |
| 812 | unsigned iold, inew; |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 813 | MemoryRegionIoeventfd *fd; |
| 814 | MemoryRegionSection section; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 815 | |
| 816 | /* Generate a symmetric difference of the old and new fd sets, adding |
| 817 | * and deleting as necessary. |
| 818 | */ |
| 819 | |
| 820 | iold = inew = 0; |
| 821 | while (iold < fds_old_nb || inew < fds_new_nb) { |
| 822 | if (iold < fds_old_nb |
| 823 | && (inew == fds_new_nb |
| 824 | || memory_region_ioeventfd_before(fds_old[iold], |
| 825 | fds_new[inew]))) { |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 826 | fd = &fds_old[iold]; |
| 827 | section = (MemoryRegionSection) { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 828 | .fv = address_space_to_flatview(as), |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 829 | .offset_within_address_space = int128_get64(fd->addr.start), |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 830 | .size = fd->addr.size, |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 831 | }; |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 832 | MEMORY_LISTENER_CALL(as, eventfd_del, Forward, §ion, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 833 | fd->match_data, fd->data, fd->e); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 834 | ++iold; |
| 835 | } else if (inew < fds_new_nb |
| 836 | && (iold == fds_old_nb |
| 837 | || memory_region_ioeventfd_before(fds_new[inew], |
| 838 | fds_old[iold]))) { |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 839 | fd = &fds_new[inew]; |
| 840 | section = (MemoryRegionSection) { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 841 | .fv = address_space_to_flatview(as), |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 842 | .offset_within_address_space = int128_get64(fd->addr.start), |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 843 | .size = fd->addr.size, |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 844 | }; |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 845 | MEMORY_LISTENER_CALL(as, eventfd_add, Reverse, §ion, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 846 | fd->match_data, fd->data, fd->e); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 847 | ++inew; |
| 848 | } else { |
| 849 | ++iold; |
| 850 | ++inew; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 855 | static FlatView *address_space_get_flatview(AddressSpace *as) |
| 856 | { |
| 857 | FlatView *view; |
| 858 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 859 | rcu_read_lock(); |
Paolo Bonzini | 447b0d0 | 2017-09-21 14:32:47 +0200 | [diff] [blame] | 860 | do { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 861 | view = address_space_to_flatview(as); |
Paolo Bonzini | 447b0d0 | 2017-09-21 14:32:47 +0200 | [diff] [blame] | 862 | /* If somebody has replaced as->current_map concurrently, |
| 863 | * flatview_ref returns false. |
| 864 | */ |
| 865 | } while (!flatview_ref(view)); |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 866 | rcu_read_unlock(); |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 867 | return view; |
| 868 | } |
| 869 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 870 | static void address_space_update_ioeventfds(AddressSpace *as) |
| 871 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 872 | FlatView *view; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 873 | FlatRange *fr; |
| 874 | unsigned ioeventfd_nb = 0; |
| 875 | MemoryRegionIoeventfd *ioeventfds = NULL; |
| 876 | AddrRange tmp; |
| 877 | unsigned i; |
| 878 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 879 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 880 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 881 | for (i = 0; i < fr->mr->ioeventfd_nb; ++i) { |
| 882 | tmp = addrrange_shift(fr->mr->ioeventfds[i].addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 883 | int128_sub(fr->addr.start, |
| 884 | int128_make64(fr->offset_in_region))); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 885 | if (addrrange_intersects(fr->addr, tmp)) { |
| 886 | ++ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 887 | ioeventfds = g_realloc(ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 888 | ioeventfd_nb * sizeof(*ioeventfds)); |
| 889 | ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i]; |
| 890 | ioeventfds[ioeventfd_nb-1].addr = tmp; |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb, |
| 896 | as->ioeventfds, as->ioeventfd_nb); |
| 897 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 898 | g_free(as->ioeventfds); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 899 | as->ioeventfds = ioeventfds; |
| 900 | as->ioeventfd_nb = ioeventfd_nb; |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 901 | flatview_unref(view); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 902 | } |
| 903 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 904 | static void address_space_update_topology_pass(AddressSpace *as, |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 905 | const FlatView *old_view, |
| 906 | const FlatView *new_view, |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 907 | bool adding) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 908 | { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 909 | unsigned iold, inew; |
| 910 | FlatRange *frold, *frnew; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 911 | |
| 912 | /* Generate a symmetric difference of the old and new memory maps. |
| 913 | * Kill ranges in the old map, and instantiate ranges in the new map. |
| 914 | */ |
| 915 | iold = inew = 0; |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 916 | while (iold < old_view->nr || inew < new_view->nr) { |
| 917 | if (iold < old_view->nr) { |
| 918 | frold = &old_view->ranges[iold]; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 919 | } else { |
| 920 | frold = NULL; |
| 921 | } |
Paolo Bonzini | a9a0c06 | 2013-05-06 10:29:07 +0200 | [diff] [blame] | 922 | if (inew < new_view->nr) { |
| 923 | frnew = &new_view->ranges[inew]; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 924 | } else { |
| 925 | frnew = NULL; |
| 926 | } |
| 927 | |
| 928 | if (frold |
| 929 | && (!frnew |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 930 | || int128_lt(frold->addr.start, frnew->addr.start) |
| 931 | || (int128_eq(frold->addr.start, frnew->addr.start) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 932 | && !flatrange_equal(frold, frnew)))) { |
Peter Crosthwaite | 41a6e47 | 2013-06-03 15:32:42 +1000 | [diff] [blame] | 933 | /* In old but not in new, or in both but attributes changed. */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 934 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 935 | if (!adding) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 936 | MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 937 | } |
| 938 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 939 | ++iold; |
| 940 | } else if (frold && frnew && flatrange_equal(frold, frnew)) { |
Peter Crosthwaite | 41a6e47 | 2013-06-03 15:32:42 +1000 | [diff] [blame] | 941 | /* In both and unchanged (except logging may have changed) */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 942 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 943 | if (adding) { |
Avi Kivity | 50c1e14 | 2012-02-08 21:36:02 +0200 | [diff] [blame] | 944 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop); |
Paolo Bonzini | b2dfd71 | 2015-04-25 14:38:30 +0200 | [diff] [blame] | 945 | if (frnew->dirty_log_mask & ~frold->dirty_log_mask) { |
| 946 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start, |
| 947 | frold->dirty_log_mask, |
| 948 | frnew->dirty_log_mask); |
| 949 | } |
| 950 | if (frold->dirty_log_mask & ~frnew->dirty_log_mask) { |
| 951 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop, |
| 952 | frold->dirty_log_mask, |
| 953 | frnew->dirty_log_mask); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 954 | } |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 955 | } |
| 956 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 957 | ++iold; |
| 958 | ++inew; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 959 | } else { |
| 960 | /* In new */ |
| 961 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 962 | if (adding) { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 963 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add); |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 964 | } |
| 965 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 966 | ++inew; |
| 967 | } |
| 968 | } |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 969 | } |
| 970 | |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 971 | static void flatviews_init(void) |
| 972 | { |
Alexey Kardashevskiy | 092aa2f | 2017-09-21 18:51:07 +1000 | [diff] [blame] | 973 | static FlatView *empty_view; |
| 974 | |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 975 | if (flat_views) { |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, |
| 980 | (GDestroyNotify) flatview_unref); |
Alexey Kardashevskiy | 092aa2f | 2017-09-21 18:51:07 +1000 | [diff] [blame] | 981 | if (!empty_view) { |
| 982 | empty_view = generate_memory_topology(NULL); |
| 983 | /* We keep it alive forever in the global variable. */ |
| 984 | flatview_ref(empty_view); |
| 985 | } else { |
| 986 | g_hash_table_replace(flat_views, NULL, empty_view); |
| 987 | flatview_ref(empty_view); |
| 988 | } |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static void flatviews_reset(void) |
| 992 | { |
| 993 | AddressSpace *as; |
| 994 | |
| 995 | if (flat_views) { |
| 996 | g_hash_table_unref(flat_views); |
| 997 | flat_views = NULL; |
| 998 | } |
| 999 | flatviews_init(); |
| 1000 | |
| 1001 | /* Render unique FVs */ |
| 1002 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 1003 | MemoryRegion *physmr = memory_region_get_flatview_root(as->root); |
| 1004 | |
| 1005 | if (g_hash_table_lookup(flat_views, physmr)) { |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | generate_memory_topology(physmr); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | static void address_space_set_flatview(AddressSpace *as) |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 1014 | { |
Alexey Kardashevskiy | 67ace39 | 2017-09-21 18:51:05 +1000 | [diff] [blame] | 1015 | FlatView *old_view = address_space_to_flatview(as); |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 1016 | MemoryRegion *physmr = memory_region_get_flatview_root(as->root); |
| 1017 | FlatView *new_view = g_hash_table_lookup(flat_views, physmr); |
| 1018 | |
| 1019 | assert(new_view); |
| 1020 | |
Alexey Kardashevskiy | 67ace39 | 2017-09-21 18:51:05 +1000 | [diff] [blame] | 1021 | if (old_view == new_view) { |
| 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | if (old_view) { |
| 1026 | flatview_ref(old_view); |
| 1027 | } |
| 1028 | |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 1029 | flatview_ref(new_view); |
Alexey Kardashevskiy | 9a62e24 | 2017-09-21 18:50:54 +1000 | [diff] [blame] | 1030 | |
| 1031 | if (!QTAILQ_EMPTY(&as->listeners)) { |
Alexey Kardashevskiy | 67ace39 | 2017-09-21 18:51:05 +1000 | [diff] [blame] | 1032 | FlatView tmpview = { .nr = 0 }, *old_view2 = old_view; |
| 1033 | |
| 1034 | if (!old_view2) { |
| 1035 | old_view2 = &tmpview; |
| 1036 | } |
| 1037 | address_space_update_topology_pass(as, old_view2, new_view, false); |
| 1038 | address_space_update_topology_pass(as, old_view2, new_view, true); |
Alexey Kardashevskiy | 9a62e24 | 2017-09-21 18:50:54 +1000 | [diff] [blame] | 1039 | } |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 1040 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 1041 | /* Writes are protected by the BQL. */ |
| 1042 | atomic_rcu_set(&as->current_map, new_view); |
Alexey Kardashevskiy | 67ace39 | 2017-09-21 18:51:05 +1000 | [diff] [blame] | 1043 | if (old_view) { |
| 1044 | flatview_unref(old_view); |
| 1045 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 1046 | |
| 1047 | /* Note that all the old MemoryRegions are still alive up to this |
| 1048 | * point. This relieves most MemoryListeners from the need to |
| 1049 | * ref/unref the MemoryRegions they get---unless they use them |
| 1050 | * outside the iothread mutex, in which case precise reference |
| 1051 | * counting is necessary. |
| 1052 | */ |
Alexey Kardashevskiy | 67ace39 | 2017-09-21 18:51:05 +1000 | [diff] [blame] | 1053 | if (old_view) { |
| 1054 | flatview_unref(old_view); |
| 1055 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1056 | } |
| 1057 | |
Alexey Kardashevskiy | 202fc01 | 2017-09-21 18:51:09 +1000 | [diff] [blame] | 1058 | static void address_space_update_topology(AddressSpace *as) |
| 1059 | { |
| 1060 | MemoryRegion *physmr = memory_region_get_flatview_root(as->root); |
| 1061 | |
| 1062 | flatviews_init(); |
| 1063 | if (!g_hash_table_lookup(flat_views, physmr)) { |
| 1064 | generate_memory_topology(physmr); |
| 1065 | } |
| 1066 | address_space_set_flatview(as); |
| 1067 | } |
| 1068 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1069 | void memory_region_transaction_begin(void) |
| 1070 | { |
Jan Kiszka | bb880de | 2012-08-23 13:02:32 +0200 | [diff] [blame] | 1071 | qemu_flush_coalesced_mmio_buffer(); |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1072 | ++memory_region_transaction_depth; |
| 1073 | } |
| 1074 | |
| 1075 | void memory_region_transaction_commit(void) |
| 1076 | { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1077 | AddressSpace *as; |
| 1078 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1079 | assert(memory_region_transaction_depth); |
Jan Kiszka | 8d04fb5 | 2017-02-23 18:29:11 +0000 | [diff] [blame] | 1080 | assert(qemu_mutex_iothread_locked()); |
| 1081 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1082 | --memory_region_transaction_depth; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1083 | if (!memory_region_transaction_depth) { |
| 1084 | if (memory_region_update_pending) { |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 1085 | flatviews_reset(); |
| 1086 | |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1087 | MEMORY_LISTENER_CALL_GLOBAL(begin, Forward); |
Jan Kiszka | 02e2b95 | 2012-08-23 13:02:31 +0200 | [diff] [blame] | 1088 | |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1089 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
Alexey Kardashevskiy | 967dc9b | 2017-09-21 18:51:04 +1000 | [diff] [blame] | 1090 | address_space_set_flatview(as); |
Alexey Kardashevskiy | 0221848 | 2017-09-21 18:51:03 +1000 | [diff] [blame] | 1091 | address_space_update_ioeventfds(as); |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1092 | } |
Xu, Anthony | ade9c1a | 2017-03-22 17:53:35 +0000 | [diff] [blame] | 1093 | memory_region_update_pending = false; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1094 | MEMORY_LISTENER_CALL_GLOBAL(commit, Forward); |
| 1095 | } else if (ioeventfd_update_pending) { |
| 1096 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 1097 | address_space_update_ioeventfds(as); |
| 1098 | } |
Xu, Anthony | ade9c1a | 2017-03-22 17:53:35 +0000 | [diff] [blame] | 1099 | ioeventfd_update_pending = false; |
Jan Kiszka | 02e2b95 | 2012-08-23 13:02:31 +0200 | [diff] [blame] | 1100 | } |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 1101 | } |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1102 | } |
| 1103 | |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1104 | static void memory_region_destructor_none(MemoryRegion *mr) |
| 1105 | { |
| 1106 | } |
| 1107 | |
| 1108 | static void memory_region_destructor_ram(MemoryRegion *mr) |
| 1109 | { |
Fam Zheng | f1060c5 | 2016-03-01 14:18:22 +0800 | [diff] [blame] | 1110 | qemu_ram_free(mr->ram_block); |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1111 | } |
| 1112 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1113 | static bool memory_region_need_escape(char c) |
| 1114 | { |
| 1115 | return c == '/' || c == '[' || c == '\\' || c == ']'; |
| 1116 | } |
| 1117 | |
| 1118 | static char *memory_region_escape_name(const char *name) |
| 1119 | { |
| 1120 | const char *p; |
| 1121 | char *escaped, *q; |
| 1122 | uint8_t c; |
| 1123 | size_t bytes = 0; |
| 1124 | |
| 1125 | for (p = name; *p; p++) { |
| 1126 | bytes += memory_region_need_escape(*p) ? 4 : 1; |
| 1127 | } |
| 1128 | if (bytes == p - name) { |
| 1129 | return g_memdup(name, bytes + 1); |
| 1130 | } |
| 1131 | |
| 1132 | escaped = g_malloc(bytes + 1); |
| 1133 | for (p = name, q = escaped; *p; p++) { |
| 1134 | c = *p; |
| 1135 | if (unlikely(memory_region_need_escape(c))) { |
| 1136 | *q++ = '\\'; |
| 1137 | *q++ = 'x'; |
| 1138 | *q++ = "0123456789abcdef"[c >> 4]; |
| 1139 | c = "0123456789abcdef"[c & 15]; |
| 1140 | } |
| 1141 | *q++ = c; |
| 1142 | } |
| 1143 | *q = 0; |
| 1144 | return escaped; |
| 1145 | } |
| 1146 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1147 | static void memory_region_do_init(MemoryRegion *mr, |
| 1148 | Object *owner, |
| 1149 | const char *name, |
| 1150 | uint64_t size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1151 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1152 | mr->size = int128_make64(size); |
| 1153 | if (size == UINT64_MAX) { |
| 1154 | mr->size = int128_2_64(); |
| 1155 | } |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 1156 | mr->name = g_strdup(name); |
Paolo Bonzini | 612263c | 2015-12-09 11:44:25 +0100 | [diff] [blame] | 1157 | mr->owner = owner; |
Gonglei | 58eaa21 | 2016-02-22 16:34:55 +0800 | [diff] [blame] | 1158 | mr->ram_block = NULL; |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1159 | |
| 1160 | if (name) { |
Peter Crosthwaite | 843ef73 | 2014-08-19 23:56:26 -0700 | [diff] [blame] | 1161 | char *escaped_name = memory_region_escape_name(name); |
| 1162 | char *name_array = g_strdup_printf("%s[*]", escaped_name); |
Paolo Bonzini | 612263c | 2015-12-09 11:44:25 +0100 | [diff] [blame] | 1163 | |
| 1164 | if (!owner) { |
| 1165 | owner = container_get(qdev_get_machine(), "/unattached"); |
| 1166 | } |
| 1167 | |
Peter Crosthwaite | 843ef73 | 2014-08-19 23:56:26 -0700 | [diff] [blame] | 1168 | object_property_add_child(owner, name_array, OBJECT(mr), &error_abort); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1169 | object_unref(OBJECT(mr)); |
Peter Crosthwaite | 843ef73 | 2014-08-19 23:56:26 -0700 | [diff] [blame] | 1170 | g_free(name_array); |
| 1171 | g_free(escaped_name); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1175 | void memory_region_init(MemoryRegion *mr, |
| 1176 | Object *owner, |
| 1177 | const char *name, |
| 1178 | uint64_t size) |
| 1179 | { |
| 1180 | object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION); |
| 1181 | memory_region_do_init(mr, owner, name, size); |
| 1182 | } |
| 1183 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 1184 | static void memory_region_get_addr(Object *obj, Visitor *v, const char *name, |
| 1185 | void *opaque, Error **errp) |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 1186 | { |
| 1187 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1188 | uint64_t value = mr->addr; |
| 1189 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 1190 | visit_type_uint64(v, name, &value, errp); |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 1193 | static void memory_region_get_container(Object *obj, Visitor *v, |
| 1194 | const char *name, void *opaque, |
| 1195 | Error **errp) |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 1196 | { |
| 1197 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1198 | gchar *path = (gchar *)""; |
| 1199 | |
| 1200 | if (mr->container) { |
| 1201 | path = object_get_canonical_path(OBJECT(mr->container)); |
| 1202 | } |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 1203 | visit_type_str(v, name, &path, errp); |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 1204 | if (mr->container) { |
| 1205 | g_free(path); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | static Object *memory_region_resolve_container(Object *obj, void *opaque, |
| 1210 | const char *part) |
| 1211 | { |
| 1212 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1213 | |
| 1214 | return OBJECT(mr->container); |
| 1215 | } |
| 1216 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 1217 | static void memory_region_get_priority(Object *obj, Visitor *v, |
| 1218 | const char *name, void *opaque, |
| 1219 | Error **errp) |
Peter Crosthwaite | d33382d | 2014-06-05 23:17:01 -0700 | [diff] [blame] | 1220 | { |
| 1221 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1222 | int32_t value = mr->priority; |
| 1223 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 1224 | visit_type_int32(v, name, &value, errp); |
Peter Crosthwaite | d33382d | 2014-06-05 23:17:01 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 1227 | static void memory_region_get_size(Object *obj, Visitor *v, const char *name, |
| 1228 | void *opaque, Error **errp) |
Peter Crosthwaite | 52aef7b | 2014-06-05 23:17:35 -0700 | [diff] [blame] | 1229 | { |
| 1230 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1231 | uint64_t value = memory_region_size(mr); |
| 1232 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 1233 | visit_type_uint64(v, name, &value, errp); |
Peter Crosthwaite | 52aef7b | 2014-06-05 23:17:35 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1236 | static void memory_region_initfn(Object *obj) |
| 1237 | { |
| 1238 | MemoryRegion *mr = MEMORY_REGION(obj); |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 1239 | ObjectProperty *op; |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1240 | |
| 1241 | mr->ops = &unassigned_mem_ops; |
| 1242 | mr->enabled = true; |
| 1243 | mr->romd_mode = true; |
Jan Kiszka | 196ea13 | 2015-06-18 18:47:20 +0200 | [diff] [blame] | 1244 | mr->global_locking = true; |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1245 | mr->destructor = memory_region_destructor_none; |
| 1246 | QTAILQ_INIT(&mr->subregions); |
| 1247 | QTAILQ_INIT(&mr->coalesced); |
Peter Crosthwaite | 409ddd0 | 2014-06-05 23:16:27 -0700 | [diff] [blame] | 1248 | |
| 1249 | op = object_property_add(OBJECT(mr), "container", |
| 1250 | "link<" TYPE_MEMORY_REGION ">", |
| 1251 | memory_region_get_container, |
| 1252 | NULL, /* memory_region_set_container */ |
| 1253 | NULL, NULL, &error_abort); |
| 1254 | op->resolve = memory_region_resolve_container; |
| 1255 | |
| 1256 | object_property_add(OBJECT(mr), "addr", "uint64", |
| 1257 | memory_region_get_addr, |
| 1258 | NULL, /* memory_region_set_addr */ |
| 1259 | NULL, NULL, &error_abort); |
Peter Crosthwaite | d33382d | 2014-06-05 23:17:01 -0700 | [diff] [blame] | 1260 | object_property_add(OBJECT(mr), "priority", "uint32", |
| 1261 | memory_region_get_priority, |
| 1262 | NULL, /* memory_region_set_priority */ |
| 1263 | NULL, NULL, &error_abort); |
Peter Crosthwaite | 52aef7b | 2014-06-05 23:17:35 -0700 | [diff] [blame] | 1264 | object_property_add(OBJECT(mr), "size", "uint64", |
| 1265 | memory_region_get_size, |
| 1266 | NULL, /* memory_region_set_size, */ |
| 1267 | NULL, NULL, &error_abort); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1268 | } |
| 1269 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1270 | static void iommu_memory_region_initfn(Object *obj) |
| 1271 | { |
| 1272 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1273 | |
| 1274 | mr->is_iommu = true; |
| 1275 | } |
| 1276 | |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 1277 | static uint64_t unassigned_mem_read(void *opaque, hwaddr addr, |
| 1278 | unsigned size) |
| 1279 | { |
| 1280 | #ifdef DEBUG_UNASSIGNED |
| 1281 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
| 1282 | #endif |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1283 | if (current_cpu != NULL) { |
| 1284 | cpu_unassigned_access(current_cpu, addr, false, false, 0, size); |
Andreas Färber | c658b94 | 2013-05-27 06:49:53 +0200 | [diff] [blame] | 1285 | } |
Jan Kiszka | 68a7439 | 2013-09-02 18:43:31 +0200 | [diff] [blame] | 1286 | return 0; |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | static void unassigned_mem_write(void *opaque, hwaddr addr, |
| 1290 | uint64_t val, unsigned size) |
| 1291 | { |
| 1292 | #ifdef DEBUG_UNASSIGNED |
| 1293 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val); |
| 1294 | #endif |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1295 | if (current_cpu != NULL) { |
| 1296 | cpu_unassigned_access(current_cpu, addr, true, false, 0, size); |
Andreas Färber | c658b94 | 2013-05-27 06:49:53 +0200 | [diff] [blame] | 1297 | } |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
Paolo Bonzini | d197063 | 2013-05-24 13:23:38 +0200 | [diff] [blame] | 1300 | static bool unassigned_mem_accepts(void *opaque, hwaddr addr, |
| 1301 | unsigned size, bool is_write) |
| 1302 | { |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
| 1306 | const MemoryRegionOps unassigned_mem_ops = { |
| 1307 | .valid.accepts = unassigned_mem_accepts, |
| 1308 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 1309 | }; |
| 1310 | |
Alex Williamson | 4a2e242 | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1311 | static uint64_t memory_region_ram_device_read(void *opaque, |
| 1312 | hwaddr addr, unsigned size) |
| 1313 | { |
| 1314 | MemoryRegion *mr = opaque; |
| 1315 | uint64_t data = (uint64_t)~0; |
| 1316 | |
| 1317 | switch (size) { |
| 1318 | case 1: |
| 1319 | data = *(uint8_t *)(mr->ram_block->host + addr); |
| 1320 | break; |
| 1321 | case 2: |
| 1322 | data = *(uint16_t *)(mr->ram_block->host + addr); |
| 1323 | break; |
| 1324 | case 4: |
| 1325 | data = *(uint32_t *)(mr->ram_block->host + addr); |
| 1326 | break; |
| 1327 | case 8: |
| 1328 | data = *(uint64_t *)(mr->ram_block->host + addr); |
| 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size); |
| 1333 | |
| 1334 | return data; |
| 1335 | } |
| 1336 | |
| 1337 | static void memory_region_ram_device_write(void *opaque, hwaddr addr, |
| 1338 | uint64_t data, unsigned size) |
| 1339 | { |
| 1340 | MemoryRegion *mr = opaque; |
| 1341 | |
| 1342 | trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size); |
| 1343 | |
| 1344 | switch (size) { |
| 1345 | case 1: |
| 1346 | *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data; |
| 1347 | break; |
| 1348 | case 2: |
| 1349 | *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data; |
| 1350 | break; |
| 1351 | case 4: |
| 1352 | *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data; |
| 1353 | break; |
| 1354 | case 8: |
| 1355 | *(uint64_t *)(mr->ram_block->host + addr) = data; |
| 1356 | break; |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | static const MemoryRegionOps ram_device_mem_ops = { |
| 1361 | .read = memory_region_ram_device_read, |
| 1362 | .write = memory_region_ram_device_write, |
Yongji Xie | c99a29e | 2017-02-27 12:52:44 +0800 | [diff] [blame] | 1363 | .endianness = DEVICE_HOST_ENDIAN, |
Alex Williamson | 4a2e242 | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1364 | .valid = { |
| 1365 | .min_access_size = 1, |
| 1366 | .max_access_size = 8, |
| 1367 | .unaligned = true, |
| 1368 | }, |
| 1369 | .impl = { |
| 1370 | .min_access_size = 1, |
| 1371 | .max_access_size = 8, |
| 1372 | .unaligned = true, |
| 1373 | }, |
| 1374 | }; |
| 1375 | |
Paolo Bonzini | d270203 | 2013-05-24 11:55:06 +0200 | [diff] [blame] | 1376 | bool memory_region_access_valid(MemoryRegion *mr, |
| 1377 | hwaddr addr, |
| 1378 | unsigned size, |
| 1379 | bool is_write) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1380 | { |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1381 | int access_size_min, access_size_max; |
| 1382 | int access_size, i; |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 1383 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1384 | if (!mr->ops->valid.unaligned && (addr & (size - 1))) { |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1388 | if (!mr->ops->valid.accepts) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1389 | return true; |
| 1390 | } |
| 1391 | |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1392 | access_size_min = mr->ops->valid.min_access_size; |
| 1393 | if (!mr->ops->valid.min_access_size) { |
| 1394 | access_size_min = 1; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1395 | } |
Paolo Bonzini | a014ed0 | 2013-05-24 17:48:52 +0200 | [diff] [blame] | 1396 | |
| 1397 | access_size_max = mr->ops->valid.max_access_size; |
| 1398 | if (!mr->ops->valid.max_access_size) { |
| 1399 | access_size_max = 4; |
| 1400 | } |
| 1401 | |
| 1402 | access_size = MAX(MIN(size, access_size_max), access_size_min); |
| 1403 | for (i = 0; i < size; i += access_size) { |
| 1404 | if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size, |
| 1405 | is_write)) { |
| 1406 | return false; |
| 1407 | } |
| 1408 | } |
| 1409 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1410 | return true; |
| 1411 | } |
| 1412 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1413 | static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr, |
| 1414 | hwaddr addr, |
| 1415 | uint64_t *pval, |
| 1416 | unsigned size, |
| 1417 | MemTxAttrs attrs) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1418 | { |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1419 | *pval = 0; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1420 | |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 1421 | if (mr->ops->read) { |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1422 | return access_with_adjusted_size(addr, pval, size, |
| 1423 | mr->ops->impl.min_access_size, |
| 1424 | mr->ops->impl.max_access_size, |
| 1425 | memory_region_read_accessor, |
| 1426 | mr, attrs); |
| 1427 | } else if (mr->ops->read_with_attrs) { |
| 1428 | return access_with_adjusted_size(addr, pval, size, |
| 1429 | mr->ops->impl.min_access_size, |
| 1430 | mr->ops->impl.max_access_size, |
| 1431 | memory_region_read_with_attrs_accessor, |
| 1432 | mr, attrs); |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 1433 | } else { |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1434 | return access_with_adjusted_size(addr, pval, size, 1, 4, |
| 1435 | memory_region_oldmmio_read_accessor, |
| 1436 | mr, attrs); |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 1437 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1438 | } |
| 1439 | |
Peter Maydell | 3b64349 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1440 | MemTxResult memory_region_dispatch_read(MemoryRegion *mr, |
| 1441 | hwaddr addr, |
| 1442 | uint64_t *pval, |
| 1443 | unsigned size, |
| 1444 | MemTxAttrs attrs) |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1445 | { |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1446 | MemTxResult r; |
| 1447 | |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1448 | if (!memory_region_access_valid(mr, addr, size, false)) { |
| 1449 | *pval = unassigned_mem_read(mr, addr, size); |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1450 | return MEMTX_DECODE_ERROR; |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1451 | } |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1452 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1453 | r = memory_region_dispatch_read1(mr, addr, pval, size, attrs); |
Paolo Bonzini | 791af8c | 2013-05-24 16:10:39 +0200 | [diff] [blame] | 1454 | adjust_endianness(mr, pval, size); |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1455 | return r; |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Pavel Fedin | 8c56c1a | 2015-11-20 12:37:16 +0300 | [diff] [blame] | 1458 | /* Return true if an eventfd was signalled */ |
| 1459 | static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr, |
| 1460 | hwaddr addr, |
| 1461 | uint64_t data, |
| 1462 | unsigned size, |
| 1463 | MemTxAttrs attrs) |
| 1464 | { |
| 1465 | MemoryRegionIoeventfd ioeventfd = { |
| 1466 | .addr = addrrange_make(int128_make64(addr), int128_make64(size)), |
| 1467 | .data = data, |
| 1468 | }; |
| 1469 | unsigned i; |
| 1470 | |
| 1471 | for (i = 0; i < mr->ioeventfd_nb; i++) { |
| 1472 | ioeventfd.match_data = mr->ioeventfds[i].match_data; |
| 1473 | ioeventfd.e = mr->ioeventfds[i].e; |
| 1474 | |
| 1475 | if (memory_region_ioeventfd_equal(ioeventfd, mr->ioeventfds[i])) { |
| 1476 | event_notifier_set(ioeventfd.e); |
| 1477 | return true; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
Peter Maydell | 3b64349 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1484 | MemTxResult memory_region_dispatch_write(MemoryRegion *mr, |
| 1485 | hwaddr addr, |
| 1486 | uint64_t data, |
| 1487 | unsigned size, |
| 1488 | MemTxAttrs attrs) |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1489 | { |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 1490 | if (!memory_region_access_valid(mr, addr, size, true)) { |
Paolo Bonzini | b018ddf | 2013-05-24 14:48:38 +0200 | [diff] [blame] | 1491 | unassigned_mem_write(mr, addr, data, size); |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1492 | return MEMTX_DECODE_ERROR; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1493 | } |
| 1494 | |
Avi Kivity | a621f38 | 2012-01-02 13:12:08 +0200 | [diff] [blame] | 1495 | adjust_endianness(mr, &data, size); |
| 1496 | |
Pavel Fedin | 8c56c1a | 2015-11-20 12:37:16 +0300 | [diff] [blame] | 1497 | if ((!kvm_eventfds_enabled()) && |
| 1498 | memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) { |
| 1499 | return MEMTX_OK; |
| 1500 | } |
| 1501 | |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 1502 | if (mr->ops->write) { |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1503 | return access_with_adjusted_size(addr, &data, size, |
| 1504 | mr->ops->impl.min_access_size, |
| 1505 | mr->ops->impl.max_access_size, |
| 1506 | memory_region_write_accessor, mr, |
| 1507 | attrs); |
| 1508 | } else if (mr->ops->write_with_attrs) { |
| 1509 | return |
| 1510 | access_with_adjusted_size(addr, &data, size, |
| 1511 | mr->ops->impl.min_access_size, |
| 1512 | mr->ops->impl.max_access_size, |
| 1513 | memory_region_write_with_attrs_accessor, |
| 1514 | mr, attrs); |
Paolo Bonzini | ce5d2f3 | 2013-05-24 17:45:48 +0200 | [diff] [blame] | 1515 | } else { |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1516 | return access_with_adjusted_size(addr, &data, size, 1, 4, |
| 1517 | memory_region_oldmmio_write_accessor, |
| 1518 | mr, attrs); |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 1519 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1520 | } |
| 1521 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1522 | void memory_region_init_io(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1523 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1524 | const MemoryRegionOps *ops, |
| 1525 | void *opaque, |
| 1526 | const char *name, |
| 1527 | uint64_t size) |
| 1528 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1529 | memory_region_init(mr, owner, name, size); |
Pavel Fedin | 6d6d2ab | 2015-08-13 11:26:21 +0100 | [diff] [blame] | 1530 | mr->ops = ops ? ops : &unassigned_mem_ops; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1531 | mr->opaque = opaque; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1532 | mr->terminates = true; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1533 | } |
| 1534 | |
Peter Maydell | 1cfe48c | 2017-07-07 15:42:49 +0100 | [diff] [blame] | 1535 | void memory_region_init_ram_nomigrate(MemoryRegion *mr, |
| 1536 | Object *owner, |
| 1537 | const char *name, |
| 1538 | uint64_t size, |
| 1539 | Error **errp) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1540 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1541 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 1542 | mr->ram = true; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1543 | mr->terminates = true; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1544 | mr->destructor = memory_region_destructor_ram; |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1545 | mr->ram_block = qemu_ram_alloc(size, mr, errp); |
Paolo Bonzini | 677e780 | 2015-03-23 10:53:21 +0100 | [diff] [blame] | 1546 | mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1547 | } |
| 1548 | |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 1549 | void memory_region_init_resizeable_ram(MemoryRegion *mr, |
| 1550 | Object *owner, |
| 1551 | const char *name, |
| 1552 | uint64_t size, |
| 1553 | uint64_t max_size, |
| 1554 | void (*resized)(const char*, |
| 1555 | uint64_t length, |
| 1556 | void *host), |
| 1557 | Error **errp) |
| 1558 | { |
| 1559 | memory_region_init(mr, owner, name, size); |
| 1560 | mr->ram = true; |
| 1561 | mr->terminates = true; |
| 1562 | mr->destructor = memory_region_destructor_ram; |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1563 | mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized, |
| 1564 | mr, errp); |
Paolo Bonzini | 677e780 | 2015-03-23 10:53:21 +0100 | [diff] [blame] | 1565 | mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1568 | #ifdef __linux__ |
| 1569 | void memory_region_init_ram_from_file(MemoryRegion *mr, |
| 1570 | struct Object *owner, |
| 1571 | const char *name, |
| 1572 | uint64_t size, |
Paolo Bonzini | dbcb898 | 2014-06-10 19:15:24 +0800 | [diff] [blame] | 1573 | bool share, |
Paolo Bonzini | 7f56e74 | 2014-05-14 17:43:20 +0800 | [diff] [blame] | 1574 | const char *path, |
| 1575 | Error **errp) |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1576 | { |
| 1577 | memory_region_init(mr, owner, name, size); |
| 1578 | mr->ram = true; |
| 1579 | mr->terminates = true; |
| 1580 | mr->destructor = memory_region_destructor_ram; |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1581 | mr->ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp); |
Paolo Bonzini | 677e780 | 2015-03-23 10:53:21 +0100 | [diff] [blame] | 1582 | mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1583 | } |
Marc-André Lureau | fea617c | 2017-06-02 18:12:24 +0400 | [diff] [blame] | 1584 | |
| 1585 | void memory_region_init_ram_from_fd(MemoryRegion *mr, |
| 1586 | struct Object *owner, |
| 1587 | const char *name, |
| 1588 | uint64_t size, |
| 1589 | bool share, |
| 1590 | int fd, |
| 1591 | Error **errp) |
| 1592 | { |
| 1593 | memory_region_init(mr, owner, name, size); |
| 1594 | mr->ram = true; |
| 1595 | mr->terminates = true; |
| 1596 | mr->destructor = memory_region_destructor_ram; |
| 1597 | mr->ram_block = qemu_ram_alloc_from_fd(size, mr, share, fd, errp); |
| 1598 | mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; |
| 1599 | } |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 1600 | #endif |
| 1601 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1602 | void memory_region_init_ram_ptr(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1603 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1604 | const char *name, |
| 1605 | uint64_t size, |
| 1606 | void *ptr) |
| 1607 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1608 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 1609 | mr->ram = true; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1610 | mr->terminates = true; |
Eduardo Habkost | fc3e766 | 2015-11-06 19:20:05 -0200 | [diff] [blame] | 1611 | mr->destructor = memory_region_destructor_ram; |
Paolo Bonzini | 677e780 | 2015-03-23 10:53:21 +0100 | [diff] [blame] | 1612 | mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; |
Hu Tao | ef701d7 | 2014-09-09 13:27:54 +0800 | [diff] [blame] | 1613 | |
| 1614 | /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */ |
| 1615 | assert(ptr != NULL); |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1616 | mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1617 | } |
| 1618 | |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1619 | void memory_region_init_ram_device_ptr(MemoryRegion *mr, |
| 1620 | Object *owner, |
| 1621 | const char *name, |
| 1622 | uint64_t size, |
| 1623 | void *ptr) |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 1624 | { |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1625 | memory_region_init_ram_ptr(mr, owner, name, size, ptr); |
| 1626 | mr->ram_device = true; |
Alex Williamson | 4a2e242 | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1627 | mr->ops = &ram_device_mem_ops; |
| 1628 | mr->opaque = mr; |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 1629 | } |
| 1630 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1631 | void memory_region_init_alias(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1632 | Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1633 | const char *name, |
| 1634 | MemoryRegion *orig, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1635 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1636 | uint64_t size) |
| 1637 | { |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1638 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1639 | mr->alias = orig; |
| 1640 | mr->alias_offset = offset; |
| 1641 | } |
| 1642 | |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 1643 | void memory_region_init_rom_nomigrate(MemoryRegion *mr, |
| 1644 | struct Object *owner, |
| 1645 | const char *name, |
| 1646 | uint64_t size, |
| 1647 | Error **errp) |
Peter Maydell | a1777f7 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 1648 | { |
| 1649 | memory_region_init(mr, owner, name, size); |
| 1650 | mr->ram = true; |
| 1651 | mr->readonly = true; |
| 1652 | mr->terminates = true; |
| 1653 | mr->destructor = memory_region_destructor_ram; |
| 1654 | mr->ram_block = qemu_ram_alloc(size, mr, errp); |
| 1655 | mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; |
| 1656 | } |
| 1657 | |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 1658 | void memory_region_init_rom_device_nomigrate(MemoryRegion *mr, |
| 1659 | Object *owner, |
| 1660 | const MemoryRegionOps *ops, |
| 1661 | void *opaque, |
| 1662 | const char *name, |
| 1663 | uint64_t size, |
| 1664 | Error **errp) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1665 | { |
Peter Maydell | 39e0b03 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 1666 | assert(ops); |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1667 | memory_region_init(mr, owner, name, size); |
Avi Kivity | 7bc2b9c | 2011-08-25 14:56:14 +0300 | [diff] [blame] | 1668 | mr->ops = ops; |
Avi Kivity | 75f5941 | 2011-08-26 00:35:15 +0300 | [diff] [blame] | 1669 | mr->opaque = opaque; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1670 | mr->terminates = true; |
Avi Kivity | 75c578d | 2012-01-02 15:40:52 +0200 | [diff] [blame] | 1671 | mr->rom_device = true; |
Paolo Bonzini | 58268c8 | 2016-09-14 11:05:59 +0200 | [diff] [blame] | 1672 | mr->destructor = memory_region_destructor_ram; |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1673 | mr->ram_block = qemu_ram_alloc(size, mr, errp); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1674 | } |
| 1675 | |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1676 | void memory_region_init_iommu(void *_iommu_mr, |
| 1677 | size_t instance_size, |
| 1678 | const char *mrtypename, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 1679 | Object *owner, |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1680 | const char *name, |
| 1681 | uint64_t size) |
| 1682 | { |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1683 | struct IOMMUMemoryRegion *iommu_mr; |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1684 | struct MemoryRegion *mr; |
| 1685 | |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1686 | object_initialize(_iommu_mr, instance_size, mrtypename); |
| 1687 | mr = MEMORY_REGION(_iommu_mr); |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1688 | memory_region_do_init(mr, owner, name, size); |
| 1689 | iommu_mr = IOMMU_MEMORY_REGION(mr); |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1690 | mr->terminates = true; /* then re-forwards */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1691 | QLIST_INIT(&iommu_mr->iommu_notify); |
| 1692 | iommu_mr->iommu_notify_flags = IOMMU_NOTIFIER_NONE; |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1695 | static void memory_region_finalize(Object *obj) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1696 | { |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 1697 | MemoryRegion *mr = MEMORY_REGION(obj); |
| 1698 | |
Paolo Bonzini | 2e2b8eb | 2015-10-01 10:59:50 +0200 | [diff] [blame] | 1699 | assert(!mr->container); |
| 1700 | |
| 1701 | /* We know the region is not visible in any address space (it |
| 1702 | * does not have a container and cannot be a root either because |
| 1703 | * it has no references, so we can blindly clear mr->enabled. |
| 1704 | * memory_region_set_enabled instead could trigger a transaction |
| 1705 | * and cause an infinite loop. |
| 1706 | */ |
| 1707 | mr->enabled = false; |
| 1708 | memory_region_transaction_begin(); |
| 1709 | while (!QTAILQ_EMPTY(&mr->subregions)) { |
| 1710 | MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions); |
| 1711 | memory_region_del_subregion(mr, subregion); |
| 1712 | } |
| 1713 | memory_region_transaction_commit(); |
| 1714 | |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1715 | mr->destructor(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1716 | memory_region_clear_coalescing(mr); |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 1717 | g_free((char *)mr->name); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1718 | g_free(mr->ioeventfds); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1719 | } |
| 1720 | |
Paolo Bonzini | 803c081 | 2013-05-07 06:59:09 +0200 | [diff] [blame] | 1721 | Object *memory_region_owner(MemoryRegion *mr) |
| 1722 | { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1723 | Object *obj = OBJECT(mr); |
| 1724 | return obj->parent; |
Paolo Bonzini | 803c081 | 2013-05-07 06:59:09 +0200 | [diff] [blame] | 1725 | } |
| 1726 | |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 1727 | void memory_region_ref(MemoryRegion *mr) |
| 1728 | { |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1729 | /* MMIO callbacks most likely will access data that belongs |
| 1730 | * to the owner, hence the need to ref/unref the owner whenever |
| 1731 | * the memory region is in use. |
| 1732 | * |
| 1733 | * The memory region is a child of its owner. As long as the |
| 1734 | * owner doesn't call unparent itself on the memory region, |
| 1735 | * ref-ing the owner will also keep the memory region alive. |
Paolo Bonzini | 612263c | 2015-12-09 11:44:25 +0100 | [diff] [blame] | 1736 | * Memory regions without an owner are supposed to never go away; |
| 1737 | * we do not ref/unref them because it slows down DMA sensibly. |
Paolo Bonzini | 22a893e | 2014-06-11 10:58:06 +0200 | [diff] [blame] | 1738 | */ |
Paolo Bonzini | 612263c | 2015-12-09 11:44:25 +0100 | [diff] [blame] | 1739 | if (mr && mr->owner) { |
| 1740 | object_ref(mr->owner); |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | void memory_region_unref(MemoryRegion *mr) |
| 1745 | { |
Paolo Bonzini | 612263c | 2015-12-09 11:44:25 +0100 | [diff] [blame] | 1746 | if (mr && mr->owner) { |
| 1747 | object_unref(mr->owner); |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1751 | uint64_t memory_region_size(MemoryRegion *mr) |
| 1752 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1753 | if (int128_eq(mr->size, int128_2_64())) { |
| 1754 | return UINT64_MAX; |
| 1755 | } |
| 1756 | return int128_get64(mr->size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1757 | } |
| 1758 | |
Peter Crosthwaite | 5d546d4 | 2014-08-14 23:55:03 -0700 | [diff] [blame] | 1759 | const char *memory_region_name(const MemoryRegion *mr) |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 1760 | { |
Peter Crosthwaite | d1dd32a | 2014-08-25 20:10:24 -0700 | [diff] [blame] | 1761 | if (!mr->name) { |
| 1762 | ((MemoryRegion *)mr)->name = |
| 1763 | object_get_canonical_path_component(OBJECT(mr)); |
| 1764 | } |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 1765 | return mr->name; |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1768 | bool memory_region_is_ram_device(MemoryRegion *mr) |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 1769 | { |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 1770 | return mr->ram_device; |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 1771 | } |
| 1772 | |
Paolo Bonzini | 2d1a35b | 2015-03-23 10:50:57 +0100 | [diff] [blame] | 1773 | uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr) |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 1774 | { |
Paolo Bonzini | 6f6a5ef | 2015-03-23 10:57:21 +0100 | [diff] [blame] | 1775 | uint8_t mask = mr->dirty_log_mask; |
Paolo Bonzini | adaad61 | 2016-09-22 16:09:08 +0200 | [diff] [blame] | 1776 | if (global_dirty_log && mr->ram_block) { |
Paolo Bonzini | 6f6a5ef | 2015-03-23 10:57:21 +0100 | [diff] [blame] | 1777 | mask |= (1 << DIRTY_MEMORY_MIGRATION); |
| 1778 | } |
| 1779 | return mask; |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 1780 | } |
| 1781 | |
Paolo Bonzini | 2d1a35b | 2015-03-23 10:50:57 +0100 | [diff] [blame] | 1782 | bool memory_region_is_logging(MemoryRegion *mr, uint8_t client) |
| 1783 | { |
| 1784 | return memory_region_get_dirty_log_mask(mr) & (1 << client); |
| 1785 | } |
| 1786 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1787 | static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion *iommu_mr) |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 1788 | { |
| 1789 | IOMMUNotifierFlag flags = IOMMU_NOTIFIER_NONE; |
| 1790 | IOMMUNotifier *iommu_notifier; |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1791 | IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr); |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 1792 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1793 | IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) { |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 1794 | flags |= iommu_notifier->notifier_flags; |
| 1795 | } |
| 1796 | |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1797 | if (flags != iommu_mr->iommu_notify_flags && imrc->notify_flag_changed) { |
| 1798 | imrc->notify_flag_changed(iommu_mr, |
| 1799 | iommu_mr->iommu_notify_flags, |
| 1800 | flags); |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 1801 | } |
| 1802 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1803 | iommu_mr->iommu_notify_flags = flags; |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1806 | void memory_region_register_iommu_notifier(MemoryRegion *mr, |
| 1807 | IOMMUNotifier *n) |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1808 | { |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1809 | IOMMUMemoryRegion *iommu_mr; |
| 1810 | |
Jason Wang | efcd38c | 2016-12-30 18:09:17 +0800 | [diff] [blame] | 1811 | if (mr->alias) { |
| 1812 | memory_region_register_iommu_notifier(mr->alias, n); |
| 1813 | return; |
| 1814 | } |
| 1815 | |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1816 | /* We need to register for at least one bitfield */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1817 | iommu_mr = IOMMU_MEMORY_REGION(mr); |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1818 | assert(n->notifier_flags != IOMMU_NOTIFIER_NONE); |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 1819 | assert(n->start <= n->end); |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1820 | QLIST_INSERT_HEAD(&iommu_mr->iommu_notify, n, node); |
| 1821 | memory_region_update_iommu_notify_flags(iommu_mr); |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1822 | } |
| 1823 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1824 | uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr) |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1825 | { |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1826 | IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr); |
| 1827 | |
| 1828 | if (imrc->get_min_page_size) { |
| 1829 | return imrc->get_min_page_size(iommu_mr); |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 1830 | } |
| 1831 | return TARGET_PAGE_SIZE; |
| 1832 | } |
| 1833 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1834 | void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 1835 | { |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1836 | MemoryRegion *mr = MEMORY_REGION(iommu_mr); |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1837 | IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr); |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 1838 | hwaddr addr, granularity; |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1839 | IOMMUTLBEntry iotlb; |
| 1840 | |
Peter Xu | faa362e | 2017-04-07 18:59:11 +0800 | [diff] [blame] | 1841 | /* If the IOMMU has its own replay callback, override */ |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1842 | if (imrc->replay) { |
| 1843 | imrc->replay(iommu_mr, n); |
Peter Xu | faa362e | 2017-04-07 18:59:11 +0800 | [diff] [blame] | 1844 | return; |
| 1845 | } |
| 1846 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1847 | granularity = memory_region_iommu_get_min_page_size(iommu_mr); |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 1848 | |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1849 | for (addr = 0; addr < memory_region_size(mr); addr += granularity) { |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 1850 | iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE); |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1851 | if (iotlb.perm != IOMMU_NONE) { |
| 1852 | n->notify(n, &iotlb); |
| 1853 | } |
| 1854 | |
| 1855 | /* if (2^64 - MR size) < granularity, it's possible to get an |
| 1856 | * infinite loop here. This should catch such a wraparound */ |
| 1857 | if ((addr + granularity) < addr) { |
| 1858 | break; |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1863 | void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr) |
Peter Xu | de472e4 | 2017-04-07 18:59:09 +0800 | [diff] [blame] | 1864 | { |
| 1865 | IOMMUNotifier *notifier; |
| 1866 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1867 | IOMMU_NOTIFIER_FOREACH(notifier, iommu_mr) { |
| 1868 | memory_region_iommu_replay(iommu_mr, notifier); |
Peter Xu | de472e4 | 2017-04-07 18:59:09 +0800 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1872 | void memory_region_unregister_iommu_notifier(MemoryRegion *mr, |
| 1873 | IOMMUNotifier *n) |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1874 | { |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1875 | IOMMUMemoryRegion *iommu_mr; |
| 1876 | |
Jason Wang | efcd38c | 2016-12-30 18:09:17 +0800 | [diff] [blame] | 1877 | if (mr->alias) { |
| 1878 | memory_region_unregister_iommu_notifier(mr->alias, n); |
| 1879 | return; |
| 1880 | } |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1881 | QLIST_REMOVE(n, node); |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1882 | iommu_mr = IOMMU_MEMORY_REGION(mr); |
| 1883 | memory_region_update_iommu_notify_flags(iommu_mr); |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1884 | } |
| 1885 | |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1886 | void memory_region_notify_one(IOMMUNotifier *notifier, |
| 1887 | IOMMUTLBEntry *entry) |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1888 | { |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1889 | IOMMUNotifierFlag request_flags; |
| 1890 | |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1891 | /* |
| 1892 | * Skip the notification if the notification does not overlap |
| 1893 | * with registered range. |
| 1894 | */ |
| 1895 | if (notifier->start > entry->iova + entry->addr_mask + 1 || |
| 1896 | notifier->end < entry->iova) { |
| 1897 | return; |
| 1898 | } |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1899 | |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1900 | if (entry->perm & IOMMU_RW) { |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1901 | request_flags = IOMMU_NOTIFIER_MAP; |
| 1902 | } else { |
| 1903 | request_flags = IOMMU_NOTIFIER_UNMAP; |
| 1904 | } |
| 1905 | |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1906 | if (notifier->notifier_flags & request_flags) { |
| 1907 | notifier->notify(notifier, entry); |
| 1908 | } |
| 1909 | } |
| 1910 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1911 | void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr, |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1912 | IOMMUTLBEntry entry) |
| 1913 | { |
| 1914 | IOMMUNotifier *iommu_notifier; |
| 1915 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1916 | assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr))); |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1917 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1918 | IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) { |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1919 | memory_region_notify_one(iommu_notifier, &entry); |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1920 | } |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1921 | } |
| 1922 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1923 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client) |
| 1924 | { |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1925 | uint8_t mask = 1 << client; |
Paolo Bonzini | deb809e | 2015-07-14 13:56:53 +0200 | [diff] [blame] | 1926 | uint8_t old_logging; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1927 | |
Paolo Bonzini | dbddac6 | 2015-03-23 10:31:53 +0100 | [diff] [blame] | 1928 | assert(client == DIRTY_MEMORY_VGA); |
Paolo Bonzini | deb809e | 2015-07-14 13:56:53 +0200 | [diff] [blame] | 1929 | old_logging = mr->vga_logging_count; |
| 1930 | mr->vga_logging_count += log ? 1 : -1; |
| 1931 | if (!!old_logging == !!mr->vga_logging_count) { |
| 1932 | return; |
| 1933 | } |
| 1934 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1935 | memory_region_transaction_begin(); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1936 | mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask); |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 1937 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 1938 | memory_region_transaction_commit(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1939 | } |
| 1940 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1941 | bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, |
| 1942 | hwaddr size, unsigned client) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1943 | { |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1944 | assert(mr->ram_block); |
| 1945 | return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr) + addr, |
| 1946 | size, client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1947 | } |
| 1948 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1949 | void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, |
| 1950 | hwaddr size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1951 | { |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1952 | assert(mr->ram_block); |
| 1953 | cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr, |
| 1954 | size, |
Paolo Bonzini | 58d2707 | 2015-03-23 11:56:01 +0100 | [diff] [blame] | 1955 | memory_region_get_dirty_log_mask(mr)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1956 | } |
| 1957 | |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1958 | bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, |
| 1959 | hwaddr size, unsigned client) |
| 1960 | { |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 1961 | assert(mr->ram_block); |
| 1962 | return cpu_physical_memory_test_and_clear_dirty( |
| 1963 | memory_region_get_ram_addr(mr) + addr, size, client); |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1964 | } |
| 1965 | |
Gerd Hoffmann | 8deaf12 | 2017-04-21 11:16:25 +0200 | [diff] [blame] | 1966 | DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr, |
| 1967 | hwaddr addr, |
| 1968 | hwaddr size, |
| 1969 | unsigned client) |
| 1970 | { |
| 1971 | assert(mr->ram_block); |
| 1972 | return cpu_physical_memory_snapshot_and_clear_dirty( |
| 1973 | memory_region_get_ram_addr(mr) + addr, size, client); |
| 1974 | } |
| 1975 | |
| 1976 | bool memory_region_snapshot_get_dirty(MemoryRegion *mr, DirtyBitmapSnapshot *snap, |
| 1977 | hwaddr addr, hwaddr size) |
| 1978 | { |
| 1979 | assert(mr->ram_block); |
| 1980 | return cpu_physical_memory_snapshot_get_dirty(snap, |
| 1981 | memory_region_get_ram_addr(mr) + addr, size); |
| 1982 | } |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1983 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1984 | void memory_region_sync_dirty_bitmap(MemoryRegion *mr) |
| 1985 | { |
Paolo Bonzini | 0a752ee | 2016-09-23 11:08:54 +0200 | [diff] [blame] | 1986 | MemoryListener *listener; |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 1987 | AddressSpace *as; |
Paolo Bonzini | 0a752ee | 2016-09-23 11:08:54 +0200 | [diff] [blame] | 1988 | FlatView *view; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1989 | FlatRange *fr; |
| 1990 | |
Paolo Bonzini | 0a752ee | 2016-09-23 11:08:54 +0200 | [diff] [blame] | 1991 | /* If the same address space has multiple log_sync listeners, we |
| 1992 | * visit that address space's FlatView multiple times. But because |
| 1993 | * log_sync listeners are rare, it's still cheaper than walking each |
| 1994 | * address space once. |
| 1995 | */ |
| 1996 | QTAILQ_FOREACH(listener, &memory_listeners, link) { |
| 1997 | if (!listener->log_sync) { |
| 1998 | continue; |
| 1999 | } |
| 2000 | as = listener->address_space; |
| 2001 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2002 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2003 | if (fr->mr == mr) { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2004 | MemoryRegionSection mrs = section_from_flat_range(fr, view); |
Paolo Bonzini | 0a752ee | 2016-09-23 11:08:54 +0200 | [diff] [blame] | 2005 | listener->log_sync(listener, &mrs); |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2006 | } |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 2007 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 2008 | flatview_unref(view); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 2009 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly) |
| 2013 | { |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 2014 | if (mr->readonly != readonly) { |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2015 | memory_region_transaction_begin(); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 2016 | mr->readonly = readonly; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 2017 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2018 | memory_region_transaction_commit(); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 2019 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2020 | } |
| 2021 | |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 2022 | void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 2023 | { |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 2024 | if (mr->romd_mode != romd_mode) { |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2025 | memory_region_transaction_begin(); |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 2026 | mr->romd_mode = romd_mode; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 2027 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2028 | memory_region_transaction_commit(); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 2029 | } |
| 2030 | } |
| 2031 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2032 | void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, |
| 2033 | hwaddr size, unsigned client) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2034 | { |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 2035 | assert(mr->ram_block); |
| 2036 | cpu_physical_memory_test_and_clear_dirty( |
| 2037 | memory_region_get_ram_addr(mr) + addr, size, client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2038 | } |
| 2039 | |
Paolo Bonzini | a35ba7b | 2014-06-10 19:15:23 +0800 | [diff] [blame] | 2040 | int memory_region_get_fd(MemoryRegion *mr) |
| 2041 | { |
Paolo Bonzini | 4ff8757 | 2016-03-25 12:30:16 +0100 | [diff] [blame] | 2042 | int fd; |
| 2043 | |
| 2044 | rcu_read_lock(); |
| 2045 | while (mr->alias) { |
| 2046 | mr = mr->alias; |
Paolo Bonzini | a35ba7b | 2014-06-10 19:15:23 +0800 | [diff] [blame] | 2047 | } |
Paolo Bonzini | 4ff8757 | 2016-03-25 12:30:16 +0100 | [diff] [blame] | 2048 | fd = mr->ram_block->fd; |
| 2049 | rcu_read_unlock(); |
Paolo Bonzini | a35ba7b | 2014-06-10 19:15:23 +0800 | [diff] [blame] | 2050 | |
Paolo Bonzini | 4ff8757 | 2016-03-25 12:30:16 +0100 | [diff] [blame] | 2051 | return fd; |
| 2052 | } |
Paolo Bonzini | a35ba7b | 2014-06-10 19:15:23 +0800 | [diff] [blame] | 2053 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2054 | void *memory_region_get_ram_ptr(MemoryRegion *mr) |
| 2055 | { |
Paolo Bonzini | 49b24af | 2015-12-16 10:30:47 +0100 | [diff] [blame] | 2056 | void *ptr; |
| 2057 | uint64_t offset = 0; |
| 2058 | |
| 2059 | rcu_read_lock(); |
| 2060 | while (mr->alias) { |
| 2061 | offset += mr->alias_offset; |
| 2062 | mr = mr->alias; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2063 | } |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 2064 | assert(mr->ram_block); |
Paolo Bonzini | 0878d0e | 2016-02-22 11:02:12 +0100 | [diff] [blame] | 2065 | ptr = qemu_map_ram_ptr(mr->ram_block, offset); |
Paolo Bonzini | 49b24af | 2015-12-16 10:30:47 +0100 | [diff] [blame] | 2066 | rcu_read_unlock(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2067 | |
Paolo Bonzini | 0878d0e | 2016-02-22 11:02:12 +0100 | [diff] [blame] | 2068 | return ptr; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2069 | } |
| 2070 | |
Paolo Bonzini | 07bdaa4 | 2016-03-25 12:55:08 +0100 | [diff] [blame] | 2071 | MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset) |
| 2072 | { |
| 2073 | RAMBlock *block; |
| 2074 | |
| 2075 | block = qemu_ram_block_from_host(ptr, false, offset); |
| 2076 | if (!block) { |
| 2077 | return NULL; |
| 2078 | } |
| 2079 | |
| 2080 | return block->mr; |
| 2081 | } |
| 2082 | |
Fam Zheng | 7ebb274 | 2016-03-01 14:18:20 +0800 | [diff] [blame] | 2083 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr) |
| 2084 | { |
| 2085 | return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID; |
| 2086 | } |
| 2087 | |
Paolo Bonzini | 37d7c08 | 2015-03-23 10:21:46 +0100 | [diff] [blame] | 2088 | void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp) |
| 2089 | { |
Fam Zheng | 8e41fb6 | 2016-03-01 14:18:21 +0800 | [diff] [blame] | 2090 | assert(mr->ram_block); |
Paolo Bonzini | 37d7c08 | 2015-03-23 10:21:46 +0100 | [diff] [blame] | 2091 | |
Gonglei | fa53a0e | 2016-05-10 10:04:59 +0800 | [diff] [blame] | 2092 | qemu_ram_resize(mr->ram_block, newsize, errp); |
Paolo Bonzini | 37d7c08 | 2015-03-23 10:21:46 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2095 | static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2096 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2097 | FlatView *view; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2098 | FlatRange *fr; |
| 2099 | CoalescedMemoryRange *cmr; |
| 2100 | AddrRange tmp; |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 2101 | MemoryRegionSection section; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2102 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 2103 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2104 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2105 | if (fr->mr == mr) { |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 2106 | section = (MemoryRegionSection) { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2107 | .fv = view, |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 2108 | .offset_within_address_space = int128_get64(fr->addr.start), |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 2109 | .size = fr->addr.size, |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 2110 | }; |
| 2111 | |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 2112 | MEMORY_LISTENER_CALL(as, coalesced_mmio_del, Reverse, §ion, |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 2113 | int128_get64(fr->addr.start), |
| 2114 | int128_get64(fr->addr.size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2115 | QTAILQ_FOREACH(cmr, &mr->coalesced, link) { |
| 2116 | tmp = addrrange_shift(cmr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2117 | int128_sub(fr->addr.start, |
| 2118 | int128_make64(fr->offset_in_region))); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2119 | if (!addrrange_intersects(tmp, fr->addr)) { |
| 2120 | continue; |
| 2121 | } |
| 2122 | tmp = addrrange_intersection(tmp, fr->addr); |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 2123 | MEMORY_LISTENER_CALL(as, coalesced_mmio_add, Forward, §ion, |
Avi Kivity | 95d2994 | 2012-10-02 18:21:54 +0200 | [diff] [blame] | 2124 | int128_get64(tmp.start), |
| 2125 | int128_get64(tmp.size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 2129 | flatview_unref(view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2130 | } |
| 2131 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2132 | static void memory_region_update_coalesced_range(MemoryRegion *mr) |
| 2133 | { |
| 2134 | AddressSpace *as; |
| 2135 | |
| 2136 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
| 2137 | memory_region_update_coalesced_range_as(mr, as); |
| 2138 | } |
| 2139 | } |
| 2140 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2141 | void memory_region_set_coalescing(MemoryRegion *mr) |
| 2142 | { |
| 2143 | memory_region_clear_coalescing(mr); |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2144 | memory_region_add_coalescing(mr, 0, int128_get64(mr->size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | void memory_region_add_coalescing(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2148 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2149 | uint64_t size) |
| 2150 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2151 | CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2152 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2153 | cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2154 | QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link); |
| 2155 | memory_region_update_coalesced_range(mr); |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 2156 | memory_region_set_flush_coalesced(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | void memory_region_clear_coalescing(MemoryRegion *mr) |
| 2160 | { |
| 2161 | CoalescedMemoryRange *cmr; |
Fam Zheng | ab5b3db | 2014-06-13 14:34:41 +0800 | [diff] [blame] | 2162 | bool updated = false; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2163 | |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 2164 | qemu_flush_coalesced_mmio_buffer(); |
| 2165 | mr->flush_coalesced_mmio = false; |
| 2166 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2167 | while (!QTAILQ_EMPTY(&mr->coalesced)) { |
| 2168 | cmr = QTAILQ_FIRST(&mr->coalesced); |
| 2169 | QTAILQ_REMOVE(&mr->coalesced, cmr, link); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2170 | g_free(cmr); |
Fam Zheng | ab5b3db | 2014-06-13 14:34:41 +0800 | [diff] [blame] | 2171 | updated = true; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2172 | } |
Fam Zheng | ab5b3db | 2014-06-13 14:34:41 +0800 | [diff] [blame] | 2173 | |
| 2174 | if (updated) { |
| 2175 | memory_region_update_coalesced_range(mr); |
| 2176 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2177 | } |
| 2178 | |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 2179 | void memory_region_set_flush_coalesced(MemoryRegion *mr) |
| 2180 | { |
| 2181 | mr->flush_coalesced_mmio = true; |
| 2182 | } |
| 2183 | |
| 2184 | void memory_region_clear_flush_coalesced(MemoryRegion *mr) |
| 2185 | { |
| 2186 | qemu_flush_coalesced_mmio_buffer(); |
| 2187 | if (QTAILQ_EMPTY(&mr->coalesced)) { |
| 2188 | mr->flush_coalesced_mmio = false; |
| 2189 | } |
| 2190 | } |
| 2191 | |
Jan Kiszka | 196ea13 | 2015-06-18 18:47:20 +0200 | [diff] [blame] | 2192 | void memory_region_set_global_locking(MemoryRegion *mr) |
| 2193 | { |
| 2194 | mr->global_locking = true; |
| 2195 | } |
| 2196 | |
| 2197 | void memory_region_clear_global_locking(MemoryRegion *mr) |
| 2198 | { |
| 2199 | mr->global_locking = false; |
| 2200 | } |
| 2201 | |
Pavel Fedin | 8c56c1a | 2015-11-20 12:37:16 +0300 | [diff] [blame] | 2202 | static bool userspace_eventfd_warning; |
| 2203 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2204 | void memory_region_add_eventfd(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2205 | hwaddr addr, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2206 | unsigned size, |
| 2207 | bool match_data, |
| 2208 | uint64_t data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 2209 | EventNotifier *e) |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2210 | { |
| 2211 | MemoryRegionIoeventfd mrfd = { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2212 | .addr.start = int128_make64(addr), |
| 2213 | .addr.size = int128_make64(size), |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2214 | .match_data = match_data, |
| 2215 | .data = data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 2216 | .e = e, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2217 | }; |
| 2218 | unsigned i; |
| 2219 | |
Pavel Fedin | 8c56c1a | 2015-11-20 12:37:16 +0300 | [diff] [blame] | 2220 | if (kvm_enabled() && (!(kvm_eventfds_enabled() || |
| 2221 | userspace_eventfd_warning))) { |
| 2222 | userspace_eventfd_warning = true; |
| 2223 | error_report("Using eventfd without MMIO binding in KVM. " |
| 2224 | "Suboptimal performance expected"); |
| 2225 | } |
| 2226 | |
Jason Wang | b8aecea | 2015-11-06 16:02:45 +0800 | [diff] [blame] | 2227 | if (size) { |
| 2228 | adjust_endianness(mr, &mrfd.data, size); |
| 2229 | } |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2230 | memory_region_transaction_begin(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2231 | for (i = 0; i < mr->ioeventfd_nb; ++i) { |
| 2232 | if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) { |
| 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | ++mr->ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2237 | mr->ioeventfds = g_realloc(mr->ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2238 | sizeof(*mr->ioeventfds) * mr->ioeventfd_nb); |
| 2239 | memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i], |
| 2240 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i)); |
| 2241 | mr->ioeventfds[i] = mrfd; |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 2242 | ioeventfd_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2243 | memory_region_transaction_commit(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | void memory_region_del_eventfd(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2247 | hwaddr addr, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2248 | unsigned size, |
| 2249 | bool match_data, |
| 2250 | uint64_t data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 2251 | EventNotifier *e) |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2252 | { |
| 2253 | MemoryRegionIoeventfd mrfd = { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 2254 | .addr.start = int128_make64(addr), |
| 2255 | .addr.size = int128_make64(size), |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2256 | .match_data = match_data, |
| 2257 | .data = data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 2258 | .e = e, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2259 | }; |
| 2260 | unsigned i; |
| 2261 | |
Jason Wang | b8aecea | 2015-11-06 16:02:45 +0800 | [diff] [blame] | 2262 | if (size) { |
| 2263 | adjust_endianness(mr, &mrfd.data, size); |
| 2264 | } |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2265 | memory_region_transaction_begin(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2266 | for (i = 0; i < mr->ioeventfd_nb; ++i) { |
| 2267 | if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) { |
| 2268 | break; |
| 2269 | } |
| 2270 | } |
| 2271 | assert(i != mr->ioeventfd_nb); |
| 2272 | memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1], |
| 2273 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1))); |
| 2274 | --mr->ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2275 | mr->ioeventfds = g_realloc(mr->ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2276 | sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1); |
Gonglei | 4dc5615 | 2014-05-08 11:47:32 +0800 | [diff] [blame] | 2277 | ioeventfd_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2278 | memory_region_transaction_commit(); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 2279 | } |
| 2280 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2281 | static void memory_region_update_container_subregions(MemoryRegion *subregion) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2282 | { |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2283 | MemoryRegion *mr = subregion->container; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2284 | MemoryRegion *other; |
| 2285 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2286 | memory_region_transaction_begin(); |
| 2287 | |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 2288 | memory_region_ref(subregion); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2289 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2290 | if (subregion->priority >= other->priority) { |
| 2291 | QTAILQ_INSERT_BEFORE(other, subregion, subregions_link); |
| 2292 | goto done; |
| 2293 | } |
| 2294 | } |
| 2295 | QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link); |
| 2296 | done: |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 2297 | memory_region_update_pending |= mr->enabled && subregion->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2298 | memory_region_transaction_commit(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2299 | } |
| 2300 | |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 2301 | static void memory_region_add_subregion_common(MemoryRegion *mr, |
| 2302 | hwaddr offset, |
| 2303 | MemoryRegion *subregion) |
| 2304 | { |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2305 | assert(!subregion->container); |
| 2306 | subregion->container = mr; |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 2307 | subregion->addr = offset; |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2308 | memory_region_update_container_subregions(subregion); |
Peter Crosthwaite | 0598701 | 2014-06-05 23:14:44 -0700 | [diff] [blame] | 2309 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2310 | |
| 2311 | void memory_region_add_subregion(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2312 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2313 | MemoryRegion *subregion) |
| 2314 | { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2315 | subregion->priority = 0; |
| 2316 | memory_region_add_subregion_common(mr, offset, subregion); |
| 2317 | } |
| 2318 | |
| 2319 | void memory_region_add_subregion_overlap(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2320 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2321 | MemoryRegion *subregion, |
Marcel Apfelbaum | a1ff8ae | 2013-09-16 11:21:14 +0300 | [diff] [blame] | 2322 | int priority) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2323 | { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2324 | subregion->priority = priority; |
| 2325 | memory_region_add_subregion_common(mr, offset, subregion); |
| 2326 | } |
| 2327 | |
| 2328 | void memory_region_del_subregion(MemoryRegion *mr, |
| 2329 | MemoryRegion *subregion) |
| 2330 | { |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2331 | memory_region_transaction_begin(); |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2332 | assert(subregion->container == mr); |
| 2333 | subregion->container = NULL; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2334 | QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link); |
Paolo Bonzini | dfde4e6 | 2013-05-06 10:46:11 +0200 | [diff] [blame] | 2335 | memory_region_unref(subregion); |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 2336 | memory_region_update_pending |= mr->enabled && subregion->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2337 | memory_region_transaction_commit(); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled) |
| 2341 | { |
| 2342 | if (enabled == mr->enabled) { |
| 2343 | return; |
| 2344 | } |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2345 | memory_region_transaction_begin(); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 2346 | mr->enabled = enabled; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 2347 | memory_region_update_pending = true; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2348 | memory_region_transaction_commit(); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2349 | } |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 2350 | |
Michael S. Tsirkin | e7af4c6 | 2014-12-16 11:21:23 +0200 | [diff] [blame] | 2351 | void memory_region_set_size(MemoryRegion *mr, uint64_t size) |
| 2352 | { |
| 2353 | Int128 s = int128_make64(size); |
| 2354 | |
| 2355 | if (size == UINT64_MAX) { |
| 2356 | s = int128_2_64(); |
| 2357 | } |
| 2358 | if (int128_eq(s, mr->size)) { |
| 2359 | return; |
| 2360 | } |
| 2361 | memory_region_transaction_begin(); |
| 2362 | mr->size = s; |
| 2363 | memory_region_update_pending = true; |
| 2364 | memory_region_transaction_commit(); |
| 2365 | } |
| 2366 | |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 2367 | static void memory_region_readd_subregion(MemoryRegion *mr) |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 2368 | { |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2369 | MemoryRegion *container = mr->container; |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 2370 | |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2371 | if (container) { |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 2372 | memory_region_transaction_begin(); |
| 2373 | memory_region_ref(mr); |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2374 | memory_region_del_subregion(container, mr); |
| 2375 | mr->container = container; |
| 2376 | memory_region_update_container_subregions(mr); |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 2377 | memory_region_unref(mr); |
| 2378 | memory_region_transaction_commit(); |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 2379 | } |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 2380 | } |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 2381 | |
Peter Crosthwaite | 67891b8 | 2014-06-05 23:15:18 -0700 | [diff] [blame] | 2382 | void memory_region_set_address(MemoryRegion *mr, hwaddr addr) |
| 2383 | { |
| 2384 | if (addr != mr->addr) { |
| 2385 | mr->addr = addr; |
| 2386 | memory_region_readd_subregion(mr); |
| 2387 | } |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 2388 | } |
| 2389 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2390 | void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset) |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 2391 | { |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 2392 | assert(mr->alias); |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 2393 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2394 | if (offset == mr->alias_offset) { |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 2395 | return; |
| 2396 | } |
| 2397 | |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2398 | memory_region_transaction_begin(); |
| 2399 | mr->alias_offset = offset; |
Jan Kiszka | 22bde71 | 2012-11-05 16:45:56 +0100 | [diff] [blame] | 2400 | memory_region_update_pending |= mr->enabled; |
Jan Kiszka | 59023ef | 2012-08-23 13:02:30 +0200 | [diff] [blame] | 2401 | memory_region_transaction_commit(); |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
Igor Mammedov | a2b257d | 2014-10-31 16:38:37 +0000 | [diff] [blame] | 2404 | uint64_t memory_region_get_alignment(const MemoryRegion *mr) |
| 2405 | { |
| 2406 | return mr->align; |
| 2407 | } |
| 2408 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2409 | static int cmp_flatrange_addr(const void *addr_, const void *fr_) |
| 2410 | { |
| 2411 | const AddrRange *addr = addr_; |
| 2412 | const FlatRange *fr = fr_; |
| 2413 | |
| 2414 | if (int128_le(addrrange_end(*addr), fr->addr.start)) { |
| 2415 | return -1; |
| 2416 | } else if (int128_ge(addr->start, addrrange_end(fr->addr))) { |
| 2417 | return 1; |
| 2418 | } |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2422 | static FlatRange *flatview_lookup(FlatView *view, AddrRange addr) |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2423 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2424 | return bsearch(&addr, view->ranges, view->nr, |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2425 | sizeof(FlatRange), cmp_flatrange_addr); |
| 2426 | } |
| 2427 | |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 2428 | bool memory_region_is_mapped(MemoryRegion *mr) |
| 2429 | { |
| 2430 | return mr->container ? true : false; |
| 2431 | } |
| 2432 | |
Paolo Bonzini | c6742b1 | 2015-07-14 13:45:34 +0200 | [diff] [blame] | 2433 | /* Same as memory_region_find, but it does not add a reference to the |
| 2434 | * returned region. It must be called from an RCU critical section. |
| 2435 | */ |
| 2436 | static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr, |
| 2437 | hwaddr addr, uint64_t size) |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2438 | { |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 2439 | MemoryRegionSection ret = { .mr = NULL }; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 2440 | MemoryRegion *root; |
| 2441 | AddressSpace *as; |
| 2442 | AddrRange range; |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2443 | FlatView *view; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 2444 | FlatRange *fr; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2445 | |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 2446 | addr += mr->addr; |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 2447 | for (root = mr; root->container; ) { |
| 2448 | root = root->container; |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 2449 | addr += root->addr; |
| 2450 | } |
| 2451 | |
| 2452 | as = memory_region_to_address_space(root); |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 2453 | if (!as) { |
| 2454 | return ret; |
| 2455 | } |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 2456 | range = addrrange_make(int128_make64(addr), int128_make64(size)); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2457 | |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2458 | view = address_space_to_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2459 | fr = flatview_lookup(view, range); |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2460 | if (!fr) { |
Paolo Bonzini | c6742b1 | 2015-07-14 13:45:34 +0200 | [diff] [blame] | 2461 | return ret; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2462 | } |
| 2463 | |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2464 | while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) { |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2465 | --fr; |
| 2466 | } |
| 2467 | |
| 2468 | ret.mr = fr->mr; |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2469 | ret.fv = view; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2470 | range = addrrange_intersection(range, fr->addr); |
| 2471 | ret.offset_within_region = fr->offset_in_region; |
| 2472 | ret.offset_within_region += int128_get64(int128_sub(range.start, |
| 2473 | fr->addr.start)); |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 2474 | ret.size = range.size; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2475 | ret.offset_within_address_space = int128_get64(range.start); |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 2476 | ret.readonly = fr->readonly; |
Paolo Bonzini | c6742b1 | 2015-07-14 13:45:34 +0200 | [diff] [blame] | 2477 | return ret; |
| 2478 | } |
| 2479 | |
| 2480 | MemoryRegionSection memory_region_find(MemoryRegion *mr, |
| 2481 | hwaddr addr, uint64_t size) |
| 2482 | { |
| 2483 | MemoryRegionSection ret; |
| 2484 | rcu_read_lock(); |
| 2485 | ret = memory_region_find_rcu(mr, addr, size); |
| 2486 | if (ret.mr) { |
| 2487 | memory_region_ref(ret.mr); |
| 2488 | } |
Paolo Bonzini | 2b64766 | 2013-05-17 12:40:44 +0200 | [diff] [blame] | 2489 | rcu_read_unlock(); |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2490 | return ret; |
| 2491 | } |
| 2492 | |
Paolo Bonzini | c6742b1 | 2015-07-14 13:45:34 +0200 | [diff] [blame] | 2493 | bool memory_region_present(MemoryRegion *container, hwaddr addr) |
| 2494 | { |
| 2495 | MemoryRegion *mr; |
| 2496 | |
| 2497 | rcu_read_lock(); |
| 2498 | mr = memory_region_find_rcu(container, addr, 1).mr; |
| 2499 | rcu_read_unlock(); |
| 2500 | return mr && mr != container; |
| 2501 | } |
| 2502 | |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 2503 | void memory_global_dirty_log_sync(void) |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 2504 | { |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 2505 | MemoryListener *listener; |
| 2506 | AddressSpace *as; |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2507 | FlatView *view; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2508 | FlatRange *fr; |
| 2509 | |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 2510 | QTAILQ_FOREACH(listener, &memory_listeners, link) { |
| 2511 | if (!listener->log_sync) { |
| 2512 | continue; |
| 2513 | } |
Paolo Bonzini | d45fa78 | 2016-09-22 16:11:54 +0200 | [diff] [blame] | 2514 | as = listener->address_space; |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 2515 | view = address_space_get_flatview(as); |
| 2516 | FOR_EACH_FLAT_RANGE(fr, view) { |
Paolo Bonzini | adaad61 | 2016-09-22 16:09:08 +0200 | [diff] [blame] | 2517 | if (fr->dirty_log_mask) { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2518 | MemoryRegionSection mrs = section_from_flat_range(fr, view); |
| 2519 | |
Paolo Bonzini | adaad61 | 2016-09-22 16:09:08 +0200 | [diff] [blame] | 2520 | listener->log_sync(listener, &mrs); |
| 2521 | } |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 2522 | } |
| 2523 | flatview_unref(view); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
Jay Zhou | 1931076 | 2017-07-28 18:28:53 +0800 | [diff] [blame] | 2527 | static VMChangeStateEntry *vmstate_change; |
| 2528 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2529 | void memory_global_dirty_log_start(void) |
| 2530 | { |
Jay Zhou | 1931076 | 2017-07-28 18:28:53 +0800 | [diff] [blame] | 2531 | if (vmstate_change) { |
| 2532 | qemu_del_vm_change_state_handler(vmstate_change); |
| 2533 | vmstate_change = NULL; |
| 2534 | } |
| 2535 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2536 | global_dirty_log = true; |
Paolo Bonzini | 6f6a5ef | 2015-03-23 10:57:21 +0100 | [diff] [blame] | 2537 | |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 2538 | MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward); |
Paolo Bonzini | 6f6a5ef | 2015-03-23 10:57:21 +0100 | [diff] [blame] | 2539 | |
| 2540 | /* Refresh DIRTY_LOG_MIGRATION bit. */ |
| 2541 | memory_region_transaction_begin(); |
| 2542 | memory_region_update_pending = true; |
| 2543 | memory_region_transaction_commit(); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2544 | } |
| 2545 | |
Jay Zhou | 1931076 | 2017-07-28 18:28:53 +0800 | [diff] [blame] | 2546 | static void memory_global_dirty_log_do_stop(void) |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2547 | { |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2548 | global_dirty_log = false; |
Paolo Bonzini | 6f6a5ef | 2015-03-23 10:57:21 +0100 | [diff] [blame] | 2549 | |
| 2550 | /* Refresh DIRTY_LOG_MIGRATION bit. */ |
| 2551 | memory_region_transaction_begin(); |
| 2552 | memory_region_update_pending = true; |
| 2553 | memory_region_transaction_commit(); |
| 2554 | |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 2555 | MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2556 | } |
| 2557 | |
Jay Zhou | 1931076 | 2017-07-28 18:28:53 +0800 | [diff] [blame] | 2558 | static void memory_vm_change_state_handler(void *opaque, int running, |
| 2559 | RunState state) |
| 2560 | { |
| 2561 | if (running) { |
| 2562 | memory_global_dirty_log_do_stop(); |
| 2563 | |
| 2564 | if (vmstate_change) { |
| 2565 | qemu_del_vm_change_state_handler(vmstate_change); |
| 2566 | vmstate_change = NULL; |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | void memory_global_dirty_log_stop(void) |
| 2572 | { |
| 2573 | if (!runstate_is_running()) { |
| 2574 | if (vmstate_change) { |
| 2575 | return; |
| 2576 | } |
| 2577 | vmstate_change = qemu_add_vm_change_state_handler( |
| 2578 | memory_vm_change_state_handler, NULL); |
| 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | memory_global_dirty_log_do_stop(); |
| 2583 | } |
| 2584 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2585 | static void listener_add_address_space(MemoryListener *listener, |
| 2586 | AddressSpace *as) |
| 2587 | { |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2588 | FlatView *view; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2589 | FlatRange *fr; |
| 2590 | |
Paolo Bonzini | 680a478 | 2015-11-02 09:23:52 +0100 | [diff] [blame] | 2591 | if (listener->begin) { |
| 2592 | listener->begin(listener); |
| 2593 | } |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2594 | if (global_dirty_log) { |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 2595 | if (listener->log_global_start) { |
| 2596 | listener->log_global_start(listener); |
| 2597 | } |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2598 | } |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 2599 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 2600 | view = address_space_get_flatview(as); |
Paolo Bonzini | 99e8634 | 2013-05-06 10:26:13 +0200 | [diff] [blame] | 2601 | FOR_EACH_FLAT_RANGE(fr, view) { |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2602 | MemoryRegionSection section = { |
| 2603 | .mr = fr->mr, |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2604 | .fv = view, |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2605 | .offset_within_region = fr->offset_in_region, |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 2606 | .size = fr->addr.size, |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2607 | .offset_within_address_space = int128_get64(fr->addr.start), |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 2608 | .readonly = fr->readonly, |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2609 | }; |
Paolo Bonzini | 680a478 | 2015-11-02 09:23:52 +0100 | [diff] [blame] | 2610 | if (fr->dirty_log_mask && listener->log_start) { |
| 2611 | listener->log_start(listener, §ion, 0, fr->dirty_log_mask); |
| 2612 | } |
Avi Kivity | 975aefe | 2012-10-02 16:39:57 +0200 | [diff] [blame] | 2613 | if (listener->region_add) { |
| 2614 | listener->region_add(listener, §ion); |
| 2615 | } |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2616 | } |
Paolo Bonzini | 680a478 | 2015-11-02 09:23:52 +0100 | [diff] [blame] | 2617 | if (listener->commit) { |
| 2618 | listener->commit(listener); |
| 2619 | } |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 2620 | flatview_unref(view); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
Paolo Bonzini | d45fa78 | 2016-09-22 16:11:54 +0200 | [diff] [blame] | 2623 | void memory_listener_register(MemoryListener *listener, AddressSpace *as) |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2624 | { |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 2625 | MemoryListener *other = NULL; |
| 2626 | |
Paolo Bonzini | d45fa78 | 2016-09-22 16:11:54 +0200 | [diff] [blame] | 2627 | listener->address_space = as; |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 2628 | if (QTAILQ_EMPTY(&memory_listeners) |
| 2629 | || listener->priority >= QTAILQ_LAST(&memory_listeners, |
| 2630 | memory_listeners)->priority) { |
| 2631 | QTAILQ_INSERT_TAIL(&memory_listeners, listener, link); |
| 2632 | } else { |
| 2633 | QTAILQ_FOREACH(other, &memory_listeners, link) { |
| 2634 | if (listener->priority < other->priority) { |
| 2635 | break; |
| 2636 | } |
| 2637 | } |
| 2638 | QTAILQ_INSERT_BEFORE(other, listener, link); |
| 2639 | } |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2640 | |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 2641 | if (QTAILQ_EMPTY(&as->listeners) |
| 2642 | || listener->priority >= QTAILQ_LAST(&as->listeners, |
| 2643 | memory_listeners)->priority) { |
| 2644 | QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as); |
| 2645 | } else { |
| 2646 | QTAILQ_FOREACH(other, &as->listeners, link_as) { |
| 2647 | if (listener->priority < other->priority) { |
| 2648 | break; |
| 2649 | } |
| 2650 | } |
| 2651 | QTAILQ_INSERT_BEFORE(other, listener, link_as); |
| 2652 | } |
| 2653 | |
Paolo Bonzini | d45fa78 | 2016-09-22 16:11:54 +0200 | [diff] [blame] | 2654 | listener_add_address_space(listener, as); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | void memory_listener_unregister(MemoryListener *listener) |
| 2658 | { |
Paolo Bonzini | 1d8280c | 2017-01-27 16:40:12 +0100 | [diff] [blame] | 2659 | if (!listener->address_space) { |
| 2660 | return; |
| 2661 | } |
| 2662 | |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 2663 | QTAILQ_REMOVE(&memory_listeners, listener, link); |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 2664 | QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as); |
Paolo Bonzini | 1d8280c | 2017-01-27 16:40:12 +0100 | [diff] [blame] | 2665 | listener->address_space = NULL; |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 2666 | } |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 2667 | |
KONRAD Frederic | c935674 | 2016-10-19 15:06:49 +0200 | [diff] [blame] | 2668 | bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr) |
| 2669 | { |
| 2670 | void *host; |
| 2671 | unsigned size = 0; |
| 2672 | unsigned offset = 0; |
| 2673 | Object *new_interface; |
| 2674 | |
| 2675 | if (!mr || !mr->ops->request_ptr) { |
| 2676 | return false; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
| 2680 | * Avoid an update if the request_ptr call |
| 2681 | * memory_region_invalidate_mmio_ptr which seems to be likely when we use |
| 2682 | * a cache. |
| 2683 | */ |
| 2684 | memory_region_transaction_begin(); |
| 2685 | |
| 2686 | host = mr->ops->request_ptr(mr->opaque, addr - mr->addr, &size, &offset); |
| 2687 | |
| 2688 | if (!host || !size) { |
| 2689 | memory_region_transaction_commit(); |
| 2690 | return false; |
| 2691 | } |
| 2692 | |
| 2693 | new_interface = object_new("mmio_interface"); |
| 2694 | qdev_prop_set_uint64(DEVICE(new_interface), "start", offset); |
| 2695 | qdev_prop_set_uint64(DEVICE(new_interface), "end", offset + size - 1); |
| 2696 | qdev_prop_set_bit(DEVICE(new_interface), "ro", true); |
| 2697 | qdev_prop_set_ptr(DEVICE(new_interface), "host_ptr", host); |
| 2698 | qdev_prop_set_ptr(DEVICE(new_interface), "subregion", mr); |
| 2699 | object_property_set_bool(OBJECT(new_interface), true, "realized", NULL); |
| 2700 | |
| 2701 | memory_region_transaction_commit(); |
| 2702 | return true; |
| 2703 | } |
| 2704 | |
| 2705 | typedef struct MMIOPtrInvalidate { |
| 2706 | MemoryRegion *mr; |
| 2707 | hwaddr offset; |
| 2708 | unsigned size; |
| 2709 | int busy; |
| 2710 | int allocated; |
| 2711 | } MMIOPtrInvalidate; |
| 2712 | |
| 2713 | #define MAX_MMIO_INVALIDATE 10 |
| 2714 | static MMIOPtrInvalidate mmio_ptr_invalidate_list[MAX_MMIO_INVALIDATE]; |
| 2715 | |
| 2716 | static void memory_region_do_invalidate_mmio_ptr(CPUState *cpu, |
| 2717 | run_on_cpu_data data) |
| 2718 | { |
| 2719 | MMIOPtrInvalidate *invalidate_data = (MMIOPtrInvalidate *)data.host_ptr; |
| 2720 | MemoryRegion *mr = invalidate_data->mr; |
| 2721 | hwaddr offset = invalidate_data->offset; |
| 2722 | unsigned size = invalidate_data->size; |
| 2723 | MemoryRegionSection section = memory_region_find(mr, offset, size); |
| 2724 | |
| 2725 | qemu_mutex_lock_iothread(); |
| 2726 | |
| 2727 | /* Reset dirty so this doesn't happen later. */ |
| 2728 | cpu_physical_memory_test_and_clear_dirty(offset, size, 1); |
| 2729 | |
| 2730 | if (section.mr != mr) { |
| 2731 | /* memory_region_find add a ref on section.mr */ |
| 2732 | memory_region_unref(section.mr); |
| 2733 | if (MMIO_INTERFACE(section.mr->owner)) { |
| 2734 | /* We found the interface just drop it. */ |
| 2735 | object_property_set_bool(section.mr->owner, false, "realized", |
| 2736 | NULL); |
| 2737 | object_unref(section.mr->owner); |
| 2738 | object_unparent(section.mr->owner); |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | qemu_mutex_unlock_iothread(); |
| 2743 | |
| 2744 | if (invalidate_data->allocated) { |
| 2745 | g_free(invalidate_data); |
| 2746 | } else { |
| 2747 | invalidate_data->busy = 0; |
| 2748 | } |
| 2749 | } |
| 2750 | |
| 2751 | void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset, |
| 2752 | unsigned size) |
| 2753 | { |
| 2754 | size_t i; |
| 2755 | MMIOPtrInvalidate *invalidate_data = NULL; |
| 2756 | |
| 2757 | for (i = 0; i < MAX_MMIO_INVALIDATE; i++) { |
| 2758 | if (atomic_cmpxchg(&(mmio_ptr_invalidate_list[i].busy), 0, 1) == 0) { |
| 2759 | invalidate_data = &mmio_ptr_invalidate_list[i]; |
| 2760 | break; |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | if (!invalidate_data) { |
| 2765 | invalidate_data = g_malloc0(sizeof(MMIOPtrInvalidate)); |
| 2766 | invalidate_data->allocated = 1; |
| 2767 | } |
| 2768 | |
| 2769 | invalidate_data->mr = mr; |
| 2770 | invalidate_data->offset = offset; |
| 2771 | invalidate_data->size = size; |
| 2772 | |
| 2773 | async_safe_run_on_cpu(first_cpu, memory_region_do_invalidate_mmio_ptr, |
| 2774 | RUN_ON_CPU_HOST_PTR(invalidate_data)); |
| 2775 | } |
| 2776 | |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 2777 | void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 2778 | { |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 2779 | memory_region_ref(root); |
Avi Kivity | 8786db7 | 2012-10-02 13:53:41 +0200 | [diff] [blame] | 2780 | as->root = root; |
Alexey Kardashevskiy | 67ace39 | 2017-09-21 18:51:05 +1000 | [diff] [blame] | 2781 | as->current_map = NULL; |
Avi Kivity | 4c19eb7 | 2012-10-30 13:47:44 +0200 | [diff] [blame] | 2782 | as->ioeventfd_nb = 0; |
| 2783 | as->ioeventfds = NULL; |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 2784 | QTAILQ_INIT(&as->listeners); |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 2785 | QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link); |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 2786 | as->name = g_strdup(name ? name : "anonymous"); |
Alexey Kardashevskiy | 202fc01 | 2017-09-21 18:51:09 +1000 | [diff] [blame] | 2787 | address_space_update_topology(as); |
| 2788 | address_space_update_ioeventfds(as); |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 2789 | } |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 2790 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 2791 | static void do_address_space_destroy(AddressSpace *as) |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 2792 | { |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 2793 | assert(QTAILQ_EMPTY(&as->listeners)); |
David Gibson | 078c44f | 2014-05-30 12:59:00 -0600 | [diff] [blame] | 2794 | |
Paolo Bonzini | 856d724 | 2013-05-06 11:57:21 +0200 | [diff] [blame] | 2795 | flatview_unref(as->current_map); |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 2796 | g_free(as->name); |
Avi Kivity | 4c19eb7 | 2012-10-30 13:47:44 +0200 | [diff] [blame] | 2797 | g_free(as->ioeventfds); |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 2798 | memory_region_unref(as->root); |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 2799 | } |
| 2800 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 2801 | void address_space_destroy(AddressSpace *as) |
| 2802 | { |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 2803 | MemoryRegion *root = as->root; |
| 2804 | |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 2805 | /* Flush out anything from MemoryListeners listening in on this */ |
| 2806 | memory_region_transaction_begin(); |
| 2807 | as->root = NULL; |
| 2808 | memory_region_transaction_commit(); |
| 2809 | QTAILQ_REMOVE(&address_spaces, as, address_spaces_link); |
| 2810 | |
| 2811 | /* At this point, as->dispatch and as->current_map are dummy |
| 2812 | * entries that the guest should never use. Wait for the old |
| 2813 | * values to expire before freeing the data. |
| 2814 | */ |
Paolo Bonzini | ac95190 | 2015-02-11 15:21:04 +0100 | [diff] [blame] | 2815 | as->root = root; |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 2816 | call_rcu(as, do_address_space_destroy, rcu); |
| 2817 | } |
| 2818 | |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2819 | static const char *memory_region_type(MemoryRegion *mr) |
| 2820 | { |
| 2821 | if (memory_region_is_ram_device(mr)) { |
| 2822 | return "ramd"; |
| 2823 | } else if (memory_region_is_romd(mr)) { |
| 2824 | return "romd"; |
| 2825 | } else if (memory_region_is_rom(mr)) { |
| 2826 | return "rom"; |
| 2827 | } else if (memory_region_is_ram(mr)) { |
| 2828 | return "ram"; |
| 2829 | } else { |
| 2830 | return "i/o"; |
| 2831 | } |
| 2832 | } |
| 2833 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2834 | typedef struct MemoryRegionList MemoryRegionList; |
| 2835 | |
| 2836 | struct MemoryRegionList { |
| 2837 | const MemoryRegion *mr; |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2838 | QTAILQ_ENTRY(MemoryRegionList) mrqueue; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2839 | }; |
| 2840 | |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2841 | typedef QTAILQ_HEAD(mrqueue, MemoryRegionList) MemoryRegionListHead; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2842 | |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2843 | #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \ |
| 2844 | int128_sub((size), int128_one())) : 0) |
| 2845 | #define MTREE_INDENT " " |
| 2846 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2847 | static void mtree_print_mr(fprintf_function mon_printf, void *f, |
| 2848 | const MemoryRegion *mr, unsigned int level, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2849 | hwaddr base, |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2850 | MemoryRegionListHead *alias_print_queue) |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2851 | { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2852 | MemoryRegionList *new_ml, *ml, *next_ml; |
| 2853 | MemoryRegionListHead submr_print_queue; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2854 | const MemoryRegion *submr; |
| 2855 | unsigned int i; |
Peter Xu | b31f841 | 2017-03-14 20:56:27 +0800 | [diff] [blame] | 2856 | hwaddr cur_start, cur_end; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2857 | |
Gerd Hoffmann | f8a9f72 | 2015-04-08 12:57:11 +0200 | [diff] [blame] | 2858 | if (!mr) { |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2859 | return; |
| 2860 | } |
| 2861 | |
| 2862 | for (i = 0; i < level; i++) { |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2863 | mon_printf(f, MTREE_INDENT); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Peter Xu | b31f841 | 2017-03-14 20:56:27 +0800 | [diff] [blame] | 2866 | cur_start = base + mr->addr; |
| 2867 | cur_end = cur_start + MR_SIZE(mr->size); |
| 2868 | |
| 2869 | /* |
| 2870 | * Try to detect overflow of memory region. This should never |
| 2871 | * happen normally. When it happens, we dump something to warn the |
| 2872 | * user who is observing this. |
| 2873 | */ |
| 2874 | if (cur_start < base || cur_end < cur_start) { |
| 2875 | mon_printf(f, "[DETECTED OVERFLOW!] "); |
| 2876 | } |
| 2877 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2878 | if (mr->alias) { |
| 2879 | MemoryRegionList *ml; |
| 2880 | bool found = false; |
| 2881 | |
| 2882 | /* check if the alias is already in the queue */ |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2883 | QTAILQ_FOREACH(ml, alias_print_queue, mrqueue) { |
Paolo Bonzini | f54bb15 | 2013-12-11 12:51:46 +0100 | [diff] [blame] | 2884 | if (ml->mr == mr->alias) { |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2885 | found = true; |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | if (!found) { |
| 2890 | ml = g_new(MemoryRegionList, 1); |
| 2891 | ml->mr = mr->alias; |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2892 | QTAILQ_INSERT_TAIL(alias_print_queue, ml, mrqueue); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2893 | } |
Jan Kiszka | 4896d74 | 2012-02-04 16:25:42 +0100 | [diff] [blame] | 2894 | mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2895 | " (prio %d, %s): alias %s @%s " TARGET_FMT_plx |
Gerd Hoffmann | f8a9f72 | 2015-04-08 12:57:11 +0200 | [diff] [blame] | 2896 | "-" TARGET_FMT_plx "%s\n", |
Peter Xu | b31f841 | 2017-03-14 20:56:27 +0800 | [diff] [blame] | 2897 | cur_start, cur_end, |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 2898 | mr->priority, |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2899 | memory_region_type((MemoryRegion *)mr), |
Peter Crosthwaite | 3fb18b4 | 2014-08-14 23:55:36 -0700 | [diff] [blame] | 2900 | memory_region_name(mr), |
| 2901 | memory_region_name(mr->alias), |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2902 | mr->alias_offset, |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2903 | mr->alias_offset + MR_SIZE(mr->size), |
Gerd Hoffmann | f8a9f72 | 2015-04-08 12:57:11 +0200 | [diff] [blame] | 2904 | mr->enabled ? "" : " [disabled]"); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2905 | } else { |
Jan Kiszka | 4896d74 | 2012-02-04 16:25:42 +0100 | [diff] [blame] | 2906 | mon_printf(f, |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2907 | TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %s): %s%s\n", |
Peter Xu | b31f841 | 2017-03-14 20:56:27 +0800 | [diff] [blame] | 2908 | cur_start, cur_end, |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 2909 | mr->priority, |
Peter Xu | 4e83190 | 2017-01-16 16:40:04 +0800 | [diff] [blame] | 2910 | memory_region_type((MemoryRegion *)mr), |
Gerd Hoffmann | f8a9f72 | 2015-04-08 12:57:11 +0200 | [diff] [blame] | 2911 | memory_region_name(mr), |
| 2912 | mr->enabled ? "" : " [disabled]"); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2913 | } |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2914 | |
| 2915 | QTAILQ_INIT(&submr_print_queue); |
| 2916 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2917 | QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2918 | new_ml = g_new(MemoryRegionList, 1); |
| 2919 | new_ml->mr = submr; |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2920 | QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2921 | if (new_ml->mr->addr < ml->mr->addr || |
| 2922 | (new_ml->mr->addr == ml->mr->addr && |
| 2923 | new_ml->mr->priority > ml->mr->priority)) { |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2924 | QTAILQ_INSERT_BEFORE(ml, new_ml, mrqueue); |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2925 | new_ml = NULL; |
| 2926 | break; |
| 2927 | } |
| 2928 | } |
| 2929 | if (new_ml) { |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2930 | QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, mrqueue); |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2934 | QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) { |
Peter Xu | b31f841 | 2017-03-14 20:56:27 +0800 | [diff] [blame] | 2935 | mtree_print_mr(mon_printf, f, ml->mr, level + 1, cur_start, |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2936 | alias_print_queue); |
| 2937 | } |
| 2938 | |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 2939 | QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, mrqueue, next_ml) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 2940 | g_free(ml); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 2944 | struct FlatViewInfo { |
| 2945 | fprintf_function mon_printf; |
| 2946 | void *f; |
| 2947 | int counter; |
| 2948 | bool dispatch_tree; |
| 2949 | }; |
| 2950 | |
| 2951 | static void mtree_print_flatview(gpointer key, gpointer value, |
| 2952 | gpointer user_data) |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 2953 | { |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 2954 | FlatView *view = key; |
| 2955 | GArray *fv_address_spaces = value; |
| 2956 | struct FlatViewInfo *fvi = user_data; |
| 2957 | fprintf_function p = fvi->mon_printf; |
| 2958 | void *f = fvi->f; |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 2959 | FlatRange *range = &view->ranges[0]; |
| 2960 | MemoryRegion *mr; |
| 2961 | int n = view->nr; |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 2962 | int i; |
| 2963 | AddressSpace *as; |
| 2964 | |
| 2965 | p(f, "FlatView #%d\n", fvi->counter); |
| 2966 | ++fvi->counter; |
| 2967 | |
| 2968 | for (i = 0; i < fv_address_spaces->len; ++i) { |
| 2969 | as = g_array_index(fv_address_spaces, AddressSpace*, i); |
| 2970 | p(f, " AS \"%s\", root: %s", as->name, memory_region_name(as->root)); |
| 2971 | if (as->root->alias) { |
| 2972 | p(f, ", alias %s", memory_region_name(as->root->alias)); |
| 2973 | } |
| 2974 | p(f, "\n"); |
| 2975 | } |
| 2976 | |
| 2977 | p(f, " Root memory region: %s\n", |
| 2978 | view->root ? memory_region_name(view->root) : "(none)"); |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 2979 | |
| 2980 | if (n <= 0) { |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 2981 | p(f, MTREE_INDENT "No rendered FlatView\n\n"); |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 2982 | return; |
| 2983 | } |
| 2984 | |
| 2985 | while (n--) { |
| 2986 | mr = range->mr; |
Paolo Bonzini | 377a07a | 2017-03-02 22:49:41 +0100 | [diff] [blame] | 2987 | if (range->offset_in_region) { |
| 2988 | p(f, MTREE_INDENT TARGET_FMT_plx "-" |
| 2989 | TARGET_FMT_plx " (prio %d, %s): %s @" TARGET_FMT_plx "\n", |
| 2990 | int128_get64(range->addr.start), |
| 2991 | int128_get64(range->addr.start) + MR_SIZE(range->addr.size), |
| 2992 | mr->priority, |
| 2993 | range->readonly ? "rom" : memory_region_type(mr), |
| 2994 | memory_region_name(mr), |
| 2995 | range->offset_in_region); |
| 2996 | } else { |
| 2997 | p(f, MTREE_INDENT TARGET_FMT_plx "-" |
| 2998 | TARGET_FMT_plx " (prio %d, %s): %s\n", |
| 2999 | int128_get64(range->addr.start), |
| 3000 | int128_get64(range->addr.start) + MR_SIZE(range->addr.size), |
| 3001 | mr->priority, |
| 3002 | range->readonly ? "rom" : memory_region_type(mr), |
| 3003 | memory_region_name(mr)); |
| 3004 | } |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 3005 | range++; |
| 3006 | } |
| 3007 | |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 3008 | #if !defined(CONFIG_USER_ONLY) |
| 3009 | if (fvi->dispatch_tree && view->root) { |
| 3010 | mtree_print_dispatch(p, f, view->dispatch, view->root); |
| 3011 | } |
| 3012 | #endif |
| 3013 | |
| 3014 | p(f, "\n"); |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 3015 | } |
| 3016 | |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 3017 | static gboolean mtree_info_flatview_free(gpointer key, gpointer value, |
| 3018 | gpointer user_data) |
| 3019 | { |
| 3020 | FlatView *view = key; |
| 3021 | GArray *fv_address_spaces = value; |
| 3022 | |
| 3023 | g_array_unref(fv_address_spaces); |
| 3024 | flatview_unref(view); |
| 3025 | |
| 3026 | return true; |
| 3027 | } |
| 3028 | |
| 3029 | void mtree_info(fprintf_function mon_printf, void *f, bool flatview, |
| 3030 | bool dispatch_tree) |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3031 | { |
| 3032 | MemoryRegionListHead ml_head; |
| 3033 | MemoryRegionList *ml, *ml2; |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 3034 | AddressSpace *as; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3035 | |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 3036 | if (flatview) { |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 3037 | FlatView *view; |
| 3038 | struct FlatViewInfo fvi = { |
| 3039 | .mon_printf = mon_printf, |
| 3040 | .f = f, |
| 3041 | .counter = 0, |
| 3042 | .dispatch_tree = dispatch_tree |
| 3043 | }; |
| 3044 | GArray *fv_address_spaces; |
| 3045 | GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 3046 | |
| 3047 | /* Gather all FVs in one table */ |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 3048 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 3049 | view = address_space_get_flatview(as); |
| 3050 | |
| 3051 | fv_address_spaces = g_hash_table_lookup(views, view); |
| 3052 | if (!fv_address_spaces) { |
| 3053 | fv_address_spaces = g_array_new(false, false, sizeof(as)); |
| 3054 | g_hash_table_insert(views, view, fv_address_spaces); |
| 3055 | } |
| 3056 | |
| 3057 | g_array_append_val(fv_address_spaces, as); |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 3058 | } |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 3059 | |
| 3060 | /* Print */ |
| 3061 | g_hash_table_foreach(views, mtree_print_flatview, &fvi); |
| 3062 | |
| 3063 | /* Free */ |
| 3064 | g_hash_table_foreach_remove(views, mtree_info_flatview_free, 0); |
| 3065 | g_hash_table_unref(views); |
| 3066 | |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 3067 | return; |
| 3068 | } |
| 3069 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3070 | QTAILQ_INIT(&ml_head); |
| 3071 | |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 3072 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { |
Gerd Hoffmann | e48816a | 2015-04-08 12:53:47 +0200 | [diff] [blame] | 3073 | mon_printf(f, "address-space: %s\n", as->name); |
| 3074 | mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head); |
| 3075 | mon_printf(f, "\n"); |
Blue Swirl | b9f9be8 | 2012-03-10 16:58:35 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3078 | /* print aliased regions */ |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 3079 | QTAILQ_FOREACH(ml, &ml_head, mrqueue) { |
Gerd Hoffmann | e48816a | 2015-04-08 12:53:47 +0200 | [diff] [blame] | 3080 | mon_printf(f, "memory-region: %s\n", memory_region_name(ml->mr)); |
| 3081 | mtree_print_mr(mon_printf, f, ml->mr, 1, 0, &ml_head); |
| 3082 | mon_printf(f, "\n"); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Kamil Rytarowski | a16878d | 2017-09-03 18:33:04 +0200 | [diff] [blame] | 3085 | QTAILQ_FOREACH_SAFE(ml, &ml_head, mrqueue, ml2) { |
Avi Kivity | 88365e4 | 2011-11-13 12:00:55 +0200 | [diff] [blame] | 3086 | g_free(ml); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3087 | } |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 3088 | } |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 3089 | |
Peter Maydell | b08199c | 2017-07-07 15:42:51 +0100 | [diff] [blame] | 3090 | void memory_region_init_ram(MemoryRegion *mr, |
| 3091 | struct Object *owner, |
| 3092 | const char *name, |
| 3093 | uint64_t size, |
| 3094 | Error **errp) |
| 3095 | { |
| 3096 | DeviceState *owner_dev; |
| 3097 | Error *err = NULL; |
| 3098 | |
| 3099 | memory_region_init_ram_nomigrate(mr, owner, name, size, &err); |
| 3100 | if (err) { |
| 3101 | error_propagate(errp, err); |
| 3102 | return; |
| 3103 | } |
| 3104 | /* This will assert if owner is neither NULL nor a DeviceState. |
| 3105 | * We only want the owner here for the purposes of defining a |
| 3106 | * unique name for migration. TODO: Ideally we should implement |
| 3107 | * a naming scheme for Objects which are not DeviceStates, in |
| 3108 | * which case we can relax this restriction. |
| 3109 | */ |
| 3110 | owner_dev = DEVICE(owner); |
| 3111 | vmstate_register_ram(mr, owner_dev); |
| 3112 | } |
| 3113 | |
| 3114 | void memory_region_init_rom(MemoryRegion *mr, |
| 3115 | struct Object *owner, |
| 3116 | const char *name, |
| 3117 | uint64_t size, |
| 3118 | Error **errp) |
| 3119 | { |
| 3120 | DeviceState *owner_dev; |
| 3121 | Error *err = NULL; |
| 3122 | |
| 3123 | memory_region_init_rom_nomigrate(mr, owner, name, size, &err); |
| 3124 | if (err) { |
| 3125 | error_propagate(errp, err); |
| 3126 | return; |
| 3127 | } |
| 3128 | /* This will assert if owner is neither NULL nor a DeviceState. |
| 3129 | * We only want the owner here for the purposes of defining a |
| 3130 | * unique name for migration. TODO: Ideally we should implement |
| 3131 | * a naming scheme for Objects which are not DeviceStates, in |
| 3132 | * which case we can relax this restriction. |
| 3133 | */ |
| 3134 | owner_dev = DEVICE(owner); |
| 3135 | vmstate_register_ram(mr, owner_dev); |
| 3136 | } |
| 3137 | |
| 3138 | void memory_region_init_rom_device(MemoryRegion *mr, |
| 3139 | struct Object *owner, |
| 3140 | const MemoryRegionOps *ops, |
| 3141 | void *opaque, |
| 3142 | const char *name, |
| 3143 | uint64_t size, |
| 3144 | Error **errp) |
| 3145 | { |
| 3146 | DeviceState *owner_dev; |
| 3147 | Error *err = NULL; |
| 3148 | |
| 3149 | memory_region_init_rom_device_nomigrate(mr, owner, ops, opaque, |
| 3150 | name, size, &err); |
| 3151 | if (err) { |
| 3152 | error_propagate(errp, err); |
| 3153 | return; |
| 3154 | } |
| 3155 | /* This will assert if owner is neither NULL nor a DeviceState. |
| 3156 | * We only want the owner here for the purposes of defining a |
| 3157 | * unique name for migration. TODO: Ideally we should implement |
| 3158 | * a naming scheme for Objects which are not DeviceStates, in |
| 3159 | * which case we can relax this restriction. |
| 3160 | */ |
| 3161 | owner_dev = DEVICE(owner); |
| 3162 | vmstate_register_ram(mr, owner_dev); |
| 3163 | } |
| 3164 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 3165 | static const TypeInfo memory_region_info = { |
| 3166 | .parent = TYPE_OBJECT, |
| 3167 | .name = TYPE_MEMORY_REGION, |
| 3168 | .instance_size = sizeof(MemoryRegion), |
| 3169 | .instance_init = memory_region_initfn, |
| 3170 | .instance_finalize = memory_region_finalize, |
| 3171 | }; |
| 3172 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 3173 | static const TypeInfo iommu_memory_region_info = { |
| 3174 | .parent = TYPE_MEMORY_REGION, |
| 3175 | .name = TYPE_IOMMU_MEMORY_REGION, |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 3176 | .class_size = sizeof(IOMMUMemoryRegionClass), |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 3177 | .instance_size = sizeof(IOMMUMemoryRegion), |
| 3178 | .instance_init = iommu_memory_region_initfn, |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 3179 | .abstract = true, |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 3180 | }; |
| 3181 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 3182 | static void memory_register_types(void) |
| 3183 | { |
| 3184 | type_register_static(&memory_region_info); |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 3185 | type_register_static(&iommu_memory_region_info); |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | type_init(memory_register_types) |