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"
25 #include "exec/memory-internal.h"
27 //#define DEBUG_UNASSIGNED
29 static unsigned memory_region_transaction_depth
;
30 static bool memory_region_update_pending
;
31 static bool global_dirty_log
= false;
33 /* flat_view_mutex is taken around reading as->current_map; the critical
34 * section is extremely short, so I'm using a single mutex for every AS.
35 * We could also RCU for the read-side.
37 * The BQL is taken around transaction commits, hence both locks are taken
38 * while writing to as->current_map (with the BQL taken outside).
40 static QemuMutex flat_view_mutex
;
42 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
43 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
45 static QTAILQ_HEAD(, AddressSpace
) address_spaces
46 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
48 static void memory_init(void)
50 qemu_mutex_init(&flat_view_mutex
);
53 typedef struct AddrRange AddrRange
;
56 * Note using signed integers limits us to physical addresses at most
57 * 63 bits wide. They are needed for negative offsetting in aliases
58 * (large MemoryRegion::alias_offset).
65 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
67 return (AddrRange
) { start
, size
};
70 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
72 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
75 static Int128
addrrange_end(AddrRange r
)
77 return int128_add(r
.start
, r
.size
);
80 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
82 int128_addto(&range
.start
, delta
);
86 static bool addrrange_contains(AddrRange range
, Int128 addr
)
88 return int128_ge(addr
, range
.start
)
89 && int128_lt(addr
, addrrange_end(range
));
92 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
94 return addrrange_contains(r1
, r2
.start
)
95 || addrrange_contains(r2
, r1
.start
);
98 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
100 Int128 start
= int128_max(r1
.start
, r2
.start
);
101 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
102 return addrrange_make(start
, int128_sub(end
, start
));
105 enum ListenerDirection
{ Forward
, Reverse
};
107 static bool memory_listener_match(MemoryListener
*listener
,
108 MemoryRegionSection
*section
)
110 return !listener
->address_space_filter
111 || listener
->address_space_filter
== section
->address_space
;
114 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
116 MemoryListener *_listener; \
118 switch (_direction) { \
120 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
121 if (_listener->_callback) { \
122 _listener->_callback(_listener, ##_args); \
127 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
128 memory_listeners, link) { \
129 if (_listener->_callback) { \
130 _listener->_callback(_listener, ##_args); \
139 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
141 MemoryListener *_listener; \
143 switch (_direction) { \
145 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
146 if (_listener->_callback \
147 && memory_listener_match(_listener, _section)) { \
148 _listener->_callback(_listener, _section, ##_args); \
153 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
154 memory_listeners, link) { \
155 if (_listener->_callback \
156 && memory_listener_match(_listener, _section)) { \
157 _listener->_callback(_listener, _section, ##_args); \
166 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
167 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
168 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
170 .address_space = (as), \
171 .offset_within_region = (fr)->offset_in_region, \
172 .size = (fr)->addr.size, \
173 .offset_within_address_space = int128_get64((fr)->addr.start), \
174 .readonly = (fr)->readonly, \
177 struct CoalescedMemoryRange
{
179 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
182 struct MemoryRegionIoeventfd
{
189 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
190 MemoryRegionIoeventfd b
)
192 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
194 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
196 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
198 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
200 } else if (a
.match_data
< b
.match_data
) {
202 } else if (a
.match_data
> b
.match_data
) {
204 } else if (a
.match_data
) {
205 if (a
.data
< b
.data
) {
207 } else if (a
.data
> b
.data
) {
213 } else if (a
.e
> b
.e
) {
219 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
220 MemoryRegionIoeventfd b
)
222 return !memory_region_ioeventfd_before(a
, b
)
223 && !memory_region_ioeventfd_before(b
, a
);
226 typedef struct FlatRange FlatRange
;
227 typedef struct FlatView FlatView
;
229 /* Range of memory in the global map. Addresses are absolute. */
232 hwaddr offset_in_region
;
234 uint8_t dirty_log_mask
;
239 /* Flattened global view of current active memory hierarchy. Kept in sorted
246 unsigned nr_allocated
;
249 typedef struct AddressSpaceOps AddressSpaceOps
;
251 #define FOR_EACH_FLAT_RANGE(var, view) \
252 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
254 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
256 return a
->mr
== b
->mr
257 && addrrange_equal(a
->addr
, b
->addr
)
258 && a
->offset_in_region
== b
->offset_in_region
259 && a
->romd_mode
== b
->romd_mode
260 && a
->readonly
== b
->readonly
;
263 static void flatview_init(FlatView
*view
)
268 view
->nr_allocated
= 0;
271 /* Insert a range into a given position. Caller is responsible for maintaining
274 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
276 if (view
->nr
== view
->nr_allocated
) {
277 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
278 view
->ranges
= g_realloc(view
->ranges
,
279 view
->nr_allocated
* sizeof(*view
->ranges
));
281 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
282 (view
->nr
- pos
) * sizeof(FlatRange
));
283 view
->ranges
[pos
] = *range
;
284 memory_region_ref(range
->mr
);
288 static void flatview_destroy(FlatView
*view
)
292 for (i
= 0; i
< view
->nr
; i
++) {
293 memory_region_unref(view
->ranges
[i
].mr
);
295 g_free(view
->ranges
);
299 static void flatview_ref(FlatView
*view
)
301 atomic_inc(&view
->ref
);
304 static void flatview_unref(FlatView
*view
)
306 if (atomic_fetch_dec(&view
->ref
) == 1) {
307 flatview_destroy(view
);
311 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
313 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
315 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
317 int128_make64(r2
->offset_in_region
))
318 && r1
->dirty_log_mask
== r2
->dirty_log_mask
319 && r1
->romd_mode
== r2
->romd_mode
320 && r1
->readonly
== r2
->readonly
;
323 /* Attempt to simplify a view by merging adjacent ranges */
324 static void flatview_simplify(FlatView
*view
)
329 while (i
< view
->nr
) {
332 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
333 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
337 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
338 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
343 static bool memory_region_big_endian(MemoryRegion
*mr
)
345 #ifdef TARGET_WORDS_BIGENDIAN
346 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
348 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
352 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
354 #ifdef TARGET_WORDS_BIGENDIAN
355 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
357 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
361 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
363 if (memory_region_wrong_endianness(mr
)) {
368 *data
= bswap16(*data
);
371 *data
= bswap32(*data
);
374 *data
= bswap64(*data
);
382 static void memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
391 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
392 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
393 *value
|= (tmp
& mask
) << shift
;
396 static void memory_region_read_accessor(MemoryRegion
*mr
,
405 if (mr
->flush_coalesced_mmio
) {
406 qemu_flush_coalesced_mmio_buffer();
408 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
409 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
410 *value
|= (tmp
& mask
) << shift
;
413 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
422 tmp
= (*value
>> shift
) & mask
;
423 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
424 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
427 static void memory_region_write_accessor(MemoryRegion
*mr
,
436 if (mr
->flush_coalesced_mmio
) {
437 qemu_flush_coalesced_mmio_buffer();
439 tmp
= (*value
>> shift
) & mask
;
440 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
441 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
444 static void access_with_adjusted_size(hwaddr addr
,
447 unsigned access_size_min
,
448 unsigned access_size_max
,
449 void (*access
)(MemoryRegion
*mr
,
457 uint64_t access_mask
;
458 unsigned access_size
;
461 if (!access_size_min
) {
464 if (!access_size_max
) {
468 /* FIXME: support unaligned access? */
469 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
470 access_mask
= -1ULL >> (64 - access_size
* 8);
471 if (memory_region_big_endian(mr
)) {
472 for (i
= 0; i
< size
; i
+= access_size
) {
473 access(mr
, addr
+ i
, value
, access_size
,
474 (size
- access_size
- i
) * 8, access_mask
);
477 for (i
= 0; i
< size
; i
+= access_size
) {
478 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
483 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
490 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
491 if (mr
== as
->root
) {
498 /* Render a memory region into the global view. Ranges in @view obscure
501 static void render_memory_region(FlatView
*view
,
507 MemoryRegion
*subregion
;
509 hwaddr offset_in_region
;
519 int128_addto(&base
, int128_make64(mr
->addr
));
520 readonly
|= mr
->readonly
;
522 tmp
= addrrange_make(base
, mr
->size
);
524 if (!addrrange_intersects(tmp
, clip
)) {
528 clip
= addrrange_intersection(tmp
, clip
);
531 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
532 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
533 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
537 /* Render subregions in priority order. */
538 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
539 render_memory_region(view
, subregion
, base
, clip
, readonly
);
542 if (!mr
->terminates
) {
546 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
551 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
552 fr
.romd_mode
= mr
->romd_mode
;
553 fr
.readonly
= readonly
;
555 /* Render the region itself into any gaps left by the current view. */
556 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
557 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
560 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
561 now
= int128_min(remain
,
562 int128_sub(view
->ranges
[i
].addr
.start
, base
));
563 fr
.offset_in_region
= offset_in_region
;
564 fr
.addr
= addrrange_make(base
, now
);
565 flatview_insert(view
, i
, &fr
);
567 int128_addto(&base
, now
);
568 offset_in_region
+= int128_get64(now
);
569 int128_subfrom(&remain
, now
);
571 now
= int128_sub(int128_min(int128_add(base
, remain
),
572 addrrange_end(view
->ranges
[i
].addr
)),
574 int128_addto(&base
, now
);
575 offset_in_region
+= int128_get64(now
);
576 int128_subfrom(&remain
, now
);
578 if (int128_nz(remain
)) {
579 fr
.offset_in_region
= offset_in_region
;
580 fr
.addr
= addrrange_make(base
, remain
);
581 flatview_insert(view
, i
, &fr
);
585 /* Render a memory topology into a list of disjoint absolute ranges. */
586 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
590 view
= g_new(FlatView
, 1);
594 render_memory_region(view
, mr
, int128_zero(),
595 addrrange_make(int128_zero(), int128_2_64()), false);
597 flatview_simplify(view
);
602 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
603 MemoryRegionIoeventfd
*fds_new
,
605 MemoryRegionIoeventfd
*fds_old
,
609 MemoryRegionIoeventfd
*fd
;
610 MemoryRegionSection section
;
612 /* Generate a symmetric difference of the old and new fd sets, adding
613 * and deleting as necessary.
617 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
618 if (iold
< fds_old_nb
619 && (inew
== fds_new_nb
620 || memory_region_ioeventfd_before(fds_old
[iold
],
623 section
= (MemoryRegionSection
) {
625 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
626 .size
= fd
->addr
.size
,
628 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
629 fd
->match_data
, fd
->data
, fd
->e
);
631 } else if (inew
< fds_new_nb
632 && (iold
== fds_old_nb
633 || memory_region_ioeventfd_before(fds_new
[inew
],
636 section
= (MemoryRegionSection
) {
638 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
639 .size
= fd
->addr
.size
,
641 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
642 fd
->match_data
, fd
->data
, fd
->e
);
651 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
655 qemu_mutex_lock(&flat_view_mutex
);
656 view
= as
->current_map
;
658 qemu_mutex_unlock(&flat_view_mutex
);
662 static void address_space_update_ioeventfds(AddressSpace
*as
)
666 unsigned ioeventfd_nb
= 0;
667 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
671 view
= address_space_get_flatview(as
);
672 FOR_EACH_FLAT_RANGE(fr
, view
) {
673 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
674 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
675 int128_sub(fr
->addr
.start
,
676 int128_make64(fr
->offset_in_region
)));
677 if (addrrange_intersects(fr
->addr
, tmp
)) {
679 ioeventfds
= g_realloc(ioeventfds
,
680 ioeventfd_nb
* sizeof(*ioeventfds
));
681 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
682 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
687 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
688 as
->ioeventfds
, as
->ioeventfd_nb
);
690 g_free(as
->ioeventfds
);
691 as
->ioeventfds
= ioeventfds
;
692 as
->ioeventfd_nb
= ioeventfd_nb
;
693 flatview_unref(view
);
696 static void address_space_update_topology_pass(AddressSpace
*as
,
697 const FlatView
*old_view
,
698 const FlatView
*new_view
,
702 FlatRange
*frold
, *frnew
;
704 /* Generate a symmetric difference of the old and new memory maps.
705 * Kill ranges in the old map, and instantiate ranges in the new map.
708 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
709 if (iold
< old_view
->nr
) {
710 frold
= &old_view
->ranges
[iold
];
714 if (inew
< new_view
->nr
) {
715 frnew
= &new_view
->ranges
[inew
];
722 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
723 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
724 && !flatrange_equal(frold
, frnew
)))) {
725 /* In old but not in new, or in both but attributes changed. */
728 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
732 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
733 /* In both and unchanged (except logging may have changed) */
736 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
737 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
738 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
739 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
740 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
750 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
759 static void address_space_update_topology(AddressSpace
*as
)
761 FlatView
*old_view
= address_space_get_flatview(as
);
762 FlatView
*new_view
= generate_memory_topology(as
->root
);
764 address_space_update_topology_pass(as
, old_view
, new_view
, false);
765 address_space_update_topology_pass(as
, old_view
, new_view
, true);
767 qemu_mutex_lock(&flat_view_mutex
);
768 flatview_unref(as
->current_map
);
769 as
->current_map
= new_view
;
770 qemu_mutex_unlock(&flat_view_mutex
);
772 /* Note that all the old MemoryRegions are still alive up to this
773 * point. This relieves most MemoryListeners from the need to
774 * ref/unref the MemoryRegions they get---unless they use them
775 * outside the iothread mutex, in which case precise reference
776 * counting is necessary.
778 flatview_unref(old_view
);
780 address_space_update_ioeventfds(as
);
783 void memory_region_transaction_begin(void)
785 qemu_flush_coalesced_mmio_buffer();
786 ++memory_region_transaction_depth
;
789 void memory_region_transaction_commit(void)
793 assert(memory_region_transaction_depth
);
794 --memory_region_transaction_depth
;
795 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
796 memory_region_update_pending
= false;
797 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
799 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
800 address_space_update_topology(as
);
803 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
807 static void memory_region_destructor_none(MemoryRegion
*mr
)
811 static void memory_region_destructor_ram(MemoryRegion
*mr
)
813 qemu_ram_free(mr
->ram_addr
);
816 static void memory_region_destructor_alias(MemoryRegion
*mr
)
818 memory_region_unref(mr
->alias
);
821 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
823 qemu_ram_free_from_ptr(mr
->ram_addr
);
826 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
828 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
831 void memory_region_init(MemoryRegion
*mr
,
836 mr
->ops
= &unassigned_mem_ops
;
839 mr
->iommu_ops
= NULL
;
841 mr
->size
= int128_make64(size
);
842 if (size
== UINT64_MAX
) {
843 mr
->size
= int128_2_64();
848 mr
->terminates
= false;
850 mr
->romd_mode
= true;
851 mr
->readonly
= false;
852 mr
->rom_device
= false;
853 mr
->destructor
= memory_region_destructor_none
;
855 mr
->may_overlap
= false;
857 QTAILQ_INIT(&mr
->subregions
);
858 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
859 QTAILQ_INIT(&mr
->coalesced
);
860 mr
->name
= g_strdup(name
);
861 mr
->dirty_log_mask
= 0;
862 mr
->ioeventfd_nb
= 0;
863 mr
->ioeventfds
= NULL
;
864 mr
->flush_coalesced_mmio
= false;
867 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
870 #ifdef DEBUG_UNASSIGNED
871 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
873 if (current_cpu
!= NULL
) {
874 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
879 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
880 uint64_t val
, unsigned size
)
882 #ifdef DEBUG_UNASSIGNED
883 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
885 if (current_cpu
!= NULL
) {
886 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
890 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
891 unsigned size
, bool is_write
)
896 const MemoryRegionOps unassigned_mem_ops
= {
897 .valid
.accepts
= unassigned_mem_accepts
,
898 .endianness
= DEVICE_NATIVE_ENDIAN
,
901 bool memory_region_access_valid(MemoryRegion
*mr
,
906 int access_size_min
, access_size_max
;
909 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
913 if (!mr
->ops
->valid
.accepts
) {
917 access_size_min
= mr
->ops
->valid
.min_access_size
;
918 if (!mr
->ops
->valid
.min_access_size
) {
922 access_size_max
= mr
->ops
->valid
.max_access_size
;
923 if (!mr
->ops
->valid
.max_access_size
) {
927 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
928 for (i
= 0; i
< size
; i
+= access_size
) {
929 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
938 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
945 access_with_adjusted_size(addr
, &data
, size
,
946 mr
->ops
->impl
.min_access_size
,
947 mr
->ops
->impl
.max_access_size
,
948 memory_region_read_accessor
, mr
);
950 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
951 memory_region_oldmmio_read_accessor
, mr
);
957 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
962 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
963 *pval
= unassigned_mem_read(mr
, addr
, size
);
967 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
968 adjust_endianness(mr
, pval
, size
);
972 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
977 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
978 unassigned_mem_write(mr
, addr
, data
, size
);
982 adjust_endianness(mr
, &data
, size
);
984 if (mr
->ops
->write
) {
985 access_with_adjusted_size(addr
, &data
, size
,
986 mr
->ops
->impl
.min_access_size
,
987 mr
->ops
->impl
.max_access_size
,
988 memory_region_write_accessor
, mr
);
990 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
991 memory_region_oldmmio_write_accessor
, mr
);
996 void memory_region_init_io(MemoryRegion
*mr
,
998 const MemoryRegionOps
*ops
,
1003 memory_region_init(mr
, owner
, name
, size
);
1005 mr
->opaque
= opaque
;
1006 mr
->terminates
= true;
1007 mr
->ram_addr
= ~(ram_addr_t
)0;
1010 void memory_region_init_ram(MemoryRegion
*mr
,
1015 memory_region_init(mr
, owner
, name
, size
);
1017 mr
->terminates
= true;
1018 mr
->destructor
= memory_region_destructor_ram
;
1019 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1022 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1028 memory_region_init(mr
, owner
, name
, size
);
1030 mr
->terminates
= true;
1031 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1032 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1035 void memory_region_init_alias(MemoryRegion
*mr
,
1042 memory_region_init(mr
, owner
, name
, size
);
1043 memory_region_ref(orig
);
1044 mr
->destructor
= memory_region_destructor_alias
;
1046 mr
->alias_offset
= offset
;
1049 void memory_region_init_rom_device(MemoryRegion
*mr
,
1051 const MemoryRegionOps
*ops
,
1056 memory_region_init(mr
, owner
, name
, size
);
1058 mr
->opaque
= opaque
;
1059 mr
->terminates
= true;
1060 mr
->rom_device
= true;
1061 mr
->destructor
= memory_region_destructor_rom_device
;
1062 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1065 void memory_region_init_iommu(MemoryRegion
*mr
,
1067 const MemoryRegionIOMMUOps
*ops
,
1071 memory_region_init(mr
, owner
, name
, size
);
1072 mr
->iommu_ops
= ops
,
1073 mr
->terminates
= true; /* then re-forwards */
1074 notifier_list_init(&mr
->iommu_notify
);
1077 void memory_region_init_reservation(MemoryRegion
*mr
,
1082 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1085 void memory_region_destroy(MemoryRegion
*mr
)
1087 assert(QTAILQ_EMPTY(&mr
->subregions
));
1088 assert(memory_region_transaction_depth
== 0);
1090 memory_region_clear_coalescing(mr
);
1091 g_free((char *)mr
->name
);
1092 g_free(mr
->ioeventfds
);
1095 Object
*memory_region_owner(MemoryRegion
*mr
)
1100 void memory_region_ref(MemoryRegion
*mr
)
1102 if (mr
&& mr
->owner
) {
1103 object_ref(mr
->owner
);
1107 void memory_region_unref(MemoryRegion
*mr
)
1109 if (mr
&& mr
->owner
) {
1110 object_unref(mr
->owner
);
1114 uint64_t memory_region_size(MemoryRegion
*mr
)
1116 if (int128_eq(mr
->size
, int128_2_64())) {
1119 return int128_get64(mr
->size
);
1122 const char *memory_region_name(MemoryRegion
*mr
)
1127 bool memory_region_is_ram(MemoryRegion
*mr
)
1132 bool memory_region_is_logging(MemoryRegion
*mr
)
1134 return mr
->dirty_log_mask
;
1137 bool memory_region_is_rom(MemoryRegion
*mr
)
1139 return mr
->ram
&& mr
->readonly
;
1142 bool memory_region_is_iommu(MemoryRegion
*mr
)
1144 return mr
->iommu_ops
;
1147 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1149 notifier_list_add(&mr
->iommu_notify
, n
);
1152 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1157 void memory_region_notify_iommu(MemoryRegion
*mr
,
1158 IOMMUTLBEntry entry
)
1160 assert(memory_region_is_iommu(mr
));
1161 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1164 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1166 uint8_t mask
= 1 << client
;
1168 memory_region_transaction_begin();
1169 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1170 memory_region_update_pending
|= mr
->enabled
;
1171 memory_region_transaction_commit();
1174 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1175 hwaddr size
, unsigned client
)
1177 assert(mr
->terminates
);
1178 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1182 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1185 assert(mr
->terminates
);
1186 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1189 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1190 hwaddr size
, unsigned client
)
1193 assert(mr
->terminates
);
1194 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1197 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1198 mr
->ram_addr
+ addr
+ size
,
1205 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1210 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1211 FlatView
*view
= address_space_get_flatview(as
);
1212 FOR_EACH_FLAT_RANGE(fr
, view
) {
1214 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1217 flatview_unref(view
);
1221 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1223 if (mr
->readonly
!= readonly
) {
1224 memory_region_transaction_begin();
1225 mr
->readonly
= readonly
;
1226 memory_region_update_pending
|= mr
->enabled
;
1227 memory_region_transaction_commit();
1231 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1233 if (mr
->romd_mode
!= romd_mode
) {
1234 memory_region_transaction_begin();
1235 mr
->romd_mode
= romd_mode
;
1236 memory_region_update_pending
|= mr
->enabled
;
1237 memory_region_transaction_commit();
1241 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1242 hwaddr size
, unsigned client
)
1244 assert(mr
->terminates
);
1245 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1246 mr
->ram_addr
+ addr
+ size
,
1250 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1253 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1256 assert(mr
->terminates
);
1258 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1261 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1265 CoalescedMemoryRange
*cmr
;
1267 MemoryRegionSection section
;
1269 view
= address_space_get_flatview(as
);
1270 FOR_EACH_FLAT_RANGE(fr
, view
) {
1272 section
= (MemoryRegionSection
) {
1273 .address_space
= as
,
1274 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1275 .size
= fr
->addr
.size
,
1278 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1279 int128_get64(fr
->addr
.start
),
1280 int128_get64(fr
->addr
.size
));
1281 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1282 tmp
= addrrange_shift(cmr
->addr
,
1283 int128_sub(fr
->addr
.start
,
1284 int128_make64(fr
->offset_in_region
)));
1285 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1288 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1289 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1290 int128_get64(tmp
.start
),
1291 int128_get64(tmp
.size
));
1295 flatview_unref(view
);
1298 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1302 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1303 memory_region_update_coalesced_range_as(mr
, as
);
1307 void memory_region_set_coalescing(MemoryRegion
*mr
)
1309 memory_region_clear_coalescing(mr
);
1310 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1313 void memory_region_add_coalescing(MemoryRegion
*mr
,
1317 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1319 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1320 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1321 memory_region_update_coalesced_range(mr
);
1322 memory_region_set_flush_coalesced(mr
);
1325 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1327 CoalescedMemoryRange
*cmr
;
1329 qemu_flush_coalesced_mmio_buffer();
1330 mr
->flush_coalesced_mmio
= false;
1332 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1333 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1334 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1337 memory_region_update_coalesced_range(mr
);
1340 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1342 mr
->flush_coalesced_mmio
= true;
1345 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1347 qemu_flush_coalesced_mmio_buffer();
1348 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1349 mr
->flush_coalesced_mmio
= false;
1353 void memory_region_add_eventfd(MemoryRegion
*mr
,
1360 MemoryRegionIoeventfd mrfd
= {
1361 .addr
.start
= int128_make64(addr
),
1362 .addr
.size
= int128_make64(size
),
1363 .match_data
= match_data
,
1369 adjust_endianness(mr
, &mrfd
.data
, size
);
1370 memory_region_transaction_begin();
1371 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1372 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1377 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1378 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1379 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1380 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1381 mr
->ioeventfds
[i
] = mrfd
;
1382 memory_region_update_pending
|= mr
->enabled
;
1383 memory_region_transaction_commit();
1386 void memory_region_del_eventfd(MemoryRegion
*mr
,
1393 MemoryRegionIoeventfd mrfd
= {
1394 .addr
.start
= int128_make64(addr
),
1395 .addr
.size
= int128_make64(size
),
1396 .match_data
= match_data
,
1402 adjust_endianness(mr
, &mrfd
.data
, size
);
1403 memory_region_transaction_begin();
1404 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1405 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1409 assert(i
!= mr
->ioeventfd_nb
);
1410 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1411 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1413 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1414 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1415 memory_region_update_pending
|= mr
->enabled
;
1416 memory_region_transaction_commit();
1419 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1421 MemoryRegion
*subregion
)
1423 MemoryRegion
*other
;
1425 memory_region_transaction_begin();
1427 assert(!subregion
->parent
);
1428 memory_region_ref(subregion
);
1429 subregion
->parent
= mr
;
1430 subregion
->addr
= offset
;
1431 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1432 if (subregion
->may_overlap
|| other
->may_overlap
) {
1435 if (int128_ge(int128_make64(offset
),
1436 int128_add(int128_make64(other
->addr
), other
->size
))
1437 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1438 int128_make64(other
->addr
))) {
1442 printf("warning: subregion collision %llx/%llx (%s) "
1443 "vs %llx/%llx (%s)\n",
1444 (unsigned long long)offset
,
1445 (unsigned long long)int128_get64(subregion
->size
),
1447 (unsigned long long)other
->addr
,
1448 (unsigned long long)int128_get64(other
->size
),
1452 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1453 if (subregion
->priority
>= other
->priority
) {
1454 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1458 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1460 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1461 memory_region_transaction_commit();
1465 void memory_region_add_subregion(MemoryRegion
*mr
,
1467 MemoryRegion
*subregion
)
1469 subregion
->may_overlap
= false;
1470 subregion
->priority
= 0;
1471 memory_region_add_subregion_common(mr
, offset
, subregion
);
1474 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1476 MemoryRegion
*subregion
,
1479 subregion
->may_overlap
= true;
1480 subregion
->priority
= priority
;
1481 memory_region_add_subregion_common(mr
, offset
, subregion
);
1484 void memory_region_del_subregion(MemoryRegion
*mr
,
1485 MemoryRegion
*subregion
)
1487 memory_region_transaction_begin();
1488 assert(subregion
->parent
== mr
);
1489 subregion
->parent
= NULL
;
1490 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1491 memory_region_unref(subregion
);
1492 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1493 memory_region_transaction_commit();
1496 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1498 if (enabled
== mr
->enabled
) {
1501 memory_region_transaction_begin();
1502 mr
->enabled
= enabled
;
1503 memory_region_update_pending
= true;
1504 memory_region_transaction_commit();
1507 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1509 MemoryRegion
*parent
= mr
->parent
;
1510 unsigned priority
= mr
->priority
;
1511 bool may_overlap
= mr
->may_overlap
;
1513 if (addr
== mr
->addr
|| !parent
) {
1518 memory_region_transaction_begin();
1519 memory_region_ref(mr
);
1520 memory_region_del_subregion(parent
, mr
);
1522 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1524 memory_region_add_subregion(parent
, addr
, mr
);
1526 memory_region_unref(mr
);
1527 memory_region_transaction_commit();
1530 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1534 if (offset
== mr
->alias_offset
) {
1538 memory_region_transaction_begin();
1539 mr
->alias_offset
= offset
;
1540 memory_region_update_pending
|= mr
->enabled
;
1541 memory_region_transaction_commit();
1544 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1546 return mr
->ram_addr
;
1549 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1551 const AddrRange
*addr
= addr_
;
1552 const FlatRange
*fr
= fr_
;
1554 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1556 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1562 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1564 return bsearch(&addr
, view
->ranges
, view
->nr
,
1565 sizeof(FlatRange
), cmp_flatrange_addr
);
1568 bool memory_region_present(MemoryRegion
*parent
, hwaddr addr
)
1570 MemoryRegion
*mr
= memory_region_find(parent
, addr
, 1).mr
;
1574 memory_region_unref(mr
);
1578 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1579 hwaddr addr
, uint64_t size
)
1581 MemoryRegionSection ret
= { .mr
= NULL
};
1589 for (root
= mr
; root
->parent
; ) {
1590 root
= root
->parent
;
1594 as
= memory_region_to_address_space(root
);
1595 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1597 view
= address_space_get_flatview(as
);
1598 fr
= flatview_lookup(view
, range
);
1603 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1608 ret
.address_space
= as
;
1609 range
= addrrange_intersection(range
, fr
->addr
);
1610 ret
.offset_within_region
= fr
->offset_in_region
;
1611 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1613 ret
.size
= range
.size
;
1614 ret
.offset_within_address_space
= int128_get64(range
.start
);
1615 ret
.readonly
= fr
->readonly
;
1616 memory_region_ref(ret
.mr
);
1618 flatview_unref(view
);
1622 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1627 view
= address_space_get_flatview(as
);
1628 FOR_EACH_FLAT_RANGE(fr
, view
) {
1629 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1631 flatview_unref(view
);
1634 void memory_global_dirty_log_start(void)
1636 global_dirty_log
= true;
1637 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1640 void memory_global_dirty_log_stop(void)
1642 global_dirty_log
= false;
1643 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1646 static void listener_add_address_space(MemoryListener
*listener
,
1652 if (listener
->address_space_filter
1653 && listener
->address_space_filter
!= as
) {
1657 if (global_dirty_log
) {
1658 if (listener
->log_global_start
) {
1659 listener
->log_global_start(listener
);
1663 view
= address_space_get_flatview(as
);
1664 FOR_EACH_FLAT_RANGE(fr
, view
) {
1665 MemoryRegionSection section
= {
1667 .address_space
= as
,
1668 .offset_within_region
= fr
->offset_in_region
,
1669 .size
= fr
->addr
.size
,
1670 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1671 .readonly
= fr
->readonly
,
1673 if (listener
->region_add
) {
1674 listener
->region_add(listener
, §ion
);
1677 flatview_unref(view
);
1680 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1682 MemoryListener
*other
= NULL
;
1685 listener
->address_space_filter
= filter
;
1686 if (QTAILQ_EMPTY(&memory_listeners
)
1687 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1688 memory_listeners
)->priority
) {
1689 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1691 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1692 if (listener
->priority
< other
->priority
) {
1696 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1699 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1700 listener_add_address_space(listener
, as
);
1704 void memory_listener_unregister(MemoryListener
*listener
)
1706 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1709 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1711 if (QTAILQ_EMPTY(&address_spaces
)) {
1715 memory_region_transaction_begin();
1717 as
->current_map
= g_new(FlatView
, 1);
1718 flatview_init(as
->current_map
);
1719 as
->ioeventfd_nb
= 0;
1720 as
->ioeventfds
= NULL
;
1721 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1722 as
->name
= g_strdup(name
? name
: "anonymous");
1723 address_space_init_dispatch(as
);
1724 memory_region_update_pending
|= root
->enabled
;
1725 memory_region_transaction_commit();
1728 void address_space_destroy(AddressSpace
*as
)
1730 /* Flush out anything from MemoryListeners listening in on this */
1731 memory_region_transaction_begin();
1733 memory_region_transaction_commit();
1734 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1735 address_space_destroy_dispatch(as
);
1736 flatview_unref(as
->current_map
);
1738 g_free(as
->ioeventfds
);
1741 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1743 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1746 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1747 uint64_t val
, unsigned size
)
1749 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1752 typedef struct MemoryRegionList MemoryRegionList
;
1754 struct MemoryRegionList
{
1755 const MemoryRegion
*mr
;
1757 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1760 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1762 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1763 const MemoryRegion
*mr
, unsigned int level
,
1765 MemoryRegionListHead
*alias_print_queue
)
1767 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1768 MemoryRegionListHead submr_print_queue
;
1769 const MemoryRegion
*submr
;
1772 if (!mr
|| !mr
->enabled
) {
1776 for (i
= 0; i
< level
; i
++) {
1781 MemoryRegionList
*ml
;
1784 /* check if the alias is already in the queue */
1785 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1786 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1792 ml
= g_new(MemoryRegionList
, 1);
1794 ml
->printed
= false;
1795 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1797 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1798 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1799 "-" TARGET_FMT_plx
"\n",
1802 + (int128_nz(mr
->size
) ?
1803 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1804 int128_one())) : 0),
1806 mr
->romd_mode
? 'R' : '-',
1807 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1813 + (hwaddr
)int128_get64(mr
->size
) - 1);
1816 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1819 + (int128_nz(mr
->size
) ?
1820 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1821 int128_one())) : 0),
1823 mr
->romd_mode
? 'R' : '-',
1824 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1829 QTAILQ_INIT(&submr_print_queue
);
1831 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1832 new_ml
= g_new(MemoryRegionList
, 1);
1834 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1835 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1836 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1837 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1838 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1844 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1848 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1849 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1853 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1858 void mtree_info(fprintf_function mon_printf
, void *f
)
1860 MemoryRegionListHead ml_head
;
1861 MemoryRegionList
*ml
, *ml2
;
1864 QTAILQ_INIT(&ml_head
);
1866 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1867 mon_printf(f
, "%s\n", as
->name
);
1868 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1871 mon_printf(f
, "aliases\n");
1872 /* print aliased regions */
1873 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1875 mon_printf(f
, "%s\n", ml
->mr
->name
);
1876 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1880 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {