Fix vmstate_register_ram() for rom/device regions
[qemu/ar7.git] / memory.c
blob5ab21129a011f3672a7223086a4e076c9bc90f56
1 /*
2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "memory.h"
15 #include "exec-memory.h"
16 #include "ioport.h"
17 #include "bitops.h"
18 #include "kvm.h"
19 #include <assert.h>
21 #define WANT_EXEC_OBSOLETE
22 #include "exec-obsolete.h"
24 unsigned memory_region_transaction_depth = 0;
25 static bool memory_region_update_pending = false;
26 static bool global_dirty_log = false;
28 static QLIST_HEAD(, MemoryListener) memory_listeners
29 = QLIST_HEAD_INITIALIZER(memory_listeners);
31 typedef struct AddrRange AddrRange;
34 * Note using signed integers limits us to physical addresses at most
35 * 63 bits wide. They are needed for negative offsetting in aliases
36 * (large MemoryRegion::alias_offset).
38 struct AddrRange {
39 Int128 start;
40 Int128 size;
43 static AddrRange addrrange_make(Int128 start, Int128 size)
45 return (AddrRange) { start, size };
48 static bool addrrange_equal(AddrRange r1, AddrRange r2)
50 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
53 static Int128 addrrange_end(AddrRange r)
55 return int128_add(r.start, r.size);
58 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
60 int128_addto(&range.start, delta);
61 return range;
64 static bool addrrange_contains(AddrRange range, Int128 addr)
66 return int128_ge(addr, range.start)
67 && int128_lt(addr, addrrange_end(range));
70 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
72 return addrrange_contains(r1, r2.start)
73 || addrrange_contains(r2, r1.start);
76 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
78 Int128 start = int128_max(r1.start, r2.start);
79 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
80 return addrrange_make(start, int128_sub(end, start));
83 struct CoalescedMemoryRange {
84 AddrRange addr;
85 QTAILQ_ENTRY(CoalescedMemoryRange) link;
88 struct MemoryRegionIoeventfd {
89 AddrRange addr;
90 bool match_data;
91 uint64_t data;
92 int fd;
95 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
96 MemoryRegionIoeventfd b)
98 if (int128_lt(a.addr.start, b.addr.start)) {
99 return true;
100 } else if (int128_gt(a.addr.start, b.addr.start)) {
101 return false;
102 } else if (int128_lt(a.addr.size, b.addr.size)) {
103 return true;
104 } else if (int128_gt(a.addr.size, b.addr.size)) {
105 return false;
106 } else if (a.match_data < b.match_data) {
107 return true;
108 } else if (a.match_data > b.match_data) {
109 return false;
110 } else if (a.match_data) {
111 if (a.data < b.data) {
112 return true;
113 } else if (a.data > b.data) {
114 return false;
117 if (a.fd < b.fd) {
118 return true;
119 } else if (a.fd > b.fd) {
120 return false;
122 return false;
125 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
126 MemoryRegionIoeventfd b)
128 return !memory_region_ioeventfd_before(a, b)
129 && !memory_region_ioeventfd_before(b, a);
132 typedef struct FlatRange FlatRange;
133 typedef struct FlatView FlatView;
135 /* Range of memory in the global map. Addresses are absolute. */
136 struct FlatRange {
137 MemoryRegion *mr;
138 target_phys_addr_t offset_in_region;
139 AddrRange addr;
140 uint8_t dirty_log_mask;
141 bool readable;
142 bool readonly;
145 /* Flattened global view of current active memory hierarchy. Kept in sorted
146 * order.
148 struct FlatView {
149 FlatRange *ranges;
150 unsigned nr;
151 unsigned nr_allocated;
154 typedef struct AddressSpace AddressSpace;
155 typedef struct AddressSpaceOps AddressSpaceOps;
157 /* A system address space - I/O, memory, etc. */
158 struct AddressSpace {
159 const AddressSpaceOps *ops;
160 MemoryRegion *root;
161 FlatView current_map;
162 int ioeventfd_nb;
163 MemoryRegionIoeventfd *ioeventfds;
166 struct AddressSpaceOps {
167 void (*range_add)(AddressSpace *as, FlatRange *fr);
168 void (*range_del)(AddressSpace *as, FlatRange *fr);
169 void (*log_start)(AddressSpace *as, FlatRange *fr);
170 void (*log_stop)(AddressSpace *as, FlatRange *fr);
171 void (*ioeventfd_add)(AddressSpace *as, MemoryRegionIoeventfd *fd);
172 void (*ioeventfd_del)(AddressSpace *as, MemoryRegionIoeventfd *fd);
175 #define FOR_EACH_FLAT_RANGE(var, view) \
176 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
178 static bool flatrange_equal(FlatRange *a, FlatRange *b)
180 return a->mr == b->mr
181 && addrrange_equal(a->addr, b->addr)
182 && a->offset_in_region == b->offset_in_region
183 && a->readable == b->readable
184 && a->readonly == b->readonly;
187 static void flatview_init(FlatView *view)
189 view->ranges = NULL;
190 view->nr = 0;
191 view->nr_allocated = 0;
194 /* Insert a range into a given position. Caller is responsible for maintaining
195 * sorting order.
197 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
199 if (view->nr == view->nr_allocated) {
200 view->nr_allocated = MAX(2 * view->nr, 10);
201 view->ranges = g_realloc(view->ranges,
202 view->nr_allocated * sizeof(*view->ranges));
204 memmove(view->ranges + pos + 1, view->ranges + pos,
205 (view->nr - pos) * sizeof(FlatRange));
206 view->ranges[pos] = *range;
207 ++view->nr;
210 static void flatview_destroy(FlatView *view)
212 g_free(view->ranges);
215 static bool can_merge(FlatRange *r1, FlatRange *r2)
217 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
218 && r1->mr == r2->mr
219 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
220 r1->addr.size),
221 int128_make64(r2->offset_in_region))
222 && r1->dirty_log_mask == r2->dirty_log_mask
223 && r1->readable == r2->readable
224 && r1->readonly == r2->readonly;
227 /* Attempt to simplify a view by merging ajacent ranges */
228 static void flatview_simplify(FlatView *view)
230 unsigned i, j;
232 i = 0;
233 while (i < view->nr) {
234 j = i + 1;
235 while (j < view->nr
236 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
237 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
238 ++j;
240 ++i;
241 memmove(&view->ranges[i], &view->ranges[j],
242 (view->nr - j) * sizeof(view->ranges[j]));
243 view->nr -= j - i;
247 static void memory_region_read_accessor(void *opaque,
248 target_phys_addr_t addr,
249 uint64_t *value,
250 unsigned size,
251 unsigned shift,
252 uint64_t mask)
254 MemoryRegion *mr = opaque;
255 uint64_t tmp;
257 tmp = mr->ops->read(mr->opaque, addr, size);
258 *value |= (tmp & mask) << shift;
261 static void memory_region_write_accessor(void *opaque,
262 target_phys_addr_t addr,
263 uint64_t *value,
264 unsigned size,
265 unsigned shift,
266 uint64_t mask)
268 MemoryRegion *mr = opaque;
269 uint64_t tmp;
271 tmp = (*value >> shift) & mask;
272 mr->ops->write(mr->opaque, addr, tmp, size);
275 static void access_with_adjusted_size(target_phys_addr_t addr,
276 uint64_t *value,
277 unsigned size,
278 unsigned access_size_min,
279 unsigned access_size_max,
280 void (*access)(void *opaque,
281 target_phys_addr_t addr,
282 uint64_t *value,
283 unsigned size,
284 unsigned shift,
285 uint64_t mask),
286 void *opaque)
288 uint64_t access_mask;
289 unsigned access_size;
290 unsigned i;
292 if (!access_size_min) {
293 access_size_min = 1;
295 if (!access_size_max) {
296 access_size_max = 4;
298 access_size = MAX(MIN(size, access_size_max), access_size_min);
299 access_mask = -1ULL >> (64 - access_size * 8);
300 for (i = 0; i < size; i += access_size) {
301 /* FIXME: big-endian support */
302 access(opaque, addr + i, value, access_size, i * 8, access_mask);
306 static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
308 MemoryRegionSection section = {
309 .mr = fr->mr,
310 .offset_within_address_space = int128_get64(fr->addr.start),
311 .offset_within_region = fr->offset_in_region,
312 .size = int128_get64(fr->addr.size),
315 cpu_register_physical_memory_log(&section, fr->readable, fr->readonly);
318 static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
320 MemoryRegionSection section = {
321 .mr = &io_mem_unassigned,
322 .offset_within_address_space = int128_get64(fr->addr.start),
323 .offset_within_region = int128_get64(fr->addr.start),
324 .size = int128_get64(fr->addr.size),
327 cpu_register_physical_memory_log(&section, true, false);
330 static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
334 static void as_memory_log_stop(AddressSpace *as, FlatRange *fr)
338 static void as_memory_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
340 int r;
342 assert(fd->match_data && int128_get64(fd->addr.size) == 4);
344 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
345 fd->data, true);
346 if (r < 0) {
347 abort();
351 static void as_memory_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
353 int r;
355 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
356 fd->data, false);
357 if (r < 0) {
358 abort();
362 static const AddressSpaceOps address_space_ops_memory = {
363 .range_add = as_memory_range_add,
364 .range_del = as_memory_range_del,
365 .log_start = as_memory_log_start,
366 .log_stop = as_memory_log_stop,
367 .ioeventfd_add = as_memory_ioeventfd_add,
368 .ioeventfd_del = as_memory_ioeventfd_del,
371 static AddressSpace address_space_memory = {
372 .ops = &address_space_ops_memory,
375 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
376 unsigned width, bool write)
378 const MemoryRegionPortio *mrp;
380 for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
381 if (offset >= mrp->offset && offset < mrp->offset + mrp->len
382 && width == mrp->size
383 && (write ? (bool)mrp->write : (bool)mrp->read)) {
384 return mrp;
387 return NULL;
390 static void memory_region_iorange_read(IORange *iorange,
391 uint64_t offset,
392 unsigned width,
393 uint64_t *data)
395 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
397 if (mr->ops->old_portio) {
398 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, false);
400 *data = ((uint64_t)1 << (width * 8)) - 1;
401 if (mrp) {
402 *data = mrp->read(mr->opaque, offset + mr->offset);
403 } else if (width == 2) {
404 mrp = find_portio(mr, offset, 1, false);
405 assert(mrp);
406 *data = mrp->read(mr->opaque, offset + mr->offset) |
407 (mrp->read(mr->opaque, offset + mr->offset + 1) << 8);
409 return;
411 *data = 0;
412 access_with_adjusted_size(offset + mr->offset, data, width,
413 mr->ops->impl.min_access_size,
414 mr->ops->impl.max_access_size,
415 memory_region_read_accessor, mr);
418 static void memory_region_iorange_write(IORange *iorange,
419 uint64_t offset,
420 unsigned width,
421 uint64_t data)
423 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
425 if (mr->ops->old_portio) {
426 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true);
428 if (mrp) {
429 mrp->write(mr->opaque, offset + mr->offset, data);
430 } else if (width == 2) {
431 mrp = find_portio(mr, offset, 1, false);
432 assert(mrp);
433 mrp->write(mr->opaque, offset + mr->offset, data & 0xff);
434 mrp->write(mr->opaque, offset + mr->offset + 1, data >> 8);
436 return;
438 access_with_adjusted_size(offset + mr->offset, &data, width,
439 mr->ops->impl.min_access_size,
440 mr->ops->impl.max_access_size,
441 memory_region_write_accessor, mr);
444 static const IORangeOps memory_region_iorange_ops = {
445 .read = memory_region_iorange_read,
446 .write = memory_region_iorange_write,
449 static void as_io_range_add(AddressSpace *as, FlatRange *fr)
451 iorange_init(&fr->mr->iorange, &memory_region_iorange_ops,
452 int128_get64(fr->addr.start), int128_get64(fr->addr.size));
453 ioport_register(&fr->mr->iorange);
456 static void as_io_range_del(AddressSpace *as, FlatRange *fr)
458 isa_unassign_ioport(int128_get64(fr->addr.start),
459 int128_get64(fr->addr.size));
462 static void as_io_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
464 int r;
466 assert(fd->match_data && int128_get64(fd->addr.size) == 2);
468 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
469 fd->data, true);
470 if (r < 0) {
471 abort();
475 static void as_io_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
477 int r;
479 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
480 fd->data, false);
481 if (r < 0) {
482 abort();
486 static const AddressSpaceOps address_space_ops_io = {
487 .range_add = as_io_range_add,
488 .range_del = as_io_range_del,
489 .ioeventfd_add = as_io_ioeventfd_add,
490 .ioeventfd_del = as_io_ioeventfd_del,
493 static AddressSpace address_space_io = {
494 .ops = &address_space_ops_io,
497 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
499 while (mr->parent) {
500 mr = mr->parent;
502 if (mr == address_space_memory.root) {
503 return &address_space_memory;
505 if (mr == address_space_io.root) {
506 return &address_space_io;
508 abort();
511 /* Render a memory region into the global view. Ranges in @view obscure
512 * ranges in @mr.
514 static void render_memory_region(FlatView *view,
515 MemoryRegion *mr,
516 Int128 base,
517 AddrRange clip,
518 bool readonly)
520 MemoryRegion *subregion;
521 unsigned i;
522 target_phys_addr_t offset_in_region;
523 Int128 remain;
524 Int128 now;
525 FlatRange fr;
526 AddrRange tmp;
528 if (!mr->enabled) {
529 return;
532 int128_addto(&base, int128_make64(mr->addr));
533 readonly |= mr->readonly;
535 tmp = addrrange_make(base, mr->size);
537 if (!addrrange_intersects(tmp, clip)) {
538 return;
541 clip = addrrange_intersection(tmp, clip);
543 if (mr->alias) {
544 int128_subfrom(&base, int128_make64(mr->alias->addr));
545 int128_subfrom(&base, int128_make64(mr->alias_offset));
546 render_memory_region(view, mr->alias, base, clip, readonly);
547 return;
550 /* Render subregions in priority order. */
551 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
552 render_memory_region(view, subregion, base, clip, readonly);
555 if (!mr->terminates) {
556 return;
559 offset_in_region = int128_get64(int128_sub(clip.start, base));
560 base = clip.start;
561 remain = clip.size;
563 /* Render the region itself into any gaps left by the current view. */
564 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
565 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
566 continue;
568 if (int128_lt(base, view->ranges[i].addr.start)) {
569 now = int128_min(remain,
570 int128_sub(view->ranges[i].addr.start, base));
571 fr.mr = mr;
572 fr.offset_in_region = offset_in_region;
573 fr.addr = addrrange_make(base, now);
574 fr.dirty_log_mask = mr->dirty_log_mask;
575 fr.readable = mr->readable;
576 fr.readonly = readonly;
577 flatview_insert(view, i, &fr);
578 ++i;
579 int128_addto(&base, now);
580 offset_in_region += int128_get64(now);
581 int128_subfrom(&remain, now);
583 if (int128_eq(base, view->ranges[i].addr.start)) {
584 now = int128_min(remain, view->ranges[i].addr.size);
585 int128_addto(&base, now);
586 offset_in_region += int128_get64(now);
587 int128_subfrom(&remain, now);
590 if (int128_nz(remain)) {
591 fr.mr = mr;
592 fr.offset_in_region = offset_in_region;
593 fr.addr = addrrange_make(base, remain);
594 fr.dirty_log_mask = mr->dirty_log_mask;
595 fr.readable = mr->readable;
596 fr.readonly = readonly;
597 flatview_insert(view, i, &fr);
601 /* Render a memory topology into a list of disjoint absolute ranges. */
602 static FlatView generate_memory_topology(MemoryRegion *mr)
604 FlatView view;
606 flatview_init(&view);
608 render_memory_region(&view, mr, int128_zero(),
609 addrrange_make(int128_zero(), int128_2_64()), false);
610 flatview_simplify(&view);
612 return view;
615 static void address_space_add_del_ioeventfds(AddressSpace *as,
616 MemoryRegionIoeventfd *fds_new,
617 unsigned fds_new_nb,
618 MemoryRegionIoeventfd *fds_old,
619 unsigned fds_old_nb)
621 unsigned iold, inew;
623 /* Generate a symmetric difference of the old and new fd sets, adding
624 * and deleting as necessary.
627 iold = inew = 0;
628 while (iold < fds_old_nb || inew < fds_new_nb) {
629 if (iold < fds_old_nb
630 && (inew == fds_new_nb
631 || memory_region_ioeventfd_before(fds_old[iold],
632 fds_new[inew]))) {
633 as->ops->ioeventfd_del(as, &fds_old[iold]);
634 ++iold;
635 } else if (inew < fds_new_nb
636 && (iold == fds_old_nb
637 || memory_region_ioeventfd_before(fds_new[inew],
638 fds_old[iold]))) {
639 as->ops->ioeventfd_add(as, &fds_new[inew]);
640 ++inew;
641 } else {
642 ++iold;
643 ++inew;
648 static void address_space_update_ioeventfds(AddressSpace *as)
650 FlatRange *fr;
651 unsigned ioeventfd_nb = 0;
652 MemoryRegionIoeventfd *ioeventfds = NULL;
653 AddrRange tmp;
654 unsigned i;
656 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
657 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
658 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
659 int128_sub(fr->addr.start,
660 int128_make64(fr->offset_in_region)));
661 if (addrrange_intersects(fr->addr, tmp)) {
662 ++ioeventfd_nb;
663 ioeventfds = g_realloc(ioeventfds,
664 ioeventfd_nb * sizeof(*ioeventfds));
665 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
666 ioeventfds[ioeventfd_nb-1].addr = tmp;
671 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
672 as->ioeventfds, as->ioeventfd_nb);
674 g_free(as->ioeventfds);
675 as->ioeventfds = ioeventfds;
676 as->ioeventfd_nb = ioeventfd_nb;
679 typedef void ListenerCallback(MemoryListener *listener,
680 MemoryRegionSection *mrs);
682 /* Want "void (&MemoryListener::*callback)(const MemoryRegionSection& s)" */
683 static void memory_listener_update_region(FlatRange *fr, AddressSpace *as,
684 size_t callback_offset)
686 MemoryRegionSection section = {
687 .mr = fr->mr,
688 .address_space = as->root,
689 .offset_within_region = fr->offset_in_region,
690 .size = int128_get64(fr->addr.size),
691 .offset_within_address_space = int128_get64(fr->addr.start),
693 MemoryListener *listener;
695 QLIST_FOREACH(listener, &memory_listeners, link) {
696 ListenerCallback *callback
697 = *(ListenerCallback **)((void *)listener + callback_offset);
698 callback(listener, &section);
702 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, callback) \
703 memory_listener_update_region(fr, as, offsetof(MemoryListener, callback))
705 static void address_space_update_topology_pass(AddressSpace *as,
706 FlatView old_view,
707 FlatView new_view,
708 bool adding)
710 unsigned iold, inew;
711 FlatRange *frold, *frnew;
713 /* Generate a symmetric difference of the old and new memory maps.
714 * Kill ranges in the old map, and instantiate ranges in the new map.
716 iold = inew = 0;
717 while (iold < old_view.nr || inew < new_view.nr) {
718 if (iold < old_view.nr) {
719 frold = &old_view.ranges[iold];
720 } else {
721 frold = NULL;
723 if (inew < new_view.nr) {
724 frnew = &new_view.ranges[inew];
725 } else {
726 frnew = NULL;
729 if (frold
730 && (!frnew
731 || int128_lt(frold->addr.start, frnew->addr.start)
732 || (int128_eq(frold->addr.start, frnew->addr.start)
733 && !flatrange_equal(frold, frnew)))) {
734 /* In old, but (not in new, or in new but attributes changed). */
736 if (!adding) {
737 MEMORY_LISTENER_UPDATE_REGION(frold, as, region_del);
738 as->ops->range_del(as, frold);
741 ++iold;
742 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
743 /* In both (logging may have changed) */
745 if (adding) {
746 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
747 MEMORY_LISTENER_UPDATE_REGION(frnew, as, log_stop);
748 as->ops->log_stop(as, frnew);
749 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
750 as->ops->log_start(as, frnew);
751 MEMORY_LISTENER_UPDATE_REGION(frnew, as, log_start);
755 ++iold;
756 ++inew;
757 } else {
758 /* In new */
760 if (adding) {
761 as->ops->range_add(as, frnew);
762 MEMORY_LISTENER_UPDATE_REGION(frnew, as, region_add);
765 ++inew;
771 static void address_space_update_topology(AddressSpace *as)
773 FlatView old_view = as->current_map;
774 FlatView new_view = generate_memory_topology(as->root);
776 address_space_update_topology_pass(as, old_view, new_view, false);
777 address_space_update_topology_pass(as, old_view, new_view, true);
779 as->current_map = new_view;
780 flatview_destroy(&old_view);
781 address_space_update_ioeventfds(as);
784 static void memory_region_update_topology(MemoryRegion *mr)
786 if (memory_region_transaction_depth) {
787 memory_region_update_pending |= !mr || mr->enabled;
788 return;
791 if (mr && !mr->enabled) {
792 return;
795 if (address_space_memory.root) {
796 address_space_update_topology(&address_space_memory);
798 if (address_space_io.root) {
799 address_space_update_topology(&address_space_io);
802 memory_region_update_pending = false;
805 void memory_region_transaction_begin(void)
807 ++memory_region_transaction_depth;
810 void memory_region_transaction_commit(void)
812 assert(memory_region_transaction_depth);
813 --memory_region_transaction_depth;
814 if (!memory_region_transaction_depth && memory_region_update_pending) {
815 memory_region_update_topology(NULL);
819 static void memory_region_destructor_none(MemoryRegion *mr)
823 static void memory_region_destructor_ram(MemoryRegion *mr)
825 qemu_ram_free(mr->ram_addr);
828 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
830 qemu_ram_free_from_ptr(mr->ram_addr);
833 static void memory_region_destructor_iomem(MemoryRegion *mr)
835 cpu_unregister_io_memory(mr->ram_addr);
838 static void memory_region_destructor_rom_device(MemoryRegion *mr)
840 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
841 cpu_unregister_io_memory(mr->ram_addr & ~TARGET_PAGE_MASK);
844 static bool memory_region_wrong_endianness(MemoryRegion *mr)
846 #ifdef TARGET_WORDS_BIGENDIAN
847 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
848 #else
849 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
850 #endif
853 void memory_region_init(MemoryRegion *mr,
854 const char *name,
855 uint64_t size)
857 mr->ops = NULL;
858 mr->parent = NULL;
859 mr->size = int128_make64(size);
860 if (size == UINT64_MAX) {
861 mr->size = int128_2_64();
863 mr->addr = 0;
864 mr->offset = 0;
865 mr->subpage = false;
866 mr->enabled = true;
867 mr->terminates = false;
868 mr->ram = false;
869 mr->readable = true;
870 mr->readonly = false;
871 mr->rom_device = false;
872 mr->destructor = memory_region_destructor_none;
873 mr->priority = 0;
874 mr->may_overlap = false;
875 mr->alias = NULL;
876 QTAILQ_INIT(&mr->subregions);
877 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
878 QTAILQ_INIT(&mr->coalesced);
879 mr->name = g_strdup(name);
880 mr->dirty_log_mask = 0;
881 mr->ioeventfd_nb = 0;
882 mr->ioeventfds = NULL;
885 static bool memory_region_access_valid(MemoryRegion *mr,
886 target_phys_addr_t addr,
887 unsigned size,
888 bool is_write)
890 if (mr->ops->valid.accepts
891 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
892 return false;
895 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
896 return false;
899 /* Treat zero as compatibility all valid */
900 if (!mr->ops->valid.max_access_size) {
901 return true;
904 if (size > mr->ops->valid.max_access_size
905 || size < mr->ops->valid.min_access_size) {
906 return false;
908 return true;
911 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
912 target_phys_addr_t addr,
913 unsigned size)
915 uint64_t data = 0;
917 if (!memory_region_access_valid(mr, addr, size, false)) {
918 return -1U; /* FIXME: better signalling */
921 if (!mr->ops->read) {
922 return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr);
925 /* FIXME: support unaligned access */
926 access_with_adjusted_size(addr + mr->offset, &data, size,
927 mr->ops->impl.min_access_size,
928 mr->ops->impl.max_access_size,
929 memory_region_read_accessor, mr);
931 return data;
934 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
936 if (memory_region_wrong_endianness(mr)) {
937 switch (size) {
938 case 1:
939 break;
940 case 2:
941 *data = bswap16(*data);
942 break;
943 case 4:
944 *data = bswap32(*data);
945 break;
946 default:
947 abort();
952 static uint64_t memory_region_dispatch_read(MemoryRegion *mr,
953 target_phys_addr_t addr,
954 unsigned size)
956 uint64_t ret;
958 ret = memory_region_dispatch_read1(mr, addr, size);
959 adjust_endianness(mr, &ret, size);
960 return ret;
963 static void memory_region_dispatch_write(MemoryRegion *mr,
964 target_phys_addr_t addr,
965 uint64_t data,
966 unsigned size)
968 if (!memory_region_access_valid(mr, addr, size, true)) {
969 return; /* FIXME: better signalling */
972 adjust_endianness(mr, &data, size);
974 if (!mr->ops->write) {
975 mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data);
976 return;
979 /* FIXME: support unaligned access */
980 access_with_adjusted_size(addr + mr->offset, &data, size,
981 mr->ops->impl.min_access_size,
982 mr->ops->impl.max_access_size,
983 memory_region_write_accessor, mr);
986 void memory_region_init_io(MemoryRegion *mr,
987 const MemoryRegionOps *ops,
988 void *opaque,
989 const char *name,
990 uint64_t size)
992 memory_region_init(mr, name, size);
993 mr->ops = ops;
994 mr->opaque = opaque;
995 mr->terminates = true;
996 mr->destructor = memory_region_destructor_iomem;
997 mr->ram_addr = cpu_register_io_memory(mr);
1000 void memory_region_init_ram(MemoryRegion *mr,
1001 const char *name,
1002 uint64_t size)
1004 memory_region_init(mr, name, size);
1005 mr->ram = true;
1006 mr->terminates = true;
1007 mr->destructor = memory_region_destructor_ram;
1008 mr->ram_addr = qemu_ram_alloc(size, mr);
1011 void memory_region_init_ram_ptr(MemoryRegion *mr,
1012 const char *name,
1013 uint64_t size,
1014 void *ptr)
1016 memory_region_init(mr, name, size);
1017 mr->ram = true;
1018 mr->terminates = true;
1019 mr->destructor = memory_region_destructor_ram_from_ptr;
1020 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1023 void memory_region_init_alias(MemoryRegion *mr,
1024 const char *name,
1025 MemoryRegion *orig,
1026 target_phys_addr_t offset,
1027 uint64_t size)
1029 memory_region_init(mr, name, size);
1030 mr->alias = orig;
1031 mr->alias_offset = offset;
1034 void memory_region_init_rom_device(MemoryRegion *mr,
1035 const MemoryRegionOps *ops,
1036 void *opaque,
1037 const char *name,
1038 uint64_t size)
1040 memory_region_init(mr, name, size);
1041 mr->ops = ops;
1042 mr->opaque = opaque;
1043 mr->terminates = true;
1044 mr->rom_device = true;
1045 mr->destructor = memory_region_destructor_rom_device;
1046 mr->ram_addr = qemu_ram_alloc(size, mr);
1047 mr->ram_addr |= cpu_register_io_memory(mr);
1050 void memory_region_destroy(MemoryRegion *mr)
1052 assert(QTAILQ_EMPTY(&mr->subregions));
1053 mr->destructor(mr);
1054 memory_region_clear_coalescing(mr);
1055 g_free((char *)mr->name);
1056 g_free(mr->ioeventfds);
1059 uint64_t memory_region_size(MemoryRegion *mr)
1061 if (int128_eq(mr->size, int128_2_64())) {
1062 return UINT64_MAX;
1064 return int128_get64(mr->size);
1067 const char *memory_region_name(MemoryRegion *mr)
1069 return mr->name;
1072 bool memory_region_is_ram(MemoryRegion *mr)
1074 return mr->ram;
1077 bool memory_region_is_logging(MemoryRegion *mr)
1079 return mr->dirty_log_mask;
1082 bool memory_region_is_rom(MemoryRegion *mr)
1084 return mr->ram && mr->readonly;
1087 void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset)
1089 mr->offset = offset;
1092 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1094 uint8_t mask = 1 << client;
1096 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1097 memory_region_update_topology(mr);
1100 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1101 unsigned client)
1103 assert(mr->terminates);
1104 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, 1 << client);
1107 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr)
1109 assert(mr->terminates);
1110 return cpu_physical_memory_set_dirty(mr->ram_addr + addr);
1113 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1115 FlatRange *fr;
1117 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1118 if (fr->mr == mr) {
1119 MEMORY_LISTENER_UPDATE_REGION(fr, &address_space_memory, log_sync);
1124 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1126 if (mr->readonly != readonly) {
1127 mr->readonly = readonly;
1128 memory_region_update_topology(mr);
1132 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
1134 if (mr->readable != readable) {
1135 mr->readable = readable;
1136 memory_region_update_topology(mr);
1140 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1141 target_phys_addr_t size, unsigned client)
1143 assert(mr->terminates);
1144 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1145 mr->ram_addr + addr + size,
1146 1 << client);
1149 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1151 if (mr->alias) {
1152 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1155 assert(mr->terminates);
1157 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1160 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1162 FlatRange *fr;
1163 CoalescedMemoryRange *cmr;
1164 AddrRange tmp;
1166 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1167 if (fr->mr == mr) {
1168 qemu_unregister_coalesced_mmio(int128_get64(fr->addr.start),
1169 int128_get64(fr->addr.size));
1170 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1171 tmp = addrrange_shift(cmr->addr,
1172 int128_sub(fr->addr.start,
1173 int128_make64(fr->offset_in_region)));
1174 if (!addrrange_intersects(tmp, fr->addr)) {
1175 continue;
1177 tmp = addrrange_intersection(tmp, fr->addr);
1178 qemu_register_coalesced_mmio(int128_get64(tmp.start),
1179 int128_get64(tmp.size));
1185 void memory_region_set_coalescing(MemoryRegion *mr)
1187 memory_region_clear_coalescing(mr);
1188 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1191 void memory_region_add_coalescing(MemoryRegion *mr,
1192 target_phys_addr_t offset,
1193 uint64_t size)
1195 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1197 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1198 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1199 memory_region_update_coalesced_range(mr);
1202 void memory_region_clear_coalescing(MemoryRegion *mr)
1204 CoalescedMemoryRange *cmr;
1206 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1207 cmr = QTAILQ_FIRST(&mr->coalesced);
1208 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1209 g_free(cmr);
1211 memory_region_update_coalesced_range(mr);
1214 void memory_region_add_eventfd(MemoryRegion *mr,
1215 target_phys_addr_t addr,
1216 unsigned size,
1217 bool match_data,
1218 uint64_t data,
1219 int fd)
1221 MemoryRegionIoeventfd mrfd = {
1222 .addr.start = int128_make64(addr),
1223 .addr.size = int128_make64(size),
1224 .match_data = match_data,
1225 .data = data,
1226 .fd = fd,
1228 unsigned i;
1230 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1231 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1232 break;
1235 ++mr->ioeventfd_nb;
1236 mr->ioeventfds = g_realloc(mr->ioeventfds,
1237 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1238 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1239 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1240 mr->ioeventfds[i] = mrfd;
1241 memory_region_update_topology(mr);
1244 void memory_region_del_eventfd(MemoryRegion *mr,
1245 target_phys_addr_t addr,
1246 unsigned size,
1247 bool match_data,
1248 uint64_t data,
1249 int fd)
1251 MemoryRegionIoeventfd mrfd = {
1252 .addr.start = int128_make64(addr),
1253 .addr.size = int128_make64(size),
1254 .match_data = match_data,
1255 .data = data,
1256 .fd = fd,
1258 unsigned i;
1260 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1261 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1262 break;
1265 assert(i != mr->ioeventfd_nb);
1266 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1267 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1268 --mr->ioeventfd_nb;
1269 mr->ioeventfds = g_realloc(mr->ioeventfds,
1270 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1271 memory_region_update_topology(mr);
1274 static void memory_region_add_subregion_common(MemoryRegion *mr,
1275 target_phys_addr_t offset,
1276 MemoryRegion *subregion)
1278 MemoryRegion *other;
1280 assert(!subregion->parent);
1281 subregion->parent = mr;
1282 subregion->addr = offset;
1283 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1284 if (subregion->may_overlap || other->may_overlap) {
1285 continue;
1287 if (int128_gt(int128_make64(offset),
1288 int128_add(int128_make64(other->addr), other->size))
1289 || int128_le(int128_add(int128_make64(offset), subregion->size),
1290 int128_make64(other->addr))) {
1291 continue;
1293 #if 0
1294 printf("warning: subregion collision %llx/%llx (%s) "
1295 "vs %llx/%llx (%s)\n",
1296 (unsigned long long)offset,
1297 (unsigned long long)int128_get64(subregion->size),
1298 subregion->name,
1299 (unsigned long long)other->addr,
1300 (unsigned long long)int128_get64(other->size),
1301 other->name);
1302 #endif
1304 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1305 if (subregion->priority >= other->priority) {
1306 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1307 goto done;
1310 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1311 done:
1312 memory_region_update_topology(mr);
1316 void memory_region_add_subregion(MemoryRegion *mr,
1317 target_phys_addr_t offset,
1318 MemoryRegion *subregion)
1320 subregion->may_overlap = false;
1321 subregion->priority = 0;
1322 memory_region_add_subregion_common(mr, offset, subregion);
1325 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1326 target_phys_addr_t offset,
1327 MemoryRegion *subregion,
1328 unsigned priority)
1330 subregion->may_overlap = true;
1331 subregion->priority = priority;
1332 memory_region_add_subregion_common(mr, offset, subregion);
1335 void memory_region_del_subregion(MemoryRegion *mr,
1336 MemoryRegion *subregion)
1338 assert(subregion->parent == mr);
1339 subregion->parent = NULL;
1340 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1341 memory_region_update_topology(mr);
1344 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1346 if (enabled == mr->enabled) {
1347 return;
1349 mr->enabled = enabled;
1350 memory_region_update_topology(NULL);
1353 void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr)
1355 MemoryRegion *parent = mr->parent;
1356 unsigned priority = mr->priority;
1357 bool may_overlap = mr->may_overlap;
1359 if (addr == mr->addr || !parent) {
1360 mr->addr = addr;
1361 return;
1364 memory_region_transaction_begin();
1365 memory_region_del_subregion(parent, mr);
1366 if (may_overlap) {
1367 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1368 } else {
1369 memory_region_add_subregion(parent, addr, mr);
1371 memory_region_transaction_commit();
1374 void memory_region_set_alias_offset(MemoryRegion *mr, target_phys_addr_t offset)
1376 target_phys_addr_t old_offset = mr->alias_offset;
1378 assert(mr->alias);
1379 mr->alias_offset = offset;
1381 if (offset == old_offset || !mr->parent) {
1382 return;
1385 memory_region_update_topology(mr);
1388 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1390 return mr->ram_addr;
1393 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1395 const AddrRange *addr = addr_;
1396 const FlatRange *fr = fr_;
1398 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1399 return -1;
1400 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1401 return 1;
1403 return 0;
1406 static FlatRange *address_space_lookup(AddressSpace *as, AddrRange addr)
1408 return bsearch(&addr, as->current_map.ranges, as->current_map.nr,
1409 sizeof(FlatRange), cmp_flatrange_addr);
1412 MemoryRegionSection memory_region_find(MemoryRegion *address_space,
1413 target_phys_addr_t addr, uint64_t size)
1415 AddressSpace *as = memory_region_to_address_space(address_space);
1416 AddrRange range = addrrange_make(int128_make64(addr),
1417 int128_make64(size));
1418 FlatRange *fr = address_space_lookup(as, range);
1419 MemoryRegionSection ret = { .mr = NULL, .size = 0 };
1421 if (!fr) {
1422 return ret;
1425 while (fr > as->current_map.ranges
1426 && addrrange_intersects(fr[-1].addr, range)) {
1427 --fr;
1430 ret.mr = fr->mr;
1431 range = addrrange_intersection(range, fr->addr);
1432 ret.offset_within_region = fr->offset_in_region;
1433 ret.offset_within_region += int128_get64(int128_sub(range.start,
1434 fr->addr.start));
1435 ret.size = int128_get64(range.size);
1436 ret.offset_within_address_space = int128_get64(range.start);
1437 return ret;
1440 void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
1442 AddressSpace *as = memory_region_to_address_space(address_space);
1443 FlatRange *fr;
1445 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
1446 MEMORY_LISTENER_UPDATE_REGION(fr, as, log_sync);
1450 void memory_global_dirty_log_start(void)
1452 MemoryListener *listener;
1454 cpu_physical_memory_set_dirty_tracking(1);
1455 global_dirty_log = true;
1456 QLIST_FOREACH(listener, &memory_listeners, link) {
1457 listener->log_global_start(listener);
1461 void memory_global_dirty_log_stop(void)
1463 MemoryListener *listener;
1465 global_dirty_log = false;
1466 QLIST_FOREACH(listener, &memory_listeners, link) {
1467 listener->log_global_stop(listener);
1469 cpu_physical_memory_set_dirty_tracking(0);
1472 static void listener_add_address_space(MemoryListener *listener,
1473 AddressSpace *as)
1475 FlatRange *fr;
1477 if (global_dirty_log) {
1478 listener->log_global_start(listener);
1480 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
1481 MemoryRegionSection section = {
1482 .mr = fr->mr,
1483 .address_space = as->root,
1484 .offset_within_region = fr->offset_in_region,
1485 .size = int128_get64(fr->addr.size),
1486 .offset_within_address_space = int128_get64(fr->addr.start),
1488 listener->region_add(listener, &section);
1492 void memory_listener_register(MemoryListener *listener)
1494 QLIST_INSERT_HEAD(&memory_listeners, listener, link);
1495 listener_add_address_space(listener, &address_space_memory);
1496 listener_add_address_space(listener, &address_space_io);
1499 void memory_listener_unregister(MemoryListener *listener)
1501 QLIST_REMOVE(listener, link);
1504 void set_system_memory_map(MemoryRegion *mr)
1506 address_space_memory.root = mr;
1507 memory_region_update_topology(NULL);
1510 void set_system_io_map(MemoryRegion *mr)
1512 address_space_io.root = mr;
1513 memory_region_update_topology(NULL);
1516 uint64_t io_mem_read(int io_index, target_phys_addr_t addr, unsigned size)
1518 return memory_region_dispatch_read(io_mem_region[io_index], addr, size);
1521 void io_mem_write(int io_index, target_phys_addr_t addr,
1522 uint64_t val, unsigned size)
1524 memory_region_dispatch_write(io_mem_region[io_index], addr, val, size);
1527 typedef struct MemoryRegionList MemoryRegionList;
1529 struct MemoryRegionList {
1530 const MemoryRegion *mr;
1531 bool printed;
1532 QTAILQ_ENTRY(MemoryRegionList) queue;
1535 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1537 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1538 const MemoryRegion *mr, unsigned int level,
1539 target_phys_addr_t base,
1540 MemoryRegionListHead *alias_print_queue)
1542 MemoryRegionList *new_ml, *ml, *next_ml;
1543 MemoryRegionListHead submr_print_queue;
1544 const MemoryRegion *submr;
1545 unsigned int i;
1547 if (!mr) {
1548 return;
1551 for (i = 0; i < level; i++) {
1552 mon_printf(f, " ");
1555 if (mr->alias) {
1556 MemoryRegionList *ml;
1557 bool found = false;
1559 /* check if the alias is already in the queue */
1560 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1561 if (ml->mr == mr->alias && !ml->printed) {
1562 found = true;
1566 if (!found) {
1567 ml = g_new(MemoryRegionList, 1);
1568 ml->mr = mr->alias;
1569 ml->printed = false;
1570 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1572 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): alias %s @%s "
1573 TARGET_FMT_plx "-" TARGET_FMT_plx "\n",
1574 base + mr->addr,
1575 base + mr->addr
1576 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1577 mr->priority,
1578 mr->name,
1579 mr->alias->name,
1580 mr->alias_offset,
1581 mr->alias_offset
1582 + (target_phys_addr_t)int128_get64(mr->size) - 1);
1583 } else {
1584 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): %s\n",
1585 base + mr->addr,
1586 base + mr->addr
1587 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1588 mr->priority,
1589 mr->name);
1592 QTAILQ_INIT(&submr_print_queue);
1594 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1595 new_ml = g_new(MemoryRegionList, 1);
1596 new_ml->mr = submr;
1597 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1598 if (new_ml->mr->addr < ml->mr->addr ||
1599 (new_ml->mr->addr == ml->mr->addr &&
1600 new_ml->mr->priority > ml->mr->priority)) {
1601 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1602 new_ml = NULL;
1603 break;
1606 if (new_ml) {
1607 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1611 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1612 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1613 alias_print_queue);
1616 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1617 g_free(ml);
1621 void mtree_info(fprintf_function mon_printf, void *f)
1623 MemoryRegionListHead ml_head;
1624 MemoryRegionList *ml, *ml2;
1626 QTAILQ_INIT(&ml_head);
1628 mon_printf(f, "memory\n");
1629 mtree_print_mr(mon_printf, f, address_space_memory.root, 0, 0, &ml_head);
1631 /* print aliased regions */
1632 QTAILQ_FOREACH(ml, &ml_head, queue) {
1633 if (!ml->printed) {
1634 mon_printf(f, "%s\n", ml->mr->name);
1635 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1639 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1640 g_free(ml);
1643 if (address_space_io.root &&
1644 !QTAILQ_EMPTY(&address_space_io.root->subregions)) {
1645 QTAILQ_INIT(&ml_head);
1646 mon_printf(f, "I/O\n");
1647 mtree_print_mr(mon_printf, f, address_space_io.root, 0, 0, &ml_head);