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 | * |
| 12 | */ |
| 13 | |
| 14 | #include "memory.h" |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 15 | #include "exec-memory.h" |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 16 | #include "ioport.h" |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 17 | #include "bitops.h" |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 18 | #include "kvm.h" |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 21 | unsigned memory_region_transaction_depth = 0; |
Avi Kivity | e87c099 | 2011-09-14 12:16:20 +0300 | [diff] [blame^] | 22 | static bool memory_region_update_pending = false; |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 23 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 24 | typedef struct AddrRange AddrRange; |
| 25 | |
Avi Kivity | 8417ceb | 2011-08-03 11:56:14 +0300 | [diff] [blame] | 26 | /* |
| 27 | * Note using signed integers limits us to physical addresses at most |
| 28 | * 63 bits wide. They are needed for negative offsetting in aliases |
| 29 | * (large MemoryRegion::alias_offset). |
| 30 | */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 31 | struct AddrRange { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 32 | Int128 start; |
| 33 | Int128 size; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 34 | }; |
| 35 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 36 | static AddrRange addrrange_make(Int128 start, Int128 size) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 37 | { |
| 38 | return (AddrRange) { start, size }; |
| 39 | } |
| 40 | |
| 41 | static bool addrrange_equal(AddrRange r1, AddrRange r2) |
| 42 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 43 | 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] | 44 | } |
| 45 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 46 | static Int128 addrrange_end(AddrRange r) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 47 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 48 | return int128_add(r.start, r.size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 49 | } |
| 50 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 51 | static AddrRange addrrange_shift(AddrRange range, Int128 delta) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 52 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 53 | int128_addto(&range.start, delta); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 54 | return range; |
| 55 | } |
| 56 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 57 | static bool addrrange_contains(AddrRange range, Int128 addr) |
| 58 | { |
| 59 | return int128_ge(addr, range.start) |
| 60 | && int128_lt(addr, addrrange_end(range)); |
| 61 | } |
| 62 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 63 | static bool addrrange_intersects(AddrRange r1, AddrRange r2) |
| 64 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 65 | return addrrange_contains(r1, r2.start) |
| 66 | || addrrange_contains(r2, r1.start); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2) |
| 70 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 71 | Int128 start = int128_max(r1.start, r2.start); |
| 72 | Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2)); |
| 73 | return addrrange_make(start, int128_sub(end, start)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | struct CoalescedMemoryRange { |
| 77 | AddrRange addr; |
| 78 | QTAILQ_ENTRY(CoalescedMemoryRange) link; |
| 79 | }; |
| 80 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 81 | struct MemoryRegionIoeventfd { |
| 82 | AddrRange addr; |
| 83 | bool match_data; |
| 84 | uint64_t data; |
| 85 | int fd; |
| 86 | }; |
| 87 | |
| 88 | static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a, |
| 89 | MemoryRegionIoeventfd b) |
| 90 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 91 | if (int128_lt(a.addr.start, b.addr.start)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 92 | return true; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 93 | } else if (int128_gt(a.addr.start, b.addr.start)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 94 | return false; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 95 | } else if (int128_lt(a.addr.size, b.addr.size)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 96 | return true; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 97 | } else if (int128_gt(a.addr.size, b.addr.size)) { |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 98 | return false; |
| 99 | } else if (a.match_data < b.match_data) { |
| 100 | return true; |
| 101 | } else if (a.match_data > b.match_data) { |
| 102 | return false; |
| 103 | } else if (a.match_data) { |
| 104 | if (a.data < b.data) { |
| 105 | return true; |
| 106 | } else if (a.data > b.data) { |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | if (a.fd < b.fd) { |
| 111 | return true; |
| 112 | } else if (a.fd > b.fd) { |
| 113 | return false; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a, |
| 119 | MemoryRegionIoeventfd b) |
| 120 | { |
| 121 | return !memory_region_ioeventfd_before(a, b) |
| 122 | && !memory_region_ioeventfd_before(b, a); |
| 123 | } |
| 124 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 125 | typedef struct FlatRange FlatRange; |
| 126 | typedef struct FlatView FlatView; |
| 127 | |
| 128 | /* Range of memory in the global map. Addresses are absolute. */ |
| 129 | struct FlatRange { |
| 130 | MemoryRegion *mr; |
| 131 | target_phys_addr_t offset_in_region; |
| 132 | AddrRange addr; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 133 | uint8_t dirty_log_mask; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 134 | bool readable; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 135 | bool readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /* Flattened global view of current active memory hierarchy. Kept in sorted |
| 139 | * order. |
| 140 | */ |
| 141 | struct FlatView { |
| 142 | FlatRange *ranges; |
| 143 | unsigned nr; |
| 144 | unsigned nr_allocated; |
| 145 | }; |
| 146 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 147 | typedef struct AddressSpace AddressSpace; |
| 148 | typedef struct AddressSpaceOps AddressSpaceOps; |
| 149 | |
| 150 | /* A system address space - I/O, memory, etc. */ |
| 151 | struct AddressSpace { |
| 152 | const AddressSpaceOps *ops; |
| 153 | MemoryRegion *root; |
| 154 | FlatView current_map; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 155 | int ioeventfd_nb; |
| 156 | MemoryRegionIoeventfd *ioeventfds; |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | struct AddressSpaceOps { |
| 160 | void (*range_add)(AddressSpace *as, FlatRange *fr); |
| 161 | void (*range_del)(AddressSpace *as, FlatRange *fr); |
| 162 | void (*log_start)(AddressSpace *as, FlatRange *fr); |
| 163 | void (*log_stop)(AddressSpace *as, FlatRange *fr); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 164 | void (*ioeventfd_add)(AddressSpace *as, MemoryRegionIoeventfd *fd); |
| 165 | void (*ioeventfd_del)(AddressSpace *as, MemoryRegionIoeventfd *fd); |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 166 | }; |
| 167 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 168 | #define FOR_EACH_FLAT_RANGE(var, view) \ |
| 169 | for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var) |
| 170 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 171 | static bool flatrange_equal(FlatRange *a, FlatRange *b) |
| 172 | { |
| 173 | return a->mr == b->mr |
| 174 | && addrrange_equal(a->addr, b->addr) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 175 | && a->offset_in_region == b->offset_in_region |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 176 | && a->readable == b->readable |
| 177 | && a->readonly == b->readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void flatview_init(FlatView *view) |
| 181 | { |
| 182 | view->ranges = NULL; |
| 183 | view->nr = 0; |
| 184 | view->nr_allocated = 0; |
| 185 | } |
| 186 | |
| 187 | /* Insert a range into a given position. Caller is responsible for maintaining |
| 188 | * sorting order. |
| 189 | */ |
| 190 | static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range) |
| 191 | { |
| 192 | if (view->nr == view->nr_allocated) { |
| 193 | view->nr_allocated = MAX(2 * view->nr, 10); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 194 | view->ranges = g_realloc(view->ranges, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 195 | view->nr_allocated * sizeof(*view->ranges)); |
| 196 | } |
| 197 | memmove(view->ranges + pos + 1, view->ranges + pos, |
| 198 | (view->nr - pos) * sizeof(FlatRange)); |
| 199 | view->ranges[pos] = *range; |
| 200 | ++view->nr; |
| 201 | } |
| 202 | |
| 203 | static void flatview_destroy(FlatView *view) |
| 204 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 205 | g_free(view->ranges); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 208 | static bool can_merge(FlatRange *r1, FlatRange *r2) |
| 209 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 210 | return int128_eq(addrrange_end(r1->addr), r2->addr.start) |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 211 | && r1->mr == r2->mr |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 212 | && int128_eq(int128_add(int128_make64(r1->offset_in_region), |
| 213 | r1->addr.size), |
| 214 | int128_make64(r2->offset_in_region)) |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 215 | && r1->dirty_log_mask == r2->dirty_log_mask |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 216 | && r1->readable == r2->readable |
| 217 | && r1->readonly == r2->readonly; |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* Attempt to simplify a view by merging ajacent ranges */ |
| 221 | static void flatview_simplify(FlatView *view) |
| 222 | { |
| 223 | unsigned i, j; |
| 224 | |
| 225 | i = 0; |
| 226 | while (i < view->nr) { |
| 227 | j = i + 1; |
| 228 | while (j < view->nr |
| 229 | && can_merge(&view->ranges[j-1], &view->ranges[j])) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 230 | int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size); |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 231 | ++j; |
| 232 | } |
| 233 | ++i; |
| 234 | memmove(&view->ranges[i], &view->ranges[j], |
| 235 | (view->nr - j) * sizeof(view->ranges[j])); |
| 236 | view->nr -= j - i; |
| 237 | } |
| 238 | } |
| 239 | |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 240 | static void memory_region_read_accessor(void *opaque, |
| 241 | target_phys_addr_t addr, |
| 242 | uint64_t *value, |
| 243 | unsigned size, |
| 244 | unsigned shift, |
| 245 | uint64_t mask) |
| 246 | { |
| 247 | MemoryRegion *mr = opaque; |
| 248 | uint64_t tmp; |
| 249 | |
| 250 | tmp = mr->ops->read(mr->opaque, addr, size); |
| 251 | *value |= (tmp & mask) << shift; |
| 252 | } |
| 253 | |
| 254 | static void memory_region_write_accessor(void *opaque, |
| 255 | target_phys_addr_t addr, |
| 256 | uint64_t *value, |
| 257 | unsigned size, |
| 258 | unsigned shift, |
| 259 | uint64_t mask) |
| 260 | { |
| 261 | MemoryRegion *mr = opaque; |
| 262 | uint64_t tmp; |
| 263 | |
| 264 | tmp = (*value >> shift) & mask; |
| 265 | mr->ops->write(mr->opaque, addr, tmp, size); |
| 266 | } |
| 267 | |
| 268 | static void access_with_adjusted_size(target_phys_addr_t addr, |
| 269 | uint64_t *value, |
| 270 | unsigned size, |
| 271 | unsigned access_size_min, |
| 272 | unsigned access_size_max, |
| 273 | void (*access)(void *opaque, |
| 274 | target_phys_addr_t addr, |
| 275 | uint64_t *value, |
| 276 | unsigned size, |
| 277 | unsigned shift, |
| 278 | uint64_t mask), |
| 279 | void *opaque) |
| 280 | { |
| 281 | uint64_t access_mask; |
| 282 | unsigned access_size; |
| 283 | unsigned i; |
| 284 | |
| 285 | if (!access_size_min) { |
| 286 | access_size_min = 1; |
| 287 | } |
| 288 | if (!access_size_max) { |
| 289 | access_size_max = 4; |
| 290 | } |
| 291 | access_size = MAX(MIN(size, access_size_max), access_size_min); |
| 292 | access_mask = -1ULL >> (64 - access_size * 8); |
| 293 | for (i = 0; i < size; i += access_size) { |
| 294 | /* FIXME: big-endian support */ |
| 295 | access(opaque, addr + i, value, access_size, i * 8, access_mask); |
| 296 | } |
| 297 | } |
| 298 | |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 299 | static void memory_region_prepare_ram_addr(MemoryRegion *mr); |
| 300 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 301 | static void as_memory_range_add(AddressSpace *as, FlatRange *fr) |
| 302 | { |
| 303 | ram_addr_t phys_offset, region_offset; |
| 304 | |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 305 | memory_region_prepare_ram_addr(fr->mr); |
| 306 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 307 | phys_offset = fr->mr->ram_addr; |
| 308 | region_offset = fr->offset_in_region; |
| 309 | /* cpu_register_physical_memory_log() wants region_offset for |
| 310 | * mmio, but prefers offseting phys_offset for RAM. Humour it. |
| 311 | */ |
| 312 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { |
| 313 | phys_offset += region_offset; |
| 314 | region_offset = 0; |
| 315 | } |
| 316 | |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 317 | if (!fr->readable) { |
Avi Kivity | b5fe14c | 2011-08-29 09:12:49 +0300 | [diff] [blame] | 318 | phys_offset &= ~TARGET_PAGE_MASK & ~IO_MEM_ROMD; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 319 | } |
| 320 | |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 321 | if (fr->readonly) { |
| 322 | phys_offset |= IO_MEM_ROM; |
| 323 | } |
| 324 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 325 | cpu_register_physical_memory_log(int128_get64(fr->addr.start), |
| 326 | int128_get64(fr->addr.size), |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 327 | phys_offset, |
| 328 | region_offset, |
| 329 | fr->dirty_log_mask); |
| 330 | } |
| 331 | |
| 332 | static void as_memory_range_del(AddressSpace *as, FlatRange *fr) |
| 333 | { |
Avi Kivity | 39b796f | 2011-07-31 22:47:58 +0300 | [diff] [blame] | 334 | if (fr->dirty_log_mask) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 335 | Int128 end = addrrange_end(fr->addr); |
| 336 | cpu_physical_sync_dirty_bitmap(int128_get64(fr->addr.start), |
| 337 | int128_get64(end)); |
Avi Kivity | 39b796f | 2011-07-31 22:47:58 +0300 | [diff] [blame] | 338 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 339 | cpu_register_physical_memory(int128_get64(fr->addr.start), |
| 340 | int128_get64(fr->addr.size), |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 341 | IO_MEM_UNASSIGNED); |
| 342 | } |
| 343 | |
| 344 | static void as_memory_log_start(AddressSpace *as, FlatRange *fr) |
| 345 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 346 | cpu_physical_log_start(int128_get64(fr->addr.start), |
| 347 | int128_get64(fr->addr.size)); |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static void as_memory_log_stop(AddressSpace *as, FlatRange *fr) |
| 351 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 352 | cpu_physical_log_stop(int128_get64(fr->addr.start), |
| 353 | int128_get64(fr->addr.size)); |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 354 | } |
| 355 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 356 | static void as_memory_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd) |
| 357 | { |
| 358 | int r; |
| 359 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 360 | assert(fd->match_data && int128_get64(fd->addr.size) == 4); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 361 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 362 | r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start), |
| 363 | fd->data, true); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 364 | if (r < 0) { |
| 365 | abort(); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static void as_memory_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd) |
| 370 | { |
| 371 | int r; |
| 372 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 373 | r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start), |
| 374 | fd->data, false); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 375 | if (r < 0) { |
| 376 | abort(); |
| 377 | } |
| 378 | } |
| 379 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 380 | static const AddressSpaceOps address_space_ops_memory = { |
| 381 | .range_add = as_memory_range_add, |
| 382 | .range_del = as_memory_range_del, |
| 383 | .log_start = as_memory_log_start, |
| 384 | .log_stop = as_memory_log_stop, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 385 | .ioeventfd_add = as_memory_ioeventfd_add, |
| 386 | .ioeventfd_del = as_memory_ioeventfd_del, |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | static AddressSpace address_space_memory = { |
| 390 | .ops = &address_space_ops_memory, |
| 391 | }; |
| 392 | |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 393 | static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset, |
| 394 | unsigned width, bool write) |
| 395 | { |
| 396 | const MemoryRegionPortio *mrp; |
| 397 | |
| 398 | for (mrp = mr->ops->old_portio; mrp->size; ++mrp) { |
| 399 | if (offset >= mrp->offset && offset < mrp->offset + mrp->len |
| 400 | && width == mrp->size |
| 401 | && (write ? (bool)mrp->write : (bool)mrp->read)) { |
| 402 | return mrp; |
| 403 | } |
| 404 | } |
| 405 | return NULL; |
| 406 | } |
| 407 | |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 408 | static void memory_region_iorange_read(IORange *iorange, |
| 409 | uint64_t offset, |
| 410 | unsigned width, |
| 411 | uint64_t *data) |
| 412 | { |
| 413 | MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange); |
| 414 | |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 415 | if (mr->ops->old_portio) { |
| 416 | const MemoryRegionPortio *mrp = find_portio(mr, offset, width, false); |
| 417 | |
| 418 | *data = ((uint64_t)1 << (width * 8)) - 1; |
| 419 | if (mrp) { |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 420 | *data = mrp->read(mr->opaque, offset + mr->offset); |
Jan Kiszka | 03808f5 | 2011-09-18 14:51:58 +0200 | [diff] [blame] | 421 | } else if (width == 2) { |
| 422 | mrp = find_portio(mr, offset, 1, false); |
| 423 | assert(mrp); |
| 424 | *data = mrp->read(mr->opaque, offset + mr->offset) | |
| 425 | (mrp->read(mr->opaque, offset + mr->offset + 1) << 8); |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 426 | } |
| 427 | return; |
| 428 | } |
Avi Kivity | 3a130f4 | 2011-08-11 10:40:26 +0300 | [diff] [blame] | 429 | *data = 0; |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 430 | access_with_adjusted_size(offset + mr->offset, data, width, |
Avi Kivity | 3a130f4 | 2011-08-11 10:40:26 +0300 | [diff] [blame] | 431 | mr->ops->impl.min_access_size, |
| 432 | mr->ops->impl.max_access_size, |
| 433 | memory_region_read_accessor, mr); |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static void memory_region_iorange_write(IORange *iorange, |
| 437 | uint64_t offset, |
| 438 | unsigned width, |
| 439 | uint64_t data) |
| 440 | { |
| 441 | MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange); |
| 442 | |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 443 | if (mr->ops->old_portio) { |
| 444 | const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true); |
| 445 | |
| 446 | if (mrp) { |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 447 | mrp->write(mr->opaque, offset + mr->offset, data); |
Jan Kiszka | 03808f5 | 2011-09-18 14:51:58 +0200 | [diff] [blame] | 448 | } else if (width == 2) { |
| 449 | mrp = find_portio(mr, offset, 1, false); |
| 450 | assert(mrp); |
| 451 | mrp->write(mr->opaque, offset + mr->offset, data & 0xff); |
| 452 | mrp->write(mr->opaque, offset + mr->offset + 1, data >> 8); |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 453 | } |
| 454 | return; |
| 455 | } |
Avi Kivity | 6bf9fd4 | 2011-09-26 14:52:26 +0300 | [diff] [blame] | 456 | access_with_adjusted_size(offset + mr->offset, &data, width, |
Avi Kivity | 3a130f4 | 2011-08-11 10:40:26 +0300 | [diff] [blame] | 457 | mr->ops->impl.min_access_size, |
| 458 | mr->ops->impl.max_access_size, |
| 459 | memory_region_write_accessor, mr); |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static const IORangeOps memory_region_iorange_ops = { |
| 463 | .read = memory_region_iorange_read, |
| 464 | .write = memory_region_iorange_write, |
| 465 | }; |
| 466 | |
| 467 | static void as_io_range_add(AddressSpace *as, FlatRange *fr) |
| 468 | { |
| 469 | iorange_init(&fr->mr->iorange, &memory_region_iorange_ops, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 470 | int128_get64(fr->addr.start), int128_get64(fr->addr.size)); |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 471 | ioport_register(&fr->mr->iorange); |
| 472 | } |
| 473 | |
| 474 | static void as_io_range_del(AddressSpace *as, FlatRange *fr) |
| 475 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 476 | isa_unassign_ioport(int128_get64(fr->addr.start), |
| 477 | int128_get64(fr->addr.size)); |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 478 | } |
| 479 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 480 | static void as_io_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd) |
| 481 | { |
| 482 | int r; |
| 483 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 484 | assert(fd->match_data && int128_get64(fd->addr.size) == 2); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 485 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 486 | r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start), |
| 487 | fd->data, true); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 488 | if (r < 0) { |
| 489 | abort(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static void as_io_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd) |
| 494 | { |
| 495 | int r; |
| 496 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 497 | r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start), |
| 498 | fd->data, false); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 499 | if (r < 0) { |
| 500 | abort(); |
| 501 | } |
| 502 | } |
| 503 | |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 504 | static const AddressSpaceOps address_space_ops_io = { |
| 505 | .range_add = as_io_range_add, |
| 506 | .range_del = as_io_range_del, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 507 | .ioeventfd_add = as_io_ioeventfd_add, |
| 508 | .ioeventfd_del = as_io_ioeventfd_del, |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | static AddressSpace address_space_io = { |
| 512 | .ops = &address_space_ops_io, |
| 513 | }; |
| 514 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 515 | /* Render a memory region into the global view. Ranges in @view obscure |
| 516 | * ranges in @mr. |
| 517 | */ |
| 518 | static void render_memory_region(FlatView *view, |
| 519 | MemoryRegion *mr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 520 | Int128 base, |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 521 | AddrRange clip, |
| 522 | bool readonly) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 523 | { |
| 524 | MemoryRegion *subregion; |
| 525 | unsigned i; |
| 526 | target_phys_addr_t offset_in_region; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 527 | Int128 remain; |
| 528 | Int128 now; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 529 | FlatRange fr; |
| 530 | AddrRange tmp; |
| 531 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 532 | if (!mr->enabled) { |
| 533 | return; |
| 534 | } |
| 535 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 536 | int128_addto(&base, int128_make64(mr->addr)); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 537 | readonly |= mr->readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 538 | |
| 539 | tmp = addrrange_make(base, mr->size); |
| 540 | |
| 541 | if (!addrrange_intersects(tmp, clip)) { |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | clip = addrrange_intersection(tmp, clip); |
| 546 | |
| 547 | if (mr->alias) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 548 | int128_subfrom(&base, int128_make64(mr->alias->addr)); |
| 549 | int128_subfrom(&base, int128_make64(mr->alias_offset)); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 550 | render_memory_region(view, mr->alias, base, clip, readonly); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | |
| 554 | /* Render subregions in priority order. */ |
| 555 | QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) { |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 556 | render_memory_region(view, subregion, base, clip, readonly); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 557 | } |
| 558 | |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 559 | if (!mr->terminates) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 563 | offset_in_region = int128_get64(int128_sub(clip.start, base)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 564 | base = clip.start; |
| 565 | remain = clip.size; |
| 566 | |
| 567 | /* Render the region itself into any gaps left by the current view. */ |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 568 | for (i = 0; i < view->nr && int128_nz(remain); ++i) { |
| 569 | if (int128_ge(base, addrrange_end(view->ranges[i].addr))) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 570 | continue; |
| 571 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 572 | if (int128_lt(base, view->ranges[i].addr.start)) { |
| 573 | now = int128_min(remain, |
| 574 | int128_sub(view->ranges[i].addr.start, base)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 575 | fr.mr = mr; |
| 576 | fr.offset_in_region = offset_in_region; |
| 577 | fr.addr = addrrange_make(base, now); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 578 | fr.dirty_log_mask = mr->dirty_log_mask; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 579 | fr.readable = mr->readable; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 580 | fr.readonly = readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 581 | flatview_insert(view, i, &fr); |
| 582 | ++i; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 583 | int128_addto(&base, now); |
| 584 | offset_in_region += int128_get64(now); |
| 585 | int128_subfrom(&remain, now); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 586 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 587 | if (int128_eq(base, view->ranges[i].addr.start)) { |
| 588 | now = int128_min(remain, view->ranges[i].addr.size); |
| 589 | int128_addto(&base, now); |
| 590 | offset_in_region += int128_get64(now); |
| 591 | int128_subfrom(&remain, now); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 592 | } |
| 593 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 594 | if (int128_nz(remain)) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 595 | fr.mr = mr; |
| 596 | fr.offset_in_region = offset_in_region; |
| 597 | fr.addr = addrrange_make(base, remain); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 598 | fr.dirty_log_mask = mr->dirty_log_mask; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 599 | fr.readable = mr->readable; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 600 | fr.readonly = readonly; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 601 | flatview_insert(view, i, &fr); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* Render a memory topology into a list of disjoint absolute ranges. */ |
| 606 | static FlatView generate_memory_topology(MemoryRegion *mr) |
| 607 | { |
| 608 | FlatView view; |
| 609 | |
| 610 | flatview_init(&view); |
| 611 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 612 | render_memory_region(&view, mr, int128_zero(), |
| 613 | addrrange_make(int128_zero(), int128_2_64()), false); |
Avi Kivity | 3d8e6bf | 2011-07-26 14:26:03 +0300 | [diff] [blame] | 614 | flatview_simplify(&view); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 615 | |
| 616 | return view; |
| 617 | } |
| 618 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 619 | static void address_space_add_del_ioeventfds(AddressSpace *as, |
| 620 | MemoryRegionIoeventfd *fds_new, |
| 621 | unsigned fds_new_nb, |
| 622 | MemoryRegionIoeventfd *fds_old, |
| 623 | unsigned fds_old_nb) |
| 624 | { |
| 625 | unsigned iold, inew; |
| 626 | |
| 627 | /* Generate a symmetric difference of the old and new fd sets, adding |
| 628 | * and deleting as necessary. |
| 629 | */ |
| 630 | |
| 631 | iold = inew = 0; |
| 632 | while (iold < fds_old_nb || inew < fds_new_nb) { |
| 633 | if (iold < fds_old_nb |
| 634 | && (inew == fds_new_nb |
| 635 | || memory_region_ioeventfd_before(fds_old[iold], |
| 636 | fds_new[inew]))) { |
| 637 | as->ops->ioeventfd_del(as, &fds_old[iold]); |
| 638 | ++iold; |
| 639 | } else if (inew < fds_new_nb |
| 640 | && (iold == fds_old_nb |
| 641 | || memory_region_ioeventfd_before(fds_new[inew], |
| 642 | fds_old[iold]))) { |
| 643 | as->ops->ioeventfd_add(as, &fds_new[inew]); |
| 644 | ++inew; |
| 645 | } else { |
| 646 | ++iold; |
| 647 | ++inew; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | static void address_space_update_ioeventfds(AddressSpace *as) |
| 653 | { |
| 654 | FlatRange *fr; |
| 655 | unsigned ioeventfd_nb = 0; |
| 656 | MemoryRegionIoeventfd *ioeventfds = NULL; |
| 657 | AddrRange tmp; |
| 658 | unsigned i; |
| 659 | |
| 660 | FOR_EACH_FLAT_RANGE(fr, &as->current_map) { |
| 661 | for (i = 0; i < fr->mr->ioeventfd_nb; ++i) { |
| 662 | tmp = addrrange_shift(fr->mr->ioeventfds[i].addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 663 | int128_sub(fr->addr.start, |
| 664 | int128_make64(fr->offset_in_region))); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 665 | if (addrrange_intersects(fr->addr, tmp)) { |
| 666 | ++ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 667 | ioeventfds = g_realloc(ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 668 | ioeventfd_nb * sizeof(*ioeventfds)); |
| 669 | ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i]; |
| 670 | ioeventfds[ioeventfd_nb-1].addr = tmp; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb, |
| 676 | as->ioeventfds, as->ioeventfd_nb); |
| 677 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 678 | g_free(as->ioeventfds); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 679 | as->ioeventfds = ioeventfds; |
| 680 | as->ioeventfd_nb = ioeventfd_nb; |
| 681 | } |
| 682 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 683 | static void address_space_update_topology_pass(AddressSpace *as, |
| 684 | FlatView old_view, |
| 685 | FlatView new_view, |
| 686 | bool adding) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 687 | { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 688 | unsigned iold, inew; |
| 689 | FlatRange *frold, *frnew; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 690 | |
| 691 | /* Generate a symmetric difference of the old and new memory maps. |
| 692 | * Kill ranges in the old map, and instantiate ranges in the new map. |
| 693 | */ |
| 694 | iold = inew = 0; |
| 695 | while (iold < old_view.nr || inew < new_view.nr) { |
| 696 | if (iold < old_view.nr) { |
| 697 | frold = &old_view.ranges[iold]; |
| 698 | } else { |
| 699 | frold = NULL; |
| 700 | } |
| 701 | if (inew < new_view.nr) { |
| 702 | frnew = &new_view.ranges[inew]; |
| 703 | } else { |
| 704 | frnew = NULL; |
| 705 | } |
| 706 | |
| 707 | if (frold |
| 708 | && (!frnew |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 709 | || int128_lt(frold->addr.start, frnew->addr.start) |
| 710 | || (int128_eq(frold->addr.start, frnew->addr.start) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 711 | && !flatrange_equal(frold, frnew)))) { |
| 712 | /* In old, but (not in new, or in new but attributes changed). */ |
| 713 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 714 | if (!adding) { |
| 715 | as->ops->range_del(as, frold); |
| 716 | } |
| 717 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 718 | ++iold; |
| 719 | } else if (frold && frnew && flatrange_equal(frold, frnew)) { |
| 720 | /* In both (logging may have changed) */ |
| 721 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 722 | if (adding) { |
| 723 | if (frold->dirty_log_mask && !frnew->dirty_log_mask) { |
| 724 | as->ops->log_stop(as, frnew); |
| 725 | } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) { |
| 726 | as->ops->log_start(as, frnew); |
| 727 | } |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 728 | } |
| 729 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 730 | ++iold; |
| 731 | ++inew; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 732 | } else { |
| 733 | /* In new */ |
| 734 | |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 735 | if (adding) { |
| 736 | as->ops->range_add(as, frnew); |
| 737 | } |
| 738 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 739 | ++inew; |
| 740 | } |
| 741 | } |
Avi Kivity | b8af1af | 2011-07-26 14:26:12 +0300 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
| 745 | static void address_space_update_topology(AddressSpace *as) |
| 746 | { |
| 747 | FlatView old_view = as->current_map; |
| 748 | FlatView new_view = generate_memory_topology(as->root); |
| 749 | |
| 750 | address_space_update_topology_pass(as, old_view, new_view, false); |
| 751 | address_space_update_topology_pass(as, old_view, new_view, true); |
| 752 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 753 | as->current_map = new_view; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 754 | flatview_destroy(&old_view); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 755 | address_space_update_ioeventfds(as); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 756 | } |
| 757 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 758 | static void memory_region_update_topology(MemoryRegion *mr) |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 759 | { |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 760 | if (memory_region_transaction_depth) { |
Avi Kivity | e87c099 | 2011-09-14 12:16:20 +0300 | [diff] [blame^] | 761 | memory_region_update_pending |= !mr || mr->enabled; |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 765 | if (mr && !mr->enabled) { |
| 766 | return; |
| 767 | } |
| 768 | |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 769 | if (address_space_memory.root) { |
| 770 | address_space_update_topology(&address_space_memory); |
| 771 | } |
| 772 | if (address_space_io.root) { |
| 773 | address_space_update_topology(&address_space_io); |
| 774 | } |
Avi Kivity | e87c099 | 2011-09-14 12:16:20 +0300 | [diff] [blame^] | 775 | |
| 776 | memory_region_update_pending = false; |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 777 | } |
| 778 | |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 779 | void memory_region_transaction_begin(void) |
| 780 | { |
| 781 | ++memory_region_transaction_depth; |
| 782 | } |
| 783 | |
| 784 | void memory_region_transaction_commit(void) |
| 785 | { |
| 786 | assert(memory_region_transaction_depth); |
| 787 | --memory_region_transaction_depth; |
Avi Kivity | e87c099 | 2011-09-14 12:16:20 +0300 | [diff] [blame^] | 788 | if (!memory_region_transaction_depth && memory_region_update_pending) { |
| 789 | memory_region_update_topology(NULL); |
| 790 | } |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 791 | } |
| 792 | |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 793 | static void memory_region_destructor_none(MemoryRegion *mr) |
| 794 | { |
| 795 | } |
| 796 | |
| 797 | static void memory_region_destructor_ram(MemoryRegion *mr) |
| 798 | { |
| 799 | qemu_ram_free(mr->ram_addr); |
| 800 | } |
| 801 | |
| 802 | static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr) |
| 803 | { |
| 804 | qemu_ram_free_from_ptr(mr->ram_addr); |
| 805 | } |
| 806 | |
| 807 | static void memory_region_destructor_iomem(MemoryRegion *mr) |
| 808 | { |
| 809 | cpu_unregister_io_memory(mr->ram_addr); |
| 810 | } |
| 811 | |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 812 | static void memory_region_destructor_rom_device(MemoryRegion *mr) |
| 813 | { |
| 814 | qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK); |
| 815 | cpu_unregister_io_memory(mr->ram_addr & ~(TARGET_PAGE_MASK | IO_MEM_ROMD)); |
| 816 | } |
| 817 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 818 | void memory_region_init(MemoryRegion *mr, |
| 819 | const char *name, |
| 820 | uint64_t size) |
| 821 | { |
| 822 | mr->ops = NULL; |
| 823 | mr->parent = NULL; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 824 | mr->size = int128_make64(size); |
| 825 | if (size == UINT64_MAX) { |
| 826 | mr->size = int128_2_64(); |
| 827 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 828 | mr->addr = 0; |
| 829 | mr->offset = 0; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 830 | mr->enabled = true; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 831 | mr->terminates = false; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 832 | mr->readable = true; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 833 | mr->readonly = false; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 834 | mr->destructor = memory_region_destructor_none; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 835 | mr->priority = 0; |
| 836 | mr->may_overlap = false; |
| 837 | mr->alias = NULL; |
| 838 | QTAILQ_INIT(&mr->subregions); |
| 839 | memset(&mr->subregions_link, 0, sizeof mr->subregions_link); |
| 840 | QTAILQ_INIT(&mr->coalesced); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 841 | mr->name = g_strdup(name); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 842 | mr->dirty_log_mask = 0; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 843 | mr->ioeventfd_nb = 0; |
| 844 | mr->ioeventfds = NULL; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static bool memory_region_access_valid(MemoryRegion *mr, |
| 848 | target_phys_addr_t addr, |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 849 | unsigned size, |
| 850 | bool is_write) |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 851 | { |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 852 | if (mr->ops->valid.accepts |
| 853 | && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) { |
| 854 | return false; |
| 855 | } |
| 856 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 857 | if (!mr->ops->valid.unaligned && (addr & (size - 1))) { |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | /* Treat zero as compatibility all valid */ |
| 862 | if (!mr->ops->valid.max_access_size) { |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | if (size > mr->ops->valid.max_access_size |
| 867 | || size < mr->ops->valid.min_access_size) { |
| 868 | return false; |
| 869 | } |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | static uint32_t memory_region_read_thunk_n(void *_mr, |
| 874 | target_phys_addr_t addr, |
| 875 | unsigned size) |
| 876 | { |
| 877 | MemoryRegion *mr = _mr; |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 878 | uint64_t data = 0; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 879 | |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 880 | if (!memory_region_access_valid(mr, addr, size, false)) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 881 | return -1U; /* FIXME: better signalling */ |
| 882 | } |
| 883 | |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 884 | if (!mr->ops->read) { |
| 885 | return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr); |
| 886 | } |
| 887 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 888 | /* FIXME: support unaligned access */ |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 889 | access_with_adjusted_size(addr + mr->offset, &data, size, |
| 890 | mr->ops->impl.min_access_size, |
| 891 | mr->ops->impl.max_access_size, |
| 892 | memory_region_read_accessor, mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 893 | |
| 894 | return data; |
| 895 | } |
| 896 | |
| 897 | static void memory_region_write_thunk_n(void *_mr, |
| 898 | target_phys_addr_t addr, |
| 899 | unsigned size, |
| 900 | uint64_t data) |
| 901 | { |
| 902 | MemoryRegion *mr = _mr; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 903 | |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 904 | if (!memory_region_access_valid(mr, addr, size, true)) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 905 | return; /* FIXME: better signalling */ |
| 906 | } |
| 907 | |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 908 | if (!mr->ops->write) { |
| 909 | mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data); |
| 910 | return; |
| 911 | } |
| 912 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 913 | /* FIXME: support unaligned access */ |
Avi Kivity | 164a4dc | 2011-08-11 10:40:25 +0300 | [diff] [blame] | 914 | access_with_adjusted_size(addr + mr->offset, &data, size, |
| 915 | mr->ops->impl.min_access_size, |
| 916 | mr->ops->impl.max_access_size, |
| 917 | memory_region_write_accessor, mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static uint32_t memory_region_read_thunk_b(void *mr, target_phys_addr_t addr) |
| 921 | { |
| 922 | return memory_region_read_thunk_n(mr, addr, 1); |
| 923 | } |
| 924 | |
| 925 | static uint32_t memory_region_read_thunk_w(void *mr, target_phys_addr_t addr) |
| 926 | { |
| 927 | return memory_region_read_thunk_n(mr, addr, 2); |
| 928 | } |
| 929 | |
| 930 | static uint32_t memory_region_read_thunk_l(void *mr, target_phys_addr_t addr) |
| 931 | { |
| 932 | return memory_region_read_thunk_n(mr, addr, 4); |
| 933 | } |
| 934 | |
| 935 | static void memory_region_write_thunk_b(void *mr, target_phys_addr_t addr, |
| 936 | uint32_t data) |
| 937 | { |
| 938 | memory_region_write_thunk_n(mr, addr, 1, data); |
| 939 | } |
| 940 | |
| 941 | static void memory_region_write_thunk_w(void *mr, target_phys_addr_t addr, |
| 942 | uint32_t data) |
| 943 | { |
| 944 | memory_region_write_thunk_n(mr, addr, 2, data); |
| 945 | } |
| 946 | |
| 947 | static void memory_region_write_thunk_l(void *mr, target_phys_addr_t addr, |
| 948 | uint32_t data) |
| 949 | { |
| 950 | memory_region_write_thunk_n(mr, addr, 4, data); |
| 951 | } |
| 952 | |
| 953 | static CPUReadMemoryFunc * const memory_region_read_thunk[] = { |
| 954 | memory_region_read_thunk_b, |
| 955 | memory_region_read_thunk_w, |
| 956 | memory_region_read_thunk_l, |
| 957 | }; |
| 958 | |
| 959 | static CPUWriteMemoryFunc * const memory_region_write_thunk[] = { |
| 960 | memory_region_write_thunk_b, |
| 961 | memory_region_write_thunk_w, |
| 962 | memory_region_write_thunk_l, |
| 963 | }; |
| 964 | |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 965 | static void memory_region_prepare_ram_addr(MemoryRegion *mr) |
| 966 | { |
| 967 | if (mr->backend_registered) { |
| 968 | return; |
| 969 | } |
| 970 | |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 971 | mr->destructor = memory_region_destructor_iomem; |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 972 | mr->ram_addr = cpu_register_io_memory(memory_region_read_thunk, |
| 973 | memory_region_write_thunk, |
| 974 | mr, |
| 975 | mr->ops->endianness); |
| 976 | mr->backend_registered = true; |
| 977 | } |
| 978 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 979 | void memory_region_init_io(MemoryRegion *mr, |
| 980 | const MemoryRegionOps *ops, |
| 981 | void *opaque, |
| 982 | const char *name, |
| 983 | uint64_t size) |
| 984 | { |
| 985 | memory_region_init(mr, name, size); |
| 986 | mr->ops = ops; |
| 987 | mr->opaque = opaque; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 988 | mr->terminates = true; |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 989 | mr->backend_registered = false; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | void memory_region_init_ram(MemoryRegion *mr, |
| 993 | DeviceState *dev, |
| 994 | const char *name, |
| 995 | uint64_t size) |
| 996 | { |
| 997 | memory_region_init(mr, name, size); |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 998 | mr->terminates = true; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 999 | mr->destructor = memory_region_destructor_ram; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1000 | mr->ram_addr = qemu_ram_alloc(dev, name, size); |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 1001 | mr->backend_registered = true; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | void memory_region_init_ram_ptr(MemoryRegion *mr, |
| 1005 | DeviceState *dev, |
| 1006 | const char *name, |
| 1007 | uint64_t size, |
| 1008 | void *ptr) |
| 1009 | { |
| 1010 | memory_region_init(mr, name, size); |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1011 | mr->terminates = true; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1012 | mr->destructor = memory_region_destructor_ram_from_ptr; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1013 | mr->ram_addr = qemu_ram_alloc_from_ptr(dev, name, size, ptr); |
Avi Kivity | 16ef61c | 2011-07-26 14:26:07 +0300 | [diff] [blame] | 1014 | mr->backend_registered = true; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | void memory_region_init_alias(MemoryRegion *mr, |
| 1018 | const char *name, |
| 1019 | MemoryRegion *orig, |
| 1020 | target_phys_addr_t offset, |
| 1021 | uint64_t size) |
| 1022 | { |
| 1023 | memory_region_init(mr, name, size); |
| 1024 | mr->alias = orig; |
| 1025 | mr->alias_offset = offset; |
| 1026 | } |
| 1027 | |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1028 | void memory_region_init_rom_device(MemoryRegion *mr, |
| 1029 | const MemoryRegionOps *ops, |
Avi Kivity | 75f5941 | 2011-08-26 00:35:15 +0300 | [diff] [blame] | 1030 | void *opaque, |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1031 | DeviceState *dev, |
| 1032 | const char *name, |
| 1033 | uint64_t size) |
| 1034 | { |
| 1035 | memory_region_init(mr, name, size); |
Avi Kivity | 7bc2b9c | 2011-08-25 14:56:14 +0300 | [diff] [blame] | 1036 | mr->ops = ops; |
Avi Kivity | 75f5941 | 2011-08-26 00:35:15 +0300 | [diff] [blame] | 1037 | mr->opaque = opaque; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1038 | mr->terminates = true; |
| 1039 | mr->destructor = memory_region_destructor_rom_device; |
| 1040 | mr->ram_addr = qemu_ram_alloc(dev, name, size); |
| 1041 | mr->ram_addr |= cpu_register_io_memory(memory_region_read_thunk, |
| 1042 | memory_region_write_thunk, |
| 1043 | mr, |
| 1044 | mr->ops->endianness); |
| 1045 | mr->ram_addr |= IO_MEM_ROMD; |
| 1046 | mr->backend_registered = true; |
| 1047 | } |
| 1048 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1049 | void memory_region_destroy(MemoryRegion *mr) |
| 1050 | { |
| 1051 | assert(QTAILQ_EMPTY(&mr->subregions)); |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 1052 | mr->destructor(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1053 | memory_region_clear_coalescing(mr); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1054 | g_free((char *)mr->name); |
| 1055 | g_free(mr->ioeventfds); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | uint64_t memory_region_size(MemoryRegion *mr) |
| 1059 | { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1060 | if (int128_eq(mr->size, int128_2_64())) { |
| 1061 | return UINT64_MAX; |
| 1062 | } |
| 1063 | return int128_get64(mr->size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset) |
| 1067 | { |
| 1068 | mr->offset = offset; |
| 1069 | } |
| 1070 | |
| 1071 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client) |
| 1072 | { |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1073 | uint8_t mask = 1 << client; |
| 1074 | |
| 1075 | mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1076 | memory_region_update_topology(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr, |
| 1080 | unsigned client) |
| 1081 | { |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1082 | assert(mr->terminates); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1083 | return cpu_physical_memory_get_dirty(mr->ram_addr + addr, 1 << client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr) |
| 1087 | { |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1088 | assert(mr->terminates); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1089 | return cpu_physical_memory_set_dirty(mr->ram_addr + addr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | void memory_region_sync_dirty_bitmap(MemoryRegion *mr) |
| 1093 | { |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1094 | FlatRange *fr; |
| 1095 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 1096 | FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) { |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1097 | if (fr->mr == mr) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1098 | cpu_physical_sync_dirty_bitmap(int128_get64(fr->addr.start), |
| 1099 | int128_get64(addrrange_end(fr->addr))); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1100 | } |
| 1101 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly) |
| 1105 | { |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 1106 | if (mr->readonly != readonly) { |
| 1107 | mr->readonly = readonly; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1108 | memory_region_update_topology(mr); |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 1109 | } |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1110 | } |
| 1111 | |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1112 | void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable) |
| 1113 | { |
| 1114 | if (mr->readable != readable) { |
| 1115 | mr->readable = readable; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1116 | memory_region_update_topology(mr); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1120 | void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr, |
| 1121 | target_phys_addr_t size, unsigned client) |
| 1122 | { |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1123 | assert(mr->terminates); |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 1124 | cpu_physical_memory_reset_dirty(mr->ram_addr + addr, |
| 1125 | mr->ram_addr + addr + size, |
| 1126 | 1 << client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | void *memory_region_get_ram_ptr(MemoryRegion *mr) |
| 1130 | { |
| 1131 | if (mr->alias) { |
| 1132 | return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset; |
| 1133 | } |
| 1134 | |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 1135 | assert(mr->terminates); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1136 | |
Jan Kiszka | 021d26d | 2011-08-30 00:38:24 +0200 | [diff] [blame] | 1137 | return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static void memory_region_update_coalesced_range(MemoryRegion *mr) |
| 1141 | { |
| 1142 | FlatRange *fr; |
| 1143 | CoalescedMemoryRange *cmr; |
| 1144 | AddrRange tmp; |
| 1145 | |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 1146 | FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1147 | if (fr->mr == mr) { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1148 | qemu_unregister_coalesced_mmio(int128_get64(fr->addr.start), |
| 1149 | int128_get64(fr->addr.size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1150 | QTAILQ_FOREACH(cmr, &mr->coalesced, link) { |
| 1151 | tmp = addrrange_shift(cmr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1152 | int128_sub(fr->addr.start, |
| 1153 | int128_make64(fr->offset_in_region))); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1154 | if (!addrrange_intersects(tmp, fr->addr)) { |
| 1155 | continue; |
| 1156 | } |
| 1157 | tmp = addrrange_intersection(tmp, fr->addr); |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1158 | qemu_register_coalesced_mmio(int128_get64(tmp.start), |
| 1159 | int128_get64(tmp.size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | void memory_region_set_coalescing(MemoryRegion *mr) |
| 1166 | { |
| 1167 | memory_region_clear_coalescing(mr); |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1168 | memory_region_add_coalescing(mr, 0, int128_get64(mr->size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | void memory_region_add_coalescing(MemoryRegion *mr, |
| 1172 | target_phys_addr_t offset, |
| 1173 | uint64_t size) |
| 1174 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1175 | CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1176 | |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1177 | cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size)); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1178 | QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link); |
| 1179 | memory_region_update_coalesced_range(mr); |
| 1180 | } |
| 1181 | |
| 1182 | void memory_region_clear_coalescing(MemoryRegion *mr) |
| 1183 | { |
| 1184 | CoalescedMemoryRange *cmr; |
| 1185 | |
| 1186 | while (!QTAILQ_EMPTY(&mr->coalesced)) { |
| 1187 | cmr = QTAILQ_FIRST(&mr->coalesced); |
| 1188 | QTAILQ_REMOVE(&mr->coalesced, cmr, link); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1189 | g_free(cmr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1190 | } |
| 1191 | memory_region_update_coalesced_range(mr); |
| 1192 | } |
| 1193 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1194 | void memory_region_add_eventfd(MemoryRegion *mr, |
| 1195 | target_phys_addr_t addr, |
| 1196 | unsigned size, |
| 1197 | bool match_data, |
| 1198 | uint64_t data, |
| 1199 | int fd) |
| 1200 | { |
| 1201 | MemoryRegionIoeventfd mrfd = { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1202 | .addr.start = int128_make64(addr), |
| 1203 | .addr.size = int128_make64(size), |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1204 | .match_data = match_data, |
| 1205 | .data = data, |
| 1206 | .fd = fd, |
| 1207 | }; |
| 1208 | unsigned i; |
| 1209 | |
| 1210 | for (i = 0; i < mr->ioeventfd_nb; ++i) { |
| 1211 | if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) { |
| 1212 | break; |
| 1213 | } |
| 1214 | } |
| 1215 | ++mr->ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1216 | mr->ioeventfds = g_realloc(mr->ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1217 | sizeof(*mr->ioeventfds) * mr->ioeventfd_nb); |
| 1218 | memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i], |
| 1219 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i)); |
| 1220 | mr->ioeventfds[i] = mrfd; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1221 | memory_region_update_topology(mr); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | void memory_region_del_eventfd(MemoryRegion *mr, |
| 1225 | target_phys_addr_t addr, |
| 1226 | unsigned size, |
| 1227 | bool match_data, |
| 1228 | uint64_t data, |
| 1229 | int fd) |
| 1230 | { |
| 1231 | MemoryRegionIoeventfd mrfd = { |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1232 | .addr.start = int128_make64(addr), |
| 1233 | .addr.size = int128_make64(size), |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1234 | .match_data = match_data, |
| 1235 | .data = data, |
| 1236 | .fd = fd, |
| 1237 | }; |
| 1238 | unsigned i; |
| 1239 | |
| 1240 | for (i = 0; i < mr->ioeventfd_nb; ++i) { |
| 1241 | if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) { |
| 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | assert(i != mr->ioeventfd_nb); |
| 1246 | memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1], |
| 1247 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1))); |
| 1248 | --mr->ioeventfd_nb; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1249 | mr->ioeventfds = g_realloc(mr->ioeventfds, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1250 | sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1251 | memory_region_update_topology(mr); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1252 | } |
| 1253 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1254 | static void memory_region_add_subregion_common(MemoryRegion *mr, |
| 1255 | target_phys_addr_t offset, |
| 1256 | MemoryRegion *subregion) |
| 1257 | { |
| 1258 | MemoryRegion *other; |
| 1259 | |
| 1260 | assert(!subregion->parent); |
| 1261 | subregion->parent = mr; |
| 1262 | subregion->addr = offset; |
| 1263 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link) { |
| 1264 | if (subregion->may_overlap || other->may_overlap) { |
| 1265 | continue; |
| 1266 | } |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1267 | if (int128_gt(int128_make64(offset), |
| 1268 | int128_add(int128_make64(other->addr), other->size)) |
| 1269 | || int128_le(int128_add(int128_make64(offset), subregion->size), |
| 1270 | int128_make64(other->addr))) { |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1271 | continue; |
| 1272 | } |
Anthony Liguori | a5e1cbc | 2011-08-22 11:14:56 -0500 | [diff] [blame] | 1273 | #if 0 |
Michael Walle | 860329b | 2011-09-15 23:16:49 +0200 | [diff] [blame] | 1274 | printf("warning: subregion collision %llx/%llx (%s) " |
| 1275 | "vs %llx/%llx (%s)\n", |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1276 | (unsigned long long)offset, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1277 | (unsigned long long)int128_get64(subregion->size), |
Michael Walle | 860329b | 2011-09-15 23:16:49 +0200 | [diff] [blame] | 1278 | subregion->name, |
| 1279 | (unsigned long long)other->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1280 | (unsigned long long)int128_get64(other->size), |
Michael Walle | 860329b | 2011-09-15 23:16:49 +0200 | [diff] [blame] | 1281 | other->name); |
Anthony Liguori | a5e1cbc | 2011-08-22 11:14:56 -0500 | [diff] [blame] | 1282 | #endif |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1283 | } |
| 1284 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link) { |
| 1285 | if (subregion->priority >= other->priority) { |
| 1286 | QTAILQ_INSERT_BEFORE(other, subregion, subregions_link); |
| 1287 | goto done; |
| 1288 | } |
| 1289 | } |
| 1290 | QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link); |
| 1291 | done: |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1292 | memory_region_update_topology(mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | void memory_region_add_subregion(MemoryRegion *mr, |
| 1297 | target_phys_addr_t offset, |
| 1298 | MemoryRegion *subregion) |
| 1299 | { |
| 1300 | subregion->may_overlap = false; |
| 1301 | subregion->priority = 0; |
| 1302 | memory_region_add_subregion_common(mr, offset, subregion); |
| 1303 | } |
| 1304 | |
| 1305 | void memory_region_add_subregion_overlap(MemoryRegion *mr, |
| 1306 | target_phys_addr_t offset, |
| 1307 | MemoryRegion *subregion, |
| 1308 | unsigned priority) |
| 1309 | { |
| 1310 | subregion->may_overlap = true; |
| 1311 | subregion->priority = priority; |
| 1312 | memory_region_add_subregion_common(mr, offset, subregion); |
| 1313 | } |
| 1314 | |
| 1315 | void memory_region_del_subregion(MemoryRegion *mr, |
| 1316 | MemoryRegion *subregion) |
| 1317 | { |
| 1318 | assert(subregion->parent == mr); |
| 1319 | subregion->parent = NULL; |
| 1320 | QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link); |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1321 | memory_region_update_topology(mr); |
| 1322 | } |
| 1323 | |
| 1324 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled) |
| 1325 | { |
| 1326 | if (enabled == mr->enabled) { |
| 1327 | return; |
| 1328 | } |
| 1329 | mr->enabled = enabled; |
| 1330 | memory_region_update_topology(NULL); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1331 | } |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 1332 | |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1333 | void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr) |
| 1334 | { |
| 1335 | MemoryRegion *parent = mr->parent; |
| 1336 | unsigned priority = mr->priority; |
| 1337 | bool may_overlap = mr->may_overlap; |
| 1338 | |
| 1339 | if (addr == mr->addr || !parent) { |
| 1340 | mr->addr = addr; |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | memory_region_transaction_begin(); |
| 1345 | memory_region_del_subregion(parent, mr); |
| 1346 | if (may_overlap) { |
| 1347 | memory_region_add_subregion_overlap(parent, addr, mr, priority); |
| 1348 | } else { |
| 1349 | memory_region_add_subregion(parent, addr, mr); |
| 1350 | } |
| 1351 | memory_region_transaction_commit(); |
| 1352 | } |
| 1353 | |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1354 | void memory_region_set_alias_offset(MemoryRegion *mr, target_phys_addr_t offset) |
| 1355 | { |
| 1356 | target_phys_addr_t old_offset = mr->alias_offset; |
| 1357 | |
| 1358 | assert(mr->alias); |
| 1359 | mr->alias_offset = offset; |
| 1360 | |
| 1361 | if (offset == old_offset || !mr->parent) { |
| 1362 | return; |
| 1363 | } |
| 1364 | |
| 1365 | memory_region_update_topology(mr); |
| 1366 | } |
| 1367 | |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 1368 | void set_system_memory_map(MemoryRegion *mr) |
| 1369 | { |
Avi Kivity | cc31e6e | 2011-07-26 14:26:05 +0300 | [diff] [blame] | 1370 | address_space_memory.root = mr; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1371 | memory_region_update_topology(NULL); |
Avi Kivity | 1c0ffa5 | 2011-07-26 14:26:04 +0300 | [diff] [blame] | 1372 | } |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 1373 | |
| 1374 | void set_system_io_map(MemoryRegion *mr) |
| 1375 | { |
| 1376 | address_space_io.root = mr; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1377 | memory_region_update_topology(NULL); |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 1378 | } |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1379 | |
| 1380 | typedef struct MemoryRegionList MemoryRegionList; |
| 1381 | |
| 1382 | struct MemoryRegionList { |
| 1383 | const MemoryRegion *mr; |
| 1384 | bool printed; |
| 1385 | QTAILQ_ENTRY(MemoryRegionList) queue; |
| 1386 | }; |
| 1387 | |
| 1388 | typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead; |
| 1389 | |
| 1390 | static void mtree_print_mr(fprintf_function mon_printf, void *f, |
| 1391 | const MemoryRegion *mr, unsigned int level, |
| 1392 | target_phys_addr_t base, |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1393 | MemoryRegionListHead *alias_print_queue) |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1394 | { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1395 | MemoryRegionList *new_ml, *ml, *next_ml; |
| 1396 | MemoryRegionListHead submr_print_queue; |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1397 | const MemoryRegion *submr; |
| 1398 | unsigned int i; |
| 1399 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1400 | if (!mr) { |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | for (i = 0; i < level; i++) { |
| 1405 | mon_printf(f, " "); |
| 1406 | } |
| 1407 | |
| 1408 | if (mr->alias) { |
| 1409 | MemoryRegionList *ml; |
| 1410 | bool found = false; |
| 1411 | |
| 1412 | /* check if the alias is already in the queue */ |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1413 | QTAILQ_FOREACH(ml, alias_print_queue, queue) { |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1414 | if (ml->mr == mr->alias && !ml->printed) { |
| 1415 | found = true; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | if (!found) { |
| 1420 | ml = g_new(MemoryRegionList, 1); |
| 1421 | ml->mr = mr->alias; |
| 1422 | ml->printed = false; |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1423 | QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1424 | } |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 1425 | mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): alias %s @%s " |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1426 | TARGET_FMT_plx "-" TARGET_FMT_plx "\n", |
| 1427 | base + mr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1428 | base + mr->addr |
| 1429 | + (target_phys_addr_t)int128_get64(mr->size) - 1, |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 1430 | mr->priority, |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1431 | mr->name, |
| 1432 | mr->alias->name, |
| 1433 | mr->alias_offset, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1434 | mr->alias_offset |
| 1435 | + (target_phys_addr_t)int128_get64(mr->size) - 1); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1436 | } else { |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 1437 | mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): %s\n", |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1438 | base + mr->addr, |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 1439 | base + mr->addr |
| 1440 | + (target_phys_addr_t)int128_get64(mr->size) - 1, |
Jan Kiszka | 4b474ba | 2011-09-27 15:00:31 +0200 | [diff] [blame] | 1441 | mr->priority, |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1442 | mr->name); |
| 1443 | } |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1444 | |
| 1445 | QTAILQ_INIT(&submr_print_queue); |
| 1446 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1447 | QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1448 | new_ml = g_new(MemoryRegionList, 1); |
| 1449 | new_ml->mr = submr; |
| 1450 | QTAILQ_FOREACH(ml, &submr_print_queue, queue) { |
| 1451 | if (new_ml->mr->addr < ml->mr->addr || |
| 1452 | (new_ml->mr->addr == ml->mr->addr && |
| 1453 | new_ml->mr->priority > ml->mr->priority)) { |
| 1454 | QTAILQ_INSERT_BEFORE(ml, new_ml, queue); |
| 1455 | new_ml = NULL; |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | if (new_ml) { |
| 1460 | QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | QTAILQ_FOREACH(ml, &submr_print_queue, queue) { |
| 1465 | mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr, |
| 1466 | alias_print_queue); |
| 1467 | } |
| 1468 | |
Avi Kivity | 88365e4 | 2011-11-13 12:00:55 +0200 | [diff] [blame] | 1469 | QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) { |
Jan Kiszka | 9479c57 | 2011-09-27 15:00:41 +0200 | [diff] [blame] | 1470 | g_free(ml); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | void mtree_info(fprintf_function mon_printf, void *f) |
| 1475 | { |
| 1476 | MemoryRegionListHead ml_head; |
| 1477 | MemoryRegionList *ml, *ml2; |
| 1478 | |
| 1479 | QTAILQ_INIT(&ml_head); |
| 1480 | |
| 1481 | mon_printf(f, "memory\n"); |
| 1482 | mtree_print_mr(mon_printf, f, address_space_memory.root, 0, 0, &ml_head); |
| 1483 | |
| 1484 | /* print aliased regions */ |
| 1485 | QTAILQ_FOREACH(ml, &ml_head, queue) { |
| 1486 | if (!ml->printed) { |
| 1487 | mon_printf(f, "%s\n", ml->mr->name); |
| 1488 | mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) { |
Avi Kivity | 88365e4 | 2011-11-13 12:00:55 +0200 | [diff] [blame] | 1493 | g_free(ml); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Jan Kiszka | 0663181 | 2011-09-27 15:00:38 +0200 | [diff] [blame] | 1496 | if (address_space_io.root && |
| 1497 | !QTAILQ_EMPTY(&address_space_io.root->subregions)) { |
| 1498 | QTAILQ_INIT(&ml_head); |
| 1499 | mon_printf(f, "I/O\n"); |
| 1500 | mtree_print_mr(mon_printf, f, address_space_io.root, 0, 0, &ml_head); |
| 1501 | } |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1502 | } |