2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
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 "exec/memory.h"
17 #include "exec/address-spaces.h"
18 #include "exec/ioport.h"
19 #include "qemu/bitops.h"
20 #include "qom/object.h"
21 #include "sysemu/kvm.h"
24 #include "exec/memory-internal.h"
26 //#define DEBUG_UNASSIGNED
28 static unsigned memory_region_transaction_depth
;
29 static bool memory_region_update_pending
;
30 static bool global_dirty_log
= false;
32 /* flat_view_mutex is taken around reading as->current_map; the critical
33 * section is extremely short, so I'm using a single mutex for every AS.
34 * We could also RCU for the read-side.
36 * The BQL is taken around transaction commits, hence both locks are taken
37 * while writing to as->current_map (with the BQL taken outside).
39 static QemuMutex flat_view_mutex
;
41 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
42 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
44 static QTAILQ_HEAD(, AddressSpace
) address_spaces
45 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
47 static void memory_init(void)
49 qemu_mutex_init(&flat_view_mutex
);
52 typedef struct AddrRange AddrRange
;
55 * Note using signed integers limits us to physical addresses at most
56 * 63 bits wide. They are needed for negative offsetting in aliases
57 * (large MemoryRegion::alias_offset).
64 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
66 return (AddrRange
) { start
, size
};
69 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
71 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
74 static Int128
addrrange_end(AddrRange r
)
76 return int128_add(r
.start
, r
.size
);
79 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
81 int128_addto(&range
.start
, delta
);
85 static bool addrrange_contains(AddrRange range
, Int128 addr
)
87 return int128_ge(addr
, range
.start
)
88 && int128_lt(addr
, addrrange_end(range
));
91 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
93 return addrrange_contains(r1
, r2
.start
)
94 || addrrange_contains(r2
, r1
.start
);
97 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
99 Int128 start
= int128_max(r1
.start
, r2
.start
);
100 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
101 return addrrange_make(start
, int128_sub(end
, start
));
104 enum ListenerDirection
{ Forward
, Reverse
};
106 static bool memory_listener_match(MemoryListener
*listener
,
107 MemoryRegionSection
*section
)
109 return !listener
->address_space_filter
110 || listener
->address_space_filter
== section
->address_space
;
113 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
115 MemoryListener *_listener; \
117 switch (_direction) { \
119 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
120 if (_listener->_callback) { \
121 _listener->_callback(_listener, ##_args); \
126 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
127 memory_listeners, link) { \
128 if (_listener->_callback) { \
129 _listener->_callback(_listener, ##_args); \
138 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
140 MemoryListener *_listener; \
142 switch (_direction) { \
144 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
145 if (_listener->_callback \
146 && memory_listener_match(_listener, _section)) { \
147 _listener->_callback(_listener, _section, ##_args); \
152 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
153 memory_listeners, link) { \
154 if (_listener->_callback \
155 && memory_listener_match(_listener, _section)) { \
156 _listener->_callback(_listener, _section, ##_args); \
165 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
166 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
167 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
169 .address_space = (as), \
170 .offset_within_region = (fr)->offset_in_region, \
171 .size = (fr)->addr.size, \
172 .offset_within_address_space = int128_get64((fr)->addr.start), \
173 .readonly = (fr)->readonly, \
176 struct CoalescedMemoryRange
{
178 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
181 struct MemoryRegionIoeventfd
{
188 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
189 MemoryRegionIoeventfd b
)
191 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
193 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
195 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
197 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
199 } else if (a
.match_data
< b
.match_data
) {
201 } else if (a
.match_data
> b
.match_data
) {
203 } else if (a
.match_data
) {
204 if (a
.data
< b
.data
) {
206 } else if (a
.data
> b
.data
) {
212 } else if (a
.e
> b
.e
) {
218 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
219 MemoryRegionIoeventfd b
)
221 return !memory_region_ioeventfd_before(a
, b
)
222 && !memory_region_ioeventfd_before(b
, a
);
225 typedef struct FlatRange FlatRange
;
226 typedef struct FlatView FlatView
;
228 /* Range of memory in the global map. Addresses are absolute. */
231 hwaddr offset_in_region
;
233 uint8_t dirty_log_mask
;
238 /* Flattened global view of current active memory hierarchy. Kept in sorted
245 unsigned nr_allocated
;
248 typedef struct AddressSpaceOps AddressSpaceOps
;
250 #define FOR_EACH_FLAT_RANGE(var, view) \
251 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
253 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
255 return a
->mr
== b
->mr
256 && addrrange_equal(a
->addr
, b
->addr
)
257 && a
->offset_in_region
== b
->offset_in_region
258 && a
->romd_mode
== b
->romd_mode
259 && a
->readonly
== b
->readonly
;
262 static void flatview_init(FlatView
*view
)
267 view
->nr_allocated
= 0;
270 /* Insert a range into a given position. Caller is responsible for maintaining
273 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
275 if (view
->nr
== view
->nr_allocated
) {
276 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
277 view
->ranges
= g_realloc(view
->ranges
,
278 view
->nr_allocated
* sizeof(*view
->ranges
));
280 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
281 (view
->nr
- pos
) * sizeof(FlatRange
));
282 view
->ranges
[pos
] = *range
;
283 memory_region_ref(range
->mr
);
287 static void flatview_destroy(FlatView
*view
)
291 for (i
= 0; i
< view
->nr
; i
++) {
292 memory_region_unref(view
->ranges
[i
].mr
);
294 g_free(view
->ranges
);
298 static void flatview_ref(FlatView
*view
)
300 atomic_inc(&view
->ref
);
303 static void flatview_unref(FlatView
*view
)
305 if (atomic_fetch_dec(&view
->ref
) == 1) {
306 flatview_destroy(view
);
310 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
312 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
314 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
316 int128_make64(r2
->offset_in_region
))
317 && r1
->dirty_log_mask
== r2
->dirty_log_mask
318 && r1
->romd_mode
== r2
->romd_mode
319 && r1
->readonly
== r2
->readonly
;
322 /* Attempt to simplify a view by merging adjacent ranges */
323 static void flatview_simplify(FlatView
*view
)
328 while (i
< view
->nr
) {
331 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
332 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
336 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
337 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
342 static void memory_region_oldmmio_read_accessor(void *opaque
,
349 MemoryRegion
*mr
= opaque
;
352 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
353 *value
|= (tmp
& mask
) << shift
;
356 static void memory_region_read_accessor(void *opaque
,
363 MemoryRegion
*mr
= opaque
;
366 if (mr
->flush_coalesced_mmio
) {
367 qemu_flush_coalesced_mmio_buffer();
369 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
370 *value
|= (tmp
& mask
) << shift
;
373 static void memory_region_oldmmio_write_accessor(void *opaque
,
380 MemoryRegion
*mr
= opaque
;
383 tmp
= (*value
>> shift
) & mask
;
384 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
387 static void memory_region_write_accessor(void *opaque
,
394 MemoryRegion
*mr
= opaque
;
397 if (mr
->flush_coalesced_mmio
) {
398 qemu_flush_coalesced_mmio_buffer();
400 tmp
= (*value
>> shift
) & mask
;
401 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
404 static void access_with_adjusted_size(hwaddr addr
,
407 unsigned access_size_min
,
408 unsigned access_size_max
,
409 void (*access
)(void *opaque
,
417 uint64_t access_mask
;
418 unsigned access_size
;
421 if (!access_size_min
) {
424 if (!access_size_max
) {
428 /* FIXME: support unaligned access? */
429 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
430 access_mask
= -1ULL >> (64 - access_size
* 8);
431 for (i
= 0; i
< size
; i
+= access_size
) {
432 #ifdef TARGET_WORDS_BIGENDIAN
433 access(opaque
, addr
+ i
, value
, access_size
,
434 (size
- access_size
- i
) * 8, access_mask
);
436 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
441 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
448 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
449 if (mr
== as
->root
) {
456 /* Render a memory region into the global view. Ranges in @view obscure
459 static void render_memory_region(FlatView
*view
,
465 MemoryRegion
*subregion
;
467 hwaddr offset_in_region
;
477 int128_addto(&base
, int128_make64(mr
->addr
));
478 readonly
|= mr
->readonly
;
480 tmp
= addrrange_make(base
, mr
->size
);
482 if (!addrrange_intersects(tmp
, clip
)) {
486 clip
= addrrange_intersection(tmp
, clip
);
489 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
490 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
491 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
495 /* Render subregions in priority order. */
496 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
497 render_memory_region(view
, subregion
, base
, clip
, readonly
);
500 if (!mr
->terminates
) {
504 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
509 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
510 fr
.romd_mode
= mr
->romd_mode
;
511 fr
.readonly
= readonly
;
513 /* Render the region itself into any gaps left by the current view. */
514 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
515 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
518 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
519 now
= int128_min(remain
,
520 int128_sub(view
->ranges
[i
].addr
.start
, base
));
521 fr
.offset_in_region
= offset_in_region
;
522 fr
.addr
= addrrange_make(base
, now
);
523 flatview_insert(view
, i
, &fr
);
525 int128_addto(&base
, now
);
526 offset_in_region
+= int128_get64(now
);
527 int128_subfrom(&remain
, now
);
529 now
= int128_sub(int128_min(int128_add(base
, remain
),
530 addrrange_end(view
->ranges
[i
].addr
)),
532 int128_addto(&base
, now
);
533 offset_in_region
+= int128_get64(now
);
534 int128_subfrom(&remain
, now
);
536 if (int128_nz(remain
)) {
537 fr
.offset_in_region
= offset_in_region
;
538 fr
.addr
= addrrange_make(base
, remain
);
539 flatview_insert(view
, i
, &fr
);
543 /* Render a memory topology into a list of disjoint absolute ranges. */
544 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
548 view
= g_new(FlatView
, 1);
552 render_memory_region(view
, mr
, int128_zero(),
553 addrrange_make(int128_zero(), int128_2_64()), false);
555 flatview_simplify(view
);
560 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
561 MemoryRegionIoeventfd
*fds_new
,
563 MemoryRegionIoeventfd
*fds_old
,
567 MemoryRegionIoeventfd
*fd
;
568 MemoryRegionSection section
;
570 /* Generate a symmetric difference of the old and new fd sets, adding
571 * and deleting as necessary.
575 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
576 if (iold
< fds_old_nb
577 && (inew
== fds_new_nb
578 || memory_region_ioeventfd_before(fds_old
[iold
],
581 section
= (MemoryRegionSection
) {
583 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
584 .size
= fd
->addr
.size
,
586 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
587 fd
->match_data
, fd
->data
, fd
->e
);
589 } else if (inew
< fds_new_nb
590 && (iold
== fds_old_nb
591 || memory_region_ioeventfd_before(fds_new
[inew
],
594 section
= (MemoryRegionSection
) {
596 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
597 .size
= fd
->addr
.size
,
599 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
600 fd
->match_data
, fd
->data
, fd
->e
);
609 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
613 qemu_mutex_lock(&flat_view_mutex
);
614 view
= as
->current_map
;
616 qemu_mutex_unlock(&flat_view_mutex
);
620 static void address_space_update_ioeventfds(AddressSpace
*as
)
624 unsigned ioeventfd_nb
= 0;
625 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
629 view
= address_space_get_flatview(as
);
630 FOR_EACH_FLAT_RANGE(fr
, view
) {
631 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
632 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
633 int128_sub(fr
->addr
.start
,
634 int128_make64(fr
->offset_in_region
)));
635 if (addrrange_intersects(fr
->addr
, tmp
)) {
637 ioeventfds
= g_realloc(ioeventfds
,
638 ioeventfd_nb
* sizeof(*ioeventfds
));
639 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
640 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
645 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
646 as
->ioeventfds
, as
->ioeventfd_nb
);
648 g_free(as
->ioeventfds
);
649 as
->ioeventfds
= ioeventfds
;
650 as
->ioeventfd_nb
= ioeventfd_nb
;
651 flatview_unref(view
);
654 static void address_space_update_topology_pass(AddressSpace
*as
,
655 const FlatView
*old_view
,
656 const FlatView
*new_view
,
660 FlatRange
*frold
, *frnew
;
662 /* Generate a symmetric difference of the old and new memory maps.
663 * Kill ranges in the old map, and instantiate ranges in the new map.
666 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
667 if (iold
< old_view
->nr
) {
668 frold
= &old_view
->ranges
[iold
];
672 if (inew
< new_view
->nr
) {
673 frnew
= &new_view
->ranges
[inew
];
680 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
681 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
682 && !flatrange_equal(frold
, frnew
)))) {
683 /* In old but not in new, or in both but attributes changed. */
686 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
690 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
691 /* In both and unchanged (except logging may have changed) */
694 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
695 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
696 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
697 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
698 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
708 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
717 static void address_space_update_topology(AddressSpace
*as
)
719 FlatView
*old_view
= address_space_get_flatview(as
);
720 FlatView
*new_view
= generate_memory_topology(as
->root
);
722 address_space_update_topology_pass(as
, old_view
, new_view
, false);
723 address_space_update_topology_pass(as
, old_view
, new_view
, true);
725 qemu_mutex_lock(&flat_view_mutex
);
726 flatview_unref(as
->current_map
);
727 as
->current_map
= new_view
;
728 qemu_mutex_unlock(&flat_view_mutex
);
730 /* Note that all the old MemoryRegions are still alive up to this
731 * point. This relieves most MemoryListeners from the need to
732 * ref/unref the MemoryRegions they get---unless they use them
733 * outside the iothread mutex, in which case precise reference
734 * counting is necessary.
736 flatview_unref(old_view
);
738 address_space_update_ioeventfds(as
);
741 void memory_region_transaction_begin(void)
743 qemu_flush_coalesced_mmio_buffer();
744 ++memory_region_transaction_depth
;
747 void memory_region_transaction_commit(void)
751 assert(memory_region_transaction_depth
);
752 --memory_region_transaction_depth
;
753 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
754 memory_region_update_pending
= false;
755 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
757 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
758 address_space_update_topology(as
);
761 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
765 static void memory_region_destructor_none(MemoryRegion
*mr
)
769 static void memory_region_destructor_ram(MemoryRegion
*mr
)
771 qemu_ram_free(mr
->ram_addr
);
774 static void memory_region_destructor_alias(MemoryRegion
*mr
)
776 memory_region_unref(mr
->alias
);
779 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
781 qemu_ram_free_from_ptr(mr
->ram_addr
);
784 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
786 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
789 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
791 #ifdef TARGET_WORDS_BIGENDIAN
792 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
794 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
798 void memory_region_init(MemoryRegion
*mr
,
803 mr
->ops
= &unassigned_mem_ops
;
806 mr
->iommu_ops
= NULL
;
808 mr
->size
= int128_make64(size
);
809 if (size
== UINT64_MAX
) {
810 mr
->size
= int128_2_64();
815 mr
->terminates
= false;
817 mr
->romd_mode
= true;
818 mr
->readonly
= false;
819 mr
->rom_device
= false;
820 mr
->destructor
= memory_region_destructor_none
;
822 mr
->may_overlap
= false;
824 QTAILQ_INIT(&mr
->subregions
);
825 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
826 QTAILQ_INIT(&mr
->coalesced
);
827 mr
->name
= g_strdup(name
);
828 mr
->dirty_log_mask
= 0;
829 mr
->ioeventfd_nb
= 0;
830 mr
->ioeventfds
= NULL
;
831 mr
->flush_coalesced_mmio
= false;
834 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
837 #ifdef DEBUG_UNASSIGNED
838 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
840 if (current_cpu
!= NULL
) {
841 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
846 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
847 uint64_t val
, unsigned size
)
849 #ifdef DEBUG_UNASSIGNED
850 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
852 if (current_cpu
!= NULL
) {
853 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
857 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
858 unsigned size
, bool is_write
)
863 const MemoryRegionOps unassigned_mem_ops
= {
864 .valid
.accepts
= unassigned_mem_accepts
,
865 .endianness
= DEVICE_NATIVE_ENDIAN
,
868 bool memory_region_access_valid(MemoryRegion
*mr
,
873 int access_size_min
, access_size_max
;
876 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
880 if (!mr
->ops
->valid
.accepts
) {
884 access_size_min
= mr
->ops
->valid
.min_access_size
;
885 if (!mr
->ops
->valid
.min_access_size
) {
889 access_size_max
= mr
->ops
->valid
.max_access_size
;
890 if (!mr
->ops
->valid
.max_access_size
) {
894 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
895 for (i
= 0; i
< size
; i
+= access_size
) {
896 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
905 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
912 access_with_adjusted_size(addr
, &data
, size
,
913 mr
->ops
->impl
.min_access_size
,
914 mr
->ops
->impl
.max_access_size
,
915 memory_region_read_accessor
, mr
);
917 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
918 memory_region_oldmmio_read_accessor
, mr
);
924 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
926 if (memory_region_wrong_endianness(mr
)) {
931 *data
= bswap16(*data
);
934 *data
= bswap32(*data
);
937 *data
= bswap64(*data
);
945 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
950 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
951 *pval
= unassigned_mem_read(mr
, addr
, size
);
955 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
956 adjust_endianness(mr
, pval
, size
);
960 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
965 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
966 unassigned_mem_write(mr
, addr
, data
, size
);
970 adjust_endianness(mr
, &data
, size
);
972 if (mr
->ops
->write
) {
973 access_with_adjusted_size(addr
, &data
, size
,
974 mr
->ops
->impl
.min_access_size
,
975 mr
->ops
->impl
.max_access_size
,
976 memory_region_write_accessor
, mr
);
978 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
979 memory_region_oldmmio_write_accessor
, mr
);
984 void memory_region_init_io(MemoryRegion
*mr
,
986 const MemoryRegionOps
*ops
,
991 memory_region_init(mr
, owner
, name
, size
);
994 mr
->terminates
= true;
995 mr
->ram_addr
= ~(ram_addr_t
)0;
998 void memory_region_init_ram(MemoryRegion
*mr
,
1003 memory_region_init(mr
, owner
, name
, size
);
1005 mr
->terminates
= true;
1006 mr
->destructor
= memory_region_destructor_ram
;
1007 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1010 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1016 memory_region_init(mr
, owner
, name
, size
);
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
,
1030 memory_region_init(mr
, owner
, name
, size
);
1031 memory_region_ref(orig
);
1032 mr
->destructor
= memory_region_destructor_alias
;
1034 mr
->alias_offset
= offset
;
1037 void memory_region_init_rom_device(MemoryRegion
*mr
,
1039 const MemoryRegionOps
*ops
,
1044 memory_region_init(mr
, owner
, name
, size
);
1046 mr
->opaque
= opaque
;
1047 mr
->terminates
= true;
1048 mr
->rom_device
= true;
1049 mr
->destructor
= memory_region_destructor_rom_device
;
1050 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1053 void memory_region_init_iommu(MemoryRegion
*mr
,
1055 const MemoryRegionIOMMUOps
*ops
,
1059 memory_region_init(mr
, owner
, name
, size
);
1060 mr
->iommu_ops
= ops
,
1061 mr
->terminates
= true; /* then re-forwards */
1062 notifier_list_init(&mr
->iommu_notify
);
1065 void memory_region_init_reservation(MemoryRegion
*mr
,
1070 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1073 void memory_region_destroy(MemoryRegion
*mr
)
1075 assert(QTAILQ_EMPTY(&mr
->subregions
));
1076 assert(memory_region_transaction_depth
== 0);
1078 memory_region_clear_coalescing(mr
);
1079 g_free((char *)mr
->name
);
1080 g_free(mr
->ioeventfds
);
1083 Object
*memory_region_owner(MemoryRegion
*mr
)
1088 void memory_region_ref(MemoryRegion
*mr
)
1090 if (mr
&& mr
->owner
) {
1091 object_ref(mr
->owner
);
1095 void memory_region_unref(MemoryRegion
*mr
)
1097 if (mr
&& mr
->owner
) {
1098 object_unref(mr
->owner
);
1102 uint64_t memory_region_size(MemoryRegion
*mr
)
1104 if (int128_eq(mr
->size
, int128_2_64())) {
1107 return int128_get64(mr
->size
);
1110 const char *memory_region_name(MemoryRegion
*mr
)
1115 bool memory_region_is_ram(MemoryRegion
*mr
)
1120 bool memory_region_is_logging(MemoryRegion
*mr
)
1122 return mr
->dirty_log_mask
;
1125 bool memory_region_is_rom(MemoryRegion
*mr
)
1127 return mr
->ram
&& mr
->readonly
;
1130 bool memory_region_is_iommu(MemoryRegion
*mr
)
1132 return mr
->iommu_ops
;
1135 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1137 notifier_list_add(&mr
->iommu_notify
, n
);
1140 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1145 void memory_region_notify_iommu(MemoryRegion
*mr
,
1146 IOMMUTLBEntry entry
)
1148 assert(memory_region_is_iommu(mr
));
1149 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1152 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1154 uint8_t mask
= 1 << client
;
1156 memory_region_transaction_begin();
1157 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1158 memory_region_update_pending
|= mr
->enabled
;
1159 memory_region_transaction_commit();
1162 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1163 hwaddr size
, unsigned client
)
1165 assert(mr
->terminates
);
1166 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1170 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1173 assert(mr
->terminates
);
1174 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1177 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1178 hwaddr size
, unsigned client
)
1181 assert(mr
->terminates
);
1182 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1185 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1186 mr
->ram_addr
+ addr
+ size
,
1193 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1198 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1199 FlatView
*view
= address_space_get_flatview(as
);
1200 FOR_EACH_FLAT_RANGE(fr
, view
) {
1202 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1205 flatview_unref(view
);
1209 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1211 if (mr
->readonly
!= readonly
) {
1212 memory_region_transaction_begin();
1213 mr
->readonly
= readonly
;
1214 memory_region_update_pending
|= mr
->enabled
;
1215 memory_region_transaction_commit();
1219 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1221 if (mr
->romd_mode
!= romd_mode
) {
1222 memory_region_transaction_begin();
1223 mr
->romd_mode
= romd_mode
;
1224 memory_region_update_pending
|= mr
->enabled
;
1225 memory_region_transaction_commit();
1229 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1230 hwaddr size
, unsigned client
)
1232 assert(mr
->terminates
);
1233 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1234 mr
->ram_addr
+ addr
+ size
,
1238 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1241 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1244 assert(mr
->terminates
);
1246 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1249 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1253 CoalescedMemoryRange
*cmr
;
1255 MemoryRegionSection section
;
1257 view
= address_space_get_flatview(as
);
1258 FOR_EACH_FLAT_RANGE(fr
, view
) {
1260 section
= (MemoryRegionSection
) {
1261 .address_space
= as
,
1262 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1263 .size
= fr
->addr
.size
,
1266 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1267 int128_get64(fr
->addr
.start
),
1268 int128_get64(fr
->addr
.size
));
1269 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1270 tmp
= addrrange_shift(cmr
->addr
,
1271 int128_sub(fr
->addr
.start
,
1272 int128_make64(fr
->offset_in_region
)));
1273 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1276 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1277 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1278 int128_get64(tmp
.start
),
1279 int128_get64(tmp
.size
));
1283 flatview_unref(view
);
1286 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1290 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1291 memory_region_update_coalesced_range_as(mr
, as
);
1295 void memory_region_set_coalescing(MemoryRegion
*mr
)
1297 memory_region_clear_coalescing(mr
);
1298 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1301 void memory_region_add_coalescing(MemoryRegion
*mr
,
1305 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1307 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1308 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1309 memory_region_update_coalesced_range(mr
);
1310 memory_region_set_flush_coalesced(mr
);
1313 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1315 CoalescedMemoryRange
*cmr
;
1317 qemu_flush_coalesced_mmio_buffer();
1318 mr
->flush_coalesced_mmio
= false;
1320 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1321 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1322 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1325 memory_region_update_coalesced_range(mr
);
1328 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1330 mr
->flush_coalesced_mmio
= true;
1333 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1335 qemu_flush_coalesced_mmio_buffer();
1336 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1337 mr
->flush_coalesced_mmio
= false;
1341 void memory_region_add_eventfd(MemoryRegion
*mr
,
1348 MemoryRegionIoeventfd mrfd
= {
1349 .addr
.start
= int128_make64(addr
),
1350 .addr
.size
= int128_make64(size
),
1351 .match_data
= match_data
,
1357 adjust_endianness(mr
, &mrfd
.data
, size
);
1358 memory_region_transaction_begin();
1359 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1360 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1365 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1366 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1367 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1368 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1369 mr
->ioeventfds
[i
] = mrfd
;
1370 memory_region_update_pending
|= mr
->enabled
;
1371 memory_region_transaction_commit();
1374 void memory_region_del_eventfd(MemoryRegion
*mr
,
1381 MemoryRegionIoeventfd mrfd
= {
1382 .addr
.start
= int128_make64(addr
),
1383 .addr
.size
= int128_make64(size
),
1384 .match_data
= match_data
,
1390 adjust_endianness(mr
, &mrfd
.data
, size
);
1391 memory_region_transaction_begin();
1392 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1393 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1397 assert(i
!= mr
->ioeventfd_nb
);
1398 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1399 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1401 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1402 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1403 memory_region_update_pending
|= mr
->enabled
;
1404 memory_region_transaction_commit();
1407 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1409 MemoryRegion
*subregion
)
1411 MemoryRegion
*other
;
1413 memory_region_transaction_begin();
1415 assert(!subregion
->parent
);
1416 memory_region_ref(subregion
);
1417 subregion
->parent
= mr
;
1418 subregion
->addr
= offset
;
1419 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1420 if (subregion
->may_overlap
|| other
->may_overlap
) {
1423 if (int128_ge(int128_make64(offset
),
1424 int128_add(int128_make64(other
->addr
), other
->size
))
1425 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1426 int128_make64(other
->addr
))) {
1430 printf("warning: subregion collision %llx/%llx (%s) "
1431 "vs %llx/%llx (%s)\n",
1432 (unsigned long long)offset
,
1433 (unsigned long long)int128_get64(subregion
->size
),
1435 (unsigned long long)other
->addr
,
1436 (unsigned long long)int128_get64(other
->size
),
1440 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1441 if (subregion
->priority
>= other
->priority
) {
1442 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1446 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1448 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1449 memory_region_transaction_commit();
1453 void memory_region_add_subregion(MemoryRegion
*mr
,
1455 MemoryRegion
*subregion
)
1457 subregion
->may_overlap
= false;
1458 subregion
->priority
= 0;
1459 memory_region_add_subregion_common(mr
, offset
, subregion
);
1462 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1464 MemoryRegion
*subregion
,
1467 subregion
->may_overlap
= true;
1468 subregion
->priority
= priority
;
1469 memory_region_add_subregion_common(mr
, offset
, subregion
);
1472 void memory_region_del_subregion(MemoryRegion
*mr
,
1473 MemoryRegion
*subregion
)
1475 memory_region_transaction_begin();
1476 assert(subregion
->parent
== mr
);
1477 subregion
->parent
= NULL
;
1478 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1479 memory_region_unref(subregion
);
1480 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1481 memory_region_transaction_commit();
1484 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1486 if (enabled
== mr
->enabled
) {
1489 memory_region_transaction_begin();
1490 mr
->enabled
= enabled
;
1491 memory_region_update_pending
= true;
1492 memory_region_transaction_commit();
1495 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1497 MemoryRegion
*parent
= mr
->parent
;
1498 unsigned priority
= mr
->priority
;
1499 bool may_overlap
= mr
->may_overlap
;
1501 if (addr
== mr
->addr
|| !parent
) {
1506 memory_region_transaction_begin();
1507 memory_region_ref(mr
);
1508 memory_region_del_subregion(parent
, mr
);
1510 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1512 memory_region_add_subregion(parent
, addr
, mr
);
1514 memory_region_unref(mr
);
1515 memory_region_transaction_commit();
1518 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1522 if (offset
== mr
->alias_offset
) {
1526 memory_region_transaction_begin();
1527 mr
->alias_offset
= offset
;
1528 memory_region_update_pending
|= mr
->enabled
;
1529 memory_region_transaction_commit();
1532 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1534 return mr
->ram_addr
;
1537 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1539 const AddrRange
*addr
= addr_
;
1540 const FlatRange
*fr
= fr_
;
1542 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1544 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1550 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1552 return bsearch(&addr
, view
->ranges
, view
->nr
,
1553 sizeof(FlatRange
), cmp_flatrange_addr
);
1556 bool memory_region_present(MemoryRegion
*parent
, hwaddr addr
)
1558 MemoryRegion
*mr
= memory_region_find(parent
, addr
, 1).mr
;
1562 memory_region_unref(mr
);
1566 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1567 hwaddr addr
, uint64_t size
)
1569 MemoryRegionSection ret
= { .mr
= NULL
};
1577 for (root
= mr
; root
->parent
; ) {
1578 root
= root
->parent
;
1582 as
= memory_region_to_address_space(root
);
1583 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1585 view
= address_space_get_flatview(as
);
1586 fr
= flatview_lookup(view
, range
);
1591 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1596 ret
.address_space
= as
;
1597 range
= addrrange_intersection(range
, fr
->addr
);
1598 ret
.offset_within_region
= fr
->offset_in_region
;
1599 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1601 ret
.size
= range
.size
;
1602 ret
.offset_within_address_space
= int128_get64(range
.start
);
1603 ret
.readonly
= fr
->readonly
;
1604 memory_region_ref(ret
.mr
);
1606 flatview_unref(view
);
1610 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1615 view
= address_space_get_flatview(as
);
1616 FOR_EACH_FLAT_RANGE(fr
, view
) {
1617 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1619 flatview_unref(view
);
1622 void memory_global_dirty_log_start(void)
1624 global_dirty_log
= true;
1625 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1628 void memory_global_dirty_log_stop(void)
1630 global_dirty_log
= false;
1631 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1634 static void listener_add_address_space(MemoryListener
*listener
,
1640 if (listener
->address_space_filter
1641 && listener
->address_space_filter
!= as
) {
1645 if (global_dirty_log
) {
1646 if (listener
->log_global_start
) {
1647 listener
->log_global_start(listener
);
1651 view
= address_space_get_flatview(as
);
1652 FOR_EACH_FLAT_RANGE(fr
, view
) {
1653 MemoryRegionSection section
= {
1655 .address_space
= as
,
1656 .offset_within_region
= fr
->offset_in_region
,
1657 .size
= fr
->addr
.size
,
1658 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1659 .readonly
= fr
->readonly
,
1661 if (listener
->region_add
) {
1662 listener
->region_add(listener
, §ion
);
1665 flatview_unref(view
);
1668 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1670 MemoryListener
*other
= NULL
;
1673 listener
->address_space_filter
= filter
;
1674 if (QTAILQ_EMPTY(&memory_listeners
)
1675 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1676 memory_listeners
)->priority
) {
1677 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1679 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1680 if (listener
->priority
< other
->priority
) {
1684 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1687 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1688 listener_add_address_space(listener
, as
);
1692 void memory_listener_unregister(MemoryListener
*listener
)
1694 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1697 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1699 if (QTAILQ_EMPTY(&address_spaces
)) {
1703 memory_region_transaction_begin();
1705 as
->current_map
= g_new(FlatView
, 1);
1706 flatview_init(as
->current_map
);
1707 as
->ioeventfd_nb
= 0;
1708 as
->ioeventfds
= NULL
;
1709 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1710 as
->name
= g_strdup(name
? name
: "anonymous");
1711 address_space_init_dispatch(as
);
1712 memory_region_update_pending
|= root
->enabled
;
1713 memory_region_transaction_commit();
1716 void address_space_destroy(AddressSpace
*as
)
1718 /* Flush out anything from MemoryListeners listening in on this */
1719 memory_region_transaction_begin();
1721 memory_region_transaction_commit();
1722 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1723 address_space_destroy_dispatch(as
);
1724 flatview_unref(as
->current_map
);
1726 g_free(as
->ioeventfds
);
1729 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1731 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1734 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1735 uint64_t val
, unsigned size
)
1737 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1740 typedef struct MemoryRegionList MemoryRegionList
;
1742 struct MemoryRegionList
{
1743 const MemoryRegion
*mr
;
1745 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1748 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1750 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1751 const MemoryRegion
*mr
, unsigned int level
,
1753 MemoryRegionListHead
*alias_print_queue
)
1755 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1756 MemoryRegionListHead submr_print_queue
;
1757 const MemoryRegion
*submr
;
1760 if (!mr
|| !mr
->enabled
) {
1764 for (i
= 0; i
< level
; i
++) {
1769 MemoryRegionList
*ml
;
1772 /* check if the alias is already in the queue */
1773 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1774 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1780 ml
= g_new(MemoryRegionList
, 1);
1782 ml
->printed
= false;
1783 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1785 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1786 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1787 "-" TARGET_FMT_plx
"\n",
1790 + (hwaddr
)int128_get64(int128_sub(mr
->size
, int128_make64(1))),
1792 mr
->romd_mode
? 'R' : '-',
1793 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1799 + (hwaddr
)int128_get64(mr
->size
) - 1);
1802 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1805 + (hwaddr
)int128_get64(int128_sub(mr
->size
, int128_make64(1))),
1807 mr
->romd_mode
? 'R' : '-',
1808 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1813 QTAILQ_INIT(&submr_print_queue
);
1815 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1816 new_ml
= g_new(MemoryRegionList
, 1);
1818 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1819 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1820 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1821 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1822 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1828 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1832 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1833 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1837 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1842 void mtree_info(fprintf_function mon_printf
, void *f
)
1844 MemoryRegionListHead ml_head
;
1845 MemoryRegionList
*ml
, *ml2
;
1848 QTAILQ_INIT(&ml_head
);
1850 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1851 mon_printf(f
, "%s\n", as
->name
);
1852 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1855 mon_printf(f
, "aliases\n");
1856 /* print aliased regions */
1857 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1859 mon_printf(f
, "%s\n", ml
->mr
->name
);
1860 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1864 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {