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 "qemu/error-report.h"
22 #include "qom/object.h"
26 #include "exec/memory-internal.h"
27 #include "exec/ram_addr.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/sysemu.h"
31 //#define DEBUG_UNASSIGNED
33 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
35 static unsigned memory_region_transaction_depth
;
36 static bool memory_region_update_pending
;
37 static bool ioeventfd_update_pending
;
38 static bool global_dirty_log
= false;
40 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
41 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
43 static QTAILQ_HEAD(, AddressSpace
) address_spaces
44 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
46 typedef struct AddrRange AddrRange
;
49 * Note that signed integers are needed for negative offsetting in aliases
50 * (large MemoryRegion::alias_offset).
57 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
59 return (AddrRange
) { start
, size
};
62 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
64 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
67 static Int128
addrrange_end(AddrRange r
)
69 return int128_add(r
.start
, r
.size
);
72 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
74 int128_addto(&range
.start
, delta
);
78 static bool addrrange_contains(AddrRange range
, Int128 addr
)
80 return int128_ge(addr
, range
.start
)
81 && int128_lt(addr
, addrrange_end(range
));
84 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
86 return addrrange_contains(r1
, r2
.start
)
87 || addrrange_contains(r2
, r1
.start
);
90 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
92 Int128 start
= int128_max(r1
.start
, r2
.start
);
93 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
94 return addrrange_make(start
, int128_sub(end
, start
));
97 enum ListenerDirection
{ Forward
, Reverse
};
99 static bool memory_listener_match(MemoryListener
*listener
,
100 MemoryRegionSection
*section
)
102 return !listener
->address_space_filter
103 || listener
->address_space_filter
== section
->address_space
;
106 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
108 MemoryListener *_listener; \
110 switch (_direction) { \
112 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
113 if (_listener->_callback) { \
114 _listener->_callback(_listener, ##_args); \
119 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
120 memory_listeners, link) { \
121 if (_listener->_callback) { \
122 _listener->_callback(_listener, ##_args); \
131 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
133 MemoryListener *_listener; \
135 switch (_direction) { \
137 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
138 if (_listener->_callback \
139 && memory_listener_match(_listener, _section)) { \
140 _listener->_callback(_listener, _section, ##_args); \
145 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
146 memory_listeners, link) { \
147 if (_listener->_callback \
148 && memory_listener_match(_listener, _section)) { \
149 _listener->_callback(_listener, _section, ##_args); \
158 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
159 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
160 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
162 .address_space = (as), \
163 .offset_within_region = (fr)->offset_in_region, \
164 .size = (fr)->addr.size, \
165 .offset_within_address_space = int128_get64((fr)->addr.start), \
166 .readonly = (fr)->readonly, \
169 struct CoalescedMemoryRange
{
171 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
174 struct MemoryRegionIoeventfd
{
181 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
182 MemoryRegionIoeventfd b
)
184 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
186 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
188 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
190 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
192 } else if (a
.match_data
< b
.match_data
) {
194 } else if (a
.match_data
> b
.match_data
) {
196 } else if (a
.match_data
) {
197 if (a
.data
< b
.data
) {
199 } else if (a
.data
> b
.data
) {
205 } else if (a
.e
> b
.e
) {
211 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
212 MemoryRegionIoeventfd b
)
214 return !memory_region_ioeventfd_before(a
, b
)
215 && !memory_region_ioeventfd_before(b
, a
);
218 typedef struct FlatRange FlatRange
;
219 typedef struct FlatView FlatView
;
221 /* Range of memory in the global map. Addresses are absolute. */
224 hwaddr offset_in_region
;
226 uint8_t dirty_log_mask
;
231 /* Flattened global view of current active memory hierarchy. Kept in sorted
239 unsigned nr_allocated
;
242 typedef struct AddressSpaceOps AddressSpaceOps
;
244 #define FOR_EACH_FLAT_RANGE(var, view) \
245 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
247 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
249 return a
->mr
== b
->mr
250 && addrrange_equal(a
->addr
, b
->addr
)
251 && a
->offset_in_region
== b
->offset_in_region
252 && a
->romd_mode
== b
->romd_mode
253 && a
->readonly
== b
->readonly
;
256 static void flatview_init(FlatView
*view
)
261 view
->nr_allocated
= 0;
264 /* Insert a range into a given position. Caller is responsible for maintaining
267 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
269 if (view
->nr
== view
->nr_allocated
) {
270 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
271 view
->ranges
= g_realloc(view
->ranges
,
272 view
->nr_allocated
* sizeof(*view
->ranges
));
274 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
275 (view
->nr
- pos
) * sizeof(FlatRange
));
276 view
->ranges
[pos
] = *range
;
277 memory_region_ref(range
->mr
);
281 static void flatview_destroy(FlatView
*view
)
285 for (i
= 0; i
< view
->nr
; i
++) {
286 memory_region_unref(view
->ranges
[i
].mr
);
288 g_free(view
->ranges
);
292 static void flatview_ref(FlatView
*view
)
294 atomic_inc(&view
->ref
);
297 static void flatview_unref(FlatView
*view
)
299 if (atomic_fetch_dec(&view
->ref
) == 1) {
300 flatview_destroy(view
);
304 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
306 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
308 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
310 int128_make64(r2
->offset_in_region
))
311 && r1
->dirty_log_mask
== r2
->dirty_log_mask
312 && r1
->romd_mode
== r2
->romd_mode
313 && r1
->readonly
== r2
->readonly
;
316 /* Attempt to simplify a view by merging adjacent ranges */
317 static void flatview_simplify(FlatView
*view
)
322 while (i
< view
->nr
) {
325 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
326 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
330 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
331 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
336 static bool memory_region_big_endian(MemoryRegion
*mr
)
338 #ifdef TARGET_WORDS_BIGENDIAN
339 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
341 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
345 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
347 #ifdef TARGET_WORDS_BIGENDIAN
348 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
350 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
354 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
356 if (memory_region_wrong_endianness(mr
)) {
361 *data
= bswap16(*data
);
364 *data
= bswap32(*data
);
367 *data
= bswap64(*data
);
375 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
385 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
386 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
387 *value
|= (tmp
& mask
) << shift
;
391 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
401 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
402 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
403 *value
|= (tmp
& mask
) << shift
;
407 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
418 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
419 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
420 *value
|= (tmp
& mask
) << shift
;
424 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
434 tmp
= (*value
>> shift
) & mask
;
435 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
436 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
440 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
450 tmp
= (*value
>> shift
) & mask
;
451 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
452 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
456 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
466 tmp
= (*value
>> shift
) & mask
;
467 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
468 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
471 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
474 unsigned access_size_min
,
475 unsigned access_size_max
,
476 MemTxResult (*access
)(MemoryRegion
*mr
,
486 uint64_t access_mask
;
487 unsigned access_size
;
489 MemTxResult r
= MEMTX_OK
;
491 if (!access_size_min
) {
494 if (!access_size_max
) {
498 /* FIXME: support unaligned access? */
499 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
500 access_mask
= -1ULL >> (64 - access_size
* 8);
501 if (memory_region_big_endian(mr
)) {
502 for (i
= 0; i
< size
; i
+= access_size
) {
503 r
|= access(mr
, addr
+ i
, value
, access_size
,
504 (size
- access_size
- i
) * 8, access_mask
, attrs
);
507 for (i
= 0; i
< size
; i
+= access_size
) {
508 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
515 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
519 while (mr
->container
) {
522 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
523 if (mr
== as
->root
) {
530 /* Render a memory region into the global view. Ranges in @view obscure
533 static void render_memory_region(FlatView
*view
,
539 MemoryRegion
*subregion
;
541 hwaddr offset_in_region
;
551 int128_addto(&base
, int128_make64(mr
->addr
));
552 readonly
|= mr
->readonly
;
554 tmp
= addrrange_make(base
, mr
->size
);
556 if (!addrrange_intersects(tmp
, clip
)) {
560 clip
= addrrange_intersection(tmp
, clip
);
563 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
564 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
565 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
569 /* Render subregions in priority order. */
570 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
571 render_memory_region(view
, subregion
, base
, clip
, readonly
);
574 if (!mr
->terminates
) {
578 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
583 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
584 fr
.romd_mode
= mr
->romd_mode
;
585 fr
.readonly
= readonly
;
587 /* Render the region itself into any gaps left by the current view. */
588 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
589 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
592 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
593 now
= int128_min(remain
,
594 int128_sub(view
->ranges
[i
].addr
.start
, base
));
595 fr
.offset_in_region
= offset_in_region
;
596 fr
.addr
= addrrange_make(base
, now
);
597 flatview_insert(view
, i
, &fr
);
599 int128_addto(&base
, now
);
600 offset_in_region
+= int128_get64(now
);
601 int128_subfrom(&remain
, now
);
603 now
= int128_sub(int128_min(int128_add(base
, remain
),
604 addrrange_end(view
->ranges
[i
].addr
)),
606 int128_addto(&base
, now
);
607 offset_in_region
+= int128_get64(now
);
608 int128_subfrom(&remain
, now
);
610 if (int128_nz(remain
)) {
611 fr
.offset_in_region
= offset_in_region
;
612 fr
.addr
= addrrange_make(base
, remain
);
613 flatview_insert(view
, i
, &fr
);
617 /* Render a memory topology into a list of disjoint absolute ranges. */
618 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
622 view
= g_new(FlatView
, 1);
626 render_memory_region(view
, mr
, int128_zero(),
627 addrrange_make(int128_zero(), int128_2_64()), false);
629 flatview_simplify(view
);
634 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
635 MemoryRegionIoeventfd
*fds_new
,
637 MemoryRegionIoeventfd
*fds_old
,
641 MemoryRegionIoeventfd
*fd
;
642 MemoryRegionSection section
;
644 /* Generate a symmetric difference of the old and new fd sets, adding
645 * and deleting as necessary.
649 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
650 if (iold
< fds_old_nb
651 && (inew
== fds_new_nb
652 || memory_region_ioeventfd_before(fds_old
[iold
],
655 section
= (MemoryRegionSection
) {
657 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
658 .size
= fd
->addr
.size
,
660 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
661 fd
->match_data
, fd
->data
, fd
->e
);
663 } else if (inew
< fds_new_nb
664 && (iold
== fds_old_nb
665 || memory_region_ioeventfd_before(fds_new
[inew
],
668 section
= (MemoryRegionSection
) {
670 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
671 .size
= fd
->addr
.size
,
673 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
674 fd
->match_data
, fd
->data
, fd
->e
);
683 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
688 view
= atomic_rcu_read(&as
->current_map
);
694 static void address_space_update_ioeventfds(AddressSpace
*as
)
698 unsigned ioeventfd_nb
= 0;
699 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
703 view
= address_space_get_flatview(as
);
704 FOR_EACH_FLAT_RANGE(fr
, view
) {
705 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
706 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
707 int128_sub(fr
->addr
.start
,
708 int128_make64(fr
->offset_in_region
)));
709 if (addrrange_intersects(fr
->addr
, tmp
)) {
711 ioeventfds
= g_realloc(ioeventfds
,
712 ioeventfd_nb
* sizeof(*ioeventfds
));
713 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
714 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
719 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
720 as
->ioeventfds
, as
->ioeventfd_nb
);
722 g_free(as
->ioeventfds
);
723 as
->ioeventfds
= ioeventfds
;
724 as
->ioeventfd_nb
= ioeventfd_nb
;
725 flatview_unref(view
);
728 static void address_space_update_topology_pass(AddressSpace
*as
,
729 const FlatView
*old_view
,
730 const FlatView
*new_view
,
734 FlatRange
*frold
, *frnew
;
736 /* Generate a symmetric difference of the old and new memory maps.
737 * Kill ranges in the old map, and instantiate ranges in the new map.
740 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
741 if (iold
< old_view
->nr
) {
742 frold
= &old_view
->ranges
[iold
];
746 if (inew
< new_view
->nr
) {
747 frnew
= &new_view
->ranges
[inew
];
754 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
755 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
756 && !flatrange_equal(frold
, frnew
)))) {
757 /* In old but not in new, or in both but attributes changed. */
760 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
764 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
765 /* In both and unchanged (except logging may have changed) */
768 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
769 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
770 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
771 frold
->dirty_log_mask
,
772 frnew
->dirty_log_mask
);
774 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
775 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
776 frold
->dirty_log_mask
,
777 frnew
->dirty_log_mask
);
787 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
796 static void address_space_update_topology(AddressSpace
*as
)
798 FlatView
*old_view
= address_space_get_flatview(as
);
799 FlatView
*new_view
= generate_memory_topology(as
->root
);
801 address_space_update_topology_pass(as
, old_view
, new_view
, false);
802 address_space_update_topology_pass(as
, old_view
, new_view
, true);
804 /* Writes are protected by the BQL. */
805 atomic_rcu_set(&as
->current_map
, new_view
);
806 call_rcu(old_view
, flatview_unref
, rcu
);
808 /* Note that all the old MemoryRegions are still alive up to this
809 * point. This relieves most MemoryListeners from the need to
810 * ref/unref the MemoryRegions they get---unless they use them
811 * outside the iothread mutex, in which case precise reference
812 * counting is necessary.
814 flatview_unref(old_view
);
816 address_space_update_ioeventfds(as
);
819 void memory_region_transaction_begin(void)
821 qemu_flush_coalesced_mmio_buffer();
822 ++memory_region_transaction_depth
;
825 static void memory_region_clear_pending(void)
827 memory_region_update_pending
= false;
828 ioeventfd_update_pending
= false;
831 void memory_region_transaction_commit(void)
835 assert(memory_region_transaction_depth
);
836 --memory_region_transaction_depth
;
837 if (!memory_region_transaction_depth
) {
838 if (memory_region_update_pending
) {
839 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
841 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
842 address_space_update_topology(as
);
845 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
846 } else if (ioeventfd_update_pending
) {
847 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
848 address_space_update_ioeventfds(as
);
851 memory_region_clear_pending();
855 static void memory_region_destructor_none(MemoryRegion
*mr
)
859 static void memory_region_destructor_ram(MemoryRegion
*mr
)
861 qemu_ram_free(mr
->ram_addr
);
864 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
866 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
869 static bool memory_region_need_escape(char c
)
871 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
874 static char *memory_region_escape_name(const char *name
)
881 for (p
= name
; *p
; p
++) {
882 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
884 if (bytes
== p
- name
) {
885 return g_memdup(name
, bytes
+ 1);
888 escaped
= g_malloc(bytes
+ 1);
889 for (p
= name
, q
= escaped
; *p
; p
++) {
891 if (unlikely(memory_region_need_escape(c
))) {
894 *q
++ = "0123456789abcdef"[c
>> 4];
895 c
= "0123456789abcdef"[c
& 15];
903 void memory_region_init(MemoryRegion
*mr
,
908 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
909 mr
->size
= int128_make64(size
);
910 if (size
== UINT64_MAX
) {
911 mr
->size
= int128_2_64();
913 mr
->name
= g_strdup(name
);
917 char *escaped_name
= memory_region_escape_name(name
);
918 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
921 owner
= container_get(qdev_get_machine(), "/unattached");
924 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
925 object_unref(OBJECT(mr
));
927 g_free(escaped_name
);
931 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, void *opaque
,
932 const char *name
, Error
**errp
)
934 MemoryRegion
*mr
= MEMORY_REGION(obj
);
935 uint64_t value
= mr
->addr
;
937 visit_type_uint64(v
, &value
, name
, errp
);
940 static void memory_region_get_container(Object
*obj
, Visitor
*v
, void *opaque
,
941 const char *name
, Error
**errp
)
943 MemoryRegion
*mr
= MEMORY_REGION(obj
);
944 gchar
*path
= (gchar
*)"";
947 path
= object_get_canonical_path(OBJECT(mr
->container
));
949 visit_type_str(v
, &path
, name
, errp
);
955 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
958 MemoryRegion
*mr
= MEMORY_REGION(obj
);
960 return OBJECT(mr
->container
);
963 static void memory_region_get_priority(Object
*obj
, Visitor
*v
, void *opaque
,
964 const char *name
, Error
**errp
)
966 MemoryRegion
*mr
= MEMORY_REGION(obj
);
967 int32_t value
= mr
->priority
;
969 visit_type_int32(v
, &value
, name
, errp
);
972 static bool memory_region_get_may_overlap(Object
*obj
, Error
**errp
)
974 MemoryRegion
*mr
= MEMORY_REGION(obj
);
976 return mr
->may_overlap
;
979 static void memory_region_get_size(Object
*obj
, Visitor
*v
, void *opaque
,
980 const char *name
, Error
**errp
)
982 MemoryRegion
*mr
= MEMORY_REGION(obj
);
983 uint64_t value
= memory_region_size(mr
);
985 visit_type_uint64(v
, &value
, name
, errp
);
988 static void memory_region_initfn(Object
*obj
)
990 MemoryRegion
*mr
= MEMORY_REGION(obj
);
993 mr
->ops
= &unassigned_mem_ops
;
994 mr
->ram_addr
= RAM_ADDR_INVALID
;
996 mr
->romd_mode
= true;
997 mr
->global_locking
= true;
998 mr
->destructor
= memory_region_destructor_none
;
999 QTAILQ_INIT(&mr
->subregions
);
1000 QTAILQ_INIT(&mr
->coalesced
);
1002 op
= object_property_add(OBJECT(mr
), "container",
1003 "link<" TYPE_MEMORY_REGION
">",
1004 memory_region_get_container
,
1005 NULL
, /* memory_region_set_container */
1006 NULL
, NULL
, &error_abort
);
1007 op
->resolve
= memory_region_resolve_container
;
1009 object_property_add(OBJECT(mr
), "addr", "uint64",
1010 memory_region_get_addr
,
1011 NULL
, /* memory_region_set_addr */
1012 NULL
, NULL
, &error_abort
);
1013 object_property_add(OBJECT(mr
), "priority", "uint32",
1014 memory_region_get_priority
,
1015 NULL
, /* memory_region_set_priority */
1016 NULL
, NULL
, &error_abort
);
1017 object_property_add_bool(OBJECT(mr
), "may-overlap",
1018 memory_region_get_may_overlap
,
1019 NULL
, /* memory_region_set_may_overlap */
1021 object_property_add(OBJECT(mr
), "size", "uint64",
1022 memory_region_get_size
,
1023 NULL
, /* memory_region_set_size, */
1024 NULL
, NULL
, &error_abort
);
1027 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1030 #ifdef DEBUG_UNASSIGNED
1031 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1033 if (current_cpu
!= NULL
) {
1034 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1039 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1040 uint64_t val
, unsigned size
)
1042 #ifdef DEBUG_UNASSIGNED
1043 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1045 if (current_cpu
!= NULL
) {
1046 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1050 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1051 unsigned size
, bool is_write
)
1056 const MemoryRegionOps unassigned_mem_ops
= {
1057 .valid
.accepts
= unassigned_mem_accepts
,
1058 .endianness
= DEVICE_NATIVE_ENDIAN
,
1061 bool memory_region_access_valid(MemoryRegion
*mr
,
1066 int access_size_min
, access_size_max
;
1069 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1073 if (!mr
->ops
->valid
.accepts
) {
1077 access_size_min
= mr
->ops
->valid
.min_access_size
;
1078 if (!mr
->ops
->valid
.min_access_size
) {
1079 access_size_min
= 1;
1082 access_size_max
= mr
->ops
->valid
.max_access_size
;
1083 if (!mr
->ops
->valid
.max_access_size
) {
1084 access_size_max
= 4;
1087 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1088 for (i
= 0; i
< size
; i
+= access_size
) {
1089 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1098 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1106 if (mr
->ops
->read
) {
1107 return access_with_adjusted_size(addr
, pval
, size
,
1108 mr
->ops
->impl
.min_access_size
,
1109 mr
->ops
->impl
.max_access_size
,
1110 memory_region_read_accessor
,
1112 } else if (mr
->ops
->read_with_attrs
) {
1113 return access_with_adjusted_size(addr
, pval
, size
,
1114 mr
->ops
->impl
.min_access_size
,
1115 mr
->ops
->impl
.max_access_size
,
1116 memory_region_read_with_attrs_accessor
,
1119 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1120 memory_region_oldmmio_read_accessor
,
1125 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1133 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1134 *pval
= unassigned_mem_read(mr
, addr
, size
);
1135 return MEMTX_DECODE_ERROR
;
1138 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1139 adjust_endianness(mr
, pval
, size
);
1143 /* Return true if an eventfd was signalled */
1144 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1150 MemoryRegionIoeventfd ioeventfd
= {
1151 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1156 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1157 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1158 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1160 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1161 event_notifier_set(ioeventfd
.e
);
1169 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1175 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1176 unassigned_mem_write(mr
, addr
, data
, size
);
1177 return MEMTX_DECODE_ERROR
;
1180 adjust_endianness(mr
, &data
, size
);
1182 if ((!kvm_eventfds_enabled()) &&
1183 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1187 if (mr
->ops
->write
) {
1188 return access_with_adjusted_size(addr
, &data
, size
,
1189 mr
->ops
->impl
.min_access_size
,
1190 mr
->ops
->impl
.max_access_size
,
1191 memory_region_write_accessor
, mr
,
1193 } else if (mr
->ops
->write_with_attrs
) {
1195 access_with_adjusted_size(addr
, &data
, size
,
1196 mr
->ops
->impl
.min_access_size
,
1197 mr
->ops
->impl
.max_access_size
,
1198 memory_region_write_with_attrs_accessor
,
1201 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1202 memory_region_oldmmio_write_accessor
,
1207 void memory_region_init_io(MemoryRegion
*mr
,
1209 const MemoryRegionOps
*ops
,
1214 memory_region_init(mr
, owner
, name
, size
);
1215 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1216 mr
->opaque
= opaque
;
1217 mr
->terminates
= true;
1220 void memory_region_init_ram(MemoryRegion
*mr
,
1226 memory_region_init(mr
, owner
, name
, size
);
1228 mr
->terminates
= true;
1229 mr
->destructor
= memory_region_destructor_ram
;
1230 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1231 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1234 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1239 void (*resized
)(const char*,
1244 memory_region_init(mr
, owner
, name
, size
);
1246 mr
->terminates
= true;
1247 mr
->destructor
= memory_region_destructor_ram
;
1248 mr
->ram_addr
= qemu_ram_alloc_resizeable(size
, max_size
, resized
, mr
, errp
);
1249 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1253 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1254 struct Object
*owner
,
1261 memory_region_init(mr
, owner
, name
, size
);
1263 mr
->terminates
= true;
1264 mr
->destructor
= memory_region_destructor_ram
;
1265 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1266 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1270 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1276 memory_region_init(mr
, owner
, name
, size
);
1278 mr
->terminates
= true;
1279 mr
->destructor
= memory_region_destructor_ram
;
1280 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1282 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1283 assert(ptr
!= NULL
);
1284 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1287 void memory_region_set_skip_dump(MemoryRegion
*mr
)
1289 mr
->skip_dump
= true;
1292 void memory_region_init_alias(MemoryRegion
*mr
,
1299 memory_region_init(mr
, owner
, name
, size
);
1301 mr
->alias_offset
= offset
;
1304 void memory_region_init_rom_device(MemoryRegion
*mr
,
1306 const MemoryRegionOps
*ops
,
1312 memory_region_init(mr
, owner
, name
, size
);
1314 mr
->opaque
= opaque
;
1315 mr
->terminates
= true;
1316 mr
->rom_device
= true;
1317 mr
->destructor
= memory_region_destructor_rom_device
;
1318 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1321 void memory_region_init_iommu(MemoryRegion
*mr
,
1323 const MemoryRegionIOMMUOps
*ops
,
1327 memory_region_init(mr
, owner
, name
, size
);
1328 mr
->iommu_ops
= ops
,
1329 mr
->terminates
= true; /* then re-forwards */
1330 notifier_list_init(&mr
->iommu_notify
);
1333 static void memory_region_finalize(Object
*obj
)
1335 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1337 assert(!mr
->container
);
1339 /* We know the region is not visible in any address space (it
1340 * does not have a container and cannot be a root either because
1341 * it has no references, so we can blindly clear mr->enabled.
1342 * memory_region_set_enabled instead could trigger a transaction
1343 * and cause an infinite loop.
1345 mr
->enabled
= false;
1346 memory_region_transaction_begin();
1347 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1348 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1349 memory_region_del_subregion(mr
, subregion
);
1351 memory_region_transaction_commit();
1354 memory_region_clear_coalescing(mr
);
1355 g_free((char *)mr
->name
);
1356 g_free(mr
->ioeventfds
);
1359 Object
*memory_region_owner(MemoryRegion
*mr
)
1361 Object
*obj
= OBJECT(mr
);
1365 void memory_region_ref(MemoryRegion
*mr
)
1367 /* MMIO callbacks most likely will access data that belongs
1368 * to the owner, hence the need to ref/unref the owner whenever
1369 * the memory region is in use.
1371 * The memory region is a child of its owner. As long as the
1372 * owner doesn't call unparent itself on the memory region,
1373 * ref-ing the owner will also keep the memory region alive.
1374 * Memory regions without an owner are supposed to never go away;
1375 * we do not ref/unref them because it slows down DMA sensibly.
1377 if (mr
&& mr
->owner
) {
1378 object_ref(mr
->owner
);
1382 void memory_region_unref(MemoryRegion
*mr
)
1384 if (mr
&& mr
->owner
) {
1385 object_unref(mr
->owner
);
1389 uint64_t memory_region_size(MemoryRegion
*mr
)
1391 if (int128_eq(mr
->size
, int128_2_64())) {
1394 return int128_get64(mr
->size
);
1397 const char *memory_region_name(const MemoryRegion
*mr
)
1400 ((MemoryRegion
*)mr
)->name
=
1401 object_get_canonical_path_component(OBJECT(mr
));
1406 bool memory_region_is_skip_dump(MemoryRegion
*mr
)
1408 return mr
->skip_dump
;
1411 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1413 uint8_t mask
= mr
->dirty_log_mask
;
1414 if (global_dirty_log
) {
1415 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1420 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1422 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1425 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1427 notifier_list_add(&mr
->iommu_notify
, n
);
1430 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
,
1431 hwaddr granularity
, bool is_write
)
1434 IOMMUTLBEntry iotlb
;
1436 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1437 iotlb
= mr
->iommu_ops
->translate(mr
, addr
, is_write
);
1438 if (iotlb
.perm
!= IOMMU_NONE
) {
1439 n
->notify(n
, &iotlb
);
1442 /* if (2^64 - MR size) < granularity, it's possible to get an
1443 * infinite loop here. This should catch such a wraparound */
1444 if ((addr
+ granularity
) < addr
) {
1450 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1455 void memory_region_notify_iommu(MemoryRegion
*mr
,
1456 IOMMUTLBEntry entry
)
1458 assert(memory_region_is_iommu(mr
));
1459 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1462 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1464 uint8_t mask
= 1 << client
;
1465 uint8_t old_logging
;
1467 assert(client
== DIRTY_MEMORY_VGA
);
1468 old_logging
= mr
->vga_logging_count
;
1469 mr
->vga_logging_count
+= log
? 1 : -1;
1470 if (!!old_logging
== !!mr
->vga_logging_count
) {
1474 memory_region_transaction_begin();
1475 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1476 memory_region_update_pending
|= mr
->enabled
;
1477 memory_region_transaction_commit();
1480 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1481 hwaddr size
, unsigned client
)
1483 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1484 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1487 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1490 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1491 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
,
1492 memory_region_get_dirty_log_mask(mr
));
1495 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1496 hwaddr size
, unsigned client
)
1498 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1499 return cpu_physical_memory_test_and_clear_dirty(mr
->ram_addr
+ addr
,
1504 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1509 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1510 FlatView
*view
= address_space_get_flatview(as
);
1511 FOR_EACH_FLAT_RANGE(fr
, view
) {
1513 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1516 flatview_unref(view
);
1520 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1522 if (mr
->readonly
!= readonly
) {
1523 memory_region_transaction_begin();
1524 mr
->readonly
= readonly
;
1525 memory_region_update_pending
|= mr
->enabled
;
1526 memory_region_transaction_commit();
1530 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1532 if (mr
->romd_mode
!= romd_mode
) {
1533 memory_region_transaction_begin();
1534 mr
->romd_mode
= romd_mode
;
1535 memory_region_update_pending
|= mr
->enabled
;
1536 memory_region_transaction_commit();
1540 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1541 hwaddr size
, unsigned client
)
1543 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1544 cpu_physical_memory_test_and_clear_dirty(mr
->ram_addr
+ addr
, size
,
1548 int memory_region_get_fd(MemoryRegion
*mr
)
1551 return memory_region_get_fd(mr
->alias
);
1554 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1556 return qemu_get_ram_fd(mr
->ram_addr
& TARGET_PAGE_MASK
);
1559 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1562 uint64_t offset
= 0;
1566 offset
+= mr
->alias_offset
;
1569 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1570 ptr
= qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1573 return ptr
+ offset
;
1576 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1578 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1580 qemu_ram_resize(mr
->ram_addr
, newsize
, errp
);
1583 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1587 CoalescedMemoryRange
*cmr
;
1589 MemoryRegionSection section
;
1591 view
= address_space_get_flatview(as
);
1592 FOR_EACH_FLAT_RANGE(fr
, view
) {
1594 section
= (MemoryRegionSection
) {
1595 .address_space
= as
,
1596 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1597 .size
= fr
->addr
.size
,
1600 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1601 int128_get64(fr
->addr
.start
),
1602 int128_get64(fr
->addr
.size
));
1603 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1604 tmp
= addrrange_shift(cmr
->addr
,
1605 int128_sub(fr
->addr
.start
,
1606 int128_make64(fr
->offset_in_region
)));
1607 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1610 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1611 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1612 int128_get64(tmp
.start
),
1613 int128_get64(tmp
.size
));
1617 flatview_unref(view
);
1620 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1624 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1625 memory_region_update_coalesced_range_as(mr
, as
);
1629 void memory_region_set_coalescing(MemoryRegion
*mr
)
1631 memory_region_clear_coalescing(mr
);
1632 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1635 void memory_region_add_coalescing(MemoryRegion
*mr
,
1639 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1641 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1642 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1643 memory_region_update_coalesced_range(mr
);
1644 memory_region_set_flush_coalesced(mr
);
1647 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1649 CoalescedMemoryRange
*cmr
;
1650 bool updated
= false;
1652 qemu_flush_coalesced_mmio_buffer();
1653 mr
->flush_coalesced_mmio
= false;
1655 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1656 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1657 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1663 memory_region_update_coalesced_range(mr
);
1667 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1669 mr
->flush_coalesced_mmio
= true;
1672 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1674 qemu_flush_coalesced_mmio_buffer();
1675 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1676 mr
->flush_coalesced_mmio
= false;
1680 void memory_region_set_global_locking(MemoryRegion
*mr
)
1682 mr
->global_locking
= true;
1685 void memory_region_clear_global_locking(MemoryRegion
*mr
)
1687 mr
->global_locking
= false;
1690 static bool userspace_eventfd_warning
;
1692 void memory_region_add_eventfd(MemoryRegion
*mr
,
1699 MemoryRegionIoeventfd mrfd
= {
1700 .addr
.start
= int128_make64(addr
),
1701 .addr
.size
= int128_make64(size
),
1702 .match_data
= match_data
,
1708 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1709 userspace_eventfd_warning
))) {
1710 userspace_eventfd_warning
= true;
1711 error_report("Using eventfd without MMIO binding in KVM. "
1712 "Suboptimal performance expected");
1716 adjust_endianness(mr
, &mrfd
.data
, size
);
1718 memory_region_transaction_begin();
1719 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1720 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1725 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1726 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1727 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1728 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1729 mr
->ioeventfds
[i
] = mrfd
;
1730 ioeventfd_update_pending
|= mr
->enabled
;
1731 memory_region_transaction_commit();
1734 void memory_region_del_eventfd(MemoryRegion
*mr
,
1741 MemoryRegionIoeventfd mrfd
= {
1742 .addr
.start
= int128_make64(addr
),
1743 .addr
.size
= int128_make64(size
),
1744 .match_data
= match_data
,
1751 adjust_endianness(mr
, &mrfd
.data
, size
);
1753 memory_region_transaction_begin();
1754 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1755 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1759 assert(i
!= mr
->ioeventfd_nb
);
1760 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1761 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1763 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1764 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1765 ioeventfd_update_pending
|= mr
->enabled
;
1766 memory_region_transaction_commit();
1769 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1771 hwaddr offset
= subregion
->addr
;
1772 MemoryRegion
*mr
= subregion
->container
;
1773 MemoryRegion
*other
;
1775 memory_region_transaction_begin();
1777 memory_region_ref(subregion
);
1778 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1779 if (subregion
->may_overlap
|| other
->may_overlap
) {
1782 if (int128_ge(int128_make64(offset
),
1783 int128_add(int128_make64(other
->addr
), other
->size
))
1784 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1785 int128_make64(other
->addr
))) {
1789 printf("warning: subregion collision %llx/%llx (%s) "
1790 "vs %llx/%llx (%s)\n",
1791 (unsigned long long)offset
,
1792 (unsigned long long)int128_get64(subregion
->size
),
1794 (unsigned long long)other
->addr
,
1795 (unsigned long long)int128_get64(other
->size
),
1799 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1800 if (subregion
->priority
>= other
->priority
) {
1801 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1805 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1807 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1808 memory_region_transaction_commit();
1811 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1813 MemoryRegion
*subregion
)
1815 assert(!subregion
->container
);
1816 subregion
->container
= mr
;
1817 subregion
->addr
= offset
;
1818 memory_region_update_container_subregions(subregion
);
1821 void memory_region_add_subregion(MemoryRegion
*mr
,
1823 MemoryRegion
*subregion
)
1825 subregion
->may_overlap
= false;
1826 subregion
->priority
= 0;
1827 memory_region_add_subregion_common(mr
, offset
, subregion
);
1830 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1832 MemoryRegion
*subregion
,
1835 subregion
->may_overlap
= true;
1836 subregion
->priority
= priority
;
1837 memory_region_add_subregion_common(mr
, offset
, subregion
);
1840 void memory_region_del_subregion(MemoryRegion
*mr
,
1841 MemoryRegion
*subregion
)
1843 memory_region_transaction_begin();
1844 assert(subregion
->container
== mr
);
1845 subregion
->container
= NULL
;
1846 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1847 memory_region_unref(subregion
);
1848 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1849 memory_region_transaction_commit();
1852 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1854 if (enabled
== mr
->enabled
) {
1857 memory_region_transaction_begin();
1858 mr
->enabled
= enabled
;
1859 memory_region_update_pending
= true;
1860 memory_region_transaction_commit();
1863 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
1865 Int128 s
= int128_make64(size
);
1867 if (size
== UINT64_MAX
) {
1870 if (int128_eq(s
, mr
->size
)) {
1873 memory_region_transaction_begin();
1875 memory_region_update_pending
= true;
1876 memory_region_transaction_commit();
1879 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1881 MemoryRegion
*container
= mr
->container
;
1884 memory_region_transaction_begin();
1885 memory_region_ref(mr
);
1886 memory_region_del_subregion(container
, mr
);
1887 mr
->container
= container
;
1888 memory_region_update_container_subregions(mr
);
1889 memory_region_unref(mr
);
1890 memory_region_transaction_commit();
1894 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1896 if (addr
!= mr
->addr
) {
1898 memory_region_readd_subregion(mr
);
1902 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1906 if (offset
== mr
->alias_offset
) {
1910 memory_region_transaction_begin();
1911 mr
->alias_offset
= offset
;
1912 memory_region_update_pending
|= mr
->enabled
;
1913 memory_region_transaction_commit();
1916 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
1921 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1923 const AddrRange
*addr
= addr_
;
1924 const FlatRange
*fr
= fr_
;
1926 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1928 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1934 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1936 return bsearch(&addr
, view
->ranges
, view
->nr
,
1937 sizeof(FlatRange
), cmp_flatrange_addr
);
1940 bool memory_region_is_mapped(MemoryRegion
*mr
)
1942 return mr
->container
? true : false;
1945 /* Same as memory_region_find, but it does not add a reference to the
1946 * returned region. It must be called from an RCU critical section.
1948 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
1949 hwaddr addr
, uint64_t size
)
1951 MemoryRegionSection ret
= { .mr
= NULL
};
1959 for (root
= mr
; root
->container
; ) {
1960 root
= root
->container
;
1964 as
= memory_region_to_address_space(root
);
1968 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1970 view
= atomic_rcu_read(&as
->current_map
);
1971 fr
= flatview_lookup(view
, range
);
1976 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1981 ret
.address_space
= as
;
1982 range
= addrrange_intersection(range
, fr
->addr
);
1983 ret
.offset_within_region
= fr
->offset_in_region
;
1984 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1986 ret
.size
= range
.size
;
1987 ret
.offset_within_address_space
= int128_get64(range
.start
);
1988 ret
.readonly
= fr
->readonly
;
1992 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1993 hwaddr addr
, uint64_t size
)
1995 MemoryRegionSection ret
;
1997 ret
= memory_region_find_rcu(mr
, addr
, size
);
1999 memory_region_ref(ret
.mr
);
2005 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2010 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2012 return mr
&& mr
!= container
;
2015 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
2020 view
= address_space_get_flatview(as
);
2021 FOR_EACH_FLAT_RANGE(fr
, view
) {
2022 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
2024 flatview_unref(view
);
2027 void memory_global_dirty_log_start(void)
2029 global_dirty_log
= true;
2031 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2033 /* Refresh DIRTY_LOG_MIGRATION bit. */
2034 memory_region_transaction_begin();
2035 memory_region_update_pending
= true;
2036 memory_region_transaction_commit();
2039 void memory_global_dirty_log_stop(void)
2041 global_dirty_log
= false;
2043 /* Refresh DIRTY_LOG_MIGRATION bit. */
2044 memory_region_transaction_begin();
2045 memory_region_update_pending
= true;
2046 memory_region_transaction_commit();
2048 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2051 static void listener_add_address_space(MemoryListener
*listener
,
2057 if (listener
->address_space_filter
2058 && listener
->address_space_filter
!= as
) {
2062 if (listener
->begin
) {
2063 listener
->begin(listener
);
2065 if (global_dirty_log
) {
2066 if (listener
->log_global_start
) {
2067 listener
->log_global_start(listener
);
2071 view
= address_space_get_flatview(as
);
2072 FOR_EACH_FLAT_RANGE(fr
, view
) {
2073 MemoryRegionSection section
= {
2075 .address_space
= as
,
2076 .offset_within_region
= fr
->offset_in_region
,
2077 .size
= fr
->addr
.size
,
2078 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2079 .readonly
= fr
->readonly
,
2081 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2082 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2084 if (listener
->region_add
) {
2085 listener
->region_add(listener
, §ion
);
2088 if (listener
->commit
) {
2089 listener
->commit(listener
);
2091 flatview_unref(view
);
2094 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
2096 MemoryListener
*other
= NULL
;
2099 listener
->address_space_filter
= filter
;
2100 if (QTAILQ_EMPTY(&memory_listeners
)
2101 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2102 memory_listeners
)->priority
) {
2103 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2105 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2106 if (listener
->priority
< other
->priority
) {
2110 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2113 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2114 listener_add_address_space(listener
, as
);
2118 void memory_listener_unregister(MemoryListener
*listener
)
2120 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2123 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2125 memory_region_ref(root
);
2126 memory_region_transaction_begin();
2129 as
->malloced
= false;
2130 as
->current_map
= g_new(FlatView
, 1);
2131 flatview_init(as
->current_map
);
2132 as
->ioeventfd_nb
= 0;
2133 as
->ioeventfds
= NULL
;
2134 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2135 as
->name
= g_strdup(name
? name
: "anonymous");
2136 address_space_init_dispatch(as
);
2137 memory_region_update_pending
|= root
->enabled
;
2138 memory_region_transaction_commit();
2141 static void do_address_space_destroy(AddressSpace
*as
)
2143 MemoryListener
*listener
;
2144 bool do_free
= as
->malloced
;
2146 address_space_destroy_dispatch(as
);
2148 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2149 assert(listener
->address_space_filter
!= as
);
2152 flatview_unref(as
->current_map
);
2154 g_free(as
->ioeventfds
);
2155 memory_region_unref(as
->root
);
2161 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
, const char *name
)
2165 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2166 if (root
== as
->root
&& as
->malloced
) {
2172 as
= g_malloc0(sizeof *as
);
2173 address_space_init(as
, root
, name
);
2174 as
->malloced
= true;
2178 void address_space_destroy(AddressSpace
*as
)
2180 MemoryRegion
*root
= as
->root
;
2183 if (as
->ref_count
) {
2186 /* Flush out anything from MemoryListeners listening in on this */
2187 memory_region_transaction_begin();
2189 memory_region_transaction_commit();
2190 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2191 address_space_unregister(as
);
2193 /* At this point, as->dispatch and as->current_map are dummy
2194 * entries that the guest should never use. Wait for the old
2195 * values to expire before freeing the data.
2198 call_rcu(as
, do_address_space_destroy
, rcu
);
2201 typedef struct MemoryRegionList MemoryRegionList
;
2203 struct MemoryRegionList
{
2204 const MemoryRegion
*mr
;
2205 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2208 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2210 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2211 const MemoryRegion
*mr
, unsigned int level
,
2213 MemoryRegionListHead
*alias_print_queue
)
2215 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2216 MemoryRegionListHead submr_print_queue
;
2217 const MemoryRegion
*submr
;
2224 for (i
= 0; i
< level
; i
++) {
2229 MemoryRegionList
*ml
;
2232 /* check if the alias is already in the queue */
2233 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2234 if (ml
->mr
== mr
->alias
) {
2240 ml
= g_new(MemoryRegionList
, 1);
2242 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2244 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2245 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2246 "-" TARGET_FMT_plx
"%s\n",
2249 + (int128_nz(mr
->size
) ?
2250 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2251 int128_one())) : 0),
2253 mr
->romd_mode
? 'R' : '-',
2254 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2256 memory_region_name(mr
),
2257 memory_region_name(mr
->alias
),
2260 + (int128_nz(mr
->size
) ?
2261 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2262 int128_one())) : 0),
2263 mr
->enabled
? "" : " [disabled]");
2266 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s%s\n",
2269 + (int128_nz(mr
->size
) ?
2270 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2271 int128_one())) : 0),
2273 mr
->romd_mode
? 'R' : '-',
2274 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2276 memory_region_name(mr
),
2277 mr
->enabled
? "" : " [disabled]");
2280 QTAILQ_INIT(&submr_print_queue
);
2282 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2283 new_ml
= g_new(MemoryRegionList
, 1);
2285 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2286 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2287 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2288 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2289 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2295 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2299 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2300 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2304 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2309 void mtree_info(fprintf_function mon_printf
, void *f
)
2311 MemoryRegionListHead ml_head
;
2312 MemoryRegionList
*ml
, *ml2
;
2315 QTAILQ_INIT(&ml_head
);
2317 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2318 mon_printf(f
, "address-space: %s\n", as
->name
);
2319 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2320 mon_printf(f
, "\n");
2323 /* print aliased regions */
2324 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2325 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2326 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2327 mon_printf(f
, "\n");
2330 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2335 static const TypeInfo memory_region_info
= {
2336 .parent
= TYPE_OBJECT
,
2337 .name
= TYPE_MEMORY_REGION
,
2338 .instance_size
= sizeof(MemoryRegion
),
2339 .instance_init
= memory_region_initfn
,
2340 .instance_finalize
= memory_region_finalize
,
2343 static void memory_register_types(void)
2345 type_register_static(&memory_region_info
);
2348 type_init(memory_register_types
)