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 (mr
== &io_mem_notdirty
) {
438 /* Accesses to code which has previously been translated into a TB show
439 * up in the MMIO path, as accesses to the io_mem_notdirty
441 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
442 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
443 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
444 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
446 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
450 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
461 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
463 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
464 } else if (mr
== &io_mem_notdirty
) {
465 /* Accesses to code which has previously been translated into a TB show
466 * up in the MMIO path, as accesses to the io_mem_notdirty
468 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
469 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
470 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
471 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
473 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
477 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
485 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
488 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
489 } else if (mr
== &io_mem_notdirty
) {
490 /* Accesses to code which has previously been translated into a TB show
491 * up in the MMIO path, as accesses to the io_mem_notdirty
493 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
494 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
495 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
496 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
498 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
502 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
510 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
513 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
514 } else if (mr
== &io_mem_notdirty
) {
515 /* Accesses to code which has previously been translated into a TB show
516 * up in the MMIO path, as accesses to the io_mem_notdirty
518 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
519 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
520 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
521 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
523 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
526 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
529 unsigned access_size_min
,
530 unsigned access_size_max
,
531 MemTxResult (*access_fn
)
542 uint64_t access_mask
;
543 unsigned access_size
;
545 MemTxResult r
= MEMTX_OK
;
547 if (!access_size_min
) {
550 if (!access_size_max
) {
554 /* FIXME: support unaligned access? */
555 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
556 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
557 if (memory_region_big_endian(mr
)) {
558 for (i
= 0; i
< size
; i
+= access_size
) {
559 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
560 (size
- access_size
- i
) * 8, access_mask
, attrs
);
563 for (i
= 0; i
< size
; i
+= access_size
) {
564 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
571 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
575 while (mr
->container
) {
578 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
579 if (mr
== as
->root
) {
586 /* Render a memory region into the global view. Ranges in @view obscure
589 static void render_memory_region(FlatView
*view
,
596 MemoryRegion
*subregion
;
598 hwaddr offset_in_region
;
608 int128_addto(&base
, int128_make64(mr
->addr
));
609 readonly
|= mr
->readonly
;
610 nonvolatile
|= mr
->nonvolatile
;
612 tmp
= addrrange_make(base
, mr
->size
);
614 if (!addrrange_intersects(tmp
, clip
)) {
618 clip
= addrrange_intersection(tmp
, clip
);
621 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
622 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
623 render_memory_region(view
, mr
->alias
, base
, clip
,
624 readonly
, nonvolatile
);
628 /* Render subregions in priority order. */
629 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
630 render_memory_region(view
, subregion
, base
, clip
,
631 readonly
, nonvolatile
);
634 if (!mr
->terminates
) {
638 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
643 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
644 fr
.romd_mode
= mr
->romd_mode
;
645 fr
.readonly
= readonly
;
646 fr
.nonvolatile
= nonvolatile
;
648 /* Render the region itself into any gaps left by the current view. */
649 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
650 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
653 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
654 now
= int128_min(remain
,
655 int128_sub(view
->ranges
[i
].addr
.start
, base
));
656 fr
.offset_in_region
= offset_in_region
;
657 fr
.addr
= addrrange_make(base
, now
);
658 flatview_insert(view
, i
, &fr
);
660 int128_addto(&base
, now
);
661 offset_in_region
+= int128_get64(now
);
662 int128_subfrom(&remain
, now
);
664 now
= int128_sub(int128_min(int128_add(base
, remain
),
665 addrrange_end(view
->ranges
[i
].addr
)),
667 int128_addto(&base
, now
);
668 offset_in_region
+= int128_get64(now
);
669 int128_subfrom(&remain
, now
);
671 if (int128_nz(remain
)) {
672 fr
.offset_in_region
= offset_in_region
;
673 fr
.addr
= addrrange_make(base
, remain
);
674 flatview_insert(view
, i
, &fr
);
678 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
680 while (mr
->enabled
) {
682 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
683 /* The alias is included in its entirety. Use it as
684 * the "real" root, so that we can share more FlatViews.
689 } else if (!mr
->terminates
) {
690 unsigned int found
= 0;
691 MemoryRegion
*child
, *next
= NULL
;
692 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
693 if (child
->enabled
) {
698 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
699 /* A child is included in its entirety. If it's the only
700 * enabled one, use it in the hope of finding an alias down the
701 * way. This will also let us share FlatViews.
722 /* Render a memory topology into a list of disjoint absolute ranges. */
723 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
728 view
= flatview_new(mr
);
731 render_memory_region(view
, mr
, int128_zero(),
732 addrrange_make(int128_zero(), int128_2_64()),
735 flatview_simplify(view
);
737 view
->dispatch
= address_space_dispatch_new(view
);
738 for (i
= 0; i
< view
->nr
; i
++) {
739 MemoryRegionSection mrs
=
740 section_from_flat_range(&view
->ranges
[i
], view
);
741 flatview_add_to_dispatch(view
, &mrs
);
743 address_space_dispatch_compact(view
->dispatch
);
744 g_hash_table_replace(flat_views
, mr
, view
);
749 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
750 MemoryRegionIoeventfd
*fds_new
,
752 MemoryRegionIoeventfd
*fds_old
,
756 MemoryRegionIoeventfd
*fd
;
757 MemoryRegionSection section
;
759 /* Generate a symmetric difference of the old and new fd sets, adding
760 * and deleting as necessary.
764 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
765 if (iold
< fds_old_nb
766 && (inew
== fds_new_nb
767 || memory_region_ioeventfd_before(&fds_old
[iold
],
770 section
= (MemoryRegionSection
) {
771 .fv
= address_space_to_flatview(as
),
772 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
773 .size
= fd
->addr
.size
,
775 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
776 fd
->match_data
, fd
->data
, fd
->e
);
778 } else if (inew
< fds_new_nb
779 && (iold
== fds_old_nb
780 || memory_region_ioeventfd_before(&fds_new
[inew
],
783 section
= (MemoryRegionSection
) {
784 .fv
= address_space_to_flatview(as
),
785 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
786 .size
= fd
->addr
.size
,
788 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
789 fd
->match_data
, fd
->data
, fd
->e
);
798 FlatView
*address_space_get_flatview(AddressSpace
*as
)
804 view
= address_space_to_flatview(as
);
805 /* If somebody has replaced as->current_map concurrently,
806 * flatview_ref returns false.
808 } while (!flatview_ref(view
));
813 static void address_space_update_ioeventfds(AddressSpace
*as
)
817 unsigned ioeventfd_nb
= 0;
818 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
822 view
= address_space_get_flatview(as
);
823 FOR_EACH_FLAT_RANGE(fr
, view
) {
824 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
825 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
826 int128_sub(fr
->addr
.start
,
827 int128_make64(fr
->offset_in_region
)));
828 if (addrrange_intersects(fr
->addr
, tmp
)) {
830 ioeventfds
= g_realloc(ioeventfds
,
831 ioeventfd_nb
* sizeof(*ioeventfds
));
832 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
833 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
838 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
839 as
->ioeventfds
, as
->ioeventfd_nb
);
841 g_free(as
->ioeventfds
);
842 as
->ioeventfds
= ioeventfds
;
843 as
->ioeventfd_nb
= ioeventfd_nb
;
844 flatview_unref(view
);
848 * Notify the memory listeners about the coalesced IO change events of
849 * range `cmr'. Only the part that has intersection of the specified
850 * FlatRange will be sent.
852 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
853 CoalescedMemoryRange
*cmr
, bool add
)
857 tmp
= addrrange_shift(cmr
->addr
,
858 int128_sub(fr
->addr
.start
,
859 int128_make64(fr
->offset_in_region
)));
860 if (!addrrange_intersects(tmp
, fr
->addr
)) {
863 tmp
= addrrange_intersection(tmp
, fr
->addr
);
866 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
867 int128_get64(tmp
.start
),
868 int128_get64(tmp
.size
));
870 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
871 int128_get64(tmp
.start
),
872 int128_get64(tmp
.size
));
876 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
878 CoalescedMemoryRange
*cmr
;
880 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
881 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
885 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
887 MemoryRegion
*mr
= fr
->mr
;
888 CoalescedMemoryRange
*cmr
;
890 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
894 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
895 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
899 static void address_space_update_topology_pass(AddressSpace
*as
,
900 const FlatView
*old_view
,
901 const FlatView
*new_view
,
905 FlatRange
*frold
, *frnew
;
907 /* Generate a symmetric difference of the old and new memory maps.
908 * Kill ranges in the old map, and instantiate ranges in the new map.
911 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
912 if (iold
< old_view
->nr
) {
913 frold
= &old_view
->ranges
[iold
];
917 if (inew
< new_view
->nr
) {
918 frnew
= &new_view
->ranges
[inew
];
925 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
926 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
927 && !flatrange_equal(frold
, frnew
)))) {
928 /* In old but not in new, or in both but attributes changed. */
931 flat_range_coalesced_io_del(frold
, as
);
932 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
936 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
937 /* In both and unchanged (except logging may have changed) */
940 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
941 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
942 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
943 frold
->dirty_log_mask
,
944 frnew
->dirty_log_mask
);
946 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
947 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
948 frold
->dirty_log_mask
,
949 frnew
->dirty_log_mask
);
959 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
960 flat_range_coalesced_io_add(frnew
, as
);
968 static void flatviews_init(void)
970 static FlatView
*empty_view
;
976 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
977 (GDestroyNotify
) flatview_unref
);
979 empty_view
= generate_memory_topology(NULL
);
980 /* We keep it alive forever in the global variable. */
981 flatview_ref(empty_view
);
983 g_hash_table_replace(flat_views
, NULL
, empty_view
);
984 flatview_ref(empty_view
);
988 static void flatviews_reset(void)
993 g_hash_table_unref(flat_views
);
998 /* Render unique FVs */
999 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1000 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1002 if (g_hash_table_lookup(flat_views
, physmr
)) {
1006 generate_memory_topology(physmr
);
1010 static void address_space_set_flatview(AddressSpace
*as
)
1012 FlatView
*old_view
= address_space_to_flatview(as
);
1013 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1014 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1018 if (old_view
== new_view
) {
1023 flatview_ref(old_view
);
1026 flatview_ref(new_view
);
1028 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1029 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1032 old_view2
= &tmpview
;
1034 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1035 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1038 /* Writes are protected by the BQL. */
1039 atomic_rcu_set(&as
->current_map
, new_view
);
1041 flatview_unref(old_view
);
1044 /* Note that all the old MemoryRegions are still alive up to this
1045 * point. This relieves most MemoryListeners from the need to
1046 * ref/unref the MemoryRegions they get---unless they use them
1047 * outside the iothread mutex, in which case precise reference
1048 * counting is necessary.
1051 flatview_unref(old_view
);
1055 static void address_space_update_topology(AddressSpace
*as
)
1057 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1060 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1061 generate_memory_topology(physmr
);
1063 address_space_set_flatview(as
);
1066 void memory_region_transaction_begin(void)
1068 qemu_flush_coalesced_mmio_buffer();
1069 ++memory_region_transaction_depth
;
1072 void memory_region_transaction_commit(void)
1076 assert(memory_region_transaction_depth
);
1077 assert(qemu_mutex_iothread_locked());
1079 --memory_region_transaction_depth
;
1080 if (!memory_region_transaction_depth
) {
1081 if (memory_region_update_pending
) {
1084 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1086 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1087 address_space_set_flatview(as
);
1088 address_space_update_ioeventfds(as
);
1090 memory_region_update_pending
= false;
1091 ioeventfd_update_pending
= false;
1092 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1093 } else if (ioeventfd_update_pending
) {
1094 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1095 address_space_update_ioeventfds(as
);
1097 ioeventfd_update_pending
= false;
1102 static void memory_region_destructor_none(MemoryRegion
*mr
)
1106 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1108 qemu_ram_free(mr
->ram_block
);
1111 static bool memory_region_need_escape(char c
)
1113 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1116 static char *memory_region_escape_name(const char *name
)
1123 for (p
= name
; *p
; p
++) {
1124 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1126 if (bytes
== p
- name
) {
1127 return g_memdup(name
, bytes
+ 1);
1130 escaped
= g_malloc(bytes
+ 1);
1131 for (p
= name
, q
= escaped
; *p
; p
++) {
1133 if (unlikely(memory_region_need_escape(c
))) {
1136 *q
++ = "0123456789abcdef"[c
>> 4];
1137 c
= "0123456789abcdef"[c
& 15];
1145 static void memory_region_do_init(MemoryRegion
*mr
,
1150 mr
->size
= int128_make64(size
);
1151 if (size
== UINT64_MAX
) {
1152 mr
->size
= int128_2_64();
1154 mr
->name
= g_strdup(name
);
1156 mr
->ram_block
= NULL
;
1159 char *escaped_name
= memory_region_escape_name(name
);
1160 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1163 owner
= container_get(qdev_get_machine(), "/unattached");
1166 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1167 object_unref(OBJECT(mr
));
1169 g_free(escaped_name
);
1173 void memory_region_init(MemoryRegion
*mr
,
1178 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1179 memory_region_do_init(mr
, owner
, name
, size
);
1182 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1183 void *opaque
, Error
**errp
)
1185 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1186 uint64_t value
= mr
->addr
;
1188 visit_type_uint64(v
, name
, &value
, errp
);
1191 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1192 const char *name
, void *opaque
,
1195 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1196 gchar
*path
= (gchar
*)"";
1198 if (mr
->container
) {
1199 path
= object_get_canonical_path(OBJECT(mr
->container
));
1201 visit_type_str(v
, name
, &path
, errp
);
1202 if (mr
->container
) {
1207 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1210 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1212 return OBJECT(mr
->container
);
1215 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1216 const char *name
, void *opaque
,
1219 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1220 int32_t value
= mr
->priority
;
1222 visit_type_int32(v
, name
, &value
, errp
);
1225 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1226 void *opaque
, Error
**errp
)
1228 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1229 uint64_t value
= memory_region_size(mr
);
1231 visit_type_uint64(v
, name
, &value
, errp
);
1234 static void memory_region_initfn(Object
*obj
)
1236 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1239 mr
->ops
= &unassigned_mem_ops
;
1241 mr
->romd_mode
= true;
1242 mr
->global_locking
= true;
1243 mr
->destructor
= memory_region_destructor_none
;
1244 QTAILQ_INIT(&mr
->subregions
);
1245 QTAILQ_INIT(&mr
->coalesced
);
1247 op
= object_property_add(OBJECT(mr
), "container",
1248 "link<" TYPE_MEMORY_REGION
">",
1249 memory_region_get_container
,
1250 NULL
, /* memory_region_set_container */
1251 NULL
, NULL
, &error_abort
);
1252 op
->resolve
= memory_region_resolve_container
;
1254 object_property_add(OBJECT(mr
), "addr", "uint64",
1255 memory_region_get_addr
,
1256 NULL
, /* memory_region_set_addr */
1257 NULL
, NULL
, &error_abort
);
1258 object_property_add(OBJECT(mr
), "priority", "uint32",
1259 memory_region_get_priority
,
1260 NULL
, /* memory_region_set_priority */
1261 NULL
, NULL
, &error_abort
);
1262 object_property_add(OBJECT(mr
), "size", "uint64",
1263 memory_region_get_size
,
1264 NULL
, /* memory_region_set_size, */
1265 NULL
, NULL
, &error_abort
);
1268 static void iommu_memory_region_initfn(Object
*obj
)
1270 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1272 mr
->is_iommu
= true;
1275 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1278 #ifdef DEBUG_UNASSIGNED
1279 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1281 if (current_cpu
!= NULL
) {
1282 bool is_exec
= current_cpu
->mem_io_access_type
== MMU_INST_FETCH
;
1283 cpu_unassigned_access(current_cpu
, addr
, false, is_exec
, 0, size
);
1288 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1289 uint64_t val
, unsigned size
)
1291 #ifdef DEBUG_UNASSIGNED
1292 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1294 if (current_cpu
!= NULL
) {
1295 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1299 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1300 unsigned size
, bool is_write
,
1306 const MemoryRegionOps unassigned_mem_ops
= {
1307 .valid
.accepts
= unassigned_mem_accepts
,
1308 .endianness
= DEVICE_NATIVE_ENDIAN
,
1311 static uint64_t memory_region_ram_device_read(void *opaque
,
1312 hwaddr addr
, unsigned size
)
1314 MemoryRegion
*mr
= opaque
;
1315 uint64_t data
= (uint64_t)~0;
1319 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1322 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1325 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1328 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1332 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1337 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1338 uint64_t data
, unsigned size
)
1340 MemoryRegion
*mr
= opaque
;
1342 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1346 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1349 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1352 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1355 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1360 static const MemoryRegionOps ram_device_mem_ops
= {
1361 .read
= memory_region_ram_device_read
,
1362 .write
= memory_region_ram_device_write
,
1363 .endianness
= DEVICE_HOST_ENDIAN
,
1365 .min_access_size
= 1,
1366 .max_access_size
= 8,
1370 .min_access_size
= 1,
1371 .max_access_size
= 8,
1376 bool memory_region_access_valid(MemoryRegion
*mr
,
1382 int access_size_min
, access_size_max
;
1385 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1389 if (!mr
->ops
->valid
.accepts
) {
1393 access_size_min
= mr
->ops
->valid
.min_access_size
;
1394 if (!mr
->ops
->valid
.min_access_size
) {
1395 access_size_min
= 1;
1398 access_size_max
= mr
->ops
->valid
.max_access_size
;
1399 if (!mr
->ops
->valid
.max_access_size
) {
1400 access_size_max
= 4;
1403 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1404 for (i
= 0; i
< size
; i
+= access_size
) {
1405 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1414 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1422 if (mr
->ops
->read
) {
1423 return access_with_adjusted_size(addr
, pval
, size
,
1424 mr
->ops
->impl
.min_access_size
,
1425 mr
->ops
->impl
.max_access_size
,
1426 memory_region_read_accessor
,
1429 return access_with_adjusted_size(addr
, pval
, size
,
1430 mr
->ops
->impl
.min_access_size
,
1431 mr
->ops
->impl
.max_access_size
,
1432 memory_region_read_with_attrs_accessor
,
1437 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1443 unsigned size
= memop_size(op
);
1446 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1447 *pval
= unassigned_mem_read(mr
, addr
, size
);
1448 return MEMTX_DECODE_ERROR
;
1451 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1452 adjust_endianness(mr
, pval
, op
);
1456 /* Return true if an eventfd was signalled */
1457 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1463 MemoryRegionIoeventfd ioeventfd
= {
1464 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1469 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1470 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1471 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1473 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1474 event_notifier_set(ioeventfd
.e
);
1482 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1488 unsigned size
= memop_size(op
);
1490 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1491 unassigned_mem_write(mr
, addr
, data
, size
);
1492 return MEMTX_DECODE_ERROR
;
1495 adjust_endianness(mr
, &data
, op
);
1497 if ((!kvm_eventfds_enabled()) &&
1498 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1502 if (mr
->ops
->write
) {
1503 return access_with_adjusted_size(addr
, &data
, size
,
1504 mr
->ops
->impl
.min_access_size
,
1505 mr
->ops
->impl
.max_access_size
,
1506 memory_region_write_accessor
, mr
,
1510 access_with_adjusted_size(addr
, &data
, size
,
1511 mr
->ops
->impl
.min_access_size
,
1512 mr
->ops
->impl
.max_access_size
,
1513 memory_region_write_with_attrs_accessor
,
1518 void memory_region_init_io(MemoryRegion
*mr
,
1520 const MemoryRegionOps
*ops
,
1525 memory_region_init(mr
, owner
, name
, size
);
1526 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1527 mr
->opaque
= opaque
;
1528 mr
->terminates
= true;
1531 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1537 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1540 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1548 memory_region_init(mr
, owner
, name
, size
);
1550 mr
->terminates
= true;
1551 mr
->destructor
= memory_region_destructor_ram
;
1552 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, &err
);
1553 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1555 mr
->size
= int128_zero();
1556 object_unparent(OBJECT(mr
));
1557 error_propagate(errp
, err
);
1561 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1566 void (*resized
)(const char*,
1572 memory_region_init(mr
, owner
, name
, size
);
1574 mr
->terminates
= true;
1575 mr
->destructor
= memory_region_destructor_ram
;
1576 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1578 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1580 mr
->size
= int128_zero();
1581 object_unparent(OBJECT(mr
));
1582 error_propagate(errp
, err
);
1587 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1588 struct Object
*owner
,
1597 memory_region_init(mr
, owner
, name
, size
);
1599 mr
->terminates
= true;
1600 mr
->destructor
= memory_region_destructor_ram
;
1602 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
, &err
);
1603 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1605 mr
->size
= int128_zero();
1606 object_unparent(OBJECT(mr
));
1607 error_propagate(errp
, err
);
1611 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1612 struct Object
*owner
,
1620 memory_region_init(mr
, owner
, name
, size
);
1622 mr
->terminates
= true;
1623 mr
->destructor
= memory_region_destructor_ram
;
1624 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
,
1625 share
? RAM_SHARED
: 0,
1627 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1629 mr
->size
= int128_zero();
1630 object_unparent(OBJECT(mr
));
1631 error_propagate(errp
, err
);
1636 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1642 memory_region_init(mr
, owner
, name
, size
);
1644 mr
->terminates
= true;
1645 mr
->destructor
= memory_region_destructor_ram
;
1646 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1648 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1649 assert(ptr
!= NULL
);
1650 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1653 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1659 memory_region_init(mr
, owner
, name
, size
);
1661 mr
->terminates
= true;
1662 mr
->ram_device
= true;
1663 mr
->ops
= &ram_device_mem_ops
;
1665 mr
->destructor
= memory_region_destructor_ram
;
1666 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1667 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1668 assert(ptr
!= NULL
);
1669 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1672 void memory_region_init_alias(MemoryRegion
*mr
,
1679 memory_region_init(mr
, owner
, name
, size
);
1681 mr
->alias_offset
= offset
;
1684 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1685 struct Object
*owner
,
1691 memory_region_init(mr
, owner
, name
, size
);
1693 mr
->readonly
= true;
1694 mr
->terminates
= true;
1695 mr
->destructor
= memory_region_destructor_ram
;
1696 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1697 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1699 mr
->size
= int128_zero();
1700 object_unparent(OBJECT(mr
));
1701 error_propagate(errp
, err
);
1705 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1707 const MemoryRegionOps
*ops
,
1715 memory_region_init(mr
, owner
, name
, size
);
1717 mr
->opaque
= opaque
;
1718 mr
->terminates
= true;
1719 mr
->rom_device
= true;
1720 mr
->destructor
= memory_region_destructor_ram
;
1721 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1723 mr
->size
= int128_zero();
1724 object_unparent(OBJECT(mr
));
1725 error_propagate(errp
, err
);
1729 void memory_region_init_iommu(void *_iommu_mr
,
1730 size_t instance_size
,
1731 const char *mrtypename
,
1736 struct IOMMUMemoryRegion
*iommu_mr
;
1737 struct MemoryRegion
*mr
;
1739 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1740 mr
= MEMORY_REGION(_iommu_mr
);
1741 memory_region_do_init(mr
, owner
, name
, size
);
1742 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1743 mr
->terminates
= true; /* then re-forwards */
1744 QLIST_INIT(&iommu_mr
->iommu_notify
);
1745 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1748 static void memory_region_finalize(Object
*obj
)
1750 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1752 assert(!mr
->container
);
1754 /* We know the region is not visible in any address space (it
1755 * does not have a container and cannot be a root either because
1756 * it has no references, so we can blindly clear mr->enabled.
1757 * memory_region_set_enabled instead could trigger a transaction
1758 * and cause an infinite loop.
1760 mr
->enabled
= false;
1761 memory_region_transaction_begin();
1762 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1763 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1764 memory_region_del_subregion(mr
, subregion
);
1766 memory_region_transaction_commit();
1769 memory_region_clear_coalescing(mr
);
1770 g_free((char *)mr
->name
);
1771 g_free(mr
->ioeventfds
);
1774 Object
*memory_region_owner(MemoryRegion
*mr
)
1776 Object
*obj
= OBJECT(mr
);
1780 void memory_region_ref(MemoryRegion
*mr
)
1782 /* MMIO callbacks most likely will access data that belongs
1783 * to the owner, hence the need to ref/unref the owner whenever
1784 * the memory region is in use.
1786 * The memory region is a child of its owner. As long as the
1787 * owner doesn't call unparent itself on the memory region,
1788 * ref-ing the owner will also keep the memory region alive.
1789 * Memory regions without an owner are supposed to never go away;
1790 * we do not ref/unref them because it slows down DMA sensibly.
1792 if (mr
&& mr
->owner
) {
1793 object_ref(mr
->owner
);
1797 void memory_region_unref(MemoryRegion
*mr
)
1799 if (mr
&& mr
->owner
) {
1800 object_unref(mr
->owner
);
1804 uint64_t memory_region_size(MemoryRegion
*mr
)
1806 if (int128_eq(mr
->size
, int128_2_64())) {
1809 return int128_get64(mr
->size
);
1812 const char *memory_region_name(const MemoryRegion
*mr
)
1815 ((MemoryRegion
*)mr
)->name
=
1816 object_get_canonical_path_component(OBJECT(mr
));
1821 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1823 return mr
->ram_device
;
1826 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1828 uint8_t mask
= mr
->dirty_log_mask
;
1829 if (global_dirty_log
&& mr
->ram_block
) {
1830 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1835 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1837 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1840 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1842 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1843 IOMMUNotifier
*iommu_notifier
;
1844 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1846 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1847 flags
|= iommu_notifier
->notifier_flags
;
1850 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1851 imrc
->notify_flag_changed(iommu_mr
,
1852 iommu_mr
->iommu_notify_flags
,
1856 iommu_mr
->iommu_notify_flags
= flags
;
1859 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1862 IOMMUMemoryRegion
*iommu_mr
;
1865 memory_region_register_iommu_notifier(mr
->alias
, n
);
1869 /* We need to register for at least one bitfield */
1870 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1871 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1872 assert(n
->start
<= n
->end
);
1873 assert(n
->iommu_idx
>= 0 &&
1874 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1876 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1877 memory_region_update_iommu_notify_flags(iommu_mr
);
1880 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1882 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1884 if (imrc
->get_min_page_size
) {
1885 return imrc
->get_min_page_size(iommu_mr
);
1887 return TARGET_PAGE_SIZE
;
1890 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1892 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1893 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1894 hwaddr addr
, granularity
;
1895 IOMMUTLBEntry iotlb
;
1897 /* If the IOMMU has its own replay callback, override */
1899 imrc
->replay(iommu_mr
, n
);
1903 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1905 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1906 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1907 if (iotlb
.perm
!= IOMMU_NONE
) {
1908 n
->notify(n
, &iotlb
);
1911 /* if (2^64 - MR size) < granularity, it's possible to get an
1912 * infinite loop here. This should catch such a wraparound */
1913 if ((addr
+ granularity
) < addr
) {
1919 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1922 IOMMUMemoryRegion
*iommu_mr
;
1925 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1928 QLIST_REMOVE(n
, node
);
1929 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1930 memory_region_update_iommu_notify_flags(iommu_mr
);
1933 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1934 IOMMUTLBEntry
*entry
)
1936 IOMMUNotifierFlag request_flags
;
1937 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1940 * Skip the notification if the notification does not overlap
1941 * with registered range.
1943 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1947 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1949 if (entry
->perm
& IOMMU_RW
) {
1950 request_flags
= IOMMU_NOTIFIER_MAP
;
1952 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1955 if (notifier
->notifier_flags
& request_flags
) {
1956 notifier
->notify(notifier
, entry
);
1960 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1962 IOMMUTLBEntry entry
)
1964 IOMMUNotifier
*iommu_notifier
;
1966 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1968 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1969 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1970 memory_region_notify_one(iommu_notifier
, &entry
);
1975 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1976 enum IOMMUMemoryRegionAttr attr
,
1979 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1981 if (!imrc
->get_attr
) {
1985 return imrc
->get_attr(iommu_mr
, attr
, data
);
1988 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1991 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1993 if (!imrc
->attrs_to_index
) {
1997 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2000 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2002 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2004 if (!imrc
->num_indexes
) {
2008 return imrc
->num_indexes(iommu_mr
);
2011 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2013 uint8_t mask
= 1 << client
;
2014 uint8_t old_logging
;
2016 assert(client
== DIRTY_MEMORY_VGA
);
2017 old_logging
= mr
->vga_logging_count
;
2018 mr
->vga_logging_count
+= log
? 1 : -1;
2019 if (!!old_logging
== !!mr
->vga_logging_count
) {
2023 memory_region_transaction_begin();
2024 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2025 memory_region_update_pending
|= mr
->enabled
;
2026 memory_region_transaction_commit();
2029 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2032 assert(mr
->ram_block
);
2033 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2035 memory_region_get_dirty_log_mask(mr
));
2038 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2040 MemoryListener
*listener
;
2045 /* If the same address space has multiple log_sync listeners, we
2046 * visit that address space's FlatView multiple times. But because
2047 * log_sync listeners are rare, it's still cheaper than walking each
2048 * address space once.
2050 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2051 if (!listener
->log_sync
) {
2054 as
= listener
->address_space
;
2055 view
= address_space_get_flatview(as
);
2056 FOR_EACH_FLAT_RANGE(fr
, view
) {
2057 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2058 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2059 listener
->log_sync(listener
, &mrs
);
2062 flatview_unref(view
);
2066 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2069 MemoryRegionSection mrs
;
2070 MemoryListener
*listener
;
2074 hwaddr sec_start
, sec_end
, sec_size
;
2076 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2077 if (!listener
->log_clear
) {
2080 as
= listener
->address_space
;
2081 view
= address_space_get_flatview(as
);
2082 FOR_EACH_FLAT_RANGE(fr
, view
) {
2083 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2085 * Clear dirty bitmap operation only applies to those
2086 * regions whose dirty logging is at least enabled
2091 mrs
= section_from_flat_range(fr
, view
);
2093 sec_start
= MAX(mrs
.offset_within_region
, start
);
2094 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2095 sec_end
= MIN(sec_end
, start
+ len
);
2097 if (sec_start
>= sec_end
) {
2099 * If this memory region section has no intersection
2100 * with the requested range, skip.
2105 /* Valid case; shrink the section if needed */
2106 mrs
.offset_within_address_space
+=
2107 sec_start
- mrs
.offset_within_region
;
2108 mrs
.offset_within_region
= sec_start
;
2109 sec_size
= sec_end
- sec_start
;
2110 mrs
.size
= int128_make64(sec_size
);
2111 listener
->log_clear(listener
, &mrs
);
2113 flatview_unref(view
);
2117 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2122 DirtyBitmapSnapshot
*snapshot
;
2123 assert(mr
->ram_block
);
2124 memory_region_sync_dirty_bitmap(mr
);
2125 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2126 memory_global_after_dirty_log_sync();
2130 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2131 hwaddr addr
, hwaddr size
)
2133 assert(mr
->ram_block
);
2134 return cpu_physical_memory_snapshot_get_dirty(snap
,
2135 memory_region_get_ram_addr(mr
) + addr
, size
);
2138 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2140 if (mr
->readonly
!= readonly
) {
2141 memory_region_transaction_begin();
2142 mr
->readonly
= readonly
;
2143 memory_region_update_pending
|= mr
->enabled
;
2144 memory_region_transaction_commit();
2148 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2150 if (mr
->nonvolatile
!= nonvolatile
) {
2151 memory_region_transaction_begin();
2152 mr
->nonvolatile
= nonvolatile
;
2153 memory_region_update_pending
|= mr
->enabled
;
2154 memory_region_transaction_commit();
2158 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2160 if (mr
->romd_mode
!= romd_mode
) {
2161 memory_region_transaction_begin();
2162 mr
->romd_mode
= romd_mode
;
2163 memory_region_update_pending
|= mr
->enabled
;
2164 memory_region_transaction_commit();
2168 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2169 hwaddr size
, unsigned client
)
2171 assert(mr
->ram_block
);
2172 cpu_physical_memory_test_and_clear_dirty(
2173 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2176 int memory_region_get_fd(MemoryRegion
*mr
)
2184 fd
= mr
->ram_block
->fd
;
2190 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2193 uint64_t offset
= 0;
2197 offset
+= mr
->alias_offset
;
2200 assert(mr
->ram_block
);
2201 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2207 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2211 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2219 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2221 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2224 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2226 assert(mr
->ram_block
);
2228 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2232 * Call proper memory listeners about the change on the newly
2233 * added/removed CoalescedMemoryRange.
2235 static void memory_region_update_coalesced_range(MemoryRegion
*mr
,
2236 CoalescedMemoryRange
*cmr
,
2243 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2244 view
= address_space_get_flatview(as
);
2245 FOR_EACH_FLAT_RANGE(fr
, view
) {
2247 flat_range_coalesced_io_notify(fr
, as
, cmr
, add
);
2250 flatview_unref(view
);
2254 void memory_region_set_coalescing(MemoryRegion
*mr
)
2256 memory_region_clear_coalescing(mr
);
2257 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2260 void memory_region_add_coalescing(MemoryRegion
*mr
,
2264 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2266 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2267 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2268 memory_region_update_coalesced_range(mr
, cmr
, true);
2269 memory_region_set_flush_coalesced(mr
);
2272 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2274 CoalescedMemoryRange
*cmr
;
2276 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2280 qemu_flush_coalesced_mmio_buffer();
2281 mr
->flush_coalesced_mmio
= false;
2283 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2284 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2285 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2286 memory_region_update_coalesced_range(mr
, cmr
, false);
2291 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2293 mr
->flush_coalesced_mmio
= true;
2296 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2298 qemu_flush_coalesced_mmio_buffer();
2299 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2300 mr
->flush_coalesced_mmio
= false;
2304 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2306 mr
->global_locking
= false;
2309 static bool userspace_eventfd_warning
;
2311 void memory_region_add_eventfd(MemoryRegion
*mr
,
2318 MemoryRegionIoeventfd mrfd
= {
2319 .addr
.start
= int128_make64(addr
),
2320 .addr
.size
= int128_make64(size
),
2321 .match_data
= match_data
,
2327 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2328 userspace_eventfd_warning
))) {
2329 userspace_eventfd_warning
= true;
2330 error_report("Using eventfd without MMIO binding in KVM. "
2331 "Suboptimal performance expected");
2335 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2337 memory_region_transaction_begin();
2338 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2339 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2344 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2345 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2346 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2347 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2348 mr
->ioeventfds
[i
] = mrfd
;
2349 ioeventfd_update_pending
|= mr
->enabled
;
2350 memory_region_transaction_commit();
2353 void memory_region_del_eventfd(MemoryRegion
*mr
,
2360 MemoryRegionIoeventfd mrfd
= {
2361 .addr
.start
= int128_make64(addr
),
2362 .addr
.size
= int128_make64(size
),
2363 .match_data
= match_data
,
2370 adjust_endianness(mr
, &mrfd
.data
, size_memop(size
) | MO_TE
);
2372 memory_region_transaction_begin();
2373 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2374 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2378 assert(i
!= mr
->ioeventfd_nb
);
2379 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2380 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2382 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2383 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2384 ioeventfd_update_pending
|= mr
->enabled
;
2385 memory_region_transaction_commit();
2388 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2390 MemoryRegion
*mr
= subregion
->container
;
2391 MemoryRegion
*other
;
2393 memory_region_transaction_begin();
2395 memory_region_ref(subregion
);
2396 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2397 if (subregion
->priority
>= other
->priority
) {
2398 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2402 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2404 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2405 memory_region_transaction_commit();
2408 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2410 MemoryRegion
*subregion
)
2412 assert(!subregion
->container
);
2413 subregion
->container
= mr
;
2414 subregion
->addr
= offset
;
2415 memory_region_update_container_subregions(subregion
);
2418 void memory_region_add_subregion(MemoryRegion
*mr
,
2420 MemoryRegion
*subregion
)
2422 subregion
->priority
= 0;
2423 memory_region_add_subregion_common(mr
, offset
, subregion
);
2426 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2428 MemoryRegion
*subregion
,
2431 subregion
->priority
= priority
;
2432 memory_region_add_subregion_common(mr
, offset
, subregion
);
2435 void memory_region_del_subregion(MemoryRegion
*mr
,
2436 MemoryRegion
*subregion
)
2438 memory_region_transaction_begin();
2439 assert(subregion
->container
== mr
);
2440 subregion
->container
= NULL
;
2441 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2442 memory_region_unref(subregion
);
2443 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2444 memory_region_transaction_commit();
2447 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2449 if (enabled
== mr
->enabled
) {
2452 memory_region_transaction_begin();
2453 mr
->enabled
= enabled
;
2454 memory_region_update_pending
= true;
2455 memory_region_transaction_commit();
2458 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2460 Int128 s
= int128_make64(size
);
2462 if (size
== UINT64_MAX
) {
2465 if (int128_eq(s
, mr
->size
)) {
2468 memory_region_transaction_begin();
2470 memory_region_update_pending
= true;
2471 memory_region_transaction_commit();
2474 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2476 MemoryRegion
*container
= mr
->container
;
2479 memory_region_transaction_begin();
2480 memory_region_ref(mr
);
2481 memory_region_del_subregion(container
, mr
);
2482 mr
->container
= container
;
2483 memory_region_update_container_subregions(mr
);
2484 memory_region_unref(mr
);
2485 memory_region_transaction_commit();
2489 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2491 if (addr
!= mr
->addr
) {
2493 memory_region_readd_subregion(mr
);
2497 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2501 if (offset
== mr
->alias_offset
) {
2505 memory_region_transaction_begin();
2506 mr
->alias_offset
= offset
;
2507 memory_region_update_pending
|= mr
->enabled
;
2508 memory_region_transaction_commit();
2511 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2516 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2518 const AddrRange
*addr
= addr_
;
2519 const FlatRange
*fr
= fr_
;
2521 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2523 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2529 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2531 return bsearch(&addr
, view
->ranges
, view
->nr
,
2532 sizeof(FlatRange
), cmp_flatrange_addr
);
2535 bool memory_region_is_mapped(MemoryRegion
*mr
)
2537 return mr
->container
? true : false;
2540 /* Same as memory_region_find, but it does not add a reference to the
2541 * returned region. It must be called from an RCU critical section.
2543 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2544 hwaddr addr
, uint64_t size
)
2546 MemoryRegionSection ret
= { .mr
= NULL
};
2554 for (root
= mr
; root
->container
; ) {
2555 root
= root
->container
;
2559 as
= memory_region_to_address_space(root
);
2563 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2565 view
= address_space_to_flatview(as
);
2566 fr
= flatview_lookup(view
, range
);
2571 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2577 range
= addrrange_intersection(range
, fr
->addr
);
2578 ret
.offset_within_region
= fr
->offset_in_region
;
2579 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2581 ret
.size
= range
.size
;
2582 ret
.offset_within_address_space
= int128_get64(range
.start
);
2583 ret
.readonly
= fr
->readonly
;
2584 ret
.nonvolatile
= fr
->nonvolatile
;
2588 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2589 hwaddr addr
, uint64_t size
)
2591 MemoryRegionSection ret
;
2593 ret
= memory_region_find_rcu(mr
, addr
, size
);
2595 memory_region_ref(ret
.mr
);
2601 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2606 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2608 return mr
&& mr
!= container
;
2611 void memory_global_dirty_log_sync(void)
2613 memory_region_sync_dirty_bitmap(NULL
);
2616 void memory_global_after_dirty_log_sync(void)
2618 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2621 static VMChangeStateEntry
*vmstate_change
;
2623 void memory_global_dirty_log_start(void)
2625 if (vmstate_change
) {
2626 qemu_del_vm_change_state_handler(vmstate_change
);
2627 vmstate_change
= NULL
;
2630 global_dirty_log
= true;
2632 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2634 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2635 memory_region_transaction_begin();
2636 memory_region_update_pending
= true;
2637 memory_region_transaction_commit();
2640 static void memory_global_dirty_log_do_stop(void)
2642 global_dirty_log
= false;
2644 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2645 memory_region_transaction_begin();
2646 memory_region_update_pending
= true;
2647 memory_region_transaction_commit();
2649 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2652 static void memory_vm_change_state_handler(void *opaque
, int running
,
2656 memory_global_dirty_log_do_stop();
2658 if (vmstate_change
) {
2659 qemu_del_vm_change_state_handler(vmstate_change
);
2660 vmstate_change
= NULL
;
2665 void memory_global_dirty_log_stop(void)
2667 if (!runstate_is_running()) {
2668 if (vmstate_change
) {
2671 vmstate_change
= qemu_add_vm_change_state_handler(
2672 memory_vm_change_state_handler
, NULL
);
2676 memory_global_dirty_log_do_stop();
2679 static void listener_add_address_space(MemoryListener
*listener
,
2685 if (listener
->begin
) {
2686 listener
->begin(listener
);
2688 if (global_dirty_log
) {
2689 if (listener
->log_global_start
) {
2690 listener
->log_global_start(listener
);
2694 view
= address_space_get_flatview(as
);
2695 FOR_EACH_FLAT_RANGE(fr
, view
) {
2696 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2698 if (listener
->region_add
) {
2699 listener
->region_add(listener
, §ion
);
2701 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2702 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2705 if (listener
->commit
) {
2706 listener
->commit(listener
);
2708 flatview_unref(view
);
2711 static void listener_del_address_space(MemoryListener
*listener
,
2717 if (listener
->begin
) {
2718 listener
->begin(listener
);
2720 view
= address_space_get_flatview(as
);
2721 FOR_EACH_FLAT_RANGE(fr
, view
) {
2722 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2724 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2725 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2727 if (listener
->region_del
) {
2728 listener
->region_del(listener
, §ion
);
2731 if (listener
->commit
) {
2732 listener
->commit(listener
);
2734 flatview_unref(view
);
2737 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2739 MemoryListener
*other
= NULL
;
2741 listener
->address_space
= as
;
2742 if (QTAILQ_EMPTY(&memory_listeners
)
2743 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2744 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2746 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2747 if (listener
->priority
< other
->priority
) {
2751 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2754 if (QTAILQ_EMPTY(&as
->listeners
)
2755 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2756 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2758 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2759 if (listener
->priority
< other
->priority
) {
2763 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2766 listener_add_address_space(listener
, as
);
2769 void memory_listener_unregister(MemoryListener
*listener
)
2771 if (!listener
->address_space
) {
2775 listener_del_address_space(listener
, listener
->address_space
);
2776 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2777 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2778 listener
->address_space
= NULL
;
2781 void address_space_remove_listeners(AddressSpace
*as
)
2783 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2784 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2788 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2790 memory_region_ref(root
);
2792 as
->current_map
= NULL
;
2793 as
->ioeventfd_nb
= 0;
2794 as
->ioeventfds
= NULL
;
2795 QTAILQ_INIT(&as
->listeners
);
2796 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2797 as
->name
= g_strdup(name
? name
: "anonymous");
2798 address_space_update_topology(as
);
2799 address_space_update_ioeventfds(as
);
2802 static void do_address_space_destroy(AddressSpace
*as
)
2804 assert(QTAILQ_EMPTY(&as
->listeners
));
2806 flatview_unref(as
->current_map
);
2808 g_free(as
->ioeventfds
);
2809 memory_region_unref(as
->root
);
2812 void address_space_destroy(AddressSpace
*as
)
2814 MemoryRegion
*root
= as
->root
;
2816 /* Flush out anything from MemoryListeners listening in on this */
2817 memory_region_transaction_begin();
2819 memory_region_transaction_commit();
2820 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2822 /* At this point, as->dispatch and as->current_map are dummy
2823 * entries that the guest should never use. Wait for the old
2824 * values to expire before freeing the data.
2827 call_rcu(as
, do_address_space_destroy
, rcu
);
2830 static const char *memory_region_type(MemoryRegion
*mr
)
2832 if (memory_region_is_ram_device(mr
)) {
2834 } else if (memory_region_is_romd(mr
)) {
2836 } else if (memory_region_is_rom(mr
)) {
2838 } else if (memory_region_is_ram(mr
)) {
2845 typedef struct MemoryRegionList MemoryRegionList
;
2847 struct MemoryRegionList
{
2848 const MemoryRegion
*mr
;
2849 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2852 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2854 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2855 int128_sub((size), int128_one())) : 0)
2856 #define MTREE_INDENT " "
2858 static void mtree_expand_owner(const char *label
, Object
*obj
)
2860 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
2862 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
2863 if (dev
&& dev
->id
) {
2864 qemu_printf(" id=%s", dev
->id
);
2866 gchar
*canonical_path
= object_get_canonical_path(obj
);
2867 if (canonical_path
) {
2868 qemu_printf(" path=%s", canonical_path
);
2869 g_free(canonical_path
);
2871 qemu_printf(" type=%s", object_get_typename(obj
));
2877 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
2879 Object
*owner
= mr
->owner
;
2880 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
2882 if (!owner
&& !parent
) {
2883 qemu_printf(" orphan");
2887 mtree_expand_owner("owner", owner
);
2889 if (parent
&& parent
!= owner
) {
2890 mtree_expand_owner("parent", parent
);
2894 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
2896 MemoryRegionListHead
*alias_print_queue
,
2899 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2900 MemoryRegionListHead submr_print_queue
;
2901 const MemoryRegion
*submr
;
2903 hwaddr cur_start
, cur_end
;
2909 for (i
= 0; i
< level
; i
++) {
2910 qemu_printf(MTREE_INDENT
);
2913 cur_start
= base
+ mr
->addr
;
2914 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2917 * Try to detect overflow of memory region. This should never
2918 * happen normally. When it happens, we dump something to warn the
2919 * user who is observing this.
2921 if (cur_start
< base
|| cur_end
< cur_start
) {
2922 qemu_printf("[DETECTED OVERFLOW!] ");
2926 MemoryRegionList
*ml
;
2929 /* check if the alias is already in the queue */
2930 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2931 if (ml
->mr
== mr
->alias
) {
2937 ml
= g_new(MemoryRegionList
, 1);
2939 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2941 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2942 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2943 "-" TARGET_FMT_plx
"%s",
2946 mr
->nonvolatile
? "nv-" : "",
2947 memory_region_type((MemoryRegion
*)mr
),
2948 memory_region_name(mr
),
2949 memory_region_name(mr
->alias
),
2951 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2952 mr
->enabled
? "" : " [disabled]");
2954 mtree_print_mr_owner(mr
);
2957 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2958 " (prio %d, %s%s): %s%s",
2961 mr
->nonvolatile
? "nv-" : "",
2962 memory_region_type((MemoryRegion
*)mr
),
2963 memory_region_name(mr
),
2964 mr
->enabled
? "" : " [disabled]");
2966 mtree_print_mr_owner(mr
);
2971 QTAILQ_INIT(&submr_print_queue
);
2973 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2974 new_ml
= g_new(MemoryRegionList
, 1);
2976 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2977 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2978 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2979 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2980 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
2986 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
2990 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2991 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
2992 alias_print_queue
, owner
);
2995 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3000 struct FlatViewInfo
{
3005 const char *ac_name
;
3008 static void mtree_print_flatview(gpointer key
, gpointer value
,
3011 FlatView
*view
= key
;
3012 GArray
*fv_address_spaces
= value
;
3013 struct FlatViewInfo
*fvi
= user_data
;
3014 FlatRange
*range
= &view
->ranges
[0];
3020 qemu_printf("FlatView #%d\n", fvi
->counter
);
3023 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3024 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3025 qemu_printf(" AS \"%s\", root: %s",
3026 as
->name
, memory_region_name(as
->root
));
3027 if (as
->root
->alias
) {
3028 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3033 qemu_printf(" Root memory region: %s\n",
3034 view
->root
? memory_region_name(view
->root
) : "(none)");
3037 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3043 if (range
->offset_in_region
) {
3044 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3045 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3046 int128_get64(range
->addr
.start
),
3047 int128_get64(range
->addr
.start
)
3048 + MR_SIZE(range
->addr
.size
),
3050 range
->nonvolatile
? "nv-" : "",
3051 range
->readonly
? "rom" : memory_region_type(mr
),
3052 memory_region_name(mr
),
3053 range
->offset_in_region
);
3055 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3056 " (prio %d, %s%s): %s",
3057 int128_get64(range
->addr
.start
),
3058 int128_get64(range
->addr
.start
)
3059 + MR_SIZE(range
->addr
.size
),
3061 range
->nonvolatile
? "nv-" : "",
3062 range
->readonly
? "rom" : memory_region_type(mr
),
3063 memory_region_name(mr
));
3066 mtree_print_mr_owner(mr
);
3070 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3071 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3072 if (fvi
->ac
->has_memory(current_machine
, as
,
3073 int128_get64(range
->addr
.start
),
3074 MR_SIZE(range
->addr
.size
) + 1)) {
3075 qemu_printf(" %s", fvi
->ac_name
);
3083 #if !defined(CONFIG_USER_ONLY)
3084 if (fvi
->dispatch_tree
&& view
->root
) {
3085 mtree_print_dispatch(view
->dispatch
, view
->root
);
3092 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3095 FlatView
*view
= key
;
3096 GArray
*fv_address_spaces
= value
;
3098 g_array_unref(fv_address_spaces
);
3099 flatview_unref(view
);
3104 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
)
3106 MemoryRegionListHead ml_head
;
3107 MemoryRegionList
*ml
, *ml2
;
3112 struct FlatViewInfo fvi
= {
3114 .dispatch_tree
= dispatch_tree
,
3117 GArray
*fv_address_spaces
;
3118 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3119 AccelClass
*ac
= ACCEL_GET_CLASS(current_machine
->accelerator
);
3121 if (ac
->has_memory
) {
3123 fvi
.ac_name
= current_machine
->accel
? current_machine
->accel
:
3124 object_class_get_name(OBJECT_CLASS(ac
));
3127 /* Gather all FVs in one table */
3128 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3129 view
= address_space_get_flatview(as
);
3131 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3132 if (!fv_address_spaces
) {
3133 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3134 g_hash_table_insert(views
, view
, fv_address_spaces
);
3137 g_array_append_val(fv_address_spaces
, as
);
3141 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3144 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3145 g_hash_table_unref(views
);
3150 QTAILQ_INIT(&ml_head
);
3152 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3153 qemu_printf("address-space: %s\n", as
->name
);
3154 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
);
3158 /* print aliased regions */
3159 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3160 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3161 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
);
3165 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3170 void memory_region_init_ram(MemoryRegion
*mr
,
3171 struct Object
*owner
,
3176 DeviceState
*owner_dev
;
3179 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3181 error_propagate(errp
, err
);
3184 /* This will assert if owner is neither NULL nor a DeviceState.
3185 * We only want the owner here for the purposes of defining a
3186 * unique name for migration. TODO: Ideally we should implement
3187 * a naming scheme for Objects which are not DeviceStates, in
3188 * which case we can relax this restriction.
3190 owner_dev
= DEVICE(owner
);
3191 vmstate_register_ram(mr
, owner_dev
);
3194 void memory_region_init_rom(MemoryRegion
*mr
,
3195 struct Object
*owner
,
3200 DeviceState
*owner_dev
;
3203 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3205 error_propagate(errp
, err
);
3208 /* This will assert if owner is neither NULL nor a DeviceState.
3209 * We only want the owner here for the purposes of defining a
3210 * unique name for migration. TODO: Ideally we should implement
3211 * a naming scheme for Objects which are not DeviceStates, in
3212 * which case we can relax this restriction.
3214 owner_dev
= DEVICE(owner
);
3215 vmstate_register_ram(mr
, owner_dev
);
3218 void memory_region_init_rom_device(MemoryRegion
*mr
,
3219 struct Object
*owner
,
3220 const MemoryRegionOps
*ops
,
3226 DeviceState
*owner_dev
;
3229 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3232 error_propagate(errp
, err
);
3235 /* This will assert if owner is neither NULL nor a DeviceState.
3236 * We only want the owner here for the purposes of defining a
3237 * unique name for migration. TODO: Ideally we should implement
3238 * a naming scheme for Objects which are not DeviceStates, in
3239 * which case we can relax this restriction.
3241 owner_dev
= DEVICE(owner
);
3242 vmstate_register_ram(mr
, owner_dev
);
3245 static const TypeInfo memory_region_info
= {
3246 .parent
= TYPE_OBJECT
,
3247 .name
= TYPE_MEMORY_REGION
,
3248 .class_size
= sizeof(MemoryRegionClass
),
3249 .instance_size
= sizeof(MemoryRegion
),
3250 .instance_init
= memory_region_initfn
,
3251 .instance_finalize
= memory_region_finalize
,
3254 static const TypeInfo iommu_memory_region_info
= {
3255 .parent
= TYPE_MEMORY_REGION
,
3256 .name
= TYPE_IOMMU_MEMORY_REGION
,
3257 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3258 .instance_size
= sizeof(IOMMUMemoryRegion
),
3259 .instance_init
= iommu_memory_region_initfn
,
3263 static void memory_register_types(void)
3265 type_register_static(&memory_region_info
);
3266 type_register_static(&iommu_memory_region_info
);
3269 type_init(memory_register_types
)
3271 MemOp
devend_memop(enum device_endian end
)
3273 static MemOp conv
[] = {
3274 [DEVICE_LITTLE_ENDIAN
] = MO_LE
,
3275 [DEVICE_BIG_ENDIAN
] = MO_BE
,
3276 [DEVICE_NATIVE_ENDIAN
] = MO_TE
,
3277 [DEVICE_HOST_ENDIAN
] = 0,
3280 case DEVICE_LITTLE_ENDIAN
:
3281 case DEVICE_BIG_ENDIAN
:
3282 case DEVICE_NATIVE_ENDIAN
:
3285 g_assert_not_reached();