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
;
809 mr
->size
= int128_make64(size
);
810 if (size
== UINT64_MAX
) {
811 mr
->size
= int128_2_64();
816 mr
->terminates
= false;
818 mr
->romd_mode
= true;
819 mr
->readonly
= false;
820 mr
->rom_device
= false;
821 mr
->destructor
= memory_region_destructor_none
;
823 mr
->may_overlap
= false;
825 QTAILQ_INIT(&mr
->subregions
);
826 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
827 QTAILQ_INIT(&mr
->coalesced
);
828 mr
->name
= g_strdup(name
);
829 mr
->dirty_log_mask
= 0;
830 mr
->ioeventfd_nb
= 0;
831 mr
->ioeventfds
= NULL
;
832 mr
->flush_coalesced_mmio
= false;
835 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
838 #ifdef DEBUG_UNASSIGNED
839 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
841 if (current_cpu
!= NULL
) {
842 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
847 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
848 uint64_t val
, unsigned size
)
850 #ifdef DEBUG_UNASSIGNED
851 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
853 if (current_cpu
!= NULL
) {
854 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
858 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
859 unsigned size
, bool is_write
)
864 const MemoryRegionOps unassigned_mem_ops
= {
865 .valid
.accepts
= unassigned_mem_accepts
,
866 .endianness
= DEVICE_NATIVE_ENDIAN
,
869 bool memory_region_access_valid(MemoryRegion
*mr
,
874 int access_size_min
, access_size_max
;
877 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
881 if (!mr
->ops
->valid
.accepts
) {
885 access_size_min
= mr
->ops
->valid
.min_access_size
;
886 if (!mr
->ops
->valid
.min_access_size
) {
890 access_size_max
= mr
->ops
->valid
.max_access_size
;
891 if (!mr
->ops
->valid
.max_access_size
) {
895 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
896 for (i
= 0; i
< size
; i
+= access_size
) {
897 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
906 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
913 access_with_adjusted_size(addr
, &data
, size
,
914 mr
->ops
->impl
.min_access_size
,
915 mr
->ops
->impl
.max_access_size
,
916 memory_region_read_accessor
, mr
);
918 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
919 memory_region_oldmmio_read_accessor
, mr
);
925 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
927 if (memory_region_wrong_endianness(mr
)) {
932 *data
= bswap16(*data
);
935 *data
= bswap32(*data
);
938 *data
= bswap64(*data
);
946 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
951 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
952 *pval
= unassigned_mem_read(mr
, addr
, size
);
956 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
957 adjust_endianness(mr
, pval
, size
);
961 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
966 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
967 unassigned_mem_write(mr
, addr
, data
, size
);
971 adjust_endianness(mr
, &data
, size
);
973 if (mr
->ops
->write
) {
974 access_with_adjusted_size(addr
, &data
, size
,
975 mr
->ops
->impl
.min_access_size
,
976 mr
->ops
->impl
.max_access_size
,
977 memory_region_write_accessor
, mr
);
979 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
980 memory_region_oldmmio_write_accessor
, mr
);
985 void memory_region_init_io(MemoryRegion
*mr
,
987 const MemoryRegionOps
*ops
,
992 memory_region_init(mr
, owner
, name
, size
);
995 mr
->terminates
= true;
996 mr
->ram_addr
= ~(ram_addr_t
)0;
999 void memory_region_init_ram(MemoryRegion
*mr
,
1004 memory_region_init(mr
, owner
, name
, size
);
1006 mr
->terminates
= true;
1007 mr
->destructor
= memory_region_destructor_ram
;
1008 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1011 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1017 memory_region_init(mr
, owner
, name
, size
);
1019 mr
->terminates
= true;
1020 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1021 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1024 void memory_region_init_alias(MemoryRegion
*mr
,
1031 memory_region_init(mr
, owner
, name
, size
);
1032 memory_region_ref(orig
);
1033 mr
->destructor
= memory_region_destructor_alias
;
1035 mr
->alias_offset
= offset
;
1038 void memory_region_init_rom_device(MemoryRegion
*mr
,
1040 const MemoryRegionOps
*ops
,
1045 memory_region_init(mr
, owner
, name
, size
);
1047 mr
->opaque
= opaque
;
1048 mr
->terminates
= true;
1049 mr
->rom_device
= true;
1050 mr
->destructor
= memory_region_destructor_rom_device
;
1051 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1054 void memory_region_init_iommu(MemoryRegion
*mr
,
1056 const MemoryRegionIOMMUOps
*ops
,
1060 memory_region_init(mr
, owner
, name
, size
);
1061 mr
->iommu_ops
= ops
,
1062 mr
->terminates
= true; /* then re-forwards */
1063 notifier_list_init(&mr
->iommu_notify
);
1066 void memory_region_init_reservation(MemoryRegion
*mr
,
1071 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1074 void memory_region_destroy(MemoryRegion
*mr
)
1076 assert(QTAILQ_EMPTY(&mr
->subregions
));
1077 assert(memory_region_transaction_depth
== 0);
1079 memory_region_clear_coalescing(mr
);
1080 g_free((char *)mr
->name
);
1081 g_free(mr
->ioeventfds
);
1084 Object
*memory_region_owner(MemoryRegion
*mr
)
1089 void memory_region_ref(MemoryRegion
*mr
)
1091 if (mr
&& mr
->owner
) {
1092 object_ref(mr
->owner
);
1096 void memory_region_unref(MemoryRegion
*mr
)
1098 if (mr
&& mr
->owner
) {
1099 object_unref(mr
->owner
);
1103 uint64_t memory_region_size(MemoryRegion
*mr
)
1105 if (int128_eq(mr
->size
, int128_2_64())) {
1108 return int128_get64(mr
->size
);
1111 const char *memory_region_name(MemoryRegion
*mr
)
1116 bool memory_region_is_ram(MemoryRegion
*mr
)
1121 bool memory_region_is_logging(MemoryRegion
*mr
)
1123 return mr
->dirty_log_mask
;
1126 bool memory_region_is_rom(MemoryRegion
*mr
)
1128 return mr
->ram
&& mr
->readonly
;
1131 bool memory_region_is_iommu(MemoryRegion
*mr
)
1133 return mr
->iommu_ops
;
1136 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1138 notifier_list_add(&mr
->iommu_notify
, n
);
1141 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1146 void memory_region_notify_iommu(MemoryRegion
*mr
,
1147 IOMMUTLBEntry entry
)
1149 assert(memory_region_is_iommu(mr
));
1150 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1153 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1155 uint8_t mask
= 1 << client
;
1157 memory_region_transaction_begin();
1158 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1159 memory_region_update_pending
|= mr
->enabled
;
1160 memory_region_transaction_commit();
1163 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1164 hwaddr size
, unsigned client
)
1166 assert(mr
->terminates
);
1167 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1171 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1174 assert(mr
->terminates
);
1175 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1178 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1179 hwaddr size
, unsigned client
)
1182 assert(mr
->terminates
);
1183 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1186 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1187 mr
->ram_addr
+ addr
+ size
,
1194 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1199 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1200 FlatView
*view
= address_space_get_flatview(as
);
1201 FOR_EACH_FLAT_RANGE(fr
, view
) {
1203 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1206 flatview_unref(view
);
1210 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1212 if (mr
->readonly
!= readonly
) {
1213 memory_region_transaction_begin();
1214 mr
->readonly
= readonly
;
1215 memory_region_update_pending
|= mr
->enabled
;
1216 memory_region_transaction_commit();
1220 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1222 if (mr
->romd_mode
!= romd_mode
) {
1223 memory_region_transaction_begin();
1224 mr
->romd_mode
= romd_mode
;
1225 memory_region_update_pending
|= mr
->enabled
;
1226 memory_region_transaction_commit();
1230 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1231 hwaddr size
, unsigned client
)
1233 assert(mr
->terminates
);
1234 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1235 mr
->ram_addr
+ addr
+ size
,
1239 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1242 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1245 assert(mr
->terminates
);
1247 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1250 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1254 CoalescedMemoryRange
*cmr
;
1256 MemoryRegionSection section
;
1258 view
= address_space_get_flatview(as
);
1259 FOR_EACH_FLAT_RANGE(fr
, view
) {
1261 section
= (MemoryRegionSection
) {
1262 .address_space
= as
,
1263 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1264 .size
= fr
->addr
.size
,
1267 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1268 int128_get64(fr
->addr
.start
),
1269 int128_get64(fr
->addr
.size
));
1270 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1271 tmp
= addrrange_shift(cmr
->addr
,
1272 int128_sub(fr
->addr
.start
,
1273 int128_make64(fr
->offset_in_region
)));
1274 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1277 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1278 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1279 int128_get64(tmp
.start
),
1280 int128_get64(tmp
.size
));
1284 flatview_unref(view
);
1287 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1291 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1292 memory_region_update_coalesced_range_as(mr
, as
);
1296 void memory_region_set_coalescing(MemoryRegion
*mr
)
1298 memory_region_clear_coalescing(mr
);
1299 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1302 void memory_region_add_coalescing(MemoryRegion
*mr
,
1306 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1308 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1309 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1310 memory_region_update_coalesced_range(mr
);
1311 memory_region_set_flush_coalesced(mr
);
1314 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1316 CoalescedMemoryRange
*cmr
;
1318 qemu_flush_coalesced_mmio_buffer();
1319 mr
->flush_coalesced_mmio
= false;
1321 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1322 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1323 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1326 memory_region_update_coalesced_range(mr
);
1329 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1331 mr
->flush_coalesced_mmio
= true;
1334 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1336 qemu_flush_coalesced_mmio_buffer();
1337 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1338 mr
->flush_coalesced_mmio
= false;
1342 void memory_region_add_eventfd(MemoryRegion
*mr
,
1349 MemoryRegionIoeventfd mrfd
= {
1350 .addr
.start
= int128_make64(addr
),
1351 .addr
.size
= int128_make64(size
),
1352 .match_data
= match_data
,
1358 adjust_endianness(mr
, &mrfd
.data
, size
);
1359 memory_region_transaction_begin();
1360 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1361 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1366 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1367 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1368 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1369 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1370 mr
->ioeventfds
[i
] = mrfd
;
1371 memory_region_update_pending
|= mr
->enabled
;
1372 memory_region_transaction_commit();
1375 void memory_region_del_eventfd(MemoryRegion
*mr
,
1382 MemoryRegionIoeventfd mrfd
= {
1383 .addr
.start
= int128_make64(addr
),
1384 .addr
.size
= int128_make64(size
),
1385 .match_data
= match_data
,
1391 adjust_endianness(mr
, &mrfd
.data
, size
);
1392 memory_region_transaction_begin();
1393 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1394 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1398 assert(i
!= mr
->ioeventfd_nb
);
1399 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1400 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1402 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1403 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1404 memory_region_update_pending
|= mr
->enabled
;
1405 memory_region_transaction_commit();
1408 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1410 MemoryRegion
*subregion
)
1412 MemoryRegion
*other
;
1414 memory_region_transaction_begin();
1416 assert(!subregion
->parent
);
1417 memory_region_ref(subregion
);
1418 subregion
->parent
= mr
;
1419 subregion
->addr
= offset
;
1420 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1421 if (subregion
->may_overlap
|| other
->may_overlap
) {
1424 if (int128_ge(int128_make64(offset
),
1425 int128_add(int128_make64(other
->addr
), other
->size
))
1426 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1427 int128_make64(other
->addr
))) {
1431 printf("warning: subregion collision %llx/%llx (%s) "
1432 "vs %llx/%llx (%s)\n",
1433 (unsigned long long)offset
,
1434 (unsigned long long)int128_get64(subregion
->size
),
1436 (unsigned long long)other
->addr
,
1437 (unsigned long long)int128_get64(other
->size
),
1441 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1442 if (subregion
->priority
>= other
->priority
) {
1443 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1447 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1449 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1450 memory_region_transaction_commit();
1454 void memory_region_add_subregion(MemoryRegion
*mr
,
1456 MemoryRegion
*subregion
)
1458 subregion
->may_overlap
= false;
1459 subregion
->priority
= 0;
1460 memory_region_add_subregion_common(mr
, offset
, subregion
);
1463 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1465 MemoryRegion
*subregion
,
1468 subregion
->may_overlap
= true;
1469 subregion
->priority
= priority
;
1470 memory_region_add_subregion_common(mr
, offset
, subregion
);
1473 void memory_region_del_subregion(MemoryRegion
*mr
,
1474 MemoryRegion
*subregion
)
1476 memory_region_transaction_begin();
1477 assert(subregion
->parent
== mr
);
1478 subregion
->parent
= NULL
;
1479 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1480 memory_region_unref(subregion
);
1481 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1482 memory_region_transaction_commit();
1485 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1487 if (enabled
== mr
->enabled
) {
1490 memory_region_transaction_begin();
1491 mr
->enabled
= enabled
;
1492 memory_region_update_pending
= true;
1493 memory_region_transaction_commit();
1496 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1498 MemoryRegion
*parent
= mr
->parent
;
1499 unsigned priority
= mr
->priority
;
1500 bool may_overlap
= mr
->may_overlap
;
1502 if (addr
== mr
->addr
|| !parent
) {
1507 memory_region_transaction_begin();
1508 memory_region_ref(mr
);
1509 memory_region_del_subregion(parent
, mr
);
1511 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1513 memory_region_add_subregion(parent
, addr
, mr
);
1515 memory_region_unref(mr
);
1516 memory_region_transaction_commit();
1519 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1523 if (offset
== mr
->alias_offset
) {
1527 memory_region_transaction_begin();
1528 mr
->alias_offset
= offset
;
1529 memory_region_update_pending
|= mr
->enabled
;
1530 memory_region_transaction_commit();
1533 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1535 return mr
->ram_addr
;
1538 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1540 const AddrRange
*addr
= addr_
;
1541 const FlatRange
*fr
= fr_
;
1543 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1545 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1551 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1553 return bsearch(&addr
, view
->ranges
, view
->nr
,
1554 sizeof(FlatRange
), cmp_flatrange_addr
);
1557 bool memory_region_present(MemoryRegion
*parent
, hwaddr addr
)
1559 MemoryRegion
*mr
= memory_region_find(parent
, addr
, 1).mr
;
1563 memory_region_unref(mr
);
1567 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1568 hwaddr addr
, uint64_t size
)
1570 MemoryRegionSection ret
= { .mr
= NULL
};
1578 for (root
= mr
; root
->parent
; ) {
1579 root
= root
->parent
;
1583 as
= memory_region_to_address_space(root
);
1584 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1586 view
= address_space_get_flatview(as
);
1587 fr
= flatview_lookup(view
, range
);
1592 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1597 ret
.address_space
= as
;
1598 range
= addrrange_intersection(range
, fr
->addr
);
1599 ret
.offset_within_region
= fr
->offset_in_region
;
1600 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1602 ret
.size
= range
.size
;
1603 ret
.offset_within_address_space
= int128_get64(range
.start
);
1604 ret
.readonly
= fr
->readonly
;
1605 memory_region_ref(ret
.mr
);
1607 flatview_unref(view
);
1611 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1616 view
= address_space_get_flatview(as
);
1617 FOR_EACH_FLAT_RANGE(fr
, view
) {
1618 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1620 flatview_unref(view
);
1623 void memory_global_dirty_log_start(void)
1625 global_dirty_log
= true;
1626 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1629 void memory_global_dirty_log_stop(void)
1631 global_dirty_log
= false;
1632 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1635 static void listener_add_address_space(MemoryListener
*listener
,
1641 if (listener
->address_space_filter
1642 && listener
->address_space_filter
!= as
) {
1646 if (global_dirty_log
) {
1647 if (listener
->log_global_start
) {
1648 listener
->log_global_start(listener
);
1652 view
= address_space_get_flatview(as
);
1653 FOR_EACH_FLAT_RANGE(fr
, view
) {
1654 MemoryRegionSection section
= {
1656 .address_space
= as
,
1657 .offset_within_region
= fr
->offset_in_region
,
1658 .size
= fr
->addr
.size
,
1659 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1660 .readonly
= fr
->readonly
,
1662 if (listener
->region_add
) {
1663 listener
->region_add(listener
, §ion
);
1666 flatview_unref(view
);
1669 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1671 MemoryListener
*other
= NULL
;
1674 listener
->address_space_filter
= filter
;
1675 if (QTAILQ_EMPTY(&memory_listeners
)
1676 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1677 memory_listeners
)->priority
) {
1678 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1680 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1681 if (listener
->priority
< other
->priority
) {
1685 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1688 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1689 listener_add_address_space(listener
, as
);
1693 void memory_listener_unregister(MemoryListener
*listener
)
1695 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1698 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1700 if (QTAILQ_EMPTY(&address_spaces
)) {
1704 memory_region_transaction_begin();
1706 as
->current_map
= g_new(FlatView
, 1);
1707 flatview_init(as
->current_map
);
1708 as
->ioeventfd_nb
= 0;
1709 as
->ioeventfds
= NULL
;
1710 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1711 as
->name
= g_strdup(name
? name
: "anonymous");
1712 address_space_init_dispatch(as
);
1713 memory_region_update_pending
|= root
->enabled
;
1714 memory_region_transaction_commit();
1717 void address_space_destroy(AddressSpace
*as
)
1719 /* Flush out anything from MemoryListeners listening in on this */
1720 memory_region_transaction_begin();
1722 memory_region_transaction_commit();
1723 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1724 address_space_destroy_dispatch(as
);
1725 flatview_unref(as
->current_map
);
1727 g_free(as
->ioeventfds
);
1730 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1732 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1735 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1736 uint64_t val
, unsigned size
)
1738 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1741 typedef struct MemoryRegionList MemoryRegionList
;
1743 struct MemoryRegionList
{
1744 const MemoryRegion
*mr
;
1746 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1749 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1751 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1752 const MemoryRegion
*mr
, unsigned int level
,
1754 MemoryRegionListHead
*alias_print_queue
)
1756 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1757 MemoryRegionListHead submr_print_queue
;
1758 const MemoryRegion
*submr
;
1761 if (!mr
|| !mr
->enabled
) {
1765 for (i
= 0; i
< level
; i
++) {
1770 MemoryRegionList
*ml
;
1773 /* check if the alias is already in the queue */
1774 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1775 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1781 ml
= g_new(MemoryRegionList
, 1);
1783 ml
->printed
= false;
1784 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1786 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1787 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1788 "-" TARGET_FMT_plx
"\n",
1791 + (hwaddr
)int128_get64(int128_sub(mr
->size
, int128_make64(1))),
1793 mr
->romd_mode
? 'R' : '-',
1794 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1800 + (hwaddr
)int128_get64(mr
->size
) - 1);
1803 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1806 + (hwaddr
)int128_get64(int128_sub(mr
->size
, int128_make64(1))),
1808 mr
->romd_mode
? 'R' : '-',
1809 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1814 QTAILQ_INIT(&submr_print_queue
);
1816 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1817 new_ml
= g_new(MemoryRegionList
, 1);
1819 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1820 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1821 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1822 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1823 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1829 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1833 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1834 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1838 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1843 void mtree_info(fprintf_function mon_printf
, void *f
)
1845 MemoryRegionListHead ml_head
;
1846 MemoryRegionList
*ml
, *ml2
;
1849 QTAILQ_INIT(&ml_head
);
1851 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1852 mon_printf(f
, "%s\n", as
->name
);
1853 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1856 mon_printf(f
, "aliases\n");
1857 /* print aliased regions */
1858 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1860 mon_printf(f
, "%s\n", ml
->mr
->name
);
1861 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1865 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {