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"
17 #include "qapi/error.h"
19 #include "exec/memory.h"
20 #include "exec/address-spaces.h"
21 #include "qapi/visitor.h"
22 #include "qemu/bitops.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/qemu-print.h"
26 #include "qom/object.h"
27 #include "trace-root.h"
29 #include "exec/memory-internal.h"
30 #include "exec/ram_addr.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/runstate.h"
33 #include "sysemu/tcg.h"
34 #include "sysemu/accel.h"
35 #include "hw/boards.h"
36 #include "migration/vmstate.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 bool global_dirty_log
;
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 return !memory_region_ioeventfd_before(a
, b
)
208 && !memory_region_ioeventfd_before(b
, a
);
211 /* Range of memory in the global map. Addresses are absolute. */
214 hwaddr offset_in_region
;
216 uint8_t dirty_log_mask
;
222 #define FOR_EACH_FLAT_RANGE(var, view) \
223 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
225 static inline MemoryRegionSection
226 section_from_flat_range(FlatRange
*fr
, FlatView
*fv
)
228 return (MemoryRegionSection
) {
231 .offset_within_region
= fr
->offset_in_region
,
232 .size
= fr
->addr
.size
,
233 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
234 .readonly
= fr
->readonly
,
235 .nonvolatile
= fr
->nonvolatile
,
239 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
241 return a
->mr
== b
->mr
242 && addrrange_equal(a
->addr
, b
->addr
)
243 && a
->offset_in_region
== b
->offset_in_region
244 && a
->romd_mode
== b
->romd_mode
245 && a
->readonly
== b
->readonly
246 && a
->nonvolatile
== b
->nonvolatile
;
249 static FlatView
*flatview_new(MemoryRegion
*mr_root
)
253 view
= g_new0(FlatView
, 1);
255 view
->root
= mr_root
;
256 memory_region_ref(mr_root
);
257 trace_flatview_new(view
, mr_root
);
262 /* Insert a range into a given position. Caller is responsible for maintaining
265 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
267 if (view
->nr
== view
->nr_allocated
) {
268 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
269 view
->ranges
= g_realloc(view
->ranges
,
270 view
->nr_allocated
* sizeof(*view
->ranges
));
272 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
273 (view
->nr
- pos
) * sizeof(FlatRange
));
274 view
->ranges
[pos
] = *range
;
275 memory_region_ref(range
->mr
);
279 static void flatview_destroy(FlatView
*view
)
283 trace_flatview_destroy(view
, view
->root
);
284 if (view
->dispatch
) {
285 address_space_dispatch_free(view
->dispatch
);
287 for (i
= 0; i
< view
->nr
; i
++) {
288 memory_region_unref(view
->ranges
[i
].mr
);
290 g_free(view
->ranges
);
291 memory_region_unref(view
->root
);
295 static bool flatview_ref(FlatView
*view
)
297 return atomic_fetch_inc_nonzero(&view
->ref
) > 0;
300 void flatview_unref(FlatView
*view
)
302 if (atomic_fetch_dec(&view
->ref
) == 1) {
303 trace_flatview_destroy_rcu(view
, view
->root
);
305 call_rcu(view
, flatview_destroy
, rcu
);
309 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
311 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
313 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
315 int128_make64(r2
->offset_in_region
))
316 && r1
->dirty_log_mask
== r2
->dirty_log_mask
317 && r1
->romd_mode
== r2
->romd_mode
318 && r1
->readonly
== r2
->readonly
319 && r1
->nonvolatile
== r2
->nonvolatile
;
322 /* Attempt to simplify a view by merging adjacent ranges */
323 static void flatview_simplify(FlatView
*view
)
328 while (i
< view
->nr
) {
331 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
332 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
336 for (k
= i
; k
< j
; k
++) {
337 memory_region_unref(view
->ranges
[k
].mr
);
339 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
340 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
345 static bool memory_region_big_endian(MemoryRegion
*mr
)
347 #ifdef TARGET_WORDS_BIGENDIAN
348 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
350 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
354 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, MemOp op
)
356 if ((op
& MO_BSWAP
) != devend_memop(mr
->ops
->endianness
)) {
357 switch (op
& MO_SIZE
) {
361 *data
= bswap16(*data
);
364 *data
= bswap32(*data
);
367 *data
= bswap64(*data
);
370 g_assert_not_reached();
375 static inline void memory_region_shift_read_access(uint64_t *value
,
381 *value
|= (tmp
& mask
) << shift
;
383 *value
|= (tmp
& mask
) >> -shift
;
387 static inline uint64_t memory_region_shift_write_access(uint64_t *value
,
394 tmp
= (*value
>> shift
) & mask
;
396 tmp
= (*value
<< -shift
) & mask
;
402 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
405 hwaddr abs_addr
= offset
;
407 abs_addr
+= mr
->addr
;
408 for (root
= mr
; root
->container
; ) {
409 root
= root
->container
;
410 abs_addr
+= root
->addr
;
416 static int get_cpu_index(void)
419 return current_cpu
->cpu_index
;
424 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
434 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
436 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
437 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
438 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
439 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
441 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
445 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
456 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
458 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
459 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
460 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
461 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
463 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
467 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
475 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
478 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
479 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
480 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
481 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
483 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
487 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
495 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
498 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
499 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
500 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
501 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
503 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
506 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
509 unsigned access_size_min
,
510 unsigned access_size_max
,
511 MemTxResult (*access_fn
)
522 uint64_t access_mask
;
523 unsigned access_size
;
525 MemTxResult r
= MEMTX_OK
;
527 if (!access_size_min
) {
530 if (!access_size_max
) {
534 /* FIXME: support unaligned access? */
535 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
536 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
537 if (memory_region_big_endian(mr
)) {
538 for (i
= 0; i
< size
; i
+= access_size
) {
539 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
540 (size
- access_size
- i
) * 8, access_mask
, attrs
);
543 for (i
= 0; i
< size
; i
+= access_size
) {
544 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
551 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
555 while (mr
->container
) {
558 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
559 if (mr
== as
->root
) {
566 /* Render a memory region into the global view. Ranges in @view obscure
569 static void render_memory_region(FlatView
*view
,
576 MemoryRegion
*subregion
;
578 hwaddr offset_in_region
;
588 int128_addto(&base
, int128_make64(mr
->addr
));
589 readonly
|= mr
->readonly
;
590 nonvolatile
|= mr
->nonvolatile
;
592 tmp
= addrrange_make(base
, mr
->size
);
594 if (!addrrange_intersects(tmp
, clip
)) {
598 clip
= addrrange_intersection(tmp
, clip
);
601 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
602 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
603 render_memory_region(view
, mr
->alias
, base
, clip
,
604 readonly
, nonvolatile
);
608 /* Render subregions in priority order. */
609 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
610 render_memory_region(view
, subregion
, base
, clip
,
611 readonly
, nonvolatile
);
614 if (!mr
->terminates
) {
618 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
623 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
624 fr
.romd_mode
= mr
->romd_mode
;
625 fr
.readonly
= readonly
;
626 fr
.nonvolatile
= nonvolatile
;
628 /* Render the region itself into any gaps left by the current view. */
629 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
630 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
633 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
634 now
= int128_min(remain
,
635 int128_sub(view
->ranges
[i
].addr
.start
, base
));
636 fr
.offset_in_region
= offset_in_region
;
637 fr
.addr
= addrrange_make(base
, now
);
638 flatview_insert(view
, i
, &fr
);
640 int128_addto(&base
, now
);
641 offset_in_region
+= int128_get64(now
);
642 int128_subfrom(&remain
, now
);
644 now
= int128_sub(int128_min(int128_add(base
, remain
),
645 addrrange_end(view
->ranges
[i
].addr
)),
647 int128_addto(&base
, now
);
648 offset_in_region
+= int128_get64(now
);
649 int128_subfrom(&remain
, now
);
651 if (int128_nz(remain
)) {
652 fr
.offset_in_region
= offset_in_region
;
653 fr
.addr
= addrrange_make(base
, remain
);
654 flatview_insert(view
, i
, &fr
);
658 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
660 while (mr
->enabled
) {
662 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
663 /* The alias is included in its entirety. Use it as
664 * the "real" root, so that we can share more FlatViews.
669 } else if (!mr
->terminates
) {
670 unsigned int found
= 0;
671 MemoryRegion
*child
, *next
= NULL
;
672 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
673 if (child
->enabled
) {
678 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
679 /* A child is included in its entirety. If it's the only
680 * enabled one, use it in the hope of finding an alias down the
681 * way. This will also let us share FlatViews.
702 /* Render a memory topology into a list of disjoint absolute ranges. */
703 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
708 view
= flatview_new(mr
);
711 render_memory_region(view
, mr
, int128_zero(),
712 addrrange_make(int128_zero(), int128_2_64()),
715 flatview_simplify(view
);
717 view
->dispatch
= address_space_dispatch_new(view
);
718 for (i
= 0; i
< view
->nr
; i
++) {
719 MemoryRegionSection mrs
=
720 section_from_flat_range(&view
->ranges
[i
], view
);
721 flatview_add_to_dispatch(view
, &mrs
);
723 address_space_dispatch_compact(view
->dispatch
);
724 g_hash_table_replace(flat_views
, mr
, view
);
729 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
730 MemoryRegionIoeventfd
*fds_new
,
732 MemoryRegionIoeventfd
*fds_old
,
736 MemoryRegionIoeventfd
*fd
;
737 MemoryRegionSection section
;
739 /* Generate a symmetric difference of the old and new fd sets, adding
740 * and deleting as necessary.
744 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
745 if (iold
< fds_old_nb
746 && (inew
== fds_new_nb
747 || memory_region_ioeventfd_before(&fds_old
[iold
],
750 section
= (MemoryRegionSection
) {
751 .fv
= address_space_to_flatview(as
),
752 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
753 .size
= fd
->addr
.size
,
755 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
756 fd
->match_data
, fd
->data
, fd
->e
);
758 } else if (inew
< fds_new_nb
759 && (iold
== fds_old_nb
760 || memory_region_ioeventfd_before(&fds_new
[inew
],
763 section
= (MemoryRegionSection
) {
764 .fv
= address_space_to_flatview(as
),
765 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
766 .size
= fd
->addr
.size
,
768 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
769 fd
->match_data
, fd
->data
, fd
->e
);
778 FlatView
*address_space_get_flatview(AddressSpace
*as
)
784 view
= address_space_to_flatview(as
);
785 /* If somebody has replaced as->current_map concurrently,
786 * flatview_ref returns false.
788 } while (!flatview_ref(view
));
793 static void address_space_update_ioeventfds(AddressSpace
*as
)
797 unsigned ioeventfd_nb
= 0;
798 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
802 view
= address_space_get_flatview(as
);
803 FOR_EACH_FLAT_RANGE(fr
, view
) {
804 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
805 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
806 int128_sub(fr
->addr
.start
,
807 int128_make64(fr
->offset_in_region
)));
808 if (addrrange_intersects(fr
->addr
, tmp
)) {
810 ioeventfds
= g_realloc(ioeventfds
,
811 ioeventfd_nb
* sizeof(*ioeventfds
));
812 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
813 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
818 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
819 as
->ioeventfds
, as
->ioeventfd_nb
);
821 g_free(as
->ioeventfds
);
822 as
->ioeventfds
= ioeventfds
;
823 as
->ioeventfd_nb
= ioeventfd_nb
;
824 flatview_unref(view
);
828 * Notify the memory listeners about the coalesced IO change events of
829 * range `cmr'. Only the part that has intersection of the specified
830 * FlatRange will be sent.
832 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
833 CoalescedMemoryRange
*cmr
, bool add
)
837 tmp
= addrrange_shift(cmr
->addr
,
838 int128_sub(fr
->addr
.start
,
839 int128_make64(fr
->offset_in_region
)));
840 if (!addrrange_intersects(tmp
, fr
->addr
)) {
843 tmp
= addrrange_intersection(tmp
, fr
->addr
);
846 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
847 int128_get64(tmp
.start
),
848 int128_get64(tmp
.size
));
850 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
851 int128_get64(tmp
.start
),
852 int128_get64(tmp
.size
));
856 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
858 CoalescedMemoryRange
*cmr
;
860 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
861 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
865 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
867 MemoryRegion
*mr
= fr
->mr
;
868 CoalescedMemoryRange
*cmr
;
870 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
874 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
875 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
879 static void address_space_update_topology_pass(AddressSpace
*as
,
880 const FlatView
*old_view
,
881 const FlatView
*new_view
,
885 FlatRange
*frold
, *frnew
;
887 /* Generate a symmetric difference of the old and new memory maps.
888 * Kill ranges in the old map, and instantiate ranges in the new map.
891 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
892 if (iold
< old_view
->nr
) {
893 frold
= &old_view
->ranges
[iold
];
897 if (inew
< new_view
->nr
) {
898 frnew
= &new_view
->ranges
[inew
];
905 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
906 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
907 && !flatrange_equal(frold
, frnew
)))) {
908 /* In old but not in new, or in both but attributes changed. */
911 flat_range_coalesced_io_del(frold
, as
);
912 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
916 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
917 /* In both and unchanged (except logging may have changed) */
920 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
921 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
922 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
923 frold
->dirty_log_mask
,
924 frnew
->dirty_log_mask
);
926 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
927 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
928 frold
->dirty_log_mask
,
929 frnew
->dirty_log_mask
);
939 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
940 flat_range_coalesced_io_add(frnew
, as
);
948 static void flatviews_init(void)
950 static FlatView
*empty_view
;
956 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
957 (GDestroyNotify
) flatview_unref
);
959 empty_view
= generate_memory_topology(NULL
);
960 /* We keep it alive forever in the global variable. */
961 flatview_ref(empty_view
);
963 g_hash_table_replace(flat_views
, NULL
, empty_view
);
964 flatview_ref(empty_view
);
968 static void flatviews_reset(void)
973 g_hash_table_unref(flat_views
);
978 /* Render unique FVs */
979 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
980 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
982 if (g_hash_table_lookup(flat_views
, physmr
)) {
986 generate_memory_topology(physmr
);
990 static void address_space_set_flatview(AddressSpace
*as
)
992 FlatView
*old_view
= address_space_to_flatview(as
);
993 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
994 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
998 if (old_view
== new_view
) {
1003 flatview_ref(old_view
);
1006 flatview_ref(new_view
);
1008 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1009 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1012 old_view2
= &tmpview
;
1014 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1015 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1018 /* Writes are protected by the BQL. */
1019 atomic_rcu_set(&as
->current_map
, new_view
);
1021 flatview_unref(old_view
);
1024 /* Note that all the old MemoryRegions are still alive up to this
1025 * point. This relieves most MemoryListeners from the need to
1026 * ref/unref the MemoryRegions they get---unless they use them
1027 * outside the iothread mutex, in which case precise reference
1028 * counting is necessary.
1031 flatview_unref(old_view
);
1035 static void address_space_update_topology(AddressSpace
*as
)
1037 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1040 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1041 generate_memory_topology(physmr
);
1043 address_space_set_flatview(as
);
1046 void memory_region_transaction_begin(void)
1048 qemu_flush_coalesced_mmio_buffer();
1049 ++memory_region_transaction_depth
;
1052 void memory_region_transaction_commit(void)
1056 assert(memory_region_transaction_depth
);
1057 assert(qemu_mutex_iothread_locked());
1059 --memory_region_transaction_depth
;
1060 if (!memory_region_transaction_depth
) {
1061 if (memory_region_update_pending
) {
1064 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1066 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1067 address_space_set_flatview(as
);
1068 address_space_update_ioeventfds(as
);
1070 memory_region_update_pending
= false;
1071 ioeventfd_update_pending
= false;
1072 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1073 } else if (ioeventfd_update_pending
) {
1074 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1075 address_space_update_ioeventfds(as
);
1077 ioeventfd_update_pending
= false;
1082 static void memory_region_destructor_none(MemoryRegion
*mr
)
1086 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1088 qemu_ram_free(mr
->ram_block
);
1091 static bool memory_region_need_escape(char c
)
1093 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1096 static char *memory_region_escape_name(const char *name
)
1103 for (p
= name
; *p
; p
++) {
1104 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1106 if (bytes
== p
- name
) {
1107 return g_memdup(name
, bytes
+ 1);
1110 escaped
= g_malloc(bytes
+ 1);
1111 for (p
= name
, q
= escaped
; *p
; p
++) {
1113 if (unlikely(memory_region_need_escape(c
))) {
1116 *q
++ = "0123456789abcdef"[c
>> 4];
1117 c
= "0123456789abcdef"[c
& 15];
1125 static void memory_region_do_init(MemoryRegion
*mr
,
1130 mr
->size
= int128_make64(size
);
1131 if (size
== UINT64_MAX
) {
1132 mr
->size
= int128_2_64();
1134 mr
->name
= g_strdup(name
);
1136 mr
->ram_block
= NULL
;
1139 char *escaped_name
= memory_region_escape_name(name
);
1140 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1143 owner
= container_get(qdev_get_machine(), "/unattached");
1146 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1147 object_unref(OBJECT(mr
));
1149 g_free(escaped_name
);
1153 void memory_region_init(MemoryRegion
*mr
,
1158 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1159 memory_region_do_init(mr
, owner
, name
, size
);
1162 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1163 void *opaque
, Error
**errp
)
1165 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1166 uint64_t value
= mr
->addr
;
1168 visit_type_uint64(v
, name
, &value
, errp
);
1171 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1172 const char *name
, void *opaque
,
1175 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1176 gchar
*path
= (gchar
*)"";
1178 if (mr
->container
) {
1179 path
= object_get_canonical_path(OBJECT(mr
->container
));
1181 visit_type_str(v
, name
, &path
, errp
);
1182 if (mr
->container
) {
1187 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1190 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1192 return OBJECT(mr
->container
);
1195 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1196 const char *name
, void *opaque
,
1199 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1200 int32_t value
= mr
->priority
;
1202 visit_type_int32(v
, name
, &value
, errp
);
1205 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1206 void *opaque
, Error
**errp
)
1208 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1209 uint64_t value
= memory_region_size(mr
);
1211 visit_type_uint64(v
, name
, &value
, errp
);
1214 static void memory_region_initfn(Object
*obj
)
1216 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1219 mr
->ops
= &unassigned_mem_ops
;
1221 mr
->romd_mode
= true;
1222 mr
->global_locking
= true;
1223 mr
->destructor
= memory_region_destructor_none
;
1224 QTAILQ_INIT(&mr
->subregions
);
1225 QTAILQ_INIT(&mr
->coalesced
);
1227 op
= object_property_add(OBJECT(mr
), "container",
1228 "link<" TYPE_MEMORY_REGION
">",
1229 memory_region_get_container
,
1230 NULL
, /* memory_region_set_container */
1231 NULL
, NULL
, &error_abort
);
1232 op
->resolve
= memory_region_resolve_container
;
1234 object_property_add(OBJECT(mr
), "addr", "uint64",
1235 memory_region_get_addr
,
1236 NULL
, /* memory_region_set_addr */
1237 NULL
, NULL
, &error_abort
);
1238 object_property_add(OBJECT(mr
), "priority", "uint32",
1239 memory_region_get_priority
,
1240 NULL
, /* memory_region_set_priority */
1241 NULL
, NULL
, &error_abort
);
1242 object_property_add(OBJECT(mr
), "size", "uint64",
1243 memory_region_get_size
,
1244 NULL
, /* memory_region_set_size, */
1245 NULL
, NULL
, &error_abort
);
1248 static void iommu_memory_region_initfn(Object
*obj
)
1250 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1252 mr
->is_iommu
= true;
1255 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1258 #ifdef DEBUG_UNASSIGNED
1259 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1261 if (current_cpu
!= NULL
) {
1262 bool is_exec
= current_cpu
->mem_io_access_type
== MMU_INST_FETCH
;
1263 cpu_unassigned_access(current_cpu
, addr
, false, is_exec
, 0, size
);
1268 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1269 uint64_t val
, unsigned size
)
1271 #ifdef DEBUG_UNASSIGNED
1272 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1274 if (current_cpu
!= NULL
) {
1275 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1279 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1280 unsigned size
, bool is_write
,
1286 const MemoryRegionOps unassigned_mem_ops
= {
1287 .valid
.accepts
= unassigned_mem_accepts
,
1288 .endianness
= DEVICE_NATIVE_ENDIAN
,
1291 static uint64_t memory_region_ram_device_read(void *opaque
,
1292 hwaddr addr
, unsigned size
)
1294 MemoryRegion
*mr
= opaque
;
1295 uint64_t data
= (uint64_t)~0;
1299 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1302 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1305 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1308 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1312 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1317 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1318 uint64_t data
, unsigned size
)
1320 MemoryRegion
*mr
= opaque
;
1322 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1326 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1329 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1332 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1335 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1340 static const MemoryRegionOps ram_device_mem_ops
= {
1341 .read
= memory_region_ram_device_read
,
1342 .write
= memory_region_ram_device_write
,
1343 .endianness
= DEVICE_HOST_ENDIAN
,
1345 .min_access_size
= 1,
1346 .max_access_size
= 8,
1350 .min_access_size
= 1,
1351 .max_access_size
= 8,
1356 bool memory_region_access_valid(MemoryRegion
*mr
,
1362 int access_size_min
, access_size_max
;
1365 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1369 if (!mr
->ops
->valid
.accepts
) {
1373 access_size_min
= mr
->ops
->valid
.min_access_size
;
1374 if (!mr
->ops
->valid
.min_access_size
) {
1375 access_size_min
= 1;
1378 access_size_max
= mr
->ops
->valid
.max_access_size
;
1379 if (!mr
->ops
->valid
.max_access_size
) {
1380 access_size_max
= 4;
1383 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1384 for (i
= 0; i
< size
; i
+= access_size
) {
1385 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1394 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1402 if (mr
->ops
->read
) {
1403 return access_with_adjusted_size(addr
, pval
, size
,
1404 mr
->ops
->impl
.min_access_size
,
1405 mr
->ops
->impl
.max_access_size
,
1406 memory_region_read_accessor
,
1409 return access_with_adjusted_size(addr
, pval
, size
,
1410 mr
->ops
->impl
.min_access_size
,
1411 mr
->ops
->impl
.max_access_size
,
1412 memory_region_read_with_attrs_accessor
,
1417 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1423 unsigned size
= memop_size(op
);
1426 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1427 *pval
= unassigned_mem_read(mr
, addr
, size
);
1428 return MEMTX_DECODE_ERROR
;
1431 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1432 adjust_endianness(mr
, pval
, op
);
1436 /* Return true if an eventfd was signalled */
1437 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1443 MemoryRegionIoeventfd ioeventfd
= {
1444 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1449 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1450 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1451 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1453 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1454 event_notifier_set(ioeventfd
.e
);
1462 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1468 unsigned size
= memop_size(op
);
1470 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1471 unassigned_mem_write(mr
, addr
, data
, size
);
1472 return MEMTX_DECODE_ERROR
;
1475 adjust_endianness(mr
, &data
, op
);
1477 if ((!kvm_eventfds_enabled()) &&
1478 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1482 if (mr
->ops
->write
) {
1483 return access_with_adjusted_size(addr
, &data
, size
,
1484 mr
->ops
->impl
.min_access_size
,
1485 mr
->ops
->impl
.max_access_size
,
1486 memory_region_write_accessor
, mr
,
1490 access_with_adjusted_size(addr
, &data
, size
,
1491 mr
->ops
->impl
.min_access_size
,
1492 mr
->ops
->impl
.max_access_size
,
1493 memory_region_write_with_attrs_accessor
,
1498 void memory_region_init_io(MemoryRegion
*mr
,
1500 const MemoryRegionOps
*ops
,
1505 memory_region_init(mr
, owner
, name
, size
);
1506 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1507 mr
->opaque
= opaque
;
1508 mr
->terminates
= true;
1511 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1517 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1520 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1528 memory_region_init(mr
, owner
, name
, size
);
1530 mr
->terminates
= true;
1531 mr
->destructor
= memory_region_destructor_ram
;
1532 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, &err
);
1533 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1535 mr
->size
= int128_zero();
1536 object_unparent(OBJECT(mr
));
1537 error_propagate(errp
, err
);
1541 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1546 void (*resized
)(const char*,
1552 memory_region_init(mr
, owner
, name
, size
);
1554 mr
->terminates
= true;
1555 mr
->destructor
= memory_region_destructor_ram
;
1556 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1558 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1560 mr
->size
= int128_zero();
1561 object_unparent(OBJECT(mr
));
1562 error_propagate(errp
, err
);
1567 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1568 struct Object
*owner
,
1577 memory_region_init(mr
, owner
, name
, size
);
1579 mr
->terminates
= true;
1580 mr
->destructor
= memory_region_destructor_ram
;
1582 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
, &err
);
1583 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1585 mr
->size
= int128_zero();
1586 object_unparent(OBJECT(mr
));
1587 error_propagate(errp
, err
);
1591 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1592 struct Object
*owner
,
1600 memory_region_init(mr
, owner
, name
, size
);
1602 mr
->terminates
= true;
1603 mr
->destructor
= memory_region_destructor_ram
;
1604 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
,
1605 share
? RAM_SHARED
: 0,
1607 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1609 mr
->size
= int128_zero();
1610 object_unparent(OBJECT(mr
));
1611 error_propagate(errp
, err
);
1616 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1622 memory_region_init(mr
, owner
, name
, size
);
1624 mr
->terminates
= true;
1625 mr
->destructor
= memory_region_destructor_ram
;
1626 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1628 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1629 assert(ptr
!= NULL
);
1630 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1633 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1639 memory_region_init(mr
, owner
, name
, size
);
1641 mr
->terminates
= true;
1642 mr
->ram_device
= true;
1643 mr
->ops
= &ram_device_mem_ops
;
1645 mr
->destructor
= memory_region_destructor_ram
;
1646 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1647 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1648 assert(ptr
!= NULL
);
1649 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1652 void memory_region_init_alias(MemoryRegion
*mr
,
1659 memory_region_init(mr
, owner
, name
, size
);
1661 mr
->alias_offset
= offset
;
1664 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1665 struct Object
*owner
,
1671 memory_region_init(mr
, owner
, name
, size
);
1673 mr
->readonly
= true;
1674 mr
->terminates
= true;
1675 mr
->destructor
= memory_region_destructor_ram
;
1676 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1677 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1679 mr
->size
= int128_zero();
1680 object_unparent(OBJECT(mr
));
1681 error_propagate(errp
, err
);
1685 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1687 const MemoryRegionOps
*ops
,
1695 memory_region_init(mr
, owner
, name
, size
);
1697 mr
->opaque
= opaque
;
1698 mr
->terminates
= true;
1699 mr
->rom_device
= true;
1700 mr
->destructor
= memory_region_destructor_ram
;
1701 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1703 mr
->size
= int128_zero();
1704 object_unparent(OBJECT(mr
));
1705 error_propagate(errp
, err
);
1709 void memory_region_init_iommu(void *_iommu_mr
,
1710 size_t instance_size
,
1711 const char *mrtypename
,
1716 struct IOMMUMemoryRegion
*iommu_mr
;
1717 struct MemoryRegion
*mr
;
1719 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1720 mr
= MEMORY_REGION(_iommu_mr
);
1721 memory_region_do_init(mr
, owner
, name
, size
);
1722 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1723 mr
->terminates
= true; /* then re-forwards */
1724 QLIST_INIT(&iommu_mr
->iommu_notify
);
1725 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1728 static void memory_region_finalize(Object
*obj
)
1730 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1732 assert(!mr
->container
);
1734 /* We know the region is not visible in any address space (it
1735 * does not have a container and cannot be a root either because
1736 * it has no references, so we can blindly clear mr->enabled.
1737 * memory_region_set_enabled instead could trigger a transaction
1738 * and cause an infinite loop.
1740 mr
->enabled
= false;
1741 memory_region_transaction_begin();
1742 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1743 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1744 memory_region_del_subregion(mr
, subregion
);
1746 memory_region_transaction_commit();
1749 memory_region_clear_coalescing(mr
);
1750 g_free((char *)mr
->name
);
1751 g_free(mr
->ioeventfds
);
1754 Object
*memory_region_owner(MemoryRegion
*mr
)
1756 Object
*obj
= OBJECT(mr
);
1760 void memory_region_ref(MemoryRegion
*mr
)
1762 /* MMIO callbacks most likely will access data that belongs
1763 * to the owner, hence the need to ref/unref the owner whenever
1764 * the memory region is in use.
1766 * The memory region is a child of its owner. As long as the
1767 * owner doesn't call unparent itself on the memory region,
1768 * ref-ing the owner will also keep the memory region alive.
1769 * Memory regions without an owner are supposed to never go away;
1770 * we do not ref/unref them because it slows down DMA sensibly.
1772 if (mr
&& mr
->owner
) {
1773 object_ref(mr
->owner
);
1777 void memory_region_unref(MemoryRegion
*mr
)
1779 if (mr
&& mr
->owner
) {
1780 object_unref(mr
->owner
);
1784 uint64_t memory_region_size(MemoryRegion
*mr
)
1786 if (int128_eq(mr
->size
, int128_2_64())) {
1789 return int128_get64(mr
->size
);
1792 const char *memory_region_name(const MemoryRegion
*mr
)
1795 ((MemoryRegion
*)mr
)->name
=
1796 object_get_canonical_path_component(OBJECT(mr
));
1801 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1803 return mr
->ram_device
;
1806 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1808 uint8_t mask
= mr
->dirty_log_mask
;
1809 if (global_dirty_log
&& mr
->ram_block
) {
1810 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1815 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1817 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1820 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1822 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1823 IOMMUNotifier
*iommu_notifier
;
1824 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1826 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1827 flags
|= iommu_notifier
->notifier_flags
;
1830 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1831 imrc
->notify_flag_changed(iommu_mr
,
1832 iommu_mr
->iommu_notify_flags
,
1836 iommu_mr
->iommu_notify_flags
= flags
;
1839 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1842 IOMMUMemoryRegion
*iommu_mr
;
1845 memory_region_register_iommu_notifier(mr
->alias
, n
);
1849 /* We need to register for at least one bitfield */
1850 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1851 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1852 assert(n
->start
<= n
->end
);
1853 assert(n
->iommu_idx
>= 0 &&
1854 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1856 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1857 memory_region_update_iommu_notify_flags(iommu_mr
);
1860 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1862 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1864 if (imrc
->get_min_page_size
) {
1865 return imrc
->get_min_page_size(iommu_mr
);
1867 return TARGET_PAGE_SIZE
;
1870 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1872 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1873 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1874 hwaddr addr
, granularity
;
1875 IOMMUTLBEntry iotlb
;
1877 /* If the IOMMU has its own replay callback, override */
1879 imrc
->replay(iommu_mr
, n
);
1883 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1885 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1886 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1887 if (iotlb
.perm
!= IOMMU_NONE
) {
1888 n
->notify(n
, &iotlb
);
1891 /* if (2^64 - MR size) < granularity, it's possible to get an
1892 * infinite loop here. This should catch such a wraparound */
1893 if ((addr
+ granularity
) < addr
) {
1899 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1902 IOMMUMemoryRegion
*iommu_mr
;
1905 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1908 QLIST_REMOVE(n
, node
);
1909 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1910 memory_region_update_iommu_notify_flags(iommu_mr
);
1913 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1914 IOMMUTLBEntry
*entry
)
1916 IOMMUNotifierFlag request_flags
;
1917 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1920 * Skip the notification if the notification does not overlap
1921 * with registered range.
1923 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1927 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1929 if (entry
->perm
& IOMMU_RW
) {
1930 request_flags
= IOMMU_NOTIFIER_MAP
;
1932 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1935 if (notifier
->notifier_flags
& request_flags
) {
1936 notifier
->notify(notifier
, entry
);
1940 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1942 IOMMUTLBEntry entry
)
1944 IOMMUNotifier
*iommu_notifier
;
1946 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1948 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1949 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1950 memory_region_notify_one(iommu_notifier
, &entry
);
1955 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1956 enum IOMMUMemoryRegionAttr attr
,
1959 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1961 if (!imrc
->get_attr
) {
1965 return imrc
->get_attr(iommu_mr
, attr
, data
);
1968 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1971 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1973 if (!imrc
->attrs_to_index
) {
1977 return imrc
->attrs_to_index(iommu_mr
, attrs
);
1980 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
1982 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1984 if (!imrc
->num_indexes
) {
1988 return imrc
->num_indexes(iommu_mr
);
1991 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1993 uint8_t mask
= 1 << client
;
1994 uint8_t old_logging
;
1996 assert(client
== DIRTY_MEMORY_VGA
);
1997 old_logging
= mr
->vga_logging_count
;
1998 mr
->vga_logging_count
+= log
? 1 : -1;
1999 if (!!old_logging
== !!mr
->vga_logging_count
) {
2003 memory_region_transaction_begin();
2004 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2005 memory_region_update_pending
|= mr
->enabled
;
2006 memory_region_transaction_commit();
2009 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2012 assert(mr
->ram_block
);
2013 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2015 memory_region_get_dirty_log_mask(mr
));
2018 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2020 MemoryListener
*listener
;
2025 /* If the same address space has multiple log_sync listeners, we
2026 * visit that address space's FlatView multiple times. But because
2027 * log_sync listeners are rare, it's still cheaper than walking each
2028 * address space once.
2030 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2031 if (!listener
->log_sync
) {
2034 as
= listener
->address_space
;
2035 view
= address_space_get_flatview(as
);
2036 FOR_EACH_FLAT_RANGE(fr
, view
) {
2037 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2038 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2039 listener
->log_sync(listener
, &mrs
);
2042 flatview_unref(view
);
2046 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2049 MemoryRegionSection mrs
;
2050 MemoryListener
*listener
;
2054 hwaddr sec_start
, sec_end
, sec_size
;
2056 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2057 if (!listener
->log_clear
) {
2060 as
= listener
->address_space
;
2061 view
= address_space_get_flatview(as
);
2062 FOR_EACH_FLAT_RANGE(fr
, view
) {
2063 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2065 * Clear dirty bitmap operation only applies to those
2066 * regions whose dirty logging is at least enabled
2071 mrs
= section_from_flat_range(fr
, view
);
2073 sec_start
= MAX(mrs
.offset_within_region
, start
);
2074 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2075 sec_end
= MIN(sec_end
, start
+ len
);
2077 if (sec_start
>= sec_end
) {
2079 * If this memory region section has no intersection
2080 * with the requested range, skip.
2085 /* Valid case; shrink the section if needed */
2086 mrs
.offset_within_address_space
+=
2087 sec_start
- mrs
.offset_within_region
;
2088 mrs
.offset_within_region
= sec_start
;
2089 sec_size
= sec_end
- sec_start
;
2090 mrs
.size
= int128_make64(sec_size
);
2091 listener
->log_clear(listener
, &mrs
);
2093 flatview_unref(view
);
2097 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2102 DirtyBitmapSnapshot
*snapshot
;
2103 assert(mr
->ram_block
);
2104 memory_region_sync_dirty_bitmap(mr
);
2105 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2106 memory_global_after_dirty_log_sync();
2110 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2111 hwaddr addr
, hwaddr size
)
2113 assert(mr
->ram_block
);
2114 return cpu_physical_memory_snapshot_get_dirty(snap
,
2115 memory_region_get_ram_addr(mr
) + addr
, size
);
2118 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2120 if (mr
->readonly
!= readonly
) {
2121 memory_region_transaction_begin();
2122 mr
->readonly
= readonly
;
2123 memory_region_update_pending
|= mr
->enabled
;
2124 memory_region_transaction_commit();
2128 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2130 if (mr
->nonvolatile
!= nonvolatile
) {
2131 memory_region_transaction_begin();
2132 mr
->nonvolatile
= nonvolatile
;
2133 memory_region_update_pending
|= mr
->enabled
;
2134 memory_region_transaction_commit();
2138 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2140 if (mr
->romd_mode
!= romd_mode
) {
2141 memory_region_transaction_begin();
2142 mr
->romd_mode
= romd_mode
;
2143 memory_region_update_pending
|= mr
->enabled
;
2144 memory_region_transaction_commit();
2148 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2149 hwaddr size
, unsigned client
)
2151 assert(mr
->ram_block
);
2152 cpu_physical_memory_test_and_clear_dirty(
2153 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2156 int memory_region_get_fd(MemoryRegion
*mr
)
2164 fd
= mr
->ram_block
->fd
;
2170 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2173 uint64_t offset
= 0;
2177 offset
+= mr
->alias_offset
;
2180 assert(mr
->ram_block
);
2181 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2187 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2191 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2199 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2201 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2204 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2206 assert(mr
->ram_block
);
2208 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2212 * Call proper memory listeners about the change on the newly
2213 * added/removed CoalescedMemoryRange.
2215 static void memory_region_update_coalesced_range(MemoryRegion
*mr
,
2216 CoalescedMemoryRange
*cmr
,
2223 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2224 view
= address_space_get_flatview(as
);
2225 FOR_EACH_FLAT_RANGE(fr
, view
) {
2227 flat_range_coalesced_io_notify(fr
, as
, cmr
, add
);
2230 flatview_unref(view
);
2234 void memory_region_set_coalescing(MemoryRegion
*mr
)
2236 memory_region_clear_coalescing(mr
);
2237 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2240 void memory_region_add_coalescing(MemoryRegion
*mr
,
2244 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2246 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2247 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2248 memory_region_update_coalesced_range(mr
, cmr
, true);
2249 memory_region_set_flush_coalesced(mr
);
2252 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2254 CoalescedMemoryRange
*cmr
;
2256 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2260 qemu_flush_coalesced_mmio_buffer();
2261 mr
->flush_coalesced_mmio
= false;
2263 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2264 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2265 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2266 memory_region_update_coalesced_range(mr
, cmr
, false);
2271 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2273 mr
->flush_coalesced_mmio
= true;
2276 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2278 qemu_flush_coalesced_mmio_buffer();
2279 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2280 mr
->flush_coalesced_mmio
= false;
2284 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2286 mr
->global_locking
= false;
2289 static bool userspace_eventfd_warning
;
2291 void memory_region_add_eventfd(MemoryRegion
*mr
,
2298 MemoryRegionIoeventfd mrfd
= {
2299 .addr
.start
= int128_make64(addr
),
2300 .addr
.size
= int128_make64(size
),
2301 .match_data
= match_data
,
2307 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2308 userspace_eventfd_warning
))) {
2309 userspace_eventfd_warning
= true;
2310 error_report("Using eventfd without MMIO binding in KVM. "
2311 "Suboptimal performance expected");
2315 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2317 memory_region_transaction_begin();
2318 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2319 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2324 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2325 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2326 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2327 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2328 mr
->ioeventfds
[i
] = mrfd
;
2329 ioeventfd_update_pending
|= mr
->enabled
;
2330 memory_region_transaction_commit();
2333 void memory_region_del_eventfd(MemoryRegion
*mr
,
2340 MemoryRegionIoeventfd mrfd
= {
2341 .addr
.start
= int128_make64(addr
),
2342 .addr
.size
= int128_make64(size
),
2343 .match_data
= match_data
,
2350 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2352 memory_region_transaction_begin();
2353 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2354 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2358 assert(i
!= mr
->ioeventfd_nb
);
2359 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2360 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2362 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2363 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2364 ioeventfd_update_pending
|= mr
->enabled
;
2365 memory_region_transaction_commit();
2368 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2370 MemoryRegion
*mr
= subregion
->container
;
2371 MemoryRegion
*other
;
2373 memory_region_transaction_begin();
2375 memory_region_ref(subregion
);
2376 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2377 if (subregion
->priority
>= other
->priority
) {
2378 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2382 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2384 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2385 memory_region_transaction_commit();
2388 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2390 MemoryRegion
*subregion
)
2392 assert(!subregion
->container
);
2393 subregion
->container
= mr
;
2394 subregion
->addr
= offset
;
2395 memory_region_update_container_subregions(subregion
);
2398 void memory_region_add_subregion(MemoryRegion
*mr
,
2400 MemoryRegion
*subregion
)
2402 subregion
->priority
= 0;
2403 memory_region_add_subregion_common(mr
, offset
, subregion
);
2406 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2408 MemoryRegion
*subregion
,
2411 subregion
->priority
= priority
;
2412 memory_region_add_subregion_common(mr
, offset
, subregion
);
2415 void memory_region_del_subregion(MemoryRegion
*mr
,
2416 MemoryRegion
*subregion
)
2418 memory_region_transaction_begin();
2419 assert(subregion
->container
== mr
);
2420 subregion
->container
= NULL
;
2421 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2422 memory_region_unref(subregion
);
2423 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2424 memory_region_transaction_commit();
2427 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2429 if (enabled
== mr
->enabled
) {
2432 memory_region_transaction_begin();
2433 mr
->enabled
= enabled
;
2434 memory_region_update_pending
= true;
2435 memory_region_transaction_commit();
2438 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2440 Int128 s
= int128_make64(size
);
2442 if (size
== UINT64_MAX
) {
2445 if (int128_eq(s
, mr
->size
)) {
2448 memory_region_transaction_begin();
2450 memory_region_update_pending
= true;
2451 memory_region_transaction_commit();
2454 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2456 MemoryRegion
*container
= mr
->container
;
2459 memory_region_transaction_begin();
2460 memory_region_ref(mr
);
2461 memory_region_del_subregion(container
, mr
);
2462 mr
->container
= container
;
2463 memory_region_update_container_subregions(mr
);
2464 memory_region_unref(mr
);
2465 memory_region_transaction_commit();
2469 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2471 if (addr
!= mr
->addr
) {
2473 memory_region_readd_subregion(mr
);
2477 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2481 if (offset
== mr
->alias_offset
) {
2485 memory_region_transaction_begin();
2486 mr
->alias_offset
= offset
;
2487 memory_region_update_pending
|= mr
->enabled
;
2488 memory_region_transaction_commit();
2491 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2496 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2498 const AddrRange
*addr
= addr_
;
2499 const FlatRange
*fr
= fr_
;
2501 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2503 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2509 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2511 return bsearch(&addr
, view
->ranges
, view
->nr
,
2512 sizeof(FlatRange
), cmp_flatrange_addr
);
2515 bool memory_region_is_mapped(MemoryRegion
*mr
)
2517 return mr
->container
? true : false;
2520 /* Same as memory_region_find, but it does not add a reference to the
2521 * returned region. It must be called from an RCU critical section.
2523 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2524 hwaddr addr
, uint64_t size
)
2526 MemoryRegionSection ret
= { .mr
= NULL
};
2534 for (root
= mr
; root
->container
; ) {
2535 root
= root
->container
;
2539 as
= memory_region_to_address_space(root
);
2543 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2545 view
= address_space_to_flatview(as
);
2546 fr
= flatview_lookup(view
, range
);
2551 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2557 range
= addrrange_intersection(range
, fr
->addr
);
2558 ret
.offset_within_region
= fr
->offset_in_region
;
2559 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2561 ret
.size
= range
.size
;
2562 ret
.offset_within_address_space
= int128_get64(range
.start
);
2563 ret
.readonly
= fr
->readonly
;
2564 ret
.nonvolatile
= fr
->nonvolatile
;
2568 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2569 hwaddr addr
, uint64_t size
)
2571 MemoryRegionSection ret
;
2573 ret
= memory_region_find_rcu(mr
, addr
, size
);
2575 memory_region_ref(ret
.mr
);
2581 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2586 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2588 return mr
&& mr
!= container
;
2591 void memory_global_dirty_log_sync(void)
2593 memory_region_sync_dirty_bitmap(NULL
);
2596 void memory_global_after_dirty_log_sync(void)
2598 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2601 static VMChangeStateEntry
*vmstate_change
;
2603 void memory_global_dirty_log_start(void)
2605 if (vmstate_change
) {
2606 qemu_del_vm_change_state_handler(vmstate_change
);
2607 vmstate_change
= NULL
;
2610 global_dirty_log
= true;
2612 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2614 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2615 memory_region_transaction_begin();
2616 memory_region_update_pending
= true;
2617 memory_region_transaction_commit();
2620 static void memory_global_dirty_log_do_stop(void)
2622 global_dirty_log
= false;
2624 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2625 memory_region_transaction_begin();
2626 memory_region_update_pending
= true;
2627 memory_region_transaction_commit();
2629 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2632 static void memory_vm_change_state_handler(void *opaque
, int running
,
2636 memory_global_dirty_log_do_stop();
2638 if (vmstate_change
) {
2639 qemu_del_vm_change_state_handler(vmstate_change
);
2640 vmstate_change
= NULL
;
2645 void memory_global_dirty_log_stop(void)
2647 if (!runstate_is_running()) {
2648 if (vmstate_change
) {
2651 vmstate_change
= qemu_add_vm_change_state_handler(
2652 memory_vm_change_state_handler
, NULL
);
2656 memory_global_dirty_log_do_stop();
2659 static void listener_add_address_space(MemoryListener
*listener
,
2665 if (listener
->begin
) {
2666 listener
->begin(listener
);
2668 if (global_dirty_log
) {
2669 if (listener
->log_global_start
) {
2670 listener
->log_global_start(listener
);
2674 view
= address_space_get_flatview(as
);
2675 FOR_EACH_FLAT_RANGE(fr
, view
) {
2676 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2678 if (listener
->region_add
) {
2679 listener
->region_add(listener
, §ion
);
2681 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2682 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2685 if (listener
->commit
) {
2686 listener
->commit(listener
);
2688 flatview_unref(view
);
2691 static void listener_del_address_space(MemoryListener
*listener
,
2697 if (listener
->begin
) {
2698 listener
->begin(listener
);
2700 view
= address_space_get_flatview(as
);
2701 FOR_EACH_FLAT_RANGE(fr
, view
) {
2702 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2704 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2705 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2707 if (listener
->region_del
) {
2708 listener
->region_del(listener
, §ion
);
2711 if (listener
->commit
) {
2712 listener
->commit(listener
);
2714 flatview_unref(view
);
2717 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2719 MemoryListener
*other
= NULL
;
2721 listener
->address_space
= as
;
2722 if (QTAILQ_EMPTY(&memory_listeners
)
2723 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2724 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2726 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2727 if (listener
->priority
< other
->priority
) {
2731 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2734 if (QTAILQ_EMPTY(&as
->listeners
)
2735 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2736 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2738 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2739 if (listener
->priority
< other
->priority
) {
2743 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2746 listener_add_address_space(listener
, as
);
2749 void memory_listener_unregister(MemoryListener
*listener
)
2751 if (!listener
->address_space
) {
2755 listener_del_address_space(listener
, listener
->address_space
);
2756 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2757 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2758 listener
->address_space
= NULL
;
2761 void address_space_remove_listeners(AddressSpace
*as
)
2763 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2764 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2768 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2770 memory_region_ref(root
);
2772 as
->current_map
= NULL
;
2773 as
->ioeventfd_nb
= 0;
2774 as
->ioeventfds
= NULL
;
2775 QTAILQ_INIT(&as
->listeners
);
2776 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2777 as
->name
= g_strdup(name
? name
: "anonymous");
2778 address_space_update_topology(as
);
2779 address_space_update_ioeventfds(as
);
2782 static void do_address_space_destroy(AddressSpace
*as
)
2784 assert(QTAILQ_EMPTY(&as
->listeners
));
2786 flatview_unref(as
->current_map
);
2788 g_free(as
->ioeventfds
);
2789 memory_region_unref(as
->root
);
2792 void address_space_destroy(AddressSpace
*as
)
2794 MemoryRegion
*root
= as
->root
;
2796 /* Flush out anything from MemoryListeners listening in on this */
2797 memory_region_transaction_begin();
2799 memory_region_transaction_commit();
2800 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2802 /* At this point, as->dispatch and as->current_map are dummy
2803 * entries that the guest should never use. Wait for the old
2804 * values to expire before freeing the data.
2807 call_rcu(as
, do_address_space_destroy
, rcu
);
2810 static const char *memory_region_type(MemoryRegion
*mr
)
2812 if (memory_region_is_ram_device(mr
)) {
2814 } else if (memory_region_is_romd(mr
)) {
2816 } else if (memory_region_is_rom(mr
)) {
2818 } else if (memory_region_is_ram(mr
)) {
2825 typedef struct MemoryRegionList MemoryRegionList
;
2827 struct MemoryRegionList
{
2828 const MemoryRegion
*mr
;
2829 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2832 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2834 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2835 int128_sub((size), int128_one())) : 0)
2836 #define MTREE_INDENT " "
2838 static void mtree_expand_owner(const char *label
, Object
*obj
)
2840 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
2842 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
2843 if (dev
&& dev
->id
) {
2844 qemu_printf(" id=%s", dev
->id
);
2846 gchar
*canonical_path
= object_get_canonical_path(obj
);
2847 if (canonical_path
) {
2848 qemu_printf(" path=%s", canonical_path
);
2849 g_free(canonical_path
);
2851 qemu_printf(" type=%s", object_get_typename(obj
));
2857 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
2859 Object
*owner
= mr
->owner
;
2860 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
2862 if (!owner
&& !parent
) {
2863 qemu_printf(" orphan");
2867 mtree_expand_owner("owner", owner
);
2869 if (parent
&& parent
!= owner
) {
2870 mtree_expand_owner("parent", parent
);
2874 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
2876 MemoryRegionListHead
*alias_print_queue
,
2879 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2880 MemoryRegionListHead submr_print_queue
;
2881 const MemoryRegion
*submr
;
2883 hwaddr cur_start
, cur_end
;
2889 for (i
= 0; i
< level
; i
++) {
2890 qemu_printf(MTREE_INDENT
);
2893 cur_start
= base
+ mr
->addr
;
2894 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2897 * Try to detect overflow of memory region. This should never
2898 * happen normally. When it happens, we dump something to warn the
2899 * user who is observing this.
2901 if (cur_start
< base
|| cur_end
< cur_start
) {
2902 qemu_printf("[DETECTED OVERFLOW!] ");
2906 MemoryRegionList
*ml
;
2909 /* check if the alias is already in the queue */
2910 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2911 if (ml
->mr
== mr
->alias
) {
2917 ml
= g_new(MemoryRegionList
, 1);
2919 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2921 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2922 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2923 "-" TARGET_FMT_plx
"%s",
2926 mr
->nonvolatile
? "nv-" : "",
2927 memory_region_type((MemoryRegion
*)mr
),
2928 memory_region_name(mr
),
2929 memory_region_name(mr
->alias
),
2931 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2932 mr
->enabled
? "" : " [disabled]");
2934 mtree_print_mr_owner(mr
);
2937 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2938 " (prio %d, %s%s): %s%s",
2941 mr
->nonvolatile
? "nv-" : "",
2942 memory_region_type((MemoryRegion
*)mr
),
2943 memory_region_name(mr
),
2944 mr
->enabled
? "" : " [disabled]");
2946 mtree_print_mr_owner(mr
);
2951 QTAILQ_INIT(&submr_print_queue
);
2953 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2954 new_ml
= g_new(MemoryRegionList
, 1);
2956 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2957 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2958 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2959 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2960 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
2966 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
2970 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2971 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
2972 alias_print_queue
, owner
);
2975 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
2980 struct FlatViewInfo
{
2985 const char *ac_name
;
2988 static void mtree_print_flatview(gpointer key
, gpointer value
,
2991 FlatView
*view
= key
;
2992 GArray
*fv_address_spaces
= value
;
2993 struct FlatViewInfo
*fvi
= user_data
;
2994 FlatRange
*range
= &view
->ranges
[0];
3000 qemu_printf("FlatView #%d\n", fvi
->counter
);
3003 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3004 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3005 qemu_printf(" AS \"%s\", root: %s",
3006 as
->name
, memory_region_name(as
->root
));
3007 if (as
->root
->alias
) {
3008 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3013 qemu_printf(" Root memory region: %s\n",
3014 view
->root
? memory_region_name(view
->root
) : "(none)");
3017 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3023 if (range
->offset_in_region
) {
3024 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3025 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3026 int128_get64(range
->addr
.start
),
3027 int128_get64(range
->addr
.start
)
3028 + MR_SIZE(range
->addr
.size
),
3030 range
->nonvolatile
? "nv-" : "",
3031 range
->readonly
? "rom" : memory_region_type(mr
),
3032 memory_region_name(mr
),
3033 range
->offset_in_region
);
3035 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3036 " (prio %d, %s%s): %s",
3037 int128_get64(range
->addr
.start
),
3038 int128_get64(range
->addr
.start
)
3039 + MR_SIZE(range
->addr
.size
),
3041 range
->nonvolatile
? "nv-" : "",
3042 range
->readonly
? "rom" : memory_region_type(mr
),
3043 memory_region_name(mr
));
3046 mtree_print_mr_owner(mr
);
3050 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3051 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3052 if (fvi
->ac
->has_memory(current_machine
, as
,
3053 int128_get64(range
->addr
.start
),
3054 MR_SIZE(range
->addr
.size
) + 1)) {
3055 qemu_printf(" %s", fvi
->ac_name
);
3063 #if !defined(CONFIG_USER_ONLY)
3064 if (fvi
->dispatch_tree
&& view
->root
) {
3065 mtree_print_dispatch(view
->dispatch
, view
->root
);
3072 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3075 FlatView
*view
= key
;
3076 GArray
*fv_address_spaces
= value
;
3078 g_array_unref(fv_address_spaces
);
3079 flatview_unref(view
);
3084 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
)
3086 MemoryRegionListHead ml_head
;
3087 MemoryRegionList
*ml
, *ml2
;
3092 struct FlatViewInfo fvi
= {
3094 .dispatch_tree
= dispatch_tree
,
3097 GArray
*fv_address_spaces
;
3098 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3099 AccelClass
*ac
= ACCEL_GET_CLASS(current_machine
->accelerator
);
3101 if (ac
->has_memory
) {
3103 fvi
.ac_name
= current_machine
->accel
? current_machine
->accel
:
3104 object_class_get_name(OBJECT_CLASS(ac
));
3107 /* Gather all FVs in one table */
3108 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3109 view
= address_space_get_flatview(as
);
3111 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3112 if (!fv_address_spaces
) {
3113 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3114 g_hash_table_insert(views
, view
, fv_address_spaces
);
3117 g_array_append_val(fv_address_spaces
, as
);
3121 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3124 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3125 g_hash_table_unref(views
);
3130 QTAILQ_INIT(&ml_head
);
3132 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3133 qemu_printf("address-space: %s\n", as
->name
);
3134 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
);
3138 /* print aliased regions */
3139 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3140 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3141 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
);
3145 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3150 void memory_region_init_ram(MemoryRegion
*mr
,
3151 struct Object
*owner
,
3156 DeviceState
*owner_dev
;
3159 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3161 error_propagate(errp
, err
);
3164 /* This will assert if owner is neither NULL nor a DeviceState.
3165 * We only want the owner here for the purposes of defining a
3166 * unique name for migration. TODO: Ideally we should implement
3167 * a naming scheme for Objects which are not DeviceStates, in
3168 * which case we can relax this restriction.
3170 owner_dev
= DEVICE(owner
);
3171 vmstate_register_ram(mr
, owner_dev
);
3174 void memory_region_init_rom(MemoryRegion
*mr
,
3175 struct Object
*owner
,
3180 DeviceState
*owner_dev
;
3183 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3185 error_propagate(errp
, err
);
3188 /* This will assert if owner is neither NULL nor a DeviceState.
3189 * We only want the owner here for the purposes of defining a
3190 * unique name for migration. TODO: Ideally we should implement
3191 * a naming scheme for Objects which are not DeviceStates, in
3192 * which case we can relax this restriction.
3194 owner_dev
= DEVICE(owner
);
3195 vmstate_register_ram(mr
, owner_dev
);
3198 void memory_region_init_rom_device(MemoryRegion
*mr
,
3199 struct Object
*owner
,
3200 const MemoryRegionOps
*ops
,
3206 DeviceState
*owner_dev
;
3209 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3212 error_propagate(errp
, err
);
3215 /* This will assert if owner is neither NULL nor a DeviceState.
3216 * We only want the owner here for the purposes of defining a
3217 * unique name for migration. TODO: Ideally we should implement
3218 * a naming scheme for Objects which are not DeviceStates, in
3219 * which case we can relax this restriction.
3221 owner_dev
= DEVICE(owner
);
3222 vmstate_register_ram(mr
, owner_dev
);
3225 static const TypeInfo memory_region_info
= {
3226 .parent
= TYPE_OBJECT
,
3227 .name
= TYPE_MEMORY_REGION
,
3228 .class_size
= sizeof(MemoryRegionClass
),
3229 .instance_size
= sizeof(MemoryRegion
),
3230 .instance_init
= memory_region_initfn
,
3231 .instance_finalize
= memory_region_finalize
,
3234 static const TypeInfo iommu_memory_region_info
= {
3235 .parent
= TYPE_MEMORY_REGION
,
3236 .name
= TYPE_IOMMU_MEMORY_REGION
,
3237 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3238 .instance_size
= sizeof(IOMMUMemoryRegion
),
3239 .instance_init
= iommu_memory_region_initfn
,
3243 static void memory_register_types(void)
3245 type_register_static(&memory_region_info
);
3246 type_register_static(&iommu_memory_region_info
);
3249 type_init(memory_register_types
)