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
;
220 int has_coalesced_range
;
223 #define FOR_EACH_FLAT_RANGE(var, view) \
224 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
226 static inline MemoryRegionSection
227 section_from_flat_range(FlatRange
*fr
, FlatView
*fv
)
229 return (MemoryRegionSection
) {
232 .offset_within_region
= fr
->offset_in_region
,
233 .size
= fr
->addr
.size
,
234 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
235 .readonly
= fr
->readonly
,
236 .nonvolatile
= fr
->nonvolatile
,
240 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
242 return a
->mr
== b
->mr
243 && addrrange_equal(a
->addr
, b
->addr
)
244 && a
->offset_in_region
== b
->offset_in_region
245 && a
->romd_mode
== b
->romd_mode
246 && a
->readonly
== b
->readonly
247 && a
->nonvolatile
== b
->nonvolatile
;
250 static FlatView
*flatview_new(MemoryRegion
*mr_root
)
254 view
= g_new0(FlatView
, 1);
256 view
->root
= mr_root
;
257 memory_region_ref(mr_root
);
258 trace_flatview_new(view
, mr_root
);
263 /* Insert a range into a given position. Caller is responsible for maintaining
266 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
268 if (view
->nr
== view
->nr_allocated
) {
269 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
270 view
->ranges
= g_realloc(view
->ranges
,
271 view
->nr_allocated
* sizeof(*view
->ranges
));
273 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
274 (view
->nr
- pos
) * sizeof(FlatRange
));
275 view
->ranges
[pos
] = *range
;
276 memory_region_ref(range
->mr
);
280 static void flatview_destroy(FlatView
*view
)
284 trace_flatview_destroy(view
, view
->root
);
285 if (view
->dispatch
) {
286 address_space_dispatch_free(view
->dispatch
);
288 for (i
= 0; i
< view
->nr
; i
++) {
289 memory_region_unref(view
->ranges
[i
].mr
);
291 g_free(view
->ranges
);
292 memory_region_unref(view
->root
);
296 static bool flatview_ref(FlatView
*view
)
298 return atomic_fetch_inc_nonzero(&view
->ref
) > 0;
301 void flatview_unref(FlatView
*view
)
303 if (atomic_fetch_dec(&view
->ref
) == 1) {
304 trace_flatview_destroy_rcu(view
, view
->root
);
306 call_rcu(view
, flatview_destroy
, rcu
);
310 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
312 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
314 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
316 int128_make64(r2
->offset_in_region
))
317 && r1
->dirty_log_mask
== r2
->dirty_log_mask
318 && r1
->romd_mode
== r2
->romd_mode
319 && r1
->readonly
== r2
->readonly
320 && r1
->nonvolatile
== r2
->nonvolatile
;
323 /* Attempt to simplify a view by merging adjacent ranges */
324 static void flatview_simplify(FlatView
*view
)
329 while (i
< view
->nr
) {
332 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
333 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
337 for (k
= i
; k
< j
; k
++) {
338 memory_region_unref(view
->ranges
[k
].mr
);
340 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
341 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
346 static bool memory_region_big_endian(MemoryRegion
*mr
)
348 #ifdef TARGET_WORDS_BIGENDIAN
349 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
351 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
355 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
357 #ifdef TARGET_WORDS_BIGENDIAN
358 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
360 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
364 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
366 if (memory_region_wrong_endianness(mr
)) {
371 *data
= bswap16(*data
);
374 *data
= bswap32(*data
);
377 *data
= bswap64(*data
);
385 static inline void memory_region_shift_read_access(uint64_t *value
,
391 *value
|= (tmp
& mask
) << shift
;
393 *value
|= (tmp
& mask
) >> -shift
;
397 static inline uint64_t memory_region_shift_write_access(uint64_t *value
,
404 tmp
= (*value
>> shift
) & mask
;
406 tmp
= (*value
<< -shift
) & mask
;
412 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
415 hwaddr abs_addr
= offset
;
417 abs_addr
+= mr
->addr
;
418 for (root
= mr
; root
->container
; ) {
419 root
= root
->container
;
420 abs_addr
+= root
->addr
;
426 static int get_cpu_index(void)
429 return current_cpu
->cpu_index
;
434 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
444 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
446 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
447 } else if (mr
== &io_mem_notdirty
) {
448 /* Accesses to code which has previously been translated into a TB show
449 * up in the MMIO path, as accesses to the io_mem_notdirty
451 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
452 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
453 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
454 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
456 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
460 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
471 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
473 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
474 } else if (mr
== &io_mem_notdirty
) {
475 /* Accesses to code which has previously been translated into a TB show
476 * up in the MMIO path, as accesses to the io_mem_notdirty
478 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
479 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
480 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
481 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
483 memory_region_shift_read_access(value
, shift
, mask
, tmp
);
487 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
495 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
498 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
499 } else if (mr
== &io_mem_notdirty
) {
500 /* Accesses to code which has previously been translated into a TB show
501 * up in the MMIO path, as accesses to the io_mem_notdirty
503 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
504 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
505 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
506 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
508 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
512 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
520 uint64_t tmp
= memory_region_shift_write_access(value
, shift
, mask
);
523 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
524 } else if (mr
== &io_mem_notdirty
) {
525 /* Accesses to code which has previously been translated into a TB show
526 * up in the MMIO path, as accesses to the io_mem_notdirty
528 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
529 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
530 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
531 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
533 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
536 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
539 unsigned access_size_min
,
540 unsigned access_size_max
,
541 MemTxResult (*access_fn
)
552 uint64_t access_mask
;
553 unsigned access_size
;
555 MemTxResult r
= MEMTX_OK
;
557 if (!access_size_min
) {
560 if (!access_size_max
) {
564 /* FIXME: support unaligned access? */
565 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
566 access_mask
= MAKE_64BIT_MASK(0, access_size
* 8);
567 if (memory_region_big_endian(mr
)) {
568 for (i
= 0; i
< size
; i
+= access_size
) {
569 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
570 (size
- access_size
- i
) * 8, access_mask
, attrs
);
573 for (i
= 0; i
< size
; i
+= access_size
) {
574 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
581 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
585 while (mr
->container
) {
588 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
589 if (mr
== as
->root
) {
596 /* Render a memory region into the global view. Ranges in @view obscure
599 static void render_memory_region(FlatView
*view
,
606 MemoryRegion
*subregion
;
608 hwaddr offset_in_region
;
618 int128_addto(&base
, int128_make64(mr
->addr
));
619 readonly
|= mr
->readonly
;
620 nonvolatile
|= mr
->nonvolatile
;
622 tmp
= addrrange_make(base
, mr
->size
);
624 if (!addrrange_intersects(tmp
, clip
)) {
628 clip
= addrrange_intersection(tmp
, clip
);
631 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
632 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
633 render_memory_region(view
, mr
->alias
, base
, clip
,
634 readonly
, nonvolatile
);
638 /* Render subregions in priority order. */
639 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
640 render_memory_region(view
, subregion
, base
, clip
,
641 readonly
, nonvolatile
);
644 if (!mr
->terminates
) {
648 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
653 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
654 fr
.romd_mode
= mr
->romd_mode
;
655 fr
.readonly
= readonly
;
656 fr
.nonvolatile
= nonvolatile
;
657 fr
.has_coalesced_range
= 0;
659 /* Render the region itself into any gaps left by the current view. */
660 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
661 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
664 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
665 now
= int128_min(remain
,
666 int128_sub(view
->ranges
[i
].addr
.start
, base
));
667 fr
.offset_in_region
= offset_in_region
;
668 fr
.addr
= addrrange_make(base
, now
);
669 flatview_insert(view
, i
, &fr
);
671 int128_addto(&base
, now
);
672 offset_in_region
+= int128_get64(now
);
673 int128_subfrom(&remain
, now
);
675 now
= int128_sub(int128_min(int128_add(base
, remain
),
676 addrrange_end(view
->ranges
[i
].addr
)),
678 int128_addto(&base
, now
);
679 offset_in_region
+= int128_get64(now
);
680 int128_subfrom(&remain
, now
);
682 if (int128_nz(remain
)) {
683 fr
.offset_in_region
= offset_in_region
;
684 fr
.addr
= addrrange_make(base
, remain
);
685 flatview_insert(view
, i
, &fr
);
689 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
691 while (mr
->enabled
) {
693 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
694 /* The alias is included in its entirety. Use it as
695 * the "real" root, so that we can share more FlatViews.
700 } else if (!mr
->terminates
) {
701 unsigned int found
= 0;
702 MemoryRegion
*child
, *next
= NULL
;
703 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
704 if (child
->enabled
) {
709 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
710 /* A child is included in its entirety. If it's the only
711 * enabled one, use it in the hope of finding an alias down the
712 * way. This will also let us share FlatViews.
733 /* Render a memory topology into a list of disjoint absolute ranges. */
734 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
739 view
= flatview_new(mr
);
742 render_memory_region(view
, mr
, int128_zero(),
743 addrrange_make(int128_zero(), int128_2_64()),
746 flatview_simplify(view
);
748 view
->dispatch
= address_space_dispatch_new(view
);
749 for (i
= 0; i
< view
->nr
; i
++) {
750 MemoryRegionSection mrs
=
751 section_from_flat_range(&view
->ranges
[i
], view
);
752 flatview_add_to_dispatch(view
, &mrs
);
754 address_space_dispatch_compact(view
->dispatch
);
755 g_hash_table_replace(flat_views
, mr
, view
);
760 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
761 MemoryRegionIoeventfd
*fds_new
,
763 MemoryRegionIoeventfd
*fds_old
,
767 MemoryRegionIoeventfd
*fd
;
768 MemoryRegionSection section
;
770 /* Generate a symmetric difference of the old and new fd sets, adding
771 * and deleting as necessary.
775 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
776 if (iold
< fds_old_nb
777 && (inew
== fds_new_nb
778 || memory_region_ioeventfd_before(&fds_old
[iold
],
781 section
= (MemoryRegionSection
) {
782 .fv
= address_space_to_flatview(as
),
783 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
784 .size
= fd
->addr
.size
,
786 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
787 fd
->match_data
, fd
->data
, fd
->e
);
789 } else if (inew
< fds_new_nb
790 && (iold
== fds_old_nb
791 || memory_region_ioeventfd_before(&fds_new
[inew
],
794 section
= (MemoryRegionSection
) {
795 .fv
= address_space_to_flatview(as
),
796 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
797 .size
= fd
->addr
.size
,
799 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
800 fd
->match_data
, fd
->data
, fd
->e
);
809 FlatView
*address_space_get_flatview(AddressSpace
*as
)
815 view
= address_space_to_flatview(as
);
816 /* If somebody has replaced as->current_map concurrently,
817 * flatview_ref returns false.
819 } while (!flatview_ref(view
));
824 static void address_space_update_ioeventfds(AddressSpace
*as
)
828 unsigned ioeventfd_nb
= 0;
829 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
833 view
= address_space_get_flatview(as
);
834 FOR_EACH_FLAT_RANGE(fr
, view
) {
835 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
836 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
837 int128_sub(fr
->addr
.start
,
838 int128_make64(fr
->offset_in_region
)));
839 if (addrrange_intersects(fr
->addr
, tmp
)) {
841 ioeventfds
= g_realloc(ioeventfds
,
842 ioeventfd_nb
* sizeof(*ioeventfds
));
843 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
844 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
849 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
850 as
->ioeventfds
, as
->ioeventfd_nb
);
852 g_free(as
->ioeventfds
);
853 as
->ioeventfds
= ioeventfds
;
854 as
->ioeventfd_nb
= ioeventfd_nb
;
855 flatview_unref(view
);
858 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
860 if (!fr
->has_coalesced_range
) {
864 if (--fr
->has_coalesced_range
> 0) {
868 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
869 int128_get64(fr
->addr
.start
),
870 int128_get64(fr
->addr
.size
));
873 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
875 MemoryRegion
*mr
= fr
->mr
;
876 CoalescedMemoryRange
*cmr
;
879 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
883 if (fr
->has_coalesced_range
++) {
887 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
888 tmp
= addrrange_shift(cmr
->addr
,
889 int128_sub(fr
->addr
.start
,
890 int128_make64(fr
->offset_in_region
)));
891 if (!addrrange_intersects(tmp
, fr
->addr
)) {
894 tmp
= addrrange_intersection(tmp
, fr
->addr
);
895 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
896 int128_get64(tmp
.start
),
897 int128_get64(tmp
.size
));
901 static void address_space_update_topology_pass(AddressSpace
*as
,
902 const FlatView
*old_view
,
903 const FlatView
*new_view
,
907 FlatRange
*frold
, *frnew
;
909 /* Generate a symmetric difference of the old and new memory maps.
910 * Kill ranges in the old map, and instantiate ranges in the new map.
913 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
914 if (iold
< old_view
->nr
) {
915 frold
= &old_view
->ranges
[iold
];
919 if (inew
< new_view
->nr
) {
920 frnew
= &new_view
->ranges
[inew
];
927 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
928 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
929 && !flatrange_equal(frold
, frnew
)))) {
930 /* In old but not in new, or in both but attributes changed. */
933 flat_range_coalesced_io_del(frold
, as
);
934 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
938 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
939 /* In both and unchanged (except logging may have changed) */
942 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
943 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
944 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
945 frold
->dirty_log_mask
,
946 frnew
->dirty_log_mask
);
948 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
949 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
950 frold
->dirty_log_mask
,
951 frnew
->dirty_log_mask
);
961 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
962 flat_range_coalesced_io_add(frnew
, as
);
970 static void flatviews_init(void)
972 static FlatView
*empty_view
;
978 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
979 (GDestroyNotify
) flatview_unref
);
981 empty_view
= generate_memory_topology(NULL
);
982 /* We keep it alive forever in the global variable. */
983 flatview_ref(empty_view
);
985 g_hash_table_replace(flat_views
, NULL
, empty_view
);
986 flatview_ref(empty_view
);
990 static void flatviews_reset(void)
995 g_hash_table_unref(flat_views
);
1000 /* Render unique FVs */
1001 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1002 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1004 if (g_hash_table_lookup(flat_views
, physmr
)) {
1008 generate_memory_topology(physmr
);
1012 static void address_space_set_flatview(AddressSpace
*as
)
1014 FlatView
*old_view
= address_space_to_flatview(as
);
1015 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1016 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1020 if (old_view
== new_view
) {
1025 flatview_ref(old_view
);
1028 flatview_ref(new_view
);
1030 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1031 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1034 old_view2
= &tmpview
;
1036 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1037 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1040 /* Writes are protected by the BQL. */
1041 atomic_rcu_set(&as
->current_map
, new_view
);
1043 flatview_unref(old_view
);
1046 /* Note that all the old MemoryRegions are still alive up to this
1047 * point. This relieves most MemoryListeners from the need to
1048 * ref/unref the MemoryRegions they get---unless they use them
1049 * outside the iothread mutex, in which case precise reference
1050 * counting is necessary.
1053 flatview_unref(old_view
);
1057 static void address_space_update_topology(AddressSpace
*as
)
1059 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1062 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1063 generate_memory_topology(physmr
);
1065 address_space_set_flatview(as
);
1068 void memory_region_transaction_begin(void)
1070 qemu_flush_coalesced_mmio_buffer();
1071 ++memory_region_transaction_depth
;
1074 void memory_region_transaction_commit(void)
1078 assert(memory_region_transaction_depth
);
1079 assert(qemu_mutex_iothread_locked());
1081 --memory_region_transaction_depth
;
1082 if (!memory_region_transaction_depth
) {
1083 if (memory_region_update_pending
) {
1086 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1088 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1089 address_space_set_flatview(as
);
1090 address_space_update_ioeventfds(as
);
1092 memory_region_update_pending
= false;
1093 ioeventfd_update_pending
= false;
1094 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1095 } else if (ioeventfd_update_pending
) {
1096 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1097 address_space_update_ioeventfds(as
);
1099 ioeventfd_update_pending
= false;
1104 static void memory_region_destructor_none(MemoryRegion
*mr
)
1108 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1110 qemu_ram_free(mr
->ram_block
);
1113 static bool memory_region_need_escape(char c
)
1115 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1118 static char *memory_region_escape_name(const char *name
)
1125 for (p
= name
; *p
; p
++) {
1126 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1128 if (bytes
== p
- name
) {
1129 return g_memdup(name
, bytes
+ 1);
1132 escaped
= g_malloc(bytes
+ 1);
1133 for (p
= name
, q
= escaped
; *p
; p
++) {
1135 if (unlikely(memory_region_need_escape(c
))) {
1138 *q
++ = "0123456789abcdef"[c
>> 4];
1139 c
= "0123456789abcdef"[c
& 15];
1147 static void memory_region_do_init(MemoryRegion
*mr
,
1152 mr
->size
= int128_make64(size
);
1153 if (size
== UINT64_MAX
) {
1154 mr
->size
= int128_2_64();
1156 mr
->name
= g_strdup(name
);
1158 mr
->ram_block
= NULL
;
1161 char *escaped_name
= memory_region_escape_name(name
);
1162 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1165 owner
= container_get(qdev_get_machine(), "/unattached");
1168 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1169 object_unref(OBJECT(mr
));
1171 g_free(escaped_name
);
1175 void memory_region_init(MemoryRegion
*mr
,
1180 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1181 memory_region_do_init(mr
, owner
, name
, size
);
1184 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1185 void *opaque
, Error
**errp
)
1187 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1188 uint64_t value
= mr
->addr
;
1190 visit_type_uint64(v
, name
, &value
, errp
);
1193 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1194 const char *name
, void *opaque
,
1197 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1198 gchar
*path
= (gchar
*)"";
1200 if (mr
->container
) {
1201 path
= object_get_canonical_path(OBJECT(mr
->container
));
1203 visit_type_str(v
, name
, &path
, errp
);
1204 if (mr
->container
) {
1209 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1212 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1214 return OBJECT(mr
->container
);
1217 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1218 const char *name
, void *opaque
,
1221 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1222 int32_t value
= mr
->priority
;
1224 visit_type_int32(v
, name
, &value
, errp
);
1227 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1228 void *opaque
, Error
**errp
)
1230 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1231 uint64_t value
= memory_region_size(mr
);
1233 visit_type_uint64(v
, name
, &value
, errp
);
1236 static void memory_region_initfn(Object
*obj
)
1238 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1241 mr
->ops
= &unassigned_mem_ops
;
1243 mr
->romd_mode
= true;
1244 mr
->global_locking
= true;
1245 mr
->destructor
= memory_region_destructor_none
;
1246 QTAILQ_INIT(&mr
->subregions
);
1247 QTAILQ_INIT(&mr
->coalesced
);
1249 op
= object_property_add(OBJECT(mr
), "container",
1250 "link<" TYPE_MEMORY_REGION
">",
1251 memory_region_get_container
,
1252 NULL
, /* memory_region_set_container */
1253 NULL
, NULL
, &error_abort
);
1254 op
->resolve
= memory_region_resolve_container
;
1256 object_property_add(OBJECT(mr
), "addr", "uint64",
1257 memory_region_get_addr
,
1258 NULL
, /* memory_region_set_addr */
1259 NULL
, NULL
, &error_abort
);
1260 object_property_add(OBJECT(mr
), "priority", "uint32",
1261 memory_region_get_priority
,
1262 NULL
, /* memory_region_set_priority */
1263 NULL
, NULL
, &error_abort
);
1264 object_property_add(OBJECT(mr
), "size", "uint64",
1265 memory_region_get_size
,
1266 NULL
, /* memory_region_set_size, */
1267 NULL
, NULL
, &error_abort
);
1270 static void iommu_memory_region_initfn(Object
*obj
)
1272 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1274 mr
->is_iommu
= true;
1277 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1280 #ifdef DEBUG_UNASSIGNED
1281 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1283 if (current_cpu
!= NULL
) {
1284 bool is_exec
= current_cpu
->mem_io_access_type
== MMU_INST_FETCH
;
1285 cpu_unassigned_access(current_cpu
, addr
, false, is_exec
, 0, size
);
1290 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1291 uint64_t val
, unsigned size
)
1293 #ifdef DEBUG_UNASSIGNED
1294 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1296 if (current_cpu
!= NULL
) {
1297 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1301 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1302 unsigned size
, bool is_write
,
1308 const MemoryRegionOps unassigned_mem_ops
= {
1309 .valid
.accepts
= unassigned_mem_accepts
,
1310 .endianness
= DEVICE_NATIVE_ENDIAN
,
1313 static uint64_t memory_region_ram_device_read(void *opaque
,
1314 hwaddr addr
, unsigned size
)
1316 MemoryRegion
*mr
= opaque
;
1317 uint64_t data
= (uint64_t)~0;
1321 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1324 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1327 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1330 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1334 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1339 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1340 uint64_t data
, unsigned size
)
1342 MemoryRegion
*mr
= opaque
;
1344 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1348 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1351 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1354 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1357 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1362 static const MemoryRegionOps ram_device_mem_ops
= {
1363 .read
= memory_region_ram_device_read
,
1364 .write
= memory_region_ram_device_write
,
1365 .endianness
= DEVICE_HOST_ENDIAN
,
1367 .min_access_size
= 1,
1368 .max_access_size
= 8,
1372 .min_access_size
= 1,
1373 .max_access_size
= 8,
1378 bool memory_region_access_valid(MemoryRegion
*mr
,
1384 int access_size_min
, access_size_max
;
1387 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1391 if (!mr
->ops
->valid
.accepts
) {
1395 access_size_min
= mr
->ops
->valid
.min_access_size
;
1396 if (!mr
->ops
->valid
.min_access_size
) {
1397 access_size_min
= 1;
1400 access_size_max
= mr
->ops
->valid
.max_access_size
;
1401 if (!mr
->ops
->valid
.max_access_size
) {
1402 access_size_max
= 4;
1405 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1406 for (i
= 0; i
< size
; i
+= access_size
) {
1407 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1416 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1424 if (mr
->ops
->read
) {
1425 return access_with_adjusted_size(addr
, pval
, size
,
1426 mr
->ops
->impl
.min_access_size
,
1427 mr
->ops
->impl
.max_access_size
,
1428 memory_region_read_accessor
,
1431 return access_with_adjusted_size(addr
, pval
, size
,
1432 mr
->ops
->impl
.min_access_size
,
1433 mr
->ops
->impl
.max_access_size
,
1434 memory_region_read_with_attrs_accessor
,
1439 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1447 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1448 *pval
= unassigned_mem_read(mr
, addr
, size
);
1449 return MEMTX_DECODE_ERROR
;
1452 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1453 adjust_endianness(mr
, pval
, size
);
1457 /* Return true if an eventfd was signalled */
1458 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1464 MemoryRegionIoeventfd ioeventfd
= {
1465 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1470 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1471 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1472 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1474 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1475 event_notifier_set(ioeventfd
.e
);
1483 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1489 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1490 unassigned_mem_write(mr
, addr
, data
, size
);
1491 return MEMTX_DECODE_ERROR
;
1494 adjust_endianness(mr
, &data
, size
);
1496 if ((!kvm_eventfds_enabled()) &&
1497 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1501 if (mr
->ops
->write
) {
1502 return access_with_adjusted_size(addr
, &data
, size
,
1503 mr
->ops
->impl
.min_access_size
,
1504 mr
->ops
->impl
.max_access_size
,
1505 memory_region_write_accessor
, mr
,
1509 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_with_attrs_accessor
,
1517 void memory_region_init_io(MemoryRegion
*mr
,
1519 const MemoryRegionOps
*ops
,
1524 memory_region_init(mr
, owner
, name
, size
);
1525 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1526 mr
->opaque
= opaque
;
1527 mr
->terminates
= true;
1530 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1536 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1539 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1547 memory_region_init(mr
, owner
, name
, size
);
1549 mr
->terminates
= true;
1550 mr
->destructor
= memory_region_destructor_ram
;
1551 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, &err
);
1552 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1554 mr
->size
= int128_zero();
1555 object_unparent(OBJECT(mr
));
1556 error_propagate(errp
, err
);
1560 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1565 void (*resized
)(const char*,
1571 memory_region_init(mr
, owner
, name
, size
);
1573 mr
->terminates
= true;
1574 mr
->destructor
= memory_region_destructor_ram
;
1575 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1577 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1579 mr
->size
= int128_zero();
1580 object_unparent(OBJECT(mr
));
1581 error_propagate(errp
, err
);
1586 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1587 struct Object
*owner
,
1596 memory_region_init(mr
, owner
, name
, size
);
1598 mr
->terminates
= true;
1599 mr
->destructor
= memory_region_destructor_ram
;
1601 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
, &err
);
1602 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1604 mr
->size
= int128_zero();
1605 object_unparent(OBJECT(mr
));
1606 error_propagate(errp
, err
);
1610 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1611 struct Object
*owner
,
1619 memory_region_init(mr
, owner
, name
, size
);
1621 mr
->terminates
= true;
1622 mr
->destructor
= memory_region_destructor_ram
;
1623 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
,
1624 share
? RAM_SHARED
: 0,
1626 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1628 mr
->size
= int128_zero();
1629 object_unparent(OBJECT(mr
));
1630 error_propagate(errp
, err
);
1635 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1641 memory_region_init(mr
, owner
, name
, size
);
1643 mr
->terminates
= true;
1644 mr
->destructor
= memory_region_destructor_ram
;
1645 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1647 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1648 assert(ptr
!= NULL
);
1649 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1652 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1658 memory_region_init(mr
, owner
, name
, size
);
1660 mr
->terminates
= true;
1661 mr
->ram_device
= true;
1662 mr
->ops
= &ram_device_mem_ops
;
1664 mr
->destructor
= memory_region_destructor_ram
;
1665 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1666 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1667 assert(ptr
!= NULL
);
1668 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1671 void memory_region_init_alias(MemoryRegion
*mr
,
1678 memory_region_init(mr
, owner
, name
, size
);
1680 mr
->alias_offset
= offset
;
1683 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1684 struct Object
*owner
,
1690 memory_region_init(mr
, owner
, name
, size
);
1692 mr
->readonly
= true;
1693 mr
->terminates
= true;
1694 mr
->destructor
= memory_region_destructor_ram
;
1695 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1696 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1698 mr
->size
= int128_zero();
1699 object_unparent(OBJECT(mr
));
1700 error_propagate(errp
, err
);
1704 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1706 const MemoryRegionOps
*ops
,
1714 memory_region_init(mr
, owner
, name
, size
);
1716 mr
->opaque
= opaque
;
1717 mr
->terminates
= true;
1718 mr
->rom_device
= true;
1719 mr
->destructor
= memory_region_destructor_ram
;
1720 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1722 mr
->size
= int128_zero();
1723 object_unparent(OBJECT(mr
));
1724 error_propagate(errp
, err
);
1728 void memory_region_init_iommu(void *_iommu_mr
,
1729 size_t instance_size
,
1730 const char *mrtypename
,
1735 struct IOMMUMemoryRegion
*iommu_mr
;
1736 struct MemoryRegion
*mr
;
1738 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1739 mr
= MEMORY_REGION(_iommu_mr
);
1740 memory_region_do_init(mr
, owner
, name
, size
);
1741 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1742 mr
->terminates
= true; /* then re-forwards */
1743 QLIST_INIT(&iommu_mr
->iommu_notify
);
1744 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1747 static void memory_region_finalize(Object
*obj
)
1749 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1751 assert(!mr
->container
);
1753 /* We know the region is not visible in any address space (it
1754 * does not have a container and cannot be a root either because
1755 * it has no references, so we can blindly clear mr->enabled.
1756 * memory_region_set_enabled instead could trigger a transaction
1757 * and cause an infinite loop.
1759 mr
->enabled
= false;
1760 memory_region_transaction_begin();
1761 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1762 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1763 memory_region_del_subregion(mr
, subregion
);
1765 memory_region_transaction_commit();
1768 memory_region_clear_coalescing(mr
);
1769 g_free((char *)mr
->name
);
1770 g_free(mr
->ioeventfds
);
1773 Object
*memory_region_owner(MemoryRegion
*mr
)
1775 Object
*obj
= OBJECT(mr
);
1779 void memory_region_ref(MemoryRegion
*mr
)
1781 /* MMIO callbacks most likely will access data that belongs
1782 * to the owner, hence the need to ref/unref the owner whenever
1783 * the memory region is in use.
1785 * The memory region is a child of its owner. As long as the
1786 * owner doesn't call unparent itself on the memory region,
1787 * ref-ing the owner will also keep the memory region alive.
1788 * Memory regions without an owner are supposed to never go away;
1789 * we do not ref/unref them because it slows down DMA sensibly.
1791 if (mr
&& mr
->owner
) {
1792 object_ref(mr
->owner
);
1796 void memory_region_unref(MemoryRegion
*mr
)
1798 if (mr
&& mr
->owner
) {
1799 object_unref(mr
->owner
);
1803 uint64_t memory_region_size(MemoryRegion
*mr
)
1805 if (int128_eq(mr
->size
, int128_2_64())) {
1808 return int128_get64(mr
->size
);
1811 const char *memory_region_name(const MemoryRegion
*mr
)
1814 ((MemoryRegion
*)mr
)->name
=
1815 object_get_canonical_path_component(OBJECT(mr
));
1820 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1822 return mr
->ram_device
;
1825 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1827 uint8_t mask
= mr
->dirty_log_mask
;
1828 if (global_dirty_log
&& mr
->ram_block
) {
1829 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1834 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1836 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1839 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1841 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1842 IOMMUNotifier
*iommu_notifier
;
1843 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1845 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1846 flags
|= iommu_notifier
->notifier_flags
;
1849 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1850 imrc
->notify_flag_changed(iommu_mr
,
1851 iommu_mr
->iommu_notify_flags
,
1855 iommu_mr
->iommu_notify_flags
= flags
;
1858 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1861 IOMMUMemoryRegion
*iommu_mr
;
1864 memory_region_register_iommu_notifier(mr
->alias
, n
);
1868 /* We need to register for at least one bitfield */
1869 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1870 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1871 assert(n
->start
<= n
->end
);
1872 assert(n
->iommu_idx
>= 0 &&
1873 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1875 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1876 memory_region_update_iommu_notify_flags(iommu_mr
);
1879 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1881 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1883 if (imrc
->get_min_page_size
) {
1884 return imrc
->get_min_page_size(iommu_mr
);
1886 return TARGET_PAGE_SIZE
;
1889 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1891 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1892 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1893 hwaddr addr
, granularity
;
1894 IOMMUTLBEntry iotlb
;
1896 /* If the IOMMU has its own replay callback, override */
1898 imrc
->replay(iommu_mr
, n
);
1902 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1904 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1905 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1906 if (iotlb
.perm
!= IOMMU_NONE
) {
1907 n
->notify(n
, &iotlb
);
1910 /* if (2^64 - MR size) < granularity, it's possible to get an
1911 * infinite loop here. This should catch such a wraparound */
1912 if ((addr
+ granularity
) < addr
) {
1918 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
)
1920 IOMMUNotifier
*notifier
;
1922 IOMMU_NOTIFIER_FOREACH(notifier
, iommu_mr
) {
1923 memory_region_iommu_replay(iommu_mr
, notifier
);
1927 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1930 IOMMUMemoryRegion
*iommu_mr
;
1933 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1936 QLIST_REMOVE(n
, node
);
1937 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1938 memory_region_update_iommu_notify_flags(iommu_mr
);
1941 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1942 IOMMUTLBEntry
*entry
)
1944 IOMMUNotifierFlag request_flags
;
1945 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1948 * Skip the notification if the notification does not overlap
1949 * with registered range.
1951 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1955 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1957 if (entry
->perm
& IOMMU_RW
) {
1958 request_flags
= IOMMU_NOTIFIER_MAP
;
1960 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1963 if (notifier
->notifier_flags
& request_flags
) {
1964 notifier
->notify(notifier
, entry
);
1968 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1970 IOMMUTLBEntry entry
)
1972 IOMMUNotifier
*iommu_notifier
;
1974 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1976 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1977 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1978 memory_region_notify_one(iommu_notifier
, &entry
);
1983 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1984 enum IOMMUMemoryRegionAttr attr
,
1987 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1989 if (!imrc
->get_attr
) {
1993 return imrc
->get_attr(iommu_mr
, attr
, data
);
1996 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1999 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2001 if (!imrc
->attrs_to_index
) {
2005 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2008 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2010 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2012 if (!imrc
->num_indexes
) {
2016 return imrc
->num_indexes(iommu_mr
);
2019 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2021 uint8_t mask
= 1 << client
;
2022 uint8_t old_logging
;
2024 assert(client
== DIRTY_MEMORY_VGA
);
2025 old_logging
= mr
->vga_logging_count
;
2026 mr
->vga_logging_count
+= log
? 1 : -1;
2027 if (!!old_logging
== !!mr
->vga_logging_count
) {
2031 memory_region_transaction_begin();
2032 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2033 memory_region_update_pending
|= mr
->enabled
;
2034 memory_region_transaction_commit();
2037 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2040 assert(mr
->ram_block
);
2041 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2043 memory_region_get_dirty_log_mask(mr
));
2046 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2048 MemoryListener
*listener
;
2053 /* If the same address space has multiple log_sync listeners, we
2054 * visit that address space's FlatView multiple times. But because
2055 * log_sync listeners are rare, it's still cheaper than walking each
2056 * address space once.
2058 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2059 if (!listener
->log_sync
) {
2062 as
= listener
->address_space
;
2063 view
= address_space_get_flatview(as
);
2064 FOR_EACH_FLAT_RANGE(fr
, view
) {
2065 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2066 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2067 listener
->log_sync(listener
, &mrs
);
2070 flatview_unref(view
);
2074 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2077 MemoryRegionSection mrs
;
2078 MemoryListener
*listener
;
2082 hwaddr sec_start
, sec_end
, sec_size
;
2084 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2085 if (!listener
->log_clear
) {
2088 as
= listener
->address_space
;
2089 view
= address_space_get_flatview(as
);
2090 FOR_EACH_FLAT_RANGE(fr
, view
) {
2091 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2093 * Clear dirty bitmap operation only applies to those
2094 * regions whose dirty logging is at least enabled
2099 mrs
= section_from_flat_range(fr
, view
);
2101 sec_start
= MAX(mrs
.offset_within_region
, start
);
2102 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2103 sec_end
= MIN(sec_end
, start
+ len
);
2105 if (sec_start
>= sec_end
) {
2107 * If this memory region section has no intersection
2108 * with the requested range, skip.
2113 /* Valid case; shrink the section if needed */
2114 mrs
.offset_within_address_space
+=
2115 sec_start
- mrs
.offset_within_region
;
2116 mrs
.offset_within_region
= sec_start
;
2117 sec_size
= sec_end
- sec_start
;
2118 mrs
.size
= int128_make64(sec_size
);
2119 listener
->log_clear(listener
, &mrs
);
2121 flatview_unref(view
);
2125 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2130 DirtyBitmapSnapshot
*snapshot
;
2131 assert(mr
->ram_block
);
2132 memory_region_sync_dirty_bitmap(mr
);
2133 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2134 memory_global_after_dirty_log_sync();
2138 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2139 hwaddr addr
, hwaddr size
)
2141 assert(mr
->ram_block
);
2142 return cpu_physical_memory_snapshot_get_dirty(snap
,
2143 memory_region_get_ram_addr(mr
) + addr
, size
);
2146 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2148 if (mr
->readonly
!= readonly
) {
2149 memory_region_transaction_begin();
2150 mr
->readonly
= readonly
;
2151 memory_region_update_pending
|= mr
->enabled
;
2152 memory_region_transaction_commit();
2156 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2158 if (mr
->nonvolatile
!= nonvolatile
) {
2159 memory_region_transaction_begin();
2160 mr
->nonvolatile
= nonvolatile
;
2161 memory_region_update_pending
|= mr
->enabled
;
2162 memory_region_transaction_commit();
2166 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2168 if (mr
->romd_mode
!= romd_mode
) {
2169 memory_region_transaction_begin();
2170 mr
->romd_mode
= romd_mode
;
2171 memory_region_update_pending
|= mr
->enabled
;
2172 memory_region_transaction_commit();
2176 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2177 hwaddr size
, unsigned client
)
2179 assert(mr
->ram_block
);
2180 cpu_physical_memory_test_and_clear_dirty(
2181 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2184 int memory_region_get_fd(MemoryRegion
*mr
)
2192 fd
= mr
->ram_block
->fd
;
2198 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2201 uint64_t offset
= 0;
2205 offset
+= mr
->alias_offset
;
2208 assert(mr
->ram_block
);
2209 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2215 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2219 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2227 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2229 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2232 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2234 assert(mr
->ram_block
);
2236 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2239 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
2244 view
= address_space_get_flatview(as
);
2245 FOR_EACH_FLAT_RANGE(fr
, view
) {
2247 flat_range_coalesced_io_del(fr
, as
);
2248 flat_range_coalesced_io_add(fr
, as
);
2251 flatview_unref(view
);
2254 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
2258 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2259 memory_region_update_coalesced_range_as(mr
, as
);
2263 void memory_region_set_coalescing(MemoryRegion
*mr
)
2265 memory_region_clear_coalescing(mr
);
2266 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2269 void memory_region_add_coalescing(MemoryRegion
*mr
,
2273 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2275 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2276 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2277 memory_region_update_coalesced_range(mr
);
2278 memory_region_set_flush_coalesced(mr
);
2281 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2283 CoalescedMemoryRange
*cmr
;
2284 bool updated
= false;
2286 qemu_flush_coalesced_mmio_buffer();
2287 mr
->flush_coalesced_mmio
= false;
2289 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2290 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2291 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2297 memory_region_update_coalesced_range(mr
);
2301 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2303 mr
->flush_coalesced_mmio
= true;
2306 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2308 qemu_flush_coalesced_mmio_buffer();
2309 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2310 mr
->flush_coalesced_mmio
= false;
2314 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2316 mr
->global_locking
= false;
2319 static bool userspace_eventfd_warning
;
2321 void memory_region_add_eventfd(MemoryRegion
*mr
,
2328 MemoryRegionIoeventfd mrfd
= {
2329 .addr
.start
= int128_make64(addr
),
2330 .addr
.size
= int128_make64(size
),
2331 .match_data
= match_data
,
2337 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2338 userspace_eventfd_warning
))) {
2339 userspace_eventfd_warning
= true;
2340 error_report("Using eventfd without MMIO binding in KVM. "
2341 "Suboptimal performance expected");
2345 adjust_endianness(mr
, &mrfd
.data
, size
);
2347 memory_region_transaction_begin();
2348 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2349 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2354 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2355 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2356 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2357 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2358 mr
->ioeventfds
[i
] = mrfd
;
2359 ioeventfd_update_pending
|= mr
->enabled
;
2360 memory_region_transaction_commit();
2363 void memory_region_del_eventfd(MemoryRegion
*mr
,
2370 MemoryRegionIoeventfd mrfd
= {
2371 .addr
.start
= int128_make64(addr
),
2372 .addr
.size
= int128_make64(size
),
2373 .match_data
= match_data
,
2380 adjust_endianness(mr
, &mrfd
.data
, size
);
2382 memory_region_transaction_begin();
2383 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2384 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2388 assert(i
!= mr
->ioeventfd_nb
);
2389 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2390 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2392 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2393 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2394 ioeventfd_update_pending
|= mr
->enabled
;
2395 memory_region_transaction_commit();
2398 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2400 MemoryRegion
*mr
= subregion
->container
;
2401 MemoryRegion
*other
;
2403 memory_region_transaction_begin();
2405 memory_region_ref(subregion
);
2406 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2407 if (subregion
->priority
>= other
->priority
) {
2408 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2412 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2414 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2415 memory_region_transaction_commit();
2418 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2420 MemoryRegion
*subregion
)
2422 assert(!subregion
->container
);
2423 subregion
->container
= mr
;
2424 subregion
->addr
= offset
;
2425 memory_region_update_container_subregions(subregion
);
2428 void memory_region_add_subregion(MemoryRegion
*mr
,
2430 MemoryRegion
*subregion
)
2432 subregion
->priority
= 0;
2433 memory_region_add_subregion_common(mr
, offset
, subregion
);
2436 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2438 MemoryRegion
*subregion
,
2441 subregion
->priority
= priority
;
2442 memory_region_add_subregion_common(mr
, offset
, subregion
);
2445 void memory_region_del_subregion(MemoryRegion
*mr
,
2446 MemoryRegion
*subregion
)
2448 memory_region_transaction_begin();
2449 assert(subregion
->container
== mr
);
2450 subregion
->container
= NULL
;
2451 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2452 memory_region_unref(subregion
);
2453 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2454 memory_region_transaction_commit();
2457 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2459 if (enabled
== mr
->enabled
) {
2462 memory_region_transaction_begin();
2463 mr
->enabled
= enabled
;
2464 memory_region_update_pending
= true;
2465 memory_region_transaction_commit();
2468 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2470 Int128 s
= int128_make64(size
);
2472 if (size
== UINT64_MAX
) {
2475 if (int128_eq(s
, mr
->size
)) {
2478 memory_region_transaction_begin();
2480 memory_region_update_pending
= true;
2481 memory_region_transaction_commit();
2484 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2486 MemoryRegion
*container
= mr
->container
;
2489 memory_region_transaction_begin();
2490 memory_region_ref(mr
);
2491 memory_region_del_subregion(container
, mr
);
2492 mr
->container
= container
;
2493 memory_region_update_container_subregions(mr
);
2494 memory_region_unref(mr
);
2495 memory_region_transaction_commit();
2499 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2501 if (addr
!= mr
->addr
) {
2503 memory_region_readd_subregion(mr
);
2507 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2511 if (offset
== mr
->alias_offset
) {
2515 memory_region_transaction_begin();
2516 mr
->alias_offset
= offset
;
2517 memory_region_update_pending
|= mr
->enabled
;
2518 memory_region_transaction_commit();
2521 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2526 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2528 const AddrRange
*addr
= addr_
;
2529 const FlatRange
*fr
= fr_
;
2531 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2533 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2539 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2541 return bsearch(&addr
, view
->ranges
, view
->nr
,
2542 sizeof(FlatRange
), cmp_flatrange_addr
);
2545 bool memory_region_is_mapped(MemoryRegion
*mr
)
2547 return mr
->container
? true : false;
2550 /* Same as memory_region_find, but it does not add a reference to the
2551 * returned region. It must be called from an RCU critical section.
2553 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2554 hwaddr addr
, uint64_t size
)
2556 MemoryRegionSection ret
= { .mr
= NULL
};
2564 for (root
= mr
; root
->container
; ) {
2565 root
= root
->container
;
2569 as
= memory_region_to_address_space(root
);
2573 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2575 view
= address_space_to_flatview(as
);
2576 fr
= flatview_lookup(view
, range
);
2581 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2587 range
= addrrange_intersection(range
, fr
->addr
);
2588 ret
.offset_within_region
= fr
->offset_in_region
;
2589 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2591 ret
.size
= range
.size
;
2592 ret
.offset_within_address_space
= int128_get64(range
.start
);
2593 ret
.readonly
= fr
->readonly
;
2594 ret
.nonvolatile
= fr
->nonvolatile
;
2598 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2599 hwaddr addr
, uint64_t size
)
2601 MemoryRegionSection ret
;
2603 ret
= memory_region_find_rcu(mr
, addr
, size
);
2605 memory_region_ref(ret
.mr
);
2611 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2616 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2618 return mr
&& mr
!= container
;
2621 void memory_global_dirty_log_sync(void)
2623 memory_region_sync_dirty_bitmap(NULL
);
2626 void memory_global_after_dirty_log_sync(void)
2628 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2631 static VMChangeStateEntry
*vmstate_change
;
2633 void memory_global_dirty_log_start(void)
2635 if (vmstate_change
) {
2636 qemu_del_vm_change_state_handler(vmstate_change
);
2637 vmstate_change
= NULL
;
2640 global_dirty_log
= true;
2642 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2644 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2645 memory_region_transaction_begin();
2646 memory_region_update_pending
= true;
2647 memory_region_transaction_commit();
2650 static void memory_global_dirty_log_do_stop(void)
2652 global_dirty_log
= false;
2654 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2655 memory_region_transaction_begin();
2656 memory_region_update_pending
= true;
2657 memory_region_transaction_commit();
2659 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2662 static void memory_vm_change_state_handler(void *opaque
, int running
,
2666 memory_global_dirty_log_do_stop();
2668 if (vmstate_change
) {
2669 qemu_del_vm_change_state_handler(vmstate_change
);
2670 vmstate_change
= NULL
;
2675 void memory_global_dirty_log_stop(void)
2677 if (!runstate_is_running()) {
2678 if (vmstate_change
) {
2681 vmstate_change
= qemu_add_vm_change_state_handler(
2682 memory_vm_change_state_handler
, NULL
);
2686 memory_global_dirty_log_do_stop();
2689 static void listener_add_address_space(MemoryListener
*listener
,
2695 if (listener
->begin
) {
2696 listener
->begin(listener
);
2698 if (global_dirty_log
) {
2699 if (listener
->log_global_start
) {
2700 listener
->log_global_start(listener
);
2704 view
= address_space_get_flatview(as
);
2705 FOR_EACH_FLAT_RANGE(fr
, view
) {
2706 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2708 if (listener
->region_add
) {
2709 listener
->region_add(listener
, §ion
);
2711 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2712 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2715 if (listener
->commit
) {
2716 listener
->commit(listener
);
2718 flatview_unref(view
);
2721 static void listener_del_address_space(MemoryListener
*listener
,
2727 if (listener
->begin
) {
2728 listener
->begin(listener
);
2730 view
= address_space_get_flatview(as
);
2731 FOR_EACH_FLAT_RANGE(fr
, view
) {
2732 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2734 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2735 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2737 if (listener
->region_del
) {
2738 listener
->region_del(listener
, §ion
);
2741 if (listener
->commit
) {
2742 listener
->commit(listener
);
2744 flatview_unref(view
);
2747 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2749 MemoryListener
*other
= NULL
;
2751 listener
->address_space
= as
;
2752 if (QTAILQ_EMPTY(&memory_listeners
)
2753 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2754 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2756 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2757 if (listener
->priority
< other
->priority
) {
2761 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2764 if (QTAILQ_EMPTY(&as
->listeners
)
2765 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2766 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2768 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2769 if (listener
->priority
< other
->priority
) {
2773 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2776 listener_add_address_space(listener
, as
);
2779 void memory_listener_unregister(MemoryListener
*listener
)
2781 if (!listener
->address_space
) {
2785 listener_del_address_space(listener
, listener
->address_space
);
2786 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2787 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2788 listener
->address_space
= NULL
;
2791 void address_space_remove_listeners(AddressSpace
*as
)
2793 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2794 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2798 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2800 memory_region_ref(root
);
2802 as
->current_map
= NULL
;
2803 as
->ioeventfd_nb
= 0;
2804 as
->ioeventfds
= NULL
;
2805 QTAILQ_INIT(&as
->listeners
);
2806 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2807 as
->name
= g_strdup(name
? name
: "anonymous");
2808 address_space_update_topology(as
);
2809 address_space_update_ioeventfds(as
);
2812 static void do_address_space_destroy(AddressSpace
*as
)
2814 assert(QTAILQ_EMPTY(&as
->listeners
));
2816 flatview_unref(as
->current_map
);
2818 g_free(as
->ioeventfds
);
2819 memory_region_unref(as
->root
);
2822 void address_space_destroy(AddressSpace
*as
)
2824 MemoryRegion
*root
= as
->root
;
2826 /* Flush out anything from MemoryListeners listening in on this */
2827 memory_region_transaction_begin();
2829 memory_region_transaction_commit();
2830 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2832 /* At this point, as->dispatch and as->current_map are dummy
2833 * entries that the guest should never use. Wait for the old
2834 * values to expire before freeing the data.
2837 call_rcu(as
, do_address_space_destroy
, rcu
);
2840 static const char *memory_region_type(MemoryRegion
*mr
)
2842 if (memory_region_is_ram_device(mr
)) {
2844 } else if (memory_region_is_romd(mr
)) {
2846 } else if (memory_region_is_rom(mr
)) {
2848 } else if (memory_region_is_ram(mr
)) {
2855 typedef struct MemoryRegionList MemoryRegionList
;
2857 struct MemoryRegionList
{
2858 const MemoryRegion
*mr
;
2859 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2862 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2864 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2865 int128_sub((size), int128_one())) : 0)
2866 #define MTREE_INDENT " "
2868 static void mtree_expand_owner(const char *label
, Object
*obj
)
2870 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
2872 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
2873 if (dev
&& dev
->id
) {
2874 qemu_printf(" id=%s", dev
->id
);
2876 gchar
*canonical_path
= object_get_canonical_path(obj
);
2877 if (canonical_path
) {
2878 qemu_printf(" path=%s", canonical_path
);
2879 g_free(canonical_path
);
2881 qemu_printf(" type=%s", object_get_typename(obj
));
2887 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
2889 Object
*owner
= mr
->owner
;
2890 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
2892 if (!owner
&& !parent
) {
2893 qemu_printf(" orphan");
2897 mtree_expand_owner("owner", owner
);
2899 if (parent
&& parent
!= owner
) {
2900 mtree_expand_owner("parent", parent
);
2904 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
2906 MemoryRegionListHead
*alias_print_queue
,
2909 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2910 MemoryRegionListHead submr_print_queue
;
2911 const MemoryRegion
*submr
;
2913 hwaddr cur_start
, cur_end
;
2919 for (i
= 0; i
< level
; i
++) {
2920 qemu_printf(MTREE_INDENT
);
2923 cur_start
= base
+ mr
->addr
;
2924 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2927 * Try to detect overflow of memory region. This should never
2928 * happen normally. When it happens, we dump something to warn the
2929 * user who is observing this.
2931 if (cur_start
< base
|| cur_end
< cur_start
) {
2932 qemu_printf("[DETECTED OVERFLOW!] ");
2936 MemoryRegionList
*ml
;
2939 /* check if the alias is already in the queue */
2940 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2941 if (ml
->mr
== mr
->alias
) {
2947 ml
= g_new(MemoryRegionList
, 1);
2949 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2951 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2952 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2953 "-" TARGET_FMT_plx
"%s",
2956 mr
->nonvolatile
? "nv-" : "",
2957 memory_region_type((MemoryRegion
*)mr
),
2958 memory_region_name(mr
),
2959 memory_region_name(mr
->alias
),
2961 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2962 mr
->enabled
? "" : " [disabled]");
2964 mtree_print_mr_owner(mr
);
2967 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2968 " (prio %d, %s%s): %s%s",
2971 mr
->nonvolatile
? "nv-" : "",
2972 memory_region_type((MemoryRegion
*)mr
),
2973 memory_region_name(mr
),
2974 mr
->enabled
? "" : " [disabled]");
2976 mtree_print_mr_owner(mr
);
2981 QTAILQ_INIT(&submr_print_queue
);
2983 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2984 new_ml
= g_new(MemoryRegionList
, 1);
2986 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2987 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2988 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2989 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2990 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
2996 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
3000 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3001 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
3002 alias_print_queue
, owner
);
3005 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3010 struct FlatViewInfo
{
3015 const char *ac_name
;
3018 static void mtree_print_flatview(gpointer key
, gpointer value
,
3021 FlatView
*view
= key
;
3022 GArray
*fv_address_spaces
= value
;
3023 struct FlatViewInfo
*fvi
= user_data
;
3024 FlatRange
*range
= &view
->ranges
[0];
3030 qemu_printf("FlatView #%d\n", fvi
->counter
);
3033 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3034 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3035 qemu_printf(" AS \"%s\", root: %s",
3036 as
->name
, memory_region_name(as
->root
));
3037 if (as
->root
->alias
) {
3038 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3043 qemu_printf(" Root memory region: %s\n",
3044 view
->root
? memory_region_name(view
->root
) : "(none)");
3047 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3053 if (range
->offset_in_region
) {
3054 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3055 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3056 int128_get64(range
->addr
.start
),
3057 int128_get64(range
->addr
.start
)
3058 + MR_SIZE(range
->addr
.size
),
3060 range
->nonvolatile
? "nv-" : "",
3061 range
->readonly
? "rom" : memory_region_type(mr
),
3062 memory_region_name(mr
),
3063 range
->offset_in_region
);
3065 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3066 " (prio %d, %s%s): %s",
3067 int128_get64(range
->addr
.start
),
3068 int128_get64(range
->addr
.start
)
3069 + MR_SIZE(range
->addr
.size
),
3071 range
->nonvolatile
? "nv-" : "",
3072 range
->readonly
? "rom" : memory_region_type(mr
),
3073 memory_region_name(mr
));
3076 mtree_print_mr_owner(mr
);
3080 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3081 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3082 if (fvi
->ac
->has_memory(current_machine
, as
,
3083 int128_get64(range
->addr
.start
),
3084 MR_SIZE(range
->addr
.size
) + 1)) {
3085 qemu_printf(" %s", fvi
->ac_name
);
3093 #if !defined(CONFIG_USER_ONLY)
3094 if (fvi
->dispatch_tree
&& view
->root
) {
3095 mtree_print_dispatch(view
->dispatch
, view
->root
);
3102 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3105 FlatView
*view
= key
;
3106 GArray
*fv_address_spaces
= value
;
3108 g_array_unref(fv_address_spaces
);
3109 flatview_unref(view
);
3114 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
)
3116 MemoryRegionListHead ml_head
;
3117 MemoryRegionList
*ml
, *ml2
;
3122 struct FlatViewInfo fvi
= {
3124 .dispatch_tree
= dispatch_tree
,
3127 GArray
*fv_address_spaces
;
3128 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3129 AccelClass
*ac
= ACCEL_GET_CLASS(current_machine
->accelerator
);
3131 if (ac
->has_memory
) {
3133 fvi
.ac_name
= current_machine
->accel
? current_machine
->accel
:
3134 object_class_get_name(OBJECT_CLASS(ac
));
3137 /* Gather all FVs in one table */
3138 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3139 view
= address_space_get_flatview(as
);
3141 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3142 if (!fv_address_spaces
) {
3143 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3144 g_hash_table_insert(views
, view
, fv_address_spaces
);
3147 g_array_append_val(fv_address_spaces
, as
);
3151 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3154 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3155 g_hash_table_unref(views
);
3160 QTAILQ_INIT(&ml_head
);
3162 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3163 qemu_printf("address-space: %s\n", as
->name
);
3164 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
);
3168 /* print aliased regions */
3169 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3170 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3171 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
);
3175 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3180 void memory_region_init_ram(MemoryRegion
*mr
,
3181 struct Object
*owner
,
3186 DeviceState
*owner_dev
;
3189 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3191 error_propagate(errp
, err
);
3194 /* This will assert if owner is neither NULL nor a DeviceState.
3195 * We only want the owner here for the purposes of defining a
3196 * unique name for migration. TODO: Ideally we should implement
3197 * a naming scheme for Objects which are not DeviceStates, in
3198 * which case we can relax this restriction.
3200 owner_dev
= DEVICE(owner
);
3201 vmstate_register_ram(mr
, owner_dev
);
3204 void memory_region_init_rom(MemoryRegion
*mr
,
3205 struct Object
*owner
,
3210 DeviceState
*owner_dev
;
3213 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3215 error_propagate(errp
, err
);
3218 /* This will assert if owner is neither NULL nor a DeviceState.
3219 * We only want the owner here for the purposes of defining a
3220 * unique name for migration. TODO: Ideally we should implement
3221 * a naming scheme for Objects which are not DeviceStates, in
3222 * which case we can relax this restriction.
3224 owner_dev
= DEVICE(owner
);
3225 vmstate_register_ram(mr
, owner_dev
);
3228 void memory_region_init_rom_device(MemoryRegion
*mr
,
3229 struct Object
*owner
,
3230 const MemoryRegionOps
*ops
,
3236 DeviceState
*owner_dev
;
3239 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3242 error_propagate(errp
, err
);
3245 /* This will assert if owner is neither NULL nor a DeviceState.
3246 * We only want the owner here for the purposes of defining a
3247 * unique name for migration. TODO: Ideally we should implement
3248 * a naming scheme for Objects which are not DeviceStates, in
3249 * which case we can relax this restriction.
3251 owner_dev
= DEVICE(owner
);
3252 vmstate_register_ram(mr
, owner_dev
);
3255 static const TypeInfo memory_region_info
= {
3256 .parent
= TYPE_OBJECT
,
3257 .name
= TYPE_MEMORY_REGION
,
3258 .class_size
= sizeof(MemoryRegionClass
),
3259 .instance_size
= sizeof(MemoryRegion
),
3260 .instance_init
= memory_region_initfn
,
3261 .instance_finalize
= memory_region_finalize
,
3264 static const TypeInfo iommu_memory_region_info
= {
3265 .parent
= TYPE_MEMORY_REGION
,
3266 .name
= TYPE_IOMMU_MEMORY_REGION
,
3267 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3268 .instance_size
= sizeof(IOMMUMemoryRegion
),
3269 .instance_init
= iommu_memory_region_initfn
,
3273 static void memory_register_types(void)
3275 type_register_static(&memory_region_info
);
3276 type_register_static(&iommu_memory_region_info
);
3279 type_init(memory_register_types
)