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 "qapi/visitor.h"
20 #include "qemu/bitops.h"
21 #include "qom/object.h"
25 #include "exec/memory-internal.h"
26 #include "exec/ram_addr.h"
27 #include "sysemu/sysemu.h"
29 //#define DEBUG_UNASSIGNED
31 static unsigned memory_region_transaction_depth
;
32 static bool memory_region_update_pending
;
33 static bool ioeventfd_update_pending
;
34 static bool global_dirty_log
= false;
36 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
37 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
39 static QTAILQ_HEAD(, AddressSpace
) address_spaces
40 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
42 typedef struct AddrRange AddrRange
;
45 * Note that signed integers are needed for negative offsetting in aliases
46 * (large MemoryRegion::alias_offset).
53 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
55 return (AddrRange
) { start
, size
};
58 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
60 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
63 static Int128
addrrange_end(AddrRange r
)
65 return int128_add(r
.start
, r
.size
);
68 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
70 int128_addto(&range
.start
, delta
);
74 static bool addrrange_contains(AddrRange range
, Int128 addr
)
76 return int128_ge(addr
, range
.start
)
77 && int128_lt(addr
, addrrange_end(range
));
80 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
82 return addrrange_contains(r1
, r2
.start
)
83 || addrrange_contains(r2
, r1
.start
);
86 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
88 Int128 start
= int128_max(r1
.start
, r2
.start
);
89 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
90 return addrrange_make(start
, int128_sub(end
, start
));
93 enum ListenerDirection
{ Forward
, Reverse
};
95 static bool memory_listener_match(MemoryListener
*listener
,
96 MemoryRegionSection
*section
)
98 return !listener
->address_space_filter
99 || listener
->address_space_filter
== section
->address_space
;
102 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
104 MemoryListener *_listener; \
106 switch (_direction) { \
108 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
109 if (_listener->_callback) { \
110 _listener->_callback(_listener, ##_args); \
115 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
116 memory_listeners, link) { \
117 if (_listener->_callback) { \
118 _listener->_callback(_listener, ##_args); \
127 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
129 MemoryListener *_listener; \
131 switch (_direction) { \
133 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
134 if (_listener->_callback \
135 && memory_listener_match(_listener, _section)) { \
136 _listener->_callback(_listener, _section, ##_args); \
141 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
142 memory_listeners, link) { \
143 if (_listener->_callback \
144 && memory_listener_match(_listener, _section)) { \
145 _listener->_callback(_listener, _section, ##_args); \
154 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
155 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
156 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
158 .address_space = (as), \
159 .offset_within_region = (fr)->offset_in_region, \
160 .size = (fr)->addr.size, \
161 .offset_within_address_space = int128_get64((fr)->addr.start), \
162 .readonly = (fr)->readonly, \
165 struct CoalescedMemoryRange
{
167 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
170 struct MemoryRegionIoeventfd
{
177 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
178 MemoryRegionIoeventfd b
)
180 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
182 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
184 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
186 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
188 } else if (a
.match_data
< b
.match_data
) {
190 } else if (a
.match_data
> b
.match_data
) {
192 } else if (a
.match_data
) {
193 if (a
.data
< b
.data
) {
195 } else if (a
.data
> b
.data
) {
201 } else if (a
.e
> b
.e
) {
207 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
208 MemoryRegionIoeventfd b
)
210 return !memory_region_ioeventfd_before(a
, b
)
211 && !memory_region_ioeventfd_before(b
, a
);
214 typedef struct FlatRange FlatRange
;
215 typedef struct FlatView FlatView
;
217 /* Range of memory in the global map. Addresses are absolute. */
220 hwaddr offset_in_region
;
222 uint8_t dirty_log_mask
;
227 /* Flattened global view of current active memory hierarchy. Kept in sorted
235 unsigned nr_allocated
;
238 typedef struct AddressSpaceOps AddressSpaceOps
;
240 #define FOR_EACH_FLAT_RANGE(var, view) \
241 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
243 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
245 return a
->mr
== b
->mr
246 && addrrange_equal(a
->addr
, b
->addr
)
247 && a
->offset_in_region
== b
->offset_in_region
248 && a
->romd_mode
== b
->romd_mode
249 && a
->readonly
== b
->readonly
;
252 static void flatview_init(FlatView
*view
)
257 view
->nr_allocated
= 0;
260 /* Insert a range into a given position. Caller is responsible for maintaining
263 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
265 if (view
->nr
== view
->nr_allocated
) {
266 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
267 view
->ranges
= g_realloc(view
->ranges
,
268 view
->nr_allocated
* sizeof(*view
->ranges
));
270 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
271 (view
->nr
- pos
) * sizeof(FlatRange
));
272 view
->ranges
[pos
] = *range
;
273 memory_region_ref(range
->mr
);
277 static void flatview_destroy(FlatView
*view
)
281 for (i
= 0; i
< view
->nr
; i
++) {
282 memory_region_unref(view
->ranges
[i
].mr
);
284 g_free(view
->ranges
);
288 static void flatview_ref(FlatView
*view
)
290 atomic_inc(&view
->ref
);
293 static void flatview_unref(FlatView
*view
)
295 if (atomic_fetch_dec(&view
->ref
) == 1) {
296 flatview_destroy(view
);
300 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
302 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
304 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
306 int128_make64(r2
->offset_in_region
))
307 && r1
->dirty_log_mask
== r2
->dirty_log_mask
308 && r1
->romd_mode
== r2
->romd_mode
309 && r1
->readonly
== r2
->readonly
;
312 /* Attempt to simplify a view by merging adjacent ranges */
313 static void flatview_simplify(FlatView
*view
)
318 while (i
< view
->nr
) {
321 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
322 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
326 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
327 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
332 static bool memory_region_big_endian(MemoryRegion
*mr
)
334 #ifdef TARGET_WORDS_BIGENDIAN
335 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
337 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
341 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
343 #ifdef TARGET_WORDS_BIGENDIAN
344 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
346 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
350 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
352 if (memory_region_wrong_endianness(mr
)) {
357 *data
= bswap16(*data
);
360 *data
= bswap32(*data
);
363 *data
= bswap64(*data
);
371 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
381 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
382 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
383 *value
|= (tmp
& mask
) << shift
;
387 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
397 if (mr
->flush_coalesced_mmio
) {
398 qemu_flush_coalesced_mmio_buffer();
400 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
401 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
402 *value
|= (tmp
& mask
) << shift
;
406 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
417 if (mr
->flush_coalesced_mmio
) {
418 qemu_flush_coalesced_mmio_buffer();
420 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
421 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
422 *value
|= (tmp
& mask
) << shift
;
426 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
436 tmp
= (*value
>> shift
) & mask
;
437 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
438 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
442 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
452 if (mr
->flush_coalesced_mmio
) {
453 qemu_flush_coalesced_mmio_buffer();
455 tmp
= (*value
>> shift
) & mask
;
456 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
457 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
461 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
471 if (mr
->flush_coalesced_mmio
) {
472 qemu_flush_coalesced_mmio_buffer();
474 tmp
= (*value
>> shift
) & mask
;
475 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
476 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
479 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
482 unsigned access_size_min
,
483 unsigned access_size_max
,
484 MemTxResult (*access
)(MemoryRegion
*mr
,
494 uint64_t access_mask
;
495 unsigned access_size
;
497 MemTxResult r
= MEMTX_OK
;
499 if (!access_size_min
) {
502 if (!access_size_max
) {
506 /* FIXME: support unaligned access? */
507 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
508 access_mask
= -1ULL >> (64 - access_size
* 8);
509 if (memory_region_big_endian(mr
)) {
510 for (i
= 0; i
< size
; i
+= access_size
) {
511 r
|= access(mr
, addr
+ i
, value
, access_size
,
512 (size
- access_size
- i
) * 8, access_mask
, attrs
);
515 for (i
= 0; i
< size
; i
+= access_size
) {
516 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
523 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
527 while (mr
->container
) {
530 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
531 if (mr
== as
->root
) {
538 /* Render a memory region into the global view. Ranges in @view obscure
541 static void render_memory_region(FlatView
*view
,
547 MemoryRegion
*subregion
;
549 hwaddr offset_in_region
;
559 int128_addto(&base
, int128_make64(mr
->addr
));
560 readonly
|= mr
->readonly
;
562 tmp
= addrrange_make(base
, mr
->size
);
564 if (!addrrange_intersects(tmp
, clip
)) {
568 clip
= addrrange_intersection(tmp
, clip
);
571 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
572 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
573 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
577 /* Render subregions in priority order. */
578 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
579 render_memory_region(view
, subregion
, base
, clip
, readonly
);
582 if (!mr
->terminates
) {
586 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
591 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
592 fr
.romd_mode
= mr
->romd_mode
;
593 fr
.readonly
= readonly
;
595 /* Render the region itself into any gaps left by the current view. */
596 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
597 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
600 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
601 now
= int128_min(remain
,
602 int128_sub(view
->ranges
[i
].addr
.start
, base
));
603 fr
.offset_in_region
= offset_in_region
;
604 fr
.addr
= addrrange_make(base
, now
);
605 flatview_insert(view
, i
, &fr
);
607 int128_addto(&base
, now
);
608 offset_in_region
+= int128_get64(now
);
609 int128_subfrom(&remain
, now
);
611 now
= int128_sub(int128_min(int128_add(base
, remain
),
612 addrrange_end(view
->ranges
[i
].addr
)),
614 int128_addto(&base
, now
);
615 offset_in_region
+= int128_get64(now
);
616 int128_subfrom(&remain
, now
);
618 if (int128_nz(remain
)) {
619 fr
.offset_in_region
= offset_in_region
;
620 fr
.addr
= addrrange_make(base
, remain
);
621 flatview_insert(view
, i
, &fr
);
625 /* Render a memory topology into a list of disjoint absolute ranges. */
626 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
630 view
= g_new(FlatView
, 1);
634 render_memory_region(view
, mr
, int128_zero(),
635 addrrange_make(int128_zero(), int128_2_64()), false);
637 flatview_simplify(view
);
642 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
643 MemoryRegionIoeventfd
*fds_new
,
645 MemoryRegionIoeventfd
*fds_old
,
649 MemoryRegionIoeventfd
*fd
;
650 MemoryRegionSection section
;
652 /* Generate a symmetric difference of the old and new fd sets, adding
653 * and deleting as necessary.
657 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
658 if (iold
< fds_old_nb
659 && (inew
== fds_new_nb
660 || memory_region_ioeventfd_before(fds_old
[iold
],
663 section
= (MemoryRegionSection
) {
665 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
666 .size
= fd
->addr
.size
,
668 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
669 fd
->match_data
, fd
->data
, fd
->e
);
671 } else if (inew
< fds_new_nb
672 && (iold
== fds_old_nb
673 || memory_region_ioeventfd_before(fds_new
[inew
],
676 section
= (MemoryRegionSection
) {
678 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
679 .size
= fd
->addr
.size
,
681 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
682 fd
->match_data
, fd
->data
, fd
->e
);
691 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
696 view
= atomic_rcu_read(&as
->current_map
);
702 static void address_space_update_ioeventfds(AddressSpace
*as
)
706 unsigned ioeventfd_nb
= 0;
707 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
711 view
= address_space_get_flatview(as
);
712 FOR_EACH_FLAT_RANGE(fr
, view
) {
713 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
714 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
715 int128_sub(fr
->addr
.start
,
716 int128_make64(fr
->offset_in_region
)));
717 if (addrrange_intersects(fr
->addr
, tmp
)) {
719 ioeventfds
= g_realloc(ioeventfds
,
720 ioeventfd_nb
* sizeof(*ioeventfds
));
721 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
722 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
727 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
728 as
->ioeventfds
, as
->ioeventfd_nb
);
730 g_free(as
->ioeventfds
);
731 as
->ioeventfds
= ioeventfds
;
732 as
->ioeventfd_nb
= ioeventfd_nb
;
733 flatview_unref(view
);
736 static void address_space_update_topology_pass(AddressSpace
*as
,
737 const FlatView
*old_view
,
738 const FlatView
*new_view
,
742 FlatRange
*frold
, *frnew
;
744 /* Generate a symmetric difference of the old and new memory maps.
745 * Kill ranges in the old map, and instantiate ranges in the new map.
748 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
749 if (iold
< old_view
->nr
) {
750 frold
= &old_view
->ranges
[iold
];
754 if (inew
< new_view
->nr
) {
755 frnew
= &new_view
->ranges
[inew
];
762 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
763 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
764 && !flatrange_equal(frold
, frnew
)))) {
765 /* In old but not in new, or in both but attributes changed. */
768 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
772 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
773 /* In both and unchanged (except logging may have changed) */
776 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
777 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
778 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
779 frold
->dirty_log_mask
,
780 frnew
->dirty_log_mask
);
782 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
783 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
784 frold
->dirty_log_mask
,
785 frnew
->dirty_log_mask
);
795 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
804 static void address_space_update_topology(AddressSpace
*as
)
806 FlatView
*old_view
= address_space_get_flatview(as
);
807 FlatView
*new_view
= generate_memory_topology(as
->root
);
809 address_space_update_topology_pass(as
, old_view
, new_view
, false);
810 address_space_update_topology_pass(as
, old_view
, new_view
, true);
812 /* Writes are protected by the BQL. */
813 atomic_rcu_set(&as
->current_map
, new_view
);
814 call_rcu(old_view
, flatview_unref
, rcu
);
816 /* Note that all the old MemoryRegions are still alive up to this
817 * point. This relieves most MemoryListeners from the need to
818 * ref/unref the MemoryRegions they get---unless they use them
819 * outside the iothread mutex, in which case precise reference
820 * counting is necessary.
822 flatview_unref(old_view
);
824 address_space_update_ioeventfds(as
);
827 void memory_region_transaction_begin(void)
829 qemu_flush_coalesced_mmio_buffer();
830 ++memory_region_transaction_depth
;
833 static void memory_region_clear_pending(void)
835 memory_region_update_pending
= false;
836 ioeventfd_update_pending
= false;
839 void memory_region_transaction_commit(void)
843 assert(memory_region_transaction_depth
);
844 --memory_region_transaction_depth
;
845 if (!memory_region_transaction_depth
) {
846 if (memory_region_update_pending
) {
847 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
849 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
850 address_space_update_topology(as
);
853 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
854 } else if (ioeventfd_update_pending
) {
855 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
856 address_space_update_ioeventfds(as
);
859 memory_region_clear_pending();
863 static void memory_region_destructor_none(MemoryRegion
*mr
)
867 static void memory_region_destructor_ram(MemoryRegion
*mr
)
869 qemu_ram_free(mr
->ram_addr
);
872 static void memory_region_destructor_alias(MemoryRegion
*mr
)
874 memory_region_unref(mr
->alias
);
877 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
879 qemu_ram_free_from_ptr(mr
->ram_addr
);
882 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
884 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
887 static bool memory_region_need_escape(char c
)
889 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
892 static char *memory_region_escape_name(const char *name
)
899 for (p
= name
; *p
; p
++) {
900 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
902 if (bytes
== p
- name
) {
903 return g_memdup(name
, bytes
+ 1);
906 escaped
= g_malloc(bytes
+ 1);
907 for (p
= name
, q
= escaped
; *p
; p
++) {
909 if (unlikely(memory_region_need_escape(c
))) {
912 *q
++ = "0123456789abcdef"[c
>> 4];
913 c
= "0123456789abcdef"[c
& 15];
921 void memory_region_init(MemoryRegion
*mr
,
927 owner
= container_get(qdev_get_machine(), "/unattached");
930 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
931 mr
->size
= int128_make64(size
);
932 if (size
== UINT64_MAX
) {
933 mr
->size
= int128_2_64();
935 mr
->name
= g_strdup(name
);
938 char *escaped_name
= memory_region_escape_name(name
);
939 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
940 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
941 object_unref(OBJECT(mr
));
943 g_free(escaped_name
);
947 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, void *opaque
,
948 const char *name
, Error
**errp
)
950 MemoryRegion
*mr
= MEMORY_REGION(obj
);
951 uint64_t value
= mr
->addr
;
953 visit_type_uint64(v
, &value
, name
, errp
);
956 static void memory_region_get_container(Object
*obj
, Visitor
*v
, void *opaque
,
957 const char *name
, Error
**errp
)
959 MemoryRegion
*mr
= MEMORY_REGION(obj
);
960 gchar
*path
= (gchar
*)"";
963 path
= object_get_canonical_path(OBJECT(mr
->container
));
965 visit_type_str(v
, &path
, name
, errp
);
971 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
974 MemoryRegion
*mr
= MEMORY_REGION(obj
);
976 return OBJECT(mr
->container
);
979 static void memory_region_get_priority(Object
*obj
, Visitor
*v
, void *opaque
,
980 const char *name
, Error
**errp
)
982 MemoryRegion
*mr
= MEMORY_REGION(obj
);
983 int32_t value
= mr
->priority
;
985 visit_type_int32(v
, &value
, name
, errp
);
988 static bool memory_region_get_may_overlap(Object
*obj
, Error
**errp
)
990 MemoryRegion
*mr
= MEMORY_REGION(obj
);
992 return mr
->may_overlap
;
995 static void memory_region_get_size(Object
*obj
, Visitor
*v
, void *opaque
,
996 const char *name
, Error
**errp
)
998 MemoryRegion
*mr
= MEMORY_REGION(obj
);
999 uint64_t value
= memory_region_size(mr
);
1001 visit_type_uint64(v
, &value
, name
, errp
);
1004 static void memory_region_initfn(Object
*obj
)
1006 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1009 mr
->ops
= &unassigned_mem_ops
;
1011 mr
->romd_mode
= true;
1012 mr
->destructor
= memory_region_destructor_none
;
1013 QTAILQ_INIT(&mr
->subregions
);
1014 QTAILQ_INIT(&mr
->coalesced
);
1016 op
= object_property_add(OBJECT(mr
), "container",
1017 "link<" TYPE_MEMORY_REGION
">",
1018 memory_region_get_container
,
1019 NULL
, /* memory_region_set_container */
1020 NULL
, NULL
, &error_abort
);
1021 op
->resolve
= memory_region_resolve_container
;
1023 object_property_add(OBJECT(mr
), "addr", "uint64",
1024 memory_region_get_addr
,
1025 NULL
, /* memory_region_set_addr */
1026 NULL
, NULL
, &error_abort
);
1027 object_property_add(OBJECT(mr
), "priority", "uint32",
1028 memory_region_get_priority
,
1029 NULL
, /* memory_region_set_priority */
1030 NULL
, NULL
, &error_abort
);
1031 object_property_add_bool(OBJECT(mr
), "may-overlap",
1032 memory_region_get_may_overlap
,
1033 NULL
, /* memory_region_set_may_overlap */
1035 object_property_add(OBJECT(mr
), "size", "uint64",
1036 memory_region_get_size
,
1037 NULL
, /* memory_region_set_size, */
1038 NULL
, NULL
, &error_abort
);
1041 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1044 #ifdef DEBUG_UNASSIGNED
1045 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1047 if (current_cpu
!= NULL
) {
1048 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1053 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1054 uint64_t val
, unsigned size
)
1056 #ifdef DEBUG_UNASSIGNED
1057 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1059 if (current_cpu
!= NULL
) {
1060 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1064 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1065 unsigned size
, bool is_write
)
1070 const MemoryRegionOps unassigned_mem_ops
= {
1071 .valid
.accepts
= unassigned_mem_accepts
,
1072 .endianness
= DEVICE_NATIVE_ENDIAN
,
1075 bool memory_region_access_valid(MemoryRegion
*mr
,
1080 int access_size_min
, access_size_max
;
1083 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1087 if (!mr
->ops
->valid
.accepts
) {
1091 access_size_min
= mr
->ops
->valid
.min_access_size
;
1092 if (!mr
->ops
->valid
.min_access_size
) {
1093 access_size_min
= 1;
1096 access_size_max
= mr
->ops
->valid
.max_access_size
;
1097 if (!mr
->ops
->valid
.max_access_size
) {
1098 access_size_max
= 4;
1101 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1102 for (i
= 0; i
< size
; i
+= access_size
) {
1103 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1112 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1120 if (mr
->ops
->read
) {
1121 return access_with_adjusted_size(addr
, pval
, size
,
1122 mr
->ops
->impl
.min_access_size
,
1123 mr
->ops
->impl
.max_access_size
,
1124 memory_region_read_accessor
,
1126 } else if (mr
->ops
->read_with_attrs
) {
1127 return access_with_adjusted_size(addr
, pval
, size
,
1128 mr
->ops
->impl
.min_access_size
,
1129 mr
->ops
->impl
.max_access_size
,
1130 memory_region_read_with_attrs_accessor
,
1133 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1134 memory_region_oldmmio_read_accessor
,
1139 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1147 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1148 *pval
= unassigned_mem_read(mr
, addr
, size
);
1149 return MEMTX_DECODE_ERROR
;
1152 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1153 adjust_endianness(mr
, pval
, size
);
1157 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1163 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1164 unassigned_mem_write(mr
, addr
, data
, size
);
1165 return MEMTX_DECODE_ERROR
;
1168 adjust_endianness(mr
, &data
, size
);
1170 if (mr
->ops
->write
) {
1171 return access_with_adjusted_size(addr
, &data
, size
,
1172 mr
->ops
->impl
.min_access_size
,
1173 mr
->ops
->impl
.max_access_size
,
1174 memory_region_write_accessor
, mr
,
1176 } else if (mr
->ops
->write_with_attrs
) {
1178 access_with_adjusted_size(addr
, &data
, size
,
1179 mr
->ops
->impl
.min_access_size
,
1180 mr
->ops
->impl
.max_access_size
,
1181 memory_region_write_with_attrs_accessor
,
1184 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1185 memory_region_oldmmio_write_accessor
,
1190 void memory_region_init_io(MemoryRegion
*mr
,
1192 const MemoryRegionOps
*ops
,
1197 memory_region_init(mr
, owner
, name
, size
);
1199 mr
->opaque
= opaque
;
1200 mr
->terminates
= true;
1201 mr
->ram_addr
= ~(ram_addr_t
)0;
1204 void memory_region_init_ram(MemoryRegion
*mr
,
1210 memory_region_init(mr
, owner
, name
, size
);
1212 mr
->terminates
= true;
1213 mr
->destructor
= memory_region_destructor_ram
;
1214 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1215 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1218 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1223 void (*resized
)(const char*,
1228 memory_region_init(mr
, owner
, name
, size
);
1230 mr
->terminates
= true;
1231 mr
->destructor
= memory_region_destructor_ram
;
1232 mr
->ram_addr
= qemu_ram_alloc_resizeable(size
, max_size
, resized
, mr
, errp
);
1233 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1237 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1238 struct Object
*owner
,
1245 memory_region_init(mr
, owner
, name
, size
);
1247 mr
->terminates
= true;
1248 mr
->destructor
= memory_region_destructor_ram
;
1249 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1250 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1254 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1260 memory_region_init(mr
, owner
, name
, size
);
1262 mr
->terminates
= true;
1263 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1264 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1266 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1267 assert(ptr
!= NULL
);
1268 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_abort
);
1271 void memory_region_set_skip_dump(MemoryRegion
*mr
)
1273 mr
->skip_dump
= true;
1276 void memory_region_init_alias(MemoryRegion
*mr
,
1283 memory_region_init(mr
, owner
, name
, size
);
1284 memory_region_ref(orig
);
1285 mr
->destructor
= memory_region_destructor_alias
;
1287 mr
->alias_offset
= offset
;
1290 void memory_region_init_rom_device(MemoryRegion
*mr
,
1292 const MemoryRegionOps
*ops
,
1298 memory_region_init(mr
, owner
, name
, size
);
1300 mr
->opaque
= opaque
;
1301 mr
->terminates
= true;
1302 mr
->rom_device
= true;
1303 mr
->destructor
= memory_region_destructor_rom_device
;
1304 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1307 void memory_region_init_iommu(MemoryRegion
*mr
,
1309 const MemoryRegionIOMMUOps
*ops
,
1313 memory_region_init(mr
, owner
, name
, size
);
1314 mr
->iommu_ops
= ops
,
1315 mr
->terminates
= true; /* then re-forwards */
1316 notifier_list_init(&mr
->iommu_notify
);
1319 void memory_region_init_reservation(MemoryRegion
*mr
,
1324 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1327 static void memory_region_finalize(Object
*obj
)
1329 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1331 assert(QTAILQ_EMPTY(&mr
->subregions
));
1333 memory_region_clear_coalescing(mr
);
1334 g_free((char *)mr
->name
);
1335 g_free(mr
->ioeventfds
);
1338 Object
*memory_region_owner(MemoryRegion
*mr
)
1340 Object
*obj
= OBJECT(mr
);
1344 void memory_region_ref(MemoryRegion
*mr
)
1346 /* MMIO callbacks most likely will access data that belongs
1347 * to the owner, hence the need to ref/unref the owner whenever
1348 * the memory region is in use.
1350 * The memory region is a child of its owner. As long as the
1351 * owner doesn't call unparent itself on the memory region,
1352 * ref-ing the owner will also keep the memory region alive.
1353 * Memory regions without an owner are supposed to never go away,
1354 * but we still ref/unref them for debugging purposes.
1356 Object
*obj
= OBJECT(mr
);
1357 if (obj
&& obj
->parent
) {
1358 object_ref(obj
->parent
);
1364 void memory_region_unref(MemoryRegion
*mr
)
1366 Object
*obj
= OBJECT(mr
);
1367 if (obj
&& obj
->parent
) {
1368 object_unref(obj
->parent
);
1374 uint64_t memory_region_size(MemoryRegion
*mr
)
1376 if (int128_eq(mr
->size
, int128_2_64())) {
1379 return int128_get64(mr
->size
);
1382 const char *memory_region_name(const MemoryRegion
*mr
)
1385 ((MemoryRegion
*)mr
)->name
=
1386 object_get_canonical_path_component(OBJECT(mr
));
1391 bool memory_region_is_ram(MemoryRegion
*mr
)
1396 bool memory_region_is_skip_dump(MemoryRegion
*mr
)
1398 return mr
->skip_dump
;
1401 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1403 uint8_t mask
= mr
->dirty_log_mask
;
1404 if (global_dirty_log
) {
1405 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1410 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1412 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1415 bool memory_region_is_rom(MemoryRegion
*mr
)
1417 return mr
->ram
&& mr
->readonly
;
1420 bool memory_region_is_iommu(MemoryRegion
*mr
)
1422 return mr
->iommu_ops
;
1425 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1427 notifier_list_add(&mr
->iommu_notify
, n
);
1430 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1435 void memory_region_notify_iommu(MemoryRegion
*mr
,
1436 IOMMUTLBEntry entry
)
1438 assert(memory_region_is_iommu(mr
));
1439 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1442 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1444 uint8_t mask
= 1 << client
;
1446 assert(client
== DIRTY_MEMORY_VGA
);
1447 memory_region_transaction_begin();
1448 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1449 memory_region_update_pending
|= mr
->enabled
;
1450 memory_region_transaction_commit();
1453 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1454 hwaddr size
, unsigned client
)
1456 assert(mr
->terminates
);
1457 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1460 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1463 assert(mr
->terminates
);
1464 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
,
1465 memory_region_get_dirty_log_mask(mr
));
1468 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1469 hwaddr size
, unsigned client
)
1472 assert(mr
->terminates
);
1473 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1475 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1481 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1486 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1487 FlatView
*view
= address_space_get_flatview(as
);
1488 FOR_EACH_FLAT_RANGE(fr
, view
) {
1490 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1493 flatview_unref(view
);
1497 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1499 if (mr
->readonly
!= readonly
) {
1500 memory_region_transaction_begin();
1501 mr
->readonly
= readonly
;
1502 memory_region_update_pending
|= mr
->enabled
;
1503 memory_region_transaction_commit();
1507 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1509 if (mr
->romd_mode
!= romd_mode
) {
1510 memory_region_transaction_begin();
1511 mr
->romd_mode
= romd_mode
;
1512 memory_region_update_pending
|= mr
->enabled
;
1513 memory_region_transaction_commit();
1517 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1518 hwaddr size
, unsigned client
)
1520 assert(mr
->terminates
);
1521 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1524 int memory_region_get_fd(MemoryRegion
*mr
)
1527 return memory_region_get_fd(mr
->alias
);
1530 assert(mr
->terminates
);
1532 return qemu_get_ram_fd(mr
->ram_addr
& TARGET_PAGE_MASK
);
1535 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1538 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1541 assert(mr
->terminates
);
1543 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1546 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1548 assert(mr
->terminates
);
1550 qemu_ram_resize(mr
->ram_addr
, newsize
, errp
);
1553 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1557 CoalescedMemoryRange
*cmr
;
1559 MemoryRegionSection section
;
1561 view
= address_space_get_flatview(as
);
1562 FOR_EACH_FLAT_RANGE(fr
, view
) {
1564 section
= (MemoryRegionSection
) {
1565 .address_space
= as
,
1566 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1567 .size
= fr
->addr
.size
,
1570 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1571 int128_get64(fr
->addr
.start
),
1572 int128_get64(fr
->addr
.size
));
1573 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1574 tmp
= addrrange_shift(cmr
->addr
,
1575 int128_sub(fr
->addr
.start
,
1576 int128_make64(fr
->offset_in_region
)));
1577 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1580 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1581 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1582 int128_get64(tmp
.start
),
1583 int128_get64(tmp
.size
));
1587 flatview_unref(view
);
1590 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1594 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1595 memory_region_update_coalesced_range_as(mr
, as
);
1599 void memory_region_set_coalescing(MemoryRegion
*mr
)
1601 memory_region_clear_coalescing(mr
);
1602 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1605 void memory_region_add_coalescing(MemoryRegion
*mr
,
1609 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1611 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1612 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1613 memory_region_update_coalesced_range(mr
);
1614 memory_region_set_flush_coalesced(mr
);
1617 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1619 CoalescedMemoryRange
*cmr
;
1620 bool updated
= false;
1622 qemu_flush_coalesced_mmio_buffer();
1623 mr
->flush_coalesced_mmio
= false;
1625 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1626 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1627 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1633 memory_region_update_coalesced_range(mr
);
1637 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1639 mr
->flush_coalesced_mmio
= true;
1642 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1644 qemu_flush_coalesced_mmio_buffer();
1645 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1646 mr
->flush_coalesced_mmio
= false;
1650 void memory_region_add_eventfd(MemoryRegion
*mr
,
1657 MemoryRegionIoeventfd mrfd
= {
1658 .addr
.start
= int128_make64(addr
),
1659 .addr
.size
= int128_make64(size
),
1660 .match_data
= match_data
,
1666 adjust_endianness(mr
, &mrfd
.data
, size
);
1667 memory_region_transaction_begin();
1668 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1669 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1674 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1675 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1676 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1677 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1678 mr
->ioeventfds
[i
] = mrfd
;
1679 ioeventfd_update_pending
|= mr
->enabled
;
1680 memory_region_transaction_commit();
1683 void memory_region_del_eventfd(MemoryRegion
*mr
,
1690 MemoryRegionIoeventfd mrfd
= {
1691 .addr
.start
= int128_make64(addr
),
1692 .addr
.size
= int128_make64(size
),
1693 .match_data
= match_data
,
1699 adjust_endianness(mr
, &mrfd
.data
, size
);
1700 memory_region_transaction_begin();
1701 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1702 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1706 assert(i
!= mr
->ioeventfd_nb
);
1707 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1708 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1710 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1711 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1712 ioeventfd_update_pending
|= mr
->enabled
;
1713 memory_region_transaction_commit();
1716 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1718 hwaddr offset
= subregion
->addr
;
1719 MemoryRegion
*mr
= subregion
->container
;
1720 MemoryRegion
*other
;
1722 memory_region_transaction_begin();
1724 memory_region_ref(subregion
);
1725 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1726 if (subregion
->may_overlap
|| other
->may_overlap
) {
1729 if (int128_ge(int128_make64(offset
),
1730 int128_add(int128_make64(other
->addr
), other
->size
))
1731 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1732 int128_make64(other
->addr
))) {
1736 printf("warning: subregion collision %llx/%llx (%s) "
1737 "vs %llx/%llx (%s)\n",
1738 (unsigned long long)offset
,
1739 (unsigned long long)int128_get64(subregion
->size
),
1741 (unsigned long long)other
->addr
,
1742 (unsigned long long)int128_get64(other
->size
),
1746 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1747 if (subregion
->priority
>= other
->priority
) {
1748 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1752 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1754 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1755 memory_region_transaction_commit();
1758 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1760 MemoryRegion
*subregion
)
1762 assert(!subregion
->container
);
1763 subregion
->container
= mr
;
1764 subregion
->addr
= offset
;
1765 memory_region_update_container_subregions(subregion
);
1768 void memory_region_add_subregion(MemoryRegion
*mr
,
1770 MemoryRegion
*subregion
)
1772 subregion
->may_overlap
= false;
1773 subregion
->priority
= 0;
1774 memory_region_add_subregion_common(mr
, offset
, subregion
);
1777 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1779 MemoryRegion
*subregion
,
1782 subregion
->may_overlap
= true;
1783 subregion
->priority
= priority
;
1784 memory_region_add_subregion_common(mr
, offset
, subregion
);
1787 void memory_region_del_subregion(MemoryRegion
*mr
,
1788 MemoryRegion
*subregion
)
1790 memory_region_transaction_begin();
1791 assert(subregion
->container
== mr
);
1792 subregion
->container
= NULL
;
1793 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1794 memory_region_unref(subregion
);
1795 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1796 memory_region_transaction_commit();
1799 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1801 if (enabled
== mr
->enabled
) {
1804 memory_region_transaction_begin();
1805 mr
->enabled
= enabled
;
1806 memory_region_update_pending
= true;
1807 memory_region_transaction_commit();
1810 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
1812 Int128 s
= int128_make64(size
);
1814 if (size
== UINT64_MAX
) {
1817 if (int128_eq(s
, mr
->size
)) {
1820 memory_region_transaction_begin();
1822 memory_region_update_pending
= true;
1823 memory_region_transaction_commit();
1826 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1828 MemoryRegion
*container
= mr
->container
;
1831 memory_region_transaction_begin();
1832 memory_region_ref(mr
);
1833 memory_region_del_subregion(container
, mr
);
1834 mr
->container
= container
;
1835 memory_region_update_container_subregions(mr
);
1836 memory_region_unref(mr
);
1837 memory_region_transaction_commit();
1841 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1843 if (addr
!= mr
->addr
) {
1845 memory_region_readd_subregion(mr
);
1849 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1853 if (offset
== mr
->alias_offset
) {
1857 memory_region_transaction_begin();
1858 mr
->alias_offset
= offset
;
1859 memory_region_update_pending
|= mr
->enabled
;
1860 memory_region_transaction_commit();
1863 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1865 return mr
->ram_addr
;
1868 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
1873 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1875 const AddrRange
*addr
= addr_
;
1876 const FlatRange
*fr
= fr_
;
1878 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1880 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1886 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1888 return bsearch(&addr
, view
->ranges
, view
->nr
,
1889 sizeof(FlatRange
), cmp_flatrange_addr
);
1892 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
1894 MemoryRegion
*mr
= memory_region_find(container
, addr
, 1).mr
;
1895 if (!mr
|| (mr
== container
)) {
1898 memory_region_unref(mr
);
1902 bool memory_region_is_mapped(MemoryRegion
*mr
)
1904 return mr
->container
? true : false;
1907 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1908 hwaddr addr
, uint64_t size
)
1910 MemoryRegionSection ret
= { .mr
= NULL
};
1918 for (root
= mr
; root
->container
; ) {
1919 root
= root
->container
;
1923 as
= memory_region_to_address_space(root
);
1927 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1930 view
= atomic_rcu_read(&as
->current_map
);
1931 fr
= flatview_lookup(view
, range
);
1936 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1941 ret
.address_space
= as
;
1942 range
= addrrange_intersection(range
, fr
->addr
);
1943 ret
.offset_within_region
= fr
->offset_in_region
;
1944 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1946 ret
.size
= range
.size
;
1947 ret
.offset_within_address_space
= int128_get64(range
.start
);
1948 ret
.readonly
= fr
->readonly
;
1949 memory_region_ref(ret
.mr
);
1955 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1960 view
= address_space_get_flatview(as
);
1961 FOR_EACH_FLAT_RANGE(fr
, view
) {
1962 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1964 flatview_unref(view
);
1967 void memory_global_dirty_log_start(void)
1969 global_dirty_log
= true;
1971 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1973 /* Refresh DIRTY_LOG_MIGRATION bit. */
1974 memory_region_transaction_begin();
1975 memory_region_update_pending
= true;
1976 memory_region_transaction_commit();
1979 void memory_global_dirty_log_stop(void)
1981 global_dirty_log
= false;
1983 /* Refresh DIRTY_LOG_MIGRATION bit. */
1984 memory_region_transaction_begin();
1985 memory_region_update_pending
= true;
1986 memory_region_transaction_commit();
1988 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1991 static void listener_add_address_space(MemoryListener
*listener
,
1997 if (listener
->address_space_filter
1998 && listener
->address_space_filter
!= as
) {
2002 if (global_dirty_log
) {
2003 if (listener
->log_global_start
) {
2004 listener
->log_global_start(listener
);
2008 view
= address_space_get_flatview(as
);
2009 FOR_EACH_FLAT_RANGE(fr
, view
) {
2010 MemoryRegionSection section
= {
2012 .address_space
= as
,
2013 .offset_within_region
= fr
->offset_in_region
,
2014 .size
= fr
->addr
.size
,
2015 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2016 .readonly
= fr
->readonly
,
2018 if (listener
->region_add
) {
2019 listener
->region_add(listener
, §ion
);
2022 flatview_unref(view
);
2025 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
2027 MemoryListener
*other
= NULL
;
2030 listener
->address_space_filter
= filter
;
2031 if (QTAILQ_EMPTY(&memory_listeners
)
2032 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2033 memory_listeners
)->priority
) {
2034 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2036 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2037 if (listener
->priority
< other
->priority
) {
2041 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2044 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2045 listener_add_address_space(listener
, as
);
2049 void memory_listener_unregister(MemoryListener
*listener
)
2051 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2054 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2056 memory_region_ref(root
);
2057 memory_region_transaction_begin();
2059 as
->current_map
= g_new(FlatView
, 1);
2060 flatview_init(as
->current_map
);
2061 as
->ioeventfd_nb
= 0;
2062 as
->ioeventfds
= NULL
;
2063 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2064 as
->name
= g_strdup(name
? name
: "anonymous");
2065 address_space_init_dispatch(as
);
2066 memory_region_update_pending
|= root
->enabled
;
2067 memory_region_transaction_commit();
2070 static void do_address_space_destroy(AddressSpace
*as
)
2072 MemoryListener
*listener
;
2074 address_space_destroy_dispatch(as
);
2076 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2077 assert(listener
->address_space_filter
!= as
);
2080 flatview_unref(as
->current_map
);
2082 g_free(as
->ioeventfds
);
2083 memory_region_unref(as
->root
);
2086 void address_space_destroy(AddressSpace
*as
)
2088 MemoryRegion
*root
= as
->root
;
2090 /* Flush out anything from MemoryListeners listening in on this */
2091 memory_region_transaction_begin();
2093 memory_region_transaction_commit();
2094 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2095 address_space_unregister(as
);
2097 /* At this point, as->dispatch and as->current_map are dummy
2098 * entries that the guest should never use. Wait for the old
2099 * values to expire before freeing the data.
2102 call_rcu(as
, do_address_space_destroy
, rcu
);
2105 typedef struct MemoryRegionList MemoryRegionList
;
2107 struct MemoryRegionList
{
2108 const MemoryRegion
*mr
;
2109 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2112 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2114 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2115 const MemoryRegion
*mr
, unsigned int level
,
2117 MemoryRegionListHead
*alias_print_queue
)
2119 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2120 MemoryRegionListHead submr_print_queue
;
2121 const MemoryRegion
*submr
;
2128 for (i
= 0; i
< level
; i
++) {
2133 MemoryRegionList
*ml
;
2136 /* check if the alias is already in the queue */
2137 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2138 if (ml
->mr
== mr
->alias
) {
2144 ml
= g_new(MemoryRegionList
, 1);
2146 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2148 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2149 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2150 "-" TARGET_FMT_plx
"%s\n",
2153 + (int128_nz(mr
->size
) ?
2154 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2155 int128_one())) : 0),
2157 mr
->romd_mode
? 'R' : '-',
2158 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2160 memory_region_name(mr
),
2161 memory_region_name(mr
->alias
),
2164 + (int128_nz(mr
->size
) ?
2165 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2166 int128_one())) : 0),
2167 mr
->enabled
? "" : " [disabled]");
2170 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s%s\n",
2173 + (int128_nz(mr
->size
) ?
2174 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2175 int128_one())) : 0),
2177 mr
->romd_mode
? 'R' : '-',
2178 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2180 memory_region_name(mr
),
2181 mr
->enabled
? "" : " [disabled]");
2184 QTAILQ_INIT(&submr_print_queue
);
2186 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2187 new_ml
= g_new(MemoryRegionList
, 1);
2189 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2190 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2191 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2192 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2193 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2199 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2203 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2204 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2208 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2213 void mtree_info(fprintf_function mon_printf
, void *f
)
2215 MemoryRegionListHead ml_head
;
2216 MemoryRegionList
*ml
, *ml2
;
2219 QTAILQ_INIT(&ml_head
);
2221 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2222 mon_printf(f
, "address-space: %s\n", as
->name
);
2223 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2224 mon_printf(f
, "\n");
2227 /* print aliased regions */
2228 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2229 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2230 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2231 mon_printf(f
, "\n");
2234 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2239 static const TypeInfo memory_region_info
= {
2240 .parent
= TYPE_OBJECT
,
2241 .name
= TYPE_MEMORY_REGION
,
2242 .instance_size
= sizeof(MemoryRegion
),
2243 .instance_init
= memory_region_initfn
,
2244 .instance_finalize
= memory_region_finalize
,
2247 static void memory_register_types(void)
2249 type_register_static(&memory_region_info
);
2252 type_init(memory_register_types
)