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"
36 #include "exec/address-spaces.h"
38 //#define DEBUG_UNASSIGNED
40 static unsigned memory_region_transaction_depth
;
41 static bool memory_region_update_pending
;
42 static bool ioeventfd_update_pending
;
43 unsigned int global_dirty_tracking
;
45 static QTAILQ_HEAD(, MemoryListener
) memory_listeners
46 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
48 static QTAILQ_HEAD(, AddressSpace
) address_spaces
49 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
51 static GHashTable
*flat_views
;
53 typedef struct AddrRange AddrRange
;
56 * Note that signed integers are needed for negative offsetting in aliases
57 * (large MemoryRegion::alias_offset).
64 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
66 return (AddrRange
) { start
, size
};
69 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
71 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
74 static Int128
addrrange_end(AddrRange r
)
76 return int128_add(r
.start
, r
.size
);
79 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
81 int128_addto(&range
.start
, delta
);
85 static bool addrrange_contains(AddrRange range
, Int128 addr
)
87 return int128_ge(addr
, range
.start
)
88 && int128_lt(addr
, addrrange_end(range
));
91 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
93 return addrrange_contains(r1
, r2
.start
)
94 || addrrange_contains(r2
, r1
.start
);
97 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
99 Int128 start
= int128_max(r1
.start
, r2
.start
);
100 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
101 return addrrange_make(start
, int128_sub(end
, start
));
104 enum ListenerDirection
{ Forward
, Reverse
};
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, link) { \
120 if (_listener->_callback) { \
121 _listener->_callback(_listener, ##_args); \
130 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
132 MemoryListener *_listener; \
134 switch (_direction) { \
136 QTAILQ_FOREACH(_listener, &(_as)->listeners, link_as) { \
137 if (_listener->_callback) { \
138 _listener->_callback(_listener, _section, ##_args); \
143 QTAILQ_FOREACH_REVERSE(_listener, &(_as)->listeners, link_as) { \
144 if (_listener->_callback) { \
145 _listener->_callback(_listener, _section, ##_args); \
154 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
155 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
157 MemoryRegionSection mrs = section_from_flat_range(fr, \
158 address_space_to_flatview(as)); \
159 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
162 struct CoalescedMemoryRange
{
164 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
167 struct MemoryRegionIoeventfd
{
174 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd
*a
,
175 MemoryRegionIoeventfd
*b
)
177 if (int128_lt(a
->addr
.start
, b
->addr
.start
)) {
179 } else if (int128_gt(a
->addr
.start
, b
->addr
.start
)) {
181 } else if (int128_lt(a
->addr
.size
, b
->addr
.size
)) {
183 } else if (int128_gt(a
->addr
.size
, b
->addr
.size
)) {
185 } else if (a
->match_data
< b
->match_data
) {
187 } else if (a
->match_data
> b
->match_data
) {
189 } else if (a
->match_data
) {
190 if (a
->data
< b
->data
) {
192 } else if (a
->data
> b
->data
) {
198 } else if (a
->e
> b
->e
) {
204 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd
*a
,
205 MemoryRegionIoeventfd
*b
)
207 if (int128_eq(a
->addr
.start
, b
->addr
.start
) &&
208 (!int128_nz(a
->addr
.size
) || !int128_nz(b
->addr
.size
) ||
209 (int128_eq(a
->addr
.size
, b
->addr
.size
) &&
210 (a
->match_data
== b
->match_data
) &&
211 ((a
->match_data
&& (a
->data
== b
->data
)) || !a
->match_data
) &&
218 /* Range of memory in the global map. Addresses are absolute. */
221 hwaddr offset_in_region
;
223 uint8_t dirty_log_mask
;
229 #define FOR_EACH_FLAT_RANGE(var, view) \
230 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
232 static inline MemoryRegionSection
233 section_from_flat_range(FlatRange
*fr
, FlatView
*fv
)
235 return (MemoryRegionSection
) {
238 .offset_within_region
= fr
->offset_in_region
,
239 .size
= fr
->addr
.size
,
240 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
241 .readonly
= fr
->readonly
,
242 .nonvolatile
= fr
->nonvolatile
,
246 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
248 return a
->mr
== b
->mr
249 && addrrange_equal(a
->addr
, b
->addr
)
250 && a
->offset_in_region
== b
->offset_in_region
251 && a
->romd_mode
== b
->romd_mode
252 && a
->readonly
== b
->readonly
253 && a
->nonvolatile
== b
->nonvolatile
;
256 static FlatView
*flatview_new(MemoryRegion
*mr_root
)
260 view
= g_new0(FlatView
, 1);
262 view
->root
= mr_root
;
263 memory_region_ref(mr_root
);
264 trace_flatview_new(view
, mr_root
);
269 /* Insert a range into a given position. Caller is responsible for maintaining
272 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
274 if (view
->nr
== view
->nr_allocated
) {
275 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
276 view
->ranges
= g_realloc(view
->ranges
,
277 view
->nr_allocated
* sizeof(*view
->ranges
));
279 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
280 (view
->nr
- pos
) * sizeof(FlatRange
));
281 view
->ranges
[pos
] = *range
;
282 memory_region_ref(range
->mr
);
286 static void flatview_destroy(FlatView
*view
)
290 trace_flatview_destroy(view
, view
->root
);
291 if (view
->dispatch
) {
292 address_space_dispatch_free(view
->dispatch
);
294 for (i
= 0; i
< view
->nr
; i
++) {
295 memory_region_unref(view
->ranges
[i
].mr
);
297 g_free(view
->ranges
);
298 memory_region_unref(view
->root
);
302 static bool flatview_ref(FlatView
*view
)
304 return qatomic_fetch_inc_nonzero(&view
->ref
) > 0;
307 void flatview_unref(FlatView
*view
)
309 if (qatomic_fetch_dec(&view
->ref
) == 1) {
310 trace_flatview_destroy_rcu(view
, view
->root
);
312 call_rcu(view
, flatview_destroy
, rcu
);
316 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
318 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
320 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
322 int128_make64(r2
->offset_in_region
))
323 && r1
->dirty_log_mask
== r2
->dirty_log_mask
324 && r1
->romd_mode
== r2
->romd_mode
325 && r1
->readonly
== r2
->readonly
326 && r1
->nonvolatile
== r2
->nonvolatile
;
329 /* Attempt to simplify a view by merging adjacent ranges */
330 static void flatview_simplify(FlatView
*view
)
335 while (i
< view
->nr
) {
338 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
339 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
343 for (k
= i
; k
< j
; k
++) {
344 memory_region_unref(view
->ranges
[k
].mr
);
346 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
347 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
352 static bool memory_region_big_endian(MemoryRegion
*mr
)
354 #if TARGET_BIG_ENDIAN
355 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
357 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
361 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, MemOp op
)
363 if ((op
& MO_BSWAP
) != devend_memop(mr
->ops
->endianness
)) {
364 switch (op
& MO_SIZE
) {
368 *data
= bswap16(*data
);
371 *data
= bswap32(*data
);
374 *data
= bswap64(*data
);
377 g_assert_not_reached();
382 static inline void memory_region_shift_read_access(uint64_t *value
,
388 *value
|= (tmp
& mask
) << shift
;
390 *value
|= (tmp
& mask
) >> -shift
;
394 static inline uint64_t memory_region_shift_write_access(uint64_t *value
,
401 tmp
= (*value
>> shift
) & mask
;
403 tmp
= (*value
<< -shift
) & mask
;
409 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
412 hwaddr abs_addr
= offset
;
414 abs_addr
+= mr
->addr
;
415 for (root
= mr
; root
->container
; ) {
416 root
= root
->container
;
417 abs_addr
+= root
->addr
;
423 static int get_cpu_index(void)
426 return current_cpu
->cpu_index
;
431 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
441 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
443 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
444 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_READ
)) {
445 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
446 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
447 memory_region_name(mr
));
449 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
453 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
464 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
466 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
467 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_READ
)) {
468 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
469 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
470 memory_region_name(mr
));
472 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
476 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
484 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
487 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
488 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_WRITE
)) {
489 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
490 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
491 memory_region_name(mr
));
493 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
497 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
505 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
508 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
509 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_WRITE
)) {
510 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
511 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
,
512 memory_region_name(mr
));
514 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
517 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
520 unsigned access_size_min
,
521 unsigned access_size_max
,
522 MemTxResult (*access_fn
)
533 uint64_t access_mask
;
534 unsigned access_size
;
536 MemTxResult r
= MEMTX_OK
;
538 if (!access_size_min
) {
541 if (!access_size_max
) {
545 /* FIXME: support unaligned access? */
546 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
547 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
548 if (memory_region_big_endian(mr
)) {
549 for (i
= 0; i
< size
; i
+= access_size
) {
550 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
551 (size
- access_size
- i
) * 8, access_mask
, attrs
);
554 for (i
= 0; i
< size
; i
+= access_size
) {
555 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
562 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
566 while (mr
->container
) {
569 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
570 if (mr
== as
->root
) {
577 /* Render a memory region into the global view. Ranges in @view obscure
580 static void render_memory_region(FlatView
*view
,
587 MemoryRegion
*subregion
;
589 hwaddr offset_in_region
;
599 int128_addto(&base
, int128_make64(mr
->addr
));
600 readonly
|= mr
->readonly
;
601 nonvolatile
|= mr
->nonvolatile
;
603 tmp
= addrrange_make(base
, mr
->size
);
605 if (!addrrange_intersects(tmp
, clip
)) {
609 clip
= addrrange_intersection(tmp
, clip
);
612 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
613 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
614 render_memory_region(view
, mr
->alias
, base
, clip
,
615 readonly
, nonvolatile
);
619 /* Render subregions in priority order. */
620 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
621 render_memory_region(view
, subregion
, base
, clip
,
622 readonly
, nonvolatile
);
625 if (!mr
->terminates
) {
629 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
634 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
635 fr
.romd_mode
= mr
->romd_mode
;
636 fr
.readonly
= readonly
;
637 fr
.nonvolatile
= nonvolatile
;
639 /* Render the region itself into any gaps left by the current view. */
640 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
641 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
644 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
645 now
= int128_min(remain
,
646 int128_sub(view
->ranges
[i
].addr
.start
, base
));
647 fr
.offset_in_region
= offset_in_region
;
648 fr
.addr
= addrrange_make(base
, now
);
649 flatview_insert(view
, i
, &fr
);
651 int128_addto(&base
, now
);
652 offset_in_region
+= int128_get64(now
);
653 int128_subfrom(&remain
, now
);
655 now
= int128_sub(int128_min(int128_add(base
, remain
),
656 addrrange_end(view
->ranges
[i
].addr
)),
658 int128_addto(&base
, now
);
659 offset_in_region
+= int128_get64(now
);
660 int128_subfrom(&remain
, now
);
662 if (int128_nz(remain
)) {
663 fr
.offset_in_region
= offset_in_region
;
664 fr
.addr
= addrrange_make(base
, remain
);
665 flatview_insert(view
, i
, &fr
);
669 void flatview_for_each_range(FlatView
*fv
, flatview_cb cb
, void *opaque
)
676 FOR_EACH_FLAT_RANGE(fr
, fv
) {
677 if (cb(fr
->addr
.start
, fr
->addr
.size
, fr
->mr
,
678 fr
->offset_in_region
, opaque
)) {
684 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
686 while (mr
->enabled
) {
688 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
689 /* The alias is included in its entirety. Use it as
690 * the "real" root, so that we can share more FlatViews.
695 } else if (!mr
->terminates
) {
696 unsigned int found
= 0;
697 MemoryRegion
*child
, *next
= NULL
;
698 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
699 if (child
->enabled
) {
704 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
705 /* A child is included in its entirety. If it's the only
706 * enabled one, use it in the hope of finding an alias down the
707 * way. This will also let us share FlatViews.
728 /* Render a memory topology into a list of disjoint absolute ranges. */
729 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
734 view
= flatview_new(mr
);
737 render_memory_region(view
, mr
, int128_zero(),
738 addrrange_make(int128_zero(), int128_2_64()),
741 flatview_simplify(view
);
743 view
->dispatch
= address_space_dispatch_new(view
);
744 for (i
= 0; i
< view
->nr
; i
++) {
745 MemoryRegionSection mrs
=
746 section_from_flat_range(&view
->ranges
[i
], view
);
747 flatview_add_to_dispatch(view
, &mrs
);
749 address_space_dispatch_compact(view
->dispatch
);
750 g_hash_table_replace(flat_views
, mr
, view
);
755 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
756 MemoryRegionIoeventfd
*fds_new
,
758 MemoryRegionIoeventfd
*fds_old
,
762 MemoryRegionIoeventfd
*fd
;
763 MemoryRegionSection section
;
765 /* Generate a symmetric difference of the old and new fd sets, adding
766 * and deleting as necessary.
770 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
771 if (iold
< fds_old_nb
772 && (inew
== fds_new_nb
773 || memory_region_ioeventfd_before(&fds_old
[iold
],
776 section
= (MemoryRegionSection
) {
777 .fv
= address_space_to_flatview(as
),
778 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
779 .size
= fd
->addr
.size
,
781 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
782 fd
->match_data
, fd
->data
, fd
->e
);
784 } else if (inew
< fds_new_nb
785 && (iold
== fds_old_nb
786 || memory_region_ioeventfd_before(&fds_new
[inew
],
789 section
= (MemoryRegionSection
) {
790 .fv
= address_space_to_flatview(as
),
791 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
792 .size
= fd
->addr
.size
,
794 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
795 fd
->match_data
, fd
->data
, fd
->e
);
804 FlatView
*address_space_get_flatview(AddressSpace
*as
)
808 RCU_READ_LOCK_GUARD();
810 view
= address_space_to_flatview(as
);
811 /* If somebody has replaced as->current_map concurrently,
812 * flatview_ref returns false.
814 } while (!flatview_ref(view
));
818 static void address_space_update_ioeventfds(AddressSpace
*as
)
822 unsigned ioeventfd_nb
= 0;
823 unsigned ioeventfd_max
;
824 MemoryRegionIoeventfd
*ioeventfds
;
829 * It is likely that the number of ioeventfds hasn't changed much, so use
830 * the previous size as the starting value, with some headroom to avoid
831 * gratuitous reallocations.
833 ioeventfd_max
= QEMU_ALIGN_UP(as
->ioeventfd_nb
, 4);
834 ioeventfds
= g_new(MemoryRegionIoeventfd
, ioeventfd_max
);
836 view
= address_space_get_flatview(as
);
837 FOR_EACH_FLAT_RANGE(fr
, view
) {
838 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
839 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
840 int128_sub(fr
->addr
.start
,
841 int128_make64(fr
->offset_in_region
)));
842 if (addrrange_intersects(fr
->addr
, tmp
)) {
844 if (ioeventfd_nb
> ioeventfd_max
) {
845 ioeventfd_max
= MAX(ioeventfd_max
* 2, 4);
846 ioeventfds
= g_realloc(ioeventfds
,
847 ioeventfd_max
* sizeof(*ioeventfds
));
849 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
850 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
855 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
856 as
->ioeventfds
, as
->ioeventfd_nb
);
858 g_free(as
->ioeventfds
);
859 as
->ioeventfds
= ioeventfds
;
860 as
->ioeventfd_nb
= ioeventfd_nb
;
861 flatview_unref(view
);
865 * Notify the memory listeners about the coalesced IO change events of
866 * range `cmr'. Only the part that has intersection of the specified
867 * FlatRange will be sent.
869 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
870 CoalescedMemoryRange
*cmr
, bool add
)
874 tmp
= addrrange_shift(cmr
->addr
,
875 int128_sub(fr
->addr
.start
,
876 int128_make64(fr
->offset_in_region
)));
877 if (!addrrange_intersects(tmp
, fr
->addr
)) {
880 tmp
= addrrange_intersection(tmp
, fr
->addr
);
883 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
884 int128_get64(tmp
.start
),
885 int128_get64(tmp
.size
));
887 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
888 int128_get64(tmp
.start
),
889 int128_get64(tmp
.size
));
893 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
895 CoalescedMemoryRange
*cmr
;
897 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
898 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
902 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
904 MemoryRegion
*mr
= fr
->mr
;
905 CoalescedMemoryRange
*cmr
;
907 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
911 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
912 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
916 static void address_space_update_topology_pass(AddressSpace
*as
,
917 const FlatView
*old_view
,
918 const FlatView
*new_view
,
922 FlatRange
*frold
, *frnew
;
924 /* Generate a symmetric difference of the old and new memory maps.
925 * Kill ranges in the old map, and instantiate ranges in the new map.
928 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
929 if (iold
< old_view
->nr
) {
930 frold
= &old_view
->ranges
[iold
];
934 if (inew
< new_view
->nr
) {
935 frnew
= &new_view
->ranges
[inew
];
942 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
943 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
944 && !flatrange_equal(frold
, frnew
)))) {
945 /* In old but not in new, or in both but attributes changed. */
948 flat_range_coalesced_io_del(frold
, as
);
949 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
953 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
954 /* In both and unchanged (except logging may have changed) */
957 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
958 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
959 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
960 frold
->dirty_log_mask
,
961 frnew
->dirty_log_mask
);
963 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
964 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
965 frold
->dirty_log_mask
,
966 frnew
->dirty_log_mask
);
976 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
977 flat_range_coalesced_io_add(frnew
, as
);
985 static void flatviews_init(void)
987 static FlatView
*empty_view
;
993 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
994 (GDestroyNotify
) flatview_unref
);
996 empty_view
= generate_memory_topology(NULL
);
997 /* We keep it alive forever in the global variable. */
998 flatview_ref(empty_view
);
1000 g_hash_table_replace(flat_views
, NULL
, empty_view
);
1001 flatview_ref(empty_view
);
1005 static void flatviews_reset(void)
1010 g_hash_table_unref(flat_views
);
1015 /* Render unique FVs */
1016 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1017 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1019 if (g_hash_table_lookup(flat_views
, physmr
)) {
1023 generate_memory_topology(physmr
);
1027 static void address_space_set_flatview(AddressSpace
*as
)
1029 FlatView
*old_view
= address_space_to_flatview(as
);
1030 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1031 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1035 if (old_view
== new_view
) {
1040 flatview_ref(old_view
);
1043 flatview_ref(new_view
);
1045 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1046 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1049 old_view2
= &tmpview
;
1051 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1052 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1055 /* Writes are protected by the BQL. */
1056 qatomic_rcu_set(&as
->current_map
, new_view
);
1058 flatview_unref(old_view
);
1061 /* Note that all the old MemoryRegions are still alive up to this
1062 * point. This relieves most MemoryListeners from the need to
1063 * ref/unref the MemoryRegions they get---unless they use them
1064 * outside the iothread mutex, in which case precise reference
1065 * counting is necessary.
1068 flatview_unref(old_view
);
1072 static void address_space_update_topology(AddressSpace
*as
)
1074 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1077 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1078 generate_memory_topology(physmr
);
1080 address_space_set_flatview(as
);
1083 void memory_region_transaction_begin(void)
1085 qemu_flush_coalesced_mmio_buffer();
1086 ++memory_region_transaction_depth
;
1089 void memory_region_transaction_commit(void)
1093 assert(memory_region_transaction_depth
);
1094 assert(qemu_mutex_iothread_locked());
1096 --memory_region_transaction_depth
;
1097 if (!memory_region_transaction_depth
) {
1098 if (memory_region_update_pending
) {
1101 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1103 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1104 address_space_set_flatview(as
);
1105 address_space_update_ioeventfds(as
);
1107 memory_region_update_pending
= false;
1108 ioeventfd_update_pending
= false;
1109 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1110 } else if (ioeventfd_update_pending
) {
1111 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1112 address_space_update_ioeventfds(as
);
1114 ioeventfd_update_pending
= false;
1119 static void memory_region_destructor_none(MemoryRegion
*mr
)
1123 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1125 qemu_ram_free(mr
->ram_block
);
1128 static bool memory_region_need_escape(char c
)
1130 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1133 static char *memory_region_escape_name(const char *name
)
1140 for (p
= name
; *p
; p
++) {
1141 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1143 if (bytes
== p
- name
) {
1144 return g_memdup(name
, bytes
+ 1);
1147 escaped
= g_malloc(bytes
+ 1);
1148 for (p
= name
, q
= escaped
; *p
; p
++) {
1150 if (unlikely(memory_region_need_escape(c
))) {
1153 *q
++ = "0123456789abcdef"[c
>> 4];
1154 c
= "0123456789abcdef"[c
& 15];
1162 static void memory_region_do_init(MemoryRegion
*mr
,
1167 mr
->size
= int128_make64(size
);
1168 if (size
== UINT64_MAX
) {
1169 mr
->size
= int128_2_64();
1171 mr
->name
= g_strdup(name
);
1173 mr
->ram_block
= NULL
;
1176 char *escaped_name
= memory_region_escape_name(name
);
1177 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1180 owner
= container_get(qdev_get_machine(), "/unattached");
1183 object_property_add_child(owner
, name_array
, OBJECT(mr
));
1184 object_unref(OBJECT(mr
));
1186 g_free(escaped_name
);
1190 void memory_region_init(MemoryRegion
*mr
,
1195 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1196 memory_region_do_init(mr
, owner
, name
, size
);
1199 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1200 const char *name
, void *opaque
,
1203 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1204 char *path
= (char *)"";
1206 if (mr
->container
) {
1207 path
= object_get_canonical_path(OBJECT(mr
->container
));
1209 visit_type_str(v
, name
, &path
, errp
);
1210 if (mr
->container
) {
1215 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1218 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1220 return OBJECT(mr
->container
);
1223 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1224 const char *name
, void *opaque
,
1227 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1228 int32_t value
= mr
->priority
;
1230 visit_type_int32(v
, name
, &value
, errp
);
1233 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1234 void *opaque
, Error
**errp
)
1236 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1237 uint64_t value
= memory_region_size(mr
);
1239 visit_type_uint64(v
, name
, &value
, errp
);
1242 static void memory_region_initfn(Object
*obj
)
1244 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1247 mr
->ops
= &unassigned_mem_ops
;
1249 mr
->romd_mode
= true;
1250 mr
->destructor
= memory_region_destructor_none
;
1251 QTAILQ_INIT(&mr
->subregions
);
1252 QTAILQ_INIT(&mr
->coalesced
);
1254 op
= object_property_add(OBJECT(mr
), "container",
1255 "link<" TYPE_MEMORY_REGION
">",
1256 memory_region_get_container
,
1257 NULL
, /* memory_region_set_container */
1259 op
->resolve
= memory_region_resolve_container
;
1261 object_property_add_uint64_ptr(OBJECT(mr
), "addr",
1262 &mr
->addr
, OBJ_PROP_FLAG_READ
);
1263 object_property_add(OBJECT(mr
), "priority", "uint32",
1264 memory_region_get_priority
,
1265 NULL
, /* memory_region_set_priority */
1267 object_property_add(OBJECT(mr
), "size", "uint64",
1268 memory_region_get_size
,
1269 NULL
, /* memory_region_set_size, */
1273 static void iommu_memory_region_initfn(Object
*obj
)
1275 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1277 mr
->is_iommu
= true;
1280 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1283 #ifdef DEBUG_UNASSIGNED
1284 printf("Unassigned mem read " HWADDR_FMT_plx
"\n", addr
);
1289 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1290 uint64_t val
, unsigned size
)
1292 #ifdef DEBUG_UNASSIGNED
1293 printf("Unassigned mem write " HWADDR_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1297 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1298 unsigned size
, bool is_write
,
1304 const MemoryRegionOps unassigned_mem_ops
= {
1305 .valid
.accepts
= unassigned_mem_accepts
,
1306 .endianness
= DEVICE_NATIVE_ENDIAN
,
1309 static uint64_t memory_region_ram_device_read(void *opaque
,
1310 hwaddr addr
, unsigned size
)
1312 MemoryRegion
*mr
= opaque
;
1313 uint64_t data
= (uint64_t)~0;
1317 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1320 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1323 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1326 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1330 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1335 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1336 uint64_t data
, unsigned size
)
1338 MemoryRegion
*mr
= opaque
;
1340 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1344 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1347 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1350 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1353 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1358 static const MemoryRegionOps ram_device_mem_ops
= {
1359 .read
= memory_region_ram_device_read
,
1360 .write
= memory_region_ram_device_write
,
1361 .endianness
= DEVICE_HOST_ENDIAN
,
1363 .min_access_size
= 1,
1364 .max_access_size
= 8,
1368 .min_access_size
= 1,
1369 .max_access_size
= 8,
1374 bool memory_region_access_valid(MemoryRegion
*mr
,
1380 if (mr
->ops
->valid
.accepts
1381 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
, attrs
)) {
1382 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid %s at addr 0x%" HWADDR_PRIX
1383 ", size %u, region '%s', reason: rejected\n",
1384 is_write
? "write" : "read",
1385 addr
, size
, memory_region_name(mr
));
1389 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1390 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid %s at addr 0x%" HWADDR_PRIX
1391 ", size %u, region '%s', reason: unaligned\n",
1392 is_write
? "write" : "read",
1393 addr
, size
, memory_region_name(mr
));
1397 /* Treat zero as compatibility all valid */
1398 if (!mr
->ops
->valid
.max_access_size
) {
1402 if (size
> mr
->ops
->valid
.max_access_size
1403 || size
< mr
->ops
->valid
.min_access_size
) {
1404 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid %s at addr 0x%" HWADDR_PRIX
1405 ", size %u, region '%s', reason: invalid size "
1406 "(min:%u max:%u)\n",
1407 is_write
? "write" : "read",
1408 addr
, size
, memory_region_name(mr
),
1409 mr
->ops
->valid
.min_access_size
,
1410 mr
->ops
->valid
.max_access_size
);
1416 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1424 if (mr
->ops
->read
) {
1425 return access_with_adjusted_size(addr
, pval
, size
,
1426 mr
->ops
->impl
.min_access_size
,
1427 mr
->ops
->impl
.max_access_size
,
1428 memory_region_read_accessor
,
1431 return access_with_adjusted_size(addr
, pval
, size
,
1432 mr
->ops
->impl
.min_access_size
,
1433 mr
->ops
->impl
.max_access_size
,
1434 memory_region_read_with_attrs_accessor
,
1439 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1445 unsigned size
= memop_size(op
);
1449 return memory_region_dispatch_read(mr
->alias
,
1450 mr
->alias_offset
+ addr
,
1453 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1454 *pval
= unassigned_mem_read(mr
, addr
, size
);
1455 return MEMTX_DECODE_ERROR
;
1458 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1459 adjust_endianness(mr
, pval
, op
);
1463 /* Return true if an eventfd was signalled */
1464 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1470 MemoryRegionIoeventfd ioeventfd
= {
1471 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1476 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1477 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1478 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1480 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1481 event_notifier_set(ioeventfd
.e
);
1489 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1495 unsigned size
= memop_size(op
);
1498 return memory_region_dispatch_write(mr
->alias
,
1499 mr
->alias_offset
+ addr
,
1502 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1503 unassigned_mem_write(mr
, addr
, data
, size
);
1504 return MEMTX_DECODE_ERROR
;
1507 adjust_endianness(mr
, &data
, op
);
1509 if ((!kvm_eventfds_enabled()) &&
1510 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1514 if (mr
->ops
->write
) {
1515 return access_with_adjusted_size(addr
, &data
, size
,
1516 mr
->ops
->impl
.min_access_size
,
1517 mr
->ops
->impl
.max_access_size
,
1518 memory_region_write_accessor
, mr
,
1522 access_with_adjusted_size(addr
, &data
, size
,
1523 mr
->ops
->impl
.min_access_size
,
1524 mr
->ops
->impl
.max_access_size
,
1525 memory_region_write_with_attrs_accessor
,
1530 void memory_region_init_io(MemoryRegion
*mr
,
1532 const MemoryRegionOps
*ops
,
1537 memory_region_init(mr
, owner
, name
, size
);
1538 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1539 mr
->opaque
= opaque
;
1540 mr
->terminates
= true;
1543 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1549 memory_region_init_ram_flags_nomigrate(mr
, owner
, name
, size
, 0, errp
);
1552 void memory_region_init_ram_flags_nomigrate(MemoryRegion
*mr
,
1560 memory_region_init(mr
, owner
, name
, size
);
1562 mr
->terminates
= true;
1563 mr
->destructor
= memory_region_destructor_ram
;
1564 mr
->ram_block
= qemu_ram_alloc(size
, ram_flags
, mr
, &err
);
1566 mr
->size
= int128_zero();
1567 object_unparent(OBJECT(mr
));
1568 error_propagate(errp
, err
);
1572 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1577 void (*resized
)(const char*,
1583 memory_region_init(mr
, owner
, name
, size
);
1585 mr
->terminates
= true;
1586 mr
->destructor
= memory_region_destructor_ram
;
1587 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1590 mr
->size
= int128_zero();
1591 object_unparent(OBJECT(mr
));
1592 error_propagate(errp
, err
);
1597 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1608 memory_region_init(mr
, owner
, name
, size
);
1610 mr
->readonly
= readonly
;
1611 mr
->terminates
= true;
1612 mr
->destructor
= memory_region_destructor_ram
;
1614 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
,
1617 mr
->size
= int128_zero();
1618 object_unparent(OBJECT(mr
));
1619 error_propagate(errp
, err
);
1623 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1633 memory_region_init(mr
, owner
, name
, size
);
1635 mr
->terminates
= true;
1636 mr
->destructor
= memory_region_destructor_ram
;
1637 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
, ram_flags
, fd
, offset
,
1640 mr
->size
= int128_zero();
1641 object_unparent(OBJECT(mr
));
1642 error_propagate(errp
, err
);
1647 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1653 memory_region_init(mr
, owner
, name
, size
);
1655 mr
->terminates
= true;
1656 mr
->destructor
= memory_region_destructor_ram
;
1658 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1659 assert(ptr
!= NULL
);
1660 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1663 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1669 memory_region_init(mr
, owner
, name
, size
);
1671 mr
->terminates
= true;
1672 mr
->ram_device
= true;
1673 mr
->ops
= &ram_device_mem_ops
;
1675 mr
->destructor
= memory_region_destructor_ram
;
1677 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1678 assert(ptr
!= NULL
);
1679 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1682 void memory_region_init_alias(MemoryRegion
*mr
,
1689 memory_region_init(mr
, owner
, name
, size
);
1691 mr
->alias_offset
= offset
;
1694 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1700 memory_region_init_ram_flags_nomigrate(mr
, owner
, name
, size
, 0, errp
);
1701 mr
->readonly
= true;
1704 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1706 const MemoryRegionOps
*ops
,
1714 memory_region_init(mr
, owner
, name
, size
);
1716 mr
->opaque
= opaque
;
1717 mr
->terminates
= true;
1718 mr
->rom_device
= true;
1719 mr
->destructor
= memory_region_destructor_ram
;
1720 mr
->ram_block
= qemu_ram_alloc(size
, 0, mr
, &err
);
1722 mr
->size
= int128_zero();
1723 object_unparent(OBJECT(mr
));
1724 error_propagate(errp
, err
);
1728 void memory_region_init_iommu(void *_iommu_mr
,
1729 size_t instance_size
,
1730 const char *mrtypename
,
1735 struct IOMMUMemoryRegion
*iommu_mr
;
1736 struct MemoryRegion
*mr
;
1738 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1739 mr
= MEMORY_REGION(_iommu_mr
);
1740 memory_region_do_init(mr
, owner
, name
, size
);
1741 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1742 mr
->terminates
= true; /* then re-forwards */
1743 QLIST_INIT(&iommu_mr
->iommu_notify
);
1744 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1747 static void memory_region_finalize(Object
*obj
)
1749 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1751 assert(!mr
->container
);
1753 /* We know the region is not visible in any address space (it
1754 * does not have a container and cannot be a root either because
1755 * it has no references, so we can blindly clear mr->enabled.
1756 * memory_region_set_enabled instead could trigger a transaction
1757 * and cause an infinite loop.
1759 mr
->enabled
= false;
1760 memory_region_transaction_begin();
1761 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1762 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1763 memory_region_del_subregion(mr
, subregion
);
1765 memory_region_transaction_commit();
1768 memory_region_clear_coalescing(mr
);
1769 g_free((char *)mr
->name
);
1770 g_free(mr
->ioeventfds
);
1773 Object
*memory_region_owner(MemoryRegion
*mr
)
1775 Object
*obj
= OBJECT(mr
);
1779 void memory_region_ref(MemoryRegion
*mr
)
1781 /* MMIO callbacks most likely will access data that belongs
1782 * to the owner, hence the need to ref/unref the owner whenever
1783 * the memory region is in use.
1785 * The memory region is a child of its owner. As long as the
1786 * owner doesn't call unparent itself on the memory region,
1787 * ref-ing the owner will also keep the memory region alive.
1788 * Memory regions without an owner are supposed to never go away;
1789 * we do not ref/unref them because it slows down DMA sensibly.
1791 if (mr
&& mr
->owner
) {
1792 object_ref(mr
->owner
);
1796 void memory_region_unref(MemoryRegion
*mr
)
1798 if (mr
&& mr
->owner
) {
1799 object_unref(mr
->owner
);
1803 uint64_t memory_region_size(MemoryRegion
*mr
)
1805 if (int128_eq(mr
->size
, int128_2_64())) {
1808 return int128_get64(mr
->size
);
1811 const char *memory_region_name(const MemoryRegion
*mr
)
1814 ((MemoryRegion
*)mr
)->name
=
1815 g_strdup(object_get_canonical_path_component(OBJECT(mr
)));
1820 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1822 return mr
->ram_device
;
1825 bool memory_region_is_protected(MemoryRegion
*mr
)
1827 return mr
->ram
&& (mr
->ram_block
->flags
& RAM_PROTECTED
);
1830 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1832 uint8_t mask
= mr
->dirty_log_mask
;
1833 RAMBlock
*rb
= mr
->ram_block
;
1835 if (global_dirty_tracking
&& ((rb
&& qemu_ram_is_migratable(rb
)) ||
1836 memory_region_is_iommu(mr
))) {
1837 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1840 if (tcg_enabled() && rb
) {
1841 /* TCG only cares about dirty memory logging for RAM, not IOMMU. */
1842 mask
|= (1 << DIRTY_MEMORY_CODE
);
1847 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1849 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1852 static int memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
,
1855 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1856 IOMMUNotifier
*iommu_notifier
;
1857 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1860 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1861 flags
|= iommu_notifier
->notifier_flags
;
1864 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1865 ret
= imrc
->notify_flag_changed(iommu_mr
,
1866 iommu_mr
->iommu_notify_flags
,
1871 iommu_mr
->iommu_notify_flags
= flags
;
1876 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion
*iommu_mr
,
1877 uint64_t page_size_mask
,
1880 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1883 if (imrc
->iommu_set_page_size_mask
) {
1884 ret
= imrc
->iommu_set_page_size_mask(iommu_mr
, page_size_mask
, errp
);
1889 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1890 IOMMUNotifier
*n
, Error
**errp
)
1892 IOMMUMemoryRegion
*iommu_mr
;
1896 return memory_region_register_iommu_notifier(mr
->alias
, n
, errp
);
1899 /* We need to register for at least one bitfield */
1900 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1901 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1902 assert(n
->start
<= n
->end
);
1903 assert(n
->iommu_idx
>= 0 &&
1904 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1906 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1907 ret
= memory_region_update_iommu_notify_flags(iommu_mr
, errp
);
1909 QLIST_REMOVE(n
, node
);
1914 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1916 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1918 if (imrc
->get_min_page_size
) {
1919 return imrc
->get_min_page_size(iommu_mr
);
1921 return TARGET_PAGE_SIZE
;
1924 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1926 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1927 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1928 hwaddr addr
, granularity
;
1929 IOMMUTLBEntry iotlb
;
1931 /* If the IOMMU has its own replay callback, override */
1933 imrc
->replay(iommu_mr
, n
);
1937 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1939 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1940 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1941 if (iotlb
.perm
!= IOMMU_NONE
) {
1942 n
->notify(n
, &iotlb
);
1945 /* if (2^64 - MR size) < granularity, it's possible to get an
1946 * infinite loop here. This should catch such a wraparound */
1947 if ((addr
+ granularity
) < addr
) {
1953 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1956 IOMMUMemoryRegion
*iommu_mr
;
1959 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1962 QLIST_REMOVE(n
, node
);
1963 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1964 memory_region_update_iommu_notify_flags(iommu_mr
, NULL
);
1967 void memory_region_notify_iommu_one(IOMMUNotifier
*notifier
,
1968 IOMMUTLBEvent
*event
)
1970 IOMMUTLBEntry
*entry
= &event
->entry
;
1971 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1972 IOMMUTLBEntry tmp
= *entry
;
1974 if (event
->type
== IOMMU_NOTIFIER_UNMAP
) {
1975 assert(entry
->perm
== IOMMU_NONE
);
1979 * Skip the notification if the notification does not overlap
1980 * with registered range.
1982 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1986 if (notifier
->notifier_flags
& IOMMU_NOTIFIER_DEVIOTLB_UNMAP
) {
1987 /* Crop (iova, addr_mask) to range */
1988 tmp
.iova
= MAX(tmp
.iova
, notifier
->start
);
1989 tmp
.addr_mask
= MIN(entry_end
, notifier
->end
) - tmp
.iova
;
1991 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1994 if (event
->type
& notifier
->notifier_flags
) {
1995 notifier
->notify(notifier
, &tmp
);
1999 void memory_region_unmap_iommu_notifier_range(IOMMUNotifier
*notifier
)
2001 IOMMUTLBEvent event
;
2003 event
.type
= IOMMU_NOTIFIER_UNMAP
;
2004 event
.entry
.target_as
= &address_space_memory
;
2005 event
.entry
.iova
= notifier
->start
;
2006 event
.entry
.perm
= IOMMU_NONE
;
2007 event
.entry
.addr_mask
= notifier
->end
- notifier
->start
;
2009 memory_region_notify_iommu_one(notifier
, &event
);
2012 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
2014 IOMMUTLBEvent event
)
2016 IOMMUNotifier
*iommu_notifier
;
2018 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
2020 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
2021 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
2022 memory_region_notify_iommu_one(iommu_notifier
, &event
);
2027 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
2028 enum IOMMUMemoryRegionAttr attr
,
2031 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2033 if (!imrc
->get_attr
) {
2037 return imrc
->get_attr(iommu_mr
, attr
, data
);
2040 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
2043 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2045 if (!imrc
->attrs_to_index
) {
2049 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2052 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2054 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2056 if (!imrc
->num_indexes
) {
2060 return imrc
->num_indexes(iommu_mr
);
2063 RamDiscardManager
*memory_region_get_ram_discard_manager(MemoryRegion
*mr
)
2065 if (!memory_region_is_mapped(mr
) || !memory_region_is_ram(mr
)) {
2071 void memory_region_set_ram_discard_manager(MemoryRegion
*mr
,
2072 RamDiscardManager
*rdm
)
2074 g_assert(memory_region_is_ram(mr
) && !memory_region_is_mapped(mr
));
2075 g_assert(!rdm
|| !mr
->rdm
);
2079 uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager
*rdm
,
2080 const MemoryRegion
*mr
)
2082 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2084 g_assert(rdmc
->get_min_granularity
);
2085 return rdmc
->get_min_granularity(rdm
, mr
);
2088 bool ram_discard_manager_is_populated(const RamDiscardManager
*rdm
,
2089 const MemoryRegionSection
*section
)
2091 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2093 g_assert(rdmc
->is_populated
);
2094 return rdmc
->is_populated(rdm
, section
);
2097 int ram_discard_manager_replay_populated(const RamDiscardManager
*rdm
,
2098 MemoryRegionSection
*section
,
2099 ReplayRamPopulate replay_fn
,
2102 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2104 g_assert(rdmc
->replay_populated
);
2105 return rdmc
->replay_populated(rdm
, section
, replay_fn
, opaque
);
2108 void ram_discard_manager_replay_discarded(const RamDiscardManager
*rdm
,
2109 MemoryRegionSection
*section
,
2110 ReplayRamDiscard replay_fn
,
2113 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2115 g_assert(rdmc
->replay_discarded
);
2116 rdmc
->replay_discarded(rdm
, section
, replay_fn
, opaque
);
2119 void ram_discard_manager_register_listener(RamDiscardManager
*rdm
,
2120 RamDiscardListener
*rdl
,
2121 MemoryRegionSection
*section
)
2123 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2125 g_assert(rdmc
->register_listener
);
2126 rdmc
->register_listener(rdm
, rdl
, section
);
2129 void ram_discard_manager_unregister_listener(RamDiscardManager
*rdm
,
2130 RamDiscardListener
*rdl
)
2132 RamDiscardManagerClass
*rdmc
= RAM_DISCARD_MANAGER_GET_CLASS(rdm
);
2134 g_assert(rdmc
->unregister_listener
);
2135 rdmc
->unregister_listener(rdm
, rdl
);
2138 /* Called with rcu_read_lock held. */
2139 bool memory_get_xlat_addr(IOMMUTLBEntry
*iotlb
, void **vaddr
,
2140 ram_addr_t
*ram_addr
, bool *read_only
,
2141 bool *mr_has_discard_manager
)
2145 hwaddr len
= iotlb
->addr_mask
+ 1;
2146 bool writable
= iotlb
->perm
& IOMMU_WO
;
2148 if (mr_has_discard_manager
) {
2149 *mr_has_discard_manager
= false;
2152 * The IOMMU TLB entry we have just covers translation through
2153 * this IOMMU to its immediate target. We need to translate
2154 * it the rest of the way through to memory.
2156 mr
= address_space_translate(&address_space_memory
, iotlb
->translated_addr
,
2157 &xlat
, &len
, writable
, MEMTXATTRS_UNSPECIFIED
);
2158 if (!memory_region_is_ram(mr
)) {
2159 error_report("iommu map to non memory area %" HWADDR_PRIx
"", xlat
);
2161 } else if (memory_region_has_ram_discard_manager(mr
)) {
2162 RamDiscardManager
*rdm
= memory_region_get_ram_discard_manager(mr
);
2163 MemoryRegionSection tmp
= {
2165 .offset_within_region
= xlat
,
2166 .size
= int128_make64(len
),
2168 if (mr_has_discard_manager
) {
2169 *mr_has_discard_manager
= true;
2172 * Malicious VMs can map memory into the IOMMU, which is expected
2173 * to remain discarded. vfio will pin all pages, populating memory.
2174 * Disallow that. vmstate priorities make sure any RamDiscardManager
2175 * were already restored before IOMMUs are restored.
2177 if (!ram_discard_manager_is_populated(rdm
, &tmp
)) {
2178 error_report("iommu map to discarded memory (e.g., unplugged via"
2179 " virtio-mem): %" HWADDR_PRIx
"",
2180 iotlb
->translated_addr
);
2186 * Translation truncates length to the IOMMU page size,
2187 * check that it did not truncate too much.
2189 if (len
& iotlb
->addr_mask
) {
2190 error_report("iommu has granularity incompatible with target AS");
2195 *vaddr
= memory_region_get_ram_ptr(mr
) + xlat
;
2199 *ram_addr
= memory_region_get_ram_addr(mr
) + xlat
;
2203 *read_only
= !writable
|| mr
->readonly
;
2209 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2211 uint8_t mask
= 1 << client
;
2212 uint8_t old_logging
;
2214 assert(client
== DIRTY_MEMORY_VGA
);
2215 old_logging
= mr
->vga_logging_count
;
2216 mr
->vga_logging_count
+= log
? 1 : -1;
2217 if (!!old_logging
== !!mr
->vga_logging_count
) {
2221 memory_region_transaction_begin();
2222 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2223 memory_region_update_pending
|= mr
->enabled
;
2224 memory_region_transaction_commit();
2227 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2230 assert(mr
->ram_block
);
2231 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2233 memory_region_get_dirty_log_mask(mr
));
2237 * If memory region `mr' is NULL, do global sync. Otherwise, sync
2238 * dirty bitmap for the specified memory region.
2240 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2242 MemoryListener
*listener
;
2247 /* If the same address space has multiple log_sync listeners, we
2248 * visit that address space's FlatView multiple times. But because
2249 * log_sync listeners are rare, it's still cheaper than walking each
2250 * address space once.
2252 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2253 if (listener
->log_sync
) {
2254 as
= listener
->address_space
;
2255 view
= address_space_get_flatview(as
);
2256 FOR_EACH_FLAT_RANGE(fr
, view
) {
2257 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2258 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2259 listener
->log_sync(listener
, &mrs
);
2262 flatview_unref(view
);
2263 trace_memory_region_sync_dirty(mr
? mr
->name
: "(all)", listener
->name
, 0);
2264 } else if (listener
->log_sync_global
) {
2266 * No matter whether MR is specified, what we can do here
2267 * is to do a global sync, because we are not capable to
2268 * sync in a finer granularity.
2270 listener
->log_sync_global(listener
);
2271 trace_memory_region_sync_dirty(mr
? mr
->name
: "(all)", listener
->name
, 1);
2276 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2279 MemoryRegionSection mrs
;
2280 MemoryListener
*listener
;
2284 hwaddr sec_start
, sec_end
, sec_size
;
2286 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2287 if (!listener
->log_clear
) {
2290 as
= listener
->address_space
;
2291 view
= address_space_get_flatview(as
);
2292 FOR_EACH_FLAT_RANGE(fr
, view
) {
2293 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2295 * Clear dirty bitmap operation only applies to those
2296 * regions whose dirty logging is at least enabled
2301 mrs
= section_from_flat_range(fr
, view
);
2303 sec_start
= MAX(mrs
.offset_within_region
, start
);
2304 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2305 sec_end
= MIN(sec_end
, start
+ len
);
2307 if (sec_start
>= sec_end
) {
2309 * If this memory region section has no intersection
2310 * with the requested range, skip.
2315 /* Valid case; shrink the section if needed */
2316 mrs
.offset_within_address_space
+=
2317 sec_start
- mrs
.offset_within_region
;
2318 mrs
.offset_within_region
= sec_start
;
2319 sec_size
= sec_end
- sec_start
;
2320 mrs
.size
= int128_make64(sec_size
);
2321 listener
->log_clear(listener
, &mrs
);
2323 flatview_unref(view
);
2327 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2332 DirtyBitmapSnapshot
*snapshot
;
2333 assert(mr
->ram_block
);
2334 memory_region_sync_dirty_bitmap(mr
);
2335 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2336 memory_global_after_dirty_log_sync();
2340 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2341 hwaddr addr
, hwaddr size
)
2343 assert(mr
->ram_block
);
2344 return cpu_physical_memory_snapshot_get_dirty(snap
,
2345 memory_region_get_ram_addr(mr
) + addr
, size
);
2348 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2350 if (mr
->readonly
!= readonly
) {
2351 memory_region_transaction_begin();
2352 mr
->readonly
= readonly
;
2353 memory_region_update_pending
|= mr
->enabled
;
2354 memory_region_transaction_commit();
2358 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2360 if (mr
->nonvolatile
!= nonvolatile
) {
2361 memory_region_transaction_begin();
2362 mr
->nonvolatile
= nonvolatile
;
2363 memory_region_update_pending
|= mr
->enabled
;
2364 memory_region_transaction_commit();
2368 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2370 if (mr
->romd_mode
!= romd_mode
) {
2371 memory_region_transaction_begin();
2372 mr
->romd_mode
= romd_mode
;
2373 memory_region_update_pending
|= mr
->enabled
;
2374 memory_region_transaction_commit();
2378 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2379 hwaddr size
, unsigned client
)
2381 assert(mr
->ram_block
);
2382 cpu_physical_memory_test_and_clear_dirty(
2383 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2386 int memory_region_get_fd(MemoryRegion
*mr
)
2388 RCU_READ_LOCK_GUARD();
2392 return mr
->ram_block
->fd
;
2395 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2397 uint64_t offset
= 0;
2399 RCU_READ_LOCK_GUARD();
2401 offset
+= mr
->alias_offset
;
2404 assert(mr
->ram_block
);
2405 return qemu_map_ram_ptr(mr
->ram_block
, offset
);
2408 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2412 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2420 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2422 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2425 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2427 assert(mr
->ram_block
);
2429 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2432 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
2434 if (mr
->ram_block
) {
2435 qemu_ram_msync(mr
->ram_block
, addr
, size
);
2439 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
2442 * Might be extended case needed to cover
2443 * different types of memory regions
2445 if (mr
->dirty_log_mask
) {
2446 memory_region_msync(mr
, addr
, size
);
2451 * Call proper memory listeners about the change on the newly
2452 * added/removed CoalescedMemoryRange.
2454 static void memory_region_update_coalesced_range(MemoryRegion
*mr
,
2455 CoalescedMemoryRange
*cmr
,
2462 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2463 view
= address_space_get_flatview(as
);
2464 FOR_EACH_FLAT_RANGE(fr
, view
) {
2466 flat_range_coalesced_io_notify(fr
, as
, cmr
, add
);
2469 flatview_unref(view
);
2473 void memory_region_set_coalescing(MemoryRegion
*mr
)
2475 memory_region_clear_coalescing(mr
);
2476 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2479 void memory_region_add_coalescing(MemoryRegion
*mr
,
2483 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2485 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2486 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2487 memory_region_update_coalesced_range(mr
, cmr
, true);
2488 memory_region_set_flush_coalesced(mr
);
2491 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2493 CoalescedMemoryRange
*cmr
;
2495 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2499 qemu_flush_coalesced_mmio_buffer();
2500 mr
->flush_coalesced_mmio
= false;
2502 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2503 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2504 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2505 memory_region_update_coalesced_range(mr
, cmr
, false);
2510 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2512 mr
->flush_coalesced_mmio
= true;
2515 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2517 qemu_flush_coalesced_mmio_buffer();
2518 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2519 mr
->flush_coalesced_mmio
= false;
2523 static bool userspace_eventfd_warning
;
2525 void memory_region_add_eventfd(MemoryRegion
*mr
,
2532 MemoryRegionIoeventfd mrfd
= {
2533 .addr
.start
= int128_make64(addr
),
2534 .addr
.size
= int128_make64(size
),
2535 .match_data
= match_data
,
2541 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2542 userspace_eventfd_warning
))) {
2543 userspace_eventfd_warning
= true;
2544 error_report("Using eventfd without MMIO binding in KVM. "
2545 "Suboptimal performance expected");
2549 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2551 memory_region_transaction_begin();
2552 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2553 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2558 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2559 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2560 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2561 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2562 mr
->ioeventfds
[i
] = mrfd
;
2563 ioeventfd_update_pending
|= mr
->enabled
;
2564 memory_region_transaction_commit();
2567 void memory_region_del_eventfd(MemoryRegion
*mr
,
2574 MemoryRegionIoeventfd mrfd
= {
2575 .addr
.start
= int128_make64(addr
),
2576 .addr
.size
= int128_make64(size
),
2577 .match_data
= match_data
,
2584 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2586 memory_region_transaction_begin();
2587 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2588 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2592 assert(i
!= mr
->ioeventfd_nb
);
2593 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2594 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2596 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2597 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2598 ioeventfd_update_pending
|= mr
->enabled
;
2599 memory_region_transaction_commit();
2602 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2604 MemoryRegion
*mr
= subregion
->container
;
2605 MemoryRegion
*other
;
2607 memory_region_transaction_begin();
2609 memory_region_ref(subregion
);
2610 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2611 if (subregion
->priority
>= other
->priority
) {
2612 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2616 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2618 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2619 memory_region_transaction_commit();
2622 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2624 MemoryRegion
*subregion
)
2626 MemoryRegion
*alias
;
2628 assert(!subregion
->container
);
2629 subregion
->container
= mr
;
2630 for (alias
= subregion
->alias
; alias
; alias
= alias
->alias
) {
2631 alias
->mapped_via_alias
++;
2633 subregion
->addr
= offset
;
2634 memory_region_update_container_subregions(subregion
);
2637 void memory_region_add_subregion(MemoryRegion
*mr
,
2639 MemoryRegion
*subregion
)
2641 subregion
->priority
= 0;
2642 memory_region_add_subregion_common(mr
, offset
, subregion
);
2645 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2647 MemoryRegion
*subregion
,
2650 subregion
->priority
= priority
;
2651 memory_region_add_subregion_common(mr
, offset
, subregion
);
2654 void memory_region_del_subregion(MemoryRegion
*mr
,
2655 MemoryRegion
*subregion
)
2657 MemoryRegion
*alias
;
2659 memory_region_transaction_begin();
2660 assert(subregion
->container
== mr
);
2661 subregion
->container
= NULL
;
2662 for (alias
= subregion
->alias
; alias
; alias
= alias
->alias
) {
2663 alias
->mapped_via_alias
--;
2664 assert(alias
->mapped_via_alias
>= 0);
2666 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2667 memory_region_unref(subregion
);
2668 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2669 memory_region_transaction_commit();
2672 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2674 if (enabled
== mr
->enabled
) {
2677 memory_region_transaction_begin();
2678 mr
->enabled
= enabled
;
2679 memory_region_update_pending
= true;
2680 memory_region_transaction_commit();
2683 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2685 Int128 s
= int128_make64(size
);
2687 if (size
== UINT64_MAX
) {
2690 if (int128_eq(s
, mr
->size
)) {
2693 memory_region_transaction_begin();
2695 memory_region_update_pending
= true;
2696 memory_region_transaction_commit();
2699 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2701 MemoryRegion
*container
= mr
->container
;
2704 memory_region_transaction_begin();
2705 memory_region_ref(mr
);
2706 memory_region_del_subregion(container
, mr
);
2707 memory_region_add_subregion_common(container
, mr
->addr
, mr
);
2708 memory_region_unref(mr
);
2709 memory_region_transaction_commit();
2713 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2715 if (addr
!= mr
->addr
) {
2717 memory_region_readd_subregion(mr
);
2721 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2725 if (offset
== mr
->alias_offset
) {
2729 memory_region_transaction_begin();
2730 mr
->alias_offset
= offset
;
2731 memory_region_update_pending
|= mr
->enabled
;
2732 memory_region_transaction_commit();
2735 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2740 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2742 const AddrRange
*addr
= addr_
;
2743 const FlatRange
*fr
= fr_
;
2745 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2747 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2753 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2755 return bsearch(&addr
, view
->ranges
, view
->nr
,
2756 sizeof(FlatRange
), cmp_flatrange_addr
);
2759 bool memory_region_is_mapped(MemoryRegion
*mr
)
2761 return !!mr
->container
|| mr
->mapped_via_alias
;
2764 /* Same as memory_region_find, but it does not add a reference to the
2765 * returned region. It must be called from an RCU critical section.
2767 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2768 hwaddr addr
, uint64_t size
)
2770 MemoryRegionSection ret
= { .mr
= NULL
};
2778 for (root
= mr
; root
->container
; ) {
2779 root
= root
->container
;
2783 as
= memory_region_to_address_space(root
);
2787 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2789 view
= address_space_to_flatview(as
);
2790 fr
= flatview_lookup(view
, range
);
2795 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2801 range
= addrrange_intersection(range
, fr
->addr
);
2802 ret
.offset_within_region
= fr
->offset_in_region
;
2803 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2805 ret
.size
= range
.size
;
2806 ret
.offset_within_address_space
= int128_get64(range
.start
);
2807 ret
.readonly
= fr
->readonly
;
2808 ret
.nonvolatile
= fr
->nonvolatile
;
2812 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2813 hwaddr addr
, uint64_t size
)
2815 MemoryRegionSection ret
;
2816 RCU_READ_LOCK_GUARD();
2817 ret
= memory_region_find_rcu(mr
, addr
, size
);
2819 memory_region_ref(ret
.mr
);
2824 MemoryRegionSection
*memory_region_section_new_copy(MemoryRegionSection
*s
)
2826 MemoryRegionSection
*tmp
= g_new(MemoryRegionSection
, 1);
2830 memory_region_ref(tmp
->mr
);
2833 bool ret
= flatview_ref(tmp
->fv
);
2840 void memory_region_section_free_copy(MemoryRegionSection
*s
)
2843 flatview_unref(s
->fv
);
2846 memory_region_unref(s
->mr
);
2851 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2855 RCU_READ_LOCK_GUARD();
2856 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2857 return mr
&& mr
!= container
;
2860 void memory_global_dirty_log_sync(void)
2862 memory_region_sync_dirty_bitmap(NULL
);
2865 void memory_global_after_dirty_log_sync(void)
2867 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2871 * Dirty track stop flags that are postponed due to VM being stopped. Should
2872 * only be used within vmstate_change hook.
2874 static unsigned int postponed_stop_flags
;
2875 static VMChangeStateEntry
*vmstate_change
;
2876 static void memory_global_dirty_log_stop_postponed_run(void);
2878 void memory_global_dirty_log_start(unsigned int flags
)
2880 unsigned int old_flags
;
2882 assert(flags
&& !(flags
& (~GLOBAL_DIRTY_MASK
)));
2884 if (vmstate_change
) {
2885 /* If there is postponed stop(), operate on it first */
2886 postponed_stop_flags
&= ~flags
;
2887 memory_global_dirty_log_stop_postponed_run();
2890 flags
&= ~global_dirty_tracking
;
2895 old_flags
= global_dirty_tracking
;
2896 global_dirty_tracking
|= flags
;
2897 trace_global_dirty_changed(global_dirty_tracking
);
2900 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2901 memory_region_transaction_begin();
2902 memory_region_update_pending
= true;
2903 memory_region_transaction_commit();
2907 static void memory_global_dirty_log_do_stop(unsigned int flags
)
2909 assert(flags
&& !(flags
& (~GLOBAL_DIRTY_MASK
)));
2910 assert((global_dirty_tracking
& flags
) == flags
);
2911 global_dirty_tracking
&= ~flags
;
2913 trace_global_dirty_changed(global_dirty_tracking
);
2915 if (!global_dirty_tracking
) {
2916 memory_region_transaction_begin();
2917 memory_region_update_pending
= true;
2918 memory_region_transaction_commit();
2919 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2924 * Execute the postponed dirty log stop operations if there is, then reset
2925 * everything (including the flags and the vmstate change hook).
2927 static void memory_global_dirty_log_stop_postponed_run(void)
2929 /* This must be called with the vmstate handler registered */
2930 assert(vmstate_change
);
2932 /* Note: postponed_stop_flags can be cleared in log start routine */
2933 if (postponed_stop_flags
) {
2934 memory_global_dirty_log_do_stop(postponed_stop_flags
);
2935 postponed_stop_flags
= 0;
2938 qemu_del_vm_change_state_handler(vmstate_change
);
2939 vmstate_change
= NULL
;
2942 static void memory_vm_change_state_handler(void *opaque
, bool running
,
2946 memory_global_dirty_log_stop_postponed_run();
2950 void memory_global_dirty_log_stop(unsigned int flags
)
2952 if (!runstate_is_running()) {
2953 /* Postpone the dirty log stop, e.g., to when VM starts again */
2954 if (vmstate_change
) {
2955 /* Batch with previous postponed flags */
2956 postponed_stop_flags
|= flags
;
2958 postponed_stop_flags
= flags
;
2959 vmstate_change
= qemu_add_vm_change_state_handler(
2960 memory_vm_change_state_handler
, NULL
);
2965 memory_global_dirty_log_do_stop(flags
);
2968 static void listener_add_address_space(MemoryListener
*listener
,
2974 if (listener
->begin
) {
2975 listener
->begin(listener
);
2977 if (global_dirty_tracking
) {
2978 if (listener
->log_global_start
) {
2979 listener
->log_global_start(listener
);
2983 view
= address_space_get_flatview(as
);
2984 FOR_EACH_FLAT_RANGE(fr
, view
) {
2985 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2987 if (listener
->region_add
) {
2988 listener
->region_add(listener
, §ion
);
2990 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2991 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2994 if (listener
->commit
) {
2995 listener
->commit(listener
);
2997 flatview_unref(view
);
3000 static void listener_del_address_space(MemoryListener
*listener
,
3006 if (listener
->begin
) {
3007 listener
->begin(listener
);
3009 view
= address_space_get_flatview(as
);
3010 FOR_EACH_FLAT_RANGE(fr
, view
) {
3011 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
3013 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
3014 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
3016 if (listener
->region_del
) {
3017 listener
->region_del(listener
, §ion
);
3020 if (listener
->commit
) {
3021 listener
->commit(listener
);
3023 flatview_unref(view
);
3026 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
3028 MemoryListener
*other
= NULL
;
3030 /* Only one of them can be defined for a listener */
3031 assert(!(listener
->log_sync
&& listener
->log_sync_global
));
3033 listener
->address_space
= as
;
3034 if (QTAILQ_EMPTY(&memory_listeners
)
3035 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
3036 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
3038 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
3039 if (listener
->priority
< other
->priority
) {
3043 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
3046 if (QTAILQ_EMPTY(&as
->listeners
)
3047 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
3048 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
3050 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
3051 if (listener
->priority
< other
->priority
) {
3055 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
3058 listener_add_address_space(listener
, as
);
3061 void memory_listener_unregister(MemoryListener
*listener
)
3063 if (!listener
->address_space
) {
3067 listener_del_address_space(listener
, listener
->address_space
);
3068 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
3069 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
3070 listener
->address_space
= NULL
;
3073 void address_space_remove_listeners(AddressSpace
*as
)
3075 while (!QTAILQ_EMPTY(&as
->listeners
)) {
3076 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
3080 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
3082 memory_region_ref(root
);
3084 as
->current_map
= NULL
;
3085 as
->ioeventfd_nb
= 0;
3086 as
->ioeventfds
= NULL
;
3087 QTAILQ_INIT(&as
->listeners
);
3088 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
3089 as
->name
= g_strdup(name
? name
: "anonymous");
3090 address_space_update_topology(as
);
3091 address_space_update_ioeventfds(as
);
3094 static void do_address_space_destroy(AddressSpace
*as
)
3096 assert(QTAILQ_EMPTY(&as
->listeners
));
3098 flatview_unref(as
->current_map
);
3100 g_free(as
->ioeventfds
);
3101 memory_region_unref(as
->root
);
3104 void address_space_destroy(AddressSpace
*as
)
3106 MemoryRegion
*root
= as
->root
;
3108 /* Flush out anything from MemoryListeners listening in on this */
3109 memory_region_transaction_begin();
3111 memory_region_transaction_commit();
3112 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
3114 /* At this point, as->dispatch and as->current_map are dummy
3115 * entries that the guest should never use. Wait for the old
3116 * values to expire before freeing the data.
3119 call_rcu(as
, do_address_space_destroy
, rcu
);
3122 static const char *memory_region_type(MemoryRegion
*mr
)
3125 return memory_region_type(mr
->alias
);
3127 if (memory_region_is_ram_device(mr
)) {
3129 } else if (memory_region_is_romd(mr
)) {
3131 } else if (memory_region_is_rom(mr
)) {
3133 } else if (memory_region_is_ram(mr
)) {
3140 typedef struct MemoryRegionList MemoryRegionList
;
3142 struct MemoryRegionList
{
3143 const MemoryRegion
*mr
;
3144 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
3147 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
3149 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
3150 int128_sub((size), int128_one())) : 0)
3151 #define MTREE_INDENT " "
3153 static void mtree_expand_owner(const char *label
, Object
*obj
)
3155 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
3157 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
3158 if (dev
&& dev
->id
) {
3159 qemu_printf(" id=%s", dev
->id
);
3161 char *canonical_path
= object_get_canonical_path(obj
);
3162 if (canonical_path
) {
3163 qemu_printf(" path=%s", canonical_path
);
3164 g_free(canonical_path
);
3166 qemu_printf(" type=%s", object_get_typename(obj
));
3172 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
3174 Object
*owner
= mr
->owner
;
3175 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
3177 if (!owner
&& !parent
) {
3178 qemu_printf(" orphan");
3182 mtree_expand_owner("owner", owner
);
3184 if (parent
&& parent
!= owner
) {
3185 mtree_expand_owner("parent", parent
);
3189 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
3191 MemoryRegionListHead
*alias_print_queue
,
3192 bool owner
, bool display_disabled
)
3194 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
3195 MemoryRegionListHead submr_print_queue
;
3196 const MemoryRegion
*submr
;
3198 hwaddr cur_start
, cur_end
;
3204 cur_start
= base
+ mr
->addr
;
3205 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
3208 * Try to detect overflow of memory region. This should never
3209 * happen normally. When it happens, we dump something to warn the
3210 * user who is observing this.
3212 if (cur_start
< base
|| cur_end
< cur_start
) {
3213 qemu_printf("[DETECTED OVERFLOW!] ");
3217 MemoryRegionList
*ml
;
3220 /* check if the alias is already in the queue */
3221 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
3222 if (ml
->mr
== mr
->alias
) {
3228 ml
= g_new(MemoryRegionList
, 1);
3230 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
3232 if (mr
->enabled
|| display_disabled
) {
3233 for (i
= 0; i
< level
; i
++) {
3234 qemu_printf(MTREE_INDENT
);
3236 qemu_printf(HWADDR_FMT_plx
"-" HWADDR_FMT_plx
3237 " (prio %d, %s%s): alias %s @%s " HWADDR_FMT_plx
3238 "-" HWADDR_FMT_plx
"%s",
3241 mr
->nonvolatile
? "nv-" : "",
3242 memory_region_type((MemoryRegion
*)mr
),
3243 memory_region_name(mr
),
3244 memory_region_name(mr
->alias
),
3246 mr
->alias_offset
+ MR_SIZE(mr
->size
),
3247 mr
->enabled
? "" : " [disabled]");
3249 mtree_print_mr_owner(mr
);
3254 if (mr
->enabled
|| display_disabled
) {
3255 for (i
= 0; i
< level
; i
++) {
3256 qemu_printf(MTREE_INDENT
);
3258 qemu_printf(HWADDR_FMT_plx
"-" HWADDR_FMT_plx
3259 " (prio %d, %s%s): %s%s",
3262 mr
->nonvolatile
? "nv-" : "",
3263 memory_region_type((MemoryRegion
*)mr
),
3264 memory_region_name(mr
),
3265 mr
->enabled
? "" : " [disabled]");
3267 mtree_print_mr_owner(mr
);
3273 QTAILQ_INIT(&submr_print_queue
);
3275 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
3276 new_ml
= g_new(MemoryRegionList
, 1);
3278 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3279 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
3280 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
3281 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
3282 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
3288 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
3292 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3293 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
3294 alias_print_queue
, owner
, display_disabled
);
3297 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3302 struct FlatViewInfo
{
3309 static void mtree_print_flatview(gpointer key
, gpointer value
,
3312 FlatView
*view
= key
;
3313 GArray
*fv_address_spaces
= value
;
3314 struct FlatViewInfo
*fvi
= user_data
;
3315 FlatRange
*range
= &view
->ranges
[0];
3321 qemu_printf("FlatView #%d\n", fvi
->counter
);
3324 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3325 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3326 qemu_printf(" AS \"%s\", root: %s",
3327 as
->name
, memory_region_name(as
->root
));
3328 if (as
->root
->alias
) {
3329 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3334 qemu_printf(" Root memory region: %s\n",
3335 view
->root
? memory_region_name(view
->root
) : "(none)");
3338 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3344 if (range
->offset_in_region
) {
3345 qemu_printf(MTREE_INDENT HWADDR_FMT_plx
"-" HWADDR_FMT_plx
3346 " (prio %d, %s%s): %s @" HWADDR_FMT_plx
,
3347 int128_get64(range
->addr
.start
),
3348 int128_get64(range
->addr
.start
)
3349 + MR_SIZE(range
->addr
.size
),
3351 range
->nonvolatile
? "nv-" : "",
3352 range
->readonly
? "rom" : memory_region_type(mr
),
3353 memory_region_name(mr
),
3354 range
->offset_in_region
);
3356 qemu_printf(MTREE_INDENT HWADDR_FMT_plx
"-" HWADDR_FMT_plx
3357 " (prio %d, %s%s): %s",
3358 int128_get64(range
->addr
.start
),
3359 int128_get64(range
->addr
.start
)
3360 + MR_SIZE(range
->addr
.size
),
3362 range
->nonvolatile
? "nv-" : "",
3363 range
->readonly
? "rom" : memory_region_type(mr
),
3364 memory_region_name(mr
));
3367 mtree_print_mr_owner(mr
);
3371 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3372 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3373 if (fvi
->ac
->has_memory(current_machine
, as
,
3374 int128_get64(range
->addr
.start
),
3375 MR_SIZE(range
->addr
.size
) + 1)) {
3376 qemu_printf(" %s", fvi
->ac
->name
);
3384 #if !defined(CONFIG_USER_ONLY)
3385 if (fvi
->dispatch_tree
&& view
->root
) {
3386 mtree_print_dispatch(view
->dispatch
, view
->root
);
3393 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3396 FlatView
*view
= key
;
3397 GArray
*fv_address_spaces
= value
;
3399 g_array_unref(fv_address_spaces
);
3400 flatview_unref(view
);
3405 static void mtree_info_flatview(bool dispatch_tree
, bool owner
)
3407 struct FlatViewInfo fvi
= {
3409 .dispatch_tree
= dispatch_tree
,
3414 GArray
*fv_address_spaces
;
3415 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3416 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
3418 if (ac
->has_memory
) {
3422 /* Gather all FVs in one table */
3423 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3424 view
= address_space_get_flatview(as
);
3426 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3427 if (!fv_address_spaces
) {
3428 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3429 g_hash_table_insert(views
, view
, fv_address_spaces
);
3432 g_array_append_val(fv_address_spaces
, as
);
3436 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3439 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3440 g_hash_table_unref(views
);
3443 struct AddressSpaceInfo
{
3444 MemoryRegionListHead
*ml_head
;
3449 /* Returns negative value if a < b; zero if a = b; positive value if a > b. */
3450 static gint
address_space_compare_name(gconstpointer a
, gconstpointer b
)
3452 const AddressSpace
*as_a
= a
;
3453 const AddressSpace
*as_b
= b
;
3455 return g_strcmp0(as_a
->name
, as_b
->name
);
3458 static void mtree_print_as_name(gpointer data
, gpointer user_data
)
3460 AddressSpace
*as
= data
;
3462 qemu_printf("address-space: %s\n", as
->name
);
3465 static void mtree_print_as(gpointer key
, gpointer value
, gpointer user_data
)
3467 MemoryRegion
*mr
= key
;
3468 GSList
*as_same_root_mr_list
= value
;
3469 struct AddressSpaceInfo
*asi
= user_data
;
3471 g_slist_foreach(as_same_root_mr_list
, mtree_print_as_name
, NULL
);
3472 mtree_print_mr(mr
, 1, 0, asi
->ml_head
, asi
->owner
, asi
->disabled
);
3476 static gboolean
mtree_info_as_free(gpointer key
, gpointer value
,
3479 GSList
*as_same_root_mr_list
= value
;
3481 g_slist_free(as_same_root_mr_list
);
3486 static void mtree_info_as(bool dispatch_tree
, bool owner
, bool disabled
)
3488 MemoryRegionListHead ml_head
;
3489 MemoryRegionList
*ml
, *ml2
;
3491 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3492 GSList
*as_same_root_mr_list
;
3493 struct AddressSpaceInfo asi
= {
3494 .ml_head
= &ml_head
,
3496 .disabled
= disabled
,
3499 QTAILQ_INIT(&ml_head
);
3501 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3502 /* Create hashtable, key=AS root MR, value = list of AS */
3503 as_same_root_mr_list
= g_hash_table_lookup(views
, as
->root
);
3504 as_same_root_mr_list
= g_slist_insert_sorted(as_same_root_mr_list
, as
,
3505 address_space_compare_name
);
3506 g_hash_table_insert(views
, as
->root
, as_same_root_mr_list
);
3509 /* print address spaces */
3510 g_hash_table_foreach(views
, mtree_print_as
, &asi
);
3511 g_hash_table_foreach_remove(views
, mtree_info_as_free
, 0);
3512 g_hash_table_unref(views
);
3514 /* print aliased regions */
3515 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3516 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3517 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
, disabled
);
3521 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3526 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
)
3529 mtree_info_flatview(dispatch_tree
, owner
);
3531 mtree_info_as(dispatch_tree
, owner
, disabled
);
3535 void memory_region_init_ram(MemoryRegion
*mr
,
3541 DeviceState
*owner_dev
;
3544 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3546 error_propagate(errp
, err
);
3549 /* This will assert if owner is neither NULL nor a DeviceState.
3550 * We only want the owner here for the purposes of defining a
3551 * unique name for migration. TODO: Ideally we should implement
3552 * a naming scheme for Objects which are not DeviceStates, in
3553 * which case we can relax this restriction.
3555 owner_dev
= DEVICE(owner
);
3556 vmstate_register_ram(mr
, owner_dev
);
3559 void memory_region_init_rom(MemoryRegion
*mr
,
3565 DeviceState
*owner_dev
;
3568 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3570 error_propagate(errp
, err
);
3573 /* This will assert if owner is neither NULL nor a DeviceState.
3574 * We only want the owner here for the purposes of defining a
3575 * unique name for migration. TODO: Ideally we should implement
3576 * a naming scheme for Objects which are not DeviceStates, in
3577 * which case we can relax this restriction.
3579 owner_dev
= DEVICE(owner
);
3580 vmstate_register_ram(mr
, owner_dev
);
3583 void memory_region_init_rom_device(MemoryRegion
*mr
,
3585 const MemoryRegionOps
*ops
,
3591 DeviceState
*owner_dev
;
3594 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3597 error_propagate(errp
, err
);
3600 /* This will assert if owner is neither NULL nor a DeviceState.
3601 * We only want the owner here for the purposes of defining a
3602 * unique name for migration. TODO: Ideally we should implement
3603 * a naming scheme for Objects which are not DeviceStates, in
3604 * which case we can relax this restriction.
3606 owner_dev
= DEVICE(owner
);
3607 vmstate_register_ram(mr
, owner_dev
);
3611 * Support softmmu builds with CONFIG_FUZZ using a weak symbol and a stub for
3612 * the fuzz_dma_read_cb callback
3615 void __attribute__((weak
)) fuzz_dma_read_cb(size_t addr
,
3622 static const TypeInfo memory_region_info
= {
3623 .parent
= TYPE_OBJECT
,
3624 .name
= TYPE_MEMORY_REGION
,
3625 .class_size
= sizeof(MemoryRegionClass
),
3626 .instance_size
= sizeof(MemoryRegion
),
3627 .instance_init
= memory_region_initfn
,
3628 .instance_finalize
= memory_region_finalize
,
3631 static const TypeInfo iommu_memory_region_info
= {
3632 .parent
= TYPE_MEMORY_REGION
,
3633 .name
= TYPE_IOMMU_MEMORY_REGION
,
3634 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3635 .instance_size
= sizeof(IOMMUMemoryRegion
),
3636 .instance_init
= iommu_memory_region_initfn
,
3640 static const TypeInfo ram_discard_manager_info
= {
3641 .parent
= TYPE_INTERFACE
,
3642 .name
= TYPE_RAM_DISCARD_MANAGER
,
3643 .class_size
= sizeof(RamDiscardManagerClass
),
3646 static void memory_register_types(void)
3648 type_register_static(&memory_region_info
);
3649 type_register_static(&iommu_memory_region_info
);
3650 type_register_static(&ram_discard_manager_info
);
3653 type_init(memory_register_types
)