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 ioeventfd_update_pending
;
32 static bool global_dirty_log
= false;
34 /* flat_view_mutex is taken around reading as->current_map; the critical
35 * section is extremely short, so I'm using a single mutex for every AS.
36 * We could also RCU for the read-side.
38 * The BQL is taken around transaction commits, hence both locks are taken
39 * while writing to as->current_map (with the BQL taken outside).
41 static QemuMutex flat_view_mutex
;
43 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
44 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
46 static QTAILQ_HEAD(, AddressSpace
) address_spaces
47 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
49 static void memory_init(void)
51 qemu_mutex_init(&flat_view_mutex
);
54 typedef struct AddrRange AddrRange
;
57 * Note using signed integers limits us to physical addresses at most
58 * 63 bits wide. They are needed for negative offsetting in aliases
59 * (large MemoryRegion::alias_offset).
66 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
68 return (AddrRange
) { start
, size
};
71 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
73 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
76 static Int128
addrrange_end(AddrRange r
)
78 return int128_add(r
.start
, r
.size
);
81 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
83 int128_addto(&range
.start
, delta
);
87 static bool addrrange_contains(AddrRange range
, Int128 addr
)
89 return int128_ge(addr
, range
.start
)
90 && int128_lt(addr
, addrrange_end(range
));
93 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
95 return addrrange_contains(r1
, r2
.start
)
96 || addrrange_contains(r2
, r1
.start
);
99 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
101 Int128 start
= int128_max(r1
.start
, r2
.start
);
102 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
103 return addrrange_make(start
, int128_sub(end
, start
));
106 enum ListenerDirection
{ Forward
, Reverse
};
108 static bool memory_listener_match(MemoryListener
*listener
,
109 MemoryRegionSection
*section
)
111 return !listener
->address_space_filter
112 || listener
->address_space_filter
== section
->address_space
;
115 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
117 MemoryListener *_listener; \
119 switch (_direction) { \
121 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
122 if (_listener->_callback) { \
123 _listener->_callback(_listener, ##_args); \
128 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
129 memory_listeners, link) { \
130 if (_listener->_callback) { \
131 _listener->_callback(_listener, ##_args); \
140 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
142 MemoryListener *_listener; \
144 switch (_direction) { \
146 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
147 if (_listener->_callback \
148 && memory_listener_match(_listener, _section)) { \
149 _listener->_callback(_listener, _section, ##_args); \
154 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
155 memory_listeners, link) { \
156 if (_listener->_callback \
157 && memory_listener_match(_listener, _section)) { \
158 _listener->_callback(_listener, _section, ##_args); \
167 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
168 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
169 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
171 .address_space = (as), \
172 .offset_within_region = (fr)->offset_in_region, \
173 .size = (fr)->addr.size, \
174 .offset_within_address_space = int128_get64((fr)->addr.start), \
175 .readonly = (fr)->readonly, \
178 struct CoalescedMemoryRange
{
180 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
183 struct MemoryRegionIoeventfd
{
190 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
191 MemoryRegionIoeventfd b
)
193 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
195 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
197 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
199 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
201 } else if (a
.match_data
< b
.match_data
) {
203 } else if (a
.match_data
> b
.match_data
) {
205 } else if (a
.match_data
) {
206 if (a
.data
< b
.data
) {
208 } else if (a
.data
> b
.data
) {
214 } else if (a
.e
> b
.e
) {
220 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
221 MemoryRegionIoeventfd b
)
223 return !memory_region_ioeventfd_before(a
, b
)
224 && !memory_region_ioeventfd_before(b
, a
);
227 typedef struct FlatRange FlatRange
;
228 typedef struct FlatView FlatView
;
230 /* Range of memory in the global map. Addresses are absolute. */
233 hwaddr offset_in_region
;
235 uint8_t dirty_log_mask
;
240 /* Flattened global view of current active memory hierarchy. Kept in sorted
247 unsigned nr_allocated
;
250 typedef struct AddressSpaceOps AddressSpaceOps
;
252 #define FOR_EACH_FLAT_RANGE(var, view) \
253 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
255 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
257 return a
->mr
== b
->mr
258 && addrrange_equal(a
->addr
, b
->addr
)
259 && a
->offset_in_region
== b
->offset_in_region
260 && a
->romd_mode
== b
->romd_mode
261 && a
->readonly
== b
->readonly
;
264 static void flatview_init(FlatView
*view
)
269 view
->nr_allocated
= 0;
272 /* Insert a range into a given position. Caller is responsible for maintaining
275 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
277 if (view
->nr
== view
->nr_allocated
) {
278 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
279 view
->ranges
= g_realloc(view
->ranges
,
280 view
->nr_allocated
* sizeof(*view
->ranges
));
282 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
283 (view
->nr
- pos
) * sizeof(FlatRange
));
284 view
->ranges
[pos
] = *range
;
285 memory_region_ref(range
->mr
);
289 static void flatview_destroy(FlatView
*view
)
293 for (i
= 0; i
< view
->nr
; i
++) {
294 memory_region_unref(view
->ranges
[i
].mr
);
296 g_free(view
->ranges
);
300 static void flatview_ref(FlatView
*view
)
302 atomic_inc(&view
->ref
);
305 static void flatview_unref(FlatView
*view
)
307 if (atomic_fetch_dec(&view
->ref
) == 1) {
308 flatview_destroy(view
);
312 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
314 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
316 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
318 int128_make64(r2
->offset_in_region
))
319 && r1
->dirty_log_mask
== r2
->dirty_log_mask
320 && r1
->romd_mode
== r2
->romd_mode
321 && r1
->readonly
== r2
->readonly
;
324 /* Attempt to simplify a view by merging adjacent ranges */
325 static void flatview_simplify(FlatView
*view
)
330 while (i
< view
->nr
) {
333 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
334 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
338 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
339 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
344 static bool memory_region_big_endian(MemoryRegion
*mr
)
346 #ifdef TARGET_WORDS_BIGENDIAN
347 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
349 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
353 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
355 #ifdef TARGET_WORDS_BIGENDIAN
356 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
358 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
362 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
364 if (memory_region_wrong_endianness(mr
)) {
369 *data
= bswap16(*data
);
372 *data
= bswap32(*data
);
375 *data
= bswap64(*data
);
383 static void memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
392 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
393 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
394 *value
|= (tmp
& mask
) << shift
;
397 static void memory_region_read_accessor(MemoryRegion
*mr
,
406 if (mr
->flush_coalesced_mmio
) {
407 qemu_flush_coalesced_mmio_buffer();
409 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
410 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
411 *value
|= (tmp
& mask
) << shift
;
414 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
423 tmp
= (*value
>> shift
) & mask
;
424 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
425 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
428 static void memory_region_write_accessor(MemoryRegion
*mr
,
437 if (mr
->flush_coalesced_mmio
) {
438 qemu_flush_coalesced_mmio_buffer();
440 tmp
= (*value
>> shift
) & mask
;
441 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
442 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
445 static void access_with_adjusted_size(hwaddr addr
,
448 unsigned access_size_min
,
449 unsigned access_size_max
,
450 void (*access
)(MemoryRegion
*mr
,
458 uint64_t access_mask
;
459 unsigned access_size
;
462 if (!access_size_min
) {
465 if (!access_size_max
) {
469 /* FIXME: support unaligned access? */
470 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
471 access_mask
= -1ULL >> (64 - access_size
* 8);
472 if (memory_region_big_endian(mr
)) {
473 for (i
= 0; i
< size
; i
+= access_size
) {
474 access(mr
, addr
+ i
, value
, access_size
,
475 (size
- access_size
- i
) * 8, access_mask
);
478 for (i
= 0; i
< size
; i
+= access_size
) {
479 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
484 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
488 while (mr
->container
) {
491 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
492 if (mr
== as
->root
) {
499 /* Render a memory region into the global view. Ranges in @view obscure
502 static void render_memory_region(FlatView
*view
,
508 MemoryRegion
*subregion
;
510 hwaddr offset_in_region
;
520 int128_addto(&base
, int128_make64(mr
->addr
));
521 readonly
|= mr
->readonly
;
523 tmp
= addrrange_make(base
, mr
->size
);
525 if (!addrrange_intersects(tmp
, clip
)) {
529 clip
= addrrange_intersection(tmp
, clip
);
532 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
533 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
534 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
538 /* Render subregions in priority order. */
539 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
540 render_memory_region(view
, subregion
, base
, clip
, readonly
);
543 if (!mr
->terminates
) {
547 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
552 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
553 fr
.romd_mode
= mr
->romd_mode
;
554 fr
.readonly
= readonly
;
556 /* Render the region itself into any gaps left by the current view. */
557 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
558 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
561 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
562 now
= int128_min(remain
,
563 int128_sub(view
->ranges
[i
].addr
.start
, base
));
564 fr
.offset_in_region
= offset_in_region
;
565 fr
.addr
= addrrange_make(base
, now
);
566 flatview_insert(view
, i
, &fr
);
568 int128_addto(&base
, now
);
569 offset_in_region
+= int128_get64(now
);
570 int128_subfrom(&remain
, now
);
572 now
= int128_sub(int128_min(int128_add(base
, remain
),
573 addrrange_end(view
->ranges
[i
].addr
)),
575 int128_addto(&base
, now
);
576 offset_in_region
+= int128_get64(now
);
577 int128_subfrom(&remain
, now
);
579 if (int128_nz(remain
)) {
580 fr
.offset_in_region
= offset_in_region
;
581 fr
.addr
= addrrange_make(base
, remain
);
582 flatview_insert(view
, i
, &fr
);
586 /* Render a memory topology into a list of disjoint absolute ranges. */
587 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
591 view
= g_new(FlatView
, 1);
595 render_memory_region(view
, mr
, int128_zero(),
596 addrrange_make(int128_zero(), int128_2_64()), false);
598 flatview_simplify(view
);
603 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
604 MemoryRegionIoeventfd
*fds_new
,
606 MemoryRegionIoeventfd
*fds_old
,
610 MemoryRegionIoeventfd
*fd
;
611 MemoryRegionSection section
;
613 /* Generate a symmetric difference of the old and new fd sets, adding
614 * and deleting as necessary.
618 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
619 if (iold
< fds_old_nb
620 && (inew
== fds_new_nb
621 || memory_region_ioeventfd_before(fds_old
[iold
],
624 section
= (MemoryRegionSection
) {
626 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
627 .size
= fd
->addr
.size
,
629 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
630 fd
->match_data
, fd
->data
, fd
->e
);
632 } else if (inew
< fds_new_nb
633 && (iold
== fds_old_nb
634 || memory_region_ioeventfd_before(fds_new
[inew
],
637 section
= (MemoryRegionSection
) {
639 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
640 .size
= fd
->addr
.size
,
642 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
643 fd
->match_data
, fd
->data
, fd
->e
);
652 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
656 qemu_mutex_lock(&flat_view_mutex
);
657 view
= as
->current_map
;
659 qemu_mutex_unlock(&flat_view_mutex
);
663 static void address_space_update_ioeventfds(AddressSpace
*as
)
667 unsigned ioeventfd_nb
= 0;
668 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
672 view
= address_space_get_flatview(as
);
673 FOR_EACH_FLAT_RANGE(fr
, view
) {
674 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
675 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
676 int128_sub(fr
->addr
.start
,
677 int128_make64(fr
->offset_in_region
)));
678 if (addrrange_intersects(fr
->addr
, tmp
)) {
680 ioeventfds
= g_realloc(ioeventfds
,
681 ioeventfd_nb
* sizeof(*ioeventfds
));
682 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
683 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
688 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
689 as
->ioeventfds
, as
->ioeventfd_nb
);
691 g_free(as
->ioeventfds
);
692 as
->ioeventfds
= ioeventfds
;
693 as
->ioeventfd_nb
= ioeventfd_nb
;
694 flatview_unref(view
);
697 static void address_space_update_topology_pass(AddressSpace
*as
,
698 const FlatView
*old_view
,
699 const FlatView
*new_view
,
703 FlatRange
*frold
, *frnew
;
705 /* Generate a symmetric difference of the old and new memory maps.
706 * Kill ranges in the old map, and instantiate ranges in the new map.
709 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
710 if (iold
< old_view
->nr
) {
711 frold
= &old_view
->ranges
[iold
];
715 if (inew
< new_view
->nr
) {
716 frnew
= &new_view
->ranges
[inew
];
723 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
724 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
725 && !flatrange_equal(frold
, frnew
)))) {
726 /* In old but not in new, or in both but attributes changed. */
729 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
733 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
734 /* In both and unchanged (except logging may have changed) */
737 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
738 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
739 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
740 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
741 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
751 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
760 static void address_space_update_topology(AddressSpace
*as
)
762 FlatView
*old_view
= address_space_get_flatview(as
);
763 FlatView
*new_view
= generate_memory_topology(as
->root
);
765 address_space_update_topology_pass(as
, old_view
, new_view
, false);
766 address_space_update_topology_pass(as
, old_view
, new_view
, true);
768 qemu_mutex_lock(&flat_view_mutex
);
769 flatview_unref(as
->current_map
);
770 as
->current_map
= new_view
;
771 qemu_mutex_unlock(&flat_view_mutex
);
773 /* Note that all the old MemoryRegions are still alive up to this
774 * point. This relieves most MemoryListeners from the need to
775 * ref/unref the MemoryRegions they get---unless they use them
776 * outside the iothread mutex, in which case precise reference
777 * counting is necessary.
779 flatview_unref(old_view
);
781 address_space_update_ioeventfds(as
);
784 void memory_region_transaction_begin(void)
786 qemu_flush_coalesced_mmio_buffer();
787 ++memory_region_transaction_depth
;
790 static void memory_region_clear_pending(void)
792 memory_region_update_pending
= false;
793 ioeventfd_update_pending
= false;
796 void memory_region_transaction_commit(void)
800 assert(memory_region_transaction_depth
);
801 --memory_region_transaction_depth
;
802 if (!memory_region_transaction_depth
) {
803 if (memory_region_update_pending
) {
804 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
806 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
807 address_space_update_topology(as
);
810 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
811 } else if (ioeventfd_update_pending
) {
812 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
813 address_space_update_ioeventfds(as
);
816 memory_region_clear_pending();
820 static void memory_region_destructor_none(MemoryRegion
*mr
)
824 static void memory_region_destructor_ram(MemoryRegion
*mr
)
826 qemu_ram_free(mr
->ram_addr
);
829 static void memory_region_destructor_alias(MemoryRegion
*mr
)
831 memory_region_unref(mr
->alias
);
834 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
836 qemu_ram_free_from_ptr(mr
->ram_addr
);
839 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
841 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
844 void memory_region_init(MemoryRegion
*mr
,
849 mr
->ops
= &unassigned_mem_ops
;
852 mr
->iommu_ops
= NULL
;
853 mr
->container
= NULL
;
854 mr
->size
= int128_make64(size
);
855 if (size
== UINT64_MAX
) {
856 mr
->size
= int128_2_64();
861 mr
->terminates
= false;
863 mr
->romd_mode
= true;
864 mr
->readonly
= false;
865 mr
->rom_device
= false;
866 mr
->destructor
= memory_region_destructor_none
;
868 mr
->may_overlap
= false;
870 QTAILQ_INIT(&mr
->subregions
);
871 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
872 QTAILQ_INIT(&mr
->coalesced
);
873 mr
->name
= g_strdup(name
);
874 mr
->dirty_log_mask
= 0;
875 mr
->ioeventfd_nb
= 0;
876 mr
->ioeventfds
= NULL
;
877 mr
->flush_coalesced_mmio
= false;
880 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
883 #ifdef DEBUG_UNASSIGNED
884 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
886 if (current_cpu
!= NULL
) {
887 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
892 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
893 uint64_t val
, unsigned size
)
895 #ifdef DEBUG_UNASSIGNED
896 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
898 if (current_cpu
!= NULL
) {
899 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
903 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
904 unsigned size
, bool is_write
)
909 const MemoryRegionOps unassigned_mem_ops
= {
910 .valid
.accepts
= unassigned_mem_accepts
,
911 .endianness
= DEVICE_NATIVE_ENDIAN
,
914 bool memory_region_access_valid(MemoryRegion
*mr
,
919 int access_size_min
, access_size_max
;
922 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
926 if (!mr
->ops
->valid
.accepts
) {
930 access_size_min
= mr
->ops
->valid
.min_access_size
;
931 if (!mr
->ops
->valid
.min_access_size
) {
935 access_size_max
= mr
->ops
->valid
.max_access_size
;
936 if (!mr
->ops
->valid
.max_access_size
) {
940 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
941 for (i
= 0; i
< size
; i
+= access_size
) {
942 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
951 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
958 access_with_adjusted_size(addr
, &data
, size
,
959 mr
->ops
->impl
.min_access_size
,
960 mr
->ops
->impl
.max_access_size
,
961 memory_region_read_accessor
, mr
);
963 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
964 memory_region_oldmmio_read_accessor
, mr
);
970 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
975 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
976 *pval
= unassigned_mem_read(mr
, addr
, size
);
980 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
981 adjust_endianness(mr
, pval
, size
);
985 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
990 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
991 unassigned_mem_write(mr
, addr
, data
, size
);
995 adjust_endianness(mr
, &data
, size
);
997 if (mr
->ops
->write
) {
998 access_with_adjusted_size(addr
, &data
, size
,
999 mr
->ops
->impl
.min_access_size
,
1000 mr
->ops
->impl
.max_access_size
,
1001 memory_region_write_accessor
, mr
);
1003 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1004 memory_region_oldmmio_write_accessor
, mr
);
1009 void memory_region_init_io(MemoryRegion
*mr
,
1011 const MemoryRegionOps
*ops
,
1016 memory_region_init(mr
, owner
, name
, size
);
1018 mr
->opaque
= opaque
;
1019 mr
->terminates
= true;
1020 mr
->ram_addr
= ~(ram_addr_t
)0;
1023 void memory_region_init_ram(MemoryRegion
*mr
,
1028 memory_region_init(mr
, owner
, name
, size
);
1030 mr
->terminates
= true;
1031 mr
->destructor
= memory_region_destructor_ram
;
1032 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1035 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1041 memory_region_init(mr
, owner
, name
, size
);
1043 mr
->terminates
= true;
1044 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1045 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1048 void memory_region_init_alias(MemoryRegion
*mr
,
1055 memory_region_init(mr
, owner
, name
, size
);
1056 memory_region_ref(orig
);
1057 mr
->destructor
= memory_region_destructor_alias
;
1059 mr
->alias_offset
= offset
;
1062 void memory_region_init_rom_device(MemoryRegion
*mr
,
1064 const MemoryRegionOps
*ops
,
1069 memory_region_init(mr
, owner
, name
, size
);
1071 mr
->opaque
= opaque
;
1072 mr
->terminates
= true;
1073 mr
->rom_device
= true;
1074 mr
->destructor
= memory_region_destructor_rom_device
;
1075 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1078 void memory_region_init_iommu(MemoryRegion
*mr
,
1080 const MemoryRegionIOMMUOps
*ops
,
1084 memory_region_init(mr
, owner
, name
, size
);
1085 mr
->iommu_ops
= ops
,
1086 mr
->terminates
= true; /* then re-forwards */
1087 notifier_list_init(&mr
->iommu_notify
);
1090 void memory_region_init_reservation(MemoryRegion
*mr
,
1095 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1098 void memory_region_destroy(MemoryRegion
*mr
)
1100 assert(QTAILQ_EMPTY(&mr
->subregions
));
1101 assert(memory_region_transaction_depth
== 0);
1103 memory_region_clear_coalescing(mr
);
1104 g_free((char *)mr
->name
);
1105 g_free(mr
->ioeventfds
);
1108 Object
*memory_region_owner(MemoryRegion
*mr
)
1113 void memory_region_ref(MemoryRegion
*mr
)
1115 if (mr
&& mr
->owner
) {
1116 object_ref(mr
->owner
);
1120 void memory_region_unref(MemoryRegion
*mr
)
1122 if (mr
&& mr
->owner
) {
1123 object_unref(mr
->owner
);
1127 uint64_t memory_region_size(MemoryRegion
*mr
)
1129 if (int128_eq(mr
->size
, int128_2_64())) {
1132 return int128_get64(mr
->size
);
1135 const char *memory_region_name(MemoryRegion
*mr
)
1140 bool memory_region_is_ram(MemoryRegion
*mr
)
1145 bool memory_region_is_logging(MemoryRegion
*mr
)
1147 return mr
->dirty_log_mask
;
1150 bool memory_region_is_rom(MemoryRegion
*mr
)
1152 return mr
->ram
&& mr
->readonly
;
1155 bool memory_region_is_iommu(MemoryRegion
*mr
)
1157 return mr
->iommu_ops
;
1160 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1162 notifier_list_add(&mr
->iommu_notify
, n
);
1165 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1170 void memory_region_notify_iommu(MemoryRegion
*mr
,
1171 IOMMUTLBEntry entry
)
1173 assert(memory_region_is_iommu(mr
));
1174 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1177 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1179 uint8_t mask
= 1 << client
;
1181 memory_region_transaction_begin();
1182 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1183 memory_region_update_pending
|= mr
->enabled
;
1184 memory_region_transaction_commit();
1187 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1188 hwaddr size
, unsigned client
)
1190 assert(mr
->terminates
);
1191 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1194 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1197 assert(mr
->terminates
);
1198 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
);
1201 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1202 hwaddr size
, unsigned client
)
1205 assert(mr
->terminates
);
1206 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1208 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1214 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1219 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1220 FlatView
*view
= address_space_get_flatview(as
);
1221 FOR_EACH_FLAT_RANGE(fr
, view
) {
1223 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1226 flatview_unref(view
);
1230 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1232 if (mr
->readonly
!= readonly
) {
1233 memory_region_transaction_begin();
1234 mr
->readonly
= readonly
;
1235 memory_region_update_pending
|= mr
->enabled
;
1236 memory_region_transaction_commit();
1240 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1242 if (mr
->romd_mode
!= romd_mode
) {
1243 memory_region_transaction_begin();
1244 mr
->romd_mode
= romd_mode
;
1245 memory_region_update_pending
|= mr
->enabled
;
1246 memory_region_transaction_commit();
1250 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1251 hwaddr size
, unsigned client
)
1253 assert(mr
->terminates
);
1254 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1257 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1260 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1263 assert(mr
->terminates
);
1265 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1268 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1272 CoalescedMemoryRange
*cmr
;
1274 MemoryRegionSection section
;
1276 view
= address_space_get_flatview(as
);
1277 FOR_EACH_FLAT_RANGE(fr
, view
) {
1279 section
= (MemoryRegionSection
) {
1280 .address_space
= as
,
1281 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1282 .size
= fr
->addr
.size
,
1285 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1286 int128_get64(fr
->addr
.start
),
1287 int128_get64(fr
->addr
.size
));
1288 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1289 tmp
= addrrange_shift(cmr
->addr
,
1290 int128_sub(fr
->addr
.start
,
1291 int128_make64(fr
->offset_in_region
)));
1292 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1295 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1296 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1297 int128_get64(tmp
.start
),
1298 int128_get64(tmp
.size
));
1302 flatview_unref(view
);
1305 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1309 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1310 memory_region_update_coalesced_range_as(mr
, as
);
1314 void memory_region_set_coalescing(MemoryRegion
*mr
)
1316 memory_region_clear_coalescing(mr
);
1317 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1320 void memory_region_add_coalescing(MemoryRegion
*mr
,
1324 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1326 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1327 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1328 memory_region_update_coalesced_range(mr
);
1329 memory_region_set_flush_coalesced(mr
);
1332 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1334 CoalescedMemoryRange
*cmr
;
1335 bool updated
= false;
1337 qemu_flush_coalesced_mmio_buffer();
1338 mr
->flush_coalesced_mmio
= false;
1340 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1341 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1342 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1348 memory_region_update_coalesced_range(mr
);
1352 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1354 mr
->flush_coalesced_mmio
= true;
1357 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1359 qemu_flush_coalesced_mmio_buffer();
1360 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1361 mr
->flush_coalesced_mmio
= false;
1365 void memory_region_add_eventfd(MemoryRegion
*mr
,
1372 MemoryRegionIoeventfd mrfd
= {
1373 .addr
.start
= int128_make64(addr
),
1374 .addr
.size
= int128_make64(size
),
1375 .match_data
= match_data
,
1381 adjust_endianness(mr
, &mrfd
.data
, size
);
1382 memory_region_transaction_begin();
1383 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1384 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1389 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1390 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1391 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1392 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1393 mr
->ioeventfds
[i
] = mrfd
;
1394 ioeventfd_update_pending
|= mr
->enabled
;
1395 memory_region_transaction_commit();
1398 void memory_region_del_eventfd(MemoryRegion
*mr
,
1405 MemoryRegionIoeventfd mrfd
= {
1406 .addr
.start
= int128_make64(addr
),
1407 .addr
.size
= int128_make64(size
),
1408 .match_data
= match_data
,
1414 adjust_endianness(mr
, &mrfd
.data
, size
);
1415 memory_region_transaction_begin();
1416 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1417 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1421 assert(i
!= mr
->ioeventfd_nb
);
1422 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1423 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1425 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1426 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1427 ioeventfd_update_pending
|= mr
->enabled
;
1428 memory_region_transaction_commit();
1431 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1433 hwaddr offset
= subregion
->addr
;
1434 MemoryRegion
*mr
= subregion
->container
;
1435 MemoryRegion
*other
;
1437 memory_region_transaction_begin();
1439 memory_region_ref(subregion
);
1440 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1441 if (subregion
->may_overlap
|| other
->may_overlap
) {
1444 if (int128_ge(int128_make64(offset
),
1445 int128_add(int128_make64(other
->addr
), other
->size
))
1446 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1447 int128_make64(other
->addr
))) {
1451 printf("warning: subregion collision %llx/%llx (%s) "
1452 "vs %llx/%llx (%s)\n",
1453 (unsigned long long)offset
,
1454 (unsigned long long)int128_get64(subregion
->size
),
1456 (unsigned long long)other
->addr
,
1457 (unsigned long long)int128_get64(other
->size
),
1461 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1462 if (subregion
->priority
>= other
->priority
) {
1463 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1467 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1469 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1470 memory_region_transaction_commit();
1473 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1475 MemoryRegion
*subregion
)
1477 assert(!subregion
->container
);
1478 subregion
->container
= mr
;
1479 subregion
->addr
= offset
;
1480 memory_region_update_container_subregions(subregion
);
1483 void memory_region_add_subregion(MemoryRegion
*mr
,
1485 MemoryRegion
*subregion
)
1487 subregion
->may_overlap
= false;
1488 subregion
->priority
= 0;
1489 memory_region_add_subregion_common(mr
, offset
, subregion
);
1492 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1494 MemoryRegion
*subregion
,
1497 subregion
->may_overlap
= true;
1498 subregion
->priority
= priority
;
1499 memory_region_add_subregion_common(mr
, offset
, subregion
);
1502 void memory_region_del_subregion(MemoryRegion
*mr
,
1503 MemoryRegion
*subregion
)
1505 memory_region_transaction_begin();
1506 assert(subregion
->container
== mr
);
1507 subregion
->container
= NULL
;
1508 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1509 memory_region_unref(subregion
);
1510 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1511 memory_region_transaction_commit();
1514 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1516 if (enabled
== mr
->enabled
) {
1519 memory_region_transaction_begin();
1520 mr
->enabled
= enabled
;
1521 memory_region_update_pending
= true;
1522 memory_region_transaction_commit();
1525 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1527 MemoryRegion
*container
= mr
->container
;
1530 memory_region_transaction_begin();
1531 memory_region_ref(mr
);
1532 memory_region_del_subregion(container
, mr
);
1533 mr
->container
= container
;
1534 memory_region_update_container_subregions(mr
);
1535 memory_region_unref(mr
);
1536 memory_region_transaction_commit();
1540 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1542 if (addr
!= mr
->addr
) {
1544 memory_region_readd_subregion(mr
);
1548 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1552 if (offset
== mr
->alias_offset
) {
1556 memory_region_transaction_begin();
1557 mr
->alias_offset
= offset
;
1558 memory_region_update_pending
|= mr
->enabled
;
1559 memory_region_transaction_commit();
1562 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1564 return mr
->ram_addr
;
1567 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1569 const AddrRange
*addr
= addr_
;
1570 const FlatRange
*fr
= fr_
;
1572 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1574 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1580 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1582 return bsearch(&addr
, view
->ranges
, view
->nr
,
1583 sizeof(FlatRange
), cmp_flatrange_addr
);
1586 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
1588 MemoryRegion
*mr
= memory_region_find(container
, addr
, 1).mr
;
1589 if (!mr
|| (mr
== container
)) {
1592 memory_region_unref(mr
);
1596 bool memory_region_is_mapped(MemoryRegion
*mr
)
1598 return mr
->container
? true : false;
1601 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1602 hwaddr addr
, uint64_t size
)
1604 MemoryRegionSection ret
= { .mr
= NULL
};
1612 for (root
= mr
; root
->container
; ) {
1613 root
= root
->container
;
1617 as
= memory_region_to_address_space(root
);
1621 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1623 view
= address_space_get_flatview(as
);
1624 fr
= flatview_lookup(view
, range
);
1626 flatview_unref(view
);
1630 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1635 ret
.address_space
= as
;
1636 range
= addrrange_intersection(range
, fr
->addr
);
1637 ret
.offset_within_region
= fr
->offset_in_region
;
1638 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1640 ret
.size
= range
.size
;
1641 ret
.offset_within_address_space
= int128_get64(range
.start
);
1642 ret
.readonly
= fr
->readonly
;
1643 memory_region_ref(ret
.mr
);
1645 flatview_unref(view
);
1649 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1654 view
= address_space_get_flatview(as
);
1655 FOR_EACH_FLAT_RANGE(fr
, view
) {
1656 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1658 flatview_unref(view
);
1661 void memory_global_dirty_log_start(void)
1663 global_dirty_log
= true;
1664 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1667 void memory_global_dirty_log_stop(void)
1669 global_dirty_log
= false;
1670 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1673 static void listener_add_address_space(MemoryListener
*listener
,
1679 if (listener
->address_space_filter
1680 && listener
->address_space_filter
!= as
) {
1684 if (global_dirty_log
) {
1685 if (listener
->log_global_start
) {
1686 listener
->log_global_start(listener
);
1690 view
= address_space_get_flatview(as
);
1691 FOR_EACH_FLAT_RANGE(fr
, view
) {
1692 MemoryRegionSection section
= {
1694 .address_space
= as
,
1695 .offset_within_region
= fr
->offset_in_region
,
1696 .size
= fr
->addr
.size
,
1697 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1698 .readonly
= fr
->readonly
,
1700 if (listener
->region_add
) {
1701 listener
->region_add(listener
, §ion
);
1704 flatview_unref(view
);
1707 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1709 MemoryListener
*other
= NULL
;
1712 listener
->address_space_filter
= filter
;
1713 if (QTAILQ_EMPTY(&memory_listeners
)
1714 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1715 memory_listeners
)->priority
) {
1716 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1718 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1719 if (listener
->priority
< other
->priority
) {
1723 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1726 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1727 listener_add_address_space(listener
, as
);
1731 void memory_listener_unregister(MemoryListener
*listener
)
1733 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1736 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1738 if (QTAILQ_EMPTY(&address_spaces
)) {
1742 memory_region_transaction_begin();
1744 as
->current_map
= g_new(FlatView
, 1);
1745 flatview_init(as
->current_map
);
1746 as
->ioeventfd_nb
= 0;
1747 as
->ioeventfds
= NULL
;
1748 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1749 as
->name
= g_strdup(name
? name
: "anonymous");
1750 address_space_init_dispatch(as
);
1751 memory_region_update_pending
|= root
->enabled
;
1752 memory_region_transaction_commit();
1755 void address_space_destroy(AddressSpace
*as
)
1757 MemoryListener
*listener
;
1759 /* Flush out anything from MemoryListeners listening in on this */
1760 memory_region_transaction_begin();
1762 memory_region_transaction_commit();
1763 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1764 address_space_destroy_dispatch(as
);
1766 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1767 assert(listener
->address_space_filter
!= as
);
1770 flatview_unref(as
->current_map
);
1772 g_free(as
->ioeventfds
);
1775 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1777 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1780 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1781 uint64_t val
, unsigned size
)
1783 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1786 typedef struct MemoryRegionList MemoryRegionList
;
1788 struct MemoryRegionList
{
1789 const MemoryRegion
*mr
;
1791 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1794 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1796 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1797 const MemoryRegion
*mr
, unsigned int level
,
1799 MemoryRegionListHead
*alias_print_queue
)
1801 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1802 MemoryRegionListHead submr_print_queue
;
1803 const MemoryRegion
*submr
;
1806 if (!mr
|| !mr
->enabled
) {
1810 for (i
= 0; i
< level
; i
++) {
1815 MemoryRegionList
*ml
;
1818 /* check if the alias is already in the queue */
1819 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1820 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1826 ml
= g_new(MemoryRegionList
, 1);
1828 ml
->printed
= false;
1829 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1831 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1832 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1833 "-" TARGET_FMT_plx
"\n",
1836 + (int128_nz(mr
->size
) ?
1837 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1838 int128_one())) : 0),
1840 mr
->romd_mode
? 'R' : '-',
1841 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1847 + (int128_nz(mr
->size
) ?
1848 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1849 int128_one())) : 0));
1852 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1855 + (int128_nz(mr
->size
) ?
1856 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1857 int128_one())) : 0),
1859 mr
->romd_mode
? 'R' : '-',
1860 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1865 QTAILQ_INIT(&submr_print_queue
);
1867 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1868 new_ml
= g_new(MemoryRegionList
, 1);
1870 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1871 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1872 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1873 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1874 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1880 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1884 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1885 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1889 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1894 void mtree_info(fprintf_function mon_printf
, void *f
)
1896 MemoryRegionListHead ml_head
;
1897 MemoryRegionList
*ml
, *ml2
;
1900 QTAILQ_INIT(&ml_head
);
1902 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1903 mon_printf(f
, "%s\n", as
->name
);
1904 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1907 mon_printf(f
, "aliases\n");
1908 /* print aliased regions */
1909 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1911 mon_printf(f
, "%s\n", ml
->mr
->name
);
1912 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1916 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {