vnc: reorganize code for reverse mode
[qemu.git] / memory.c
blob2f68d67c215a28adc7b60e08e3f5240902aa6223
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.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "memory.h"
17 #include "exec-memory.h"
18 #include "ioport.h"
19 #include "bitops.h"
20 #include "kvm.h"
21 #include <assert.h>
23 #include "memory-internal.h"
25 unsigned memory_region_transaction_depth = 0;
26 static bool global_dirty_log = false;
28 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
29 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
31 static QTAILQ_HEAD(, AddressSpace) address_spaces
32 = QTAILQ_HEAD_INITIALIZER(address_spaces);
34 typedef struct AddrRange AddrRange;
37 * Note using signed integers limits us to physical addresses at most
38 * 63 bits wide. They are needed for negative offsetting in aliases
39 * (large MemoryRegion::alias_offset).
41 struct AddrRange {
42 Int128 start;
43 Int128 size;
46 static AddrRange addrrange_make(Int128 start, Int128 size)
48 return (AddrRange) { start, size };
51 static bool addrrange_equal(AddrRange r1, AddrRange r2)
53 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
56 static Int128 addrrange_end(AddrRange r)
58 return int128_add(r.start, r.size);
61 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
63 int128_addto(&range.start, delta);
64 return range;
67 static bool addrrange_contains(AddrRange range, Int128 addr)
69 return int128_ge(addr, range.start)
70 && int128_lt(addr, addrrange_end(range));
73 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
75 return addrrange_contains(r1, r2.start)
76 || addrrange_contains(r2, r1.start);
79 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
81 Int128 start = int128_max(r1.start, r2.start);
82 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
83 return addrrange_make(start, int128_sub(end, start));
86 enum ListenerDirection { Forward, Reverse };
88 static bool memory_listener_match(MemoryListener *listener,
89 MemoryRegionSection *section)
91 return !listener->address_space_filter
92 || listener->address_space_filter == section->address_space;
95 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
96 do { \
97 MemoryListener *_listener; \
99 switch (_direction) { \
100 case Forward: \
101 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
102 if (_listener->_callback) { \
103 _listener->_callback(_listener, ##_args); \
106 break; \
107 case Reverse: \
108 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
109 memory_listeners, link) { \
110 if (_listener->_callback) { \
111 _listener->_callback(_listener, ##_args); \
114 break; \
115 default: \
116 abort(); \
118 } while (0)
120 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
121 do { \
122 MemoryListener *_listener; \
124 switch (_direction) { \
125 case Forward: \
126 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
127 if (_listener->_callback \
128 && memory_listener_match(_listener, _section)) { \
129 _listener->_callback(_listener, _section, ##_args); \
132 break; \
133 case Reverse: \
134 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
135 memory_listeners, link) { \
136 if (_listener->_callback \
137 && memory_listener_match(_listener, _section)) { \
138 _listener->_callback(_listener, _section, ##_args); \
141 break; \
142 default: \
143 abort(); \
145 } while (0)
147 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
148 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
149 .mr = (fr)->mr, \
150 .address_space = (as), \
151 .offset_within_region = (fr)->offset_in_region, \
152 .size = int128_get64((fr)->addr.size), \
153 .offset_within_address_space = int128_get64((fr)->addr.start), \
154 .readonly = (fr)->readonly, \
157 struct CoalescedMemoryRange {
158 AddrRange addr;
159 QTAILQ_ENTRY(CoalescedMemoryRange) link;
162 struct MemoryRegionIoeventfd {
163 AddrRange addr;
164 bool match_data;
165 uint64_t data;
166 EventNotifier *e;
169 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
170 MemoryRegionIoeventfd b)
172 if (int128_lt(a.addr.start, b.addr.start)) {
173 return true;
174 } else if (int128_gt(a.addr.start, b.addr.start)) {
175 return false;
176 } else if (int128_lt(a.addr.size, b.addr.size)) {
177 return true;
178 } else if (int128_gt(a.addr.size, b.addr.size)) {
179 return false;
180 } else if (a.match_data < b.match_data) {
181 return true;
182 } else if (a.match_data > b.match_data) {
183 return false;
184 } else if (a.match_data) {
185 if (a.data < b.data) {
186 return true;
187 } else if (a.data > b.data) {
188 return false;
191 if (a.e < b.e) {
192 return true;
193 } else if (a.e > b.e) {
194 return false;
196 return false;
199 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
200 MemoryRegionIoeventfd b)
202 return !memory_region_ioeventfd_before(a, b)
203 && !memory_region_ioeventfd_before(b, a);
206 typedef struct FlatRange FlatRange;
207 typedef struct FlatView FlatView;
209 /* Range of memory in the global map. Addresses are absolute. */
210 struct FlatRange {
211 MemoryRegion *mr;
212 target_phys_addr_t offset_in_region;
213 AddrRange addr;
214 uint8_t dirty_log_mask;
215 bool readable;
216 bool readonly;
219 /* Flattened global view of current active memory hierarchy. Kept in sorted
220 * order.
222 struct FlatView {
223 FlatRange *ranges;
224 unsigned nr;
225 unsigned nr_allocated;
228 typedef struct AddressSpaceOps AddressSpaceOps;
230 #define FOR_EACH_FLAT_RANGE(var, view) \
231 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
233 static bool flatrange_equal(FlatRange *a, FlatRange *b)
235 return a->mr == b->mr
236 && addrrange_equal(a->addr, b->addr)
237 && a->offset_in_region == b->offset_in_region
238 && a->readable == b->readable
239 && a->readonly == b->readonly;
242 static void flatview_init(FlatView *view)
244 view->ranges = NULL;
245 view->nr = 0;
246 view->nr_allocated = 0;
249 /* Insert a range into a given position. Caller is responsible for maintaining
250 * sorting order.
252 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
254 if (view->nr == view->nr_allocated) {
255 view->nr_allocated = MAX(2 * view->nr, 10);
256 view->ranges = g_realloc(view->ranges,
257 view->nr_allocated * sizeof(*view->ranges));
259 memmove(view->ranges + pos + 1, view->ranges + pos,
260 (view->nr - pos) * sizeof(FlatRange));
261 view->ranges[pos] = *range;
262 ++view->nr;
265 static void flatview_destroy(FlatView *view)
267 g_free(view->ranges);
270 static bool can_merge(FlatRange *r1, FlatRange *r2)
272 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
273 && r1->mr == r2->mr
274 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
275 r1->addr.size),
276 int128_make64(r2->offset_in_region))
277 && r1->dirty_log_mask == r2->dirty_log_mask
278 && r1->readable == r2->readable
279 && r1->readonly == r2->readonly;
282 /* Attempt to simplify a view by merging ajacent ranges */
283 static void flatview_simplify(FlatView *view)
285 unsigned i, j;
287 i = 0;
288 while (i < view->nr) {
289 j = i + 1;
290 while (j < view->nr
291 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
292 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
293 ++j;
295 ++i;
296 memmove(&view->ranges[i], &view->ranges[j],
297 (view->nr - j) * sizeof(view->ranges[j]));
298 view->nr -= j - i;
302 static void memory_region_read_accessor(void *opaque,
303 target_phys_addr_t addr,
304 uint64_t *value,
305 unsigned size,
306 unsigned shift,
307 uint64_t mask)
309 MemoryRegion *mr = opaque;
310 uint64_t tmp;
312 if (mr->flush_coalesced_mmio) {
313 qemu_flush_coalesced_mmio_buffer();
315 tmp = mr->ops->read(mr->opaque, addr, size);
316 *value |= (tmp & mask) << shift;
319 static void memory_region_write_accessor(void *opaque,
320 target_phys_addr_t addr,
321 uint64_t *value,
322 unsigned size,
323 unsigned shift,
324 uint64_t mask)
326 MemoryRegion *mr = opaque;
327 uint64_t tmp;
329 if (mr->flush_coalesced_mmio) {
330 qemu_flush_coalesced_mmio_buffer();
332 tmp = (*value >> shift) & mask;
333 mr->ops->write(mr->opaque, addr, tmp, size);
336 static void access_with_adjusted_size(target_phys_addr_t addr,
337 uint64_t *value,
338 unsigned size,
339 unsigned access_size_min,
340 unsigned access_size_max,
341 void (*access)(void *opaque,
342 target_phys_addr_t addr,
343 uint64_t *value,
344 unsigned size,
345 unsigned shift,
346 uint64_t mask),
347 void *opaque)
349 uint64_t access_mask;
350 unsigned access_size;
351 unsigned i;
353 if (!access_size_min) {
354 access_size_min = 1;
356 if (!access_size_max) {
357 access_size_max = 4;
359 access_size = MAX(MIN(size, access_size_max), access_size_min);
360 access_mask = -1ULL >> (64 - access_size * 8);
361 for (i = 0; i < size; i += access_size) {
362 /* FIXME: big-endian support */
363 access(opaque, addr + i, value, access_size, i * 8, access_mask);
367 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
368 unsigned width, bool write)
370 const MemoryRegionPortio *mrp;
372 for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
373 if (offset >= mrp->offset && offset < mrp->offset + mrp->len
374 && width == mrp->size
375 && (write ? (bool)mrp->write : (bool)mrp->read)) {
376 return mrp;
379 return NULL;
382 static void memory_region_iorange_read(IORange *iorange,
383 uint64_t offset,
384 unsigned width,
385 uint64_t *data)
387 MemoryRegionIORange *mrio
388 = container_of(iorange, MemoryRegionIORange, iorange);
389 MemoryRegion *mr = mrio->mr;
391 offset += mrio->offset;
392 if (mr->ops->old_portio) {
393 const MemoryRegionPortio *mrp = find_portio(mr, offset - mrio->offset,
394 width, false);
396 *data = ((uint64_t)1 << (width * 8)) - 1;
397 if (mrp) {
398 *data = mrp->read(mr->opaque, offset);
399 } else if (width == 2) {
400 mrp = find_portio(mr, offset - mrio->offset, 1, false);
401 assert(mrp);
402 *data = mrp->read(mr->opaque, offset) |
403 (mrp->read(mr->opaque, offset + 1) << 8);
405 return;
407 *data = 0;
408 access_with_adjusted_size(offset, data, width,
409 mr->ops->impl.min_access_size,
410 mr->ops->impl.max_access_size,
411 memory_region_read_accessor, mr);
414 static void memory_region_iorange_write(IORange *iorange,
415 uint64_t offset,
416 unsigned width,
417 uint64_t data)
419 MemoryRegionIORange *mrio
420 = container_of(iorange, MemoryRegionIORange, iorange);
421 MemoryRegion *mr = mrio->mr;
423 offset += mrio->offset;
424 if (mr->ops->old_portio) {
425 const MemoryRegionPortio *mrp = find_portio(mr, offset - mrio->offset,
426 width, true);
428 if (mrp) {
429 mrp->write(mr->opaque, offset, data);
430 } else if (width == 2) {
431 mrp = find_portio(mr, offset - mrio->offset, 1, true);
432 assert(mrp);
433 mrp->write(mr->opaque, offset, data & 0xff);
434 mrp->write(mr->opaque, offset + 1, data >> 8);
436 return;
438 access_with_adjusted_size(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 void memory_region_iorange_destructor(IORange *iorange)
446 g_free(container_of(iorange, MemoryRegionIORange, iorange));
449 const IORangeOps memory_region_iorange_ops = {
450 .read = memory_region_iorange_read,
451 .write = memory_region_iorange_write,
452 .destructor = memory_region_iorange_destructor,
455 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
457 AddressSpace *as;
459 while (mr->parent) {
460 mr = mr->parent;
462 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
463 if (mr == as->root) {
464 return as;
467 abort();
470 /* Render a memory region into the global view. Ranges in @view obscure
471 * ranges in @mr.
473 static void render_memory_region(FlatView *view,
474 MemoryRegion *mr,
475 Int128 base,
476 AddrRange clip,
477 bool readonly)
479 MemoryRegion *subregion;
480 unsigned i;
481 target_phys_addr_t offset_in_region;
482 Int128 remain;
483 Int128 now;
484 FlatRange fr;
485 AddrRange tmp;
487 if (!mr->enabled) {
488 return;
491 int128_addto(&base, int128_make64(mr->addr));
492 readonly |= mr->readonly;
494 tmp = addrrange_make(base, mr->size);
496 if (!addrrange_intersects(tmp, clip)) {
497 return;
500 clip = addrrange_intersection(tmp, clip);
502 if (mr->alias) {
503 int128_subfrom(&base, int128_make64(mr->alias->addr));
504 int128_subfrom(&base, int128_make64(mr->alias_offset));
505 render_memory_region(view, mr->alias, base, clip, readonly);
506 return;
509 /* Render subregions in priority order. */
510 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
511 render_memory_region(view, subregion, base, clip, readonly);
514 if (!mr->terminates) {
515 return;
518 offset_in_region = int128_get64(int128_sub(clip.start, base));
519 base = clip.start;
520 remain = clip.size;
522 /* Render the region itself into any gaps left by the current view. */
523 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
524 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
525 continue;
527 if (int128_lt(base, view->ranges[i].addr.start)) {
528 now = int128_min(remain,
529 int128_sub(view->ranges[i].addr.start, base));
530 fr.mr = mr;
531 fr.offset_in_region = offset_in_region;
532 fr.addr = addrrange_make(base, now);
533 fr.dirty_log_mask = mr->dirty_log_mask;
534 fr.readable = mr->readable;
535 fr.readonly = readonly;
536 flatview_insert(view, i, &fr);
537 ++i;
538 int128_addto(&base, now);
539 offset_in_region += int128_get64(now);
540 int128_subfrom(&remain, now);
542 if (int128_eq(base, view->ranges[i].addr.start)) {
543 now = int128_min(remain, view->ranges[i].addr.size);
544 int128_addto(&base, now);
545 offset_in_region += int128_get64(now);
546 int128_subfrom(&remain, now);
549 if (int128_nz(remain)) {
550 fr.mr = mr;
551 fr.offset_in_region = offset_in_region;
552 fr.addr = addrrange_make(base, remain);
553 fr.dirty_log_mask = mr->dirty_log_mask;
554 fr.readable = mr->readable;
555 fr.readonly = readonly;
556 flatview_insert(view, i, &fr);
560 /* Render a memory topology into a list of disjoint absolute ranges. */
561 static FlatView generate_memory_topology(MemoryRegion *mr)
563 FlatView view;
565 flatview_init(&view);
567 if (mr) {
568 render_memory_region(&view, mr, int128_zero(),
569 addrrange_make(int128_zero(), int128_2_64()), false);
571 flatview_simplify(&view);
573 return view;
576 static void address_space_add_del_ioeventfds(AddressSpace *as,
577 MemoryRegionIoeventfd *fds_new,
578 unsigned fds_new_nb,
579 MemoryRegionIoeventfd *fds_old,
580 unsigned fds_old_nb)
582 unsigned iold, inew;
583 MemoryRegionIoeventfd *fd;
584 MemoryRegionSection section;
586 /* Generate a symmetric difference of the old and new fd sets, adding
587 * and deleting as necessary.
590 iold = inew = 0;
591 while (iold < fds_old_nb || inew < fds_new_nb) {
592 if (iold < fds_old_nb
593 && (inew == fds_new_nb
594 || memory_region_ioeventfd_before(fds_old[iold],
595 fds_new[inew]))) {
596 fd = &fds_old[iold];
597 section = (MemoryRegionSection) {
598 .address_space = as,
599 .offset_within_address_space = int128_get64(fd->addr.start),
600 .size = int128_get64(fd->addr.size),
602 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
603 fd->match_data, fd->data, fd->e);
604 ++iold;
605 } else if (inew < fds_new_nb
606 && (iold == fds_old_nb
607 || memory_region_ioeventfd_before(fds_new[inew],
608 fds_old[iold]))) {
609 fd = &fds_new[inew];
610 section = (MemoryRegionSection) {
611 .address_space = as,
612 .offset_within_address_space = int128_get64(fd->addr.start),
613 .size = int128_get64(fd->addr.size),
615 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
616 fd->match_data, fd->data, fd->e);
617 ++inew;
618 } else {
619 ++iold;
620 ++inew;
625 static void address_space_update_ioeventfds(AddressSpace *as)
627 FlatRange *fr;
628 unsigned ioeventfd_nb = 0;
629 MemoryRegionIoeventfd *ioeventfds = NULL;
630 AddrRange tmp;
631 unsigned i;
633 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
634 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
635 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
636 int128_sub(fr->addr.start,
637 int128_make64(fr->offset_in_region)));
638 if (addrrange_intersects(fr->addr, tmp)) {
639 ++ioeventfd_nb;
640 ioeventfds = g_realloc(ioeventfds,
641 ioeventfd_nb * sizeof(*ioeventfds));
642 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
643 ioeventfds[ioeventfd_nb-1].addr = tmp;
648 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
649 as->ioeventfds, as->ioeventfd_nb);
651 g_free(as->ioeventfds);
652 as->ioeventfds = ioeventfds;
653 as->ioeventfd_nb = ioeventfd_nb;
656 static void address_space_update_topology_pass(AddressSpace *as,
657 FlatView old_view,
658 FlatView new_view,
659 bool adding)
661 unsigned iold, inew;
662 FlatRange *frold, *frnew;
664 /* Generate a symmetric difference of the old and new memory maps.
665 * Kill ranges in the old map, and instantiate ranges in the new map.
667 iold = inew = 0;
668 while (iold < old_view.nr || inew < new_view.nr) {
669 if (iold < old_view.nr) {
670 frold = &old_view.ranges[iold];
671 } else {
672 frold = NULL;
674 if (inew < new_view.nr) {
675 frnew = &new_view.ranges[inew];
676 } else {
677 frnew = NULL;
680 if (frold
681 && (!frnew
682 || int128_lt(frold->addr.start, frnew->addr.start)
683 || (int128_eq(frold->addr.start, frnew->addr.start)
684 && !flatrange_equal(frold, frnew)))) {
685 /* In old, but (not in new, or in new but attributes changed). */
687 if (!adding) {
688 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
691 ++iold;
692 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
693 /* In both (logging may have changed) */
695 if (adding) {
696 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
697 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
698 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
699 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
700 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
704 ++iold;
705 ++inew;
706 } else {
707 /* In new */
709 if (adding) {
710 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
713 ++inew;
719 static void address_space_update_topology(AddressSpace *as)
721 FlatView old_view = *as->current_map;
722 FlatView new_view = generate_memory_topology(as->root);
724 address_space_update_topology_pass(as, old_view, new_view, false);
725 address_space_update_topology_pass(as, old_view, new_view, true);
727 *as->current_map = new_view;
728 flatview_destroy(&old_view);
729 address_space_update_ioeventfds(as);
732 void memory_region_transaction_begin(void)
734 qemu_flush_coalesced_mmio_buffer();
735 ++memory_region_transaction_depth;
738 void memory_region_transaction_commit(void)
740 AddressSpace *as;
742 assert(memory_region_transaction_depth);
743 --memory_region_transaction_depth;
744 if (!memory_region_transaction_depth) {
745 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
747 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
748 address_space_update_topology(as);
751 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
755 static void memory_region_destructor_none(MemoryRegion *mr)
759 static void memory_region_destructor_ram(MemoryRegion *mr)
761 qemu_ram_free(mr->ram_addr);
764 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
766 qemu_ram_free_from_ptr(mr->ram_addr);
769 static void memory_region_destructor_iomem(MemoryRegion *mr)
773 static void memory_region_destructor_rom_device(MemoryRegion *mr)
775 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
778 static bool memory_region_wrong_endianness(MemoryRegion *mr)
780 #ifdef TARGET_WORDS_BIGENDIAN
781 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
782 #else
783 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
784 #endif
787 void memory_region_init(MemoryRegion *mr,
788 const char *name,
789 uint64_t size)
791 mr->ops = NULL;
792 mr->parent = NULL;
793 mr->size = int128_make64(size);
794 if (size == UINT64_MAX) {
795 mr->size = int128_2_64();
797 mr->addr = 0;
798 mr->subpage = false;
799 mr->enabled = true;
800 mr->terminates = false;
801 mr->ram = false;
802 mr->readable = true;
803 mr->readonly = false;
804 mr->rom_device = false;
805 mr->destructor = memory_region_destructor_none;
806 mr->priority = 0;
807 mr->may_overlap = false;
808 mr->alias = NULL;
809 QTAILQ_INIT(&mr->subregions);
810 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
811 QTAILQ_INIT(&mr->coalesced);
812 mr->name = g_strdup(name);
813 mr->dirty_log_mask = 0;
814 mr->ioeventfd_nb = 0;
815 mr->ioeventfds = NULL;
816 mr->flush_coalesced_mmio = false;
819 static bool memory_region_access_valid(MemoryRegion *mr,
820 target_phys_addr_t addr,
821 unsigned size,
822 bool is_write)
824 if (mr->ops->valid.accepts
825 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
826 return false;
829 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
830 return false;
833 /* Treat zero as compatibility all valid */
834 if (!mr->ops->valid.max_access_size) {
835 return true;
838 if (size > mr->ops->valid.max_access_size
839 || size < mr->ops->valid.min_access_size) {
840 return false;
842 return true;
845 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
846 target_phys_addr_t addr,
847 unsigned size)
849 uint64_t data = 0;
851 if (!memory_region_access_valid(mr, addr, size, false)) {
852 return -1U; /* FIXME: better signalling */
855 if (!mr->ops->read) {
856 return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr);
859 /* FIXME: support unaligned access */
860 access_with_adjusted_size(addr, &data, size,
861 mr->ops->impl.min_access_size,
862 mr->ops->impl.max_access_size,
863 memory_region_read_accessor, mr);
865 return data;
868 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
870 if (memory_region_wrong_endianness(mr)) {
871 switch (size) {
872 case 1:
873 break;
874 case 2:
875 *data = bswap16(*data);
876 break;
877 case 4:
878 *data = bswap32(*data);
879 break;
880 default:
881 abort();
886 static uint64_t memory_region_dispatch_read(MemoryRegion *mr,
887 target_phys_addr_t addr,
888 unsigned size)
890 uint64_t ret;
892 ret = memory_region_dispatch_read1(mr, addr, size);
893 adjust_endianness(mr, &ret, size);
894 return ret;
897 static void memory_region_dispatch_write(MemoryRegion *mr,
898 target_phys_addr_t addr,
899 uint64_t data,
900 unsigned size)
902 if (!memory_region_access_valid(mr, addr, size, true)) {
903 return; /* FIXME: better signalling */
906 adjust_endianness(mr, &data, size);
908 if (!mr->ops->write) {
909 mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data);
910 return;
913 /* FIXME: support unaligned access */
914 access_with_adjusted_size(addr, &data, size,
915 mr->ops->impl.min_access_size,
916 mr->ops->impl.max_access_size,
917 memory_region_write_accessor, mr);
920 void memory_region_init_io(MemoryRegion *mr,
921 const MemoryRegionOps *ops,
922 void *opaque,
923 const char *name,
924 uint64_t size)
926 memory_region_init(mr, name, size);
927 mr->ops = ops;
928 mr->opaque = opaque;
929 mr->terminates = true;
930 mr->destructor = memory_region_destructor_iomem;
931 mr->ram_addr = ~(ram_addr_t)0;
934 void memory_region_init_ram(MemoryRegion *mr,
935 const char *name,
936 uint64_t size)
938 memory_region_init(mr, name, size);
939 mr->ram = true;
940 mr->terminates = true;
941 mr->destructor = memory_region_destructor_ram;
942 mr->ram_addr = qemu_ram_alloc(size, mr);
945 void memory_region_init_ram_ptr(MemoryRegion *mr,
946 const char *name,
947 uint64_t size,
948 void *ptr)
950 memory_region_init(mr, name, size);
951 mr->ram = true;
952 mr->terminates = true;
953 mr->destructor = memory_region_destructor_ram_from_ptr;
954 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
957 void memory_region_init_alias(MemoryRegion *mr,
958 const char *name,
959 MemoryRegion *orig,
960 target_phys_addr_t offset,
961 uint64_t size)
963 memory_region_init(mr, name, size);
964 mr->alias = orig;
965 mr->alias_offset = offset;
968 void memory_region_init_rom_device(MemoryRegion *mr,
969 const MemoryRegionOps *ops,
970 void *opaque,
971 const char *name,
972 uint64_t size)
974 memory_region_init(mr, name, size);
975 mr->ops = ops;
976 mr->opaque = opaque;
977 mr->terminates = true;
978 mr->rom_device = true;
979 mr->destructor = memory_region_destructor_rom_device;
980 mr->ram_addr = qemu_ram_alloc(size, mr);
983 static uint64_t invalid_read(void *opaque, target_phys_addr_t addr,
984 unsigned size)
986 MemoryRegion *mr = opaque;
988 if (!mr->warning_printed) {
989 fprintf(stderr, "Invalid read from memory region %s\n", mr->name);
990 mr->warning_printed = true;
992 return -1U;
995 static void invalid_write(void *opaque, target_phys_addr_t addr, uint64_t data,
996 unsigned size)
998 MemoryRegion *mr = opaque;
1000 if (!mr->warning_printed) {
1001 fprintf(stderr, "Invalid write to memory region %s\n", mr->name);
1002 mr->warning_printed = true;
1006 static const MemoryRegionOps reservation_ops = {
1007 .read = invalid_read,
1008 .write = invalid_write,
1009 .endianness = DEVICE_NATIVE_ENDIAN,
1012 void memory_region_init_reservation(MemoryRegion *mr,
1013 const char *name,
1014 uint64_t size)
1016 memory_region_init_io(mr, &reservation_ops, mr, name, size);
1019 void memory_region_destroy(MemoryRegion *mr)
1021 assert(QTAILQ_EMPTY(&mr->subregions));
1022 mr->destructor(mr);
1023 memory_region_clear_coalescing(mr);
1024 g_free((char *)mr->name);
1025 g_free(mr->ioeventfds);
1028 uint64_t memory_region_size(MemoryRegion *mr)
1030 if (int128_eq(mr->size, int128_2_64())) {
1031 return UINT64_MAX;
1033 return int128_get64(mr->size);
1036 const char *memory_region_name(MemoryRegion *mr)
1038 return mr->name;
1041 bool memory_region_is_ram(MemoryRegion *mr)
1043 return mr->ram;
1046 bool memory_region_is_logging(MemoryRegion *mr)
1048 return mr->dirty_log_mask;
1051 bool memory_region_is_rom(MemoryRegion *mr)
1053 return mr->ram && mr->readonly;
1056 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1058 uint8_t mask = 1 << client;
1060 memory_region_transaction_begin();
1061 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1062 memory_region_transaction_commit();
1065 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1066 target_phys_addr_t size, unsigned client)
1068 assert(mr->terminates);
1069 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1070 1 << client);
1073 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1074 target_phys_addr_t size)
1076 assert(mr->terminates);
1077 return cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size, -1);
1080 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1082 AddressSpace *as;
1083 FlatRange *fr;
1085 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1086 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1087 if (fr->mr == mr) {
1088 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1094 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1096 if (mr->readonly != readonly) {
1097 memory_region_transaction_begin();
1098 mr->readonly = readonly;
1099 memory_region_transaction_commit();
1103 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
1105 if (mr->readable != readable) {
1106 memory_region_transaction_begin();
1107 mr->readable = readable;
1108 memory_region_transaction_commit();
1112 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1113 target_phys_addr_t size, unsigned client)
1115 assert(mr->terminates);
1116 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1117 mr->ram_addr + addr + size,
1118 1 << client);
1121 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1123 if (mr->alias) {
1124 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1127 assert(mr->terminates);
1129 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1132 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1134 FlatRange *fr;
1135 CoalescedMemoryRange *cmr;
1136 AddrRange tmp;
1137 MemoryRegionSection section;
1139 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1140 if (fr->mr == mr) {
1141 section = (MemoryRegionSection) {
1142 .address_space = as,
1143 .offset_within_address_space = int128_get64(fr->addr.start),
1144 .size = int128_get64(fr->addr.size),
1147 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1148 int128_get64(fr->addr.start),
1149 int128_get64(fr->addr.size));
1150 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1151 tmp = addrrange_shift(cmr->addr,
1152 int128_sub(fr->addr.start,
1153 int128_make64(fr->offset_in_region)));
1154 if (!addrrange_intersects(tmp, fr->addr)) {
1155 continue;
1157 tmp = addrrange_intersection(tmp, fr->addr);
1158 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1159 int128_get64(tmp.start),
1160 int128_get64(tmp.size));
1166 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1168 AddressSpace *as;
1170 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1171 memory_region_update_coalesced_range_as(mr, as);
1175 void memory_region_set_coalescing(MemoryRegion *mr)
1177 memory_region_clear_coalescing(mr);
1178 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1181 void memory_region_add_coalescing(MemoryRegion *mr,
1182 target_phys_addr_t offset,
1183 uint64_t size)
1185 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1187 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1188 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1189 memory_region_update_coalesced_range(mr);
1190 memory_region_set_flush_coalesced(mr);
1193 void memory_region_clear_coalescing(MemoryRegion *mr)
1195 CoalescedMemoryRange *cmr;
1197 qemu_flush_coalesced_mmio_buffer();
1198 mr->flush_coalesced_mmio = false;
1200 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1201 cmr = QTAILQ_FIRST(&mr->coalesced);
1202 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1203 g_free(cmr);
1205 memory_region_update_coalesced_range(mr);
1208 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1210 mr->flush_coalesced_mmio = true;
1213 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1215 qemu_flush_coalesced_mmio_buffer();
1216 if (QTAILQ_EMPTY(&mr->coalesced)) {
1217 mr->flush_coalesced_mmio = false;
1221 void memory_region_add_eventfd(MemoryRegion *mr,
1222 target_phys_addr_t addr,
1223 unsigned size,
1224 bool match_data,
1225 uint64_t data,
1226 EventNotifier *e)
1228 MemoryRegionIoeventfd mrfd = {
1229 .addr.start = int128_make64(addr),
1230 .addr.size = int128_make64(size),
1231 .match_data = match_data,
1232 .data = data,
1233 .e = e,
1235 unsigned i;
1237 memory_region_transaction_begin();
1238 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1239 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1240 break;
1243 ++mr->ioeventfd_nb;
1244 mr->ioeventfds = g_realloc(mr->ioeventfds,
1245 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1246 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1247 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1248 mr->ioeventfds[i] = mrfd;
1249 memory_region_transaction_commit();
1252 void memory_region_del_eventfd(MemoryRegion *mr,
1253 target_phys_addr_t addr,
1254 unsigned size,
1255 bool match_data,
1256 uint64_t data,
1257 EventNotifier *e)
1259 MemoryRegionIoeventfd mrfd = {
1260 .addr.start = int128_make64(addr),
1261 .addr.size = int128_make64(size),
1262 .match_data = match_data,
1263 .data = data,
1264 .e = e,
1266 unsigned i;
1268 memory_region_transaction_begin();
1269 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1270 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1271 break;
1274 assert(i != mr->ioeventfd_nb);
1275 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1276 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1277 --mr->ioeventfd_nb;
1278 mr->ioeventfds = g_realloc(mr->ioeventfds,
1279 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1280 memory_region_transaction_commit();
1283 static void memory_region_add_subregion_common(MemoryRegion *mr,
1284 target_phys_addr_t offset,
1285 MemoryRegion *subregion)
1287 MemoryRegion *other;
1289 memory_region_transaction_begin();
1291 assert(!subregion->parent);
1292 subregion->parent = mr;
1293 subregion->addr = offset;
1294 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1295 if (subregion->may_overlap || other->may_overlap) {
1296 continue;
1298 if (int128_gt(int128_make64(offset),
1299 int128_add(int128_make64(other->addr), other->size))
1300 || int128_le(int128_add(int128_make64(offset), subregion->size),
1301 int128_make64(other->addr))) {
1302 continue;
1304 #if 0
1305 printf("warning: subregion collision %llx/%llx (%s) "
1306 "vs %llx/%llx (%s)\n",
1307 (unsigned long long)offset,
1308 (unsigned long long)int128_get64(subregion->size),
1309 subregion->name,
1310 (unsigned long long)other->addr,
1311 (unsigned long long)int128_get64(other->size),
1312 other->name);
1313 #endif
1315 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1316 if (subregion->priority >= other->priority) {
1317 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1318 goto done;
1321 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1322 done:
1323 memory_region_transaction_commit();
1327 void memory_region_add_subregion(MemoryRegion *mr,
1328 target_phys_addr_t offset,
1329 MemoryRegion *subregion)
1331 subregion->may_overlap = false;
1332 subregion->priority = 0;
1333 memory_region_add_subregion_common(mr, offset, subregion);
1336 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1337 target_phys_addr_t offset,
1338 MemoryRegion *subregion,
1339 unsigned priority)
1341 subregion->may_overlap = true;
1342 subregion->priority = priority;
1343 memory_region_add_subregion_common(mr, offset, subregion);
1346 void memory_region_del_subregion(MemoryRegion *mr,
1347 MemoryRegion *subregion)
1349 memory_region_transaction_begin();
1350 assert(subregion->parent == mr);
1351 subregion->parent = NULL;
1352 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1353 memory_region_transaction_commit();
1356 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1358 if (enabled == mr->enabled) {
1359 return;
1361 memory_region_transaction_begin();
1362 mr->enabled = enabled;
1363 memory_region_transaction_commit();
1366 void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr)
1368 MemoryRegion *parent = mr->parent;
1369 unsigned priority = mr->priority;
1370 bool may_overlap = mr->may_overlap;
1372 if (addr == mr->addr || !parent) {
1373 mr->addr = addr;
1374 return;
1377 memory_region_transaction_begin();
1378 memory_region_del_subregion(parent, mr);
1379 if (may_overlap) {
1380 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1381 } else {
1382 memory_region_add_subregion(parent, addr, mr);
1384 memory_region_transaction_commit();
1387 void memory_region_set_alias_offset(MemoryRegion *mr, target_phys_addr_t offset)
1389 assert(mr->alias);
1391 if (offset == mr->alias_offset) {
1392 return;
1395 memory_region_transaction_begin();
1396 mr->alias_offset = offset;
1397 memory_region_transaction_commit();
1400 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1402 return mr->ram_addr;
1405 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1407 const AddrRange *addr = addr_;
1408 const FlatRange *fr = fr_;
1410 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1411 return -1;
1412 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1413 return 1;
1415 return 0;
1418 static FlatRange *address_space_lookup(AddressSpace *as, AddrRange addr)
1420 return bsearch(&addr, as->current_map->ranges, as->current_map->nr,
1421 sizeof(FlatRange), cmp_flatrange_addr);
1424 MemoryRegionSection memory_region_find(MemoryRegion *address_space,
1425 target_phys_addr_t addr, uint64_t size)
1427 AddressSpace *as = memory_region_to_address_space(address_space);
1428 AddrRange range = addrrange_make(int128_make64(addr),
1429 int128_make64(size));
1430 FlatRange *fr = address_space_lookup(as, range);
1431 MemoryRegionSection ret = { .mr = NULL, .size = 0 };
1433 if (!fr) {
1434 return ret;
1437 while (fr > as->current_map->ranges
1438 && addrrange_intersects(fr[-1].addr, range)) {
1439 --fr;
1442 ret.mr = fr->mr;
1443 range = addrrange_intersection(range, fr->addr);
1444 ret.offset_within_region = fr->offset_in_region;
1445 ret.offset_within_region += int128_get64(int128_sub(range.start,
1446 fr->addr.start));
1447 ret.size = int128_get64(range.size);
1448 ret.offset_within_address_space = int128_get64(range.start);
1449 ret.readonly = fr->readonly;
1450 return ret;
1453 void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
1455 AddressSpace *as = memory_region_to_address_space(address_space);
1456 FlatRange *fr;
1458 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1459 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1463 void memory_global_dirty_log_start(void)
1465 global_dirty_log = true;
1466 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
1469 void memory_global_dirty_log_stop(void)
1471 global_dirty_log = false;
1472 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
1475 static void listener_add_address_space(MemoryListener *listener,
1476 AddressSpace *as)
1478 FlatRange *fr;
1480 if (listener->address_space_filter
1481 && listener->address_space_filter != as) {
1482 return;
1485 if (global_dirty_log) {
1486 if (listener->log_global_start) {
1487 listener->log_global_start(listener);
1491 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1492 MemoryRegionSection section = {
1493 .mr = fr->mr,
1494 .address_space = as,
1495 .offset_within_region = fr->offset_in_region,
1496 .size = int128_get64(fr->addr.size),
1497 .offset_within_address_space = int128_get64(fr->addr.start),
1498 .readonly = fr->readonly,
1500 if (listener->region_add) {
1501 listener->region_add(listener, &section);
1506 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
1508 MemoryListener *other = NULL;
1509 AddressSpace *as;
1511 listener->address_space_filter = filter;
1512 if (QTAILQ_EMPTY(&memory_listeners)
1513 || listener->priority >= QTAILQ_LAST(&memory_listeners,
1514 memory_listeners)->priority) {
1515 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
1516 } else {
1517 QTAILQ_FOREACH(other, &memory_listeners, link) {
1518 if (listener->priority < other->priority) {
1519 break;
1522 QTAILQ_INSERT_BEFORE(other, listener, link);
1525 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1526 listener_add_address_space(listener, as);
1530 void memory_listener_unregister(MemoryListener *listener)
1532 QTAILQ_REMOVE(&memory_listeners, listener, link);
1535 void address_space_init(AddressSpace *as, MemoryRegion *root)
1537 memory_region_transaction_begin();
1538 as->root = root;
1539 as->current_map = g_new(FlatView, 1);
1540 flatview_init(as->current_map);
1541 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
1542 as->name = NULL;
1543 memory_region_transaction_commit();
1544 address_space_init_dispatch(as);
1547 void address_space_destroy(AddressSpace *as)
1549 /* Flush out anything from MemoryListeners listening in on this */
1550 memory_region_transaction_begin();
1551 as->root = NULL;
1552 memory_region_transaction_commit();
1553 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
1554 address_space_destroy_dispatch(as);
1555 flatview_destroy(as->current_map);
1556 g_free(as->current_map);
1559 uint64_t io_mem_read(MemoryRegion *mr, target_phys_addr_t addr, unsigned size)
1561 return memory_region_dispatch_read(mr, addr, size);
1564 void io_mem_write(MemoryRegion *mr, target_phys_addr_t addr,
1565 uint64_t val, unsigned size)
1567 memory_region_dispatch_write(mr, addr, val, size);
1570 typedef struct MemoryRegionList MemoryRegionList;
1572 struct MemoryRegionList {
1573 const MemoryRegion *mr;
1574 bool printed;
1575 QTAILQ_ENTRY(MemoryRegionList) queue;
1578 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1580 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1581 const MemoryRegion *mr, unsigned int level,
1582 target_phys_addr_t base,
1583 MemoryRegionListHead *alias_print_queue)
1585 MemoryRegionList *new_ml, *ml, *next_ml;
1586 MemoryRegionListHead submr_print_queue;
1587 const MemoryRegion *submr;
1588 unsigned int i;
1590 if (!mr) {
1591 return;
1594 for (i = 0; i < level; i++) {
1595 mon_printf(f, " ");
1598 if (mr->alias) {
1599 MemoryRegionList *ml;
1600 bool found = false;
1602 /* check if the alias is already in the queue */
1603 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1604 if (ml->mr == mr->alias && !ml->printed) {
1605 found = true;
1609 if (!found) {
1610 ml = g_new(MemoryRegionList, 1);
1611 ml->mr = mr->alias;
1612 ml->printed = false;
1613 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1615 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
1616 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1617 "-" TARGET_FMT_plx "\n",
1618 base + mr->addr,
1619 base + mr->addr
1620 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1621 mr->priority,
1622 mr->readable ? 'R' : '-',
1623 !mr->readonly && !(mr->rom_device && mr->readable) ? 'W'
1624 : '-',
1625 mr->name,
1626 mr->alias->name,
1627 mr->alias_offset,
1628 mr->alias_offset
1629 + (target_phys_addr_t)int128_get64(mr->size) - 1);
1630 } else {
1631 mon_printf(f,
1632 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n",
1633 base + mr->addr,
1634 base + mr->addr
1635 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1636 mr->priority,
1637 mr->readable ? 'R' : '-',
1638 !mr->readonly && !(mr->rom_device && mr->readable) ? 'W'
1639 : '-',
1640 mr->name);
1643 QTAILQ_INIT(&submr_print_queue);
1645 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1646 new_ml = g_new(MemoryRegionList, 1);
1647 new_ml->mr = submr;
1648 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1649 if (new_ml->mr->addr < ml->mr->addr ||
1650 (new_ml->mr->addr == ml->mr->addr &&
1651 new_ml->mr->priority > ml->mr->priority)) {
1652 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1653 new_ml = NULL;
1654 break;
1657 if (new_ml) {
1658 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1662 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1663 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1664 alias_print_queue);
1667 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1668 g_free(ml);
1672 void mtree_info(fprintf_function mon_printf, void *f)
1674 MemoryRegionListHead ml_head;
1675 MemoryRegionList *ml, *ml2;
1676 AddressSpace *as;
1678 QTAILQ_INIT(&ml_head);
1680 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1681 if (!as->name) {
1682 continue;
1684 mon_printf(f, "%s\n", as->name);
1685 mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
1688 mon_printf(f, "aliases\n");
1689 /* print aliased regions */
1690 QTAILQ_FOREACH(ml, &ml_head, queue) {
1691 if (!ml->printed) {
1692 mon_printf(f, "%s\n", ml->mr->name);
1693 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1697 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1698 g_free(ml);