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"
25 #include "exec/ram_addr.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
, client
);
1181 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1184 assert(mr
->terminates
);
1185 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
);
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
, client
);
1195 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1201 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1206 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1207 FlatView
*view
= address_space_get_flatview(as
);
1208 FOR_EACH_FLAT_RANGE(fr
, view
) {
1210 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1213 flatview_unref(view
);
1217 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1219 if (mr
->readonly
!= readonly
) {
1220 memory_region_transaction_begin();
1221 mr
->readonly
= readonly
;
1222 memory_region_update_pending
|= mr
->enabled
;
1223 memory_region_transaction_commit();
1227 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1229 if (mr
->romd_mode
!= romd_mode
) {
1230 memory_region_transaction_begin();
1231 mr
->romd_mode
= romd_mode
;
1232 memory_region_update_pending
|= mr
->enabled
;
1233 memory_region_transaction_commit();
1237 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1238 hwaddr size
, unsigned client
)
1240 assert(mr
->terminates
);
1241 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1244 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1247 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1250 assert(mr
->terminates
);
1252 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1255 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1259 CoalescedMemoryRange
*cmr
;
1261 MemoryRegionSection section
;
1263 view
= address_space_get_flatview(as
);
1264 FOR_EACH_FLAT_RANGE(fr
, view
) {
1266 section
= (MemoryRegionSection
) {
1267 .address_space
= as
,
1268 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1269 .size
= fr
->addr
.size
,
1272 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1273 int128_get64(fr
->addr
.start
),
1274 int128_get64(fr
->addr
.size
));
1275 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1276 tmp
= addrrange_shift(cmr
->addr
,
1277 int128_sub(fr
->addr
.start
,
1278 int128_make64(fr
->offset_in_region
)));
1279 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1282 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1283 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1284 int128_get64(tmp
.start
),
1285 int128_get64(tmp
.size
));
1289 flatview_unref(view
);
1292 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1296 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1297 memory_region_update_coalesced_range_as(mr
, as
);
1301 void memory_region_set_coalescing(MemoryRegion
*mr
)
1303 memory_region_clear_coalescing(mr
);
1304 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1307 void memory_region_add_coalescing(MemoryRegion
*mr
,
1311 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1313 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1314 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1315 memory_region_update_coalesced_range(mr
);
1316 memory_region_set_flush_coalesced(mr
);
1319 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1321 CoalescedMemoryRange
*cmr
;
1323 qemu_flush_coalesced_mmio_buffer();
1324 mr
->flush_coalesced_mmio
= false;
1326 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1327 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1328 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1331 memory_region_update_coalesced_range(mr
);
1334 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1336 mr
->flush_coalesced_mmio
= true;
1339 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1341 qemu_flush_coalesced_mmio_buffer();
1342 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1343 mr
->flush_coalesced_mmio
= false;
1347 void memory_region_add_eventfd(MemoryRegion
*mr
,
1354 MemoryRegionIoeventfd mrfd
= {
1355 .addr
.start
= int128_make64(addr
),
1356 .addr
.size
= int128_make64(size
),
1357 .match_data
= match_data
,
1363 adjust_endianness(mr
, &mrfd
.data
, size
);
1364 memory_region_transaction_begin();
1365 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1366 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1371 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1372 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1373 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1374 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1375 mr
->ioeventfds
[i
] = mrfd
;
1376 memory_region_update_pending
|= mr
->enabled
;
1377 memory_region_transaction_commit();
1380 void memory_region_del_eventfd(MemoryRegion
*mr
,
1387 MemoryRegionIoeventfd mrfd
= {
1388 .addr
.start
= int128_make64(addr
),
1389 .addr
.size
= int128_make64(size
),
1390 .match_data
= match_data
,
1396 adjust_endianness(mr
, &mrfd
.data
, size
);
1397 memory_region_transaction_begin();
1398 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1399 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1403 assert(i
!= mr
->ioeventfd_nb
);
1404 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1405 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1407 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1408 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1409 memory_region_update_pending
|= mr
->enabled
;
1410 memory_region_transaction_commit();
1413 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1415 MemoryRegion
*subregion
)
1417 MemoryRegion
*other
;
1419 memory_region_transaction_begin();
1421 assert(!subregion
->parent
);
1422 memory_region_ref(subregion
);
1423 subregion
->parent
= mr
;
1424 subregion
->addr
= offset
;
1425 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1426 if (subregion
->may_overlap
|| other
->may_overlap
) {
1429 if (int128_ge(int128_make64(offset
),
1430 int128_add(int128_make64(other
->addr
), other
->size
))
1431 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1432 int128_make64(other
->addr
))) {
1436 printf("warning: subregion collision %llx/%llx (%s) "
1437 "vs %llx/%llx (%s)\n",
1438 (unsigned long long)offset
,
1439 (unsigned long long)int128_get64(subregion
->size
),
1441 (unsigned long long)other
->addr
,
1442 (unsigned long long)int128_get64(other
->size
),
1446 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1447 if (subregion
->priority
>= other
->priority
) {
1448 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1452 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1454 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1455 memory_region_transaction_commit();
1459 void memory_region_add_subregion(MemoryRegion
*mr
,
1461 MemoryRegion
*subregion
)
1463 subregion
->may_overlap
= false;
1464 subregion
->priority
= 0;
1465 memory_region_add_subregion_common(mr
, offset
, subregion
);
1468 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1470 MemoryRegion
*subregion
,
1473 subregion
->may_overlap
= true;
1474 subregion
->priority
= priority
;
1475 memory_region_add_subregion_common(mr
, offset
, subregion
);
1478 void memory_region_del_subregion(MemoryRegion
*mr
,
1479 MemoryRegion
*subregion
)
1481 memory_region_transaction_begin();
1482 assert(subregion
->parent
== mr
);
1483 subregion
->parent
= NULL
;
1484 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1485 memory_region_unref(subregion
);
1486 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1487 memory_region_transaction_commit();
1490 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1492 if (enabled
== mr
->enabled
) {
1495 memory_region_transaction_begin();
1496 mr
->enabled
= enabled
;
1497 memory_region_update_pending
= true;
1498 memory_region_transaction_commit();
1501 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1503 MemoryRegion
*parent
= mr
->parent
;
1504 int priority
= mr
->priority
;
1505 bool may_overlap
= mr
->may_overlap
;
1507 if (addr
== mr
->addr
|| !parent
) {
1512 memory_region_transaction_begin();
1513 memory_region_ref(mr
);
1514 memory_region_del_subregion(parent
, mr
);
1516 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1518 memory_region_add_subregion(parent
, addr
, mr
);
1520 memory_region_unref(mr
);
1521 memory_region_transaction_commit();
1524 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1528 if (offset
== mr
->alias_offset
) {
1532 memory_region_transaction_begin();
1533 mr
->alias_offset
= offset
;
1534 memory_region_update_pending
|= mr
->enabled
;
1535 memory_region_transaction_commit();
1538 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1540 return mr
->ram_addr
;
1543 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1545 const AddrRange
*addr
= addr_
;
1546 const FlatRange
*fr
= fr_
;
1548 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1550 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1556 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1558 return bsearch(&addr
, view
->ranges
, view
->nr
,
1559 sizeof(FlatRange
), cmp_flatrange_addr
);
1562 bool memory_region_present(MemoryRegion
*parent
, hwaddr addr
)
1564 MemoryRegion
*mr
= memory_region_find(parent
, addr
, 1).mr
;
1565 if (!mr
|| (mr
== parent
)) {
1568 memory_region_unref(mr
);
1572 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1573 hwaddr addr
, uint64_t size
)
1575 MemoryRegionSection ret
= { .mr
= NULL
};
1583 for (root
= mr
; root
->parent
; ) {
1584 root
= root
->parent
;
1588 as
= memory_region_to_address_space(root
);
1589 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1591 view
= address_space_get_flatview(as
);
1592 fr
= flatview_lookup(view
, range
);
1594 flatview_unref(view
);
1598 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1603 ret
.address_space
= as
;
1604 range
= addrrange_intersection(range
, fr
->addr
);
1605 ret
.offset_within_region
= fr
->offset_in_region
;
1606 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1608 ret
.size
= range
.size
;
1609 ret
.offset_within_address_space
= int128_get64(range
.start
);
1610 ret
.readonly
= fr
->readonly
;
1611 memory_region_ref(ret
.mr
);
1613 flatview_unref(view
);
1617 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1622 view
= address_space_get_flatview(as
);
1623 FOR_EACH_FLAT_RANGE(fr
, view
) {
1624 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1626 flatview_unref(view
);
1629 void memory_global_dirty_log_start(void)
1631 global_dirty_log
= true;
1632 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1635 void memory_global_dirty_log_stop(void)
1637 global_dirty_log
= false;
1638 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1641 static void listener_add_address_space(MemoryListener
*listener
,
1647 if (listener
->address_space_filter
1648 && listener
->address_space_filter
!= as
) {
1652 if (global_dirty_log
) {
1653 if (listener
->log_global_start
) {
1654 listener
->log_global_start(listener
);
1658 view
= address_space_get_flatview(as
);
1659 FOR_EACH_FLAT_RANGE(fr
, view
) {
1660 MemoryRegionSection section
= {
1662 .address_space
= as
,
1663 .offset_within_region
= fr
->offset_in_region
,
1664 .size
= fr
->addr
.size
,
1665 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1666 .readonly
= fr
->readonly
,
1668 if (listener
->region_add
) {
1669 listener
->region_add(listener
, §ion
);
1672 flatview_unref(view
);
1675 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1677 MemoryListener
*other
= NULL
;
1680 listener
->address_space_filter
= filter
;
1681 if (QTAILQ_EMPTY(&memory_listeners
)
1682 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1683 memory_listeners
)->priority
) {
1684 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1686 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1687 if (listener
->priority
< other
->priority
) {
1691 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1694 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1695 listener_add_address_space(listener
, as
);
1699 void memory_listener_unregister(MemoryListener
*listener
)
1701 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1704 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1706 if (QTAILQ_EMPTY(&address_spaces
)) {
1710 memory_region_transaction_begin();
1712 as
->current_map
= g_new(FlatView
, 1);
1713 flatview_init(as
->current_map
);
1714 as
->ioeventfd_nb
= 0;
1715 as
->ioeventfds
= NULL
;
1716 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1717 as
->name
= g_strdup(name
? name
: "anonymous");
1718 address_space_init_dispatch(as
);
1719 memory_region_update_pending
|= root
->enabled
;
1720 memory_region_transaction_commit();
1723 void address_space_destroy(AddressSpace
*as
)
1725 MemoryListener
*listener
;
1727 /* Flush out anything from MemoryListeners listening in on this */
1728 memory_region_transaction_begin();
1730 memory_region_transaction_commit();
1731 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1732 address_space_destroy_dispatch(as
);
1734 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1735 assert(listener
->address_space_filter
!= as
);
1738 flatview_unref(as
->current_map
);
1740 g_free(as
->ioeventfds
);
1743 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1745 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1748 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1749 uint64_t val
, unsigned size
)
1751 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1754 typedef struct MemoryRegionList MemoryRegionList
;
1756 struct MemoryRegionList
{
1757 const MemoryRegion
*mr
;
1759 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1762 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1764 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1765 const MemoryRegion
*mr
, unsigned int level
,
1767 MemoryRegionListHead
*alias_print_queue
)
1769 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1770 MemoryRegionListHead submr_print_queue
;
1771 const MemoryRegion
*submr
;
1774 if (!mr
|| !mr
->enabled
) {
1778 for (i
= 0; i
< level
; i
++) {
1783 MemoryRegionList
*ml
;
1786 /* check if the alias is already in the queue */
1787 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1788 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1794 ml
= g_new(MemoryRegionList
, 1);
1796 ml
->printed
= false;
1797 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1799 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1800 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1801 "-" TARGET_FMT_plx
"\n",
1804 + (int128_nz(mr
->size
) ?
1805 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1806 int128_one())) : 0),
1808 mr
->romd_mode
? 'R' : '-',
1809 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1815 + (int128_nz(mr
->size
) ?
1816 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1817 int128_one())) : 0));
1820 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1823 + (int128_nz(mr
->size
) ?
1824 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1825 int128_one())) : 0),
1827 mr
->romd_mode
? 'R' : '-',
1828 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1833 QTAILQ_INIT(&submr_print_queue
);
1835 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1836 new_ml
= g_new(MemoryRegionList
, 1);
1838 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1839 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1840 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1841 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1842 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1848 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1852 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1853 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1857 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1862 void mtree_info(fprintf_function mon_printf
, void *f
)
1864 MemoryRegionListHead ml_head
;
1865 MemoryRegionList
*ml
, *ml2
;
1868 QTAILQ_INIT(&ml_head
);
1870 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1871 mon_printf(f
, "%s\n", as
->name
);
1872 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1875 mon_printf(f
, "aliases\n");
1876 /* print aliased regions */
1877 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1879 mon_printf(f
, "%s\n", ml
->mr
->name
);
1880 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1884 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {