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 "qemu/osdep.h"
18 #include "qapi/error.h"
19 #include "exec/memory.h"
20 #include "qapi/visitor.h"
21 #include "qemu/bitops.h"
22 #include "qemu/error-report.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/qemu-print.h"
25 #include "qom/object.h"
28 #include "exec/memory-internal.h"
29 #include "exec/ram_addr.h"
30 #include "sysemu/kvm.h"
31 #include "sysemu/runstate.h"
32 #include "sysemu/tcg.h"
33 #include "qemu/accel.h"
34 #include "hw/boards.h"
35 #include "migration/vmstate.h"
37 //#define DEBUG_UNASSIGNED
39 static unsigned memory_region_transaction_depth
;
40 static bool memory_region_update_pending
;
41 static bool ioeventfd_update_pending
;
42 bool global_dirty_log
;
44 static QTAILQ_HEAD(, MemoryListener
) memory_listeners
45 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
47 static QTAILQ_HEAD(, AddressSpace
) address_spaces
48 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
50 static GHashTable
*flat_views
;
52 typedef struct AddrRange AddrRange
;
55 * Note that signed integers are needed for negative offsetting in aliases
56 * (large MemoryRegion::alias_offset).
63 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
65 return (AddrRange
) { start
, size
};
68 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
70 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
73 static Int128
addrrange_end(AddrRange r
)
75 return int128_add(r
.start
, r
.size
);
78 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
80 int128_addto(&range
.start
, delta
);
84 static bool addrrange_contains(AddrRange range
, Int128 addr
)
86 return int128_ge(addr
, range
.start
)
87 && int128_lt(addr
, addrrange_end(range
));
90 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
92 return addrrange_contains(r1
, r2
.start
)
93 || addrrange_contains(r2
, r1
.start
);
96 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
98 Int128 start
= int128_max(r1
.start
, r2
.start
);
99 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
100 return addrrange_make(start
, int128_sub(end
, start
));
103 enum ListenerDirection
{ Forward
, Reverse
};
105 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
107 MemoryListener *_listener; \
109 switch (_direction) { \
111 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
112 if (_listener->_callback) { \
113 _listener->_callback(_listener, ##_args); \
118 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, link) { \
119 if (_listener->_callback) { \
120 _listener->_callback(_listener, ##_args); \
129 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
131 MemoryListener *_listener; \
133 switch (_direction) { \
135 QTAILQ_FOREACH(_listener, &(_as)->listeners, link_as) { \
136 if (_listener->_callback) { \
137 _listener->_callback(_listener, _section, ##_args); \
142 QTAILQ_FOREACH_REVERSE(_listener, &(_as)->listeners, link_as) { \
143 if (_listener->_callback) { \
144 _listener->_callback(_listener, _section, ##_args); \
153 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
154 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
156 MemoryRegionSection mrs = section_from_flat_range(fr, \
157 address_space_to_flatview(as)); \
158 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
161 struct CoalescedMemoryRange
{
163 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
166 struct MemoryRegionIoeventfd
{
173 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd
*a
,
174 MemoryRegionIoeventfd
*b
)
176 if (int128_lt(a
->addr
.start
, b
->addr
.start
)) {
178 } else if (int128_gt(a
->addr
.start
, b
->addr
.start
)) {
180 } else if (int128_lt(a
->addr
.size
, b
->addr
.size
)) {
182 } else if (int128_gt(a
->addr
.size
, b
->addr
.size
)) {
184 } else if (a
->match_data
< b
->match_data
) {
186 } else if (a
->match_data
> b
->match_data
) {
188 } else if (a
->match_data
) {
189 if (a
->data
< b
->data
) {
191 } else if (a
->data
> b
->data
) {
197 } else if (a
->e
> b
->e
) {
203 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd
*a
,
204 MemoryRegionIoeventfd
*b
)
206 if (int128_eq(a
->addr
.start
, b
->addr
.start
) &&
207 (!int128_nz(a
->addr
.size
) || !int128_nz(b
->addr
.size
) ||
208 (int128_eq(a
->addr
.size
, b
->addr
.size
) &&
209 (a
->match_data
== b
->match_data
) &&
210 ((a
->match_data
&& (a
->data
== b
->data
)) || !a
->match_data
) &&
217 /* Range of memory in the global map. Addresses are absolute. */
220 hwaddr offset_in_region
;
222 uint8_t dirty_log_mask
;
228 #define FOR_EACH_FLAT_RANGE(var, view) \
229 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
231 static inline MemoryRegionSection
232 section_from_flat_range(FlatRange
*fr
, FlatView
*fv
)
234 return (MemoryRegionSection
) {
237 .offset_within_region
= fr
->offset_in_region
,
238 .size
= fr
->addr
.size
,
239 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
240 .readonly
= fr
->readonly
,
241 .nonvolatile
= fr
->nonvolatile
,
245 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
247 return a
->mr
== b
->mr
248 && addrrange_equal(a
->addr
, b
->addr
)
249 && a
->offset_in_region
== b
->offset_in_region
250 && a
->romd_mode
== b
->romd_mode
251 && a
->readonly
== b
->readonly
252 && a
->nonvolatile
== b
->nonvolatile
;
255 static FlatView
*flatview_new(MemoryRegion
*mr_root
)
259 view
= g_new0(FlatView
, 1);
261 view
->root
= mr_root
;
262 memory_region_ref(mr_root
);
263 trace_flatview_new(view
, mr_root
);
268 /* Insert a range into a given position. Caller is responsible for maintaining
271 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
273 if (view
->nr
== view
->nr_allocated
) {
274 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
275 view
->ranges
= g_realloc(view
->ranges
,
276 view
->nr_allocated
* sizeof(*view
->ranges
));
278 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
279 (view
->nr
- pos
) * sizeof(FlatRange
));
280 view
->ranges
[pos
] = *range
;
281 memory_region_ref(range
->mr
);
285 static void flatview_destroy(FlatView
*view
)
289 trace_flatview_destroy(view
, view
->root
);
290 if (view
->dispatch
) {
291 address_space_dispatch_free(view
->dispatch
);
293 for (i
= 0; i
< view
->nr
; i
++) {
294 memory_region_unref(view
->ranges
[i
].mr
);
296 g_free(view
->ranges
);
297 memory_region_unref(view
->root
);
301 static bool flatview_ref(FlatView
*view
)
303 return qatomic_fetch_inc_nonzero(&view
->ref
) > 0;
306 void flatview_unref(FlatView
*view
)
308 if (qatomic_fetch_dec(&view
->ref
) == 1) {
309 trace_flatview_destroy_rcu(view
, view
->root
);
311 call_rcu(view
, flatview_destroy
, rcu
);
315 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
317 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
319 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
321 int128_make64(r2
->offset_in_region
))
322 && r1
->dirty_log_mask
== r2
->dirty_log_mask
323 && r1
->romd_mode
== r2
->romd_mode
324 && r1
->readonly
== r2
->readonly
325 && r1
->nonvolatile
== r2
->nonvolatile
;
328 /* Attempt to simplify a view by merging adjacent ranges */
329 static void flatview_simplify(FlatView
*view
)
334 while (i
< view
->nr
) {
337 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
338 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
342 for (k
= i
; k
< j
; k
++) {
343 memory_region_unref(view
->ranges
[k
].mr
);
345 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
346 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
351 static bool memory_region_big_endian(MemoryRegion
*mr
)
353 #ifdef TARGET_WORDS_BIGENDIAN
354 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
356 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
360 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, MemOp op
)
362 if ((op
& MO_BSWAP
) != devend_memop(mr
->ops
->endianness
)) {
363 switch (op
& MO_SIZE
) {
367 *data
= bswap16(*data
);
370 *data
= bswap32(*data
);
373 *data
= bswap64(*data
);
376 g_assert_not_reached();
381 static inline void memory_region_shift_read_access(uint64_t *value
,
387 *value
|= (tmp
& mask
) << shift
;
389 *value
|= (tmp
& mask
) >> -shift
;
393 static inline uint64_t memory_region_shift_write_access(uint64_t *value
,
400 tmp
= (*value
>> shift
) & mask
;
402 tmp
= (*value
<< -shift
) & mask
;
408 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
411 hwaddr abs_addr
= offset
;
413 abs_addr
+= mr
->addr
;
414 for (root
= mr
; root
->container
; ) {
415 root
= root
->container
;
416 abs_addr
+= root
->addr
;
422 static int get_cpu_index(void)
425 return current_cpu
->cpu_index
;
430 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
440 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
442 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
443 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_READ
)) {
444 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
445 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
446 memory_region_name(mr
));
448 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
452 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
463 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
465 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
466 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_READ
)) {
467 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
468 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
469 memory_region_name(mr
));
471 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
475 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
483 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
486 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
487 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_WRITE
)) {
488 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
489 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
490 memory_region_name(mr
));
492 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
496 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
504 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
507 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
508 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_WRITE
)) {
509 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
510 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
511 memory_region_name(mr
));
513 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
516 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
519 unsigned access_size_min
,
520 unsigned access_size_max
,
521 MemTxResult (*access_fn
)
532 uint64_t access_mask
;
533 unsigned access_size
;
535 MemTxResult r
= MEMTX_OK
;
537 if (!access_size_min
) {
540 if (!access_size_max
) {
544 /* FIXME: support unaligned access? */
545 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
546 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
547 if (memory_region_big_endian(mr
)) {
548 for (i
= 0; i
< size
; i
+= access_size
) {
549 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
550 (size
- access_size
- i
) * 8, access_mask
, attrs
);
553 for (i
= 0; i
< size
; i
+= access_size
) {
554 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
561 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
565 while (mr
->container
) {
568 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
569 if (mr
== as
->root
) {
576 /* Render a memory region into the global view. Ranges in @view obscure
579 static void render_memory_region(FlatView
*view
,
586 MemoryRegion
*subregion
;
588 hwaddr offset_in_region
;
598 int128_addto(&base
, int128_make64(mr
->addr
));
599 readonly
|= mr
->readonly
;
600 nonvolatile
|= mr
->nonvolatile
;
602 tmp
= addrrange_make(base
, mr
->size
);
604 if (!addrrange_intersects(tmp
, clip
)) {
608 clip
= addrrange_intersection(tmp
, clip
);
611 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
612 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
613 render_memory_region(view
, mr
->alias
, base
, clip
,
614 readonly
, nonvolatile
);
618 /* Render subregions in priority order. */
619 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
620 render_memory_region(view
, subregion
, base
, clip
,
621 readonly
, nonvolatile
);
624 if (!mr
->terminates
) {
628 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
633 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
634 fr
.romd_mode
= mr
->romd_mode
;
635 fr
.readonly
= readonly
;
636 fr
.nonvolatile
= nonvolatile
;
638 /* Render the region itself into any gaps left by the current view. */
639 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
640 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
643 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
644 now
= int128_min(remain
,
645 int128_sub(view
->ranges
[i
].addr
.start
, base
));
646 fr
.offset_in_region
= offset_in_region
;
647 fr
.addr
= addrrange_make(base
, now
);
648 flatview_insert(view
, i
, &fr
);
650 int128_addto(&base
, now
);
651 offset_in_region
+= int128_get64(now
);
652 int128_subfrom(&remain
, now
);
654 now
= int128_sub(int128_min(int128_add(base
, remain
),
655 addrrange_end(view
->ranges
[i
].addr
)),
657 int128_addto(&base
, now
);
658 offset_in_region
+= int128_get64(now
);
659 int128_subfrom(&remain
, now
);
661 if (int128_nz(remain
)) {
662 fr
.offset_in_region
= offset_in_region
;
663 fr
.addr
= addrrange_make(base
, remain
);
664 flatview_insert(view
, i
, &fr
);
668 void flatview_for_each_range(FlatView
*fv
, flatview_cb cb
, void *opaque
)
675 FOR_EACH_FLAT_RANGE(fr
, fv
) {
676 if (cb(fr
->addr
.start
, fr
->addr
.size
, fr
->mr
,
677 fr
->offset_in_region
, opaque
)) {
683 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
685 while (mr
->enabled
) {
687 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
688 /* The alias is included in its entirety. Use it as
689 * the "real" root, so that we can share more FlatViews.
694 } else if (!mr
->terminates
) {
695 unsigned int found
= 0;
696 MemoryRegion
*child
, *next
= NULL
;
697 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
698 if (child
->enabled
) {
703 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
704 /* A child is included in its entirety. If it's the only
705 * enabled one, use it in the hope of finding an alias down the
706 * way. This will also let us share FlatViews.
727 /* Render a memory topology into a list of disjoint absolute ranges. */
728 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
733 view
= flatview_new(mr
);
736 render_memory_region(view
, mr
, int128_zero(),
737 addrrange_make(int128_zero(), int128_2_64()),
740 flatview_simplify(view
);
742 view
->dispatch
= address_space_dispatch_new(view
);
743 for (i
= 0; i
< view
->nr
; i
++) {
744 MemoryRegionSection mrs
=
745 section_from_flat_range(&view
->ranges
[i
], view
);
746 flatview_add_to_dispatch(view
, &mrs
);
748 address_space_dispatch_compact(view
->dispatch
);
749 g_hash_table_replace(flat_views
, mr
, view
);
754 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
755 MemoryRegionIoeventfd
*fds_new
,
757 MemoryRegionIoeventfd
*fds_old
,
761 MemoryRegionIoeventfd
*fd
;
762 MemoryRegionSection section
;
764 /* Generate a symmetric difference of the old and new fd sets, adding
765 * and deleting as necessary.
769 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
770 if (iold
< fds_old_nb
771 && (inew
== fds_new_nb
772 || memory_region_ioeventfd_before(&fds_old
[iold
],
775 section
= (MemoryRegionSection
) {
776 .fv
= address_space_to_flatview(as
),
777 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
778 .size
= fd
->addr
.size
,
780 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
781 fd
->match_data
, fd
->data
, fd
->e
);
783 } else if (inew
< fds_new_nb
784 && (iold
== fds_old_nb
785 || memory_region_ioeventfd_before(&fds_new
[inew
],
788 section
= (MemoryRegionSection
) {
789 .fv
= address_space_to_flatview(as
),
790 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
791 .size
= fd
->addr
.size
,
793 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
794 fd
->match_data
, fd
->data
, fd
->e
);
803 FlatView
*address_space_get_flatview(AddressSpace
*as
)
807 RCU_READ_LOCK_GUARD();
809 view
= address_space_to_flatview(as
);
810 /* If somebody has replaced as->current_map concurrently,
811 * flatview_ref returns false.
813 } while (!flatview_ref(view
));
817 static void address_space_update_ioeventfds(AddressSpace
*as
)
821 unsigned ioeventfd_nb
= 0;
822 unsigned ioeventfd_max
;
823 MemoryRegionIoeventfd
*ioeventfds
;
828 * It is likely that the number of ioeventfds hasn't changed much, so use
829 * the previous size as the starting value, with some headroom to avoid
830 * gratuitous reallocations.
832 ioeventfd_max
= QEMU_ALIGN_UP(as
->ioeventfd_nb
, 4);
833 ioeventfds
= g_new(MemoryRegionIoeventfd
, ioeventfd_max
);
835 view
= address_space_get_flatview(as
);
836 FOR_EACH_FLAT_RANGE(fr
, view
) {
837 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
838 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
839 int128_sub(fr
->addr
.start
,
840 int128_make64(fr
->offset_in_region
)));
841 if (addrrange_intersects(fr
->addr
, tmp
)) {
843 if (ioeventfd_nb
> ioeventfd_max
) {
844 ioeventfd_max
= MAX(ioeventfd_max
* 2, 4);
845 ioeventfds
= g_realloc(ioeventfds
,
846 ioeventfd_max
* sizeof(*ioeventfds
));
848 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
849 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
854 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
855 as
->ioeventfds
, as
->ioeventfd_nb
);
857 g_free(as
->ioeventfds
);
858 as
->ioeventfds
= ioeventfds
;
859 as
->ioeventfd_nb
= ioeventfd_nb
;
860 flatview_unref(view
);
864 * Notify the memory listeners about the coalesced IO change events of
865 * range `cmr'. Only the part that has intersection of the specified
866 * FlatRange will be sent.
868 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
869 CoalescedMemoryRange
*cmr
, bool add
)
873 tmp
= addrrange_shift(cmr
->addr
,
874 int128_sub(fr
->addr
.start
,
875 int128_make64(fr
->offset_in_region
)));
876 if (!addrrange_intersects(tmp
, fr
->addr
)) {
879 tmp
= addrrange_intersection(tmp
, fr
->addr
);
882 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
883 int128_get64(tmp
.start
),
884 int128_get64(tmp
.size
));
886 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
887 int128_get64(tmp
.start
),
888 int128_get64(tmp
.size
));
892 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
894 CoalescedMemoryRange
*cmr
;
896 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
897 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
901 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
903 MemoryRegion
*mr
= fr
->mr
;
904 CoalescedMemoryRange
*cmr
;
906 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
910 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
911 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
915 static void address_space_update_topology_pass(AddressSpace
*as
,
916 const FlatView
*old_view
,
917 const FlatView
*new_view
,
921 FlatRange
*frold
, *frnew
;
923 /* Generate a symmetric difference of the old and new memory maps.
924 * Kill ranges in the old map, and instantiate ranges in the new map.
927 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
928 if (iold
< old_view
->nr
) {
929 frold
= &old_view
->ranges
[iold
];
933 if (inew
< new_view
->nr
) {
934 frnew
= &new_view
->ranges
[inew
];
941 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
942 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
943 && !flatrange_equal(frold
, frnew
)))) {
944 /* In old but not in new, or in both but attributes changed. */
947 flat_range_coalesced_io_del(frold
, as
);
948 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
952 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
953 /* In both and unchanged (except logging may have changed) */
956 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
957 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
958 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
959 frold
->dirty_log_mask
,
960 frnew
->dirty_log_mask
);
962 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
963 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
964 frold
->dirty_log_mask
,
965 frnew
->dirty_log_mask
);
975 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
976 flat_range_coalesced_io_add(frnew
, as
);
984 static void flatviews_init(void)
986 static FlatView
*empty_view
;
992 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
993 (GDestroyNotify
) flatview_unref
);
995 empty_view
= generate_memory_topology(NULL
);
996 /* We keep it alive forever in the global variable. */
997 flatview_ref(empty_view
);
999 g_hash_table_replace(flat_views
, NULL
, empty_view
);
1000 flatview_ref(empty_view
);
1004 static void flatviews_reset(void)
1009 g_hash_table_unref(flat_views
);
1014 /* Render unique FVs */
1015 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1016 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1018 if (g_hash_table_lookup(flat_views
, physmr
)) {
1022 generate_memory_topology(physmr
);
1026 static void address_space_set_flatview(AddressSpace
*as
)
1028 FlatView
*old_view
= address_space_to_flatview(as
);
1029 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1030 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1034 if (old_view
== new_view
) {
1039 flatview_ref(old_view
);
1042 flatview_ref(new_view
);
1044 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1045 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1048 old_view2
= &tmpview
;
1050 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1051 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1054 /* Writes are protected by the BQL. */
1055 qatomic_rcu_set(&as
->current_map
, new_view
);
1057 flatview_unref(old_view
);
1060 /* Note that all the old MemoryRegions are still alive up to this
1061 * point. This relieves most MemoryListeners from the need to
1062 * ref/unref the MemoryRegions they get---unless they use them
1063 * outside the iothread mutex, in which case precise reference
1064 * counting is necessary.
1067 flatview_unref(old_view
);
1071 static void address_space_update_topology(AddressSpace
*as
)
1073 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1076 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1077 generate_memory_topology(physmr
);
1079 address_space_set_flatview(as
);
1082 void memory_region_transaction_begin(void)
1084 qemu_flush_coalesced_mmio_buffer();
1085 ++memory_region_transaction_depth
;
1088 void memory_region_transaction_commit(void)
1092 assert(memory_region_transaction_depth
);
1093 assert(qemu_mutex_iothread_locked());
1095 --memory_region_transaction_depth
;
1096 if (!memory_region_transaction_depth
) {
1097 if (memory_region_update_pending
) {
1100 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1102 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1103 address_space_set_flatview(as
);
1104 address_space_update_ioeventfds(as
);
1106 memory_region_update_pending
= false;
1107 ioeventfd_update_pending
= false;
1108 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1109 } else if (ioeventfd_update_pending
) {
1110 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1111 address_space_update_ioeventfds(as
);
1113 ioeventfd_update_pending
= false;
1118 static void memory_region_destructor_none(MemoryRegion
*mr
)
1122 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1124 qemu_ram_free(mr
->ram_block
);
1127 static bool memory_region_need_escape(char c
)
1129 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1132 static char *memory_region_escape_name(const char *name
)
1139 for (p
= name
; *p
; p
++) {
1140 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1142 if (bytes
== p
- name
) {
1143 return g_memdup(name
, bytes
+ 1);
1146 escaped
= g_malloc(bytes
+ 1);
1147 for (p
= name
, q
= escaped
; *p
; p
++) {
1149 if (unlikely(memory_region_need_escape(c
))) {
1152 *q
++ = "0123456789abcdef"[c
>> 4];
1153 c
= "0123456789abcdef"[c
& 15];
1161 static void memory_region_do_init(MemoryRegion
*mr
,
1166 mr
->size
= int128_make64(size
);
1167 if (size
== UINT64_MAX
) {
1168 mr
->size
= int128_2_64();
1170 mr
->name
= g_strdup(name
);
1172 mr
->ram_block
= NULL
;
1175 char *escaped_name
= memory_region_escape_name(name
);
1176 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1179 owner
= container_get(qdev_get_machine(), "/unattached");
1182 object_property_add_child(owner
, name_array
, OBJECT(mr
));
1183 object_unref(OBJECT(mr
));
1185 g_free(escaped_name
);
1189 void memory_region_init(MemoryRegion
*mr
,
1194 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1195 memory_region_do_init(mr
, owner
, name
, size
);
1198 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1199 const char *name
, void *opaque
,
1202 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1203 char *path
= (char *)"";
1205 if (mr
->container
) {
1206 path
= object_get_canonical_path(OBJECT(mr
->container
));
1208 visit_type_str(v
, name
, &path
, errp
);
1209 if (mr
->container
) {
1214 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1217 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1219 return OBJECT(mr
->container
);
1222 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1223 const char *name
, void *opaque
,
1226 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1227 int32_t value
= mr
->priority
;
1229 visit_type_int32(v
, name
, &value
, errp
);
1232 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1233 void *opaque
, Error
**errp
)
1235 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1236 uint64_t value
= memory_region_size(mr
);
1238 visit_type_uint64(v
, name
, &value
, errp
);
1241 static void memory_region_initfn(Object
*obj
)
1243 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1246 mr
->ops
= &unassigned_mem_ops
;
1248 mr
->romd_mode
= true;
1249 mr
->destructor
= memory_region_destructor_none
;
1250 QTAILQ_INIT(&mr
->subregions
);
1251 QTAILQ_INIT(&mr
->coalesced
);
1253 op
= object_property_add(OBJECT(mr
), "container",
1254 "link<" TYPE_MEMORY_REGION
">",
1255 memory_region_get_container
,
1256 NULL
, /* memory_region_set_container */
1258 op
->resolve
= memory_region_resolve_container
;
1260 object_property_add_uint64_ptr(OBJECT(mr
), "addr",
1261 &mr
->addr
, OBJ_PROP_FLAG_READ
);
1262 object_property_add(OBJECT(mr
), "priority", "uint32",
1263 memory_region_get_priority
,
1264 NULL
, /* memory_region_set_priority */
1266 object_property_add(OBJECT(mr
), "size", "uint64",
1267 memory_region_get_size
,
1268 NULL
, /* memory_region_set_size, */
1272 static void iommu_memory_region_initfn(Object
*obj
)
1274 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1276 mr
->is_iommu
= true;
1279 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1282 #ifdef DEBUG_UNASSIGNED
1283 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1288 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1289 uint64_t val
, unsigned size
)
1291 #ifdef DEBUG_UNASSIGNED
1292 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1296 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1297 unsigned size
, bool is_write
,
1303 const MemoryRegionOps unassigned_mem_ops
= {
1304 .valid
.accepts
= unassigned_mem_accepts
,
1305 .endianness
= DEVICE_NATIVE_ENDIAN
,
1308 static uint64_t memory_region_ram_device_read(void *opaque
,
1309 hwaddr addr
, unsigned size
)
1311 MemoryRegion
*mr
= opaque
;
1312 uint64_t data
= (uint64_t)~0;
1316 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1319 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1322 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1325 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1329 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1334 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1335 uint64_t data
, unsigned size
)
1337 MemoryRegion
*mr
= opaque
;
1339 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1343 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1346 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1349 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1352 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1357 static const MemoryRegionOps ram_device_mem_ops
= {
1358 .read
= memory_region_ram_device_read
,
1359 .write
= memory_region_ram_device_write
,
1360 .endianness
= DEVICE_HOST_ENDIAN
,
1362 .min_access_size
= 1,
1363 .max_access_size
= 8,
1367 .min_access_size
= 1,
1368 .max_access_size
= 8,
1373 bool memory_region_access_valid(MemoryRegion
*mr
,
1379 if (mr
->ops
->valid
.accepts
1380 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
, attrs
)) {
1381 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid access at addr "
1382 "0x%" HWADDR_PRIX
", size %u, "
1383 "region '%s', reason: rejected\n",
1384 addr
, size
, memory_region_name(mr
));
1388 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1389 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid access at addr "
1390 "0x%" HWADDR_PRIX
", size %u, "
1391 "region '%s', reason: unaligned\n",
1392 addr
, size
, memory_region_name(mr
));
1396 /* Treat zero as compatibility all valid */
1397 if (!mr
->ops
->valid
.max_access_size
) {
1401 if (size
> mr
->ops
->valid
.max_access_size
1402 || size
< mr
->ops
->valid
.min_access_size
) {
1403 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid access at addr "
1404 "0x%" HWADDR_PRIX
", size %u, "
1405 "region '%s', reason: invalid size "
1406 "(min:%u max:%u)\n",
1407 addr
, size
, memory_region_name(mr
),
1408 mr
->ops
->valid
.min_access_size
,
1409 mr
->ops
->valid
.max_access_size
);
1415 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1423 if (mr
->ops
->read
) {
1424 return access_with_adjusted_size(addr
, pval
, size
,
1425 mr
->ops
->impl
.min_access_size
,
1426 mr
->ops
->impl
.max_access_size
,
1427 memory_region_read_accessor
,
1430 return access_with_adjusted_size(addr
, pval
, size
,
1431 mr
->ops
->impl
.min_access_size
,
1432 mr
->ops
->impl
.max_access_size
,
1433 memory_region_read_with_attrs_accessor
,
1438 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1444 unsigned size
= memop_size(op
);
1447 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1448 *pval
= unassigned_mem_read(mr
, addr
, size
);
1449 return MEMTX_DECODE_ERROR
;
1452 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1453 adjust_endianness(mr
, pval
, op
);
1457 /* Return true if an eventfd was signalled */
1458 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1464 MemoryRegionIoeventfd ioeventfd
= {
1465 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1470 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1471 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1472 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1474 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1475 event_notifier_set(ioeventfd
.e
);
1483 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1489 unsigned size
= memop_size(op
);
1491 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1492 unassigned_mem_write(mr
, addr
, data
, size
);
1493 return MEMTX_DECODE_ERROR
;
1496 adjust_endianness(mr
, &data
, op
);
1498 if ((!kvm_eventfds_enabled()) &&
1499 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1503 if (mr
->ops
->write
) {
1504 return access_with_adjusted_size(addr
, &data
, size
,
1505 mr
->ops
->impl
.min_access_size
,
1506 mr
->ops
->impl
.max_access_size
,
1507 memory_region_write_accessor
, mr
,
1511 access_with_adjusted_size(addr
, &data
, size
,
1512 mr
->ops
->impl
.min_access_size
,
1513 mr
->ops
->impl
.max_access_size
,
1514 memory_region_write_with_attrs_accessor
,
1519 void memory_region_init_io(MemoryRegion
*mr
,
1521 const MemoryRegionOps
*ops
,
1526 memory_region_init(mr
, owner
, name
, size
);
1527 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1528 mr
->opaque
= opaque
;
1529 mr
->terminates
= true;
1532 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1538 memory_region_init_ram_flags_nomigrate(mr
, owner
, name
, size
, 0, errp
);
1541 void memory_region_init_ram_flags_nomigrate(MemoryRegion
*mr
,
1549 memory_region_init(mr
, owner
, name
, size
);
1551 mr
->terminates
= true;
1552 mr
->destructor
= memory_region_destructor_ram
;
1553 mr
->ram_block
= qemu_ram_alloc(size
, ram_flags
, mr
, &err
);
1555 mr
->size
= int128_zero();
1556 object_unparent(OBJECT(mr
));
1557 error_propagate(errp
, err
);
1561 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1566 void (*resized
)(const char*,
1572 memory_region_init(mr
, owner
, name
, size
);
1574 mr
->terminates
= true;
1575 mr
->destructor
= memory_region_destructor_ram
;
1576 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1579 mr
->size
= int128_zero();
1580 object_unparent(OBJECT(mr
));
1581 error_propagate(errp
, err
);
1586 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1597 memory_region_init(mr
, owner
, name
, size
);
1599 mr
->readonly
= readonly
;
1600 mr
->terminates
= true;
1601 mr
->destructor
= memory_region_destructor_ram
;
1603 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
,
1606 mr
->size
= int128_zero();
1607 object_unparent(OBJECT(mr
));
1608 error_propagate(errp
, err
);
1612 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1622 memory_region_init(mr
, owner
, name
, size
);
1624 mr
->terminates
= true;
1625 mr
->destructor
= memory_region_destructor_ram
;
1626 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
, ram_flags
, fd
, offset
,
1629 mr
->size
= int128_zero();
1630 object_unparent(OBJECT(mr
));
1631 error_propagate(errp
, err
);
1636 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1642 memory_region_init(mr
, owner
, name
, size
);
1644 mr
->terminates
= true;
1645 mr
->destructor
= memory_region_destructor_ram
;
1647 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1648 assert(ptr
!= NULL
);
1649 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1652 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1658 memory_region_init(mr
, owner
, name
, size
);
1660 mr
->terminates
= true;
1661 mr
->ram_device
= true;
1662 mr
->ops
= &ram_device_mem_ops
;
1664 mr
->destructor
= memory_region_destructor_ram
;
1666 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1667 assert(ptr
!= NULL
);
1668 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1671 void memory_region_init_alias(MemoryRegion
*mr
,
1678 memory_region_init(mr
, owner
, name
, size
);
1680 mr
->alias_offset
= offset
;
1683 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1689 memory_region_init_ram_flags_nomigrate(mr
, owner
, name
, size
, 0, errp
);
1690 mr
->readonly
= true;
1693 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1695 const MemoryRegionOps
*ops
,
1703 memory_region_init(mr
, owner
, name
, size
);
1705 mr
->opaque
= opaque
;
1706 mr
->terminates
= true;
1707 mr
->rom_device
= true;
1708 mr
->destructor
= memory_region_destructor_ram
;
1709 mr
->ram_block
= qemu_ram_alloc(size
, 0, mr
, &err
);
1711 mr
->size
= int128_zero();
1712 object_unparent(OBJECT(mr
));
1713 error_propagate(errp
, err
);
1717 void memory_region_init_iommu(void *_iommu_mr
,
1718 size_t instance_size
,
1719 const char *mrtypename
,
1724 struct IOMMUMemoryRegion
*iommu_mr
;
1725 struct MemoryRegion
*mr
;
1727 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1728 mr
= MEMORY_REGION(_iommu_mr
);
1729 memory_region_do_init(mr
, owner
, name
, size
);
1730 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1731 mr
->terminates
= true; /* then re-forwards */
1732 QLIST_INIT(&iommu_mr
->iommu_notify
);
1733 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1736 static void memory_region_finalize(Object
*obj
)
1738 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1740 assert(!mr
->container
);
1742 /* We know the region is not visible in any address space (it
1743 * does not have a container and cannot be a root either because
1744 * it has no references, so we can blindly clear mr->enabled.
1745 * memory_region_set_enabled instead could trigger a transaction
1746 * and cause an infinite loop.
1748 mr
->enabled
= false;
1749 memory_region_transaction_begin();
1750 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1751 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1752 memory_region_del_subregion(mr
, subregion
);
1754 memory_region_transaction_commit();
1757 memory_region_clear_coalescing(mr
);
1758 g_free((char *)mr
->name
);
1759 g_free(mr
->ioeventfds
);
1762 Object
*memory_region_owner(MemoryRegion
*mr
)
1764 Object
*obj
= OBJECT(mr
);
1768 void memory_region_ref(MemoryRegion
*mr
)
1770 /* MMIO callbacks most likely will access data that belongs
1771 * to the owner, hence the need to ref/unref the owner whenever
1772 * the memory region is in use.
1774 * The memory region is a child of its owner. As long as the
1775 * owner doesn't call unparent itself on the memory region,
1776 * ref-ing the owner will also keep the memory region alive.
1777 * Memory regions without an owner are supposed to never go away;
1778 * we do not ref/unref them because it slows down DMA sensibly.
1780 if (mr
&& mr
->owner
) {
1781 object_ref(mr
->owner
);
1785 void memory_region_unref(MemoryRegion
*mr
)
1787 if (mr
&& mr
->owner
) {
1788 object_unref(mr
->owner
);
1792 uint64_t memory_region_size(MemoryRegion
*mr
)
1794 if (int128_eq(mr
->size
, int128_2_64())) {
1797 return int128_get64(mr
->size
);
1800 const char *memory_region_name(const MemoryRegion
*mr
)
1803 ((MemoryRegion
*)mr
)->name
=
1804 g_strdup(object_get_canonical_path_component(OBJECT(mr
)));
1809 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1811 return mr
->ram_device
;
1814 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1816 uint8_t mask
= mr
->dirty_log_mask
;
1817 RAMBlock
*rb
= mr
->ram_block
;
1819 if (global_dirty_log
&& ((rb
&& qemu_ram_is_migratable(rb
)) ||
1820 memory_region_is_iommu(mr
))) {
1821 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1824 if (tcg_enabled() && rb
) {
1825 /* TCG only cares about dirty memory logging for RAM, not IOMMU. */
1826 mask
|= (1 << DIRTY_MEMORY_CODE
);
1831 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1833 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1836 static int memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
,
1839 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1840 IOMMUNotifier
*iommu_notifier
;
1841 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1844 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1845 flags
|= iommu_notifier
->notifier_flags
;
1848 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1849 ret
= imrc
->notify_flag_changed(iommu_mr
,
1850 iommu_mr
->iommu_notify_flags
,
1855 iommu_mr
->iommu_notify_flags
= flags
;
1860 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion
*iommu_mr
,
1861 uint64_t page_size_mask
,
1864 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1867 if (imrc
->iommu_set_page_size_mask
) {
1868 ret
= imrc
->iommu_set_page_size_mask(iommu_mr
, page_size_mask
, errp
);
1873 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1874 IOMMUNotifier
*n
, Error
**errp
)
1876 IOMMUMemoryRegion
*iommu_mr
;
1880 return memory_region_register_iommu_notifier(mr
->alias
, n
, errp
);
1883 /* We need to register for at least one bitfield */
1884 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1885 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1886 assert(n
->start
<= n
->end
);
1887 assert(n
->iommu_idx
>= 0 &&
1888 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1890 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1891 ret
= memory_region_update_iommu_notify_flags(iommu_mr
, errp
);
1893 QLIST_REMOVE(n
, node
);
1898 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1900 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1902 if (imrc
->get_min_page_size
) {
1903 return imrc
->get_min_page_size(iommu_mr
);
1905 return TARGET_PAGE_SIZE
;
1908 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1910 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1911 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1912 hwaddr addr
, granularity
;
1913 IOMMUTLBEntry iotlb
;
1915 /* If the IOMMU has its own replay callback, override */
1917 imrc
->replay(iommu_mr
, n
);
1921 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1923 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1924 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1925 if (iotlb
.perm
!= IOMMU_NONE
) {
1926 n
->notify(n
, &iotlb
);
1929 /* if (2^64 - MR size) < granularity, it's possible to get an
1930 * infinite loop here. This should catch such a wraparound */
1931 if ((addr
+ granularity
) < addr
) {
1937 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1940 IOMMUMemoryRegion
*iommu_mr
;
1943 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1946 QLIST_REMOVE(n
, node
);
1947 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1948 memory_region_update_iommu_notify_flags(iommu_mr
, NULL
);
1951 void memory_region_notify_iommu_one(IOMMUNotifier
*notifier
,
1952 IOMMUTLBEvent
*event
)
1954 IOMMUTLBEntry
*entry
= &event
->entry
;
1955 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1956 IOMMUTLBEntry tmp
= *entry
;
1958 if (event
->type
== IOMMU_NOTIFIER_UNMAP
) {
1959 assert(entry
->perm
== IOMMU_NONE
);
1963 * Skip the notification if the notification does not overlap
1964 * with registered range.
1966 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1970 if (notifier
->notifier_flags
& IOMMU_NOTIFIER_DEVIOTLB_UNMAP
) {
1971 /* Crop (iova, addr_mask) to range */
1972 tmp
.iova
= MAX(tmp
.iova
, notifier
->start
);
1973 tmp
.addr_mask
= MIN(entry_end
, notifier
->end
) - tmp
.iova
;
1975 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1978 if (event
->type
& notifier
->notifier_flags
) {
1979 notifier
->notify(notifier
, &tmp
);
1983 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1985 IOMMUTLBEvent event
)
1987 IOMMUNotifier
*iommu_notifier
;
1989 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1991 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1992 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1993 memory_region_notify_iommu_one(iommu_notifier
, &event
);
1998 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1999 enum IOMMUMemoryRegionAttr attr
,
2002 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2004 if (!imrc
->get_attr
) {
2008 return imrc
->get_attr(iommu_mr
, attr
, data
);
2011 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
2014 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2016 if (!imrc
->attrs_to_index
) {
2020 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2023 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2025 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2027 if (!imrc
->num_indexes
) {
2031 return imrc
->num_indexes(iommu_mr
);
2034 RamDiscardManager
*memory_region_get_ram_discard_manager(MemoryRegion
*mr
)
2036 if (!memory_region_is_mapped(mr
) || !memory_region_is_ram(mr
)) {
2042 void memory_region_set_ram_discard_manager(MemoryRegion
*mr
,
2043 RamDiscardManager
*rdm
)
2045 g_assert(memory_region_is_ram(mr
) && !memory_region_is_mapped(mr
));
2046 g_assert(!rdm
|| !mr
->rdm
);
2050 uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager
*rdm
,
2051 const MemoryRegion
*mr
)
2053 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2055 g_assert(rdmc
->get_min_granularity
);
2056 return rdmc
->get_min_granularity(rdm
, mr
);
2059 bool ram_discard_manager_is_populated(const RamDiscardManager
*rdm
,
2060 const MemoryRegionSection
*section
)
2062 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2064 g_assert(rdmc
->is_populated
);
2065 return rdmc
->is_populated(rdm
, section
);
2068 int ram_discard_manager_replay_populated(const RamDiscardManager
*rdm
,
2069 MemoryRegionSection
*section
,
2070 ReplayRamPopulate replay_fn
,
2073 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2075 g_assert(rdmc
->replay_populated
);
2076 return rdmc
->replay_populated(rdm
, section
, replay_fn
, opaque
);
2079 void ram_discard_manager_register_listener(RamDiscardManager
*rdm
,
2080 RamDiscardListener
*rdl
,
2081 MemoryRegionSection
*section
)
2083 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2085 g_assert(rdmc
->register_listener
);
2086 rdmc
->register_listener(rdm
, rdl
, section
);
2089 void ram_discard_manager_unregister_listener(RamDiscardManager
*rdm
,
2090 RamDiscardListener
*rdl
)
2092 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2094 g_assert(rdmc
->unregister_listener
);
2095 rdmc
->unregister_listener(rdm
, rdl
);
2098 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2100 uint8_t mask
= 1 << client
;
2101 uint8_t old_logging
;
2103 assert(client
== DIRTY_MEMORY_VGA
);
2104 old_logging
= mr
->vga_logging_count
;
2105 mr
->vga_logging_count
+= log
? 1 : -1;
2106 if (!!old_logging
== !!mr
->vga_logging_count
) {
2110 memory_region_transaction_begin();
2111 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2112 memory_region_update_pending
|= mr
->enabled
;
2113 memory_region_transaction_commit();
2116 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2119 assert(mr
->ram_block
);
2120 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2122 memory_region_get_dirty_log_mask(mr
));
2126 * If memory region `mr' is NULL, do global sync. Otherwise, sync
2127 * dirty bitmap for the specified memory region.
2129 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2131 MemoryListener
*listener
;
2136 /* If the same address space has multiple log_sync listeners, we
2137 * visit that address space's FlatView multiple times. But because
2138 * log_sync listeners are rare, it's still cheaper than walking each
2139 * address space once.
2141 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2142 if (listener
->log_sync
) {
2143 as
= listener
->address_space
;
2144 view
= address_space_get_flatview(as
);
2145 FOR_EACH_FLAT_RANGE(fr
, view
) {
2146 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2147 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2148 listener
->log_sync(listener
, &mrs
);
2151 flatview_unref(view
);
2152 } else if (listener
->log_sync_global
) {
2154 * No matter whether MR is specified, what we can do here
2155 * is to do a global sync, because we are not capable to
2156 * sync in a finer granularity.
2158 listener
->log_sync_global(listener
);
2163 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2166 MemoryRegionSection mrs
;
2167 MemoryListener
*listener
;
2171 hwaddr sec_start
, sec_end
, sec_size
;
2173 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2174 if (!listener
->log_clear
) {
2177 as
= listener
->address_space
;
2178 view
= address_space_get_flatview(as
);
2179 FOR_EACH_FLAT_RANGE(fr
, view
) {
2180 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2182 * Clear dirty bitmap operation only applies to those
2183 * regions whose dirty logging is at least enabled
2188 mrs
= section_from_flat_range(fr
, view
);
2190 sec_start
= MAX(mrs
.offset_within_region
, start
);
2191 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2192 sec_end
= MIN(sec_end
, start
+ len
);
2194 if (sec_start
>= sec_end
) {
2196 * If this memory region section has no intersection
2197 * with the requested range, skip.
2202 /* Valid case; shrink the section if needed */
2203 mrs
.offset_within_address_space
+=
2204 sec_start
- mrs
.offset_within_region
;
2205 mrs
.offset_within_region
= sec_start
;
2206 sec_size
= sec_end
- sec_start
;
2207 mrs
.size
= int128_make64(sec_size
);
2208 listener
->log_clear(listener
, &mrs
);
2210 flatview_unref(view
);
2214 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2219 DirtyBitmapSnapshot
*snapshot
;
2220 assert(mr
->ram_block
);
2221 memory_region_sync_dirty_bitmap(mr
);
2222 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2223 memory_global_after_dirty_log_sync();
2227 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2228 hwaddr addr
, hwaddr size
)
2230 assert(mr
->ram_block
);
2231 return cpu_physical_memory_snapshot_get_dirty(snap
,
2232 memory_region_get_ram_addr(mr
) + addr
, size
);
2235 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2237 if (mr
->readonly
!= readonly
) {
2238 memory_region_transaction_begin();
2239 mr
->readonly
= readonly
;
2240 memory_region_update_pending
|= mr
->enabled
;
2241 memory_region_transaction_commit();
2245 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2247 if (mr
->nonvolatile
!= nonvolatile
) {
2248 memory_region_transaction_begin();
2249 mr
->nonvolatile
= nonvolatile
;
2250 memory_region_update_pending
|= mr
->enabled
;
2251 memory_region_transaction_commit();
2255 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2257 if (mr
->romd_mode
!= romd_mode
) {
2258 memory_region_transaction_begin();
2259 mr
->romd_mode
= romd_mode
;
2260 memory_region_update_pending
|= mr
->enabled
;
2261 memory_region_transaction_commit();
2265 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2266 hwaddr size
, unsigned client
)
2268 assert(mr
->ram_block
);
2269 cpu_physical_memory_test_and_clear_dirty(
2270 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2273 int memory_region_get_fd(MemoryRegion
*mr
)
2277 RCU_READ_LOCK_GUARD();
2281 fd
= mr
->ram_block
->fd
;
2286 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2289 uint64_t offset
= 0;
2291 RCU_READ_LOCK_GUARD();
2293 offset
+= mr
->alias_offset
;
2296 assert(mr
->ram_block
);
2297 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2302 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2306 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2314 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2316 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2319 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2321 assert(mr
->ram_block
);
2323 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2326 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
2328 if (mr
->ram_block
) {
2329 qemu_ram_msync(mr
->ram_block
, addr
, size
);
2333 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
2336 * Might be extended case needed to cover
2337 * different types of memory regions
2339 if (mr
->dirty_log_mask
) {
2340 memory_region_msync(mr
, addr
, size
);
2345 * Call proper memory listeners about the change on the newly
2346 * added/removed CoalescedMemoryRange.
2348 static void memory_region_update_coalesced_range(MemoryRegion
*mr
,
2349 CoalescedMemoryRange
*cmr
,
2356 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2357 view
= address_space_get_flatview(as
);
2358 FOR_EACH_FLAT_RANGE(fr
, view
) {
2360 flat_range_coalesced_io_notify(fr
, as
, cmr
, add
);
2363 flatview_unref(view
);
2367 void memory_region_set_coalescing(MemoryRegion
*mr
)
2369 memory_region_clear_coalescing(mr
);
2370 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2373 void memory_region_add_coalescing(MemoryRegion
*mr
,
2377 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2379 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2380 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2381 memory_region_update_coalesced_range(mr
, cmr
, true);
2382 memory_region_set_flush_coalesced(mr
);
2385 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2387 CoalescedMemoryRange
*cmr
;
2389 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2393 qemu_flush_coalesced_mmio_buffer();
2394 mr
->flush_coalesced_mmio
= false;
2396 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2397 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2398 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2399 memory_region_update_coalesced_range(mr
, cmr
, false);
2404 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2406 mr
->flush_coalesced_mmio
= true;
2409 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2411 qemu_flush_coalesced_mmio_buffer();
2412 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2413 mr
->flush_coalesced_mmio
= false;
2417 static bool userspace_eventfd_warning
;
2419 void memory_region_add_eventfd(MemoryRegion
*mr
,
2426 MemoryRegionIoeventfd mrfd
= {
2427 .addr
.start
= int128_make64(addr
),
2428 .addr
.size
= int128_make64(size
),
2429 .match_data
= match_data
,
2435 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2436 userspace_eventfd_warning
))) {
2437 userspace_eventfd_warning
= true;
2438 error_report("Using eventfd without MMIO binding in KVM. "
2439 "Suboptimal performance expected");
2443 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2445 memory_region_transaction_begin();
2446 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2447 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2452 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2453 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2454 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2455 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2456 mr
->ioeventfds
[i
] = mrfd
;
2457 ioeventfd_update_pending
|= mr
->enabled
;
2458 memory_region_transaction_commit();
2461 void memory_region_del_eventfd(MemoryRegion
*mr
,
2468 MemoryRegionIoeventfd mrfd
= {
2469 .addr
.start
= int128_make64(addr
),
2470 .addr
.size
= int128_make64(size
),
2471 .match_data
= match_data
,
2478 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2480 memory_region_transaction_begin();
2481 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2482 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2486 assert(i
!= mr
->ioeventfd_nb
);
2487 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2488 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2490 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2491 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2492 ioeventfd_update_pending
|= mr
->enabled
;
2493 memory_region_transaction_commit();
2496 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2498 MemoryRegion
*mr
= subregion
->container
;
2499 MemoryRegion
*other
;
2501 memory_region_transaction_begin();
2503 memory_region_ref(subregion
);
2504 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2505 if (subregion
->priority
>= other
->priority
) {
2506 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2510 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2512 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2513 memory_region_transaction_commit();
2516 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2518 MemoryRegion
*subregion
)
2520 assert(!subregion
->container
);
2521 subregion
->container
= mr
;
2522 subregion
->addr
= offset
;
2523 memory_region_update_container_subregions(subregion
);
2526 void memory_region_add_subregion(MemoryRegion
*mr
,
2528 MemoryRegion
*subregion
)
2530 subregion
->priority
= 0;
2531 memory_region_add_subregion_common(mr
, offset
, subregion
);
2534 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2536 MemoryRegion
*subregion
,
2539 subregion
->priority
= priority
;
2540 memory_region_add_subregion_common(mr
, offset
, subregion
);
2543 void memory_region_del_subregion(MemoryRegion
*mr
,
2544 MemoryRegion
*subregion
)
2546 memory_region_transaction_begin();
2547 assert(subregion
->container
== mr
);
2548 subregion
->container
= NULL
;
2549 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2550 memory_region_unref(subregion
);
2551 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2552 memory_region_transaction_commit();
2555 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2557 if (enabled
== mr
->enabled
) {
2560 memory_region_transaction_begin();
2561 mr
->enabled
= enabled
;
2562 memory_region_update_pending
= true;
2563 memory_region_transaction_commit();
2566 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2568 Int128 s
= int128_make64(size
);
2570 if (size
== UINT64_MAX
) {
2573 if (int128_eq(s
, mr
->size
)) {
2576 memory_region_transaction_begin();
2578 memory_region_update_pending
= true;
2579 memory_region_transaction_commit();
2582 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2584 MemoryRegion
*container
= mr
->container
;
2587 memory_region_transaction_begin();
2588 memory_region_ref(mr
);
2589 memory_region_del_subregion(container
, mr
);
2590 mr
->container
= container
;
2591 memory_region_update_container_subregions(mr
);
2592 memory_region_unref(mr
);
2593 memory_region_transaction_commit();
2597 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2599 if (addr
!= mr
->addr
) {
2601 memory_region_readd_subregion(mr
);
2605 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2609 if (offset
== mr
->alias_offset
) {
2613 memory_region_transaction_begin();
2614 mr
->alias_offset
= offset
;
2615 memory_region_update_pending
|= mr
->enabled
;
2616 memory_region_transaction_commit();
2619 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2624 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2626 const AddrRange
*addr
= addr_
;
2627 const FlatRange
*fr
= fr_
;
2629 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2631 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2637 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2639 return bsearch(&addr
, view
->ranges
, view
->nr
,
2640 sizeof(FlatRange
), cmp_flatrange_addr
);
2643 bool memory_region_is_mapped(MemoryRegion
*mr
)
2645 return mr
->container
? true : false;
2648 /* Same as memory_region_find, but it does not add a reference to the
2649 * returned region. It must be called from an RCU critical section.
2651 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2652 hwaddr addr
, uint64_t size
)
2654 MemoryRegionSection ret
= { .mr
= NULL
};
2662 for (root
= mr
; root
->container
; ) {
2663 root
= root
->container
;
2667 as
= memory_region_to_address_space(root
);
2671 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2673 view
= address_space_to_flatview(as
);
2674 fr
= flatview_lookup(view
, range
);
2679 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2685 range
= addrrange_intersection(range
, fr
->addr
);
2686 ret
.offset_within_region
= fr
->offset_in_region
;
2687 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2689 ret
.size
= range
.size
;
2690 ret
.offset_within_address_space
= int128_get64(range
.start
);
2691 ret
.readonly
= fr
->readonly
;
2692 ret
.nonvolatile
= fr
->nonvolatile
;
2696 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2697 hwaddr addr
, uint64_t size
)
2699 MemoryRegionSection ret
;
2700 RCU_READ_LOCK_GUARD();
2701 ret
= memory_region_find_rcu(mr
, addr
, size
);
2703 memory_region_ref(ret
.mr
);
2708 MemoryRegionSection
*memory_region_section_new_copy(MemoryRegionSection
*s
)
2710 MemoryRegionSection
*tmp
= g_new(MemoryRegionSection
, 1);
2714 memory_region_ref(tmp
->mr
);
2717 bool ret
= flatview_ref(tmp
->fv
);
2724 void memory_region_section_free_copy(MemoryRegionSection
*s
)
2727 flatview_unref(s
->fv
);
2730 memory_region_unref(s
->mr
);
2735 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2739 RCU_READ_LOCK_GUARD();
2740 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2741 return mr
&& mr
!= container
;
2744 void memory_global_dirty_log_sync(void)
2746 memory_region_sync_dirty_bitmap(NULL
);
2749 void memory_global_after_dirty_log_sync(void)
2751 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2754 static VMChangeStateEntry
*vmstate_change
;
2756 void memory_global_dirty_log_start(void)
2758 if (vmstate_change
) {
2759 qemu_del_vm_change_state_handler(vmstate_change
);
2760 vmstate_change
= NULL
;
2763 global_dirty_log
= true;
2765 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2767 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2768 memory_region_transaction_begin();
2769 memory_region_update_pending
= true;
2770 memory_region_transaction_commit();
2773 static void memory_global_dirty_log_do_stop(void)
2775 global_dirty_log
= false;
2777 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2778 memory_region_transaction_begin();
2779 memory_region_update_pending
= true;
2780 memory_region_transaction_commit();
2782 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2785 static void memory_vm_change_state_handler(void *opaque
, bool running
,
2789 memory_global_dirty_log_do_stop();
2791 if (vmstate_change
) {
2792 qemu_del_vm_change_state_handler(vmstate_change
);
2793 vmstate_change
= NULL
;
2798 void memory_global_dirty_log_stop(void)
2800 if (!runstate_is_running()) {
2801 if (vmstate_change
) {
2804 vmstate_change
= qemu_add_vm_change_state_handler(
2805 memory_vm_change_state_handler
, NULL
);
2809 memory_global_dirty_log_do_stop();
2812 static void listener_add_address_space(MemoryListener
*listener
,
2818 if (listener
->begin
) {
2819 listener
->begin(listener
);
2821 if (global_dirty_log
) {
2822 if (listener
->log_global_start
) {
2823 listener
->log_global_start(listener
);
2827 view
= address_space_get_flatview(as
);
2828 FOR_EACH_FLAT_RANGE(fr
, view
) {
2829 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2831 if (listener
->region_add
) {
2832 listener
->region_add(listener
, §ion
);
2834 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2835 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2838 if (listener
->commit
) {
2839 listener
->commit(listener
);
2841 flatview_unref(view
);
2844 static void listener_del_address_space(MemoryListener
*listener
,
2850 if (listener
->begin
) {
2851 listener
->begin(listener
);
2853 view
= address_space_get_flatview(as
);
2854 FOR_EACH_FLAT_RANGE(fr
, view
) {
2855 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2857 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2858 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2860 if (listener
->region_del
) {
2861 listener
->region_del(listener
, §ion
);
2864 if (listener
->commit
) {
2865 listener
->commit(listener
);
2867 flatview_unref(view
);
2870 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2872 MemoryListener
*other
= NULL
;
2874 /* Only one of them can be defined for a listener */
2875 assert(!(listener
->log_sync
&& listener
->log_sync_global
));
2877 listener
->address_space
= as
;
2878 if (QTAILQ_EMPTY(&memory_listeners
)
2879 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2880 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2882 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2883 if (listener
->priority
< other
->priority
) {
2887 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2890 if (QTAILQ_EMPTY(&as
->listeners
)
2891 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2892 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2894 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2895 if (listener
->priority
< other
->priority
) {
2899 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2902 listener_add_address_space(listener
, as
);
2905 void memory_listener_unregister(MemoryListener
*listener
)
2907 if (!listener
->address_space
) {
2911 listener_del_address_space(listener
, listener
->address_space
);
2912 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2913 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2914 listener
->address_space
= NULL
;
2917 void address_space_remove_listeners(AddressSpace
*as
)
2919 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2920 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2924 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2926 memory_region_ref(root
);
2928 as
->current_map
= NULL
;
2929 as
->ioeventfd_nb
= 0;
2930 as
->ioeventfds
= NULL
;
2931 QTAILQ_INIT(&as
->listeners
);
2932 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2933 as
->name
= g_strdup(name
? name
: "anonymous");
2934 address_space_update_topology(as
);
2935 address_space_update_ioeventfds(as
);
2938 static void do_address_space_destroy(AddressSpace
*as
)
2940 assert(QTAILQ_EMPTY(&as
->listeners
));
2942 flatview_unref(as
->current_map
);
2944 g_free(as
->ioeventfds
);
2945 memory_region_unref(as
->root
);
2948 void address_space_destroy(AddressSpace
*as
)
2950 MemoryRegion
*root
= as
->root
;
2952 /* Flush out anything from MemoryListeners listening in on this */
2953 memory_region_transaction_begin();
2955 memory_region_transaction_commit();
2956 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2958 /* At this point, as->dispatch and as->current_map are dummy
2959 * entries that the guest should never use. Wait for the old
2960 * values to expire before freeing the data.
2963 call_rcu(as
, do_address_space_destroy
, rcu
);
2966 static const char *memory_region_type(MemoryRegion
*mr
)
2969 return memory_region_type(mr
->alias
);
2971 if (memory_region_is_ram_device(mr
)) {
2973 } else if (memory_region_is_romd(mr
)) {
2975 } else if (memory_region_is_rom(mr
)) {
2977 } else if (memory_region_is_ram(mr
)) {
2984 typedef struct MemoryRegionList MemoryRegionList
;
2986 struct MemoryRegionList
{
2987 const MemoryRegion
*mr
;
2988 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2991 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2993 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2994 int128_sub((size), int128_one())) : 0)
2995 #define MTREE_INDENT " "
2997 static void mtree_expand_owner(const char *label
, Object
*obj
)
2999 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
3001 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
3002 if (dev
&& dev
->id
) {
3003 qemu_printf(" id=%s", dev
->id
);
3005 char *canonical_path
= object_get_canonical_path(obj
);
3006 if (canonical_path
) {
3007 qemu_printf(" path=%s", canonical_path
);
3008 g_free(canonical_path
);
3010 qemu_printf(" type=%s", object_get_typename(obj
));
3016 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
3018 Object
*owner
= mr
->owner
;
3019 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
3021 if (!owner
&& !parent
) {
3022 qemu_printf(" orphan");
3026 mtree_expand_owner("owner", owner
);
3028 if (parent
&& parent
!= owner
) {
3029 mtree_expand_owner("parent", parent
);
3033 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
3035 MemoryRegionListHead
*alias_print_queue
,
3036 bool owner
, bool display_disabled
)
3038 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
3039 MemoryRegionListHead submr_print_queue
;
3040 const MemoryRegion
*submr
;
3042 hwaddr cur_start
, cur_end
;
3048 cur_start
= base
+ mr
->addr
;
3049 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
3052 * Try to detect overflow of memory region. This should never
3053 * happen normally. When it happens, we dump something to warn the
3054 * user who is observing this.
3056 if (cur_start
< base
|| cur_end
< cur_start
) {
3057 qemu_printf("[DETECTED OVERFLOW!] ");
3061 MemoryRegionList
*ml
;
3064 /* check if the alias is already in the queue */
3065 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
3066 if (ml
->mr
== mr
->alias
) {
3072 ml
= g_new(MemoryRegionList
, 1);
3074 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
3076 if (mr
->enabled
|| display_disabled
) {
3077 for (i
= 0; i
< level
; i
++) {
3078 qemu_printf(MTREE_INDENT
);
3080 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
3081 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
3082 "-" TARGET_FMT_plx
"%s",
3085 mr
->nonvolatile
? "nv-" : "",
3086 memory_region_type((MemoryRegion
*)mr
),
3087 memory_region_name(mr
),
3088 memory_region_name(mr
->alias
),
3090 mr
->alias_offset
+ MR_SIZE(mr
->size
),
3091 mr
->enabled
? "" : " [disabled]");
3093 mtree_print_mr_owner(mr
);
3098 if (mr
->enabled
|| display_disabled
) {
3099 for (i
= 0; i
< level
; i
++) {
3100 qemu_printf(MTREE_INDENT
);
3102 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
3103 " (prio %d, %s%s): %s%s",
3106 mr
->nonvolatile
? "nv-" : "",
3107 memory_region_type((MemoryRegion
*)mr
),
3108 memory_region_name(mr
),
3109 mr
->enabled
? "" : " [disabled]");
3111 mtree_print_mr_owner(mr
);
3117 QTAILQ_INIT(&submr_print_queue
);
3119 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
3120 new_ml
= g_new(MemoryRegionList
, 1);
3122 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3123 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
3124 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
3125 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
3126 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
3132 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
3136 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3137 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
3138 alias_print_queue
, owner
, display_disabled
);
3141 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3146 struct FlatViewInfo
{
3153 static void mtree_print_flatview(gpointer key
, gpointer value
,
3156 FlatView
*view
= key
;
3157 GArray
*fv_address_spaces
= value
;
3158 struct FlatViewInfo
*fvi
= user_data
;
3159 FlatRange
*range
= &view
->ranges
[0];
3165 qemu_printf("FlatView #%d\n", fvi
->counter
);
3168 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3169 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3170 qemu_printf(" AS \"%s\", root: %s",
3171 as
->name
, memory_region_name(as
->root
));
3172 if (as
->root
->alias
) {
3173 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3178 qemu_printf(" Root memory region: %s\n",
3179 view
->root
? memory_region_name(view
->root
) : "(none)");
3182 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3188 if (range
->offset_in_region
) {
3189 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3190 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3191 int128_get64(range
->addr
.start
),
3192 int128_get64(range
->addr
.start
)
3193 + MR_SIZE(range
->addr
.size
),
3195 range
->nonvolatile
? "nv-" : "",
3196 range
->readonly
? "rom" : memory_region_type(mr
),
3197 memory_region_name(mr
),
3198 range
->offset_in_region
);
3200 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3201 " (prio %d, %s%s): %s",
3202 int128_get64(range
->addr
.start
),
3203 int128_get64(range
->addr
.start
)
3204 + MR_SIZE(range
->addr
.size
),
3206 range
->nonvolatile
? "nv-" : "",
3207 range
->readonly
? "rom" : memory_region_type(mr
),
3208 memory_region_name(mr
));
3211 mtree_print_mr_owner(mr
);
3215 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3216 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3217 if (fvi
->ac
->has_memory(current_machine
, as
,
3218 int128_get64(range
->addr
.start
),
3219 MR_SIZE(range
->addr
.size
) + 1)) {
3220 qemu_printf(" %s", fvi
->ac
->name
);
3228 #if !defined(CONFIG_USER_ONLY)
3229 if (fvi
->dispatch_tree
&& view
->root
) {
3230 mtree_print_dispatch(view
->dispatch
, view
->root
);
3237 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3240 FlatView
*view
= key
;
3241 GArray
*fv_address_spaces
= value
;
3243 g_array_unref(fv_address_spaces
);
3244 flatview_unref(view
);
3249 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
)
3251 MemoryRegionListHead ml_head
;
3252 MemoryRegionList
*ml
, *ml2
;
3257 struct FlatViewInfo fvi
= {
3259 .dispatch_tree
= dispatch_tree
,
3262 GArray
*fv_address_spaces
;
3263 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3264 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
3266 if (ac
->has_memory
) {
3270 /* Gather all FVs in one table */
3271 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3272 view
= address_space_get_flatview(as
);
3274 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3275 if (!fv_address_spaces
) {
3276 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3277 g_hash_table_insert(views
, view
, fv_address_spaces
);
3280 g_array_append_val(fv_address_spaces
, as
);
3284 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3287 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3288 g_hash_table_unref(views
);
3293 QTAILQ_INIT(&ml_head
);
3295 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3296 qemu_printf("address-space: %s\n", as
->name
);
3297 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
, disabled
);
3301 /* print aliased regions */
3302 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3303 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3304 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
, disabled
);
3308 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3313 void memory_region_init_ram(MemoryRegion
*mr
,
3319 DeviceState
*owner_dev
;
3322 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3324 error_propagate(errp
, err
);
3327 /* This will assert if owner is neither NULL nor a DeviceState.
3328 * We only want the owner here for the purposes of defining a
3329 * unique name for migration. TODO: Ideally we should implement
3330 * a naming scheme for Objects which are not DeviceStates, in
3331 * which case we can relax this restriction.
3333 owner_dev
= DEVICE(owner
);
3334 vmstate_register_ram(mr
, owner_dev
);
3337 void memory_region_init_rom(MemoryRegion
*mr
,
3343 DeviceState
*owner_dev
;
3346 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3348 error_propagate(errp
, err
);
3351 /* This will assert if owner is neither NULL nor a DeviceState.
3352 * We only want the owner here for the purposes of defining a
3353 * unique name for migration. TODO: Ideally we should implement
3354 * a naming scheme for Objects which are not DeviceStates, in
3355 * which case we can relax this restriction.
3357 owner_dev
= DEVICE(owner
);
3358 vmstate_register_ram(mr
, owner_dev
);
3361 void memory_region_init_rom_device(MemoryRegion
*mr
,
3363 const MemoryRegionOps
*ops
,
3369 DeviceState
*owner_dev
;
3372 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3375 error_propagate(errp
, err
);
3378 /* This will assert if owner is neither NULL nor a DeviceState.
3379 * We only want the owner here for the purposes of defining a
3380 * unique name for migration. TODO: Ideally we should implement
3381 * a naming scheme for Objects which are not DeviceStates, in
3382 * which case we can relax this restriction.
3384 owner_dev
= DEVICE(owner
);
3385 vmstate_register_ram(mr
, owner_dev
);
3389 * Support softmmu builds with CONFIG_FUZZ using a weak symbol and a stub for
3390 * the fuzz_dma_read_cb callback
3393 void __attribute__((weak
)) fuzz_dma_read_cb(size_t addr
,
3400 static const TypeInfo memory_region_info
= {
3401 .parent
= TYPE_OBJECT
,
3402 .name
= TYPE_MEMORY_REGION
,
3403 .class_size
= sizeof(MemoryRegionClass
),
3404 .instance_size
= sizeof(MemoryRegion
),
3405 .instance_init
= memory_region_initfn
,
3406 .instance_finalize
= memory_region_finalize
,
3409 static const TypeInfo iommu_memory_region_info
= {
3410 .parent
= TYPE_MEMORY_REGION
,
3411 .name
= TYPE_IOMMU_MEMORY_REGION
,
3412 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3413 .instance_size
= sizeof(IOMMUMemoryRegion
),
3414 .instance_init
= iommu_memory_region_initfn
,
3418 static const TypeInfo ram_discard_manager_info
= {
3419 .parent
= TYPE_INTERFACE
,
3420 .name
= TYPE_RAM_DISCARD_MANAGER
,
3421 .class_size
= sizeof(RamDiscardManagerClass
),
3424 static void memory_register_types(void)
3426 type_register_static(&memory_region_info
);
3427 type_register_static(&iommu_memory_region_info
);
3428 type_register_static(&ram_discard_manager_info
);
3431 type_init(memory_register_types
)