blob: 87639ab6ea175f8b83d681632fcf960f1879b724 [file] [log] [blame]
Avi Kivity093bc2c2011-07-26 14:26:01 +03001/*
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 Kivity1c0ffa52011-07-26 14:26:04 +030015#include "exec-memory.h"
Avi Kivity658b2222011-07-26 14:26:08 +030016#include "ioport.h"
Avi Kivity74901c32011-07-26 14:26:10 +030017#include "bitops.h"
Avi Kivity3e9d69e2011-07-26 14:26:11 +030018#include "kvm.h"
Avi Kivity093bc2c2011-07-26 14:26:01 +030019#include <assert.h>
20
Avi Kivity4ef4db82011-07-26 14:26:13 +030021unsigned memory_region_transaction_depth = 0;
Avi Kivitye87c0992011-09-14 12:16:20 +030022static bool memory_region_update_pending = false;
Avi Kivity4ef4db82011-07-26 14:26:13 +030023
Avi Kivity093bc2c2011-07-26 14:26:01 +030024typedef struct AddrRange AddrRange;
25
Avi Kivity8417ceb2011-08-03 11:56:14 +030026/*
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 Kivity093bc2c2011-07-26 14:26:01 +030031struct AddrRange {
Avi Kivity08dafab2011-10-16 13:19:17 +020032 Int128 start;
33 Int128 size;
Avi Kivity093bc2c2011-07-26 14:26:01 +030034};
35
Avi Kivity08dafab2011-10-16 13:19:17 +020036static AddrRange addrrange_make(Int128 start, Int128 size)
Avi Kivity093bc2c2011-07-26 14:26:01 +030037{
38 return (AddrRange) { start, size };
39}
40
41static bool addrrange_equal(AddrRange r1, AddrRange r2)
42{
Avi Kivity08dafab2011-10-16 13:19:17 +020043 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
Avi Kivity093bc2c2011-07-26 14:26:01 +030044}
45
Avi Kivity08dafab2011-10-16 13:19:17 +020046static Int128 addrrange_end(AddrRange r)
Avi Kivity093bc2c2011-07-26 14:26:01 +030047{
Avi Kivity08dafab2011-10-16 13:19:17 +020048 return int128_add(r.start, r.size);
Avi Kivity093bc2c2011-07-26 14:26:01 +030049}
50
Avi Kivity08dafab2011-10-16 13:19:17 +020051static AddrRange addrrange_shift(AddrRange range, Int128 delta)
Avi Kivity093bc2c2011-07-26 14:26:01 +030052{
Avi Kivity08dafab2011-10-16 13:19:17 +020053 int128_addto(&range.start, delta);
Avi Kivity093bc2c2011-07-26 14:26:01 +030054 return range;
55}
56
Avi Kivity08dafab2011-10-16 13:19:17 +020057static 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 Kivity093bc2c2011-07-26 14:26:01 +030063static bool addrrange_intersects(AddrRange r1, AddrRange r2)
64{
Avi Kivity08dafab2011-10-16 13:19:17 +020065 return addrrange_contains(r1, r2.start)
66 || addrrange_contains(r2, r1.start);
Avi Kivity093bc2c2011-07-26 14:26:01 +030067}
68
69static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
70{
Avi Kivity08dafab2011-10-16 13:19:17 +020071 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 Kivity093bc2c2011-07-26 14:26:01 +030074}
75
76struct CoalescedMemoryRange {
77 AddrRange addr;
78 QTAILQ_ENTRY(CoalescedMemoryRange) link;
79};
80
Avi Kivity3e9d69e2011-07-26 14:26:11 +030081struct MemoryRegionIoeventfd {
82 AddrRange addr;
83 bool match_data;
84 uint64_t data;
85 int fd;
86};
87
88static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
89 MemoryRegionIoeventfd b)
90{
Avi Kivity08dafab2011-10-16 13:19:17 +020091 if (int128_lt(a.addr.start, b.addr.start)) {
Avi Kivity3e9d69e2011-07-26 14:26:11 +030092 return true;
Avi Kivity08dafab2011-10-16 13:19:17 +020093 } else if (int128_gt(a.addr.start, b.addr.start)) {
Avi Kivity3e9d69e2011-07-26 14:26:11 +030094 return false;
Avi Kivity08dafab2011-10-16 13:19:17 +020095 } else if (int128_lt(a.addr.size, b.addr.size)) {
Avi Kivity3e9d69e2011-07-26 14:26:11 +030096 return true;
Avi Kivity08dafab2011-10-16 13:19:17 +020097 } else if (int128_gt(a.addr.size, b.addr.size)) {
Avi Kivity3e9d69e2011-07-26 14:26:11 +030098 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
118static 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 Kivity093bc2c2011-07-26 14:26:01 +0300125typedef struct FlatRange FlatRange;
126typedef struct FlatView FlatView;
127
128/* Range of memory in the global map. Addresses are absolute. */
129struct FlatRange {
130 MemoryRegion *mr;
131 target_phys_addr_t offset_in_region;
132 AddrRange addr;
Avi Kivity5a583342011-07-26 14:26:02 +0300133 uint8_t dirty_log_mask;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300134 bool readable;
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300135 bool readonly;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300136};
137
138/* Flattened global view of current active memory hierarchy. Kept in sorted
139 * order.
140 */
141struct FlatView {
142 FlatRange *ranges;
143 unsigned nr;
144 unsigned nr_allocated;
145};
146
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300147typedef struct AddressSpace AddressSpace;
148typedef struct AddressSpaceOps AddressSpaceOps;
149
150/* A system address space - I/O, memory, etc. */
151struct AddressSpace {
152 const AddressSpaceOps *ops;
153 MemoryRegion *root;
154 FlatView current_map;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300155 int ioeventfd_nb;
156 MemoryRegionIoeventfd *ioeventfds;
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300157};
158
159struct 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 Kivity3e9d69e2011-07-26 14:26:11 +0300164 void (*ioeventfd_add)(AddressSpace *as, MemoryRegionIoeventfd *fd);
165 void (*ioeventfd_del)(AddressSpace *as, MemoryRegionIoeventfd *fd);
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300166};
167
Avi Kivity093bc2c2011-07-26 14:26:01 +0300168#define FOR_EACH_FLAT_RANGE(var, view) \
169 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
170
Avi Kivity093bc2c2011-07-26 14:26:01 +0300171static bool flatrange_equal(FlatRange *a, FlatRange *b)
172{
173 return a->mr == b->mr
174 && addrrange_equal(a->addr, b->addr)
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300175 && a->offset_in_region == b->offset_in_region
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300176 && a->readable == b->readable
177 && a->readonly == b->readonly;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300178}
179
180static 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 */
190static 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 Liguori7267c092011-08-20 22:09:37 -0500194 view->ranges = g_realloc(view->ranges,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300195 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
203static void flatview_destroy(FlatView *view)
204{
Anthony Liguori7267c092011-08-20 22:09:37 -0500205 g_free(view->ranges);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300206}
207
Avi Kivity3d8e6bf2011-07-26 14:26:03 +0300208static bool can_merge(FlatRange *r1, FlatRange *r2)
209{
Avi Kivity08dafab2011-10-16 13:19:17 +0200210 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
Avi Kivity3d8e6bf2011-07-26 14:26:03 +0300211 && r1->mr == r2->mr
Avi Kivity08dafab2011-10-16 13:19:17 +0200212 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
213 r1->addr.size),
214 int128_make64(r2->offset_in_region))
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300215 && r1->dirty_log_mask == r2->dirty_log_mask
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300216 && r1->readable == r2->readable
217 && r1->readonly == r2->readonly;
Avi Kivity3d8e6bf2011-07-26 14:26:03 +0300218}
219
220/* Attempt to simplify a view by merging ajacent ranges */
221static 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 Kivity08dafab2011-10-16 13:19:17 +0200230 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
Avi Kivity3d8e6bf2011-07-26 14:26:03 +0300231 ++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 Kivity164a4dc2011-08-11 10:40:25 +0300240static 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
254static 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
268static 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 Kivity16ef61c2011-07-26 14:26:07 +0300299static void memory_region_prepare_ram_addr(MemoryRegion *mr);
300
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300301static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
302{
303 ram_addr_t phys_offset, region_offset;
304
Avi Kivity16ef61c2011-07-26 14:26:07 +0300305 memory_region_prepare_ram_addr(fr->mr);
306
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300307 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 Kivityd0a9b5b2011-08-08 19:58:49 +0300317 if (!fr->readable) {
Avi Kivityb5fe14c2011-08-29 09:12:49 +0300318 phys_offset &= ~TARGET_PAGE_MASK & ~IO_MEM_ROMD;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300319 }
320
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300321 if (fr->readonly) {
322 phys_offset |= IO_MEM_ROM;
323 }
324
Avi Kivity08dafab2011-10-16 13:19:17 +0200325 cpu_register_physical_memory_log(int128_get64(fr->addr.start),
326 int128_get64(fr->addr.size),
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300327 phys_offset,
328 region_offset,
329 fr->dirty_log_mask);
330}
331
332static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
333{
Avi Kivity39b796f2011-07-31 22:47:58 +0300334 if (fr->dirty_log_mask) {
Avi Kivity08dafab2011-10-16 13:19:17 +0200335 Int128 end = addrrange_end(fr->addr);
336 cpu_physical_sync_dirty_bitmap(int128_get64(fr->addr.start),
337 int128_get64(end));
Avi Kivity39b796f2011-07-31 22:47:58 +0300338 }
Avi Kivity08dafab2011-10-16 13:19:17 +0200339 cpu_register_physical_memory(int128_get64(fr->addr.start),
340 int128_get64(fr->addr.size),
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300341 IO_MEM_UNASSIGNED);
342}
343
344static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
345{
Avi Kivity08dafab2011-10-16 13:19:17 +0200346 cpu_physical_log_start(int128_get64(fr->addr.start),
347 int128_get64(fr->addr.size));
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300348}
349
350static void as_memory_log_stop(AddressSpace *as, FlatRange *fr)
351{
Avi Kivity08dafab2011-10-16 13:19:17 +0200352 cpu_physical_log_stop(int128_get64(fr->addr.start),
353 int128_get64(fr->addr.size));
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300354}
355
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300356static void as_memory_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
357{
358 int r;
359
Avi Kivity08dafab2011-10-16 13:19:17 +0200360 assert(fd->match_data && int128_get64(fd->addr.size) == 4);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300361
Avi Kivity08dafab2011-10-16 13:19:17 +0200362 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
363 fd->data, true);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300364 if (r < 0) {
365 abort();
366 }
367}
368
369static void as_memory_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
370{
371 int r;
372
Avi Kivity08dafab2011-10-16 13:19:17 +0200373 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
374 fd->data, false);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300375 if (r < 0) {
376 abort();
377 }
378}
379
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300380static 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 Kivity3e9d69e2011-07-26 14:26:11 +0300385 .ioeventfd_add = as_memory_ioeventfd_add,
386 .ioeventfd_del = as_memory_ioeventfd_del,
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300387};
388
389static AddressSpace address_space_memory = {
390 .ops = &address_space_ops_memory,
391};
392
Avi Kivity627a0e92011-07-26 14:26:09 +0300393static 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 Kivity658b2222011-07-26 14:26:08 +0300408static 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 Kivity627a0e92011-07-26 14:26:09 +0300415 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 Kivity6bf9fd42011-09-26 14:52:26 +0300420 *data = mrp->read(mr->opaque, offset + mr->offset);
Jan Kiszka03808f52011-09-18 14:51:58 +0200421 } 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 Kivity627a0e92011-07-26 14:26:09 +0300426 }
427 return;
428 }
Avi Kivity3a130f42011-08-11 10:40:26 +0300429 *data = 0;
Avi Kivity6bf9fd42011-09-26 14:52:26 +0300430 access_with_adjusted_size(offset + mr->offset, data, width,
Avi Kivity3a130f42011-08-11 10:40:26 +0300431 mr->ops->impl.min_access_size,
432 mr->ops->impl.max_access_size,
433 memory_region_read_accessor, mr);
Avi Kivity658b2222011-07-26 14:26:08 +0300434}
435
436static 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 Kivity627a0e92011-07-26 14:26:09 +0300443 if (mr->ops->old_portio) {
444 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true);
445
446 if (mrp) {
Avi Kivity6bf9fd42011-09-26 14:52:26 +0300447 mrp->write(mr->opaque, offset + mr->offset, data);
Jan Kiszka03808f52011-09-18 14:51:58 +0200448 } 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 Kivity627a0e92011-07-26 14:26:09 +0300453 }
454 return;
455 }
Avi Kivity6bf9fd42011-09-26 14:52:26 +0300456 access_with_adjusted_size(offset + mr->offset, &data, width,
Avi Kivity3a130f42011-08-11 10:40:26 +0300457 mr->ops->impl.min_access_size,
458 mr->ops->impl.max_access_size,
459 memory_region_write_accessor, mr);
Avi Kivity658b2222011-07-26 14:26:08 +0300460}
461
462static const IORangeOps memory_region_iorange_ops = {
463 .read = memory_region_iorange_read,
464 .write = memory_region_iorange_write,
465};
466
467static void as_io_range_add(AddressSpace *as, FlatRange *fr)
468{
469 iorange_init(&fr->mr->iorange, &memory_region_iorange_ops,
Avi Kivity08dafab2011-10-16 13:19:17 +0200470 int128_get64(fr->addr.start), int128_get64(fr->addr.size));
Avi Kivity658b2222011-07-26 14:26:08 +0300471 ioport_register(&fr->mr->iorange);
472}
473
474static void as_io_range_del(AddressSpace *as, FlatRange *fr)
475{
Avi Kivity08dafab2011-10-16 13:19:17 +0200476 isa_unassign_ioport(int128_get64(fr->addr.start),
477 int128_get64(fr->addr.size));
Avi Kivity658b2222011-07-26 14:26:08 +0300478}
479
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300480static void as_io_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
481{
482 int r;
483
Avi Kivity08dafab2011-10-16 13:19:17 +0200484 assert(fd->match_data && int128_get64(fd->addr.size) == 2);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300485
Avi Kivity08dafab2011-10-16 13:19:17 +0200486 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
487 fd->data, true);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300488 if (r < 0) {
489 abort();
490 }
491}
492
493static void as_io_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
494{
495 int r;
496
Avi Kivity08dafab2011-10-16 13:19:17 +0200497 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
498 fd->data, false);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300499 if (r < 0) {
500 abort();
501 }
502}
503
Avi Kivity658b2222011-07-26 14:26:08 +0300504static const AddressSpaceOps address_space_ops_io = {
505 .range_add = as_io_range_add,
506 .range_del = as_io_range_del,
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300507 .ioeventfd_add = as_io_ioeventfd_add,
508 .ioeventfd_del = as_io_ioeventfd_del,
Avi Kivity658b2222011-07-26 14:26:08 +0300509};
510
511static AddressSpace address_space_io = {
512 .ops = &address_space_ops_io,
513};
514
Avi Kivity093bc2c2011-07-26 14:26:01 +0300515/* Render a memory region into the global view. Ranges in @view obscure
516 * ranges in @mr.
517 */
518static void render_memory_region(FlatView *view,
519 MemoryRegion *mr,
Avi Kivity08dafab2011-10-16 13:19:17 +0200520 Int128 base,
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300521 AddrRange clip,
522 bool readonly)
Avi Kivity093bc2c2011-07-26 14:26:01 +0300523{
524 MemoryRegion *subregion;
525 unsigned i;
526 target_phys_addr_t offset_in_region;
Avi Kivity08dafab2011-10-16 13:19:17 +0200527 Int128 remain;
528 Int128 now;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300529 FlatRange fr;
530 AddrRange tmp;
531
Avi Kivity6bba19b2011-09-14 11:54:58 +0300532 if (!mr->enabled) {
533 return;
534 }
535
Avi Kivity08dafab2011-10-16 13:19:17 +0200536 int128_addto(&base, int128_make64(mr->addr));
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300537 readonly |= mr->readonly;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300538
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 Kivity08dafab2011-10-16 13:19:17 +0200548 int128_subfrom(&base, int128_make64(mr->alias->addr));
549 int128_subfrom(&base, int128_make64(mr->alias_offset));
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300550 render_memory_region(view, mr->alias, base, clip, readonly);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300551 return;
552 }
553
554 /* Render subregions in priority order. */
555 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300556 render_memory_region(view, subregion, base, clip, readonly);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300557 }
558
Avi Kivity14a3c102011-07-26 14:26:06 +0300559 if (!mr->terminates) {
Avi Kivity093bc2c2011-07-26 14:26:01 +0300560 return;
561 }
562
Avi Kivity08dafab2011-10-16 13:19:17 +0200563 offset_in_region = int128_get64(int128_sub(clip.start, base));
Avi Kivity093bc2c2011-07-26 14:26:01 +0300564 base = clip.start;
565 remain = clip.size;
566
567 /* Render the region itself into any gaps left by the current view. */
Avi Kivity08dafab2011-10-16 13:19:17 +0200568 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
569 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
Avi Kivity093bc2c2011-07-26 14:26:01 +0300570 continue;
571 }
Avi Kivity08dafab2011-10-16 13:19:17 +0200572 if (int128_lt(base, view->ranges[i].addr.start)) {
573 now = int128_min(remain,
574 int128_sub(view->ranges[i].addr.start, base));
Avi Kivity093bc2c2011-07-26 14:26:01 +0300575 fr.mr = mr;
576 fr.offset_in_region = offset_in_region;
577 fr.addr = addrrange_make(base, now);
Avi Kivity5a583342011-07-26 14:26:02 +0300578 fr.dirty_log_mask = mr->dirty_log_mask;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300579 fr.readable = mr->readable;
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300580 fr.readonly = readonly;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300581 flatview_insert(view, i, &fr);
582 ++i;
Avi Kivity08dafab2011-10-16 13:19:17 +0200583 int128_addto(&base, now);
584 offset_in_region += int128_get64(now);
585 int128_subfrom(&remain, now);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300586 }
Avi Kivity08dafab2011-10-16 13:19:17 +0200587 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 Kivity093bc2c2011-07-26 14:26:01 +0300592 }
593 }
Avi Kivity08dafab2011-10-16 13:19:17 +0200594 if (int128_nz(remain)) {
Avi Kivity093bc2c2011-07-26 14:26:01 +0300595 fr.mr = mr;
596 fr.offset_in_region = offset_in_region;
597 fr.addr = addrrange_make(base, remain);
Avi Kivity5a583342011-07-26 14:26:02 +0300598 fr.dirty_log_mask = mr->dirty_log_mask;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300599 fr.readable = mr->readable;
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300600 fr.readonly = readonly;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300601 flatview_insert(view, i, &fr);
602 }
603}
604
605/* Render a memory topology into a list of disjoint absolute ranges. */
606static FlatView generate_memory_topology(MemoryRegion *mr)
607{
608 FlatView view;
609
610 flatview_init(&view);
611
Avi Kivity08dafab2011-10-16 13:19:17 +0200612 render_memory_region(&view, mr, int128_zero(),
613 addrrange_make(int128_zero(), int128_2_64()), false);
Avi Kivity3d8e6bf2011-07-26 14:26:03 +0300614 flatview_simplify(&view);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300615
616 return view;
617}
618
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300619static 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
652static 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 Kivity08dafab2011-10-16 13:19:17 +0200663 int128_sub(fr->addr.start,
664 int128_make64(fr->offset_in_region)));
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300665 if (addrrange_intersects(fr->addr, tmp)) {
666 ++ioeventfd_nb;
Anthony Liguori7267c092011-08-20 22:09:37 -0500667 ioeventfds = g_realloc(ioeventfds,
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300668 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 Liguori7267c092011-08-20 22:09:37 -0500678 g_free(as->ioeventfds);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300679 as->ioeventfds = ioeventfds;
680 as->ioeventfd_nb = ioeventfd_nb;
681}
682
Avi Kivityb8af1af2011-07-26 14:26:12 +0300683static void address_space_update_topology_pass(AddressSpace *as,
684 FlatView old_view,
685 FlatView new_view,
686 bool adding)
Avi Kivity093bc2c2011-07-26 14:26:01 +0300687{
Avi Kivity093bc2c2011-07-26 14:26:01 +0300688 unsigned iold, inew;
689 FlatRange *frold, *frnew;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300690
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 Kivity08dafab2011-10-16 13:19:17 +0200709 || int128_lt(frold->addr.start, frnew->addr.start)
710 || (int128_eq(frold->addr.start, frnew->addr.start)
Avi Kivity093bc2c2011-07-26 14:26:01 +0300711 && !flatrange_equal(frold, frnew)))) {
712 /* In old, but (not in new, or in new but attributes changed). */
713
Avi Kivityb8af1af2011-07-26 14:26:12 +0300714 if (!adding) {
715 as->ops->range_del(as, frold);
716 }
717
Avi Kivity093bc2c2011-07-26 14:26:01 +0300718 ++iold;
719 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
720 /* In both (logging may have changed) */
721
Avi Kivityb8af1af2011-07-26 14:26:12 +0300722 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 Kivity5a583342011-07-26 14:26:02 +0300728 }
729
Avi Kivity093bc2c2011-07-26 14:26:01 +0300730 ++iold;
731 ++inew;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300732 } else {
733 /* In new */
734
Avi Kivityb8af1af2011-07-26 14:26:12 +0300735 if (adding) {
736 as->ops->range_add(as, frnew);
737 }
738
Avi Kivity093bc2c2011-07-26 14:26:01 +0300739 ++inew;
740 }
741 }
Avi Kivityb8af1af2011-07-26 14:26:12 +0300742}
743
744
745static 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 Kivitycc31e6e2011-07-26 14:26:05 +0300753 as->current_map = new_view;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300754 flatview_destroy(&old_view);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300755 address_space_update_ioeventfds(as);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300756}
757
Avi Kivity6bba19b2011-09-14 11:54:58 +0300758static void memory_region_update_topology(MemoryRegion *mr)
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300759{
Avi Kivity4ef4db82011-07-26 14:26:13 +0300760 if (memory_region_transaction_depth) {
Avi Kivitye87c0992011-09-14 12:16:20 +0300761 memory_region_update_pending |= !mr || mr->enabled;
Avi Kivity4ef4db82011-07-26 14:26:13 +0300762 return;
763 }
764
Avi Kivity6bba19b2011-09-14 11:54:58 +0300765 if (mr && !mr->enabled) {
766 return;
767 }
768
Avi Kivity658b2222011-07-26 14:26:08 +0300769 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 Kivitye87c0992011-09-14 12:16:20 +0300775
776 memory_region_update_pending = false;
Avi Kivitycc31e6e2011-07-26 14:26:05 +0300777}
778
Avi Kivity4ef4db82011-07-26 14:26:13 +0300779void memory_region_transaction_begin(void)
780{
781 ++memory_region_transaction_depth;
782}
783
784void memory_region_transaction_commit(void)
785{
786 assert(memory_region_transaction_depth);
787 --memory_region_transaction_depth;
Avi Kivitye87c0992011-09-14 12:16:20 +0300788 if (!memory_region_transaction_depth && memory_region_update_pending) {
789 memory_region_update_topology(NULL);
790 }
Avi Kivity4ef4db82011-07-26 14:26:13 +0300791}
792
Avi Kivity545e92e2011-08-08 19:58:48 +0300793static void memory_region_destructor_none(MemoryRegion *mr)
794{
795}
796
797static void memory_region_destructor_ram(MemoryRegion *mr)
798{
799 qemu_ram_free(mr->ram_addr);
800}
801
802static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
803{
804 qemu_ram_free_from_ptr(mr->ram_addr);
805}
806
807static void memory_region_destructor_iomem(MemoryRegion *mr)
808{
809 cpu_unregister_io_memory(mr->ram_addr);
810}
811
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300812static 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 Kivity093bc2c2011-07-26 14:26:01 +0300818void memory_region_init(MemoryRegion *mr,
819 const char *name,
820 uint64_t size)
821{
822 mr->ops = NULL;
823 mr->parent = NULL;
Avi Kivity08dafab2011-10-16 13:19:17 +0200824 mr->size = int128_make64(size);
825 if (size == UINT64_MAX) {
826 mr->size = int128_2_64();
827 }
Avi Kivity093bc2c2011-07-26 14:26:01 +0300828 mr->addr = 0;
829 mr->offset = 0;
Avi Kivity6bba19b2011-09-14 11:54:58 +0300830 mr->enabled = true;
Avi Kivity14a3c102011-07-26 14:26:06 +0300831 mr->terminates = false;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300832 mr->readable = true;
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300833 mr->readonly = false;
Avi Kivity545e92e2011-08-08 19:58:48 +0300834 mr->destructor = memory_region_destructor_none;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300835 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 Liguori7267c092011-08-20 22:09:37 -0500841 mr->name = g_strdup(name);
Avi Kivity5a583342011-07-26 14:26:02 +0300842 mr->dirty_log_mask = 0;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300843 mr->ioeventfd_nb = 0;
844 mr->ioeventfds = NULL;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300845}
846
847static bool memory_region_access_valid(MemoryRegion *mr,
848 target_phys_addr_t addr,
Avi Kivity897fa7c2011-11-13 13:05:27 +0200849 unsigned size,
850 bool is_write)
Avi Kivity093bc2c2011-07-26 14:26:01 +0300851{
Avi Kivity897fa7c2011-11-13 13:05:27 +0200852 if (mr->ops->valid.accepts
853 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
854 return false;
855 }
856
Avi Kivity093bc2c2011-07-26 14:26:01 +0300857 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
873static uint32_t memory_region_read_thunk_n(void *_mr,
874 target_phys_addr_t addr,
875 unsigned size)
876{
877 MemoryRegion *mr = _mr;
Avi Kivity164a4dc2011-08-11 10:40:25 +0300878 uint64_t data = 0;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300879
Avi Kivity897fa7c2011-11-13 13:05:27 +0200880 if (!memory_region_access_valid(mr, addr, size, false)) {
Avi Kivity093bc2c2011-07-26 14:26:01 +0300881 return -1U; /* FIXME: better signalling */
882 }
883
Avi Kivity74901c32011-07-26 14:26:10 +0300884 if (!mr->ops->read) {
885 return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr);
886 }
887
Avi Kivity093bc2c2011-07-26 14:26:01 +0300888 /* FIXME: support unaligned access */
Avi Kivity164a4dc2011-08-11 10:40:25 +0300889 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 Kivity093bc2c2011-07-26 14:26:01 +0300893
894 return data;
895}
896
897static 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 Kivity093bc2c2011-07-26 14:26:01 +0300903
Avi Kivity897fa7c2011-11-13 13:05:27 +0200904 if (!memory_region_access_valid(mr, addr, size, true)) {
Avi Kivity093bc2c2011-07-26 14:26:01 +0300905 return; /* FIXME: better signalling */
906 }
907
Avi Kivity74901c32011-07-26 14:26:10 +0300908 if (!mr->ops->write) {
909 mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data);
910 return;
911 }
912
Avi Kivity093bc2c2011-07-26 14:26:01 +0300913 /* FIXME: support unaligned access */
Avi Kivity164a4dc2011-08-11 10:40:25 +0300914 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 Kivity093bc2c2011-07-26 14:26:01 +0300918}
919
920static 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
925static 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
930static 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
935static 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
941static 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
947static 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
953static 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
959static 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 Kivity16ef61c2011-07-26 14:26:07 +0300965static void memory_region_prepare_ram_addr(MemoryRegion *mr)
966{
967 if (mr->backend_registered) {
968 return;
969 }
970
Avi Kivity545e92e2011-08-08 19:58:48 +0300971 mr->destructor = memory_region_destructor_iomem;
Avi Kivity16ef61c2011-07-26 14:26:07 +0300972 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 Kivity093bc2c2011-07-26 14:26:01 +0300979void 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 Kivity14a3c102011-07-26 14:26:06 +0300988 mr->terminates = true;
Avi Kivity16ef61c2011-07-26 14:26:07 +0300989 mr->backend_registered = false;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300990}
991
992void 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 Kivity14a3c102011-07-26 14:26:06 +0300998 mr->terminates = true;
Avi Kivity545e92e2011-08-08 19:58:48 +0300999 mr->destructor = memory_region_destructor_ram;
Avi Kivity093bc2c2011-07-26 14:26:01 +03001000 mr->ram_addr = qemu_ram_alloc(dev, name, size);
Avi Kivity16ef61c2011-07-26 14:26:07 +03001001 mr->backend_registered = true;
Avi Kivity093bc2c2011-07-26 14:26:01 +03001002}
1003
1004void 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 Kivity14a3c102011-07-26 14:26:06 +03001011 mr->terminates = true;
Avi Kivity545e92e2011-08-08 19:58:48 +03001012 mr->destructor = memory_region_destructor_ram_from_ptr;
Avi Kivity093bc2c2011-07-26 14:26:01 +03001013 mr->ram_addr = qemu_ram_alloc_from_ptr(dev, name, size, ptr);
Avi Kivity16ef61c2011-07-26 14:26:07 +03001014 mr->backend_registered = true;
Avi Kivity093bc2c2011-07-26 14:26:01 +03001015}
1016
1017void 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 Kivityd0a9b5b2011-08-08 19:58:49 +03001028void memory_region_init_rom_device(MemoryRegion *mr,
1029 const MemoryRegionOps *ops,
Avi Kivity75f59412011-08-26 00:35:15 +03001030 void *opaque,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001031 DeviceState *dev,
1032 const char *name,
1033 uint64_t size)
1034{
1035 memory_region_init(mr, name, size);
Avi Kivity7bc2b9c2011-08-25 14:56:14 +03001036 mr->ops = ops;
Avi Kivity75f59412011-08-26 00:35:15 +03001037 mr->opaque = opaque;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001038 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 Kivity093bc2c2011-07-26 14:26:01 +03001049void memory_region_destroy(MemoryRegion *mr)
1050{
1051 assert(QTAILQ_EMPTY(&mr->subregions));
Avi Kivity545e92e2011-08-08 19:58:48 +03001052 mr->destructor(mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001053 memory_region_clear_coalescing(mr);
Anthony Liguori7267c092011-08-20 22:09:37 -05001054 g_free((char *)mr->name);
1055 g_free(mr->ioeventfds);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001056}
1057
1058uint64_t memory_region_size(MemoryRegion *mr)
1059{
Avi Kivity08dafab2011-10-16 13:19:17 +02001060 if (int128_eq(mr->size, int128_2_64())) {
1061 return UINT64_MAX;
1062 }
1063 return int128_get64(mr->size);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001064}
1065
1066void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset)
1067{
1068 mr->offset = offset;
1069}
1070
1071void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1072{
Avi Kivity5a583342011-07-26 14:26:02 +03001073 uint8_t mask = 1 << client;
1074
1075 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
Avi Kivity6bba19b2011-09-14 11:54:58 +03001076 memory_region_update_topology(mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001077}
1078
1079bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1080 unsigned client)
1081{
Avi Kivity14a3c102011-07-26 14:26:06 +03001082 assert(mr->terminates);
Avi Kivity5a583342011-07-26 14:26:02 +03001083 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, 1 << client);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001084}
1085
1086void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr)
1087{
Avi Kivity14a3c102011-07-26 14:26:06 +03001088 assert(mr->terminates);
Avi Kivity5a583342011-07-26 14:26:02 +03001089 return cpu_physical_memory_set_dirty(mr->ram_addr + addr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001090}
1091
1092void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1093{
Avi Kivity5a583342011-07-26 14:26:02 +03001094 FlatRange *fr;
1095
Avi Kivitycc31e6e2011-07-26 14:26:05 +03001096 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
Avi Kivity5a583342011-07-26 14:26:02 +03001097 if (fr->mr == mr) {
Avi Kivity08dafab2011-10-16 13:19:17 +02001098 cpu_physical_sync_dirty_bitmap(int128_get64(fr->addr.start),
1099 int128_get64(addrrange_end(fr->addr)));
Avi Kivity5a583342011-07-26 14:26:02 +03001100 }
1101 }
Avi Kivity093bc2c2011-07-26 14:26:01 +03001102}
1103
1104void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1105{
Avi Kivityfb1cd6f2011-09-25 14:48:47 +03001106 if (mr->readonly != readonly) {
1107 mr->readonly = readonly;
Avi Kivity6bba19b2011-09-14 11:54:58 +03001108 memory_region_update_topology(mr);
Avi Kivityfb1cd6f2011-09-25 14:48:47 +03001109 }
Avi Kivity093bc2c2011-07-26 14:26:01 +03001110}
1111
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001112void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
1113{
1114 if (mr->readable != readable) {
1115 mr->readable = readable;
Avi Kivity6bba19b2011-09-14 11:54:58 +03001116 memory_region_update_topology(mr);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001117 }
1118}
1119
Avi Kivity093bc2c2011-07-26 14:26:01 +03001120void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1121 target_phys_addr_t size, unsigned client)
1122{
Avi Kivity14a3c102011-07-26 14:26:06 +03001123 assert(mr->terminates);
Avi Kivity5a583342011-07-26 14:26:02 +03001124 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1125 mr->ram_addr + addr + size,
1126 1 << client);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001127}
1128
1129void *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 Kivity14a3c102011-07-26 14:26:06 +03001135 assert(mr->terminates);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001136
Jan Kiszka021d26d2011-08-30 00:38:24 +02001137 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001138}
1139
1140static void memory_region_update_coalesced_range(MemoryRegion *mr)
1141{
1142 FlatRange *fr;
1143 CoalescedMemoryRange *cmr;
1144 AddrRange tmp;
1145
Avi Kivitycc31e6e2011-07-26 14:26:05 +03001146 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
Avi Kivity093bc2c2011-07-26 14:26:01 +03001147 if (fr->mr == mr) {
Avi Kivity08dafab2011-10-16 13:19:17 +02001148 qemu_unregister_coalesced_mmio(int128_get64(fr->addr.start),
1149 int128_get64(fr->addr.size));
Avi Kivity093bc2c2011-07-26 14:26:01 +03001150 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1151 tmp = addrrange_shift(cmr->addr,
Avi Kivity08dafab2011-10-16 13:19:17 +02001152 int128_sub(fr->addr.start,
1153 int128_make64(fr->offset_in_region)));
Avi Kivity093bc2c2011-07-26 14:26:01 +03001154 if (!addrrange_intersects(tmp, fr->addr)) {
1155 continue;
1156 }
1157 tmp = addrrange_intersection(tmp, fr->addr);
Avi Kivity08dafab2011-10-16 13:19:17 +02001158 qemu_register_coalesced_mmio(int128_get64(tmp.start),
1159 int128_get64(tmp.size));
Avi Kivity093bc2c2011-07-26 14:26:01 +03001160 }
1161 }
1162 }
1163}
1164
1165void memory_region_set_coalescing(MemoryRegion *mr)
1166{
1167 memory_region_clear_coalescing(mr);
Avi Kivity08dafab2011-10-16 13:19:17 +02001168 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
Avi Kivity093bc2c2011-07-26 14:26:01 +03001169}
1170
1171void memory_region_add_coalescing(MemoryRegion *mr,
1172 target_phys_addr_t offset,
1173 uint64_t size)
1174{
Anthony Liguori7267c092011-08-20 22:09:37 -05001175 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
Avi Kivity093bc2c2011-07-26 14:26:01 +03001176
Avi Kivity08dafab2011-10-16 13:19:17 +02001177 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
Avi Kivity093bc2c2011-07-26 14:26:01 +03001178 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1179 memory_region_update_coalesced_range(mr);
1180}
1181
1182void 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 Liguori7267c092011-08-20 22:09:37 -05001189 g_free(cmr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001190 }
1191 memory_region_update_coalesced_range(mr);
1192}
1193
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001194void 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 Kivity08dafab2011-10-16 13:19:17 +02001202 .addr.start = int128_make64(addr),
1203 .addr.size = int128_make64(size),
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001204 .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 Liguori7267c092011-08-20 22:09:37 -05001216 mr->ioeventfds = g_realloc(mr->ioeventfds,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001217 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 Kivity6bba19b2011-09-14 11:54:58 +03001221 memory_region_update_topology(mr);
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001222}
1223
1224void 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 Kivity08dafab2011-10-16 13:19:17 +02001232 .addr.start = int128_make64(addr),
1233 .addr.size = int128_make64(size),
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001234 .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 Liguori7267c092011-08-20 22:09:37 -05001249 mr->ioeventfds = g_realloc(mr->ioeventfds,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001250 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
Avi Kivity6bba19b2011-09-14 11:54:58 +03001251 memory_region_update_topology(mr);
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001252}
1253
Avi Kivity093bc2c2011-07-26 14:26:01 +03001254static 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 Kivity08dafab2011-10-16 13:19:17 +02001267 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 Kivity093bc2c2011-07-26 14:26:01 +03001271 continue;
1272 }
Anthony Liguoria5e1cbc2011-08-22 11:14:56 -05001273#if 0
Michael Walle860329b2011-09-15 23:16:49 +02001274 printf("warning: subregion collision %llx/%llx (%s) "
1275 "vs %llx/%llx (%s)\n",
Avi Kivity093bc2c2011-07-26 14:26:01 +03001276 (unsigned long long)offset,
Avi Kivity08dafab2011-10-16 13:19:17 +02001277 (unsigned long long)int128_get64(subregion->size),
Michael Walle860329b2011-09-15 23:16:49 +02001278 subregion->name,
1279 (unsigned long long)other->addr,
Avi Kivity08dafab2011-10-16 13:19:17 +02001280 (unsigned long long)int128_get64(other->size),
Michael Walle860329b2011-09-15 23:16:49 +02001281 other->name);
Anthony Liguoria5e1cbc2011-08-22 11:14:56 -05001282#endif
Avi Kivity093bc2c2011-07-26 14:26:01 +03001283 }
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);
1291done:
Avi Kivity6bba19b2011-09-14 11:54:58 +03001292 memory_region_update_topology(mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001293}
1294
1295
1296void 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
1305void 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
1315void 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 Kivity6bba19b2011-09-14 11:54:58 +03001321 memory_region_update_topology(mr);
1322}
1323
1324void 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 Kivity093bc2c2011-07-26 14:26:01 +03001331}
Avi Kivity1c0ffa52011-07-26 14:26:04 +03001332
Avi Kivity2282e1a2011-09-14 12:10:12 +03001333void 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 Kivity47033592011-12-04 19:16:50 +02001354void 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 Kivity1c0ffa52011-07-26 14:26:04 +03001368void set_system_memory_map(MemoryRegion *mr)
1369{
Avi Kivitycc31e6e2011-07-26 14:26:05 +03001370 address_space_memory.root = mr;
Avi Kivity6bba19b2011-09-14 11:54:58 +03001371 memory_region_update_topology(NULL);
Avi Kivity1c0ffa52011-07-26 14:26:04 +03001372}
Avi Kivity658b2222011-07-26 14:26:08 +03001373
1374void set_system_io_map(MemoryRegion *mr)
1375{
1376 address_space_io.root = mr;
Avi Kivity6bba19b2011-09-14 11:54:58 +03001377 memory_region_update_topology(NULL);
Avi Kivity658b2222011-07-26 14:26:08 +03001378}
Blue Swirl314e2982011-09-11 20:22:05 +00001379
1380typedef struct MemoryRegionList MemoryRegionList;
1381
1382struct MemoryRegionList {
1383 const MemoryRegion *mr;
1384 bool printed;
1385 QTAILQ_ENTRY(MemoryRegionList) queue;
1386};
1387
1388typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1389
1390static void mtree_print_mr(fprintf_function mon_printf, void *f,
1391 const MemoryRegion *mr, unsigned int level,
1392 target_phys_addr_t base,
Jan Kiszka9479c572011-09-27 15:00:41 +02001393 MemoryRegionListHead *alias_print_queue)
Blue Swirl314e2982011-09-11 20:22:05 +00001394{
Jan Kiszka9479c572011-09-27 15:00:41 +02001395 MemoryRegionList *new_ml, *ml, *next_ml;
1396 MemoryRegionListHead submr_print_queue;
Blue Swirl314e2982011-09-11 20:22:05 +00001397 const MemoryRegion *submr;
1398 unsigned int i;
1399
Blue Swirl314e2982011-09-11 20:22:05 +00001400 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 Kiszka9479c572011-09-27 15:00:41 +02001413 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
Blue Swirl314e2982011-09-11 20:22:05 +00001414 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 Kiszka9479c572011-09-27 15:00:41 +02001423 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
Blue Swirl314e2982011-09-11 20:22:05 +00001424 }
Jan Kiszka4b474ba2011-09-27 15:00:31 +02001425 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): alias %s @%s "
Blue Swirl314e2982011-09-11 20:22:05 +00001426 TARGET_FMT_plx "-" TARGET_FMT_plx "\n",
1427 base + mr->addr,
Avi Kivity08dafab2011-10-16 13:19:17 +02001428 base + mr->addr
1429 + (target_phys_addr_t)int128_get64(mr->size) - 1,
Jan Kiszka4b474ba2011-09-27 15:00:31 +02001430 mr->priority,
Blue Swirl314e2982011-09-11 20:22:05 +00001431 mr->name,
1432 mr->alias->name,
1433 mr->alias_offset,
Avi Kivity08dafab2011-10-16 13:19:17 +02001434 mr->alias_offset
1435 + (target_phys_addr_t)int128_get64(mr->size) - 1);
Blue Swirl314e2982011-09-11 20:22:05 +00001436 } else {
Jan Kiszka4b474ba2011-09-27 15:00:31 +02001437 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): %s\n",
Blue Swirl314e2982011-09-11 20:22:05 +00001438 base + mr->addr,
Avi Kivity08dafab2011-10-16 13:19:17 +02001439 base + mr->addr
1440 + (target_phys_addr_t)int128_get64(mr->size) - 1,
Jan Kiszka4b474ba2011-09-27 15:00:31 +02001441 mr->priority,
Blue Swirl314e2982011-09-11 20:22:05 +00001442 mr->name);
1443 }
Jan Kiszka9479c572011-09-27 15:00:41 +02001444
1445 QTAILQ_INIT(&submr_print_queue);
1446
Blue Swirl314e2982011-09-11 20:22:05 +00001447 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
Jan Kiszka9479c572011-09-27 15:00:41 +02001448 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 Kivity88365e42011-11-13 12:00:55 +02001469 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
Jan Kiszka9479c572011-09-27 15:00:41 +02001470 g_free(ml);
Blue Swirl314e2982011-09-11 20:22:05 +00001471 }
1472}
1473
1474void 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 Kivity88365e42011-11-13 12:00:55 +02001493 g_free(ml);
Blue Swirl314e2982011-09-11 20:22:05 +00001494 }
1495
Jan Kiszka06631812011-09-27 15:00:38 +02001496 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 Swirl314e2982011-09-11 20:22:05 +00001502}