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 bool memory_region_wrong_endianness(MemoryRegion
*mr
)
356 #ifdef TARGET_WORDS_BIGENDIAN
357 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
359 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
363 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
365 if (memory_region_wrong_endianness(mr
)) {
370 *data
= bswap16(*data
);
373 *data
= bswap32(*data
);
376 *data
= bswap64(*data
);
384 static inline void memory_region_shift_read_access(uint64_t *value
,
390 *value
|= (tmp
& mask
) << shift
;
392 *value
|= (tmp
& mask
) >> -shift
;
396 static inline uint64_t memory_region_shift_write_access(uint64_t *value
,
403 tmp
= (*value
>> shift
) & mask
;
405 tmp
= (*value
<< -shift
) & mask
;
411 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
414 hwaddr abs_addr
= offset
;
416 abs_addr
+= mr
->addr
;
417 for (root
= mr
; root
->container
; ) {
418 root
= root
->container
;
419 abs_addr
+= root
->addr
;
425 static int get_cpu_index(void)
428 return current_cpu
->cpu_index
;
433 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
443 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
445 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
446 } else if (mr
== &io_mem_notdirty
) {
447 /* Accesses to code which has previously been translated into a TB show
448 * up in the MMIO path, as accesses to the io_mem_notdirty
450 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
451 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
452 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
453 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
455 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
459 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
470 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
472 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
473 } else if (mr
== &io_mem_notdirty
) {
474 /* Accesses to code which has previously been translated into a TB show
475 * up in the MMIO path, as accesses to the io_mem_notdirty
477 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
478 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
479 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
480 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
482 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
486 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
494 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
497 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
498 } else if (mr
== &io_mem_notdirty
) {
499 /* Accesses to code which has previously been translated into a TB show
500 * up in the MMIO path, as accesses to the io_mem_notdirty
502 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
503 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
504 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
505 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
507 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
511 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
519 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
522 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
523 } else if (mr
== &io_mem_notdirty
) {
524 /* Accesses to code which has previously been translated into a TB show
525 * up in the MMIO path, as accesses to the io_mem_notdirty
527 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
528 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
529 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
530 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
532 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
535 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
538 unsigned access_size_min
,
539 unsigned access_size_max
,
540 MemTxResult (*access_fn
)
551 uint64_t access_mask
;
552 unsigned access_size
;
554 MemTxResult r
= MEMTX_OK
;
556 if (!access_size_min
) {
559 if (!access_size_max
) {
563 /* FIXME: support unaligned access? */
564 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
565 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
566 if (memory_region_big_endian(mr
)) {
567 for (i
= 0; i
< size
; i
+= access_size
) {
568 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
569 (size
- access_size
- i
) * 8, access_mask
, attrs
);
572 for (i
= 0; i
< size
; i
+= access_size
) {
573 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
580 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
584 while (mr
->container
) {
587 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
588 if (mr
== as
->root
) {
595 /* Render a memory region into the global view. Ranges in @view obscure
598 static void render_memory_region(FlatView
*view
,
605 MemoryRegion
*subregion
;
607 hwaddr offset_in_region
;
617 int128_addto(&base
, int128_make64(mr
->addr
));
618 readonly
|= mr
->readonly
;
619 nonvolatile
|= mr
->nonvolatile
;
621 tmp
= addrrange_make(base
, mr
->size
);
623 if (!addrrange_intersects(tmp
, clip
)) {
627 clip
= addrrange_intersection(tmp
, clip
);
630 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
631 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
632 render_memory_region(view
, mr
->alias
, base
, clip
,
633 readonly
, nonvolatile
);
637 /* Render subregions in priority order. */
638 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
639 render_memory_region(view
, subregion
, base
, clip
,
640 readonly
, nonvolatile
);
643 if (!mr
->terminates
) {
647 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
652 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
653 fr
.romd_mode
= mr
->romd_mode
;
654 fr
.readonly
= readonly
;
655 fr
.nonvolatile
= nonvolatile
;
657 /* Render the region itself into any gaps left by the current view. */
658 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
659 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
662 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
663 now
= int128_min(remain
,
664 int128_sub(view
->ranges
[i
].addr
.start
, base
));
665 fr
.offset_in_region
= offset_in_region
;
666 fr
.addr
= addrrange_make(base
, now
);
667 flatview_insert(view
, i
, &fr
);
669 int128_addto(&base
, now
);
670 offset_in_region
+= int128_get64(now
);
671 int128_subfrom(&remain
, now
);
673 now
= int128_sub(int128_min(int128_add(base
, remain
),
674 addrrange_end(view
->ranges
[i
].addr
)),
676 int128_addto(&base
, now
);
677 offset_in_region
+= int128_get64(now
);
678 int128_subfrom(&remain
, now
);
680 if (int128_nz(remain
)) {
681 fr
.offset_in_region
= offset_in_region
;
682 fr
.addr
= addrrange_make(base
, remain
);
683 flatview_insert(view
, i
, &fr
);
687 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
689 while (mr
->enabled
) {
691 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
692 /* The alias is included in its entirety. Use it as
693 * the "real" root, so that we can share more FlatViews.
698 } else if (!mr
->terminates
) {
699 unsigned int found
= 0;
700 MemoryRegion
*child
, *next
= NULL
;
701 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
702 if (child
->enabled
) {
707 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
708 /* A child is included in its entirety. If it's the only
709 * enabled one, use it in the hope of finding an alias down the
710 * way. This will also let us share FlatViews.
731 /* Render a memory topology into a list of disjoint absolute ranges. */
732 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
737 view
= flatview_new(mr
);
740 render_memory_region(view
, mr
, int128_zero(),
741 addrrange_make(int128_zero(), int128_2_64()),
744 flatview_simplify(view
);
746 view
->dispatch
= address_space_dispatch_new(view
);
747 for (i
= 0; i
< view
->nr
; i
++) {
748 MemoryRegionSection mrs
=
749 section_from_flat_range(&view
->ranges
[i
], view
);
750 flatview_add_to_dispatch(view
, &mrs
);
752 address_space_dispatch_compact(view
->dispatch
);
753 g_hash_table_replace(flat_views
, mr
, view
);
758 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
759 MemoryRegionIoeventfd
*fds_new
,
761 MemoryRegionIoeventfd
*fds_old
,
765 MemoryRegionIoeventfd
*fd
;
766 MemoryRegionSection section
;
768 /* Generate a symmetric difference of the old and new fd sets, adding
769 * and deleting as necessary.
773 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
774 if (iold
< fds_old_nb
775 && (inew
== fds_new_nb
776 || memory_region_ioeventfd_before(&fds_old
[iold
],
779 section
= (MemoryRegionSection
) {
780 .fv
= address_space_to_flatview(as
),
781 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
782 .size
= fd
->addr
.size
,
784 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
785 fd
->match_data
, fd
->data
, fd
->e
);
787 } else if (inew
< fds_new_nb
788 && (iold
== fds_old_nb
789 || memory_region_ioeventfd_before(&fds_new
[inew
],
792 section
= (MemoryRegionSection
) {
793 .fv
= address_space_to_flatview(as
),
794 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
795 .size
= fd
->addr
.size
,
797 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
798 fd
->match_data
, fd
->data
, fd
->e
);
807 FlatView
*address_space_get_flatview(AddressSpace
*as
)
813 view
= address_space_to_flatview(as
);
814 /* If somebody has replaced as->current_map concurrently,
815 * flatview_ref returns false.
817 } while (!flatview_ref(view
));
822 static void address_space_update_ioeventfds(AddressSpace
*as
)
826 unsigned ioeventfd_nb
= 0;
827 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
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 ioeventfds
= g_realloc(ioeventfds
,
840 ioeventfd_nb
* sizeof(*ioeventfds
));
841 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
842 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
847 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
848 as
->ioeventfds
, as
->ioeventfd_nb
);
850 g_free(as
->ioeventfds
);
851 as
->ioeventfds
= ioeventfds
;
852 as
->ioeventfd_nb
= ioeventfd_nb
;
853 flatview_unref(view
);
857 * Notify the memory listeners about the coalesced IO change events of
858 * range `cmr'. Only the part that has intersection of the specified
859 * FlatRange will be sent.
861 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
862 CoalescedMemoryRange
*cmr
, bool add
)
866 tmp
= addrrange_shift(cmr
->addr
,
867 int128_sub(fr
->addr
.start
,
868 int128_make64(fr
->offset_in_region
)));
869 if (!addrrange_intersects(tmp
, fr
->addr
)) {
872 tmp
= addrrange_intersection(tmp
, fr
->addr
);
875 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
876 int128_get64(tmp
.start
),
877 int128_get64(tmp
.size
));
879 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
880 int128_get64(tmp
.start
),
881 int128_get64(tmp
.size
));
885 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
887 CoalescedMemoryRange
*cmr
;
889 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
890 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
894 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
896 MemoryRegion
*mr
= fr
->mr
;
897 CoalescedMemoryRange
*cmr
;
899 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
903 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
904 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
908 static void address_space_update_topology_pass(AddressSpace
*as
,
909 const FlatView
*old_view
,
910 const FlatView
*new_view
,
914 FlatRange
*frold
, *frnew
;
916 /* Generate a symmetric difference of the old and new memory maps.
917 * Kill ranges in the old map, and instantiate ranges in the new map.
920 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
921 if (iold
< old_view
->nr
) {
922 frold
= &old_view
->ranges
[iold
];
926 if (inew
< new_view
->nr
) {
927 frnew
= &new_view
->ranges
[inew
];
934 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
935 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
936 && !flatrange_equal(frold
, frnew
)))) {
937 /* In old but not in new, or in both but attributes changed. */
940 flat_range_coalesced_io_del(frold
, as
);
941 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
945 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
946 /* In both and unchanged (except logging may have changed) */
949 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
950 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
951 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
952 frold
->dirty_log_mask
,
953 frnew
->dirty_log_mask
);
955 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
956 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
957 frold
->dirty_log_mask
,
958 frnew
->dirty_log_mask
);
968 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
969 flat_range_coalesced_io_add(frnew
, as
);
977 static void flatviews_init(void)
979 static FlatView
*empty_view
;
985 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
986 (GDestroyNotify
) flatview_unref
);
988 empty_view
= generate_memory_topology(NULL
);
989 /* We keep it alive forever in the global variable. */
990 flatview_ref(empty_view
);
992 g_hash_table_replace(flat_views
, NULL
, empty_view
);
993 flatview_ref(empty_view
);
997 static void flatviews_reset(void)
1002 g_hash_table_unref(flat_views
);
1007 /* Render unique FVs */
1008 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1009 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1011 if (g_hash_table_lookup(flat_views
, physmr
)) {
1015 generate_memory_topology(physmr
);
1019 static void address_space_set_flatview(AddressSpace
*as
)
1021 FlatView
*old_view
= address_space_to_flatview(as
);
1022 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1023 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1027 if (old_view
== new_view
) {
1032 flatview_ref(old_view
);
1035 flatview_ref(new_view
);
1037 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1038 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1041 old_view2
= &tmpview
;
1043 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1044 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1047 /* Writes are protected by the BQL. */
1048 atomic_rcu_set(&as
->current_map
, new_view
);
1050 flatview_unref(old_view
);
1053 /* Note that all the old MemoryRegions are still alive up to this
1054 * point. This relieves most MemoryListeners from the need to
1055 * ref/unref the MemoryRegions they get---unless they use them
1056 * outside the iothread mutex, in which case precise reference
1057 * counting is necessary.
1060 flatview_unref(old_view
);
1064 static void address_space_update_topology(AddressSpace
*as
)
1066 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1069 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1070 generate_memory_topology(physmr
);
1072 address_space_set_flatview(as
);
1075 void memory_region_transaction_begin(void)
1077 qemu_flush_coalesced_mmio_buffer();
1078 ++memory_region_transaction_depth
;
1081 void memory_region_transaction_commit(void)
1085 assert(memory_region_transaction_depth
);
1086 assert(qemu_mutex_iothread_locked());
1088 --memory_region_transaction_depth
;
1089 if (!memory_region_transaction_depth
) {
1090 if (memory_region_update_pending
) {
1093 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1095 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1096 address_space_set_flatview(as
);
1097 address_space_update_ioeventfds(as
);
1099 memory_region_update_pending
= false;
1100 ioeventfd_update_pending
= false;
1101 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1102 } else if (ioeventfd_update_pending
) {
1103 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1104 address_space_update_ioeventfds(as
);
1106 ioeventfd_update_pending
= false;
1111 static void memory_region_destructor_none(MemoryRegion
*mr
)
1115 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1117 qemu_ram_free(mr
->ram_block
);
1120 static bool memory_region_need_escape(char c
)
1122 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1125 static char *memory_region_escape_name(const char *name
)
1132 for (p
= name
; *p
; p
++) {
1133 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1135 if (bytes
== p
- name
) {
1136 return g_memdup(name
, bytes
+ 1);
1139 escaped
= g_malloc(bytes
+ 1);
1140 for (p
= name
, q
= escaped
; *p
; p
++) {
1142 if (unlikely(memory_region_need_escape(c
))) {
1145 *q
++ = "0123456789abcdef"[c
>> 4];
1146 c
= "0123456789abcdef"[c
& 15];
1154 static void memory_region_do_init(MemoryRegion
*mr
,
1159 mr
->size
= int128_make64(size
);
1160 if (size
== UINT64_MAX
) {
1161 mr
->size
= int128_2_64();
1163 mr
->name
= g_strdup(name
);
1165 mr
->ram_block
= NULL
;
1168 char *escaped_name
= memory_region_escape_name(name
);
1169 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1172 owner
= container_get(qdev_get_machine(), "/unattached");
1175 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1176 object_unref(OBJECT(mr
));
1178 g_free(escaped_name
);
1182 void memory_region_init(MemoryRegion
*mr
,
1187 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1188 memory_region_do_init(mr
, owner
, name
, size
);
1191 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1192 void *opaque
, Error
**errp
)
1194 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1195 uint64_t value
= mr
->addr
;
1197 visit_type_uint64(v
, name
, &value
, errp
);
1200 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1201 const char *name
, void *opaque
,
1204 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1205 gchar
*path
= (gchar
*)"";
1207 if (mr
->container
) {
1208 path
= object_get_canonical_path(OBJECT(mr
->container
));
1210 visit_type_str(v
, name
, &path
, errp
);
1211 if (mr
->container
) {
1216 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1219 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1221 return OBJECT(mr
->container
);
1224 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1225 const char *name
, void *opaque
,
1228 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1229 int32_t value
= mr
->priority
;
1231 visit_type_int32(v
, name
, &value
, errp
);
1234 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1235 void *opaque
, Error
**errp
)
1237 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1238 uint64_t value
= memory_region_size(mr
);
1240 visit_type_uint64(v
, name
, &value
, errp
);
1243 static void memory_region_initfn(Object
*obj
)
1245 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1248 mr
->ops
= &unassigned_mem_ops
;
1250 mr
->romd_mode
= true;
1251 mr
->global_locking
= true;
1252 mr
->destructor
= memory_region_destructor_none
;
1253 QTAILQ_INIT(&mr
->subregions
);
1254 QTAILQ_INIT(&mr
->coalesced
);
1256 op
= object_property_add(OBJECT(mr
), "container",
1257 "link<" TYPE_MEMORY_REGION
">",
1258 memory_region_get_container
,
1259 NULL
, /* memory_region_set_container */
1260 NULL
, NULL
, &error_abort
);
1261 op
->resolve
= memory_region_resolve_container
;
1263 object_property_add(OBJECT(mr
), "addr", "uint64",
1264 memory_region_get_addr
,
1265 NULL
, /* memory_region_set_addr */
1266 NULL
, NULL
, &error_abort
);
1267 object_property_add(OBJECT(mr
), "priority", "uint32",
1268 memory_region_get_priority
,
1269 NULL
, /* memory_region_set_priority */
1270 NULL
, NULL
, &error_abort
);
1271 object_property_add(OBJECT(mr
), "size", "uint64",
1272 memory_region_get_size
,
1273 NULL
, /* memory_region_set_size, */
1274 NULL
, NULL
, &error_abort
);
1277 static void iommu_memory_region_initfn(Object
*obj
)
1279 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1281 mr
->is_iommu
= true;
1284 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1287 #ifdef DEBUG_UNASSIGNED
1288 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1290 if (current_cpu
!= NULL
) {
1291 bool is_exec
= current_cpu
->mem_io_access_type
== MMU_INST_FETCH
;
1292 cpu_unassigned_access(current_cpu
, addr
, false, is_exec
, 0, size
);
1297 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1298 uint64_t val
, unsigned size
)
1300 #ifdef DEBUG_UNASSIGNED
1301 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1303 if (current_cpu
!= NULL
) {
1304 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1308 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1309 unsigned size
, bool is_write
,
1315 const MemoryRegionOps unassigned_mem_ops
= {
1316 .valid
.accepts
= unassigned_mem_accepts
,
1317 .endianness
= DEVICE_NATIVE_ENDIAN
,
1320 static uint64_t memory_region_ram_device_read(void *opaque
,
1321 hwaddr addr
, unsigned size
)
1323 MemoryRegion
*mr
= opaque
;
1324 uint64_t data
= (uint64_t)~0;
1328 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1331 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1334 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1337 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1341 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1346 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1347 uint64_t data
, unsigned size
)
1349 MemoryRegion
*mr
= opaque
;
1351 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1355 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1358 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1361 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1364 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1369 static const MemoryRegionOps ram_device_mem_ops
= {
1370 .read
= memory_region_ram_device_read
,
1371 .write
= memory_region_ram_device_write
,
1372 .endianness
= DEVICE_HOST_ENDIAN
,
1374 .min_access_size
= 1,
1375 .max_access_size
= 8,
1379 .min_access_size
= 1,
1380 .max_access_size
= 8,
1385 bool memory_region_access_valid(MemoryRegion
*mr
,
1391 int access_size_min
, access_size_max
;
1394 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1398 if (!mr
->ops
->valid
.accepts
) {
1402 access_size_min
= mr
->ops
->valid
.min_access_size
;
1403 if (!mr
->ops
->valid
.min_access_size
) {
1404 access_size_min
= 1;
1407 access_size_max
= mr
->ops
->valid
.max_access_size
;
1408 if (!mr
->ops
->valid
.max_access_size
) {
1409 access_size_max
= 4;
1412 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1413 for (i
= 0; i
< size
; i
+= access_size
) {
1414 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1423 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1431 if (mr
->ops
->read
) {
1432 return access_with_adjusted_size(addr
, pval
, size
,
1433 mr
->ops
->impl
.min_access_size
,
1434 mr
->ops
->impl
.max_access_size
,
1435 memory_region_read_accessor
,
1438 return access_with_adjusted_size(addr
, pval
, size
,
1439 mr
->ops
->impl
.min_access_size
,
1440 mr
->ops
->impl
.max_access_size
,
1441 memory_region_read_with_attrs_accessor
,
1446 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1454 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1455 *pval
= unassigned_mem_read(mr
, addr
, size
);
1456 return MEMTX_DECODE_ERROR
;
1459 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1460 adjust_endianness(mr
, pval
, size
);
1464 /* Return true if an eventfd was signalled */
1465 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1471 MemoryRegionIoeventfd ioeventfd
= {
1472 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1477 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1478 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1479 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1481 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1482 event_notifier_set(ioeventfd
.e
);
1490 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1496 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1497 unassigned_mem_write(mr
, addr
, data
, size
);
1498 return MEMTX_DECODE_ERROR
;
1501 adjust_endianness(mr
, &data
, size
);
1503 if ((!kvm_eventfds_enabled()) &&
1504 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1508 if (mr
->ops
->write
) {
1509 return access_with_adjusted_size(addr
, &data
, size
,
1510 mr
->ops
->impl
.min_access_size
,
1511 mr
->ops
->impl
.max_access_size
,
1512 memory_region_write_accessor
, mr
,
1516 access_with_adjusted_size(addr
, &data
, size
,
1517 mr
->ops
->impl
.min_access_size
,
1518 mr
->ops
->impl
.max_access_size
,
1519 memory_region_write_with_attrs_accessor
,
1524 void memory_region_init_io(MemoryRegion
*mr
,
1526 const MemoryRegionOps
*ops
,
1531 memory_region_init(mr
, owner
, name
, size
);
1532 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1533 mr
->opaque
= opaque
;
1534 mr
->terminates
= true;
1537 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1543 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1546 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1554 memory_region_init(mr
, owner
, name
, size
);
1556 mr
->terminates
= true;
1557 mr
->destructor
= memory_region_destructor_ram
;
1558 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, &err
);
1559 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1561 mr
->size
= int128_zero();
1562 object_unparent(OBJECT(mr
));
1563 error_propagate(errp
, err
);
1567 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1572 void (*resized
)(const char*,
1578 memory_region_init(mr
, owner
, name
, size
);
1580 mr
->terminates
= true;
1581 mr
->destructor
= memory_region_destructor_ram
;
1582 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1584 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1586 mr
->size
= int128_zero();
1587 object_unparent(OBJECT(mr
));
1588 error_propagate(errp
, err
);
1593 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1594 struct Object
*owner
,
1603 memory_region_init(mr
, owner
, name
, size
);
1605 mr
->terminates
= true;
1606 mr
->destructor
= memory_region_destructor_ram
;
1608 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
, &err
);
1609 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1611 mr
->size
= int128_zero();
1612 object_unparent(OBJECT(mr
));
1613 error_propagate(errp
, err
);
1617 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1618 struct Object
*owner
,
1626 memory_region_init(mr
, owner
, name
, size
);
1628 mr
->terminates
= true;
1629 mr
->destructor
= memory_region_destructor_ram
;
1630 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
,
1631 share
? RAM_SHARED
: 0,
1633 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1635 mr
->size
= int128_zero();
1636 object_unparent(OBJECT(mr
));
1637 error_propagate(errp
, err
);
1642 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1648 memory_region_init(mr
, owner
, name
, size
);
1650 mr
->terminates
= true;
1651 mr
->destructor
= memory_region_destructor_ram
;
1652 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1654 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1655 assert(ptr
!= NULL
);
1656 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1659 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1665 memory_region_init(mr
, owner
, name
, size
);
1667 mr
->terminates
= true;
1668 mr
->ram_device
= true;
1669 mr
->ops
= &ram_device_mem_ops
;
1671 mr
->destructor
= memory_region_destructor_ram
;
1672 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1673 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1674 assert(ptr
!= NULL
);
1675 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1678 void memory_region_init_alias(MemoryRegion
*mr
,
1685 memory_region_init(mr
, owner
, name
, size
);
1687 mr
->alias_offset
= offset
;
1690 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1691 struct Object
*owner
,
1697 memory_region_init(mr
, owner
, name
, size
);
1699 mr
->readonly
= true;
1700 mr
->terminates
= true;
1701 mr
->destructor
= memory_region_destructor_ram
;
1702 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1703 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1705 mr
->size
= int128_zero();
1706 object_unparent(OBJECT(mr
));
1707 error_propagate(errp
, err
);
1711 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1713 const MemoryRegionOps
*ops
,
1721 memory_region_init(mr
, owner
, name
, size
);
1723 mr
->opaque
= opaque
;
1724 mr
->terminates
= true;
1725 mr
->rom_device
= true;
1726 mr
->destructor
= memory_region_destructor_ram
;
1727 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1729 mr
->size
= int128_zero();
1730 object_unparent(OBJECT(mr
));
1731 error_propagate(errp
, err
);
1735 void memory_region_init_iommu(void *_iommu_mr
,
1736 size_t instance_size
,
1737 const char *mrtypename
,
1742 struct IOMMUMemoryRegion
*iommu_mr
;
1743 struct MemoryRegion
*mr
;
1745 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1746 mr
= MEMORY_REGION(_iommu_mr
);
1747 memory_region_do_init(mr
, owner
, name
, size
);
1748 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1749 mr
->terminates
= true; /* then re-forwards */
1750 QLIST_INIT(&iommu_mr
->iommu_notify
);
1751 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1754 static void memory_region_finalize(Object
*obj
)
1756 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1758 assert(!mr
->container
);
1760 /* We know the region is not visible in any address space (it
1761 * does not have a container and cannot be a root either because
1762 * it has no references, so we can blindly clear mr->enabled.
1763 * memory_region_set_enabled instead could trigger a transaction
1764 * and cause an infinite loop.
1766 mr
->enabled
= false;
1767 memory_region_transaction_begin();
1768 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1769 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1770 memory_region_del_subregion(mr
, subregion
);
1772 memory_region_transaction_commit();
1775 memory_region_clear_coalescing(mr
);
1776 g_free((char *)mr
->name
);
1777 g_free(mr
->ioeventfds
);
1780 Object
*memory_region_owner(MemoryRegion
*mr
)
1782 Object
*obj
= OBJECT(mr
);
1786 void memory_region_ref(MemoryRegion
*mr
)
1788 /* MMIO callbacks most likely will access data that belongs
1789 * to the owner, hence the need to ref/unref the owner whenever
1790 * the memory region is in use.
1792 * The memory region is a child of its owner. As long as the
1793 * owner doesn't call unparent itself on the memory region,
1794 * ref-ing the owner will also keep the memory region alive.
1795 * Memory regions without an owner are supposed to never go away;
1796 * we do not ref/unref them because it slows down DMA sensibly.
1798 if (mr
&& mr
->owner
) {
1799 object_ref(mr
->owner
);
1803 void memory_region_unref(MemoryRegion
*mr
)
1805 if (mr
&& mr
->owner
) {
1806 object_unref(mr
->owner
);
1810 uint64_t memory_region_size(MemoryRegion
*mr
)
1812 if (int128_eq(mr
->size
, int128_2_64())) {
1815 return int128_get64(mr
->size
);
1818 const char *memory_region_name(const MemoryRegion
*mr
)
1821 ((MemoryRegion
*)mr
)->name
=
1822 object_get_canonical_path_component(OBJECT(mr
));
1827 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1829 return mr
->ram_device
;
1832 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1834 uint8_t mask
= mr
->dirty_log_mask
;
1835 if (global_dirty_log
&& mr
->ram_block
) {
1836 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1841 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1843 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1846 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1848 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1849 IOMMUNotifier
*iommu_notifier
;
1850 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1852 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1853 flags
|= iommu_notifier
->notifier_flags
;
1856 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1857 imrc
->notify_flag_changed(iommu_mr
,
1858 iommu_mr
->iommu_notify_flags
,
1862 iommu_mr
->iommu_notify_flags
= flags
;
1865 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1868 IOMMUMemoryRegion
*iommu_mr
;
1871 memory_region_register_iommu_notifier(mr
->alias
, n
);
1875 /* We need to register for at least one bitfield */
1876 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1877 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1878 assert(n
->start
<= n
->end
);
1879 assert(n
->iommu_idx
>= 0 &&
1880 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1882 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1883 memory_region_update_iommu_notify_flags(iommu_mr
);
1886 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1888 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1890 if (imrc
->get_min_page_size
) {
1891 return imrc
->get_min_page_size(iommu_mr
);
1893 return TARGET_PAGE_SIZE
;
1896 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1898 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1899 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1900 hwaddr addr
, granularity
;
1901 IOMMUTLBEntry iotlb
;
1903 /* If the IOMMU has its own replay callback, override */
1905 imrc
->replay(iommu_mr
, n
);
1909 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1911 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1912 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1913 if (iotlb
.perm
!= IOMMU_NONE
) {
1914 n
->notify(n
, &iotlb
);
1917 /* if (2^64 - MR size) < granularity, it's possible to get an
1918 * infinite loop here. This should catch such a wraparound */
1919 if ((addr
+ granularity
) < addr
) {
1925 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
)
1927 IOMMUNotifier
*notifier
;
1929 IOMMU_NOTIFIER_FOREACH(notifier
, iommu_mr
) {
1930 memory_region_iommu_replay(iommu_mr
, notifier
);
1934 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1937 IOMMUMemoryRegion
*iommu_mr
;
1940 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1943 QLIST_REMOVE(n
, node
);
1944 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1945 memory_region_update_iommu_notify_flags(iommu_mr
);
1948 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1949 IOMMUTLBEntry
*entry
)
1951 IOMMUNotifierFlag request_flags
;
1952 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1955 * Skip the notification if the notification does not overlap
1956 * with registered range.
1958 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1962 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1964 if (entry
->perm
& IOMMU_RW
) {
1965 request_flags
= IOMMU_NOTIFIER_MAP
;
1967 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1970 if (notifier
->notifier_flags
& request_flags
) {
1971 notifier
->notify(notifier
, entry
);
1975 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1977 IOMMUTLBEntry entry
)
1979 IOMMUNotifier
*iommu_notifier
;
1981 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1983 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1984 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1985 memory_region_notify_one(iommu_notifier
, &entry
);
1990 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1991 enum IOMMUMemoryRegionAttr attr
,
1994 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1996 if (!imrc
->get_attr
) {
2000 return imrc
->get_attr(iommu_mr
, attr
, data
);
2003 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
2006 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2008 if (!imrc
->attrs_to_index
) {
2012 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2015 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2017 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2019 if (!imrc
->num_indexes
) {
2023 return imrc
->num_indexes(iommu_mr
);
2026 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2028 uint8_t mask
= 1 << client
;
2029 uint8_t old_logging
;
2031 assert(client
== DIRTY_MEMORY_VGA
);
2032 old_logging
= mr
->vga_logging_count
;
2033 mr
->vga_logging_count
+= log
? 1 : -1;
2034 if (!!old_logging
== !!mr
->vga_logging_count
) {
2038 memory_region_transaction_begin();
2039 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2040 memory_region_update_pending
|= mr
->enabled
;
2041 memory_region_transaction_commit();
2044 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2047 assert(mr
->ram_block
);
2048 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2050 memory_region_get_dirty_log_mask(mr
));
2053 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2055 MemoryListener
*listener
;
2060 /* If the same address space has multiple log_sync listeners, we
2061 * visit that address space's FlatView multiple times. But because
2062 * log_sync listeners are rare, it's still cheaper than walking each
2063 * address space once.
2065 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2066 if (!listener
->log_sync
) {
2069 as
= listener
->address_space
;
2070 view
= address_space_get_flatview(as
);
2071 FOR_EACH_FLAT_RANGE(fr
, view
) {
2072 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2073 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2074 listener
->log_sync(listener
, &mrs
);
2077 flatview_unref(view
);
2081 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2084 MemoryRegionSection mrs
;
2085 MemoryListener
*listener
;
2089 hwaddr sec_start
, sec_end
, sec_size
;
2091 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2092 if (!listener
->log_clear
) {
2095 as
= listener
->address_space
;
2096 view
= address_space_get_flatview(as
);
2097 FOR_EACH_FLAT_RANGE(fr
, view
) {
2098 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2100 * Clear dirty bitmap operation only applies to those
2101 * regions whose dirty logging is at least enabled
2106 mrs
= section_from_flat_range(fr
, view
);
2108 sec_start
= MAX(mrs
.offset_within_region
, start
);
2109 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2110 sec_end
= MIN(sec_end
, start
+ len
);
2112 if (sec_start
>= sec_end
) {
2114 * If this memory region section has no intersection
2115 * with the requested range, skip.
2120 /* Valid case; shrink the section if needed */
2121 mrs
.offset_within_address_space
+=
2122 sec_start
- mrs
.offset_within_region
;
2123 mrs
.offset_within_region
= sec_start
;
2124 sec_size
= sec_end
- sec_start
;
2125 mrs
.size
= int128_make64(sec_size
);
2126 listener
->log_clear(listener
, &mrs
);
2128 flatview_unref(view
);
2132 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2137 DirtyBitmapSnapshot
*snapshot
;
2138 assert(mr
->ram_block
);
2139 memory_region_sync_dirty_bitmap(mr
);
2140 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2141 memory_global_after_dirty_log_sync();
2145 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2146 hwaddr addr
, hwaddr size
)
2148 assert(mr
->ram_block
);
2149 return cpu_physical_memory_snapshot_get_dirty(snap
,
2150 memory_region_get_ram_addr(mr
) + addr
, size
);
2153 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2155 if (mr
->readonly
!= readonly
) {
2156 memory_region_transaction_begin();
2157 mr
->readonly
= readonly
;
2158 memory_region_update_pending
|= mr
->enabled
;
2159 memory_region_transaction_commit();
2163 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2165 if (mr
->nonvolatile
!= nonvolatile
) {
2166 memory_region_transaction_begin();
2167 mr
->nonvolatile
= nonvolatile
;
2168 memory_region_update_pending
|= mr
->enabled
;
2169 memory_region_transaction_commit();
2173 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2175 if (mr
->romd_mode
!= romd_mode
) {
2176 memory_region_transaction_begin();
2177 mr
->romd_mode
= romd_mode
;
2178 memory_region_update_pending
|= mr
->enabled
;
2179 memory_region_transaction_commit();
2183 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2184 hwaddr size
, unsigned client
)
2186 assert(mr
->ram_block
);
2187 cpu_physical_memory_test_and_clear_dirty(
2188 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2191 int memory_region_get_fd(MemoryRegion
*mr
)
2199 fd
= mr
->ram_block
->fd
;
2205 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2208 uint64_t offset
= 0;
2212 offset
+= mr
->alias_offset
;
2215 assert(mr
->ram_block
);
2216 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2222 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2226 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2234 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2236 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2239 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2241 assert(mr
->ram_block
);
2243 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2247 * Call proper memory listeners about the change on the newly
2248 * added/removed CoalescedMemoryRange.
2250 static void memory_region_update_coalesced_range(MemoryRegion
*mr
,
2251 CoalescedMemoryRange
*cmr
,
2258 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2259 view
= address_space_get_flatview(as
);
2260 FOR_EACH_FLAT_RANGE(fr
, view
) {
2262 flat_range_coalesced_io_notify(fr
, as
, cmr
, add
);
2265 flatview_unref(view
);
2269 void memory_region_set_coalescing(MemoryRegion
*mr
)
2271 memory_region_clear_coalescing(mr
);
2272 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2275 void memory_region_add_coalescing(MemoryRegion
*mr
,
2279 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2281 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2282 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2283 memory_region_update_coalesced_range(mr
, cmr
, true);
2284 memory_region_set_flush_coalesced(mr
);
2287 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2289 CoalescedMemoryRange
*cmr
;
2291 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2295 qemu_flush_coalesced_mmio_buffer();
2296 mr
->flush_coalesced_mmio
= false;
2298 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2299 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2300 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2301 memory_region_update_coalesced_range(mr
, cmr
, false);
2306 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2308 mr
->flush_coalesced_mmio
= true;
2311 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2313 qemu_flush_coalesced_mmio_buffer();
2314 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2315 mr
->flush_coalesced_mmio
= false;
2319 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2321 mr
->global_locking
= false;
2324 static bool userspace_eventfd_warning
;
2326 void memory_region_add_eventfd(MemoryRegion
*mr
,
2333 MemoryRegionIoeventfd mrfd
= {
2334 .addr
.start
= int128_make64(addr
),
2335 .addr
.size
= int128_make64(size
),
2336 .match_data
= match_data
,
2342 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2343 userspace_eventfd_warning
))) {
2344 userspace_eventfd_warning
= true;
2345 error_report("Using eventfd without MMIO binding in KVM. "
2346 "Suboptimal performance expected");
2350 adjust_endianness(mr
, &mrfd
.data
, size
);
2352 memory_region_transaction_begin();
2353 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2354 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2359 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2360 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2361 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2362 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2363 mr
->ioeventfds
[i
] = mrfd
;
2364 ioeventfd_update_pending
|= mr
->enabled
;
2365 memory_region_transaction_commit();
2368 void memory_region_del_eventfd(MemoryRegion
*mr
,
2375 MemoryRegionIoeventfd mrfd
= {
2376 .addr
.start
= int128_make64(addr
),
2377 .addr
.size
= int128_make64(size
),
2378 .match_data
= match_data
,
2385 adjust_endianness(mr
, &mrfd
.data
, size
);
2387 memory_region_transaction_begin();
2388 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2389 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2393 assert(i
!= mr
->ioeventfd_nb
);
2394 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2395 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2397 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2398 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2399 ioeventfd_update_pending
|= mr
->enabled
;
2400 memory_region_transaction_commit();
2403 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2405 MemoryRegion
*mr
= subregion
->container
;
2406 MemoryRegion
*other
;
2408 memory_region_transaction_begin();
2410 memory_region_ref(subregion
);
2411 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2412 if (subregion
->priority
>= other
->priority
) {
2413 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2417 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2419 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2420 memory_region_transaction_commit();
2423 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2425 MemoryRegion
*subregion
)
2427 assert(!subregion
->container
);
2428 subregion
->container
= mr
;
2429 subregion
->addr
= offset
;
2430 memory_region_update_container_subregions(subregion
);
2433 void memory_region_add_subregion(MemoryRegion
*mr
,
2435 MemoryRegion
*subregion
)
2437 subregion
->priority
= 0;
2438 memory_region_add_subregion_common(mr
, offset
, subregion
);
2441 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2443 MemoryRegion
*subregion
,
2446 subregion
->priority
= priority
;
2447 memory_region_add_subregion_common(mr
, offset
, subregion
);
2450 void memory_region_del_subregion(MemoryRegion
*mr
,
2451 MemoryRegion
*subregion
)
2453 memory_region_transaction_begin();
2454 assert(subregion
->container
== mr
);
2455 subregion
->container
= NULL
;
2456 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2457 memory_region_unref(subregion
);
2458 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2459 memory_region_transaction_commit();
2462 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2464 if (enabled
== mr
->enabled
) {
2467 memory_region_transaction_begin();
2468 mr
->enabled
= enabled
;
2469 memory_region_update_pending
= true;
2470 memory_region_transaction_commit();
2473 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2475 Int128 s
= int128_make64(size
);
2477 if (size
== UINT64_MAX
) {
2480 if (int128_eq(s
, mr
->size
)) {
2483 memory_region_transaction_begin();
2485 memory_region_update_pending
= true;
2486 memory_region_transaction_commit();
2489 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2491 MemoryRegion
*container
= mr
->container
;
2494 memory_region_transaction_begin();
2495 memory_region_ref(mr
);
2496 memory_region_del_subregion(container
, mr
);
2497 mr
->container
= container
;
2498 memory_region_update_container_subregions(mr
);
2499 memory_region_unref(mr
);
2500 memory_region_transaction_commit();
2504 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2506 if (addr
!= mr
->addr
) {
2508 memory_region_readd_subregion(mr
);
2512 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2516 if (offset
== mr
->alias_offset
) {
2520 memory_region_transaction_begin();
2521 mr
->alias_offset
= offset
;
2522 memory_region_update_pending
|= mr
->enabled
;
2523 memory_region_transaction_commit();
2526 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2531 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2533 const AddrRange
*addr
= addr_
;
2534 const FlatRange
*fr
= fr_
;
2536 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2538 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2544 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2546 return bsearch(&addr
, view
->ranges
, view
->nr
,
2547 sizeof(FlatRange
), cmp_flatrange_addr
);
2550 bool memory_region_is_mapped(MemoryRegion
*mr
)
2552 return mr
->container
? true : false;
2555 /* Same as memory_region_find, but it does not add a reference to the
2556 * returned region. It must be called from an RCU critical section.
2558 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2559 hwaddr addr
, uint64_t size
)
2561 MemoryRegionSection ret
= { .mr
= NULL
};
2569 for (root
= mr
; root
->container
; ) {
2570 root
= root
->container
;
2574 as
= memory_region_to_address_space(root
);
2578 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2580 view
= address_space_to_flatview(as
);
2581 fr
= flatview_lookup(view
, range
);
2586 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2592 range
= addrrange_intersection(range
, fr
->addr
);
2593 ret
.offset_within_region
= fr
->offset_in_region
;
2594 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2596 ret
.size
= range
.size
;
2597 ret
.offset_within_address_space
= int128_get64(range
.start
);
2598 ret
.readonly
= fr
->readonly
;
2599 ret
.nonvolatile
= fr
->nonvolatile
;
2603 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2604 hwaddr addr
, uint64_t size
)
2606 MemoryRegionSection ret
;
2608 ret
= memory_region_find_rcu(mr
, addr
, size
);
2610 memory_region_ref(ret
.mr
);
2616 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2621 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2623 return mr
&& mr
!= container
;
2626 void memory_global_dirty_log_sync(void)
2628 memory_region_sync_dirty_bitmap(NULL
);
2631 void memory_global_after_dirty_log_sync(void)
2633 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2636 static VMChangeStateEntry
*vmstate_change
;
2638 void memory_global_dirty_log_start(void)
2640 if (vmstate_change
) {
2641 qemu_del_vm_change_state_handler(vmstate_change
);
2642 vmstate_change
= NULL
;
2645 global_dirty_log
= true;
2647 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2649 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2650 memory_region_transaction_begin();
2651 memory_region_update_pending
= true;
2652 memory_region_transaction_commit();
2655 static void memory_global_dirty_log_do_stop(void)
2657 global_dirty_log
= false;
2659 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2660 memory_region_transaction_begin();
2661 memory_region_update_pending
= true;
2662 memory_region_transaction_commit();
2664 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2667 static void memory_vm_change_state_handler(void *opaque
, int running
,
2671 memory_global_dirty_log_do_stop();
2673 if (vmstate_change
) {
2674 qemu_del_vm_change_state_handler(vmstate_change
);
2675 vmstate_change
= NULL
;
2680 void memory_global_dirty_log_stop(void)
2682 if (!runstate_is_running()) {
2683 if (vmstate_change
) {
2686 vmstate_change
= qemu_add_vm_change_state_handler(
2687 memory_vm_change_state_handler
, NULL
);
2691 memory_global_dirty_log_do_stop();
2694 static void listener_add_address_space(MemoryListener
*listener
,
2700 if (listener
->begin
) {
2701 listener
->begin(listener
);
2703 if (global_dirty_log
) {
2704 if (listener
->log_global_start
) {
2705 listener
->log_global_start(listener
);
2709 view
= address_space_get_flatview(as
);
2710 FOR_EACH_FLAT_RANGE(fr
, view
) {
2711 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2713 if (listener
->region_add
) {
2714 listener
->region_add(listener
, §ion
);
2716 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2717 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2720 if (listener
->commit
) {
2721 listener
->commit(listener
);
2723 flatview_unref(view
);
2726 static void listener_del_address_space(MemoryListener
*listener
,
2732 if (listener
->begin
) {
2733 listener
->begin(listener
);
2735 view
= address_space_get_flatview(as
);
2736 FOR_EACH_FLAT_RANGE(fr
, view
) {
2737 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2739 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2740 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2742 if (listener
->region_del
) {
2743 listener
->region_del(listener
, §ion
);
2746 if (listener
->commit
) {
2747 listener
->commit(listener
);
2749 flatview_unref(view
);
2752 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2754 MemoryListener
*other
= NULL
;
2756 listener
->address_space
= as
;
2757 if (QTAILQ_EMPTY(&memory_listeners
)
2758 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2759 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2761 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2762 if (listener
->priority
< other
->priority
) {
2766 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2769 if (QTAILQ_EMPTY(&as
->listeners
)
2770 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2771 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2773 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2774 if (listener
->priority
< other
->priority
) {
2778 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2781 listener_add_address_space(listener
, as
);
2784 void memory_listener_unregister(MemoryListener
*listener
)
2786 if (!listener
->address_space
) {
2790 listener_del_address_space(listener
, listener
->address_space
);
2791 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2792 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2793 listener
->address_space
= NULL
;
2796 void address_space_remove_listeners(AddressSpace
*as
)
2798 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2799 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2803 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2805 memory_region_ref(root
);
2807 as
->current_map
= NULL
;
2808 as
->ioeventfd_nb
= 0;
2809 as
->ioeventfds
= NULL
;
2810 QTAILQ_INIT(&as
->listeners
);
2811 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2812 as
->name
= g_strdup(name
? name
: "anonymous");
2813 address_space_update_topology(as
);
2814 address_space_update_ioeventfds(as
);
2817 static void do_address_space_destroy(AddressSpace
*as
)
2819 assert(QTAILQ_EMPTY(&as
->listeners
));
2821 flatview_unref(as
->current_map
);
2823 g_free(as
->ioeventfds
);
2824 memory_region_unref(as
->root
);
2827 void address_space_destroy(AddressSpace
*as
)
2829 MemoryRegion
*root
= as
->root
;
2831 /* Flush out anything from MemoryListeners listening in on this */
2832 memory_region_transaction_begin();
2834 memory_region_transaction_commit();
2835 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2837 /* At this point, as->dispatch and as->current_map are dummy
2838 * entries that the guest should never use. Wait for the old
2839 * values to expire before freeing the data.
2842 call_rcu(as
, do_address_space_destroy
, rcu
);
2845 static const char *memory_region_type(MemoryRegion
*mr
)
2847 if (memory_region_is_ram_device(mr
)) {
2849 } else if (memory_region_is_romd(mr
)) {
2851 } else if (memory_region_is_rom(mr
)) {
2853 } else if (memory_region_is_ram(mr
)) {
2860 typedef struct MemoryRegionList MemoryRegionList
;
2862 struct MemoryRegionList
{
2863 const MemoryRegion
*mr
;
2864 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2867 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2869 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2870 int128_sub((size), int128_one())) : 0)
2871 #define MTREE_INDENT " "
2873 static void mtree_expand_owner(const char *label
, Object
*obj
)
2875 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
2877 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
2878 if (dev
&& dev
->id
) {
2879 qemu_printf(" id=%s", dev
->id
);
2881 gchar
*canonical_path
= object_get_canonical_path(obj
);
2882 if (canonical_path
) {
2883 qemu_printf(" path=%s", canonical_path
);
2884 g_free(canonical_path
);
2886 qemu_printf(" type=%s", object_get_typename(obj
));
2892 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
2894 Object
*owner
= mr
->owner
;
2895 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
2897 if (!owner
&& !parent
) {
2898 qemu_printf(" orphan");
2902 mtree_expand_owner("owner", owner
);
2904 if (parent
&& parent
!= owner
) {
2905 mtree_expand_owner("parent", parent
);
2909 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
2911 MemoryRegionListHead
*alias_print_queue
,
2914 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2915 MemoryRegionListHead submr_print_queue
;
2916 const MemoryRegion
*submr
;
2918 hwaddr cur_start
, cur_end
;
2924 for (i
= 0; i
< level
; i
++) {
2925 qemu_printf(MTREE_INDENT
);
2928 cur_start
= base
+ mr
->addr
;
2929 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2932 * Try to detect overflow of memory region. This should never
2933 * happen normally. When it happens, we dump something to warn the
2934 * user who is observing this.
2936 if (cur_start
< base
|| cur_end
< cur_start
) {
2937 qemu_printf("[DETECTED OVERFLOW!] ");
2941 MemoryRegionList
*ml
;
2944 /* check if the alias is already in the queue */
2945 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2946 if (ml
->mr
== mr
->alias
) {
2952 ml
= g_new(MemoryRegionList
, 1);
2954 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2956 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2957 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2958 "-" TARGET_FMT_plx
"%s",
2961 mr
->nonvolatile
? "nv-" : "",
2962 memory_region_type((MemoryRegion
*)mr
),
2963 memory_region_name(mr
),
2964 memory_region_name(mr
->alias
),
2966 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2967 mr
->enabled
? "" : " [disabled]");
2969 mtree_print_mr_owner(mr
);
2972 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2973 " (prio %d, %s%s): %s%s",
2976 mr
->nonvolatile
? "nv-" : "",
2977 memory_region_type((MemoryRegion
*)mr
),
2978 memory_region_name(mr
),
2979 mr
->enabled
? "" : " [disabled]");
2981 mtree_print_mr_owner(mr
);
2986 QTAILQ_INIT(&submr_print_queue
);
2988 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2989 new_ml
= g_new(MemoryRegionList
, 1);
2991 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2992 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2993 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2994 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2995 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
3001 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
3005 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3006 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
3007 alias_print_queue
, owner
);
3010 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3015 struct FlatViewInfo
{
3020 const char *ac_name
;
3023 static void mtree_print_flatview(gpointer key
, gpointer value
,
3026 FlatView
*view
= key
;
3027 GArray
*fv_address_spaces
= value
;
3028 struct FlatViewInfo
*fvi
= user_data
;
3029 FlatRange
*range
= &view
->ranges
[0];
3035 qemu_printf("FlatView #%d\n", fvi
->counter
);
3038 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3039 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3040 qemu_printf(" AS \"%s\", root: %s",
3041 as
->name
, memory_region_name(as
->root
));
3042 if (as
->root
->alias
) {
3043 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3048 qemu_printf(" Root memory region: %s\n",
3049 view
->root
? memory_region_name(view
->root
) : "(none)");
3052 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3058 if (range
->offset_in_region
) {
3059 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3060 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3061 int128_get64(range
->addr
.start
),
3062 int128_get64(range
->addr
.start
)
3063 + MR_SIZE(range
->addr
.size
),
3065 range
->nonvolatile
? "nv-" : "",
3066 range
->readonly
? "rom" : memory_region_type(mr
),
3067 memory_region_name(mr
),
3068 range
->offset_in_region
);
3070 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3071 " (prio %d, %s%s): %s",
3072 int128_get64(range
->addr
.start
),
3073 int128_get64(range
->addr
.start
)
3074 + MR_SIZE(range
->addr
.size
),
3076 range
->nonvolatile
? "nv-" : "",
3077 range
->readonly
? "rom" : memory_region_type(mr
),
3078 memory_region_name(mr
));
3081 mtree_print_mr_owner(mr
);
3085 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3086 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3087 if (fvi
->ac
->has_memory(current_machine
, as
,
3088 int128_get64(range
->addr
.start
),
3089 MR_SIZE(range
->addr
.size
) + 1)) {
3090 qemu_printf(" %s", fvi
->ac_name
);
3098 #if !defined(CONFIG_USER_ONLY)
3099 if (fvi
->dispatch_tree
&& view
->root
) {
3100 mtree_print_dispatch(view
->dispatch
, view
->root
);
3107 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3110 FlatView
*view
= key
;
3111 GArray
*fv_address_spaces
= value
;
3113 g_array_unref(fv_address_spaces
);
3114 flatview_unref(view
);
3119 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
)
3121 MemoryRegionListHead ml_head
;
3122 MemoryRegionList
*ml
, *ml2
;
3127 struct FlatViewInfo fvi
= {
3129 .dispatch_tree
= dispatch_tree
,
3132 GArray
*fv_address_spaces
;
3133 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3134 AccelClass
*ac
= ACCEL_GET_CLASS(current_machine
->accelerator
);
3136 if (ac
->has_memory
) {
3138 fvi
.ac_name
= current_machine
->accel
? current_machine
->accel
:
3139 object_class_get_name(OBJECT_CLASS(ac
));
3142 /* Gather all FVs in one table */
3143 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3144 view
= address_space_get_flatview(as
);
3146 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3147 if (!fv_address_spaces
) {
3148 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3149 g_hash_table_insert(views
, view
, fv_address_spaces
);
3152 g_array_append_val(fv_address_spaces
, as
);
3156 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3159 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3160 g_hash_table_unref(views
);
3165 QTAILQ_INIT(&ml_head
);
3167 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3168 qemu_printf("address-space: %s\n", as
->name
);
3169 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
);
3173 /* print aliased regions */
3174 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3175 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3176 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
);
3180 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3185 void memory_region_init_ram(MemoryRegion
*mr
,
3186 struct Object
*owner
,
3191 DeviceState
*owner_dev
;
3194 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3196 error_propagate(errp
, err
);
3199 /* This will assert if owner is neither NULL nor a DeviceState.
3200 * We only want the owner here for the purposes of defining a
3201 * unique name for migration. TODO: Ideally we should implement
3202 * a naming scheme for Objects which are not DeviceStates, in
3203 * which case we can relax this restriction.
3205 owner_dev
= DEVICE(owner
);
3206 vmstate_register_ram(mr
, owner_dev
);
3209 void memory_region_init_rom(MemoryRegion
*mr
,
3210 struct Object
*owner
,
3215 DeviceState
*owner_dev
;
3218 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3220 error_propagate(errp
, err
);
3223 /* This will assert if owner is neither NULL nor a DeviceState.
3224 * We only want the owner here for the purposes of defining a
3225 * unique name for migration. TODO: Ideally we should implement
3226 * a naming scheme for Objects which are not DeviceStates, in
3227 * which case we can relax this restriction.
3229 owner_dev
= DEVICE(owner
);
3230 vmstate_register_ram(mr
, owner_dev
);
3233 void memory_region_init_rom_device(MemoryRegion
*mr
,
3234 struct Object
*owner
,
3235 const MemoryRegionOps
*ops
,
3241 DeviceState
*owner_dev
;
3244 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3247 error_propagate(errp
, err
);
3250 /* This will assert if owner is neither NULL nor a DeviceState.
3251 * We only want the owner here for the purposes of defining a
3252 * unique name for migration. TODO: Ideally we should implement
3253 * a naming scheme for Objects which are not DeviceStates, in
3254 * which case we can relax this restriction.
3256 owner_dev
= DEVICE(owner
);
3257 vmstate_register_ram(mr
, owner_dev
);
3260 static const TypeInfo memory_region_info
= {
3261 .parent
= TYPE_OBJECT
,
3262 .name
= TYPE_MEMORY_REGION
,
3263 .class_size
= sizeof(MemoryRegionClass
),
3264 .instance_size
= sizeof(MemoryRegion
),
3265 .instance_init
= memory_region_initfn
,
3266 .instance_finalize
= memory_region_finalize
,
3269 static const TypeInfo iommu_memory_region_info
= {
3270 .parent
= TYPE_MEMORY_REGION
,
3271 .name
= TYPE_IOMMU_MEMORY_REGION
,
3272 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3273 .instance_size
= sizeof(IOMMUMemoryRegion
),
3274 .instance_init
= iommu_memory_region_initfn
,
3278 static void memory_register_types(void)
3280 type_register_static(&memory_region_info
);
3281 type_register_static(&iommu_memory_region_info
);
3284 type_init(memory_register_types
)