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 bool memory_region_big_endian(MemoryRegion
*mr
)
344 #ifdef TARGET_WORDS_BIGENDIAN
345 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
347 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
351 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
353 #ifdef TARGET_WORDS_BIGENDIAN
354 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
356 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
360 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
362 if (memory_region_wrong_endianness(mr
)) {
367 *data
= bswap16(*data
);
370 *data
= bswap32(*data
);
373 *data
= bswap64(*data
);
381 static void memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
390 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
391 *value
|= (tmp
& mask
) << shift
;
394 static void memory_region_read_accessor(MemoryRegion
*mr
,
403 if (mr
->flush_coalesced_mmio
) {
404 qemu_flush_coalesced_mmio_buffer();
406 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
407 *value
|= (tmp
& mask
) << shift
;
410 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
419 tmp
= (*value
>> shift
) & mask
;
420 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
423 static void memory_region_write_accessor(MemoryRegion
*mr
,
432 if (mr
->flush_coalesced_mmio
) {
433 qemu_flush_coalesced_mmio_buffer();
435 tmp
= (*value
>> shift
) & mask
;
436 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
439 static void access_with_adjusted_size(hwaddr addr
,
442 unsigned access_size_min
,
443 unsigned access_size_max
,
444 void (*access
)(MemoryRegion
*mr
,
452 uint64_t access_mask
;
453 unsigned access_size
;
456 if (!access_size_min
) {
459 if (!access_size_max
) {
463 /* FIXME: support unaligned access? */
464 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
465 access_mask
= -1ULL >> (64 - access_size
* 8);
466 if (memory_region_big_endian(mr
)) {
467 for (i
= 0; i
< size
; i
+= access_size
) {
468 access(mr
, addr
+ i
, value
, access_size
,
469 (size
- access_size
- i
) * 8, access_mask
);
472 for (i
= 0; i
< size
; i
+= access_size
) {
473 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
478 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
485 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
486 if (mr
== as
->root
) {
493 /* Render a memory region into the global view. Ranges in @view obscure
496 static void render_memory_region(FlatView
*view
,
502 MemoryRegion
*subregion
;
504 hwaddr offset_in_region
;
514 int128_addto(&base
, int128_make64(mr
->addr
));
515 readonly
|= mr
->readonly
;
517 tmp
= addrrange_make(base
, mr
->size
);
519 if (!addrrange_intersects(tmp
, clip
)) {
523 clip
= addrrange_intersection(tmp
, clip
);
526 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
527 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
528 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
532 /* Render subregions in priority order. */
533 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
534 render_memory_region(view
, subregion
, base
, clip
, readonly
);
537 if (!mr
->terminates
) {
541 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
546 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
547 fr
.romd_mode
= mr
->romd_mode
;
548 fr
.readonly
= readonly
;
550 /* Render the region itself into any gaps left by the current view. */
551 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
552 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
555 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
556 now
= int128_min(remain
,
557 int128_sub(view
->ranges
[i
].addr
.start
, base
));
558 fr
.offset_in_region
= offset_in_region
;
559 fr
.addr
= addrrange_make(base
, now
);
560 flatview_insert(view
, i
, &fr
);
562 int128_addto(&base
, now
);
563 offset_in_region
+= int128_get64(now
);
564 int128_subfrom(&remain
, now
);
566 now
= int128_sub(int128_min(int128_add(base
, remain
),
567 addrrange_end(view
->ranges
[i
].addr
)),
569 int128_addto(&base
, now
);
570 offset_in_region
+= int128_get64(now
);
571 int128_subfrom(&remain
, now
);
573 if (int128_nz(remain
)) {
574 fr
.offset_in_region
= offset_in_region
;
575 fr
.addr
= addrrange_make(base
, remain
);
576 flatview_insert(view
, i
, &fr
);
580 /* Render a memory topology into a list of disjoint absolute ranges. */
581 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
585 view
= g_new(FlatView
, 1);
589 render_memory_region(view
, mr
, int128_zero(),
590 addrrange_make(int128_zero(), int128_2_64()), false);
592 flatview_simplify(view
);
597 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
598 MemoryRegionIoeventfd
*fds_new
,
600 MemoryRegionIoeventfd
*fds_old
,
604 MemoryRegionIoeventfd
*fd
;
605 MemoryRegionSection section
;
607 /* Generate a symmetric difference of the old and new fd sets, adding
608 * and deleting as necessary.
612 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
613 if (iold
< fds_old_nb
614 && (inew
== fds_new_nb
615 || memory_region_ioeventfd_before(fds_old
[iold
],
618 section
= (MemoryRegionSection
) {
620 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
621 .size
= fd
->addr
.size
,
623 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
624 fd
->match_data
, fd
->data
, fd
->e
);
626 } else if (inew
< fds_new_nb
627 && (iold
== fds_old_nb
628 || memory_region_ioeventfd_before(fds_new
[inew
],
631 section
= (MemoryRegionSection
) {
633 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
634 .size
= fd
->addr
.size
,
636 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
637 fd
->match_data
, fd
->data
, fd
->e
);
646 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
650 qemu_mutex_lock(&flat_view_mutex
);
651 view
= as
->current_map
;
653 qemu_mutex_unlock(&flat_view_mutex
);
657 static void address_space_update_ioeventfds(AddressSpace
*as
)
661 unsigned ioeventfd_nb
= 0;
662 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
666 view
= address_space_get_flatview(as
);
667 FOR_EACH_FLAT_RANGE(fr
, view
) {
668 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
669 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
670 int128_sub(fr
->addr
.start
,
671 int128_make64(fr
->offset_in_region
)));
672 if (addrrange_intersects(fr
->addr
, tmp
)) {
674 ioeventfds
= g_realloc(ioeventfds
,
675 ioeventfd_nb
* sizeof(*ioeventfds
));
676 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
677 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
682 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
683 as
->ioeventfds
, as
->ioeventfd_nb
);
685 g_free(as
->ioeventfds
);
686 as
->ioeventfds
= ioeventfds
;
687 as
->ioeventfd_nb
= ioeventfd_nb
;
688 flatview_unref(view
);
691 static void address_space_update_topology_pass(AddressSpace
*as
,
692 const FlatView
*old_view
,
693 const FlatView
*new_view
,
697 FlatRange
*frold
, *frnew
;
699 /* Generate a symmetric difference of the old and new memory maps.
700 * Kill ranges in the old map, and instantiate ranges in the new map.
703 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
704 if (iold
< old_view
->nr
) {
705 frold
= &old_view
->ranges
[iold
];
709 if (inew
< new_view
->nr
) {
710 frnew
= &new_view
->ranges
[inew
];
717 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
718 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
719 && !flatrange_equal(frold
, frnew
)))) {
720 /* In old but not in new, or in both but attributes changed. */
723 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
727 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
728 /* In both and unchanged (except logging may have changed) */
731 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
732 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
733 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
734 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
735 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
745 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
754 static void address_space_update_topology(AddressSpace
*as
)
756 FlatView
*old_view
= address_space_get_flatview(as
);
757 FlatView
*new_view
= generate_memory_topology(as
->root
);
759 address_space_update_topology_pass(as
, old_view
, new_view
, false);
760 address_space_update_topology_pass(as
, old_view
, new_view
, true);
762 qemu_mutex_lock(&flat_view_mutex
);
763 flatview_unref(as
->current_map
);
764 as
->current_map
= new_view
;
765 qemu_mutex_unlock(&flat_view_mutex
);
767 /* Note that all the old MemoryRegions are still alive up to this
768 * point. This relieves most MemoryListeners from the need to
769 * ref/unref the MemoryRegions they get---unless they use them
770 * outside the iothread mutex, in which case precise reference
771 * counting is necessary.
773 flatview_unref(old_view
);
775 address_space_update_ioeventfds(as
);
778 void memory_region_transaction_begin(void)
780 qemu_flush_coalesced_mmio_buffer();
781 ++memory_region_transaction_depth
;
784 void memory_region_transaction_commit(void)
788 assert(memory_region_transaction_depth
);
789 --memory_region_transaction_depth
;
790 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
791 memory_region_update_pending
= false;
792 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
794 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
795 address_space_update_topology(as
);
798 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
802 static void memory_region_destructor_none(MemoryRegion
*mr
)
806 static void memory_region_destructor_ram(MemoryRegion
*mr
)
808 qemu_ram_free(mr
->ram_addr
);
811 static void memory_region_destructor_alias(MemoryRegion
*mr
)
813 memory_region_unref(mr
->alias
);
816 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
818 qemu_ram_free_from_ptr(mr
->ram_addr
);
821 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
823 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
826 void memory_region_init(MemoryRegion
*mr
,
831 mr
->ops
= &unassigned_mem_ops
;
834 mr
->iommu_ops
= NULL
;
836 mr
->size
= int128_make64(size
);
837 if (size
== UINT64_MAX
) {
838 mr
->size
= int128_2_64();
843 mr
->terminates
= false;
845 mr
->romd_mode
= true;
846 mr
->readonly
= false;
847 mr
->rom_device
= false;
848 mr
->destructor
= memory_region_destructor_none
;
850 mr
->may_overlap
= false;
852 QTAILQ_INIT(&mr
->subregions
);
853 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
854 QTAILQ_INIT(&mr
->coalesced
);
855 mr
->name
= g_strdup(name
);
856 mr
->dirty_log_mask
= 0;
857 mr
->ioeventfd_nb
= 0;
858 mr
->ioeventfds
= NULL
;
859 mr
->flush_coalesced_mmio
= false;
862 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
865 #ifdef DEBUG_UNASSIGNED
866 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
868 if (current_cpu
!= NULL
) {
869 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
874 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
875 uint64_t val
, unsigned size
)
877 #ifdef DEBUG_UNASSIGNED
878 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
880 if (current_cpu
!= NULL
) {
881 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
885 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
886 unsigned size
, bool is_write
)
891 const MemoryRegionOps unassigned_mem_ops
= {
892 .valid
.accepts
= unassigned_mem_accepts
,
893 .endianness
= DEVICE_NATIVE_ENDIAN
,
896 bool memory_region_access_valid(MemoryRegion
*mr
,
901 int access_size_min
, access_size_max
;
904 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
908 if (!mr
->ops
->valid
.accepts
) {
912 access_size_min
= mr
->ops
->valid
.min_access_size
;
913 if (!mr
->ops
->valid
.min_access_size
) {
917 access_size_max
= mr
->ops
->valid
.max_access_size
;
918 if (!mr
->ops
->valid
.max_access_size
) {
922 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
923 for (i
= 0; i
< size
; i
+= access_size
) {
924 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
933 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
940 access_with_adjusted_size(addr
, &data
, size
,
941 mr
->ops
->impl
.min_access_size
,
942 mr
->ops
->impl
.max_access_size
,
943 memory_region_read_accessor
, mr
);
945 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
946 memory_region_oldmmio_read_accessor
, mr
);
952 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
957 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
958 *pval
= unassigned_mem_read(mr
, addr
, size
);
962 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
963 adjust_endianness(mr
, pval
, size
);
967 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
972 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
973 unassigned_mem_write(mr
, addr
, data
, size
);
977 adjust_endianness(mr
, &data
, size
);
979 if (mr
->ops
->write
) {
980 access_with_adjusted_size(addr
, &data
, size
,
981 mr
->ops
->impl
.min_access_size
,
982 mr
->ops
->impl
.max_access_size
,
983 memory_region_write_accessor
, mr
);
985 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
986 memory_region_oldmmio_write_accessor
, mr
);
991 void memory_region_init_io(MemoryRegion
*mr
,
993 const MemoryRegionOps
*ops
,
998 memory_region_init(mr
, owner
, name
, size
);
1000 mr
->opaque
= opaque
;
1001 mr
->terminates
= true;
1002 mr
->ram_addr
= ~(ram_addr_t
)0;
1005 void memory_region_init_ram(MemoryRegion
*mr
,
1010 memory_region_init(mr
, owner
, name
, size
);
1012 mr
->terminates
= true;
1013 mr
->destructor
= memory_region_destructor_ram
;
1014 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1017 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1023 memory_region_init(mr
, owner
, name
, size
);
1025 mr
->terminates
= true;
1026 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1027 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1030 void memory_region_init_alias(MemoryRegion
*mr
,
1037 memory_region_init(mr
, owner
, name
, size
);
1038 memory_region_ref(orig
);
1039 mr
->destructor
= memory_region_destructor_alias
;
1041 mr
->alias_offset
= offset
;
1044 void memory_region_init_rom_device(MemoryRegion
*mr
,
1046 const MemoryRegionOps
*ops
,
1051 memory_region_init(mr
, owner
, name
, size
);
1053 mr
->opaque
= opaque
;
1054 mr
->terminates
= true;
1055 mr
->rom_device
= true;
1056 mr
->destructor
= memory_region_destructor_rom_device
;
1057 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1060 void memory_region_init_iommu(MemoryRegion
*mr
,
1062 const MemoryRegionIOMMUOps
*ops
,
1066 memory_region_init(mr
, owner
, name
, size
);
1067 mr
->iommu_ops
= ops
,
1068 mr
->terminates
= true; /* then re-forwards */
1069 notifier_list_init(&mr
->iommu_notify
);
1072 void memory_region_init_reservation(MemoryRegion
*mr
,
1077 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1080 void memory_region_destroy(MemoryRegion
*mr
)
1082 assert(QTAILQ_EMPTY(&mr
->subregions
));
1083 assert(memory_region_transaction_depth
== 0);
1085 memory_region_clear_coalescing(mr
);
1086 g_free((char *)mr
->name
);
1087 g_free(mr
->ioeventfds
);
1090 Object
*memory_region_owner(MemoryRegion
*mr
)
1095 void memory_region_ref(MemoryRegion
*mr
)
1097 if (mr
&& mr
->owner
) {
1098 object_ref(mr
->owner
);
1102 void memory_region_unref(MemoryRegion
*mr
)
1104 if (mr
&& mr
->owner
) {
1105 object_unref(mr
->owner
);
1109 uint64_t memory_region_size(MemoryRegion
*mr
)
1111 if (int128_eq(mr
->size
, int128_2_64())) {
1114 return int128_get64(mr
->size
);
1117 const char *memory_region_name(MemoryRegion
*mr
)
1122 bool memory_region_is_ram(MemoryRegion
*mr
)
1127 bool memory_region_is_logging(MemoryRegion
*mr
)
1129 return mr
->dirty_log_mask
;
1132 bool memory_region_is_rom(MemoryRegion
*mr
)
1134 return mr
->ram
&& mr
->readonly
;
1137 bool memory_region_is_iommu(MemoryRegion
*mr
)
1139 return mr
->iommu_ops
;
1142 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1144 notifier_list_add(&mr
->iommu_notify
, n
);
1147 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1152 void memory_region_notify_iommu(MemoryRegion
*mr
,
1153 IOMMUTLBEntry entry
)
1155 assert(memory_region_is_iommu(mr
));
1156 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1159 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1161 uint8_t mask
= 1 << client
;
1163 memory_region_transaction_begin();
1164 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1165 memory_region_update_pending
|= mr
->enabled
;
1166 memory_region_transaction_commit();
1169 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1170 hwaddr size
, unsigned client
)
1172 assert(mr
->terminates
);
1173 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1177 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1180 assert(mr
->terminates
);
1181 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1184 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1185 hwaddr size
, unsigned client
)
1188 assert(mr
->terminates
);
1189 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1192 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1193 mr
->ram_addr
+ addr
+ size
,
1200 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1205 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1206 FlatView
*view
= address_space_get_flatview(as
);
1207 FOR_EACH_FLAT_RANGE(fr
, view
) {
1209 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1212 flatview_unref(view
);
1216 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1218 if (mr
->readonly
!= readonly
) {
1219 memory_region_transaction_begin();
1220 mr
->readonly
= readonly
;
1221 memory_region_update_pending
|= mr
->enabled
;
1222 memory_region_transaction_commit();
1226 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1228 if (mr
->romd_mode
!= romd_mode
) {
1229 memory_region_transaction_begin();
1230 mr
->romd_mode
= romd_mode
;
1231 memory_region_update_pending
|= mr
->enabled
;
1232 memory_region_transaction_commit();
1236 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1237 hwaddr size
, unsigned client
)
1239 assert(mr
->terminates
);
1240 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1241 mr
->ram_addr
+ addr
+ size
,
1245 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1248 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1251 assert(mr
->terminates
);
1253 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1256 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1260 CoalescedMemoryRange
*cmr
;
1262 MemoryRegionSection section
;
1264 view
= address_space_get_flatview(as
);
1265 FOR_EACH_FLAT_RANGE(fr
, view
) {
1267 section
= (MemoryRegionSection
) {
1268 .address_space
= as
,
1269 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1270 .size
= fr
->addr
.size
,
1273 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1274 int128_get64(fr
->addr
.start
),
1275 int128_get64(fr
->addr
.size
));
1276 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1277 tmp
= addrrange_shift(cmr
->addr
,
1278 int128_sub(fr
->addr
.start
,
1279 int128_make64(fr
->offset_in_region
)));
1280 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1283 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1284 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1285 int128_get64(tmp
.start
),
1286 int128_get64(tmp
.size
));
1290 flatview_unref(view
);
1293 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1297 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1298 memory_region_update_coalesced_range_as(mr
, as
);
1302 void memory_region_set_coalescing(MemoryRegion
*mr
)
1304 memory_region_clear_coalescing(mr
);
1305 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1308 void memory_region_add_coalescing(MemoryRegion
*mr
,
1312 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1314 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1315 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1316 memory_region_update_coalesced_range(mr
);
1317 memory_region_set_flush_coalesced(mr
);
1320 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1322 CoalescedMemoryRange
*cmr
;
1324 qemu_flush_coalesced_mmio_buffer();
1325 mr
->flush_coalesced_mmio
= false;
1327 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1328 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1329 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1332 memory_region_update_coalesced_range(mr
);
1335 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1337 mr
->flush_coalesced_mmio
= true;
1340 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1342 qemu_flush_coalesced_mmio_buffer();
1343 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1344 mr
->flush_coalesced_mmio
= false;
1348 void memory_region_add_eventfd(MemoryRegion
*mr
,
1355 MemoryRegionIoeventfd mrfd
= {
1356 .addr
.start
= int128_make64(addr
),
1357 .addr
.size
= int128_make64(size
),
1358 .match_data
= match_data
,
1364 adjust_endianness(mr
, &mrfd
.data
, size
);
1365 memory_region_transaction_begin();
1366 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1367 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1372 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1373 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1374 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1375 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1376 mr
->ioeventfds
[i
] = mrfd
;
1377 memory_region_update_pending
|= mr
->enabled
;
1378 memory_region_transaction_commit();
1381 void memory_region_del_eventfd(MemoryRegion
*mr
,
1388 MemoryRegionIoeventfd mrfd
= {
1389 .addr
.start
= int128_make64(addr
),
1390 .addr
.size
= int128_make64(size
),
1391 .match_data
= match_data
,
1397 adjust_endianness(mr
, &mrfd
.data
, size
);
1398 memory_region_transaction_begin();
1399 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1400 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1404 assert(i
!= mr
->ioeventfd_nb
);
1405 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1406 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1408 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1409 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1410 memory_region_update_pending
|= mr
->enabled
;
1411 memory_region_transaction_commit();
1414 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1416 MemoryRegion
*subregion
)
1418 MemoryRegion
*other
;
1420 memory_region_transaction_begin();
1422 assert(!subregion
->parent
);
1423 memory_region_ref(subregion
);
1424 subregion
->parent
= mr
;
1425 subregion
->addr
= offset
;
1426 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1427 if (subregion
->may_overlap
|| other
->may_overlap
) {
1430 if (int128_ge(int128_make64(offset
),
1431 int128_add(int128_make64(other
->addr
), other
->size
))
1432 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1433 int128_make64(other
->addr
))) {
1437 printf("warning: subregion collision %llx/%llx (%s) "
1438 "vs %llx/%llx (%s)\n",
1439 (unsigned long long)offset
,
1440 (unsigned long long)int128_get64(subregion
->size
),
1442 (unsigned long long)other
->addr
,
1443 (unsigned long long)int128_get64(other
->size
),
1447 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1448 if (subregion
->priority
>= other
->priority
) {
1449 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1453 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1455 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1456 memory_region_transaction_commit();
1460 void memory_region_add_subregion(MemoryRegion
*mr
,
1462 MemoryRegion
*subregion
)
1464 subregion
->may_overlap
= false;
1465 subregion
->priority
= 0;
1466 memory_region_add_subregion_common(mr
, offset
, subregion
);
1469 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1471 MemoryRegion
*subregion
,
1474 subregion
->may_overlap
= true;
1475 subregion
->priority
= priority
;
1476 memory_region_add_subregion_common(mr
, offset
, subregion
);
1479 void memory_region_del_subregion(MemoryRegion
*mr
,
1480 MemoryRegion
*subregion
)
1482 memory_region_transaction_begin();
1483 assert(subregion
->parent
== mr
);
1484 subregion
->parent
= NULL
;
1485 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1486 memory_region_unref(subregion
);
1487 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1488 memory_region_transaction_commit();
1491 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1493 if (enabled
== mr
->enabled
) {
1496 memory_region_transaction_begin();
1497 mr
->enabled
= enabled
;
1498 memory_region_update_pending
= true;
1499 memory_region_transaction_commit();
1502 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1504 MemoryRegion
*parent
= mr
->parent
;
1505 unsigned priority
= mr
->priority
;
1506 bool may_overlap
= mr
->may_overlap
;
1508 if (addr
== mr
->addr
|| !parent
) {
1513 memory_region_transaction_begin();
1514 memory_region_ref(mr
);
1515 memory_region_del_subregion(parent
, mr
);
1517 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1519 memory_region_add_subregion(parent
, addr
, mr
);
1521 memory_region_unref(mr
);
1522 memory_region_transaction_commit();
1525 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1529 if (offset
== mr
->alias_offset
) {
1533 memory_region_transaction_begin();
1534 mr
->alias_offset
= offset
;
1535 memory_region_update_pending
|= mr
->enabled
;
1536 memory_region_transaction_commit();
1539 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1541 return mr
->ram_addr
;
1544 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1546 const AddrRange
*addr
= addr_
;
1547 const FlatRange
*fr
= fr_
;
1549 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1551 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1557 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1559 return bsearch(&addr
, view
->ranges
, view
->nr
,
1560 sizeof(FlatRange
), cmp_flatrange_addr
);
1563 bool memory_region_present(MemoryRegion
*parent
, hwaddr addr
)
1565 MemoryRegion
*mr
= memory_region_find(parent
, addr
, 1).mr
;
1569 memory_region_unref(mr
);
1573 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1574 hwaddr addr
, uint64_t size
)
1576 MemoryRegionSection ret
= { .mr
= NULL
};
1584 for (root
= mr
; root
->parent
; ) {
1585 root
= root
->parent
;
1589 as
= memory_region_to_address_space(root
);
1590 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1592 view
= address_space_get_flatview(as
);
1593 fr
= flatview_lookup(view
, range
);
1598 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1603 ret
.address_space
= as
;
1604 range
= addrrange_intersection(range
, fr
->addr
);
1605 ret
.offset_within_region
= fr
->offset_in_region
;
1606 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1608 ret
.size
= range
.size
;
1609 ret
.offset_within_address_space
= int128_get64(range
.start
);
1610 ret
.readonly
= fr
->readonly
;
1611 memory_region_ref(ret
.mr
);
1613 flatview_unref(view
);
1617 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1622 view
= address_space_get_flatview(as
);
1623 FOR_EACH_FLAT_RANGE(fr
, view
) {
1624 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1626 flatview_unref(view
);
1629 void memory_global_dirty_log_start(void)
1631 global_dirty_log
= true;
1632 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1635 void memory_global_dirty_log_stop(void)
1637 global_dirty_log
= false;
1638 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1641 static void listener_add_address_space(MemoryListener
*listener
,
1647 if (listener
->address_space_filter
1648 && listener
->address_space_filter
!= as
) {
1652 if (global_dirty_log
) {
1653 if (listener
->log_global_start
) {
1654 listener
->log_global_start(listener
);
1658 view
= address_space_get_flatview(as
);
1659 FOR_EACH_FLAT_RANGE(fr
, view
) {
1660 MemoryRegionSection section
= {
1662 .address_space
= as
,
1663 .offset_within_region
= fr
->offset_in_region
,
1664 .size
= fr
->addr
.size
,
1665 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1666 .readonly
= fr
->readonly
,
1668 if (listener
->region_add
) {
1669 listener
->region_add(listener
, §ion
);
1672 flatview_unref(view
);
1675 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1677 MemoryListener
*other
= NULL
;
1680 listener
->address_space_filter
= filter
;
1681 if (QTAILQ_EMPTY(&memory_listeners
)
1682 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1683 memory_listeners
)->priority
) {
1684 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1686 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1687 if (listener
->priority
< other
->priority
) {
1691 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1694 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1695 listener_add_address_space(listener
, as
);
1699 void memory_listener_unregister(MemoryListener
*listener
)
1701 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1704 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1706 if (QTAILQ_EMPTY(&address_spaces
)) {
1710 memory_region_transaction_begin();
1712 as
->current_map
= g_new(FlatView
, 1);
1713 flatview_init(as
->current_map
);
1714 as
->ioeventfd_nb
= 0;
1715 as
->ioeventfds
= NULL
;
1716 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1717 as
->name
= g_strdup(name
? name
: "anonymous");
1718 address_space_init_dispatch(as
);
1719 memory_region_update_pending
|= root
->enabled
;
1720 memory_region_transaction_commit();
1723 void address_space_destroy(AddressSpace
*as
)
1725 /* Flush out anything from MemoryListeners listening in on this */
1726 memory_region_transaction_begin();
1728 memory_region_transaction_commit();
1729 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1730 address_space_destroy_dispatch(as
);
1731 flatview_unref(as
->current_map
);
1733 g_free(as
->ioeventfds
);
1736 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1738 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1741 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1742 uint64_t val
, unsigned size
)
1744 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1747 typedef struct MemoryRegionList MemoryRegionList
;
1749 struct MemoryRegionList
{
1750 const MemoryRegion
*mr
;
1752 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1755 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1757 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1758 const MemoryRegion
*mr
, unsigned int level
,
1760 MemoryRegionListHead
*alias_print_queue
)
1762 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1763 MemoryRegionListHead submr_print_queue
;
1764 const MemoryRegion
*submr
;
1767 if (!mr
|| !mr
->enabled
) {
1771 for (i
= 0; i
< level
; i
++) {
1776 MemoryRegionList
*ml
;
1779 /* check if the alias is already in the queue */
1780 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1781 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1787 ml
= g_new(MemoryRegionList
, 1);
1789 ml
->printed
= false;
1790 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1792 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1793 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1794 "-" TARGET_FMT_plx
"\n",
1797 + (int128_nz(mr
->size
) ?
1798 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1799 int128_one())) : 0),
1801 mr
->romd_mode
? 'R' : '-',
1802 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1808 + (hwaddr
)int128_get64(mr
->size
) - 1);
1811 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1814 + (int128_nz(mr
->size
) ?
1815 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1816 int128_one())) : 0),
1818 mr
->romd_mode
? 'R' : '-',
1819 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1824 QTAILQ_INIT(&submr_print_queue
);
1826 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1827 new_ml
= g_new(MemoryRegionList
, 1);
1829 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1830 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1831 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1832 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1833 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1839 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1843 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1844 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1848 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1853 void mtree_info(fprintf_function mon_printf
, void *f
)
1855 MemoryRegionListHead ml_head
;
1856 MemoryRegionList
*ml
, *ml2
;
1859 QTAILQ_INIT(&ml_head
);
1861 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1862 mon_printf(f
, "%s\n", as
->name
);
1863 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1866 mon_printf(f
, "aliases\n");
1867 /* print aliased regions */
1868 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1870 mon_printf(f
, "%s\n", ml
->mr
->name
);
1871 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1875 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {