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) \
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 void memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
380 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
381 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
382 *value
|= (tmp
& mask
) << shift
;
385 static void memory_region_read_accessor(MemoryRegion
*mr
,
394 if (mr
->flush_coalesced_mmio
) {
395 qemu_flush_coalesced_mmio_buffer();
397 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
398 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
399 *value
|= (tmp
& mask
) << shift
;
402 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
411 tmp
= (*value
>> shift
) & mask
;
412 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
413 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
416 static void memory_region_write_accessor(MemoryRegion
*mr
,
425 if (mr
->flush_coalesced_mmio
) {
426 qemu_flush_coalesced_mmio_buffer();
428 tmp
= (*value
>> shift
) & mask
;
429 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
430 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
433 static void access_with_adjusted_size(hwaddr addr
,
436 unsigned access_size_min
,
437 unsigned access_size_max
,
438 void (*access
)(MemoryRegion
*mr
,
446 uint64_t access_mask
;
447 unsigned access_size
;
450 if (!access_size_min
) {
453 if (!access_size_max
) {
457 /* FIXME: support unaligned access? */
458 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
459 access_mask
= -1ULL >> (64 - access_size
* 8);
460 if (memory_region_big_endian(mr
)) {
461 for (i
= 0; i
< size
; i
+= access_size
) {
462 access(mr
, addr
+ i
, value
, access_size
,
463 (size
- access_size
- i
) * 8, access_mask
);
466 for (i
= 0; i
< size
; i
+= access_size
) {
467 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
472 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
476 while (mr
->container
) {
479 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
480 if (mr
== as
->root
) {
487 /* Render a memory region into the global view. Ranges in @view obscure
490 static void render_memory_region(FlatView
*view
,
496 MemoryRegion
*subregion
;
498 hwaddr offset_in_region
;
508 int128_addto(&base
, int128_make64(mr
->addr
));
509 readonly
|= mr
->readonly
;
511 tmp
= addrrange_make(base
, mr
->size
);
513 if (!addrrange_intersects(tmp
, clip
)) {
517 clip
= addrrange_intersection(tmp
, clip
);
520 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
521 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
522 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
526 /* Render subregions in priority order. */
527 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
528 render_memory_region(view
, subregion
, base
, clip
, readonly
);
531 if (!mr
->terminates
) {
535 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
540 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
541 fr
.romd_mode
= mr
->romd_mode
;
542 fr
.readonly
= readonly
;
544 /* Render the region itself into any gaps left by the current view. */
545 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
546 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
549 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
550 now
= int128_min(remain
,
551 int128_sub(view
->ranges
[i
].addr
.start
, base
));
552 fr
.offset_in_region
= offset_in_region
;
553 fr
.addr
= addrrange_make(base
, now
);
554 flatview_insert(view
, i
, &fr
);
556 int128_addto(&base
, now
);
557 offset_in_region
+= int128_get64(now
);
558 int128_subfrom(&remain
, now
);
560 now
= int128_sub(int128_min(int128_add(base
, remain
),
561 addrrange_end(view
->ranges
[i
].addr
)),
563 int128_addto(&base
, now
);
564 offset_in_region
+= int128_get64(now
);
565 int128_subfrom(&remain
, now
);
567 if (int128_nz(remain
)) {
568 fr
.offset_in_region
= offset_in_region
;
569 fr
.addr
= addrrange_make(base
, remain
);
570 flatview_insert(view
, i
, &fr
);
574 /* Render a memory topology into a list of disjoint absolute ranges. */
575 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
579 view
= g_new(FlatView
, 1);
583 render_memory_region(view
, mr
, int128_zero(),
584 addrrange_make(int128_zero(), int128_2_64()), false);
586 flatview_simplify(view
);
591 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
592 MemoryRegionIoeventfd
*fds_new
,
594 MemoryRegionIoeventfd
*fds_old
,
598 MemoryRegionIoeventfd
*fd
;
599 MemoryRegionSection section
;
601 /* Generate a symmetric difference of the old and new fd sets, adding
602 * and deleting as necessary.
606 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
607 if (iold
< fds_old_nb
608 && (inew
== fds_new_nb
609 || memory_region_ioeventfd_before(fds_old
[iold
],
612 section
= (MemoryRegionSection
) {
614 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
615 .size
= fd
->addr
.size
,
617 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
618 fd
->match_data
, fd
->data
, fd
->e
);
620 } else if (inew
< fds_new_nb
621 && (iold
== fds_old_nb
622 || memory_region_ioeventfd_before(fds_new
[inew
],
625 section
= (MemoryRegionSection
) {
627 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
628 .size
= fd
->addr
.size
,
630 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
631 fd
->match_data
, fd
->data
, fd
->e
);
640 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
645 view
= atomic_rcu_read(&as
->current_map
);
651 static void address_space_update_ioeventfds(AddressSpace
*as
)
655 unsigned ioeventfd_nb
= 0;
656 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
660 view
= address_space_get_flatview(as
);
661 FOR_EACH_FLAT_RANGE(fr
, view
) {
662 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
663 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
664 int128_sub(fr
->addr
.start
,
665 int128_make64(fr
->offset_in_region
)));
666 if (addrrange_intersects(fr
->addr
, tmp
)) {
668 ioeventfds
= g_realloc(ioeventfds
,
669 ioeventfd_nb
* sizeof(*ioeventfds
));
670 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
671 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
676 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
677 as
->ioeventfds
, as
->ioeventfd_nb
);
679 g_free(as
->ioeventfds
);
680 as
->ioeventfds
= ioeventfds
;
681 as
->ioeventfd_nb
= ioeventfd_nb
;
682 flatview_unref(view
);
685 static void address_space_update_topology_pass(AddressSpace
*as
,
686 const FlatView
*old_view
,
687 const FlatView
*new_view
,
691 FlatRange
*frold
, *frnew
;
693 /* Generate a symmetric difference of the old and new memory maps.
694 * Kill ranges in the old map, and instantiate ranges in the new map.
697 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
698 if (iold
< old_view
->nr
) {
699 frold
= &old_view
->ranges
[iold
];
703 if (inew
< new_view
->nr
) {
704 frnew
= &new_view
->ranges
[inew
];
711 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
712 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
713 && !flatrange_equal(frold
, frnew
)))) {
714 /* In old but not in new, or in both but attributes changed. */
717 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
721 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
722 /* In both and unchanged (except logging may have changed) */
725 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
726 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
727 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
728 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
729 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
739 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
748 static void address_space_update_topology(AddressSpace
*as
)
750 FlatView
*old_view
= address_space_get_flatview(as
);
751 FlatView
*new_view
= generate_memory_topology(as
->root
);
753 address_space_update_topology_pass(as
, old_view
, new_view
, false);
754 address_space_update_topology_pass(as
, old_view
, new_view
, true);
756 /* Writes are protected by the BQL. */
757 atomic_rcu_set(&as
->current_map
, new_view
);
758 call_rcu(old_view
, flatview_unref
, rcu
);
760 /* Note that all the old MemoryRegions are still alive up to this
761 * point. This relieves most MemoryListeners from the need to
762 * ref/unref the MemoryRegions they get---unless they use them
763 * outside the iothread mutex, in which case precise reference
764 * counting is necessary.
766 flatview_unref(old_view
);
768 address_space_update_ioeventfds(as
);
771 void memory_region_transaction_begin(void)
773 qemu_flush_coalesced_mmio_buffer();
774 ++memory_region_transaction_depth
;
777 static void memory_region_clear_pending(void)
779 memory_region_update_pending
= false;
780 ioeventfd_update_pending
= false;
783 void memory_region_transaction_commit(void)
787 assert(memory_region_transaction_depth
);
788 --memory_region_transaction_depth
;
789 if (!memory_region_transaction_depth
) {
790 if (memory_region_update_pending
) {
791 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
793 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
794 address_space_update_topology(as
);
797 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
798 } else if (ioeventfd_update_pending
) {
799 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
800 address_space_update_ioeventfds(as
);
803 memory_region_clear_pending();
807 static void memory_region_destructor_none(MemoryRegion
*mr
)
811 static void memory_region_destructor_ram(MemoryRegion
*mr
)
813 qemu_ram_free(mr
->ram_addr
);
816 static void memory_region_destructor_alias(MemoryRegion
*mr
)
818 memory_region_unref(mr
->alias
);
821 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
823 qemu_ram_free_from_ptr(mr
->ram_addr
);
826 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
828 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
831 static bool memory_region_need_escape(char c
)
833 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
836 static char *memory_region_escape_name(const char *name
)
843 for (p
= name
; *p
; p
++) {
844 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
846 if (bytes
== p
- name
) {
847 return g_memdup(name
, bytes
+ 1);
850 escaped
= g_malloc(bytes
+ 1);
851 for (p
= name
, q
= escaped
; *p
; p
++) {
853 if (unlikely(memory_region_need_escape(c
))) {
856 *q
++ = "0123456789abcdef"[c
>> 4];
857 c
= "0123456789abcdef"[c
& 15];
865 void memory_region_init(MemoryRegion
*mr
,
871 owner
= container_get(qdev_get_machine(), "/unattached");
874 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
875 mr
->size
= int128_make64(size
);
876 if (size
== UINT64_MAX
) {
877 mr
->size
= int128_2_64();
879 mr
->name
= g_strdup(name
);
882 char *escaped_name
= memory_region_escape_name(name
);
883 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
884 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
885 object_unref(OBJECT(mr
));
887 g_free(escaped_name
);
891 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, void *opaque
,
892 const char *name
, Error
**errp
)
894 MemoryRegion
*mr
= MEMORY_REGION(obj
);
895 uint64_t value
= mr
->addr
;
897 visit_type_uint64(v
, &value
, name
, errp
);
900 static void memory_region_get_container(Object
*obj
, Visitor
*v
, void *opaque
,
901 const char *name
, Error
**errp
)
903 MemoryRegion
*mr
= MEMORY_REGION(obj
);
904 gchar
*path
= (gchar
*)"";
907 path
= object_get_canonical_path(OBJECT(mr
->container
));
909 visit_type_str(v
, &path
, name
, errp
);
915 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
918 MemoryRegion
*mr
= MEMORY_REGION(obj
);
920 return OBJECT(mr
->container
);
923 static void memory_region_get_priority(Object
*obj
, Visitor
*v
, void *opaque
,
924 const char *name
, Error
**errp
)
926 MemoryRegion
*mr
= MEMORY_REGION(obj
);
927 int32_t value
= mr
->priority
;
929 visit_type_int32(v
, &value
, name
, errp
);
932 static bool memory_region_get_may_overlap(Object
*obj
, Error
**errp
)
934 MemoryRegion
*mr
= MEMORY_REGION(obj
);
936 return mr
->may_overlap
;
939 static void memory_region_get_size(Object
*obj
, Visitor
*v
, void *opaque
,
940 const char *name
, Error
**errp
)
942 MemoryRegion
*mr
= MEMORY_REGION(obj
);
943 uint64_t value
= memory_region_size(mr
);
945 visit_type_uint64(v
, &value
, name
, errp
);
948 static void memory_region_initfn(Object
*obj
)
950 MemoryRegion
*mr
= MEMORY_REGION(obj
);
953 mr
->ops
= &unassigned_mem_ops
;
955 mr
->romd_mode
= true;
956 mr
->destructor
= memory_region_destructor_none
;
957 QTAILQ_INIT(&mr
->subregions
);
958 QTAILQ_INIT(&mr
->coalesced
);
960 op
= object_property_add(OBJECT(mr
), "container",
961 "link<" TYPE_MEMORY_REGION
">",
962 memory_region_get_container
,
963 NULL
, /* memory_region_set_container */
964 NULL
, NULL
, &error_abort
);
965 op
->resolve
= memory_region_resolve_container
;
967 object_property_add(OBJECT(mr
), "addr", "uint64",
968 memory_region_get_addr
,
969 NULL
, /* memory_region_set_addr */
970 NULL
, NULL
, &error_abort
);
971 object_property_add(OBJECT(mr
), "priority", "uint32",
972 memory_region_get_priority
,
973 NULL
, /* memory_region_set_priority */
974 NULL
, NULL
, &error_abort
);
975 object_property_add_bool(OBJECT(mr
), "may-overlap",
976 memory_region_get_may_overlap
,
977 NULL
, /* memory_region_set_may_overlap */
979 object_property_add(OBJECT(mr
), "size", "uint64",
980 memory_region_get_size
,
981 NULL
, /* memory_region_set_size, */
982 NULL
, NULL
, &error_abort
);
985 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
988 #ifdef DEBUG_UNASSIGNED
989 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
991 if (current_cpu
!= NULL
) {
992 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
997 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
998 uint64_t val
, unsigned size
)
1000 #ifdef DEBUG_UNASSIGNED
1001 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1003 if (current_cpu
!= NULL
) {
1004 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1008 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1009 unsigned size
, bool is_write
)
1014 const MemoryRegionOps unassigned_mem_ops
= {
1015 .valid
.accepts
= unassigned_mem_accepts
,
1016 .endianness
= DEVICE_NATIVE_ENDIAN
,
1019 bool memory_region_access_valid(MemoryRegion
*mr
,
1024 int access_size_min
, access_size_max
;
1027 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1031 if (!mr
->ops
->valid
.accepts
) {
1035 access_size_min
= mr
->ops
->valid
.min_access_size
;
1036 if (!mr
->ops
->valid
.min_access_size
) {
1037 access_size_min
= 1;
1040 access_size_max
= mr
->ops
->valid
.max_access_size
;
1041 if (!mr
->ops
->valid
.max_access_size
) {
1042 access_size_max
= 4;
1045 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1046 for (i
= 0; i
< size
; i
+= access_size
) {
1047 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1056 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
1062 if (mr
->ops
->read
) {
1063 access_with_adjusted_size(addr
, &data
, size
,
1064 mr
->ops
->impl
.min_access_size
,
1065 mr
->ops
->impl
.max_access_size
,
1066 memory_region_read_accessor
, mr
);
1068 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1069 memory_region_oldmmio_read_accessor
, mr
);
1075 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
1080 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1081 *pval
= unassigned_mem_read(mr
, addr
, size
);
1085 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
1086 adjust_endianness(mr
, pval
, size
);
1090 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
1095 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1096 unassigned_mem_write(mr
, addr
, data
, size
);
1100 adjust_endianness(mr
, &data
, size
);
1102 if (mr
->ops
->write
) {
1103 access_with_adjusted_size(addr
, &data
, size
,
1104 mr
->ops
->impl
.min_access_size
,
1105 mr
->ops
->impl
.max_access_size
,
1106 memory_region_write_accessor
, mr
);
1108 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1109 memory_region_oldmmio_write_accessor
, mr
);
1114 void memory_region_init_io(MemoryRegion
*mr
,
1116 const MemoryRegionOps
*ops
,
1121 memory_region_init(mr
, owner
, name
, size
);
1123 mr
->opaque
= opaque
;
1124 mr
->terminates
= true;
1125 mr
->ram_addr
= ~(ram_addr_t
)0;
1128 void memory_region_init_ram(MemoryRegion
*mr
,
1134 memory_region_init(mr
, owner
, name
, size
);
1136 mr
->terminates
= true;
1137 mr
->destructor
= memory_region_destructor_ram
;
1138 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1141 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1146 void (*resized
)(const char*,
1151 memory_region_init(mr
, owner
, name
, size
);
1153 mr
->terminates
= true;
1154 mr
->destructor
= memory_region_destructor_ram
;
1155 mr
->ram_addr
= qemu_ram_alloc_resizeable(size
, max_size
, resized
, mr
, errp
);
1159 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1160 struct Object
*owner
,
1167 memory_region_init(mr
, owner
, name
, size
);
1169 mr
->terminates
= true;
1170 mr
->destructor
= memory_region_destructor_ram
;
1171 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1175 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1181 memory_region_init(mr
, owner
, name
, size
);
1183 mr
->terminates
= true;
1184 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1186 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1187 assert(ptr
!= NULL
);
1188 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_abort
);
1191 void memory_region_set_skip_dump(MemoryRegion
*mr
)
1193 mr
->skip_dump
= true;
1196 void memory_region_init_alias(MemoryRegion
*mr
,
1203 memory_region_init(mr
, owner
, name
, size
);
1204 memory_region_ref(orig
);
1205 mr
->destructor
= memory_region_destructor_alias
;
1207 mr
->alias_offset
= offset
;
1210 void memory_region_init_rom_device(MemoryRegion
*mr
,
1212 const MemoryRegionOps
*ops
,
1218 memory_region_init(mr
, owner
, name
, size
);
1220 mr
->opaque
= opaque
;
1221 mr
->terminates
= true;
1222 mr
->rom_device
= true;
1223 mr
->destructor
= memory_region_destructor_rom_device
;
1224 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1227 void memory_region_init_iommu(MemoryRegion
*mr
,
1229 const MemoryRegionIOMMUOps
*ops
,
1233 memory_region_init(mr
, owner
, name
, size
);
1234 mr
->iommu_ops
= ops
,
1235 mr
->terminates
= true; /* then re-forwards */
1236 notifier_list_init(&mr
->iommu_notify
);
1239 void memory_region_init_reservation(MemoryRegion
*mr
,
1244 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1247 static void memory_region_finalize(Object
*obj
)
1249 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1251 assert(QTAILQ_EMPTY(&mr
->subregions
));
1253 memory_region_clear_coalescing(mr
);
1254 g_free((char *)mr
->name
);
1255 g_free(mr
->ioeventfds
);
1258 Object
*memory_region_owner(MemoryRegion
*mr
)
1260 Object
*obj
= OBJECT(mr
);
1264 void memory_region_ref(MemoryRegion
*mr
)
1266 /* MMIO callbacks most likely will access data that belongs
1267 * to the owner, hence the need to ref/unref the owner whenever
1268 * the memory region is in use.
1270 * The memory region is a child of its owner. As long as the
1271 * owner doesn't call unparent itself on the memory region,
1272 * ref-ing the owner will also keep the memory region alive.
1273 * Memory regions without an owner are supposed to never go away,
1274 * but we still ref/unref them for debugging purposes.
1276 Object
*obj
= OBJECT(mr
);
1277 if (obj
&& obj
->parent
) {
1278 object_ref(obj
->parent
);
1284 void memory_region_unref(MemoryRegion
*mr
)
1286 Object
*obj
= OBJECT(mr
);
1287 if (obj
&& obj
->parent
) {
1288 object_unref(obj
->parent
);
1294 uint64_t memory_region_size(MemoryRegion
*mr
)
1296 if (int128_eq(mr
->size
, int128_2_64())) {
1299 return int128_get64(mr
->size
);
1302 const char *memory_region_name(const MemoryRegion
*mr
)
1305 ((MemoryRegion
*)mr
)->name
=
1306 object_get_canonical_path_component(OBJECT(mr
));
1311 bool memory_region_is_ram(MemoryRegion
*mr
)
1316 bool memory_region_is_skip_dump(MemoryRegion
*mr
)
1318 return mr
->skip_dump
;
1321 bool memory_region_is_logging(MemoryRegion
*mr
)
1323 return mr
->dirty_log_mask
;
1326 bool memory_region_is_rom(MemoryRegion
*mr
)
1328 return mr
->ram
&& mr
->readonly
;
1331 bool memory_region_is_iommu(MemoryRegion
*mr
)
1333 return mr
->iommu_ops
;
1336 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1338 notifier_list_add(&mr
->iommu_notify
, n
);
1341 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1346 void memory_region_notify_iommu(MemoryRegion
*mr
,
1347 IOMMUTLBEntry entry
)
1349 assert(memory_region_is_iommu(mr
));
1350 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1353 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1355 uint8_t mask
= 1 << client
;
1357 memory_region_transaction_begin();
1358 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1359 memory_region_update_pending
|= mr
->enabled
;
1360 memory_region_transaction_commit();
1363 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1364 hwaddr size
, unsigned client
)
1366 assert(mr
->terminates
);
1367 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1370 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1373 assert(mr
->terminates
);
1374 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
);
1377 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1378 hwaddr size
, unsigned client
)
1381 assert(mr
->terminates
);
1382 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1384 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1390 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1395 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1396 FlatView
*view
= address_space_get_flatview(as
);
1397 FOR_EACH_FLAT_RANGE(fr
, view
) {
1399 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1402 flatview_unref(view
);
1406 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1408 if (mr
->readonly
!= readonly
) {
1409 memory_region_transaction_begin();
1410 mr
->readonly
= readonly
;
1411 memory_region_update_pending
|= mr
->enabled
;
1412 memory_region_transaction_commit();
1416 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1418 if (mr
->romd_mode
!= romd_mode
) {
1419 memory_region_transaction_begin();
1420 mr
->romd_mode
= romd_mode
;
1421 memory_region_update_pending
|= mr
->enabled
;
1422 memory_region_transaction_commit();
1426 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1427 hwaddr size
, unsigned client
)
1429 assert(mr
->terminates
);
1430 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1433 int memory_region_get_fd(MemoryRegion
*mr
)
1436 return memory_region_get_fd(mr
->alias
);
1439 assert(mr
->terminates
);
1441 return qemu_get_ram_fd(mr
->ram_addr
& TARGET_PAGE_MASK
);
1444 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1447 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1450 assert(mr
->terminates
);
1452 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1455 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1457 assert(mr
->terminates
);
1459 qemu_ram_resize(mr
->ram_addr
, newsize
, errp
);
1462 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1466 CoalescedMemoryRange
*cmr
;
1468 MemoryRegionSection section
;
1470 view
= address_space_get_flatview(as
);
1471 FOR_EACH_FLAT_RANGE(fr
, view
) {
1473 section
= (MemoryRegionSection
) {
1474 .address_space
= as
,
1475 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1476 .size
= fr
->addr
.size
,
1479 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1480 int128_get64(fr
->addr
.start
),
1481 int128_get64(fr
->addr
.size
));
1482 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1483 tmp
= addrrange_shift(cmr
->addr
,
1484 int128_sub(fr
->addr
.start
,
1485 int128_make64(fr
->offset_in_region
)));
1486 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1489 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1490 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1491 int128_get64(tmp
.start
),
1492 int128_get64(tmp
.size
));
1496 flatview_unref(view
);
1499 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1503 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1504 memory_region_update_coalesced_range_as(mr
, as
);
1508 void memory_region_set_coalescing(MemoryRegion
*mr
)
1510 memory_region_clear_coalescing(mr
);
1511 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1514 void memory_region_add_coalescing(MemoryRegion
*mr
,
1518 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1520 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1521 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1522 memory_region_update_coalesced_range(mr
);
1523 memory_region_set_flush_coalesced(mr
);
1526 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1528 CoalescedMemoryRange
*cmr
;
1529 bool updated
= false;
1531 qemu_flush_coalesced_mmio_buffer();
1532 mr
->flush_coalesced_mmio
= false;
1534 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1535 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1536 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1542 memory_region_update_coalesced_range(mr
);
1546 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1548 mr
->flush_coalesced_mmio
= true;
1551 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1553 qemu_flush_coalesced_mmio_buffer();
1554 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1555 mr
->flush_coalesced_mmio
= false;
1559 void memory_region_add_eventfd(MemoryRegion
*mr
,
1566 MemoryRegionIoeventfd mrfd
= {
1567 .addr
.start
= int128_make64(addr
),
1568 .addr
.size
= int128_make64(size
),
1569 .match_data
= match_data
,
1575 adjust_endianness(mr
, &mrfd
.data
, size
);
1576 memory_region_transaction_begin();
1577 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1578 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1583 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1584 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1585 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1586 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1587 mr
->ioeventfds
[i
] = mrfd
;
1588 ioeventfd_update_pending
|= mr
->enabled
;
1589 memory_region_transaction_commit();
1592 void memory_region_del_eventfd(MemoryRegion
*mr
,
1599 MemoryRegionIoeventfd mrfd
= {
1600 .addr
.start
= int128_make64(addr
),
1601 .addr
.size
= int128_make64(size
),
1602 .match_data
= match_data
,
1608 adjust_endianness(mr
, &mrfd
.data
, size
);
1609 memory_region_transaction_begin();
1610 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1611 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1615 assert(i
!= mr
->ioeventfd_nb
);
1616 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1617 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1619 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1620 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1621 ioeventfd_update_pending
|= mr
->enabled
;
1622 memory_region_transaction_commit();
1625 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1627 hwaddr offset
= subregion
->addr
;
1628 MemoryRegion
*mr
= subregion
->container
;
1629 MemoryRegion
*other
;
1631 memory_region_transaction_begin();
1633 memory_region_ref(subregion
);
1634 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1635 if (subregion
->may_overlap
|| other
->may_overlap
) {
1638 if (int128_ge(int128_make64(offset
),
1639 int128_add(int128_make64(other
->addr
), other
->size
))
1640 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1641 int128_make64(other
->addr
))) {
1645 printf("warning: subregion collision %llx/%llx (%s) "
1646 "vs %llx/%llx (%s)\n",
1647 (unsigned long long)offset
,
1648 (unsigned long long)int128_get64(subregion
->size
),
1650 (unsigned long long)other
->addr
,
1651 (unsigned long long)int128_get64(other
->size
),
1655 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1656 if (subregion
->priority
>= other
->priority
) {
1657 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1661 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1663 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1664 memory_region_transaction_commit();
1667 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1669 MemoryRegion
*subregion
)
1671 assert(!subregion
->container
);
1672 subregion
->container
= mr
;
1673 subregion
->addr
= offset
;
1674 memory_region_update_container_subregions(subregion
);
1677 void memory_region_add_subregion(MemoryRegion
*mr
,
1679 MemoryRegion
*subregion
)
1681 subregion
->may_overlap
= false;
1682 subregion
->priority
= 0;
1683 memory_region_add_subregion_common(mr
, offset
, subregion
);
1686 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1688 MemoryRegion
*subregion
,
1691 subregion
->may_overlap
= true;
1692 subregion
->priority
= priority
;
1693 memory_region_add_subregion_common(mr
, offset
, subregion
);
1696 void memory_region_del_subregion(MemoryRegion
*mr
,
1697 MemoryRegion
*subregion
)
1699 memory_region_transaction_begin();
1700 assert(subregion
->container
== mr
);
1701 subregion
->container
= NULL
;
1702 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1703 memory_region_unref(subregion
);
1704 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1705 memory_region_transaction_commit();
1708 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1710 if (enabled
== mr
->enabled
) {
1713 memory_region_transaction_begin();
1714 mr
->enabled
= enabled
;
1715 memory_region_update_pending
= true;
1716 memory_region_transaction_commit();
1719 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
1721 Int128 s
= int128_make64(size
);
1723 if (size
== UINT64_MAX
) {
1726 if (int128_eq(s
, mr
->size
)) {
1729 memory_region_transaction_begin();
1731 memory_region_update_pending
= true;
1732 memory_region_transaction_commit();
1735 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1737 MemoryRegion
*container
= mr
->container
;
1740 memory_region_transaction_begin();
1741 memory_region_ref(mr
);
1742 memory_region_del_subregion(container
, mr
);
1743 mr
->container
= container
;
1744 memory_region_update_container_subregions(mr
);
1745 memory_region_unref(mr
);
1746 memory_region_transaction_commit();
1750 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1752 if (addr
!= mr
->addr
) {
1754 memory_region_readd_subregion(mr
);
1758 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1762 if (offset
== mr
->alias_offset
) {
1766 memory_region_transaction_begin();
1767 mr
->alias_offset
= offset
;
1768 memory_region_update_pending
|= mr
->enabled
;
1769 memory_region_transaction_commit();
1772 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1774 return mr
->ram_addr
;
1777 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
1782 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1784 const AddrRange
*addr
= addr_
;
1785 const FlatRange
*fr
= fr_
;
1787 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1789 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1795 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1797 return bsearch(&addr
, view
->ranges
, view
->nr
,
1798 sizeof(FlatRange
), cmp_flatrange_addr
);
1801 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
1803 MemoryRegion
*mr
= memory_region_find(container
, addr
, 1).mr
;
1804 if (!mr
|| (mr
== container
)) {
1807 memory_region_unref(mr
);
1811 bool memory_region_is_mapped(MemoryRegion
*mr
)
1813 return mr
->container
? true : false;
1816 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1817 hwaddr addr
, uint64_t size
)
1819 MemoryRegionSection ret
= { .mr
= NULL
};
1827 for (root
= mr
; root
->container
; ) {
1828 root
= root
->container
;
1832 as
= memory_region_to_address_space(root
);
1836 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1839 view
= atomic_rcu_read(&as
->current_map
);
1840 fr
= flatview_lookup(view
, range
);
1845 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1850 ret
.address_space
= as
;
1851 range
= addrrange_intersection(range
, fr
->addr
);
1852 ret
.offset_within_region
= fr
->offset_in_region
;
1853 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1855 ret
.size
= range
.size
;
1856 ret
.offset_within_address_space
= int128_get64(range
.start
);
1857 ret
.readonly
= fr
->readonly
;
1858 memory_region_ref(ret
.mr
);
1864 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1869 view
= address_space_get_flatview(as
);
1870 FOR_EACH_FLAT_RANGE(fr
, view
) {
1871 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1873 flatview_unref(view
);
1876 void memory_global_dirty_log_start(void)
1878 global_dirty_log
= true;
1879 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1882 void memory_global_dirty_log_stop(void)
1884 global_dirty_log
= false;
1885 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1888 static void listener_add_address_space(MemoryListener
*listener
,
1894 if (listener
->address_space_filter
1895 && listener
->address_space_filter
!= as
) {
1899 if (global_dirty_log
) {
1900 if (listener
->log_global_start
) {
1901 listener
->log_global_start(listener
);
1905 view
= address_space_get_flatview(as
);
1906 FOR_EACH_FLAT_RANGE(fr
, view
) {
1907 MemoryRegionSection section
= {
1909 .address_space
= as
,
1910 .offset_within_region
= fr
->offset_in_region
,
1911 .size
= fr
->addr
.size
,
1912 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1913 .readonly
= fr
->readonly
,
1915 if (listener
->region_add
) {
1916 listener
->region_add(listener
, §ion
);
1919 flatview_unref(view
);
1922 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1924 MemoryListener
*other
= NULL
;
1927 listener
->address_space_filter
= filter
;
1928 if (QTAILQ_EMPTY(&memory_listeners
)
1929 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1930 memory_listeners
)->priority
) {
1931 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1933 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1934 if (listener
->priority
< other
->priority
) {
1938 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1941 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1942 listener_add_address_space(listener
, as
);
1946 void memory_listener_unregister(MemoryListener
*listener
)
1948 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1951 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1953 memory_region_ref(root
);
1954 memory_region_transaction_begin();
1956 as
->current_map
= g_new(FlatView
, 1);
1957 flatview_init(as
->current_map
);
1958 as
->ioeventfd_nb
= 0;
1959 as
->ioeventfds
= NULL
;
1960 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1961 as
->name
= g_strdup(name
? name
: "anonymous");
1962 address_space_init_dispatch(as
);
1963 memory_region_update_pending
|= root
->enabled
;
1964 memory_region_transaction_commit();
1967 static void do_address_space_destroy(AddressSpace
*as
)
1969 MemoryListener
*listener
;
1971 address_space_destroy_dispatch(as
);
1973 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1974 assert(listener
->address_space_filter
!= as
);
1977 flatview_unref(as
->current_map
);
1979 g_free(as
->ioeventfds
);
1980 memory_region_unref(as
->root
);
1983 void address_space_destroy(AddressSpace
*as
)
1985 MemoryRegion
*root
= as
->root
;
1987 /* Flush out anything from MemoryListeners listening in on this */
1988 memory_region_transaction_begin();
1990 memory_region_transaction_commit();
1991 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1992 address_space_unregister(as
);
1994 /* At this point, as->dispatch and as->current_map are dummy
1995 * entries that the guest should never use. Wait for the old
1996 * values to expire before freeing the data.
1999 call_rcu(as
, do_address_space_destroy
, rcu
);
2002 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
2004 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
2007 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
2008 uint64_t val
, unsigned size
)
2010 return memory_region_dispatch_write(mr
, addr
, val
, size
);
2013 typedef struct MemoryRegionList MemoryRegionList
;
2015 struct MemoryRegionList
{
2016 const MemoryRegion
*mr
;
2017 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2020 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2022 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2023 const MemoryRegion
*mr
, unsigned int level
,
2025 MemoryRegionListHead
*alias_print_queue
)
2027 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2028 MemoryRegionListHead submr_print_queue
;
2029 const MemoryRegion
*submr
;
2032 if (!mr
|| !mr
->enabled
) {
2036 for (i
= 0; i
< level
; i
++) {
2041 MemoryRegionList
*ml
;
2044 /* check if the alias is already in the queue */
2045 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2046 if (ml
->mr
== mr
->alias
) {
2052 ml
= g_new(MemoryRegionList
, 1);
2054 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2056 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2057 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2058 "-" TARGET_FMT_plx
"\n",
2061 + (int128_nz(mr
->size
) ?
2062 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2063 int128_one())) : 0),
2065 mr
->romd_mode
? 'R' : '-',
2066 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2068 memory_region_name(mr
),
2069 memory_region_name(mr
->alias
),
2072 + (int128_nz(mr
->size
) ?
2073 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2074 int128_one())) : 0));
2077 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
2080 + (int128_nz(mr
->size
) ?
2081 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2082 int128_one())) : 0),
2084 mr
->romd_mode
? 'R' : '-',
2085 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2087 memory_region_name(mr
));
2090 QTAILQ_INIT(&submr_print_queue
);
2092 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2093 new_ml
= g_new(MemoryRegionList
, 1);
2095 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2096 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2097 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2098 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2099 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2105 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2109 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2110 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2114 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2119 void mtree_info(fprintf_function mon_printf
, void *f
)
2121 MemoryRegionListHead ml_head
;
2122 MemoryRegionList
*ml
, *ml2
;
2125 QTAILQ_INIT(&ml_head
);
2127 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2128 mon_printf(f
, "%s\n", as
->name
);
2129 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
2132 mon_printf(f
, "aliases\n");
2133 /* print aliased regions */
2134 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2135 mon_printf(f
, "%s\n", memory_region_name(ml
->mr
));
2136 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
2139 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2144 static const TypeInfo memory_region_info
= {
2145 .parent
= TYPE_OBJECT
,
2146 .name
= TYPE_MEMORY_REGION
,
2147 .instance_size
= sizeof(MemoryRegion
),
2148 .instance_init
= memory_region_initfn
,
2149 .instance_finalize
= memory_region_finalize
,
2152 static void memory_register_types(void)
2154 type_register_static(&memory_region_info
);
2157 type_init(memory_register_types
)