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"
26 #include "sysemu/sysemu.h"
28 //#define DEBUG_UNASSIGNED
30 static unsigned memory_region_transaction_depth
;
31 static bool memory_region_update_pending
;
32 static bool ioeventfd_update_pending
;
33 static bool global_dirty_log
= false;
35 /* flat_view_mutex is taken around reading as->current_map; the critical
36 * section is extremely short, so I'm using a single mutex for every AS.
37 * We could also RCU for the read-side.
39 * The BQL is taken around transaction commits, hence both locks are taken
40 * while writing to as->current_map (with the BQL taken outside).
42 static QemuMutex flat_view_mutex
;
44 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
45 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
47 static QTAILQ_HEAD(, AddressSpace
) address_spaces
48 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
50 static void memory_init(void)
52 qemu_mutex_init(&flat_view_mutex
);
55 typedef struct AddrRange AddrRange
;
58 * Note using signed integers limits us to physical addresses at most
59 * 63 bits wide. They are needed for negative offsetting in aliases
60 * (large MemoryRegion::alias_offset).
67 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
69 return (AddrRange
) { start
, size
};
72 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
74 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
77 static Int128
addrrange_end(AddrRange r
)
79 return int128_add(r
.start
, r
.size
);
82 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
84 int128_addto(&range
.start
, delta
);
88 static bool addrrange_contains(AddrRange range
, Int128 addr
)
90 return int128_ge(addr
, range
.start
)
91 && int128_lt(addr
, addrrange_end(range
));
94 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
96 return addrrange_contains(r1
, r2
.start
)
97 || addrrange_contains(r2
, r1
.start
);
100 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
102 Int128 start
= int128_max(r1
.start
, r2
.start
);
103 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
104 return addrrange_make(start
, int128_sub(end
, start
));
107 enum ListenerDirection
{ Forward
, Reverse
};
109 static bool memory_listener_match(MemoryListener
*listener
,
110 MemoryRegionSection
*section
)
112 return !listener
->address_space_filter
113 || listener
->address_space_filter
== section
->address_space
;
116 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
118 MemoryListener *_listener; \
120 switch (_direction) { \
122 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
123 if (_listener->_callback) { \
124 _listener->_callback(_listener, ##_args); \
129 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
130 memory_listeners, link) { \
131 if (_listener->_callback) { \
132 _listener->_callback(_listener, ##_args); \
141 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
143 MemoryListener *_listener; \
145 switch (_direction) { \
147 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
148 if (_listener->_callback \
149 && memory_listener_match(_listener, _section)) { \
150 _listener->_callback(_listener, _section, ##_args); \
155 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
156 memory_listeners, link) { \
157 if (_listener->_callback \
158 && memory_listener_match(_listener, _section)) { \
159 _listener->_callback(_listener, _section, ##_args); \
168 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
169 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
170 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
172 .address_space = (as), \
173 .offset_within_region = (fr)->offset_in_region, \
174 .size = (fr)->addr.size, \
175 .offset_within_address_space = int128_get64((fr)->addr.start), \
176 .readonly = (fr)->readonly, \
179 struct CoalescedMemoryRange
{
181 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
184 struct MemoryRegionIoeventfd
{
191 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
192 MemoryRegionIoeventfd b
)
194 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
196 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
198 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
200 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
202 } else if (a
.match_data
< b
.match_data
) {
204 } else if (a
.match_data
> b
.match_data
) {
206 } else if (a
.match_data
) {
207 if (a
.data
< b
.data
) {
209 } else if (a
.data
> b
.data
) {
215 } else if (a
.e
> b
.e
) {
221 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
222 MemoryRegionIoeventfd b
)
224 return !memory_region_ioeventfd_before(a
, b
)
225 && !memory_region_ioeventfd_before(b
, a
);
228 typedef struct FlatRange FlatRange
;
229 typedef struct FlatView FlatView
;
231 /* Range of memory in the global map. Addresses are absolute. */
234 hwaddr offset_in_region
;
236 uint8_t dirty_log_mask
;
241 /* Flattened global view of current active memory hierarchy. Kept in sorted
248 unsigned nr_allocated
;
251 typedef struct AddressSpaceOps AddressSpaceOps
;
253 #define FOR_EACH_FLAT_RANGE(var, view) \
254 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
256 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
258 return a
->mr
== b
->mr
259 && addrrange_equal(a
->addr
, b
->addr
)
260 && a
->offset_in_region
== b
->offset_in_region
261 && a
->romd_mode
== b
->romd_mode
262 && a
->readonly
== b
->readonly
;
265 static void flatview_init(FlatView
*view
)
270 view
->nr_allocated
= 0;
273 /* Insert a range into a given position. Caller is responsible for maintaining
276 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
278 if (view
->nr
== view
->nr_allocated
) {
279 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
280 view
->ranges
= g_realloc(view
->ranges
,
281 view
->nr_allocated
* sizeof(*view
->ranges
));
283 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
284 (view
->nr
- pos
) * sizeof(FlatRange
));
285 view
->ranges
[pos
] = *range
;
286 memory_region_ref(range
->mr
);
290 static void flatview_destroy(FlatView
*view
)
294 for (i
= 0; i
< view
->nr
; i
++) {
295 memory_region_unref(view
->ranges
[i
].mr
);
297 g_free(view
->ranges
);
301 static void flatview_ref(FlatView
*view
)
303 atomic_inc(&view
->ref
);
306 static void flatview_unref(FlatView
*view
)
308 if (atomic_fetch_dec(&view
->ref
) == 1) {
309 flatview_destroy(view
);
313 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
315 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
317 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
319 int128_make64(r2
->offset_in_region
))
320 && r1
->dirty_log_mask
== r2
->dirty_log_mask
321 && r1
->romd_mode
== r2
->romd_mode
322 && r1
->readonly
== r2
->readonly
;
325 /* Attempt to simplify a view by merging adjacent ranges */
326 static void flatview_simplify(FlatView
*view
)
331 while (i
< view
->nr
) {
334 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
335 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
339 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
340 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
345 static bool memory_region_big_endian(MemoryRegion
*mr
)
347 #ifdef TARGET_WORDS_BIGENDIAN
348 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
350 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
354 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
356 #ifdef TARGET_WORDS_BIGENDIAN
357 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
359 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
363 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
365 if (memory_region_wrong_endianness(mr
)) {
370 *data
= bswap16(*data
);
373 *data
= bswap32(*data
);
376 *data
= bswap64(*data
);
384 static void memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
393 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
394 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
395 *value
|= (tmp
& mask
) << shift
;
398 static void memory_region_read_accessor(MemoryRegion
*mr
,
407 if (mr
->flush_coalesced_mmio
) {
408 qemu_flush_coalesced_mmio_buffer();
410 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
411 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
412 *value
|= (tmp
& mask
) << shift
;
415 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
424 tmp
= (*value
>> shift
) & mask
;
425 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
426 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
429 static void memory_region_write_accessor(MemoryRegion
*mr
,
438 if (mr
->flush_coalesced_mmio
) {
439 qemu_flush_coalesced_mmio_buffer();
441 tmp
= (*value
>> shift
) & mask
;
442 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
443 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
446 static void access_with_adjusted_size(hwaddr addr
,
449 unsigned access_size_min
,
450 unsigned access_size_max
,
451 void (*access
)(MemoryRegion
*mr
,
459 uint64_t access_mask
;
460 unsigned access_size
;
463 if (!access_size_min
) {
466 if (!access_size_max
) {
470 /* FIXME: support unaligned access? */
471 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
472 access_mask
= -1ULL >> (64 - access_size
* 8);
473 if (memory_region_big_endian(mr
)) {
474 for (i
= 0; i
< size
; i
+= access_size
) {
475 access(mr
, addr
+ i
, value
, access_size
,
476 (size
- access_size
- i
) * 8, access_mask
);
479 for (i
= 0; i
< size
; i
+= access_size
) {
480 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
485 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
489 while (mr
->container
) {
492 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
493 if (mr
== as
->root
) {
500 /* Render a memory region into the global view. Ranges in @view obscure
503 static void render_memory_region(FlatView
*view
,
509 MemoryRegion
*subregion
;
511 hwaddr offset_in_region
;
521 int128_addto(&base
, int128_make64(mr
->addr
));
522 readonly
|= mr
->readonly
;
524 tmp
= addrrange_make(base
, mr
->size
);
526 if (!addrrange_intersects(tmp
, clip
)) {
530 clip
= addrrange_intersection(tmp
, clip
);
533 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
534 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
535 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
539 /* Render subregions in priority order. */
540 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
541 render_memory_region(view
, subregion
, base
, clip
, readonly
);
544 if (!mr
->terminates
) {
548 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
553 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
554 fr
.romd_mode
= mr
->romd_mode
;
555 fr
.readonly
= readonly
;
557 /* Render the region itself into any gaps left by the current view. */
558 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
559 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
562 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
563 now
= int128_min(remain
,
564 int128_sub(view
->ranges
[i
].addr
.start
, base
));
565 fr
.offset_in_region
= offset_in_region
;
566 fr
.addr
= addrrange_make(base
, now
);
567 flatview_insert(view
, i
, &fr
);
569 int128_addto(&base
, now
);
570 offset_in_region
+= int128_get64(now
);
571 int128_subfrom(&remain
, now
);
573 now
= int128_sub(int128_min(int128_add(base
, remain
),
574 addrrange_end(view
->ranges
[i
].addr
)),
576 int128_addto(&base
, now
);
577 offset_in_region
+= int128_get64(now
);
578 int128_subfrom(&remain
, now
);
580 if (int128_nz(remain
)) {
581 fr
.offset_in_region
= offset_in_region
;
582 fr
.addr
= addrrange_make(base
, remain
);
583 flatview_insert(view
, i
, &fr
);
587 /* Render a memory topology into a list of disjoint absolute ranges. */
588 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
592 view
= g_new(FlatView
, 1);
596 render_memory_region(view
, mr
, int128_zero(),
597 addrrange_make(int128_zero(), int128_2_64()), false);
599 flatview_simplify(view
);
604 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
605 MemoryRegionIoeventfd
*fds_new
,
607 MemoryRegionIoeventfd
*fds_old
,
611 MemoryRegionIoeventfd
*fd
;
612 MemoryRegionSection section
;
614 /* Generate a symmetric difference of the old and new fd sets, adding
615 * and deleting as necessary.
619 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
620 if (iold
< fds_old_nb
621 && (inew
== fds_new_nb
622 || memory_region_ioeventfd_before(fds_old
[iold
],
625 section
= (MemoryRegionSection
) {
627 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
628 .size
= fd
->addr
.size
,
630 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
631 fd
->match_data
, fd
->data
, fd
->e
);
633 } else if (inew
< fds_new_nb
634 && (iold
== fds_old_nb
635 || memory_region_ioeventfd_before(fds_new
[inew
],
638 section
= (MemoryRegionSection
) {
640 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
641 .size
= fd
->addr
.size
,
643 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
644 fd
->match_data
, fd
->data
, fd
->e
);
653 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
657 qemu_mutex_lock(&flat_view_mutex
);
658 view
= as
->current_map
;
660 qemu_mutex_unlock(&flat_view_mutex
);
664 static void address_space_update_ioeventfds(AddressSpace
*as
)
668 unsigned ioeventfd_nb
= 0;
669 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
673 view
= address_space_get_flatview(as
);
674 FOR_EACH_FLAT_RANGE(fr
, view
) {
675 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
676 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
677 int128_sub(fr
->addr
.start
,
678 int128_make64(fr
->offset_in_region
)));
679 if (addrrange_intersects(fr
->addr
, tmp
)) {
681 ioeventfds
= g_realloc(ioeventfds
,
682 ioeventfd_nb
* sizeof(*ioeventfds
));
683 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
684 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
689 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
690 as
->ioeventfds
, as
->ioeventfd_nb
);
692 g_free(as
->ioeventfds
);
693 as
->ioeventfds
= ioeventfds
;
694 as
->ioeventfd_nb
= ioeventfd_nb
;
695 flatview_unref(view
);
698 static void address_space_update_topology_pass(AddressSpace
*as
,
699 const FlatView
*old_view
,
700 const FlatView
*new_view
,
704 FlatRange
*frold
, *frnew
;
706 /* Generate a symmetric difference of the old and new memory maps.
707 * Kill ranges in the old map, and instantiate ranges in the new map.
710 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
711 if (iold
< old_view
->nr
) {
712 frold
= &old_view
->ranges
[iold
];
716 if (inew
< new_view
->nr
) {
717 frnew
= &new_view
->ranges
[inew
];
724 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
725 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
726 && !flatrange_equal(frold
, frnew
)))) {
727 /* In old but not in new, or in both but attributes changed. */
730 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
734 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
735 /* In both and unchanged (except logging may have changed) */
738 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
739 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
740 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
741 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
742 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
752 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
761 static void address_space_update_topology(AddressSpace
*as
)
763 FlatView
*old_view
= address_space_get_flatview(as
);
764 FlatView
*new_view
= generate_memory_topology(as
->root
);
766 address_space_update_topology_pass(as
, old_view
, new_view
, false);
767 address_space_update_topology_pass(as
, old_view
, new_view
, true);
769 qemu_mutex_lock(&flat_view_mutex
);
770 flatview_unref(as
->current_map
);
771 as
->current_map
= new_view
;
772 qemu_mutex_unlock(&flat_view_mutex
);
774 /* Note that all the old MemoryRegions are still alive up to this
775 * point. This relieves most MemoryListeners from the need to
776 * ref/unref the MemoryRegions they get---unless they use them
777 * outside the iothread mutex, in which case precise reference
778 * counting is necessary.
780 flatview_unref(old_view
);
782 address_space_update_ioeventfds(as
);
785 void memory_region_transaction_begin(void)
787 qemu_flush_coalesced_mmio_buffer();
788 ++memory_region_transaction_depth
;
791 static void memory_region_clear_pending(void)
793 memory_region_update_pending
= false;
794 ioeventfd_update_pending
= false;
797 void memory_region_transaction_commit(void)
801 assert(memory_region_transaction_depth
);
802 --memory_region_transaction_depth
;
803 if (!memory_region_transaction_depth
) {
804 if (memory_region_update_pending
) {
805 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
807 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
808 address_space_update_topology(as
);
811 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
812 } else if (ioeventfd_update_pending
) {
813 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
814 address_space_update_ioeventfds(as
);
817 memory_region_clear_pending();
821 static void memory_region_destructor_none(MemoryRegion
*mr
)
825 static void memory_region_destructor_ram(MemoryRegion
*mr
)
827 qemu_ram_free(mr
->ram_addr
);
830 static void memory_region_destructor_alias(MemoryRegion
*mr
)
832 memory_region_unref(mr
->alias
);
835 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
837 qemu_ram_free_from_ptr(mr
->ram_addr
);
840 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
842 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
845 void memory_region_init(MemoryRegion
*mr
,
850 mr
->ops
= &unassigned_mem_ops
;
853 mr
->iommu_ops
= NULL
;
854 mr
->container
= NULL
;
855 mr
->size
= int128_make64(size
);
856 if (size
== UINT64_MAX
) {
857 mr
->size
= int128_2_64();
862 mr
->terminates
= false;
864 mr
->romd_mode
= true;
865 mr
->readonly
= false;
866 mr
->rom_device
= false;
867 mr
->destructor
= memory_region_destructor_none
;
869 mr
->may_overlap
= false;
871 QTAILQ_INIT(&mr
->subregions
);
872 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
873 QTAILQ_INIT(&mr
->coalesced
);
874 mr
->name
= g_strdup(name
);
875 mr
->dirty_log_mask
= 0;
876 mr
->ioeventfd_nb
= 0;
877 mr
->ioeventfds
= NULL
;
878 mr
->flush_coalesced_mmio
= false;
881 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
884 #ifdef DEBUG_UNASSIGNED
885 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
887 if (current_cpu
!= NULL
) {
888 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
893 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
894 uint64_t val
, unsigned size
)
896 #ifdef DEBUG_UNASSIGNED
897 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
899 if (current_cpu
!= NULL
) {
900 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
904 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
905 unsigned size
, bool is_write
)
910 const MemoryRegionOps unassigned_mem_ops
= {
911 .valid
.accepts
= unassigned_mem_accepts
,
912 .endianness
= DEVICE_NATIVE_ENDIAN
,
915 bool memory_region_access_valid(MemoryRegion
*mr
,
920 int access_size_min
, access_size_max
;
923 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
927 if (!mr
->ops
->valid
.accepts
) {
931 access_size_min
= mr
->ops
->valid
.min_access_size
;
932 if (!mr
->ops
->valid
.min_access_size
) {
936 access_size_max
= mr
->ops
->valid
.max_access_size
;
937 if (!mr
->ops
->valid
.max_access_size
) {
941 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
942 for (i
= 0; i
< size
; i
+= access_size
) {
943 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
952 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
959 access_with_adjusted_size(addr
, &data
, size
,
960 mr
->ops
->impl
.min_access_size
,
961 mr
->ops
->impl
.max_access_size
,
962 memory_region_read_accessor
, mr
);
964 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
965 memory_region_oldmmio_read_accessor
, mr
);
971 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
976 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
977 *pval
= unassigned_mem_read(mr
, addr
, size
);
981 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
982 adjust_endianness(mr
, pval
, size
);
986 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
991 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
992 unassigned_mem_write(mr
, addr
, data
, size
);
996 adjust_endianness(mr
, &data
, size
);
998 if (mr
->ops
->write
) {
999 access_with_adjusted_size(addr
, &data
, size
,
1000 mr
->ops
->impl
.min_access_size
,
1001 mr
->ops
->impl
.max_access_size
,
1002 memory_region_write_accessor
, mr
);
1004 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1005 memory_region_oldmmio_write_accessor
, mr
);
1010 void memory_region_init_io(MemoryRegion
*mr
,
1012 const MemoryRegionOps
*ops
,
1017 memory_region_init(mr
, owner
, name
, size
);
1019 mr
->opaque
= opaque
;
1020 mr
->terminates
= true;
1021 mr
->ram_addr
= ~(ram_addr_t
)0;
1024 void memory_region_init_ram(MemoryRegion
*mr
,
1029 memory_region_init(mr
, owner
, name
, size
);
1031 mr
->terminates
= true;
1032 mr
->destructor
= memory_region_destructor_ram
;
1033 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1037 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1038 struct Object
*owner
,
1044 memory_region_init(mr
, owner
, name
, size
);
1046 mr
->terminates
= true;
1047 mr
->destructor
= memory_region_destructor_ram
;
1048 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, path
, errp
);
1052 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1058 memory_region_init(mr
, owner
, name
, size
);
1060 mr
->terminates
= true;
1061 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1062 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1065 void memory_region_init_alias(MemoryRegion
*mr
,
1072 memory_region_init(mr
, owner
, name
, size
);
1073 memory_region_ref(orig
);
1074 mr
->destructor
= memory_region_destructor_alias
;
1076 mr
->alias_offset
= offset
;
1079 void memory_region_init_rom_device(MemoryRegion
*mr
,
1081 const MemoryRegionOps
*ops
,
1086 memory_region_init(mr
, owner
, name
, size
);
1088 mr
->opaque
= opaque
;
1089 mr
->terminates
= true;
1090 mr
->rom_device
= true;
1091 mr
->destructor
= memory_region_destructor_rom_device
;
1092 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1095 void memory_region_init_iommu(MemoryRegion
*mr
,
1097 const MemoryRegionIOMMUOps
*ops
,
1101 memory_region_init(mr
, owner
, name
, size
);
1102 mr
->iommu_ops
= ops
,
1103 mr
->terminates
= true; /* then re-forwards */
1104 notifier_list_init(&mr
->iommu_notify
);
1107 void memory_region_init_reservation(MemoryRegion
*mr
,
1112 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1115 void memory_region_destroy(MemoryRegion
*mr
)
1117 assert(QTAILQ_EMPTY(&mr
->subregions
));
1118 assert(memory_region_transaction_depth
== 0);
1120 memory_region_clear_coalescing(mr
);
1121 g_free((char *)mr
->name
);
1122 g_free(mr
->ioeventfds
);
1125 Object
*memory_region_owner(MemoryRegion
*mr
)
1130 void memory_region_ref(MemoryRegion
*mr
)
1132 if (mr
&& mr
->owner
) {
1133 object_ref(mr
->owner
);
1137 void memory_region_unref(MemoryRegion
*mr
)
1139 if (mr
&& mr
->owner
) {
1140 object_unref(mr
->owner
);
1144 uint64_t memory_region_size(MemoryRegion
*mr
)
1146 if (int128_eq(mr
->size
, int128_2_64())) {
1149 return int128_get64(mr
->size
);
1152 const char *memory_region_name(MemoryRegion
*mr
)
1157 bool memory_region_is_ram(MemoryRegion
*mr
)
1162 bool memory_region_is_logging(MemoryRegion
*mr
)
1164 return mr
->dirty_log_mask
;
1167 bool memory_region_is_rom(MemoryRegion
*mr
)
1169 return mr
->ram
&& mr
->readonly
;
1172 bool memory_region_is_iommu(MemoryRegion
*mr
)
1174 return mr
->iommu_ops
;
1177 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1179 notifier_list_add(&mr
->iommu_notify
, n
);
1182 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1187 void memory_region_notify_iommu(MemoryRegion
*mr
,
1188 IOMMUTLBEntry entry
)
1190 assert(memory_region_is_iommu(mr
));
1191 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1194 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1196 uint8_t mask
= 1 << client
;
1198 memory_region_transaction_begin();
1199 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1200 memory_region_update_pending
|= mr
->enabled
;
1201 memory_region_transaction_commit();
1204 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1205 hwaddr size
, unsigned client
)
1207 assert(mr
->terminates
);
1208 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1211 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1214 assert(mr
->terminates
);
1215 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
);
1218 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1219 hwaddr size
, unsigned client
)
1222 assert(mr
->terminates
);
1223 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1225 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1231 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1236 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1237 FlatView
*view
= address_space_get_flatview(as
);
1238 FOR_EACH_FLAT_RANGE(fr
, view
) {
1240 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1243 flatview_unref(view
);
1247 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1249 if (mr
->readonly
!= readonly
) {
1250 memory_region_transaction_begin();
1251 mr
->readonly
= readonly
;
1252 memory_region_update_pending
|= mr
->enabled
;
1253 memory_region_transaction_commit();
1257 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1259 if (mr
->romd_mode
!= romd_mode
) {
1260 memory_region_transaction_begin();
1261 mr
->romd_mode
= romd_mode
;
1262 memory_region_update_pending
|= mr
->enabled
;
1263 memory_region_transaction_commit();
1267 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1268 hwaddr size
, unsigned client
)
1270 assert(mr
->terminates
);
1271 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1274 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1277 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1280 assert(mr
->terminates
);
1282 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1285 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1289 CoalescedMemoryRange
*cmr
;
1291 MemoryRegionSection section
;
1293 view
= address_space_get_flatview(as
);
1294 FOR_EACH_FLAT_RANGE(fr
, view
) {
1296 section
= (MemoryRegionSection
) {
1297 .address_space
= as
,
1298 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1299 .size
= fr
->addr
.size
,
1302 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1303 int128_get64(fr
->addr
.start
),
1304 int128_get64(fr
->addr
.size
));
1305 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1306 tmp
= addrrange_shift(cmr
->addr
,
1307 int128_sub(fr
->addr
.start
,
1308 int128_make64(fr
->offset_in_region
)));
1309 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1312 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1313 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1314 int128_get64(tmp
.start
),
1315 int128_get64(tmp
.size
));
1319 flatview_unref(view
);
1322 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1326 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1327 memory_region_update_coalesced_range_as(mr
, as
);
1331 void memory_region_set_coalescing(MemoryRegion
*mr
)
1333 memory_region_clear_coalescing(mr
);
1334 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1337 void memory_region_add_coalescing(MemoryRegion
*mr
,
1341 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1343 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1344 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1345 memory_region_update_coalesced_range(mr
);
1346 memory_region_set_flush_coalesced(mr
);
1349 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1351 CoalescedMemoryRange
*cmr
;
1352 bool updated
= false;
1354 qemu_flush_coalesced_mmio_buffer();
1355 mr
->flush_coalesced_mmio
= false;
1357 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1358 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1359 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1365 memory_region_update_coalesced_range(mr
);
1369 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1371 mr
->flush_coalesced_mmio
= true;
1374 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1376 qemu_flush_coalesced_mmio_buffer();
1377 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1378 mr
->flush_coalesced_mmio
= false;
1382 void memory_region_add_eventfd(MemoryRegion
*mr
,
1389 MemoryRegionIoeventfd mrfd
= {
1390 .addr
.start
= int128_make64(addr
),
1391 .addr
.size
= int128_make64(size
),
1392 .match_data
= match_data
,
1398 adjust_endianness(mr
, &mrfd
.data
, size
);
1399 memory_region_transaction_begin();
1400 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1401 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1406 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1407 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1408 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1409 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1410 mr
->ioeventfds
[i
] = mrfd
;
1411 ioeventfd_update_pending
|= mr
->enabled
;
1412 memory_region_transaction_commit();
1415 void memory_region_del_eventfd(MemoryRegion
*mr
,
1422 MemoryRegionIoeventfd mrfd
= {
1423 .addr
.start
= int128_make64(addr
),
1424 .addr
.size
= int128_make64(size
),
1425 .match_data
= match_data
,
1431 adjust_endianness(mr
, &mrfd
.data
, size
);
1432 memory_region_transaction_begin();
1433 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1434 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1438 assert(i
!= mr
->ioeventfd_nb
);
1439 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1440 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1442 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1443 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1444 ioeventfd_update_pending
|= mr
->enabled
;
1445 memory_region_transaction_commit();
1448 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1450 hwaddr offset
= subregion
->addr
;
1451 MemoryRegion
*mr
= subregion
->container
;
1452 MemoryRegion
*other
;
1454 memory_region_transaction_begin();
1456 memory_region_ref(subregion
);
1457 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1458 if (subregion
->may_overlap
|| other
->may_overlap
) {
1461 if (int128_ge(int128_make64(offset
),
1462 int128_add(int128_make64(other
->addr
), other
->size
))
1463 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1464 int128_make64(other
->addr
))) {
1468 printf("warning: subregion collision %llx/%llx (%s) "
1469 "vs %llx/%llx (%s)\n",
1470 (unsigned long long)offset
,
1471 (unsigned long long)int128_get64(subregion
->size
),
1473 (unsigned long long)other
->addr
,
1474 (unsigned long long)int128_get64(other
->size
),
1478 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1479 if (subregion
->priority
>= other
->priority
) {
1480 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1484 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1486 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1487 memory_region_transaction_commit();
1490 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1492 MemoryRegion
*subregion
)
1494 assert(!subregion
->container
);
1495 subregion
->container
= mr
;
1496 subregion
->addr
= offset
;
1497 memory_region_update_container_subregions(subregion
);
1500 void memory_region_add_subregion(MemoryRegion
*mr
,
1502 MemoryRegion
*subregion
)
1504 subregion
->may_overlap
= false;
1505 subregion
->priority
= 0;
1506 memory_region_add_subregion_common(mr
, offset
, subregion
);
1509 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1511 MemoryRegion
*subregion
,
1514 subregion
->may_overlap
= true;
1515 subregion
->priority
= priority
;
1516 memory_region_add_subregion_common(mr
, offset
, subregion
);
1519 void memory_region_del_subregion(MemoryRegion
*mr
,
1520 MemoryRegion
*subregion
)
1522 memory_region_transaction_begin();
1523 assert(subregion
->container
== mr
);
1524 subregion
->container
= NULL
;
1525 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1526 memory_region_unref(subregion
);
1527 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1528 memory_region_transaction_commit();
1531 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1533 if (enabled
== mr
->enabled
) {
1536 memory_region_transaction_begin();
1537 mr
->enabled
= enabled
;
1538 memory_region_update_pending
= true;
1539 memory_region_transaction_commit();
1542 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1544 MemoryRegion
*container
= mr
->container
;
1547 memory_region_transaction_begin();
1548 memory_region_ref(mr
);
1549 memory_region_del_subregion(container
, mr
);
1550 mr
->container
= container
;
1551 memory_region_update_container_subregions(mr
);
1552 memory_region_unref(mr
);
1553 memory_region_transaction_commit();
1557 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1559 if (addr
!= mr
->addr
) {
1561 memory_region_readd_subregion(mr
);
1565 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1569 if (offset
== mr
->alias_offset
) {
1573 memory_region_transaction_begin();
1574 mr
->alias_offset
= offset
;
1575 memory_region_update_pending
|= mr
->enabled
;
1576 memory_region_transaction_commit();
1579 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1581 return mr
->ram_addr
;
1584 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1586 const AddrRange
*addr
= addr_
;
1587 const FlatRange
*fr
= fr_
;
1589 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1591 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1597 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1599 return bsearch(&addr
, view
->ranges
, view
->nr
,
1600 sizeof(FlatRange
), cmp_flatrange_addr
);
1603 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
1605 MemoryRegion
*mr
= memory_region_find(container
, addr
, 1).mr
;
1606 if (!mr
|| (mr
== container
)) {
1609 memory_region_unref(mr
);
1613 bool memory_region_is_mapped(MemoryRegion
*mr
)
1615 return mr
->container
? true : false;
1618 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1619 hwaddr addr
, uint64_t size
)
1621 MemoryRegionSection ret
= { .mr
= NULL
};
1629 for (root
= mr
; root
->container
; ) {
1630 root
= root
->container
;
1634 as
= memory_region_to_address_space(root
);
1638 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1640 view
= address_space_get_flatview(as
);
1641 fr
= flatview_lookup(view
, range
);
1643 flatview_unref(view
);
1647 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1652 ret
.address_space
= as
;
1653 range
= addrrange_intersection(range
, fr
->addr
);
1654 ret
.offset_within_region
= fr
->offset_in_region
;
1655 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1657 ret
.size
= range
.size
;
1658 ret
.offset_within_address_space
= int128_get64(range
.start
);
1659 ret
.readonly
= fr
->readonly
;
1660 memory_region_ref(ret
.mr
);
1662 flatview_unref(view
);
1666 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1671 view
= address_space_get_flatview(as
);
1672 FOR_EACH_FLAT_RANGE(fr
, view
) {
1673 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1675 flatview_unref(view
);
1678 void memory_global_dirty_log_start(void)
1680 global_dirty_log
= true;
1681 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1684 void memory_global_dirty_log_stop(void)
1686 global_dirty_log
= false;
1687 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1690 static void listener_add_address_space(MemoryListener
*listener
,
1696 if (listener
->address_space_filter
1697 && listener
->address_space_filter
!= as
) {
1701 if (global_dirty_log
) {
1702 if (listener
->log_global_start
) {
1703 listener
->log_global_start(listener
);
1707 view
= address_space_get_flatview(as
);
1708 FOR_EACH_FLAT_RANGE(fr
, view
) {
1709 MemoryRegionSection section
= {
1711 .address_space
= as
,
1712 .offset_within_region
= fr
->offset_in_region
,
1713 .size
= fr
->addr
.size
,
1714 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1715 .readonly
= fr
->readonly
,
1717 if (listener
->region_add
) {
1718 listener
->region_add(listener
, §ion
);
1721 flatview_unref(view
);
1724 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1726 MemoryListener
*other
= NULL
;
1729 listener
->address_space_filter
= filter
;
1730 if (QTAILQ_EMPTY(&memory_listeners
)
1731 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1732 memory_listeners
)->priority
) {
1733 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1735 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1736 if (listener
->priority
< other
->priority
) {
1740 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1743 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1744 listener_add_address_space(listener
, as
);
1748 void memory_listener_unregister(MemoryListener
*listener
)
1750 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1753 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1755 if (QTAILQ_EMPTY(&address_spaces
)) {
1759 memory_region_transaction_begin();
1761 as
->current_map
= g_new(FlatView
, 1);
1762 flatview_init(as
->current_map
);
1763 as
->ioeventfd_nb
= 0;
1764 as
->ioeventfds
= NULL
;
1765 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1766 as
->name
= g_strdup(name
? name
: "anonymous");
1767 address_space_init_dispatch(as
);
1768 memory_region_update_pending
|= root
->enabled
;
1769 memory_region_transaction_commit();
1772 void address_space_destroy(AddressSpace
*as
)
1774 MemoryListener
*listener
;
1776 /* Flush out anything from MemoryListeners listening in on this */
1777 memory_region_transaction_begin();
1779 memory_region_transaction_commit();
1780 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1781 address_space_destroy_dispatch(as
);
1783 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1784 assert(listener
->address_space_filter
!= as
);
1787 flatview_unref(as
->current_map
);
1789 g_free(as
->ioeventfds
);
1792 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1794 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1797 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1798 uint64_t val
, unsigned size
)
1800 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1803 typedef struct MemoryRegionList MemoryRegionList
;
1805 struct MemoryRegionList
{
1806 const MemoryRegion
*mr
;
1808 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1811 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1813 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1814 const MemoryRegion
*mr
, unsigned int level
,
1816 MemoryRegionListHead
*alias_print_queue
)
1818 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1819 MemoryRegionListHead submr_print_queue
;
1820 const MemoryRegion
*submr
;
1823 if (!mr
|| !mr
->enabled
) {
1827 for (i
= 0; i
< level
; i
++) {
1832 MemoryRegionList
*ml
;
1835 /* check if the alias is already in the queue */
1836 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1837 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1843 ml
= g_new(MemoryRegionList
, 1);
1845 ml
->printed
= false;
1846 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1848 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1849 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1850 "-" TARGET_FMT_plx
"\n",
1853 + (int128_nz(mr
->size
) ?
1854 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1855 int128_one())) : 0),
1857 mr
->romd_mode
? 'R' : '-',
1858 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1864 + (int128_nz(mr
->size
) ?
1865 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1866 int128_one())) : 0));
1869 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1872 + (int128_nz(mr
->size
) ?
1873 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1874 int128_one())) : 0),
1876 mr
->romd_mode
? 'R' : '-',
1877 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
1882 QTAILQ_INIT(&submr_print_queue
);
1884 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1885 new_ml
= g_new(MemoryRegionList
, 1);
1887 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1888 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1889 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1890 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1891 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1897 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1901 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1902 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1906 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1911 void mtree_info(fprintf_function mon_printf
, void *f
)
1913 MemoryRegionListHead ml_head
;
1914 MemoryRegionList
*ml
, *ml2
;
1917 QTAILQ_INIT(&ml_head
);
1919 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1920 mon_printf(f
, "%s\n", as
->name
);
1921 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1924 mon_printf(f
, "aliases\n");
1925 /* print aliased regions */
1926 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1928 mon_printf(f
, "%s\n", ml
->mr
->name
);
1929 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1933 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {