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"
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 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
392 *value
|= (tmp
& mask
) << shift
;
395 static void memory_region_read_accessor(MemoryRegion
*mr
,
404 if (mr
->flush_coalesced_mmio
) {
405 qemu_flush_coalesced_mmio_buffer();
407 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
408 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
409 *value
|= (tmp
& mask
) << shift
;
412 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
421 tmp
= (*value
>> shift
) & mask
;
422 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
423 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
426 static void memory_region_write_accessor(MemoryRegion
*mr
,
435 if (mr
->flush_coalesced_mmio
) {
436 qemu_flush_coalesced_mmio_buffer();
438 tmp
= (*value
>> shift
) & mask
;
439 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
440 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
443 static void access_with_adjusted_size(hwaddr addr
,
446 unsigned access_size_min
,
447 unsigned access_size_max
,
448 void (*access
)(MemoryRegion
*mr
,
456 uint64_t access_mask
;
457 unsigned access_size
;
460 if (!access_size_min
) {
463 if (!access_size_max
) {
467 /* FIXME: support unaligned access? */
468 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
469 access_mask
= -1ULL >> (64 - access_size
* 8);
470 if (memory_region_big_endian(mr
)) {
471 for (i
= 0; i
< size
; i
+= access_size
) {
472 access(mr
, addr
+ i
, value
, access_size
,
473 (size
- access_size
- i
) * 8, access_mask
);
476 for (i
= 0; i
< size
; i
+= access_size
) {
477 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
482 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
489 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
490 if (mr
== as
->root
) {
497 /* Render a memory region into the global view. Ranges in @view obscure
500 static void render_memory_region(FlatView
*view
,
506 MemoryRegion
*subregion
;
508 hwaddr offset_in_region
;
518 int128_addto(&base
, int128_make64(mr
->addr
));
519 readonly
|= mr
->readonly
;
521 tmp
= addrrange_make(base
, mr
->size
);
523 if (!addrrange_intersects(tmp
, clip
)) {
527 clip
= addrrange_intersection(tmp
, clip
);
530 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
531 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
532 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
536 /* Render subregions in priority order. */
537 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
538 render_memory_region(view
, subregion
, base
, clip
, readonly
);
541 if (!mr
->terminates
) {
545 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
550 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
551 fr
.romd_mode
= mr
->romd_mode
;
552 fr
.readonly
= readonly
;
554 /* Render the region itself into any gaps left by the current view. */
555 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
556 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
559 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
560 now
= int128_min(remain
,
561 int128_sub(view
->ranges
[i
].addr
.start
, base
));
562 fr
.offset_in_region
= offset_in_region
;
563 fr
.addr
= addrrange_make(base
, now
);
564 flatview_insert(view
, i
, &fr
);
566 int128_addto(&base
, now
);
567 offset_in_region
+= int128_get64(now
);
568 int128_subfrom(&remain
, now
);
570 now
= int128_sub(int128_min(int128_add(base
, remain
),
571 addrrange_end(view
->ranges
[i
].addr
)),
573 int128_addto(&base
, now
);
574 offset_in_region
+= int128_get64(now
);
575 int128_subfrom(&remain
, now
);
577 if (int128_nz(remain
)) {
578 fr
.offset_in_region
= offset_in_region
;
579 fr
.addr
= addrrange_make(base
, remain
);
580 flatview_insert(view
, i
, &fr
);
584 /* Render a memory topology into a list of disjoint absolute ranges. */
585 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
589 view
= g_new(FlatView
, 1);
593 render_memory_region(view
, mr
, int128_zero(),
594 addrrange_make(int128_zero(), int128_2_64()), false);
596 flatview_simplify(view
);
601 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
602 MemoryRegionIoeventfd
*fds_new
,
604 MemoryRegionIoeventfd
*fds_old
,
608 MemoryRegionIoeventfd
*fd
;
609 MemoryRegionSection section
;
611 /* Generate a symmetric difference of the old and new fd sets, adding
612 * and deleting as necessary.
616 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
617 if (iold
< fds_old_nb
618 && (inew
== fds_new_nb
619 || memory_region_ioeventfd_before(fds_old
[iold
],
622 section
= (MemoryRegionSection
) {
624 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
625 .size
= fd
->addr
.size
,
627 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
628 fd
->match_data
, fd
->data
, fd
->e
);
630 } else if (inew
< fds_new_nb
631 && (iold
== fds_old_nb
632 || memory_region_ioeventfd_before(fds_new
[inew
],
635 section
= (MemoryRegionSection
) {
637 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
638 .size
= fd
->addr
.size
,
640 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
641 fd
->match_data
, fd
->data
, fd
->e
);
650 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
654 qemu_mutex_lock(&flat_view_mutex
);
655 view
= as
->current_map
;
657 qemu_mutex_unlock(&flat_view_mutex
);
661 static void address_space_update_ioeventfds(AddressSpace
*as
)
665 unsigned ioeventfd_nb
= 0;
666 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
670 view
= address_space_get_flatview(as
);
671 FOR_EACH_FLAT_RANGE(fr
, view
) {
672 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
673 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
674 int128_sub(fr
->addr
.start
,
675 int128_make64(fr
->offset_in_region
)));
676 if (addrrange_intersects(fr
->addr
, tmp
)) {
678 ioeventfds
= g_realloc(ioeventfds
,
679 ioeventfd_nb
* sizeof(*ioeventfds
));
680 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
681 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
686 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
687 as
->ioeventfds
, as
->ioeventfd_nb
);
689 g_free(as
->ioeventfds
);
690 as
->ioeventfds
= ioeventfds
;
691 as
->ioeventfd_nb
= ioeventfd_nb
;
692 flatview_unref(view
);
695 static void address_space_update_topology_pass(AddressSpace
*as
,
696 const FlatView
*old_view
,
697 const FlatView
*new_view
,
701 FlatRange
*frold
, *frnew
;
703 /* Generate a symmetric difference of the old and new memory maps.
704 * Kill ranges in the old map, and instantiate ranges in the new map.
707 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
708 if (iold
< old_view
->nr
) {
709 frold
= &old_view
->ranges
[iold
];
713 if (inew
< new_view
->nr
) {
714 frnew
= &new_view
->ranges
[inew
];
721 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
722 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
723 && !flatrange_equal(frold
, frnew
)))) {
724 /* In old but not in new, or in both but attributes changed. */
727 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
731 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
732 /* In both and unchanged (except logging may have changed) */
735 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
736 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
737 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
738 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
739 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
749 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
758 static void address_space_update_topology(AddressSpace
*as
)
760 FlatView
*old_view
= address_space_get_flatview(as
);
761 FlatView
*new_view
= generate_memory_topology(as
->root
);
763 address_space_update_topology_pass(as
, old_view
, new_view
, false);
764 address_space_update_topology_pass(as
, old_view
, new_view
, true);
766 qemu_mutex_lock(&flat_view_mutex
);
767 flatview_unref(as
->current_map
);
768 as
->current_map
= new_view
;
769 qemu_mutex_unlock(&flat_view_mutex
);
771 /* Note that all the old MemoryRegions are still alive up to this
772 * point. This relieves most MemoryListeners from the need to
773 * ref/unref the MemoryRegions they get---unless they use them
774 * outside the iothread mutex, in which case precise reference
775 * counting is necessary.
777 flatview_unref(old_view
);
779 address_space_update_ioeventfds(as
);
782 void memory_region_transaction_begin(void)
784 qemu_flush_coalesced_mmio_buffer();
785 ++memory_region_transaction_depth
;
788 void memory_region_transaction_commit(void)
792 assert(memory_region_transaction_depth
);
793 --memory_region_transaction_depth
;
794 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
795 memory_region_update_pending
= false;
796 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
798 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
799 address_space_update_topology(as
);
802 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
806 static void memory_region_destructor_none(MemoryRegion
*mr
)
810 static void memory_region_destructor_ram(MemoryRegion
*mr
)
812 qemu_ram_free(mr
->ram_addr
);
815 static void memory_region_destructor_alias(MemoryRegion
*mr
)
817 memory_region_unref(mr
->alias
);
820 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
822 qemu_ram_free_from_ptr(mr
->ram_addr
);
825 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
827 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
830 void memory_region_init(MemoryRegion
*mr
,
835 mr
->ops
= &unassigned_mem_ops
;
838 mr
->iommu_ops
= NULL
;
840 mr
->size
= int128_make64(size
);
841 if (size
== UINT64_MAX
) {
842 mr
->size
= int128_2_64();
847 mr
->terminates
= false;
849 mr
->romd_mode
= true;
850 mr
->readonly
= false;
851 mr
->rom_device
= false;
852 mr
->destructor
= memory_region_destructor_none
;
854 mr
->may_overlap
= false;
856 QTAILQ_INIT(&mr
->subregions
);
857 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
858 QTAILQ_INIT(&mr
->coalesced
);
859 mr
->name
= g_strdup(name
);
860 mr
->dirty_log_mask
= 0;
861 mr
->ioeventfd_nb
= 0;
862 mr
->ioeventfds
= NULL
;
863 mr
->flush_coalesced_mmio
= false;
866 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
869 #ifdef DEBUG_UNASSIGNED
870 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
872 if (current_cpu
!= NULL
) {
873 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
878 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
879 uint64_t val
, unsigned size
)
881 #ifdef DEBUG_UNASSIGNED
882 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
884 if (current_cpu
!= NULL
) {
885 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
889 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
890 unsigned size
, bool is_write
)
895 const MemoryRegionOps unassigned_mem_ops
= {
896 .valid
.accepts
= unassigned_mem_accepts
,
897 .endianness
= DEVICE_NATIVE_ENDIAN
,
900 bool memory_region_access_valid(MemoryRegion
*mr
,
905 int access_size_min
, access_size_max
;
908 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
912 if (!mr
->ops
->valid
.accepts
) {
916 access_size_min
= mr
->ops
->valid
.min_access_size
;
917 if (!mr
->ops
->valid
.min_access_size
) {
921 access_size_max
= mr
->ops
->valid
.max_access_size
;
922 if (!mr
->ops
->valid
.max_access_size
) {
926 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
927 for (i
= 0; i
< size
; i
+= access_size
) {
928 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
937 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
944 access_with_adjusted_size(addr
, &data
, size
,
945 mr
->ops
->impl
.min_access_size
,
946 mr
->ops
->impl
.max_access_size
,
947 memory_region_read_accessor
, mr
);
949 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
950 memory_region_oldmmio_read_accessor
, mr
);
956 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
961 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
962 *pval
= unassigned_mem_read(mr
, addr
, size
);
966 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
967 adjust_endianness(mr
, pval
, size
);
971 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
976 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
977 unassigned_mem_write(mr
, addr
, data
, size
);
981 adjust_endianness(mr
, &data
, size
);
983 if (mr
->ops
->write
) {
984 access_with_adjusted_size(addr
, &data
, size
,
985 mr
->ops
->impl
.min_access_size
,
986 mr
->ops
->impl
.max_access_size
,
987 memory_region_write_accessor
, mr
);
989 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
990 memory_region_oldmmio_write_accessor
, mr
);
995 void memory_region_init_io(MemoryRegion
*mr
,
997 const MemoryRegionOps
*ops
,
1002 memory_region_init(mr
, owner
, name
, size
);
1004 mr
->opaque
= opaque
;
1005 mr
->terminates
= true;
1006 mr
->ram_addr
= ~(ram_addr_t
)0;
1009 void memory_region_init_ram(MemoryRegion
*mr
,
1014 memory_region_init(mr
, owner
, name
, size
);
1016 mr
->terminates
= true;
1017 mr
->destructor
= memory_region_destructor_ram
;
1018 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1021 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1027 memory_region_init(mr
, owner
, name
, size
);
1029 mr
->terminates
= true;
1030 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1031 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1034 void memory_region_init_alias(MemoryRegion
*mr
,
1041 memory_region_init(mr
, owner
, name
, size
);
1042 memory_region_ref(orig
);
1043 mr
->destructor
= memory_region_destructor_alias
;
1045 mr
->alias_offset
= offset
;
1048 void memory_region_init_rom_device(MemoryRegion
*mr
,
1050 const MemoryRegionOps
*ops
,
1055 memory_region_init(mr
, owner
, name
, size
);
1057 mr
->opaque
= opaque
;
1058 mr
->terminates
= true;
1059 mr
->rom_device
= true;
1060 mr
->destructor
= memory_region_destructor_rom_device
;
1061 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1064 void memory_region_init_iommu(MemoryRegion
*mr
,
1066 const MemoryRegionIOMMUOps
*ops
,
1070 memory_region_init(mr
, owner
, name
, size
);
1071 mr
->iommu_ops
= ops
,
1072 mr
->terminates
= true; /* then re-forwards */
1073 notifier_list_init(&mr
->iommu_notify
);
1076 void memory_region_init_reservation(MemoryRegion
*mr
,
1081 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1084 void memory_region_destroy(MemoryRegion
*mr
)
1086 assert(QTAILQ_EMPTY(&mr
->subregions
));
1087 assert(memory_region_transaction_depth
== 0);
1089 memory_region_clear_coalescing(mr
);
1090 g_free((char *)mr
->name
);
1091 g_free(mr
->ioeventfds
);
1094 Object
*memory_region_owner(MemoryRegion
*mr
)
1099 void memory_region_ref(MemoryRegion
*mr
)
1101 if (mr
&& mr
->owner
) {
1102 object_ref(mr
->owner
);
1106 void memory_region_unref(MemoryRegion
*mr
)
1108 if (mr
&& mr
->owner
) {
1109 object_unref(mr
->owner
);
1113 uint64_t memory_region_size(MemoryRegion
*mr
)
1115 if (int128_eq(mr
->size
, int128_2_64())) {
1118 return int128_get64(mr
->size
);
1121 const char *memory_region_name(MemoryRegion
*mr
)
1126 bool memory_region_is_ram(MemoryRegion
*mr
)
1131 bool memory_region_is_logging(MemoryRegion
*mr
)
1133 return mr
->dirty_log_mask
;
1136 bool memory_region_is_rom(MemoryRegion
*mr
)
1138 return mr
->ram
&& mr
->readonly
;
1141 bool memory_region_is_iommu(MemoryRegion
*mr
)
1143 return mr
->iommu_ops
;
1146 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1148 notifier_list_add(&mr
->iommu_notify
, n
);
1151 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1156 void memory_region_notify_iommu(MemoryRegion
*mr
,
1157 IOMMUTLBEntry entry
)
1159 assert(memory_region_is_iommu(mr
));
1160 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1163 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1165 uint8_t mask
= 1 << client
;
1167 memory_region_transaction_begin();
1168 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1169 memory_region_update_pending
|= mr
->enabled
;
1170 memory_region_transaction_commit();
1173 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1174 hwaddr size
, unsigned client
)
1176 assert(mr
->terminates
);
1177 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1181 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1184 assert(mr
->terminates
);
1185 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1188 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1189 hwaddr size
, unsigned client
)
1192 assert(mr
->terminates
);
1193 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1196 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1197 mr
->ram_addr
+ addr
+ size
,
1204 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1209 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1210 FlatView
*view
= address_space_get_flatview(as
);
1211 FOR_EACH_FLAT_RANGE(fr
, view
) {
1213 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1216 flatview_unref(view
);
1220 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1222 if (mr
->readonly
!= readonly
) {
1223 memory_region_transaction_begin();
1224 mr
->readonly
= readonly
;
1225 memory_region_update_pending
|= mr
->enabled
;
1226 memory_region_transaction_commit();
1230 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1232 if (mr
->romd_mode
!= romd_mode
) {
1233 memory_region_transaction_begin();
1234 mr
->romd_mode
= romd_mode
;
1235 memory_region_update_pending
|= mr
->enabled
;
1236 memory_region_transaction_commit();
1240 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1241 hwaddr size
, unsigned client
)
1243 assert(mr
->terminates
);
1244 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1245 mr
->ram_addr
+ addr
+ size
,
1249 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1252 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1255 assert(mr
->terminates
);
1257 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1260 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1264 CoalescedMemoryRange
*cmr
;
1266 MemoryRegionSection section
;
1268 view
= address_space_get_flatview(as
);
1269 FOR_EACH_FLAT_RANGE(fr
, view
) {
1271 section
= (MemoryRegionSection
) {
1272 .address_space
= as
,
1273 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1274 .size
= fr
->addr
.size
,
1277 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1278 int128_get64(fr
->addr
.start
),
1279 int128_get64(fr
->addr
.size
));
1280 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1281 tmp
= addrrange_shift(cmr
->addr
,
1282 int128_sub(fr
->addr
.start
,
1283 int128_make64(fr
->offset_in_region
)));
1284 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1287 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1288 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1289 int128_get64(tmp
.start
),
1290 int128_get64(tmp
.size
));
1294 flatview_unref(view
);
1297 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1301 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1302 memory_region_update_coalesced_range_as(mr
, as
);
1306 void memory_region_set_coalescing(MemoryRegion
*mr
)
1308 memory_region_clear_coalescing(mr
);
1309 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1312 void memory_region_add_coalescing(MemoryRegion
*mr
,
1316 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1318 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1319 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1320 memory_region_update_coalesced_range(mr
);
1321 memory_region_set_flush_coalesced(mr
);
1324 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1326 CoalescedMemoryRange
*cmr
;
1328 qemu_flush_coalesced_mmio_buffer();
1329 mr
->flush_coalesced_mmio
= false;
1331 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1332 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1333 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1336 memory_region_update_coalesced_range(mr
);
1339 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1341 mr
->flush_coalesced_mmio
= true;
1344 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1346 qemu_flush_coalesced_mmio_buffer();
1347 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1348 mr
->flush_coalesced_mmio
= false;
1352 void memory_region_add_eventfd(MemoryRegion
*mr
,
1359 MemoryRegionIoeventfd mrfd
= {
1360 .addr
.start
= int128_make64(addr
),
1361 .addr
.size
= int128_make64(size
),
1362 .match_data
= match_data
,
1368 adjust_endianness(mr
, &mrfd
.data
, size
);
1369 memory_region_transaction_begin();
1370 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1371 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1376 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1377 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1378 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1379 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1380 mr
->ioeventfds
[i
] = mrfd
;
1381 memory_region_update_pending
|= mr
->enabled
;
1382 memory_region_transaction_commit();
1385 void memory_region_del_eventfd(MemoryRegion
*mr
,
1392 MemoryRegionIoeventfd mrfd
= {
1393 .addr
.start
= int128_make64(addr
),
1394 .addr
.size
= int128_make64(size
),
1395 .match_data
= match_data
,
1401 adjust_endianness(mr
, &mrfd
.data
, size
);
1402 memory_region_transaction_begin();
1403 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1404 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1408 assert(i
!= mr
->ioeventfd_nb
);
1409 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1410 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1412 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1413 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1414 memory_region_update_pending
|= mr
->enabled
;
1415 memory_region_transaction_commit();
1418 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1420 MemoryRegion
*subregion
)
1422 MemoryRegion
*other
;
1424 memory_region_transaction_begin();
1426 assert(!subregion
->parent
);
1427 memory_region_ref(subregion
);
1428 subregion
->parent
= mr
;
1429 subregion
->addr
= offset
;
1430 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1431 if (subregion
->may_overlap
|| other
->may_overlap
) {
1434 if (int128_ge(int128_make64(offset
),
1435 int128_add(int128_make64(other
->addr
), other
->size
))
1436 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1437 int128_make64(other
->addr
))) {
1441 printf("warning: subregion collision %llx/%llx (%s) "
1442 "vs %llx/%llx (%s)\n",
1443 (unsigned long long)offset
,
1444 (unsigned long long)int128_get64(subregion
->size
),
1446 (unsigned long long)other
->addr
,
1447 (unsigned long long)int128_get64(other
->size
),
1451 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1452 if (subregion
->priority
>= other
->priority
) {
1453 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1457 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1459 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1460 memory_region_transaction_commit();
1464 void memory_region_add_subregion(MemoryRegion
*mr
,
1466 MemoryRegion
*subregion
)
1468 subregion
->may_overlap
= false;
1469 subregion
->priority
= 0;
1470 memory_region_add_subregion_common(mr
, offset
, subregion
);
1473 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1475 MemoryRegion
*subregion
,
1478 subregion
->may_overlap
= true;
1479 subregion
->priority
= priority
;
1480 memory_region_add_subregion_common(mr
, offset
, subregion
);
1483 void memory_region_del_subregion(MemoryRegion
*mr
,
1484 MemoryRegion
*subregion
)
1486 memory_region_transaction_begin();
1487 assert(subregion
->parent
== mr
);
1488 subregion
->parent
= NULL
;
1489 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1490 memory_region_unref(subregion
);
1491 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1492 memory_region_transaction_commit();
1495 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1497 if (enabled
== mr
->enabled
) {
1500 memory_region_transaction_begin();
1501 mr
->enabled
= enabled
;
1502 memory_region_update_pending
= true;
1503 memory_region_transaction_commit();
1506 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1508 MemoryRegion
*parent
= mr
->parent
;
1509 int priority
= mr
->priority
;
1510 bool may_overlap
= mr
->may_overlap
;
1512 if (addr
== mr
->addr
|| !parent
) {
1517 memory_region_transaction_begin();
1518 memory_region_ref(mr
);
1519 memory_region_del_subregion(parent
, mr
);
1521 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1523 memory_region_add_subregion(parent
, addr
, mr
);
1525 memory_region_unref(mr
);
1526 memory_region_transaction_commit();
1529 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1533 if (offset
== mr
->alias_offset
) {
1537 memory_region_transaction_begin();
1538 mr
->alias_offset
= offset
;
1539 memory_region_update_pending
|= mr
->enabled
;
1540 memory_region_transaction_commit();
1543 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1545 return mr
->ram_addr
;
1548 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1550 const AddrRange
*addr
= addr_
;
1551 const FlatRange
*fr
= fr_
;
1553 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1555 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1561 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1563 return bsearch(&addr
, view
->ranges
, view
->nr
,
1564 sizeof(FlatRange
), cmp_flatrange_addr
);
1567 bool memory_region_present(MemoryRegion
*parent
, hwaddr addr
)
1569 MemoryRegion
*mr
= memory_region_find(parent
, addr
, 1).mr
;
1573 memory_region_unref(mr
);
1577 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1578 hwaddr addr
, uint64_t size
)
1580 MemoryRegionSection ret
= { .mr
= NULL
};
1588 for (root
= mr
; root
->parent
; ) {
1589 root
= root
->parent
;
1593 as
= memory_region_to_address_space(root
);
1594 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1596 view
= address_space_get_flatview(as
);
1597 fr
= flatview_lookup(view
, range
);
1602 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1607 ret
.address_space
= as
;
1608 range
= addrrange_intersection(range
, fr
->addr
);
1609 ret
.offset_within_region
= fr
->offset_in_region
;
1610 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1612 ret
.size
= range
.size
;
1613 ret
.offset_within_address_space
= int128_get64(range
.start
);
1614 ret
.readonly
= fr
->readonly
;
1615 memory_region_ref(ret
.mr
);
1617 flatview_unref(view
);
1621 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1626 view
= address_space_get_flatview(as
);
1627 FOR_EACH_FLAT_RANGE(fr
, view
) {
1628 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1630 flatview_unref(view
);
1633 void memory_global_dirty_log_start(void)
1635 global_dirty_log
= true;
1636 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1639 void memory_global_dirty_log_stop(void)
1641 global_dirty_log
= false;
1642 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1645 static void listener_add_address_space(MemoryListener
*listener
,
1651 if (listener
->address_space_filter
1652 && listener
->address_space_filter
!= as
) {
1656 if (global_dirty_log
) {
1657 if (listener
->log_global_start
) {
1658 listener
->log_global_start(listener
);
1662 view
= address_space_get_flatview(as
);
1663 FOR_EACH_FLAT_RANGE(fr
, view
) {
1664 MemoryRegionSection section
= {
1666 .address_space
= as
,
1667 .offset_within_region
= fr
->offset_in_region
,
1668 .size
= fr
->addr
.size
,
1669 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1670 .readonly
= fr
->readonly
,
1672 if (listener
->region_add
) {
1673 listener
->region_add(listener
, §ion
);
1676 flatview_unref(view
);
1679 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1681 MemoryListener
*other
= NULL
;
1684 listener
->address_space_filter
= filter
;
1685 if (QTAILQ_EMPTY(&memory_listeners
)
1686 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1687 memory_listeners
)->priority
) {
1688 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1690 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1691 if (listener
->priority
< other
->priority
) {
1695 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1698 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1699 listener_add_address_space(listener
, as
);
1703 void memory_listener_unregister(MemoryListener
*listener
)
1705 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1708 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1710 if (QTAILQ_EMPTY(&address_spaces
)) {
1714 memory_region_transaction_begin();
1716 as
->current_map
= g_new(FlatView
, 1);
1717 flatview_init(as
->current_map
);
1718 as
->ioeventfd_nb
= 0;
1719 as
->ioeventfds
= NULL
;
1720 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1721 as
->name
= g_strdup(name
? name
: "anonymous");
1722 address_space_init_dispatch(as
);
1723 memory_region_update_pending
|= root
->enabled
;
1724 memory_region_transaction_commit();
1727 void address_space_destroy(AddressSpace
*as
)
1729 /* Flush out anything from MemoryListeners listening in on this */
1730 memory_region_transaction_begin();
1732 memory_region_transaction_commit();
1733 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1734 address_space_destroy_dispatch(as
);
1735 flatview_unref(as
->current_map
);
1737 g_free(as
->ioeventfds
);
1740 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1742 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1745 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1746 uint64_t val
, unsigned size
)
1748 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1751 typedef struct MemoryRegionList MemoryRegionList
;
1753 struct MemoryRegionList
{
1754 const MemoryRegion
*mr
;
1756 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1759 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1761 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1762 const MemoryRegion
*mr
, unsigned int level
,
1764 MemoryRegionListHead
*alias_print_queue
)
1766 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1767 MemoryRegionListHead submr_print_queue
;
1768 const MemoryRegion
*submr
;
1771 if (!mr
|| !mr
->enabled
) {
1775 for (i
= 0; i
< level
; i
++) {
1780 MemoryRegionList
*ml
;
1783 /* check if the alias is already in the queue */
1784 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1785 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1791 ml
= g_new(MemoryRegionList
, 1);
1793 ml
->printed
= false;
1794 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1796 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1797 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1798 "-" TARGET_FMT_plx
"\n",
1801 + (int128_nz(mr
->size
) ?
1802 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1803 int128_one())) : 0),
1805 mr
->romd_mode
? 'R' : '-',
1806 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1812 + (int128_nz(mr
->size
) ?
1813 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1814 int128_one())) : 0));
1817 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1820 + (int128_nz(mr
->size
) ?
1821 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1822 int128_one())) : 0),
1824 mr
->romd_mode
? 'R' : '-',
1825 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1830 QTAILQ_INIT(&submr_print_queue
);
1832 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1833 new_ml
= g_new(MemoryRegionList
, 1);
1835 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1836 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1837 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1838 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1839 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1845 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1849 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1850 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1854 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1859 void mtree_info(fprintf_function mon_printf
, void *f
)
1861 MemoryRegionListHead ml_head
;
1862 MemoryRegionList
*ml
, *ml2
;
1865 QTAILQ_INIT(&ml_head
);
1867 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1868 mon_printf(f
, "%s\n", as
->name
);
1869 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1872 mon_printf(f
, "aliases\n");
1873 /* print aliased regions */
1874 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1876 mon_printf(f
, "%s\n", ml
->mr
->name
);
1877 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1881 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {