Sort RAMBlocks by ID for migration, not by ram_addr
[qemu-kvm.git] / memory.c
blobf7b3d50a1acfe6936231fc28eb3a58f5567b9623
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 memory_region_prepare_ram_addr(MemoryRegion *mr);
308 static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
310 ram_addr_t phys_offset, region_offset;
312 memory_region_prepare_ram_addr(fr->mr);
314 phys_offset = fr->mr->ram_addr;
315 region_offset = fr->offset_in_region;
316 /* cpu_register_physical_memory_log() wants region_offset for
317 * mmio, but prefers offseting phys_offset for RAM. Humour it.
319 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
320 phys_offset += region_offset;
321 region_offset = 0;
324 if (!fr->readable) {
325 phys_offset &= ~TARGET_PAGE_MASK & ~IO_MEM_ROMD;
328 if (fr->readonly) {
329 phys_offset |= IO_MEM_ROM;
332 cpu_register_physical_memory_log(int128_get64(fr->addr.start),
333 int128_get64(fr->addr.size),
334 phys_offset,
335 region_offset,
336 fr->dirty_log_mask);
339 static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
341 cpu_register_physical_memory(int128_get64(fr->addr.start),
342 int128_get64(fr->addr.size),
343 IO_MEM_UNASSIGNED);
346 static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
350 static void as_memory_log_stop(AddressSpace *as, FlatRange *fr)
354 static void as_memory_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
356 int r;
358 assert(fd->match_data && int128_get64(fd->addr.size) == 4);
360 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
361 fd->data, true);
362 if (r < 0) {
363 abort();
367 static void as_memory_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
369 int r;
371 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
372 fd->data, false);
373 if (r < 0) {
374 abort();
378 static const AddressSpaceOps address_space_ops_memory = {
379 .range_add = as_memory_range_add,
380 .range_del = as_memory_range_del,
381 .log_start = as_memory_log_start,
382 .log_stop = as_memory_log_stop,
383 .ioeventfd_add = as_memory_ioeventfd_add,
384 .ioeventfd_del = as_memory_ioeventfd_del,
387 static AddressSpace address_space_memory = {
388 .ops = &address_space_ops_memory,
391 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
392 unsigned width, bool write)
394 const MemoryRegionPortio *mrp;
396 for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
397 if (offset >= mrp->offset && offset < mrp->offset + mrp->len
398 && width == mrp->size
399 && (write ? (bool)mrp->write : (bool)mrp->read)) {
400 return mrp;
403 return NULL;
406 static void memory_region_iorange_read(IORange *iorange,
407 uint64_t offset,
408 unsigned width,
409 uint64_t *data)
411 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
413 if (mr->ops->old_portio) {
414 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, false);
416 *data = ((uint64_t)1 << (width * 8)) - 1;
417 if (mrp) {
418 *data = mrp->read(mr->opaque, offset + mr->offset);
419 } else if (width == 2) {
420 mrp = find_portio(mr, offset, 1, false);
421 assert(mrp);
422 *data = mrp->read(mr->opaque, offset + mr->offset) |
423 (mrp->read(mr->opaque, offset + mr->offset + 1) << 8);
425 return;
427 *data = 0;
428 access_with_adjusted_size(offset + mr->offset, data, width,
429 mr->ops->impl.min_access_size,
430 mr->ops->impl.max_access_size,
431 memory_region_read_accessor, mr);
434 static void memory_region_iorange_write(IORange *iorange,
435 uint64_t offset,
436 unsigned width,
437 uint64_t data)
439 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
441 if (mr->ops->old_portio) {
442 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true);
444 if (mrp) {
445 mrp->write(mr->opaque, offset + mr->offset, data);
446 } else if (width == 2) {
447 mrp = find_portio(mr, offset, 1, false);
448 assert(mrp);
449 mrp->write(mr->opaque, offset + mr->offset, data & 0xff);
450 mrp->write(mr->opaque, offset + mr->offset + 1, data >> 8);
452 return;
454 access_with_adjusted_size(offset + mr->offset, &data, width,
455 mr->ops->impl.min_access_size,
456 mr->ops->impl.max_access_size,
457 memory_region_write_accessor, mr);
460 static const IORangeOps memory_region_iorange_ops = {
461 .read = memory_region_iorange_read,
462 .write = memory_region_iorange_write,
465 static void as_io_range_add(AddressSpace *as, FlatRange *fr)
467 iorange_init(&fr->mr->iorange, &memory_region_iorange_ops,
468 int128_get64(fr->addr.start), int128_get64(fr->addr.size));
469 ioport_register(&fr->mr->iorange);
472 static void as_io_range_del(AddressSpace *as, FlatRange *fr)
474 isa_unassign_ioport(int128_get64(fr->addr.start),
475 int128_get64(fr->addr.size));
478 static void as_io_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
480 int r;
482 assert(fd->match_data && int128_get64(fd->addr.size) == 2);
484 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
485 fd->data, true);
486 if (r < 0) {
487 abort();
491 static void as_io_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
493 int r;
495 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
496 fd->data, false);
497 if (r < 0) {
498 abort();
502 static const AddressSpaceOps address_space_ops_io = {
503 .range_add = as_io_range_add,
504 .range_del = as_io_range_del,
505 .ioeventfd_add = as_io_ioeventfd_add,
506 .ioeventfd_del = as_io_ioeventfd_del,
509 static AddressSpace address_space_io = {
510 .ops = &address_space_ops_io,
513 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
515 while (mr->parent) {
516 mr = mr->parent;
518 if (mr == address_space_memory.root) {
519 return &address_space_memory;
521 if (mr == address_space_io.root) {
522 return &address_space_io;
524 abort();
527 /* Render a memory region into the global view. Ranges in @view obscure
528 * ranges in @mr.
530 static void render_memory_region(FlatView *view,
531 MemoryRegion *mr,
532 Int128 base,
533 AddrRange clip,
534 bool readonly)
536 MemoryRegion *subregion;
537 unsigned i;
538 target_phys_addr_t offset_in_region;
539 Int128 remain;
540 Int128 now;
541 FlatRange fr;
542 AddrRange tmp;
544 if (!mr->enabled) {
545 return;
548 int128_addto(&base, int128_make64(mr->addr));
549 readonly |= mr->readonly;
551 tmp = addrrange_make(base, mr->size);
553 if (!addrrange_intersects(tmp, clip)) {
554 return;
557 clip = addrrange_intersection(tmp, clip);
559 if (mr->alias) {
560 int128_subfrom(&base, int128_make64(mr->alias->addr));
561 int128_subfrom(&base, int128_make64(mr->alias_offset));
562 render_memory_region(view, mr->alias, base, clip, readonly);
563 return;
566 /* Render subregions in priority order. */
567 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
568 render_memory_region(view, subregion, base, clip, readonly);
571 if (!mr->terminates) {
572 return;
575 offset_in_region = int128_get64(int128_sub(clip.start, base));
576 base = clip.start;
577 remain = clip.size;
579 /* Render the region itself into any gaps left by the current view. */
580 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
581 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
582 continue;
584 if (int128_lt(base, view->ranges[i].addr.start)) {
585 now = int128_min(remain,
586 int128_sub(view->ranges[i].addr.start, base));
587 fr.mr = mr;
588 fr.offset_in_region = offset_in_region;
589 fr.addr = addrrange_make(base, now);
590 fr.dirty_log_mask = mr->dirty_log_mask;
591 fr.readable = mr->readable;
592 fr.readonly = readonly;
593 flatview_insert(view, i, &fr);
594 ++i;
595 int128_addto(&base, now);
596 offset_in_region += int128_get64(now);
597 int128_subfrom(&remain, now);
599 if (int128_eq(base, view->ranges[i].addr.start)) {
600 now = int128_min(remain, view->ranges[i].addr.size);
601 int128_addto(&base, now);
602 offset_in_region += int128_get64(now);
603 int128_subfrom(&remain, now);
606 if (int128_nz(remain)) {
607 fr.mr = mr;
608 fr.offset_in_region = offset_in_region;
609 fr.addr = addrrange_make(base, remain);
610 fr.dirty_log_mask = mr->dirty_log_mask;
611 fr.readable = mr->readable;
612 fr.readonly = readonly;
613 flatview_insert(view, i, &fr);
617 /* Render a memory topology into a list of disjoint absolute ranges. */
618 static FlatView generate_memory_topology(MemoryRegion *mr)
620 FlatView view;
622 flatview_init(&view);
624 render_memory_region(&view, mr, int128_zero(),
625 addrrange_make(int128_zero(), int128_2_64()), false);
626 flatview_simplify(&view);
628 return view;
631 static void address_space_add_del_ioeventfds(AddressSpace *as,
632 MemoryRegionIoeventfd *fds_new,
633 unsigned fds_new_nb,
634 MemoryRegionIoeventfd *fds_old,
635 unsigned fds_old_nb)
637 unsigned iold, inew;
639 /* Generate a symmetric difference of the old and new fd sets, adding
640 * and deleting as necessary.
643 iold = inew = 0;
644 while (iold < fds_old_nb || inew < fds_new_nb) {
645 if (iold < fds_old_nb
646 && (inew == fds_new_nb
647 || memory_region_ioeventfd_before(fds_old[iold],
648 fds_new[inew]))) {
649 as->ops->ioeventfd_del(as, &fds_old[iold]);
650 ++iold;
651 } else if (inew < fds_new_nb
652 && (iold == fds_old_nb
653 || memory_region_ioeventfd_before(fds_new[inew],
654 fds_old[iold]))) {
655 as->ops->ioeventfd_add(as, &fds_new[inew]);
656 ++inew;
657 } else {
658 ++iold;
659 ++inew;
664 static void address_space_update_ioeventfds(AddressSpace *as)
666 FlatRange *fr;
667 unsigned ioeventfd_nb = 0;
668 MemoryRegionIoeventfd *ioeventfds = NULL;
669 AddrRange tmp;
670 unsigned i;
672 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
673 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
674 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
675 int128_sub(fr->addr.start,
676 int128_make64(fr->offset_in_region)));
677 if (addrrange_intersects(fr->addr, tmp)) {
678 ++ioeventfd_nb;
679 ioeventfds = g_realloc(ioeventfds,
680 ioeventfd_nb * sizeof(*ioeventfds));
681 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
682 ioeventfds[ioeventfd_nb-1].addr = tmp;
687 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
688 as->ioeventfds, as->ioeventfd_nb);
690 g_free(as->ioeventfds);
691 as->ioeventfds = ioeventfds;
692 as->ioeventfd_nb = ioeventfd_nb;
695 typedef void ListenerCallback(MemoryListener *listener,
696 MemoryRegionSection *mrs);
698 /* Want "void (&MemoryListener::*callback)(const MemoryRegionSection& s)" */
699 static void memory_listener_update_region(FlatRange *fr, AddressSpace *as,
700 size_t callback_offset)
702 MemoryRegionSection section = {
703 .mr = fr->mr,
704 .address_space = as->root,
705 .offset_within_region = fr->offset_in_region,
706 .size = int128_get64(fr->addr.size),
707 .offset_within_address_space = int128_get64(fr->addr.start),
709 MemoryListener *listener;
711 QLIST_FOREACH(listener, &memory_listeners, link) {
712 ListenerCallback *callback
713 = *(ListenerCallback **)((void *)listener + callback_offset);
714 callback(listener, &section);
718 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, callback) \
719 memory_listener_update_region(fr, as, offsetof(MemoryListener, callback))
721 static void address_space_update_topology_pass(AddressSpace *as,
722 FlatView old_view,
723 FlatView new_view,
724 bool adding)
726 unsigned iold, inew;
727 FlatRange *frold, *frnew;
729 /* Generate a symmetric difference of the old and new memory maps.
730 * Kill ranges in the old map, and instantiate ranges in the new map.
732 iold = inew = 0;
733 while (iold < old_view.nr || inew < new_view.nr) {
734 if (iold < old_view.nr) {
735 frold = &old_view.ranges[iold];
736 } else {
737 frold = NULL;
739 if (inew < new_view.nr) {
740 frnew = &new_view.ranges[inew];
741 } else {
742 frnew = NULL;
745 if (frold
746 && (!frnew
747 || int128_lt(frold->addr.start, frnew->addr.start)
748 || (int128_eq(frold->addr.start, frnew->addr.start)
749 && !flatrange_equal(frold, frnew)))) {
750 /* In old, but (not in new, or in new but attributes changed). */
752 if (!adding) {
753 MEMORY_LISTENER_UPDATE_REGION(frold, as, region_del);
754 as->ops->range_del(as, frold);
757 ++iold;
758 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
759 /* In both (logging may have changed) */
761 if (adding) {
762 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
763 MEMORY_LISTENER_UPDATE_REGION(frnew, as, log_stop);
764 as->ops->log_stop(as, frnew);
765 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
766 as->ops->log_start(as, frnew);
767 MEMORY_LISTENER_UPDATE_REGION(frnew, as, log_start);
771 ++iold;
772 ++inew;
773 } else {
774 /* In new */
776 if (adding) {
777 as->ops->range_add(as, frnew);
778 MEMORY_LISTENER_UPDATE_REGION(frnew, as, region_add);
781 ++inew;
787 static void address_space_update_topology(AddressSpace *as)
789 FlatView old_view = as->current_map;
790 FlatView new_view = generate_memory_topology(as->root);
792 address_space_update_topology_pass(as, old_view, new_view, false);
793 address_space_update_topology_pass(as, old_view, new_view, true);
795 as->current_map = new_view;
796 flatview_destroy(&old_view);
797 address_space_update_ioeventfds(as);
800 static void memory_region_update_topology(MemoryRegion *mr)
802 if (memory_region_transaction_depth) {
803 memory_region_update_pending |= !mr || mr->enabled;
804 return;
807 if (mr && !mr->enabled) {
808 return;
811 if (address_space_memory.root) {
812 address_space_update_topology(&address_space_memory);
814 if (address_space_io.root) {
815 address_space_update_topology(&address_space_io);
818 memory_region_update_pending = false;
821 void memory_region_transaction_begin(void)
823 ++memory_region_transaction_depth;
826 void memory_region_transaction_commit(void)
828 assert(memory_region_transaction_depth);
829 --memory_region_transaction_depth;
830 if (!memory_region_transaction_depth && memory_region_update_pending) {
831 memory_region_update_topology(NULL);
835 static void memory_region_destructor_none(MemoryRegion *mr)
839 static void memory_region_destructor_ram(MemoryRegion *mr)
841 qemu_ram_free(mr->ram_addr);
844 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
846 qemu_ram_free_from_ptr(mr->ram_addr);
849 static void memory_region_destructor_iomem(MemoryRegion *mr)
851 cpu_unregister_io_memory(mr->ram_addr);
854 static void memory_region_destructor_rom_device(MemoryRegion *mr)
856 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
857 cpu_unregister_io_memory(mr->ram_addr & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
860 void memory_region_init(MemoryRegion *mr,
861 const char *name,
862 uint64_t size)
864 mr->ops = NULL;
865 mr->parent = NULL;
866 mr->size = int128_make64(size);
867 if (size == UINT64_MAX) {
868 mr->size = int128_2_64();
870 mr->addr = 0;
871 mr->offset = 0;
872 mr->enabled = true;
873 mr->terminates = false;
874 mr->ram = false;
875 mr->readable = true;
876 mr->readonly = false;
877 mr->destructor = memory_region_destructor_none;
878 mr->priority = 0;
879 mr->may_overlap = false;
880 mr->alias = NULL;
881 QTAILQ_INIT(&mr->subregions);
882 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
883 QTAILQ_INIT(&mr->coalesced);
884 mr->name = g_strdup(name);
885 mr->dirty_log_mask = 0;
886 mr->ioeventfd_nb = 0;
887 mr->ioeventfds = NULL;
890 static bool memory_region_access_valid(MemoryRegion *mr,
891 target_phys_addr_t addr,
892 unsigned size,
893 bool is_write)
895 if (mr->ops->valid.accepts
896 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
897 return false;
900 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
901 return false;
904 /* Treat zero as compatibility all valid */
905 if (!mr->ops->valid.max_access_size) {
906 return true;
909 if (size > mr->ops->valid.max_access_size
910 || size < mr->ops->valid.min_access_size) {
911 return false;
913 return true;
916 static uint32_t memory_region_read_thunk_n(void *_mr,
917 target_phys_addr_t addr,
918 unsigned size)
920 MemoryRegion *mr = _mr;
921 uint64_t data = 0;
923 if (!memory_region_access_valid(mr, addr, size, false)) {
924 return -1U; /* FIXME: better signalling */
927 if (!mr->ops->read) {
928 return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr);
931 /* FIXME: support unaligned access */
932 access_with_adjusted_size(addr + mr->offset, &data, size,
933 mr->ops->impl.min_access_size,
934 mr->ops->impl.max_access_size,
935 memory_region_read_accessor, mr);
937 return data;
940 static void memory_region_write_thunk_n(void *_mr,
941 target_phys_addr_t addr,
942 unsigned size,
943 uint64_t data)
945 MemoryRegion *mr = _mr;
947 if (!memory_region_access_valid(mr, addr, size, true)) {
948 return; /* FIXME: better signalling */
951 if (!mr->ops->write) {
952 mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data);
953 return;
956 /* FIXME: support unaligned access */
957 access_with_adjusted_size(addr + mr->offset, &data, size,
958 mr->ops->impl.min_access_size,
959 mr->ops->impl.max_access_size,
960 memory_region_write_accessor, mr);
963 static uint32_t memory_region_read_thunk_b(void *mr, target_phys_addr_t addr)
965 return memory_region_read_thunk_n(mr, addr, 1);
968 static uint32_t memory_region_read_thunk_w(void *mr, target_phys_addr_t addr)
970 return memory_region_read_thunk_n(mr, addr, 2);
973 static uint32_t memory_region_read_thunk_l(void *mr, target_phys_addr_t addr)
975 return memory_region_read_thunk_n(mr, addr, 4);
978 static void memory_region_write_thunk_b(void *mr, target_phys_addr_t addr,
979 uint32_t data)
981 memory_region_write_thunk_n(mr, addr, 1, data);
984 static void memory_region_write_thunk_w(void *mr, target_phys_addr_t addr,
985 uint32_t data)
987 memory_region_write_thunk_n(mr, addr, 2, data);
990 static void memory_region_write_thunk_l(void *mr, target_phys_addr_t addr,
991 uint32_t data)
993 memory_region_write_thunk_n(mr, addr, 4, data);
996 static CPUReadMemoryFunc * const memory_region_read_thunk[] = {
997 memory_region_read_thunk_b,
998 memory_region_read_thunk_w,
999 memory_region_read_thunk_l,
1002 static CPUWriteMemoryFunc * const memory_region_write_thunk[] = {
1003 memory_region_write_thunk_b,
1004 memory_region_write_thunk_w,
1005 memory_region_write_thunk_l,
1008 static void memory_region_prepare_ram_addr(MemoryRegion *mr)
1010 if (mr->backend_registered) {
1011 return;
1014 mr->destructor = memory_region_destructor_iomem;
1015 mr->ram_addr = cpu_register_io_memory(memory_region_read_thunk,
1016 memory_region_write_thunk,
1018 mr->ops->endianness);
1019 mr->backend_registered = true;
1022 void memory_region_init_io(MemoryRegion *mr,
1023 const MemoryRegionOps *ops,
1024 void *opaque,
1025 const char *name,
1026 uint64_t size)
1028 memory_region_init(mr, name, size);
1029 mr->ops = ops;
1030 mr->opaque = opaque;
1031 mr->terminates = true;
1032 mr->backend_registered = false;
1035 void memory_region_init_ram(MemoryRegion *mr,
1036 const char *name,
1037 uint64_t size)
1039 memory_region_init(mr, name, size);
1040 mr->ram = true;
1041 mr->terminates = true;
1042 mr->destructor = memory_region_destructor_ram;
1043 mr->ram_addr = qemu_ram_alloc(size, mr);
1044 mr->backend_registered = true;
1047 void memory_region_init_ram_ptr(MemoryRegion *mr,
1048 const char *name,
1049 uint64_t size,
1050 void *ptr)
1052 memory_region_init(mr, name, size);
1053 mr->ram = true;
1054 mr->terminates = true;
1055 mr->destructor = memory_region_destructor_ram_from_ptr;
1056 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1057 mr->backend_registered = true;
1060 void memory_region_init_alias(MemoryRegion *mr,
1061 const char *name,
1062 MemoryRegion *orig,
1063 target_phys_addr_t offset,
1064 uint64_t size)
1066 memory_region_init(mr, name, size);
1067 mr->alias = orig;
1068 mr->alias_offset = offset;
1071 void memory_region_init_rom_device(MemoryRegion *mr,
1072 const MemoryRegionOps *ops,
1073 void *opaque,
1074 const char *name,
1075 uint64_t size)
1077 memory_region_init(mr, name, size);
1078 mr->ops = ops;
1079 mr->opaque = opaque;
1080 mr->terminates = true;
1081 mr->destructor = memory_region_destructor_rom_device;
1082 mr->ram_addr = qemu_ram_alloc(size, mr);
1083 mr->ram_addr |= cpu_register_io_memory(memory_region_read_thunk,
1084 memory_region_write_thunk,
1086 mr->ops->endianness);
1087 mr->ram_addr |= IO_MEM_ROMD;
1088 mr->backend_registered = true;
1091 void memory_region_destroy(MemoryRegion *mr)
1093 assert(QTAILQ_EMPTY(&mr->subregions));
1094 mr->destructor(mr);
1095 memory_region_clear_coalescing(mr);
1096 g_free((char *)mr->name);
1097 g_free(mr->ioeventfds);
1100 uint64_t memory_region_size(MemoryRegion *mr)
1102 if (int128_eq(mr->size, int128_2_64())) {
1103 return UINT64_MAX;
1105 return int128_get64(mr->size);
1108 const char *memory_region_name(MemoryRegion *mr)
1110 return mr->name;
1113 bool memory_region_is_ram(MemoryRegion *mr)
1115 return mr->ram;
1118 bool memory_region_is_logging(MemoryRegion *mr)
1120 return mr->dirty_log_mask;
1123 bool memory_region_is_rom(MemoryRegion *mr)
1125 return mr->ram && mr->readonly;
1128 void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset)
1130 mr->offset = offset;
1133 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1135 uint8_t mask = 1 << client;
1137 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1138 memory_region_update_topology(mr);
1141 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1142 unsigned client)
1144 assert(mr->terminates);
1145 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, 1 << client);
1148 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr)
1150 assert(mr->terminates);
1151 return cpu_physical_memory_set_dirty(mr->ram_addr + addr);
1154 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1156 FlatRange *fr;
1158 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1159 if (fr->mr == mr) {
1160 MEMORY_LISTENER_UPDATE_REGION(fr, &address_space_memory, log_sync);
1165 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1167 if (mr->readonly != readonly) {
1168 mr->readonly = readonly;
1169 memory_region_update_topology(mr);
1173 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
1175 if (mr->readable != readable) {
1176 mr->readable = readable;
1177 memory_region_update_topology(mr);
1181 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1182 target_phys_addr_t size, unsigned client)
1184 assert(mr->terminates);
1185 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1186 mr->ram_addr + addr + size,
1187 1 << client);
1190 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1192 if (mr->alias) {
1193 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1196 assert(mr->terminates);
1198 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1201 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1203 FlatRange *fr;
1204 CoalescedMemoryRange *cmr;
1205 AddrRange tmp;
1207 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1208 if (fr->mr == mr) {
1209 qemu_unregister_coalesced_mmio(int128_get64(fr->addr.start),
1210 int128_get64(fr->addr.size));
1211 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1212 tmp = addrrange_shift(cmr->addr,
1213 int128_sub(fr->addr.start,
1214 int128_make64(fr->offset_in_region)));
1215 if (!addrrange_intersects(tmp, fr->addr)) {
1216 continue;
1218 tmp = addrrange_intersection(tmp, fr->addr);
1219 qemu_register_coalesced_mmio(int128_get64(tmp.start),
1220 int128_get64(tmp.size));
1226 void memory_region_set_coalescing(MemoryRegion *mr)
1228 memory_region_clear_coalescing(mr);
1229 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1232 void memory_region_add_coalescing(MemoryRegion *mr,
1233 target_phys_addr_t offset,
1234 uint64_t size)
1236 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1238 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1239 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1240 memory_region_update_coalesced_range(mr);
1243 void memory_region_clear_coalescing(MemoryRegion *mr)
1245 CoalescedMemoryRange *cmr;
1247 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1248 cmr = QTAILQ_FIRST(&mr->coalesced);
1249 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1250 g_free(cmr);
1252 memory_region_update_coalesced_range(mr);
1255 void memory_region_add_eventfd(MemoryRegion *mr,
1256 target_phys_addr_t addr,
1257 unsigned size,
1258 bool match_data,
1259 uint64_t data,
1260 int fd)
1262 MemoryRegionIoeventfd mrfd = {
1263 .addr.start = int128_make64(addr),
1264 .addr.size = int128_make64(size),
1265 .match_data = match_data,
1266 .data = data,
1267 .fd = fd,
1269 unsigned i;
1271 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1272 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1273 break;
1276 ++mr->ioeventfd_nb;
1277 mr->ioeventfds = g_realloc(mr->ioeventfds,
1278 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1279 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1280 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1281 mr->ioeventfds[i] = mrfd;
1282 memory_region_update_topology(mr);
1285 void memory_region_del_eventfd(MemoryRegion *mr,
1286 target_phys_addr_t addr,
1287 unsigned size,
1288 bool match_data,
1289 uint64_t data,
1290 int fd)
1292 MemoryRegionIoeventfd mrfd = {
1293 .addr.start = int128_make64(addr),
1294 .addr.size = int128_make64(size),
1295 .match_data = match_data,
1296 .data = data,
1297 .fd = fd,
1299 unsigned i;
1301 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1302 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1303 break;
1306 assert(i != mr->ioeventfd_nb);
1307 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1308 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1309 --mr->ioeventfd_nb;
1310 mr->ioeventfds = g_realloc(mr->ioeventfds,
1311 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1312 memory_region_update_topology(mr);
1315 static void memory_region_add_subregion_common(MemoryRegion *mr,
1316 target_phys_addr_t offset,
1317 MemoryRegion *subregion)
1319 MemoryRegion *other;
1321 assert(!subregion->parent);
1322 subregion->parent = mr;
1323 subregion->addr = offset;
1324 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1325 if (subregion->may_overlap || other->may_overlap) {
1326 continue;
1328 if (int128_gt(int128_make64(offset),
1329 int128_add(int128_make64(other->addr), other->size))
1330 || int128_le(int128_add(int128_make64(offset), subregion->size),
1331 int128_make64(other->addr))) {
1332 continue;
1334 #if 0
1335 printf("warning: subregion collision %llx/%llx (%s) "
1336 "vs %llx/%llx (%s)\n",
1337 (unsigned long long)offset,
1338 (unsigned long long)int128_get64(subregion->size),
1339 subregion->name,
1340 (unsigned long long)other->addr,
1341 (unsigned long long)int128_get64(other->size),
1342 other->name);
1343 #endif
1345 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1346 if (subregion->priority >= other->priority) {
1347 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1348 goto done;
1351 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1352 done:
1353 memory_region_update_topology(mr);
1357 void memory_region_add_subregion(MemoryRegion *mr,
1358 target_phys_addr_t offset,
1359 MemoryRegion *subregion)
1361 subregion->may_overlap = false;
1362 subregion->priority = 0;
1363 memory_region_add_subregion_common(mr, offset, subregion);
1366 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1367 target_phys_addr_t offset,
1368 MemoryRegion *subregion,
1369 unsigned priority)
1371 subregion->may_overlap = true;
1372 subregion->priority = priority;
1373 memory_region_add_subregion_common(mr, offset, subregion);
1376 void memory_region_del_subregion(MemoryRegion *mr,
1377 MemoryRegion *subregion)
1379 assert(subregion->parent == mr);
1380 subregion->parent = NULL;
1381 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1382 memory_region_update_topology(mr);
1385 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1387 if (enabled == mr->enabled) {
1388 return;
1390 mr->enabled = enabled;
1391 memory_region_update_topology(NULL);
1394 void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr)
1396 MemoryRegion *parent = mr->parent;
1397 unsigned priority = mr->priority;
1398 bool may_overlap = mr->may_overlap;
1400 if (addr == mr->addr || !parent) {
1401 mr->addr = addr;
1402 return;
1405 memory_region_transaction_begin();
1406 memory_region_del_subregion(parent, mr);
1407 if (may_overlap) {
1408 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1409 } else {
1410 memory_region_add_subregion(parent, addr, mr);
1412 memory_region_transaction_commit();
1415 void memory_region_set_alias_offset(MemoryRegion *mr, target_phys_addr_t offset)
1417 target_phys_addr_t old_offset = mr->alias_offset;
1419 assert(mr->alias);
1420 mr->alias_offset = offset;
1422 if (offset == old_offset || !mr->parent) {
1423 return;
1426 memory_region_update_topology(mr);
1429 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1431 assert(mr->backend_registered);
1432 return mr->ram_addr;
1435 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1437 const AddrRange *addr = addr_;
1438 const FlatRange *fr = fr_;
1440 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1441 return -1;
1442 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1443 return 1;
1445 return 0;
1448 static FlatRange *address_space_lookup(AddressSpace *as, AddrRange addr)
1450 return bsearch(&addr, as->current_map.ranges, as->current_map.nr,
1451 sizeof(FlatRange), cmp_flatrange_addr);
1454 MemoryRegionSection memory_region_find(MemoryRegion *address_space,
1455 target_phys_addr_t addr, uint64_t size)
1457 AddressSpace *as = memory_region_to_address_space(address_space);
1458 AddrRange range = addrrange_make(int128_make64(addr),
1459 int128_make64(size));
1460 FlatRange *fr = address_space_lookup(as, range);
1461 MemoryRegionSection ret = { .mr = NULL, .size = 0 };
1463 if (!fr) {
1464 return ret;
1467 while (fr > as->current_map.ranges
1468 && addrrange_intersects(fr[-1].addr, range)) {
1469 --fr;
1472 ret.mr = fr->mr;
1473 range = addrrange_intersection(range, fr->addr);
1474 ret.offset_within_region = fr->offset_in_region;
1475 ret.offset_within_region += int128_get64(int128_sub(range.start,
1476 fr->addr.start));
1477 ret.size = int128_get64(range.size);
1478 ret.offset_within_address_space = int128_get64(range.start);
1479 return ret;
1482 void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
1484 AddressSpace *as = memory_region_to_address_space(address_space);
1485 FlatRange *fr;
1487 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
1488 MEMORY_LISTENER_UPDATE_REGION(fr, as, log_sync);
1492 void memory_global_dirty_log_start(void)
1494 MemoryListener *listener;
1496 global_dirty_log = true;
1497 QLIST_FOREACH(listener, &memory_listeners, link) {
1498 listener->log_global_start(listener);
1502 void memory_global_dirty_log_stop(void)
1504 MemoryListener *listener;
1506 global_dirty_log = false;
1507 QLIST_FOREACH(listener, &memory_listeners, link) {
1508 listener->log_global_stop(listener);
1512 static void listener_add_address_space(MemoryListener *listener,
1513 AddressSpace *as)
1515 FlatRange *fr;
1517 if (global_dirty_log) {
1518 listener->log_global_start(listener);
1520 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
1521 MemoryRegionSection section = {
1522 .mr = fr->mr,
1523 .address_space = as->root,
1524 .offset_within_region = fr->offset_in_region,
1525 .size = int128_get64(fr->addr.size),
1526 .offset_within_address_space = int128_get64(fr->addr.start),
1528 listener->region_add(listener, &section);
1532 void memory_listener_register(MemoryListener *listener)
1534 QLIST_INSERT_HEAD(&memory_listeners, listener, link);
1535 listener_add_address_space(listener, &address_space_memory);
1536 listener_add_address_space(listener, &address_space_io);
1539 void memory_listener_unregister(MemoryListener *listener)
1541 QLIST_REMOVE(listener, link);
1544 void set_system_memory_map(MemoryRegion *mr)
1546 address_space_memory.root = mr;
1547 memory_region_update_topology(NULL);
1550 void set_system_io_map(MemoryRegion *mr)
1552 address_space_io.root = mr;
1553 memory_region_update_topology(NULL);
1556 typedef struct MemoryRegionList MemoryRegionList;
1558 struct MemoryRegionList {
1559 const MemoryRegion *mr;
1560 bool printed;
1561 QTAILQ_ENTRY(MemoryRegionList) queue;
1564 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1566 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1567 const MemoryRegion *mr, unsigned int level,
1568 target_phys_addr_t base,
1569 MemoryRegionListHead *alias_print_queue)
1571 MemoryRegionList *new_ml, *ml, *next_ml;
1572 MemoryRegionListHead submr_print_queue;
1573 const MemoryRegion *submr;
1574 unsigned int i;
1576 if (!mr) {
1577 return;
1580 for (i = 0; i < level; i++) {
1581 mon_printf(f, " ");
1584 if (mr->alias) {
1585 MemoryRegionList *ml;
1586 bool found = false;
1588 /* check if the alias is already in the queue */
1589 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1590 if (ml->mr == mr->alias && !ml->printed) {
1591 found = true;
1595 if (!found) {
1596 ml = g_new(MemoryRegionList, 1);
1597 ml->mr = mr->alias;
1598 ml->printed = false;
1599 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1601 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): alias %s @%s "
1602 TARGET_FMT_plx "-" TARGET_FMT_plx "\n",
1603 base + mr->addr,
1604 base + mr->addr
1605 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1606 mr->priority,
1607 mr->name,
1608 mr->alias->name,
1609 mr->alias_offset,
1610 mr->alias_offset
1611 + (target_phys_addr_t)int128_get64(mr->size) - 1);
1612 } else {
1613 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): %s\n",
1614 base + mr->addr,
1615 base + mr->addr
1616 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1617 mr->priority,
1618 mr->name);
1621 QTAILQ_INIT(&submr_print_queue);
1623 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1624 new_ml = g_new(MemoryRegionList, 1);
1625 new_ml->mr = submr;
1626 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1627 if (new_ml->mr->addr < ml->mr->addr ||
1628 (new_ml->mr->addr == ml->mr->addr &&
1629 new_ml->mr->priority > ml->mr->priority)) {
1630 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1631 new_ml = NULL;
1632 break;
1635 if (new_ml) {
1636 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1640 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1641 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1642 alias_print_queue);
1645 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1646 g_free(ml);
1650 void mtree_info(fprintf_function mon_printf, void *f)
1652 MemoryRegionListHead ml_head;
1653 MemoryRegionList *ml, *ml2;
1655 QTAILQ_INIT(&ml_head);
1657 mon_printf(f, "memory\n");
1658 mtree_print_mr(mon_printf, f, address_space_memory.root, 0, 0, &ml_head);
1660 /* print aliased regions */
1661 QTAILQ_FOREACH(ml, &ml_head, queue) {
1662 if (!ml->printed) {
1663 mon_printf(f, "%s\n", ml->mr->name);
1664 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1668 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1669 g_free(ml);
1672 if (address_space_io.root &&
1673 !QTAILQ_EMPTY(&address_space_io.root->subregions)) {
1674 QTAILQ_INIT(&ml_head);
1675 mon_printf(f, "I/O\n");
1676 mtree_print_mr(mon_printf, f, address_space_io.root, 0, 0, &ml_head);