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"
20 #include "exec/memory.h"
21 #include "exec/address-spaces.h"
22 #include "qapi/visitor.h"
23 #include "qemu/bitops.h"
24 #include "qemu/error-report.h"
25 #include "qemu/main-loop.h"
26 #include "qemu/qemu-print.h"
27 #include "qom/object.h"
30 #include "exec/memory-internal.h"
31 #include "exec/ram_addr.h"
32 #include "sysemu/kvm.h"
33 #include "sysemu/runstate.h"
34 #include "sysemu/tcg.h"
35 #include "qemu/accel.h"
36 #include "hw/boards.h"
37 #include "migration/vmstate.h"
39 //#define DEBUG_UNASSIGNED
41 static unsigned memory_region_transaction_depth
;
42 static bool memory_region_update_pending
;
43 static bool ioeventfd_update_pending
;
44 bool global_dirty_log
;
46 static QTAILQ_HEAD(, MemoryListener
) memory_listeners
47 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
49 static QTAILQ_HEAD(, AddressSpace
) address_spaces
50 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
52 static GHashTable
*flat_views
;
54 typedef struct AddrRange AddrRange
;
57 * Note that signed integers are needed for negative offsetting in aliases
58 * (large MemoryRegion::alias_offset).
65 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
67 return (AddrRange
) { start
, size
};
70 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
72 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
75 static Int128
addrrange_end(AddrRange r
)
77 return int128_add(r
.start
, r
.size
);
80 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
82 int128_addto(&range
.start
, delta
);
86 static bool addrrange_contains(AddrRange range
, Int128 addr
)
88 return int128_ge(addr
, range
.start
)
89 && int128_lt(addr
, addrrange_end(range
));
92 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
94 return addrrange_contains(r1
, r2
.start
)
95 || addrrange_contains(r2
, r1
.start
);
98 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
100 Int128 start
= int128_max(r1
.start
, r2
.start
);
101 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
102 return addrrange_make(start
, int128_sub(end
, start
));
105 enum ListenerDirection
{ Forward
, Reverse
};
107 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
109 MemoryListener *_listener; \
111 switch (_direction) { \
113 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
114 if (_listener->_callback) { \
115 _listener->_callback(_listener, ##_args); \
120 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, link) { \
121 if (_listener->_callback) { \
122 _listener->_callback(_listener, ##_args); \
131 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
133 MemoryListener *_listener; \
135 switch (_direction) { \
137 QTAILQ_FOREACH(_listener, &(_as)->listeners, link_as) { \
138 if (_listener->_callback) { \
139 _listener->_callback(_listener, _section, ##_args); \
144 QTAILQ_FOREACH_REVERSE(_listener, &(_as)->listeners, link_as) { \
145 if (_listener->_callback) { \
146 _listener->_callback(_listener, _section, ##_args); \
155 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
156 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
158 MemoryRegionSection mrs = section_from_flat_range(fr, \
159 address_space_to_flatview(as)); \
160 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
163 struct CoalescedMemoryRange
{
165 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
168 struct MemoryRegionIoeventfd
{
175 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd
*a
,
176 MemoryRegionIoeventfd
*b
)
178 if (int128_lt(a
->addr
.start
, b
->addr
.start
)) {
180 } else if (int128_gt(a
->addr
.start
, b
->addr
.start
)) {
182 } else if (int128_lt(a
->addr
.size
, b
->addr
.size
)) {
184 } else if (int128_gt(a
->addr
.size
, b
->addr
.size
)) {
186 } else if (a
->match_data
< b
->match_data
) {
188 } else if (a
->match_data
> b
->match_data
) {
190 } else if (a
->match_data
) {
191 if (a
->data
< b
->data
) {
193 } else if (a
->data
> b
->data
) {
199 } else if (a
->e
> b
->e
) {
205 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd
*a
,
206 MemoryRegionIoeventfd
*b
)
208 if (int128_eq(a
->addr
.start
, b
->addr
.start
) &&
209 (!int128_nz(a
->addr
.size
) || !int128_nz(b
->addr
.size
) ||
210 (int128_eq(a
->addr
.size
, b
->addr
.size
) &&
211 (a
->match_data
== b
->match_data
) &&
212 ((a
->match_data
&& (a
->data
== b
->data
)) || !a
->match_data
) &&
219 /* Range of memory in the global map. Addresses are absolute. */
222 hwaddr offset_in_region
;
224 uint8_t dirty_log_mask
;
230 #define FOR_EACH_FLAT_RANGE(var, view) \
231 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
233 static inline MemoryRegionSection
234 section_from_flat_range(FlatRange
*fr
, FlatView
*fv
)
236 return (MemoryRegionSection
) {
239 .offset_within_region
= fr
->offset_in_region
,
240 .size
= fr
->addr
.size
,
241 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
242 .readonly
= fr
->readonly
,
243 .nonvolatile
= fr
->nonvolatile
,
247 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
249 return a
->mr
== b
->mr
250 && addrrange_equal(a
->addr
, b
->addr
)
251 && a
->offset_in_region
== b
->offset_in_region
252 && a
->romd_mode
== b
->romd_mode
253 && a
->readonly
== b
->readonly
254 && a
->nonvolatile
== b
->nonvolatile
;
257 static FlatView
*flatview_new(MemoryRegion
*mr_root
)
261 view
= g_new0(FlatView
, 1);
263 view
->root
= mr_root
;
264 memory_region_ref(mr_root
);
265 trace_flatview_new(view
, mr_root
);
270 /* Insert a range into a given position. Caller is responsible for maintaining
273 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
275 if (view
->nr
== view
->nr_allocated
) {
276 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
277 view
->ranges
= g_realloc(view
->ranges
,
278 view
->nr_allocated
* sizeof(*view
->ranges
));
280 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
281 (view
->nr
- pos
) * sizeof(FlatRange
));
282 view
->ranges
[pos
] = *range
;
283 memory_region_ref(range
->mr
);
287 static void flatview_destroy(FlatView
*view
)
291 trace_flatview_destroy(view
, view
->root
);
292 if (view
->dispatch
) {
293 address_space_dispatch_free(view
->dispatch
);
295 for (i
= 0; i
< view
->nr
; i
++) {
296 memory_region_unref(view
->ranges
[i
].mr
);
298 g_free(view
->ranges
);
299 memory_region_unref(view
->root
);
303 static bool flatview_ref(FlatView
*view
)
305 return qatomic_fetch_inc_nonzero(&view
->ref
) > 0;
308 void flatview_unref(FlatView
*view
)
310 if (qatomic_fetch_dec(&view
->ref
) == 1) {
311 trace_flatview_destroy_rcu(view
, view
->root
);
313 call_rcu(view
, flatview_destroy
, rcu
);
317 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
319 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
321 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
323 int128_make64(r2
->offset_in_region
))
324 && r1
->dirty_log_mask
== r2
->dirty_log_mask
325 && r1
->romd_mode
== r2
->romd_mode
326 && r1
->readonly
== r2
->readonly
327 && r1
->nonvolatile
== r2
->nonvolatile
;
330 /* Attempt to simplify a view by merging adjacent ranges */
331 static void flatview_simplify(FlatView
*view
)
336 while (i
< view
->nr
) {
339 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
340 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
344 for (k
= i
; k
< j
; k
++) {
345 memory_region_unref(view
->ranges
[k
].mr
);
347 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
348 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
353 static bool memory_region_big_endian(MemoryRegion
*mr
)
355 #ifdef TARGET_WORDS_BIGENDIAN
356 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
358 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
362 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, MemOp op
)
364 if ((op
& MO_BSWAP
) != devend_memop(mr
->ops
->endianness
)) {
365 switch (op
& MO_SIZE
) {
369 *data
= bswap16(*data
);
372 *data
= bswap32(*data
);
375 *data
= bswap64(*data
);
378 g_assert_not_reached();
383 static inline void memory_region_shift_read_access(uint64_t *value
,
389 *value
|= (tmp
& mask
) << shift
;
391 *value
|= (tmp
& mask
) >> -shift
;
395 static inline uint64_t memory_region_shift_write_access(uint64_t *value
,
402 tmp
= (*value
>> shift
) & mask
;
404 tmp
= (*value
<< -shift
) & mask
;
410 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
413 hwaddr abs_addr
= offset
;
415 abs_addr
+= mr
->addr
;
416 for (root
= mr
; root
->container
; ) {
417 root
= root
->container
;
418 abs_addr
+= root
->addr
;
424 static int get_cpu_index(void)
427 return current_cpu
->cpu_index
;
432 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
442 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
444 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
445 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_READ
)) {
446 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
447 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
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
);
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
);
491 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
495 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
503 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
506 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
507 } else if (trace_event_get_state_backends(TRACE_MEMORY_REGION_OPS_WRITE
)) {
508 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
509 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
511 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
514 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
517 unsigned access_size_min
,
518 unsigned access_size_max
,
519 MemTxResult (*access_fn
)
530 uint64_t access_mask
;
531 unsigned access_size
;
533 MemTxResult r
= MEMTX_OK
;
535 if (!access_size_min
) {
538 if (!access_size_max
) {
542 /* FIXME: support unaligned access? */
543 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
544 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
545 if (memory_region_big_endian(mr
)) {
546 for (i
= 0; i
< size
; i
+= access_size
) {
547 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
548 (size
- access_size
- i
) * 8, access_mask
, attrs
);
551 for (i
= 0; i
< size
; i
+= access_size
) {
552 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
559 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
563 while (mr
->container
) {
566 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
567 if (mr
== as
->root
) {
574 /* Render a memory region into the global view. Ranges in @view obscure
577 static void render_memory_region(FlatView
*view
,
584 MemoryRegion
*subregion
;
586 hwaddr offset_in_region
;
596 int128_addto(&base
, int128_make64(mr
->addr
));
597 readonly
|= mr
->readonly
;
598 nonvolatile
|= mr
->nonvolatile
;
600 tmp
= addrrange_make(base
, mr
->size
);
602 if (!addrrange_intersects(tmp
, clip
)) {
606 clip
= addrrange_intersection(tmp
, clip
);
609 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
610 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
611 render_memory_region(view
, mr
->alias
, base
, clip
,
612 readonly
, nonvolatile
);
616 /* Render subregions in priority order. */
617 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
618 render_memory_region(view
, subregion
, base
, clip
,
619 readonly
, nonvolatile
);
622 if (!mr
->terminates
) {
626 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
631 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
632 fr
.romd_mode
= mr
->romd_mode
;
633 fr
.readonly
= readonly
;
634 fr
.nonvolatile
= nonvolatile
;
636 /* Render the region itself into any gaps left by the current view. */
637 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
638 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
641 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
642 now
= int128_min(remain
,
643 int128_sub(view
->ranges
[i
].addr
.start
, base
));
644 fr
.offset_in_region
= offset_in_region
;
645 fr
.addr
= addrrange_make(base
, now
);
646 flatview_insert(view
, i
, &fr
);
648 int128_addto(&base
, now
);
649 offset_in_region
+= int128_get64(now
);
650 int128_subfrom(&remain
, now
);
652 now
= int128_sub(int128_min(int128_add(base
, remain
),
653 addrrange_end(view
->ranges
[i
].addr
)),
655 int128_addto(&base
, now
);
656 offset_in_region
+= int128_get64(now
);
657 int128_subfrom(&remain
, now
);
659 if (int128_nz(remain
)) {
660 fr
.offset_in_region
= offset_in_region
;
661 fr
.addr
= addrrange_make(base
, remain
);
662 flatview_insert(view
, i
, &fr
);
666 void flatview_for_each_range(FlatView
*fv
, flatview_cb cb
, void *opaque
)
673 FOR_EACH_FLAT_RANGE(fr
, fv
) {
674 if (cb(fr
->addr
.start
, fr
->addr
.size
, fr
->mr
, opaque
))
679 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
681 while (mr
->enabled
) {
683 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
684 /* The alias is included in its entirety. Use it as
685 * the "real" root, so that we can share more FlatViews.
690 } else if (!mr
->terminates
) {
691 unsigned int found
= 0;
692 MemoryRegion
*child
, *next
= NULL
;
693 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
694 if (child
->enabled
) {
699 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
700 /* A child is included in its entirety. If it's the only
701 * enabled one, use it in the hope of finding an alias down the
702 * way. This will also let us share FlatViews.
723 /* Render a memory topology into a list of disjoint absolute ranges. */
724 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
729 view
= flatview_new(mr
);
732 render_memory_region(view
, mr
, int128_zero(),
733 addrrange_make(int128_zero(), int128_2_64()),
736 flatview_simplify(view
);
738 view
->dispatch
= address_space_dispatch_new(view
);
739 for (i
= 0; i
< view
->nr
; i
++) {
740 MemoryRegionSection mrs
=
741 section_from_flat_range(&view
->ranges
[i
], view
);
742 flatview_add_to_dispatch(view
, &mrs
);
744 address_space_dispatch_compact(view
->dispatch
);
745 g_hash_table_replace(flat_views
, mr
, view
);
750 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
751 MemoryRegionIoeventfd
*fds_new
,
753 MemoryRegionIoeventfd
*fds_old
,
757 MemoryRegionIoeventfd
*fd
;
758 MemoryRegionSection section
;
760 /* Generate a symmetric difference of the old and new fd sets, adding
761 * and deleting as necessary.
765 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
766 if (iold
< fds_old_nb
767 && (inew
== fds_new_nb
768 || memory_region_ioeventfd_before(&fds_old
[iold
],
771 section
= (MemoryRegionSection
) {
772 .fv
= address_space_to_flatview(as
),
773 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
774 .size
= fd
->addr
.size
,
776 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
777 fd
->match_data
, fd
->data
, fd
->e
);
779 } else if (inew
< fds_new_nb
780 && (iold
== fds_old_nb
781 || memory_region_ioeventfd_before(&fds_new
[inew
],
784 section
= (MemoryRegionSection
) {
785 .fv
= address_space_to_flatview(as
),
786 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
787 .size
= fd
->addr
.size
,
789 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
790 fd
->match_data
, fd
->data
, fd
->e
);
799 FlatView
*address_space_get_flatview(AddressSpace
*as
)
803 RCU_READ_LOCK_GUARD();
805 view
= address_space_to_flatview(as
);
806 /* If somebody has replaced as->current_map concurrently,
807 * flatview_ref returns false.
809 } while (!flatview_ref(view
));
813 static void address_space_update_ioeventfds(AddressSpace
*as
)
817 unsigned ioeventfd_nb
= 0;
818 unsigned ioeventfd_max
;
819 MemoryRegionIoeventfd
*ioeventfds
;
824 * It is likely that the number of ioeventfds hasn't changed much, so use
825 * the previous size as the starting value, with some headroom to avoid
826 * gratuitous reallocations.
828 ioeventfd_max
= QEMU_ALIGN_UP(as
->ioeventfd_nb
, 4);
829 ioeventfds
= g_new(MemoryRegionIoeventfd
, ioeventfd_max
);
831 view
= address_space_get_flatview(as
);
832 FOR_EACH_FLAT_RANGE(fr
, view
) {
833 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
834 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
835 int128_sub(fr
->addr
.start
,
836 int128_make64(fr
->offset_in_region
)));
837 if (addrrange_intersects(fr
->addr
, tmp
)) {
839 if (ioeventfd_nb
> ioeventfd_max
) {
840 ioeventfd_max
= MAX(ioeventfd_max
* 2, 4);
841 ioeventfds
= g_realloc(ioeventfds
,
842 ioeventfd_max
* sizeof(*ioeventfds
));
844 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
845 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
850 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
851 as
->ioeventfds
, as
->ioeventfd_nb
);
853 g_free(as
->ioeventfds
);
854 as
->ioeventfds
= ioeventfds
;
855 as
->ioeventfd_nb
= ioeventfd_nb
;
856 flatview_unref(view
);
860 * Notify the memory listeners about the coalesced IO change events of
861 * range `cmr'. Only the part that has intersection of the specified
862 * FlatRange will be sent.
864 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
865 CoalescedMemoryRange
*cmr
, bool add
)
869 tmp
= addrrange_shift(cmr
->addr
,
870 int128_sub(fr
->addr
.start
,
871 int128_make64(fr
->offset_in_region
)));
872 if (!addrrange_intersects(tmp
, fr
->addr
)) {
875 tmp
= addrrange_intersection(tmp
, fr
->addr
);
878 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
879 int128_get64(tmp
.start
),
880 int128_get64(tmp
.size
));
882 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
883 int128_get64(tmp
.start
),
884 int128_get64(tmp
.size
));
888 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
890 CoalescedMemoryRange
*cmr
;
892 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
893 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
897 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
899 MemoryRegion
*mr
= fr
->mr
;
900 CoalescedMemoryRange
*cmr
;
902 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
906 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
907 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
911 static void address_space_update_topology_pass(AddressSpace
*as
,
912 const FlatView
*old_view
,
913 const FlatView
*new_view
,
917 FlatRange
*frold
, *frnew
;
919 /* Generate a symmetric difference of the old and new memory maps.
920 * Kill ranges in the old map, and instantiate ranges in the new map.
923 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
924 if (iold
< old_view
->nr
) {
925 frold
= &old_view
->ranges
[iold
];
929 if (inew
< new_view
->nr
) {
930 frnew
= &new_view
->ranges
[inew
];
937 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
938 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
939 && !flatrange_equal(frold
, frnew
)))) {
940 /* In old but not in new, or in both but attributes changed. */
943 flat_range_coalesced_io_del(frold
, as
);
944 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
948 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
949 /* In both and unchanged (except logging may have changed) */
952 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
953 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
954 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
955 frold
->dirty_log_mask
,
956 frnew
->dirty_log_mask
);
958 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
959 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
960 frold
->dirty_log_mask
,
961 frnew
->dirty_log_mask
);
971 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
972 flat_range_coalesced_io_add(frnew
, as
);
980 static void flatviews_init(void)
982 static FlatView
*empty_view
;
988 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
989 (GDestroyNotify
) flatview_unref
);
991 empty_view
= generate_memory_topology(NULL
);
992 /* We keep it alive forever in the global variable. */
993 flatview_ref(empty_view
);
995 g_hash_table_replace(flat_views
, NULL
, empty_view
);
996 flatview_ref(empty_view
);
1000 static void flatviews_reset(void)
1005 g_hash_table_unref(flat_views
);
1010 /* Render unique FVs */
1011 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1012 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1014 if (g_hash_table_lookup(flat_views
, physmr
)) {
1018 generate_memory_topology(physmr
);
1022 static void address_space_set_flatview(AddressSpace
*as
)
1024 FlatView
*old_view
= address_space_to_flatview(as
);
1025 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1026 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1030 if (old_view
== new_view
) {
1035 flatview_ref(old_view
);
1038 flatview_ref(new_view
);
1040 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1041 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1044 old_view2
= &tmpview
;
1046 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1047 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1050 /* Writes are protected by the BQL. */
1051 qatomic_rcu_set(&as
->current_map
, new_view
);
1053 flatview_unref(old_view
);
1056 /* Note that all the old MemoryRegions are still alive up to this
1057 * point. This relieves most MemoryListeners from the need to
1058 * ref/unref the MemoryRegions they get---unless they use them
1059 * outside the iothread mutex, in which case precise reference
1060 * counting is necessary.
1063 flatview_unref(old_view
);
1067 static void address_space_update_topology(AddressSpace
*as
)
1069 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1072 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1073 generate_memory_topology(physmr
);
1075 address_space_set_flatview(as
);
1078 void memory_region_transaction_begin(void)
1080 qemu_flush_coalesced_mmio_buffer();
1081 ++memory_region_transaction_depth
;
1084 void memory_region_transaction_commit(void)
1088 assert(memory_region_transaction_depth
);
1089 assert(qemu_mutex_iothread_locked());
1091 --memory_region_transaction_depth
;
1092 if (!memory_region_transaction_depth
) {
1093 if (memory_region_update_pending
) {
1096 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1098 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1099 address_space_set_flatview(as
);
1100 address_space_update_ioeventfds(as
);
1102 memory_region_update_pending
= false;
1103 ioeventfd_update_pending
= false;
1104 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1105 } else if (ioeventfd_update_pending
) {
1106 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1107 address_space_update_ioeventfds(as
);
1109 ioeventfd_update_pending
= false;
1114 static void memory_region_destructor_none(MemoryRegion
*mr
)
1118 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1120 qemu_ram_free(mr
->ram_block
);
1123 static bool memory_region_need_escape(char c
)
1125 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1128 static char *memory_region_escape_name(const char *name
)
1135 for (p
= name
; *p
; p
++) {
1136 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1138 if (bytes
== p
- name
) {
1139 return g_memdup(name
, bytes
+ 1);
1142 escaped
= g_malloc(bytes
+ 1);
1143 for (p
= name
, q
= escaped
; *p
; p
++) {
1145 if (unlikely(memory_region_need_escape(c
))) {
1148 *q
++ = "0123456789abcdef"[c
>> 4];
1149 c
= "0123456789abcdef"[c
& 15];
1157 static void memory_region_do_init(MemoryRegion
*mr
,
1162 mr
->size
= int128_make64(size
);
1163 if (size
== UINT64_MAX
) {
1164 mr
->size
= int128_2_64();
1166 mr
->name
= g_strdup(name
);
1168 mr
->ram_block
= NULL
;
1171 char *escaped_name
= memory_region_escape_name(name
);
1172 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1175 owner
= container_get(qdev_get_machine(), "/unattached");
1178 object_property_add_child(owner
, name_array
, OBJECT(mr
));
1179 object_unref(OBJECT(mr
));
1181 g_free(escaped_name
);
1185 void memory_region_init(MemoryRegion
*mr
,
1190 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1191 memory_region_do_init(mr
, owner
, name
, size
);
1194 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1195 const char *name
, void *opaque
,
1198 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1199 char *path
= (char *)"";
1201 if (mr
->container
) {
1202 path
= object_get_canonical_path(OBJECT(mr
->container
));
1204 visit_type_str(v
, name
, &path
, errp
);
1205 if (mr
->container
) {
1210 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1213 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1215 return OBJECT(mr
->container
);
1218 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1219 const char *name
, void *opaque
,
1222 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1223 int32_t value
= mr
->priority
;
1225 visit_type_int32(v
, name
, &value
, errp
);
1228 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1229 void *opaque
, Error
**errp
)
1231 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1232 uint64_t value
= memory_region_size(mr
);
1234 visit_type_uint64(v
, name
, &value
, errp
);
1237 static void memory_region_initfn(Object
*obj
)
1239 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1242 mr
->ops
= &unassigned_mem_ops
;
1244 mr
->romd_mode
= true;
1245 mr
->destructor
= memory_region_destructor_none
;
1246 QTAILQ_INIT(&mr
->subregions
);
1247 QTAILQ_INIT(&mr
->coalesced
);
1249 op
= object_property_add(OBJECT(mr
), "container",
1250 "link<" TYPE_MEMORY_REGION
">",
1251 memory_region_get_container
,
1252 NULL
, /* memory_region_set_container */
1254 op
->resolve
= memory_region_resolve_container
;
1256 object_property_add_uint64_ptr(OBJECT(mr
), "addr",
1257 &mr
->addr
, OBJ_PROP_FLAG_READ
);
1258 object_property_add(OBJECT(mr
), "priority", "uint32",
1259 memory_region_get_priority
,
1260 NULL
, /* memory_region_set_priority */
1262 object_property_add(OBJECT(mr
), "size", "uint64",
1263 memory_region_get_size
,
1264 NULL
, /* memory_region_set_size, */
1268 static void iommu_memory_region_initfn(Object
*obj
)
1270 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1272 mr
->is_iommu
= true;
1275 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1278 #ifdef DEBUG_UNASSIGNED
1279 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1284 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1285 uint64_t val
, unsigned size
)
1287 #ifdef DEBUG_UNASSIGNED
1288 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1292 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1293 unsigned size
, bool is_write
,
1299 const MemoryRegionOps unassigned_mem_ops
= {
1300 .valid
.accepts
= unassigned_mem_accepts
,
1301 .endianness
= DEVICE_NATIVE_ENDIAN
,
1304 static uint64_t memory_region_ram_device_read(void *opaque
,
1305 hwaddr addr
, unsigned size
)
1307 MemoryRegion
*mr
= opaque
;
1308 uint64_t data
= (uint64_t)~0;
1312 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1315 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1318 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1321 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1325 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1330 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1331 uint64_t data
, unsigned size
)
1333 MemoryRegion
*mr
= opaque
;
1335 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1339 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1342 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1345 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1348 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1353 static const MemoryRegionOps ram_device_mem_ops
= {
1354 .read
= memory_region_ram_device_read
,
1355 .write
= memory_region_ram_device_write
,
1356 .endianness
= DEVICE_HOST_ENDIAN
,
1358 .min_access_size
= 1,
1359 .max_access_size
= 8,
1363 .min_access_size
= 1,
1364 .max_access_size
= 8,
1369 bool memory_region_access_valid(MemoryRegion
*mr
,
1375 if (mr
->ops
->valid
.accepts
1376 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
, attrs
)) {
1377 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid access at addr "
1378 "0x%" HWADDR_PRIX
", size %u, "
1379 "region '%s', reason: rejected\n",
1380 addr
, size
, memory_region_name(mr
));
1384 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1385 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid access at addr "
1386 "0x%" HWADDR_PRIX
", size %u, "
1387 "region '%s', reason: unaligned\n",
1388 addr
, size
, memory_region_name(mr
));
1392 /* Treat zero as compatibility all valid */
1393 if (!mr
->ops
->valid
.max_access_size
) {
1397 if (size
> mr
->ops
->valid
.max_access_size
1398 || size
< mr
->ops
->valid
.min_access_size
) {
1399 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid access at addr "
1400 "0x%" HWADDR_PRIX
", size %u, "
1401 "region '%s', reason: invalid size "
1402 "(min:%u max:%u)\n",
1403 addr
, size
, memory_region_name(mr
),
1404 mr
->ops
->valid
.min_access_size
,
1405 mr
->ops
->valid
.max_access_size
);
1411 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1419 if (mr
->ops
->read
) {
1420 return access_with_adjusted_size(addr
, pval
, size
,
1421 mr
->ops
->impl
.min_access_size
,
1422 mr
->ops
->impl
.max_access_size
,
1423 memory_region_read_accessor
,
1426 return access_with_adjusted_size(addr
, pval
, size
,
1427 mr
->ops
->impl
.min_access_size
,
1428 mr
->ops
->impl
.max_access_size
,
1429 memory_region_read_with_attrs_accessor
,
1434 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1440 unsigned size
= memop_size(op
);
1443 fuzz_dma_read_cb(addr
, size
, mr
);
1444 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1445 *pval
= unassigned_mem_read(mr
, addr
, size
);
1446 return MEMTX_DECODE_ERROR
;
1449 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1450 adjust_endianness(mr
, pval
, op
);
1454 /* Return true if an eventfd was signalled */
1455 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1461 MemoryRegionIoeventfd ioeventfd
= {
1462 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1467 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1468 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1469 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1471 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1472 event_notifier_set(ioeventfd
.e
);
1480 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1486 unsigned size
= memop_size(op
);
1488 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1489 unassigned_mem_write(mr
, addr
, data
, size
);
1490 return MEMTX_DECODE_ERROR
;
1493 adjust_endianness(mr
, &data
, op
);
1495 if ((!kvm_eventfds_enabled()) &&
1496 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1500 if (mr
->ops
->write
) {
1501 return access_with_adjusted_size(addr
, &data
, size
,
1502 mr
->ops
->impl
.min_access_size
,
1503 mr
->ops
->impl
.max_access_size
,
1504 memory_region_write_accessor
, mr
,
1508 access_with_adjusted_size(addr
, &data
, size
,
1509 mr
->ops
->impl
.min_access_size
,
1510 mr
->ops
->impl
.max_access_size
,
1511 memory_region_write_with_attrs_accessor
,
1516 void memory_region_init_io(MemoryRegion
*mr
,
1518 const MemoryRegionOps
*ops
,
1523 memory_region_init(mr
, owner
, name
, size
);
1524 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1525 mr
->opaque
= opaque
;
1526 mr
->terminates
= true;
1529 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1535 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1538 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1546 memory_region_init(mr
, owner
, name
, size
);
1548 mr
->terminates
= true;
1549 mr
->destructor
= memory_region_destructor_ram
;
1550 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, &err
);
1552 mr
->size
= int128_zero();
1553 object_unparent(OBJECT(mr
));
1554 error_propagate(errp
, err
);
1558 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1563 void (*resized
)(const char*,
1569 memory_region_init(mr
, owner
, name
, size
);
1571 mr
->terminates
= true;
1572 mr
->destructor
= memory_region_destructor_ram
;
1573 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1576 mr
->size
= int128_zero();
1577 object_unparent(OBJECT(mr
));
1578 error_propagate(errp
, err
);
1583 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1584 struct Object
*owner
,
1594 memory_region_init(mr
, owner
, name
, size
);
1596 mr
->readonly
= readonly
;
1597 mr
->terminates
= true;
1598 mr
->destructor
= memory_region_destructor_ram
;
1600 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
,
1603 mr
->size
= int128_zero();
1604 object_unparent(OBJECT(mr
));
1605 error_propagate(errp
, err
);
1609 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1610 struct Object
*owner
,
1619 memory_region_init(mr
, owner
, name
, size
);
1621 mr
->terminates
= true;
1622 mr
->destructor
= memory_region_destructor_ram
;
1623 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
,
1624 share
? RAM_SHARED
: 0,
1625 fd
, offset
, false, &err
);
1627 mr
->size
= int128_zero();
1628 object_unparent(OBJECT(mr
));
1629 error_propagate(errp
, err
);
1634 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1640 memory_region_init(mr
, owner
, name
, size
);
1642 mr
->terminates
= true;
1643 mr
->destructor
= memory_region_destructor_ram
;
1645 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1646 assert(ptr
!= NULL
);
1647 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1650 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1656 memory_region_init(mr
, owner
, name
, size
);
1658 mr
->terminates
= true;
1659 mr
->ram_device
= true;
1660 mr
->ops
= &ram_device_mem_ops
;
1662 mr
->destructor
= memory_region_destructor_ram
;
1664 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1665 assert(ptr
!= NULL
);
1666 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1669 void memory_region_init_alias(MemoryRegion
*mr
,
1676 memory_region_init(mr
, owner
, name
, size
);
1678 mr
->alias_offset
= offset
;
1681 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1682 struct Object
*owner
,
1687 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1688 mr
->readonly
= true;
1691 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1693 const MemoryRegionOps
*ops
,
1701 memory_region_init(mr
, owner
, name
, size
);
1703 mr
->opaque
= opaque
;
1704 mr
->terminates
= true;
1705 mr
->rom_device
= true;
1706 mr
->destructor
= memory_region_destructor_ram
;
1707 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1709 mr
->size
= int128_zero();
1710 object_unparent(OBJECT(mr
));
1711 error_propagate(errp
, err
);
1715 void memory_region_init_iommu(void *_iommu_mr
,
1716 size_t instance_size
,
1717 const char *mrtypename
,
1722 struct IOMMUMemoryRegion
*iommu_mr
;
1723 struct MemoryRegion
*mr
;
1725 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1726 mr
= MEMORY_REGION(_iommu_mr
);
1727 memory_region_do_init(mr
, owner
, name
, size
);
1728 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1729 mr
->terminates
= true; /* then re-forwards */
1730 QLIST_INIT(&iommu_mr
->iommu_notify
);
1731 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1734 static void memory_region_finalize(Object
*obj
)
1736 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1738 assert(!mr
->container
);
1740 /* We know the region is not visible in any address space (it
1741 * does not have a container and cannot be a root either because
1742 * it has no references, so we can blindly clear mr->enabled.
1743 * memory_region_set_enabled instead could trigger a transaction
1744 * and cause an infinite loop.
1746 mr
->enabled
= false;
1747 memory_region_transaction_begin();
1748 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1749 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1750 memory_region_del_subregion(mr
, subregion
);
1752 memory_region_transaction_commit();
1755 memory_region_clear_coalescing(mr
);
1756 g_free((char *)mr
->name
);
1757 g_free(mr
->ioeventfds
);
1760 Object
*memory_region_owner(MemoryRegion
*mr
)
1762 Object
*obj
= OBJECT(mr
);
1766 void memory_region_ref(MemoryRegion
*mr
)
1768 /* MMIO callbacks most likely will access data that belongs
1769 * to the owner, hence the need to ref/unref the owner whenever
1770 * the memory region is in use.
1772 * The memory region is a child of its owner. As long as the
1773 * owner doesn't call unparent itself on the memory region,
1774 * ref-ing the owner will also keep the memory region alive.
1775 * Memory regions without an owner are supposed to never go away;
1776 * we do not ref/unref them because it slows down DMA sensibly.
1778 if (mr
&& mr
->owner
) {
1779 object_ref(mr
->owner
);
1783 void memory_region_unref(MemoryRegion
*mr
)
1785 if (mr
&& mr
->owner
) {
1786 object_unref(mr
->owner
);
1790 uint64_t memory_region_size(MemoryRegion
*mr
)
1792 if (int128_eq(mr
->size
, int128_2_64())) {
1795 return int128_get64(mr
->size
);
1798 const char *memory_region_name(const MemoryRegion
*mr
)
1801 ((MemoryRegion
*)mr
)->name
=
1802 g_strdup(object_get_canonical_path_component(OBJECT(mr
)));
1807 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1809 return mr
->ram_device
;
1812 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1814 uint8_t mask
= mr
->dirty_log_mask
;
1815 RAMBlock
*rb
= mr
->ram_block
;
1817 if (global_dirty_log
&& ((rb
&& qemu_ram_is_migratable(rb
)) ||
1818 memory_region_is_iommu(mr
))) {
1819 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1822 if (tcg_enabled() && rb
) {
1823 /* TCG only cares about dirty memory logging for RAM, not IOMMU. */
1824 mask
|= (1 << DIRTY_MEMORY_CODE
);
1829 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1831 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1834 static int memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
,
1837 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1838 IOMMUNotifier
*iommu_notifier
;
1839 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1842 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1843 flags
|= iommu_notifier
->notifier_flags
;
1846 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1847 ret
= imrc
->notify_flag_changed(iommu_mr
,
1848 iommu_mr
->iommu_notify_flags
,
1853 iommu_mr
->iommu_notify_flags
= flags
;
1858 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion
*iommu_mr
,
1859 uint64_t page_size_mask
,
1862 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1865 if (imrc
->iommu_set_page_size_mask
) {
1866 ret
= imrc
->iommu_set_page_size_mask(iommu_mr
, page_size_mask
, errp
);
1871 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1872 IOMMUNotifier
*n
, Error
**errp
)
1874 IOMMUMemoryRegion
*iommu_mr
;
1878 return memory_region_register_iommu_notifier(mr
->alias
, n
, errp
);
1881 /* We need to register for at least one bitfield */
1882 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1883 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1884 assert(n
->start
<= n
->end
);
1885 assert(n
->iommu_idx
>= 0 &&
1886 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1888 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1889 ret
= memory_region_update_iommu_notify_flags(iommu_mr
, errp
);
1891 QLIST_REMOVE(n
, node
);
1896 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1898 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1900 if (imrc
->get_min_page_size
) {
1901 return imrc
->get_min_page_size(iommu_mr
);
1903 return TARGET_PAGE_SIZE
;
1906 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1908 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1909 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1910 hwaddr addr
, granularity
;
1911 IOMMUTLBEntry iotlb
;
1913 /* If the IOMMU has its own replay callback, override */
1915 imrc
->replay(iommu_mr
, n
);
1919 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1921 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1922 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1923 if (iotlb
.perm
!= IOMMU_NONE
) {
1924 n
->notify(n
, &iotlb
);
1927 /* if (2^64 - MR size) < granularity, it's possible to get an
1928 * infinite loop here. This should catch such a wraparound */
1929 if ((addr
+ granularity
) < addr
) {
1935 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1938 IOMMUMemoryRegion
*iommu_mr
;
1941 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1944 QLIST_REMOVE(n
, node
);
1945 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1946 memory_region_update_iommu_notify_flags(iommu_mr
, NULL
);
1949 void memory_region_notify_iommu_one(IOMMUNotifier
*notifier
,
1950 IOMMUTLBEvent
*event
)
1952 IOMMUTLBEntry
*entry
= &event
->entry
;
1953 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1954 IOMMUTLBEntry tmp
= *entry
;
1956 if (event
->type
== IOMMU_NOTIFIER_UNMAP
) {
1957 assert(entry
->perm
== IOMMU_NONE
);
1961 * Skip the notification if the notification does not overlap
1962 * with registered range.
1964 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1968 if (notifier
->notifier_flags
& IOMMU_NOTIFIER_DEVIOTLB_UNMAP
) {
1969 /* Crop (iova, addr_mask) to range */
1970 tmp
.iova
= MAX(tmp
.iova
, notifier
->start
);
1971 tmp
.addr_mask
= MIN(entry_end
, notifier
->end
) - tmp
.iova
;
1973 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1976 if (event
->type
& notifier
->notifier_flags
) {
1977 notifier
->notify(notifier
, &tmp
);
1981 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1983 IOMMUTLBEvent event
)
1985 IOMMUNotifier
*iommu_notifier
;
1987 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1989 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1990 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1991 memory_region_notify_iommu_one(iommu_notifier
, &event
);
1996 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1997 enum IOMMUMemoryRegionAttr attr
,
2000 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2002 if (!imrc
->get_attr
) {
2006 return imrc
->get_attr(iommu_mr
, attr
, data
);
2009 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
2012 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2014 if (!imrc
->attrs_to_index
) {
2018 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2021 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2023 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2025 if (!imrc
->num_indexes
) {
2029 return imrc
->num_indexes(iommu_mr
);
2032 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2034 uint8_t mask
= 1 << client
;
2035 uint8_t old_logging
;
2037 assert(client
== DIRTY_MEMORY_VGA
);
2038 old_logging
= mr
->vga_logging_count
;
2039 mr
->vga_logging_count
+= log
? 1 : -1;
2040 if (!!old_logging
== !!mr
->vga_logging_count
) {
2044 memory_region_transaction_begin();
2045 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2046 memory_region_update_pending
|= mr
->enabled
;
2047 memory_region_transaction_commit();
2050 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2053 assert(mr
->ram_block
);
2054 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2056 memory_region_get_dirty_log_mask(mr
));
2059 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2061 MemoryListener
*listener
;
2066 /* If the same address space has multiple log_sync listeners, we
2067 * visit that address space's FlatView multiple times. But because
2068 * log_sync listeners are rare, it's still cheaper than walking each
2069 * address space once.
2071 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2072 if (!listener
->log_sync
) {
2075 as
= listener
->address_space
;
2076 view
= address_space_get_flatview(as
);
2077 FOR_EACH_FLAT_RANGE(fr
, view
) {
2078 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2079 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2080 listener
->log_sync(listener
, &mrs
);
2083 flatview_unref(view
);
2087 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2090 MemoryRegionSection mrs
;
2091 MemoryListener
*listener
;
2095 hwaddr sec_start
, sec_end
, sec_size
;
2097 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2098 if (!listener
->log_clear
) {
2101 as
= listener
->address_space
;
2102 view
= address_space_get_flatview(as
);
2103 FOR_EACH_FLAT_RANGE(fr
, view
) {
2104 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2106 * Clear dirty bitmap operation only applies to those
2107 * regions whose dirty logging is at least enabled
2112 mrs
= section_from_flat_range(fr
, view
);
2114 sec_start
= MAX(mrs
.offset_within_region
, start
);
2115 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2116 sec_end
= MIN(sec_end
, start
+ len
);
2118 if (sec_start
>= sec_end
) {
2120 * If this memory region section has no intersection
2121 * with the requested range, skip.
2126 /* Valid case; shrink the section if needed */
2127 mrs
.offset_within_address_space
+=
2128 sec_start
- mrs
.offset_within_region
;
2129 mrs
.offset_within_region
= sec_start
;
2130 sec_size
= sec_end
- sec_start
;
2131 mrs
.size
= int128_make64(sec_size
);
2132 listener
->log_clear(listener
, &mrs
);
2134 flatview_unref(view
);
2138 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2143 DirtyBitmapSnapshot
*snapshot
;
2144 assert(mr
->ram_block
);
2145 memory_region_sync_dirty_bitmap(mr
);
2146 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2147 memory_global_after_dirty_log_sync();
2151 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2152 hwaddr addr
, hwaddr size
)
2154 assert(mr
->ram_block
);
2155 return cpu_physical_memory_snapshot_get_dirty(snap
,
2156 memory_region_get_ram_addr(mr
) + addr
, size
);
2159 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2161 if (mr
->readonly
!= readonly
) {
2162 memory_region_transaction_begin();
2163 mr
->readonly
= readonly
;
2164 memory_region_update_pending
|= mr
->enabled
;
2165 memory_region_transaction_commit();
2169 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2171 if (mr
->nonvolatile
!= nonvolatile
) {
2172 memory_region_transaction_begin();
2173 mr
->nonvolatile
= nonvolatile
;
2174 memory_region_update_pending
|= mr
->enabled
;
2175 memory_region_transaction_commit();
2179 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2181 if (mr
->romd_mode
!= romd_mode
) {
2182 memory_region_transaction_begin();
2183 mr
->romd_mode
= romd_mode
;
2184 memory_region_update_pending
|= mr
->enabled
;
2185 memory_region_transaction_commit();
2189 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2190 hwaddr size
, unsigned client
)
2192 assert(mr
->ram_block
);
2193 cpu_physical_memory_test_and_clear_dirty(
2194 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2197 int memory_region_get_fd(MemoryRegion
*mr
)
2201 RCU_READ_LOCK_GUARD();
2205 fd
= mr
->ram_block
->fd
;
2210 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2213 uint64_t offset
= 0;
2215 RCU_READ_LOCK_GUARD();
2217 offset
+= mr
->alias_offset
;
2220 assert(mr
->ram_block
);
2221 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2226 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2230 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2238 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2240 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2243 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2245 assert(mr
->ram_block
);
2247 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2250 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
2252 if (mr
->ram_block
) {
2253 qemu_ram_msync(mr
->ram_block
, addr
, size
);
2257 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
2260 * Might be extended case needed to cover
2261 * different types of memory regions
2263 if (mr
->dirty_log_mask
) {
2264 memory_region_msync(mr
, addr
, size
);
2269 * Call proper memory listeners about the change on the newly
2270 * added/removed CoalescedMemoryRange.
2272 static void memory_region_update_coalesced_range(MemoryRegion
*mr
,
2273 CoalescedMemoryRange
*cmr
,
2280 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2281 view
= address_space_get_flatview(as
);
2282 FOR_EACH_FLAT_RANGE(fr
, view
) {
2284 flat_range_coalesced_io_notify(fr
, as
, cmr
, add
);
2287 flatview_unref(view
);
2291 void memory_region_set_coalescing(MemoryRegion
*mr
)
2293 memory_region_clear_coalescing(mr
);
2294 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2297 void memory_region_add_coalescing(MemoryRegion
*mr
,
2301 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2303 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2304 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2305 memory_region_update_coalesced_range(mr
, cmr
, true);
2306 memory_region_set_flush_coalesced(mr
);
2309 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2311 CoalescedMemoryRange
*cmr
;
2313 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2317 qemu_flush_coalesced_mmio_buffer();
2318 mr
->flush_coalesced_mmio
= false;
2320 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2321 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2322 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2323 memory_region_update_coalesced_range(mr
, cmr
, false);
2328 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2330 mr
->flush_coalesced_mmio
= true;
2333 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2335 qemu_flush_coalesced_mmio_buffer();
2336 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2337 mr
->flush_coalesced_mmio
= false;
2341 static bool userspace_eventfd_warning
;
2343 void memory_region_add_eventfd(MemoryRegion
*mr
,
2350 MemoryRegionIoeventfd mrfd
= {
2351 .addr
.start
= int128_make64(addr
),
2352 .addr
.size
= int128_make64(size
),
2353 .match_data
= match_data
,
2359 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2360 userspace_eventfd_warning
))) {
2361 userspace_eventfd_warning
= true;
2362 error_report("Using eventfd without MMIO binding in KVM. "
2363 "Suboptimal performance expected");
2367 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2369 memory_region_transaction_begin();
2370 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2371 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2376 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2377 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2378 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2379 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2380 mr
->ioeventfds
[i
] = mrfd
;
2381 ioeventfd_update_pending
|= mr
->enabled
;
2382 memory_region_transaction_commit();
2385 void memory_region_del_eventfd(MemoryRegion
*mr
,
2392 MemoryRegionIoeventfd mrfd
= {
2393 .addr
.start
= int128_make64(addr
),
2394 .addr
.size
= int128_make64(size
),
2395 .match_data
= match_data
,
2402 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2404 memory_region_transaction_begin();
2405 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2406 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2410 assert(i
!= mr
->ioeventfd_nb
);
2411 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2412 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2414 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2415 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2416 ioeventfd_update_pending
|= mr
->enabled
;
2417 memory_region_transaction_commit();
2420 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2422 MemoryRegion
*mr
= subregion
->container
;
2423 MemoryRegion
*other
;
2425 memory_region_transaction_begin();
2427 memory_region_ref(subregion
);
2428 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2429 if (subregion
->priority
>= other
->priority
) {
2430 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2434 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2436 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2437 memory_region_transaction_commit();
2440 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2442 MemoryRegion
*subregion
)
2444 assert(!subregion
->container
);
2445 subregion
->container
= mr
;
2446 subregion
->addr
= offset
;
2447 memory_region_update_container_subregions(subregion
);
2450 void memory_region_add_subregion(MemoryRegion
*mr
,
2452 MemoryRegion
*subregion
)
2454 subregion
->priority
= 0;
2455 memory_region_add_subregion_common(mr
, offset
, subregion
);
2458 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2460 MemoryRegion
*subregion
,
2463 subregion
->priority
= priority
;
2464 memory_region_add_subregion_common(mr
, offset
, subregion
);
2467 void memory_region_del_subregion(MemoryRegion
*mr
,
2468 MemoryRegion
*subregion
)
2470 memory_region_transaction_begin();
2471 assert(subregion
->container
== mr
);
2472 subregion
->container
= NULL
;
2473 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2474 memory_region_unref(subregion
);
2475 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2476 memory_region_transaction_commit();
2479 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2481 if (enabled
== mr
->enabled
) {
2484 memory_region_transaction_begin();
2485 mr
->enabled
= enabled
;
2486 memory_region_update_pending
= true;
2487 memory_region_transaction_commit();
2490 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2492 Int128 s
= int128_make64(size
);
2494 if (size
== UINT64_MAX
) {
2497 if (int128_eq(s
, mr
->size
)) {
2500 memory_region_transaction_begin();
2502 memory_region_update_pending
= true;
2503 memory_region_transaction_commit();
2506 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2508 MemoryRegion
*container
= mr
->container
;
2511 memory_region_transaction_begin();
2512 memory_region_ref(mr
);
2513 memory_region_del_subregion(container
, mr
);
2514 mr
->container
= container
;
2515 memory_region_update_container_subregions(mr
);
2516 memory_region_unref(mr
);
2517 memory_region_transaction_commit();
2521 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2523 if (addr
!= mr
->addr
) {
2525 memory_region_readd_subregion(mr
);
2529 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2533 if (offset
== mr
->alias_offset
) {
2537 memory_region_transaction_begin();
2538 mr
->alias_offset
= offset
;
2539 memory_region_update_pending
|= mr
->enabled
;
2540 memory_region_transaction_commit();
2543 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2548 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2550 const AddrRange
*addr
= addr_
;
2551 const FlatRange
*fr
= fr_
;
2553 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2555 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2561 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2563 return bsearch(&addr
, view
->ranges
, view
->nr
,
2564 sizeof(FlatRange
), cmp_flatrange_addr
);
2567 bool memory_region_is_mapped(MemoryRegion
*mr
)
2569 return mr
->container
? true : false;
2572 /* Same as memory_region_find, but it does not add a reference to the
2573 * returned region. It must be called from an RCU critical section.
2575 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2576 hwaddr addr
, uint64_t size
)
2578 MemoryRegionSection ret
= { .mr
= NULL
};
2586 for (root
= mr
; root
->container
; ) {
2587 root
= root
->container
;
2591 as
= memory_region_to_address_space(root
);
2595 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2597 view
= address_space_to_flatview(as
);
2598 fr
= flatview_lookup(view
, range
);
2603 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2609 range
= addrrange_intersection(range
, fr
->addr
);
2610 ret
.offset_within_region
= fr
->offset_in_region
;
2611 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2613 ret
.size
= range
.size
;
2614 ret
.offset_within_address_space
= int128_get64(range
.start
);
2615 ret
.readonly
= fr
->readonly
;
2616 ret
.nonvolatile
= fr
->nonvolatile
;
2620 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2621 hwaddr addr
, uint64_t size
)
2623 MemoryRegionSection ret
;
2624 RCU_READ_LOCK_GUARD();
2625 ret
= memory_region_find_rcu(mr
, addr
, size
);
2627 memory_region_ref(ret
.mr
);
2632 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2636 RCU_READ_LOCK_GUARD();
2637 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2638 return mr
&& mr
!= container
;
2641 void memory_global_dirty_log_sync(void)
2643 memory_region_sync_dirty_bitmap(NULL
);
2646 void memory_global_after_dirty_log_sync(void)
2648 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2651 static VMChangeStateEntry
*vmstate_change
;
2653 void memory_global_dirty_log_start(void)
2655 if (vmstate_change
) {
2656 qemu_del_vm_change_state_handler(vmstate_change
);
2657 vmstate_change
= NULL
;
2660 global_dirty_log
= true;
2662 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2664 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2665 memory_region_transaction_begin();
2666 memory_region_update_pending
= true;
2667 memory_region_transaction_commit();
2670 static void memory_global_dirty_log_do_stop(void)
2672 global_dirty_log
= false;
2674 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2675 memory_region_transaction_begin();
2676 memory_region_update_pending
= true;
2677 memory_region_transaction_commit();
2679 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2682 static void memory_vm_change_state_handler(void *opaque
, int running
,
2686 memory_global_dirty_log_do_stop();
2688 if (vmstate_change
) {
2689 qemu_del_vm_change_state_handler(vmstate_change
);
2690 vmstate_change
= NULL
;
2695 void memory_global_dirty_log_stop(void)
2697 if (!runstate_is_running()) {
2698 if (vmstate_change
) {
2701 vmstate_change
= qemu_add_vm_change_state_handler(
2702 memory_vm_change_state_handler
, NULL
);
2706 memory_global_dirty_log_do_stop();
2709 static void listener_add_address_space(MemoryListener
*listener
,
2715 if (listener
->begin
) {
2716 listener
->begin(listener
);
2718 if (global_dirty_log
) {
2719 if (listener
->log_global_start
) {
2720 listener
->log_global_start(listener
);
2724 view
= address_space_get_flatview(as
);
2725 FOR_EACH_FLAT_RANGE(fr
, view
) {
2726 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2728 if (listener
->region_add
) {
2729 listener
->region_add(listener
, §ion
);
2731 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2732 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2735 if (listener
->commit
) {
2736 listener
->commit(listener
);
2738 flatview_unref(view
);
2741 static void listener_del_address_space(MemoryListener
*listener
,
2747 if (listener
->begin
) {
2748 listener
->begin(listener
);
2750 view
= address_space_get_flatview(as
);
2751 FOR_EACH_FLAT_RANGE(fr
, view
) {
2752 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2754 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2755 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2757 if (listener
->region_del
) {
2758 listener
->region_del(listener
, §ion
);
2761 if (listener
->commit
) {
2762 listener
->commit(listener
);
2764 flatview_unref(view
);
2767 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2769 MemoryListener
*other
= NULL
;
2771 listener
->address_space
= as
;
2772 if (QTAILQ_EMPTY(&memory_listeners
)
2773 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2774 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2776 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2777 if (listener
->priority
< other
->priority
) {
2781 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2784 if (QTAILQ_EMPTY(&as
->listeners
)
2785 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2786 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2788 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2789 if (listener
->priority
< other
->priority
) {
2793 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2796 listener_add_address_space(listener
, as
);
2799 void memory_listener_unregister(MemoryListener
*listener
)
2801 if (!listener
->address_space
) {
2805 listener_del_address_space(listener
, listener
->address_space
);
2806 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2807 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2808 listener
->address_space
= NULL
;
2811 void address_space_remove_listeners(AddressSpace
*as
)
2813 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2814 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2818 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2820 memory_region_ref(root
);
2822 as
->current_map
= NULL
;
2823 as
->ioeventfd_nb
= 0;
2824 as
->ioeventfds
= NULL
;
2825 QTAILQ_INIT(&as
->listeners
);
2826 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2827 as
->name
= g_strdup(name
? name
: "anonymous");
2828 address_space_update_topology(as
);
2829 address_space_update_ioeventfds(as
);
2832 static void do_address_space_destroy(AddressSpace
*as
)
2834 assert(QTAILQ_EMPTY(&as
->listeners
));
2836 flatview_unref(as
->current_map
);
2838 g_free(as
->ioeventfds
);
2839 memory_region_unref(as
->root
);
2842 void address_space_destroy(AddressSpace
*as
)
2844 MemoryRegion
*root
= as
->root
;
2846 /* Flush out anything from MemoryListeners listening in on this */
2847 memory_region_transaction_begin();
2849 memory_region_transaction_commit();
2850 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2852 /* At this point, as->dispatch and as->current_map are dummy
2853 * entries that the guest should never use. Wait for the old
2854 * values to expire before freeing the data.
2857 call_rcu(as
, do_address_space_destroy
, rcu
);
2860 static const char *memory_region_type(MemoryRegion
*mr
)
2863 return memory_region_type(mr
->alias
);
2865 if (memory_region_is_ram_device(mr
)) {
2867 } else if (memory_region_is_romd(mr
)) {
2869 } else if (memory_region_is_rom(mr
)) {
2871 } else if (memory_region_is_ram(mr
)) {
2878 typedef struct MemoryRegionList MemoryRegionList
;
2880 struct MemoryRegionList
{
2881 const MemoryRegion
*mr
;
2882 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2885 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2887 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2888 int128_sub((size), int128_one())) : 0)
2889 #define MTREE_INDENT " "
2891 static void mtree_expand_owner(const char *label
, Object
*obj
)
2893 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
2895 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
2896 if (dev
&& dev
->id
) {
2897 qemu_printf(" id=%s", dev
->id
);
2899 char *canonical_path
= object_get_canonical_path(obj
);
2900 if (canonical_path
) {
2901 qemu_printf(" path=%s", canonical_path
);
2902 g_free(canonical_path
);
2904 qemu_printf(" type=%s", object_get_typename(obj
));
2910 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
2912 Object
*owner
= mr
->owner
;
2913 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
2915 if (!owner
&& !parent
) {
2916 qemu_printf(" orphan");
2920 mtree_expand_owner("owner", owner
);
2922 if (parent
&& parent
!= owner
) {
2923 mtree_expand_owner("parent", parent
);
2927 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
2929 MemoryRegionListHead
*alias_print_queue
,
2930 bool owner
, bool display_disabled
)
2932 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2933 MemoryRegionListHead submr_print_queue
;
2934 const MemoryRegion
*submr
;
2936 hwaddr cur_start
, cur_end
;
2942 cur_start
= base
+ mr
->addr
;
2943 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2946 * Try to detect overflow of memory region. This should never
2947 * happen normally. When it happens, we dump something to warn the
2948 * user who is observing this.
2950 if (cur_start
< base
|| cur_end
< cur_start
) {
2951 qemu_printf("[DETECTED OVERFLOW!] ");
2955 MemoryRegionList
*ml
;
2958 /* check if the alias is already in the queue */
2959 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2960 if (ml
->mr
== mr
->alias
) {
2966 ml
= g_new(MemoryRegionList
, 1);
2968 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2970 if (mr
->enabled
|| display_disabled
) {
2971 for (i
= 0; i
< level
; i
++) {
2972 qemu_printf(MTREE_INDENT
);
2974 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2975 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2976 "-" TARGET_FMT_plx
"%s",
2979 mr
->nonvolatile
? "nv-" : "",
2980 memory_region_type((MemoryRegion
*)mr
),
2981 memory_region_name(mr
),
2982 memory_region_name(mr
->alias
),
2984 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2985 mr
->enabled
? "" : " [disabled]");
2987 mtree_print_mr_owner(mr
);
2992 if (mr
->enabled
|| display_disabled
) {
2993 for (i
= 0; i
< level
; i
++) {
2994 qemu_printf(MTREE_INDENT
);
2996 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2997 " (prio %d, %s%s): %s%s",
3000 mr
->nonvolatile
? "nv-" : "",
3001 memory_region_type((MemoryRegion
*)mr
),
3002 memory_region_name(mr
),
3003 mr
->enabled
? "" : " [disabled]");
3005 mtree_print_mr_owner(mr
);
3011 QTAILQ_INIT(&submr_print_queue
);
3013 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
3014 new_ml
= g_new(MemoryRegionList
, 1);
3016 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3017 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
3018 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
3019 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
3020 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
3026 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
3030 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3031 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
3032 alias_print_queue
, owner
, display_disabled
);
3035 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3040 struct FlatViewInfo
{
3047 static void mtree_print_flatview(gpointer key
, gpointer value
,
3050 FlatView
*view
= key
;
3051 GArray
*fv_address_spaces
= value
;
3052 struct FlatViewInfo
*fvi
= user_data
;
3053 FlatRange
*range
= &view
->ranges
[0];
3059 qemu_printf("FlatView #%d\n", fvi
->counter
);
3062 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3063 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3064 qemu_printf(" AS \"%s\", root: %s",
3065 as
->name
, memory_region_name(as
->root
));
3066 if (as
->root
->alias
) {
3067 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3072 qemu_printf(" Root memory region: %s\n",
3073 view
->root
? memory_region_name(view
->root
) : "(none)");
3076 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3082 if (range
->offset_in_region
) {
3083 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3084 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3085 int128_get64(range
->addr
.start
),
3086 int128_get64(range
->addr
.start
)
3087 + MR_SIZE(range
->addr
.size
),
3089 range
->nonvolatile
? "nv-" : "",
3090 range
->readonly
? "rom" : memory_region_type(mr
),
3091 memory_region_name(mr
),
3092 range
->offset_in_region
);
3094 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3095 " (prio %d, %s%s): %s",
3096 int128_get64(range
->addr
.start
),
3097 int128_get64(range
->addr
.start
)
3098 + MR_SIZE(range
->addr
.size
),
3100 range
->nonvolatile
? "nv-" : "",
3101 range
->readonly
? "rom" : memory_region_type(mr
),
3102 memory_region_name(mr
));
3105 mtree_print_mr_owner(mr
);
3109 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3110 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3111 if (fvi
->ac
->has_memory(current_machine
, as
,
3112 int128_get64(range
->addr
.start
),
3113 MR_SIZE(range
->addr
.size
) + 1)) {
3114 qemu_printf(" %s", fvi
->ac
->name
);
3122 #if !defined(CONFIG_USER_ONLY)
3123 if (fvi
->dispatch_tree
&& view
->root
) {
3124 mtree_print_dispatch(view
->dispatch
, view
->root
);
3131 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3134 FlatView
*view
= key
;
3135 GArray
*fv_address_spaces
= value
;
3137 g_array_unref(fv_address_spaces
);
3138 flatview_unref(view
);
3143 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
)
3145 MemoryRegionListHead ml_head
;
3146 MemoryRegionList
*ml
, *ml2
;
3151 struct FlatViewInfo fvi
= {
3153 .dispatch_tree
= dispatch_tree
,
3156 GArray
*fv_address_spaces
;
3157 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3158 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
3160 if (ac
->has_memory
) {
3164 /* Gather all FVs in one table */
3165 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3166 view
= address_space_get_flatview(as
);
3168 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3169 if (!fv_address_spaces
) {
3170 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3171 g_hash_table_insert(views
, view
, fv_address_spaces
);
3174 g_array_append_val(fv_address_spaces
, as
);
3178 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3181 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3182 g_hash_table_unref(views
);
3187 QTAILQ_INIT(&ml_head
);
3189 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3190 qemu_printf("address-space: %s\n", as
->name
);
3191 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
, disabled
);
3195 /* print aliased regions */
3196 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3197 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3198 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
, disabled
);
3202 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3207 void memory_region_init_ram(MemoryRegion
*mr
,
3208 struct Object
*owner
,
3213 DeviceState
*owner_dev
;
3216 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3218 error_propagate(errp
, err
);
3221 /* This will assert if owner is neither NULL nor a DeviceState.
3222 * We only want the owner here for the purposes of defining a
3223 * unique name for migration. TODO: Ideally we should implement
3224 * a naming scheme for Objects which are not DeviceStates, in
3225 * which case we can relax this restriction.
3227 owner_dev
= DEVICE(owner
);
3228 vmstate_register_ram(mr
, owner_dev
);
3231 void memory_region_init_rom(MemoryRegion
*mr
,
3232 struct Object
*owner
,
3237 DeviceState
*owner_dev
;
3240 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3242 error_propagate(errp
, err
);
3245 /* This will assert if owner is neither NULL nor a DeviceState.
3246 * We only want the owner here for the purposes of defining a
3247 * unique name for migration. TODO: Ideally we should implement
3248 * a naming scheme for Objects which are not DeviceStates, in
3249 * which case we can relax this restriction.
3251 owner_dev
= DEVICE(owner
);
3252 vmstate_register_ram(mr
, owner_dev
);
3255 void memory_region_init_rom_device(MemoryRegion
*mr
,
3256 struct Object
*owner
,
3257 const MemoryRegionOps
*ops
,
3263 DeviceState
*owner_dev
;
3266 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3269 error_propagate(errp
, err
);
3272 /* This will assert if owner is neither NULL nor a DeviceState.
3273 * We only want the owner here for the purposes of defining a
3274 * unique name for migration. TODO: Ideally we should implement
3275 * a naming scheme for Objects which are not DeviceStates, in
3276 * which case we can relax this restriction.
3278 owner_dev
= DEVICE(owner
);
3279 vmstate_register_ram(mr
, owner_dev
);
3283 * Support softmmu builds with CONFIG_FUZZ using a weak symbol and a stub for
3284 * the fuzz_dma_read_cb callback
3287 void __attribute__((weak
)) fuzz_dma_read_cb(size_t addr
,
3294 static const TypeInfo memory_region_info
= {
3295 .parent
= TYPE_OBJECT
,
3296 .name
= TYPE_MEMORY_REGION
,
3297 .class_size
= sizeof(MemoryRegionClass
),
3298 .instance_size
= sizeof(MemoryRegion
),
3299 .instance_init
= memory_region_initfn
,
3300 .instance_finalize
= memory_region_finalize
,
3303 static const TypeInfo iommu_memory_region_info
= {
3304 .parent
= TYPE_MEMORY_REGION
,
3305 .name
= TYPE_IOMMU_MEMORY_REGION
,
3306 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3307 .instance_size
= sizeof(IOMMUMemoryRegion
),
3308 .instance_init
= iommu_memory_region_initfn
,
3312 static void memory_register_types(void)
3314 type_register_static(&memory_region_info
);
3315 type_register_static(&iommu_memory_region_info
);
3318 type_init(memory_register_types
)