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
);
859 * Notify the memory listeners about the coalesced IO change events of
860 * range `cmr'. Only the part that has intersection of the specified
861 * FlatRange will be sent.
863 static void flat_range_coalesced_io_notify(FlatRange
*fr
, AddressSpace
*as
,
864 CoalescedMemoryRange
*cmr
, bool add
)
868 tmp
= addrrange_shift(cmr
->addr
,
869 int128_sub(fr
->addr
.start
,
870 int128_make64(fr
->offset_in_region
)));
871 if (!addrrange_intersects(tmp
, fr
->addr
)) {
874 tmp
= addrrange_intersection(tmp
, fr
->addr
);
877 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, coalesced_io_add
,
878 int128_get64(tmp
.start
),
879 int128_get64(tmp
.size
));
881 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Reverse
, coalesced_io_del
,
882 int128_get64(tmp
.start
),
883 int128_get64(tmp
.size
));
887 static void flat_range_coalesced_io_del(FlatRange
*fr
, AddressSpace
*as
)
889 CoalescedMemoryRange
*cmr
;
891 if (!fr
->has_coalesced_range
) {
895 if (--fr
->has_coalesced_range
> 0) {
899 QTAILQ_FOREACH(cmr
, &fr
->mr
->coalesced
, link
) {
900 flat_range_coalesced_io_notify(fr
, as
, cmr
, false);
904 static void flat_range_coalesced_io_add(FlatRange
*fr
, AddressSpace
*as
)
906 MemoryRegion
*mr
= fr
->mr
;
907 CoalescedMemoryRange
*cmr
;
909 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
913 if (fr
->has_coalesced_range
++) {
917 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
918 flat_range_coalesced_io_notify(fr
, as
, cmr
, true);
922 static void address_space_update_topology_pass(AddressSpace
*as
,
923 const FlatView
*old_view
,
924 const FlatView
*new_view
,
928 FlatRange
*frold
, *frnew
;
930 /* Generate a symmetric difference of the old and new memory maps.
931 * Kill ranges in the old map, and instantiate ranges in the new map.
934 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
935 if (iold
< old_view
->nr
) {
936 frold
= &old_view
->ranges
[iold
];
940 if (inew
< new_view
->nr
) {
941 frnew
= &new_view
->ranges
[inew
];
948 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
949 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
950 && !flatrange_equal(frold
, frnew
)))) {
951 /* In old but not in new, or in both but attributes changed. */
954 flat_range_coalesced_io_del(frold
, as
);
955 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
959 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
960 /* In both and unchanged (except logging may have changed) */
963 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
964 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
965 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
966 frold
->dirty_log_mask
,
967 frnew
->dirty_log_mask
);
969 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
970 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
971 frold
->dirty_log_mask
,
972 frnew
->dirty_log_mask
);
982 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
983 flat_range_coalesced_io_add(frnew
, as
);
991 static void flatviews_init(void)
993 static FlatView
*empty_view
;
999 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1000 (GDestroyNotify
) flatview_unref
);
1002 empty_view
= generate_memory_topology(NULL
);
1003 /* We keep it alive forever in the global variable. */
1004 flatview_ref(empty_view
);
1006 g_hash_table_replace(flat_views
, NULL
, empty_view
);
1007 flatview_ref(empty_view
);
1011 static void flatviews_reset(void)
1016 g_hash_table_unref(flat_views
);
1021 /* Render unique FVs */
1022 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1023 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1025 if (g_hash_table_lookup(flat_views
, physmr
)) {
1029 generate_memory_topology(physmr
);
1033 static void address_space_set_flatview(AddressSpace
*as
)
1035 FlatView
*old_view
= address_space_to_flatview(as
);
1036 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1037 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
1041 if (old_view
== new_view
) {
1046 flatview_ref(old_view
);
1049 flatview_ref(new_view
);
1051 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1052 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1055 old_view2
= &tmpview
;
1057 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1058 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1061 /* Writes are protected by the BQL. */
1062 atomic_rcu_set(&as
->current_map
, new_view
);
1064 flatview_unref(old_view
);
1067 /* Note that all the old MemoryRegions are still alive up to this
1068 * point. This relieves most MemoryListeners from the need to
1069 * ref/unref the MemoryRegions they get---unless they use them
1070 * outside the iothread mutex, in which case precise reference
1071 * counting is necessary.
1074 flatview_unref(old_view
);
1078 static void address_space_update_topology(AddressSpace
*as
)
1080 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1083 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1084 generate_memory_topology(physmr
);
1086 address_space_set_flatview(as
);
1089 void memory_region_transaction_begin(void)
1091 qemu_flush_coalesced_mmio_buffer();
1092 ++memory_region_transaction_depth
;
1095 void memory_region_transaction_commit(void)
1099 assert(memory_region_transaction_depth
);
1100 assert(qemu_mutex_iothread_locked());
1102 --memory_region_transaction_depth
;
1103 if (!memory_region_transaction_depth
) {
1104 if (memory_region_update_pending
) {
1107 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1109 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1110 address_space_set_flatview(as
);
1111 address_space_update_ioeventfds(as
);
1113 memory_region_update_pending
= false;
1114 ioeventfd_update_pending
= false;
1115 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1116 } else if (ioeventfd_update_pending
) {
1117 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1118 address_space_update_ioeventfds(as
);
1120 ioeventfd_update_pending
= false;
1125 static void memory_region_destructor_none(MemoryRegion
*mr
)
1129 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1131 qemu_ram_free(mr
->ram_block
);
1134 static bool memory_region_need_escape(char c
)
1136 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1139 static char *memory_region_escape_name(const char *name
)
1146 for (p
= name
; *p
; p
++) {
1147 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1149 if (bytes
== p
- name
) {
1150 return g_memdup(name
, bytes
+ 1);
1153 escaped
= g_malloc(bytes
+ 1);
1154 for (p
= name
, q
= escaped
; *p
; p
++) {
1156 if (unlikely(memory_region_need_escape(c
))) {
1159 *q
++ = "0123456789abcdef"[c
>> 4];
1160 c
= "0123456789abcdef"[c
& 15];
1168 static void memory_region_do_init(MemoryRegion
*mr
,
1173 mr
->size
= int128_make64(size
);
1174 if (size
== UINT64_MAX
) {
1175 mr
->size
= int128_2_64();
1177 mr
->name
= g_strdup(name
);
1179 mr
->ram_block
= NULL
;
1182 char *escaped_name
= memory_region_escape_name(name
);
1183 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1186 owner
= container_get(qdev_get_machine(), "/unattached");
1189 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1190 object_unref(OBJECT(mr
));
1192 g_free(escaped_name
);
1196 void memory_region_init(MemoryRegion
*mr
,
1201 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1202 memory_region_do_init(mr
, owner
, name
, size
);
1205 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1206 void *opaque
, Error
**errp
)
1208 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1209 uint64_t value
= mr
->addr
;
1211 visit_type_uint64(v
, name
, &value
, errp
);
1214 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1215 const char *name
, void *opaque
,
1218 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1219 gchar
*path
= (gchar
*)"";
1221 if (mr
->container
) {
1222 path
= object_get_canonical_path(OBJECT(mr
->container
));
1224 visit_type_str(v
, name
, &path
, errp
);
1225 if (mr
->container
) {
1230 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1233 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1235 return OBJECT(mr
->container
);
1238 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1239 const char *name
, void *opaque
,
1242 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1243 int32_t value
= mr
->priority
;
1245 visit_type_int32(v
, name
, &value
, errp
);
1248 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1249 void *opaque
, Error
**errp
)
1251 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1252 uint64_t value
= memory_region_size(mr
);
1254 visit_type_uint64(v
, name
, &value
, errp
);
1257 static void memory_region_initfn(Object
*obj
)
1259 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1262 mr
->ops
= &unassigned_mem_ops
;
1264 mr
->romd_mode
= true;
1265 mr
->global_locking
= true;
1266 mr
->destructor
= memory_region_destructor_none
;
1267 QTAILQ_INIT(&mr
->subregions
);
1268 QTAILQ_INIT(&mr
->coalesced
);
1270 op
= object_property_add(OBJECT(mr
), "container",
1271 "link<" TYPE_MEMORY_REGION
">",
1272 memory_region_get_container
,
1273 NULL
, /* memory_region_set_container */
1274 NULL
, NULL
, &error_abort
);
1275 op
->resolve
= memory_region_resolve_container
;
1277 object_property_add(OBJECT(mr
), "addr", "uint64",
1278 memory_region_get_addr
,
1279 NULL
, /* memory_region_set_addr */
1280 NULL
, NULL
, &error_abort
);
1281 object_property_add(OBJECT(mr
), "priority", "uint32",
1282 memory_region_get_priority
,
1283 NULL
, /* memory_region_set_priority */
1284 NULL
, NULL
, &error_abort
);
1285 object_property_add(OBJECT(mr
), "size", "uint64",
1286 memory_region_get_size
,
1287 NULL
, /* memory_region_set_size, */
1288 NULL
, NULL
, &error_abort
);
1291 static void iommu_memory_region_initfn(Object
*obj
)
1293 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1295 mr
->is_iommu
= true;
1298 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1301 #ifdef DEBUG_UNASSIGNED
1302 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1304 if (current_cpu
!= NULL
) {
1305 bool is_exec
= current_cpu
->mem_io_access_type
== MMU_INST_FETCH
;
1306 cpu_unassigned_access(current_cpu
, addr
, false, is_exec
, 0, size
);
1311 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1312 uint64_t val
, unsigned size
)
1314 #ifdef DEBUG_UNASSIGNED
1315 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1317 if (current_cpu
!= NULL
) {
1318 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1322 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1323 unsigned size
, bool is_write
,
1329 const MemoryRegionOps unassigned_mem_ops
= {
1330 .valid
.accepts
= unassigned_mem_accepts
,
1331 .endianness
= DEVICE_NATIVE_ENDIAN
,
1334 static uint64_t memory_region_ram_device_read(void *opaque
,
1335 hwaddr addr
, unsigned size
)
1337 MemoryRegion
*mr
= opaque
;
1338 uint64_t data
= (uint64_t)~0;
1342 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1345 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1348 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1351 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1355 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1360 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1361 uint64_t data
, unsigned size
)
1363 MemoryRegion
*mr
= opaque
;
1365 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1369 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1372 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1375 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1378 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1383 static const MemoryRegionOps ram_device_mem_ops
= {
1384 .read
= memory_region_ram_device_read
,
1385 .write
= memory_region_ram_device_write
,
1386 .endianness
= DEVICE_HOST_ENDIAN
,
1388 .min_access_size
= 1,
1389 .max_access_size
= 8,
1393 .min_access_size
= 1,
1394 .max_access_size
= 8,
1399 bool memory_region_access_valid(MemoryRegion
*mr
,
1405 int access_size_min
, access_size_max
;
1408 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1412 if (!mr
->ops
->valid
.accepts
) {
1416 access_size_min
= mr
->ops
->valid
.min_access_size
;
1417 if (!mr
->ops
->valid
.min_access_size
) {
1418 access_size_min
= 1;
1421 access_size_max
= mr
->ops
->valid
.max_access_size
;
1422 if (!mr
->ops
->valid
.max_access_size
) {
1423 access_size_max
= 4;
1426 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1427 for (i
= 0; i
< size
; i
+= access_size
) {
1428 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1437 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1445 if (mr
->ops
->read
) {
1446 return access_with_adjusted_size(addr
, pval
, size
,
1447 mr
->ops
->impl
.min_access_size
,
1448 mr
->ops
->impl
.max_access_size
,
1449 memory_region_read_accessor
,
1452 return access_with_adjusted_size(addr
, pval
, size
,
1453 mr
->ops
->impl
.min_access_size
,
1454 mr
->ops
->impl
.max_access_size
,
1455 memory_region_read_with_attrs_accessor
,
1460 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1468 if (!memory_region_access_valid(mr
, addr
, size
, false, attrs
)) {
1469 *pval
= unassigned_mem_read(mr
, addr
, size
);
1470 return MEMTX_DECODE_ERROR
;
1473 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1474 adjust_endianness(mr
, pval
, size
);
1478 /* Return true if an eventfd was signalled */
1479 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1485 MemoryRegionIoeventfd ioeventfd
= {
1486 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1491 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1492 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1493 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1495 if (memory_region_ioeventfd_equal(&ioeventfd
, &mr
->ioeventfds
[i
])) {
1496 event_notifier_set(ioeventfd
.e
);
1504 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1510 if (!memory_region_access_valid(mr
, addr
, size
, true, attrs
)) {
1511 unassigned_mem_write(mr
, addr
, data
, size
);
1512 return MEMTX_DECODE_ERROR
;
1515 adjust_endianness(mr
, &data
, size
);
1517 if ((!kvm_eventfds_enabled()) &&
1518 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1522 if (mr
->ops
->write
) {
1523 return access_with_adjusted_size(addr
, &data
, size
,
1524 mr
->ops
->impl
.min_access_size
,
1525 mr
->ops
->impl
.max_access_size
,
1526 memory_region_write_accessor
, mr
,
1530 access_with_adjusted_size(addr
, &data
, size
,
1531 mr
->ops
->impl
.min_access_size
,
1532 mr
->ops
->impl
.max_access_size
,
1533 memory_region_write_with_attrs_accessor
,
1538 void memory_region_init_io(MemoryRegion
*mr
,
1540 const MemoryRegionOps
*ops
,
1545 memory_region_init(mr
, owner
, name
, size
);
1546 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1547 mr
->opaque
= opaque
;
1548 mr
->terminates
= true;
1551 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1557 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1560 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1568 memory_region_init(mr
, owner
, name
, size
);
1570 mr
->terminates
= true;
1571 mr
->destructor
= memory_region_destructor_ram
;
1572 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, &err
);
1573 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1575 mr
->size
= int128_zero();
1576 object_unparent(OBJECT(mr
));
1577 error_propagate(errp
, err
);
1581 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1586 void (*resized
)(const char*,
1592 memory_region_init(mr
, owner
, name
, size
);
1594 mr
->terminates
= true;
1595 mr
->destructor
= memory_region_destructor_ram
;
1596 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1598 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1600 mr
->size
= int128_zero();
1601 object_unparent(OBJECT(mr
));
1602 error_propagate(errp
, err
);
1607 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1608 struct Object
*owner
,
1617 memory_region_init(mr
, owner
, name
, size
);
1619 mr
->terminates
= true;
1620 mr
->destructor
= memory_region_destructor_ram
;
1622 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, ram_flags
, path
, &err
);
1623 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1625 mr
->size
= int128_zero();
1626 object_unparent(OBJECT(mr
));
1627 error_propagate(errp
, err
);
1631 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1632 struct Object
*owner
,
1640 memory_region_init(mr
, owner
, name
, size
);
1642 mr
->terminates
= true;
1643 mr
->destructor
= memory_region_destructor_ram
;
1644 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
,
1645 share
? RAM_SHARED
: 0,
1647 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1649 mr
->size
= int128_zero();
1650 object_unparent(OBJECT(mr
));
1651 error_propagate(errp
, err
);
1656 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1662 memory_region_init(mr
, owner
, name
, size
);
1664 mr
->terminates
= true;
1665 mr
->destructor
= memory_region_destructor_ram
;
1666 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1668 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1669 assert(ptr
!= NULL
);
1670 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1673 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1679 memory_region_init(mr
, owner
, name
, size
);
1681 mr
->terminates
= true;
1682 mr
->ram_device
= true;
1683 mr
->ops
= &ram_device_mem_ops
;
1685 mr
->destructor
= memory_region_destructor_ram
;
1686 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1687 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1688 assert(ptr
!= NULL
);
1689 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1692 void memory_region_init_alias(MemoryRegion
*mr
,
1699 memory_region_init(mr
, owner
, name
, size
);
1701 mr
->alias_offset
= offset
;
1704 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1705 struct Object
*owner
,
1711 memory_region_init(mr
, owner
, name
, size
);
1713 mr
->readonly
= true;
1714 mr
->terminates
= true;
1715 mr
->destructor
= memory_region_destructor_ram
;
1716 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1717 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1719 mr
->size
= int128_zero();
1720 object_unparent(OBJECT(mr
));
1721 error_propagate(errp
, err
);
1725 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1727 const MemoryRegionOps
*ops
,
1735 memory_region_init(mr
, owner
, name
, size
);
1737 mr
->opaque
= opaque
;
1738 mr
->terminates
= true;
1739 mr
->rom_device
= true;
1740 mr
->destructor
= memory_region_destructor_ram
;
1741 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, &err
);
1743 mr
->size
= int128_zero();
1744 object_unparent(OBJECT(mr
));
1745 error_propagate(errp
, err
);
1749 void memory_region_init_iommu(void *_iommu_mr
,
1750 size_t instance_size
,
1751 const char *mrtypename
,
1756 struct IOMMUMemoryRegion
*iommu_mr
;
1757 struct MemoryRegion
*mr
;
1759 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1760 mr
= MEMORY_REGION(_iommu_mr
);
1761 memory_region_do_init(mr
, owner
, name
, size
);
1762 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1763 mr
->terminates
= true; /* then re-forwards */
1764 QLIST_INIT(&iommu_mr
->iommu_notify
);
1765 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1768 static void memory_region_finalize(Object
*obj
)
1770 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1772 assert(!mr
->container
);
1774 /* We know the region is not visible in any address space (it
1775 * does not have a container and cannot be a root either because
1776 * it has no references, so we can blindly clear mr->enabled.
1777 * memory_region_set_enabled instead could trigger a transaction
1778 * and cause an infinite loop.
1780 mr
->enabled
= false;
1781 memory_region_transaction_begin();
1782 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1783 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1784 memory_region_del_subregion(mr
, subregion
);
1786 memory_region_transaction_commit();
1789 memory_region_clear_coalescing(mr
);
1790 g_free((char *)mr
->name
);
1791 g_free(mr
->ioeventfds
);
1794 Object
*memory_region_owner(MemoryRegion
*mr
)
1796 Object
*obj
= OBJECT(mr
);
1800 void memory_region_ref(MemoryRegion
*mr
)
1802 /* MMIO callbacks most likely will access data that belongs
1803 * to the owner, hence the need to ref/unref the owner whenever
1804 * the memory region is in use.
1806 * The memory region is a child of its owner. As long as the
1807 * owner doesn't call unparent itself on the memory region,
1808 * ref-ing the owner will also keep the memory region alive.
1809 * Memory regions without an owner are supposed to never go away;
1810 * we do not ref/unref them because it slows down DMA sensibly.
1812 if (mr
&& mr
->owner
) {
1813 object_ref(mr
->owner
);
1817 void memory_region_unref(MemoryRegion
*mr
)
1819 if (mr
&& mr
->owner
) {
1820 object_unref(mr
->owner
);
1824 uint64_t memory_region_size(MemoryRegion
*mr
)
1826 if (int128_eq(mr
->size
, int128_2_64())) {
1829 return int128_get64(mr
->size
);
1832 const char *memory_region_name(const MemoryRegion
*mr
)
1835 ((MemoryRegion
*)mr
)->name
=
1836 object_get_canonical_path_component(OBJECT(mr
));
1841 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1843 return mr
->ram_device
;
1846 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1848 uint8_t mask
= mr
->dirty_log_mask
;
1849 if (global_dirty_log
&& mr
->ram_block
) {
1850 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1855 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1857 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1860 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1862 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1863 IOMMUNotifier
*iommu_notifier
;
1864 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1866 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1867 flags
|= iommu_notifier
->notifier_flags
;
1870 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1871 imrc
->notify_flag_changed(iommu_mr
,
1872 iommu_mr
->iommu_notify_flags
,
1876 iommu_mr
->iommu_notify_flags
= flags
;
1879 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1882 IOMMUMemoryRegion
*iommu_mr
;
1885 memory_region_register_iommu_notifier(mr
->alias
, n
);
1889 /* We need to register for at least one bitfield */
1890 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1891 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1892 assert(n
->start
<= n
->end
);
1893 assert(n
->iommu_idx
>= 0 &&
1894 n
->iommu_idx
< memory_region_iommu_num_indexes(iommu_mr
));
1896 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1897 memory_region_update_iommu_notify_flags(iommu_mr
);
1900 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1902 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1904 if (imrc
->get_min_page_size
) {
1905 return imrc
->get_min_page_size(iommu_mr
);
1907 return TARGET_PAGE_SIZE
;
1910 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1912 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1913 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1914 hwaddr addr
, granularity
;
1915 IOMMUTLBEntry iotlb
;
1917 /* If the IOMMU has its own replay callback, override */
1919 imrc
->replay(iommu_mr
, n
);
1923 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1925 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1926 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, n
->iommu_idx
);
1927 if (iotlb
.perm
!= IOMMU_NONE
) {
1928 n
->notify(n
, &iotlb
);
1931 /* if (2^64 - MR size) < granularity, it's possible to get an
1932 * infinite loop here. This should catch such a wraparound */
1933 if ((addr
+ granularity
) < addr
) {
1939 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
)
1941 IOMMUNotifier
*notifier
;
1943 IOMMU_NOTIFIER_FOREACH(notifier
, iommu_mr
) {
1944 memory_region_iommu_replay(iommu_mr
, notifier
);
1948 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1951 IOMMUMemoryRegion
*iommu_mr
;
1954 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1957 QLIST_REMOVE(n
, node
);
1958 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1959 memory_region_update_iommu_notify_flags(iommu_mr
);
1962 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1963 IOMMUTLBEntry
*entry
)
1965 IOMMUNotifierFlag request_flags
;
1966 hwaddr entry_end
= entry
->iova
+ entry
->addr_mask
;
1969 * Skip the notification if the notification does not overlap
1970 * with registered range.
1972 if (notifier
->start
> entry_end
|| notifier
->end
< entry
->iova
) {
1976 assert(entry
->iova
>= notifier
->start
&& entry_end
<= notifier
->end
);
1978 if (entry
->perm
& IOMMU_RW
) {
1979 request_flags
= IOMMU_NOTIFIER_MAP
;
1981 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1984 if (notifier
->notifier_flags
& request_flags
) {
1985 notifier
->notify(notifier
, entry
);
1989 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1991 IOMMUTLBEntry entry
)
1993 IOMMUNotifier
*iommu_notifier
;
1995 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1997 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1998 if (iommu_notifier
->iommu_idx
== iommu_idx
) {
1999 memory_region_notify_one(iommu_notifier
, &entry
);
2004 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
2005 enum IOMMUMemoryRegionAttr attr
,
2008 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2010 if (!imrc
->get_attr
) {
2014 return imrc
->get_attr(iommu_mr
, attr
, data
);
2017 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
2020 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2022 if (!imrc
->attrs_to_index
) {
2026 return imrc
->attrs_to_index(iommu_mr
, attrs
);
2029 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
)
2031 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
2033 if (!imrc
->num_indexes
) {
2037 return imrc
->num_indexes(iommu_mr
);
2040 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
2042 uint8_t mask
= 1 << client
;
2043 uint8_t old_logging
;
2045 assert(client
== DIRTY_MEMORY_VGA
);
2046 old_logging
= mr
->vga_logging_count
;
2047 mr
->vga_logging_count
+= log
? 1 : -1;
2048 if (!!old_logging
== !!mr
->vga_logging_count
) {
2052 memory_region_transaction_begin();
2053 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
2054 memory_region_update_pending
|= mr
->enabled
;
2055 memory_region_transaction_commit();
2058 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2061 assert(mr
->ram_block
);
2062 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
2064 memory_region_get_dirty_log_mask(mr
));
2067 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
2069 MemoryListener
*listener
;
2074 /* If the same address space has multiple log_sync listeners, we
2075 * visit that address space's FlatView multiple times. But because
2076 * log_sync listeners are rare, it's still cheaper than walking each
2077 * address space once.
2079 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2080 if (!listener
->log_sync
) {
2083 as
= listener
->address_space
;
2084 view
= address_space_get_flatview(as
);
2085 FOR_EACH_FLAT_RANGE(fr
, view
) {
2086 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
2087 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
2088 listener
->log_sync(listener
, &mrs
);
2091 flatview_unref(view
);
2095 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2098 MemoryRegionSection mrs
;
2099 MemoryListener
*listener
;
2103 hwaddr sec_start
, sec_end
, sec_size
;
2105 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2106 if (!listener
->log_clear
) {
2109 as
= listener
->address_space
;
2110 view
= address_space_get_flatview(as
);
2111 FOR_EACH_FLAT_RANGE(fr
, view
) {
2112 if (!fr
->dirty_log_mask
|| fr
->mr
!= mr
) {
2114 * Clear dirty bitmap operation only applies to those
2115 * regions whose dirty logging is at least enabled
2120 mrs
= section_from_flat_range(fr
, view
);
2122 sec_start
= MAX(mrs
.offset_within_region
, start
);
2123 sec_end
= mrs
.offset_within_region
+ int128_get64(mrs
.size
);
2124 sec_end
= MIN(sec_end
, start
+ len
);
2126 if (sec_start
>= sec_end
) {
2128 * If this memory region section has no intersection
2129 * with the requested range, skip.
2134 /* Valid case; shrink the section if needed */
2135 mrs
.offset_within_address_space
+=
2136 sec_start
- mrs
.offset_within_region
;
2137 mrs
.offset_within_region
= sec_start
;
2138 sec_size
= sec_end
- sec_start
;
2139 mrs
.size
= int128_make64(sec_size
);
2140 listener
->log_clear(listener
, &mrs
);
2142 flatview_unref(view
);
2146 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2151 DirtyBitmapSnapshot
*snapshot
;
2152 assert(mr
->ram_block
);
2153 memory_region_sync_dirty_bitmap(mr
);
2154 snapshot
= cpu_physical_memory_snapshot_and_clear_dirty(mr
, addr
, size
, client
);
2155 memory_global_after_dirty_log_sync();
2159 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
2160 hwaddr addr
, hwaddr size
)
2162 assert(mr
->ram_block
);
2163 return cpu_physical_memory_snapshot_get_dirty(snap
,
2164 memory_region_get_ram_addr(mr
) + addr
, size
);
2167 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2169 if (mr
->readonly
!= readonly
) {
2170 memory_region_transaction_begin();
2171 mr
->readonly
= readonly
;
2172 memory_region_update_pending
|= mr
->enabled
;
2173 memory_region_transaction_commit();
2177 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
)
2179 if (mr
->nonvolatile
!= nonvolatile
) {
2180 memory_region_transaction_begin();
2181 mr
->nonvolatile
= nonvolatile
;
2182 memory_region_update_pending
|= mr
->enabled
;
2183 memory_region_transaction_commit();
2187 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2189 if (mr
->romd_mode
!= romd_mode
) {
2190 memory_region_transaction_begin();
2191 mr
->romd_mode
= romd_mode
;
2192 memory_region_update_pending
|= mr
->enabled
;
2193 memory_region_transaction_commit();
2197 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2198 hwaddr size
, unsigned client
)
2200 assert(mr
->ram_block
);
2201 cpu_physical_memory_test_and_clear_dirty(
2202 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2205 int memory_region_get_fd(MemoryRegion
*mr
)
2213 fd
= mr
->ram_block
->fd
;
2219 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2222 uint64_t offset
= 0;
2226 offset
+= mr
->alias_offset
;
2229 assert(mr
->ram_block
);
2230 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2236 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2240 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2248 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2250 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2253 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2255 assert(mr
->ram_block
);
2257 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2260 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
2265 view
= address_space_get_flatview(as
);
2266 FOR_EACH_FLAT_RANGE(fr
, view
) {
2268 flat_range_coalesced_io_del(fr
, as
);
2269 flat_range_coalesced_io_add(fr
, as
);
2272 flatview_unref(view
);
2275 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
2279 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2280 memory_region_update_coalesced_range_as(mr
, as
);
2284 void memory_region_set_coalescing(MemoryRegion
*mr
)
2286 memory_region_clear_coalescing(mr
);
2287 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2290 void memory_region_add_coalescing(MemoryRegion
*mr
,
2294 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2296 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2297 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2298 memory_region_update_coalesced_range(mr
);
2299 memory_region_set_flush_coalesced(mr
);
2302 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2304 CoalescedMemoryRange
*cmr
;
2306 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2310 qemu_flush_coalesced_mmio_buffer();
2311 mr
->flush_coalesced_mmio
= false;
2313 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2314 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2315 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2319 memory_region_update_coalesced_range(mr
);
2322 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2324 mr
->flush_coalesced_mmio
= true;
2327 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2329 qemu_flush_coalesced_mmio_buffer();
2330 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2331 mr
->flush_coalesced_mmio
= false;
2335 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2337 mr
->global_locking
= false;
2340 static bool userspace_eventfd_warning
;
2342 void memory_region_add_eventfd(MemoryRegion
*mr
,
2349 MemoryRegionIoeventfd mrfd
= {
2350 .addr
.start
= int128_make64(addr
),
2351 .addr
.size
= int128_make64(size
),
2352 .match_data
= match_data
,
2358 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2359 userspace_eventfd_warning
))) {
2360 userspace_eventfd_warning
= true;
2361 error_report("Using eventfd without MMIO binding in KVM. "
2362 "Suboptimal performance expected");
2366 adjust_endianness(mr
, &mrfd
.data
, size
);
2368 memory_region_transaction_begin();
2369 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2370 if (memory_region_ioeventfd_before(&mrfd
, &mr
->ioeventfds
[i
])) {
2375 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2376 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2377 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2378 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2379 mr
->ioeventfds
[i
] = mrfd
;
2380 ioeventfd_update_pending
|= mr
->enabled
;
2381 memory_region_transaction_commit();
2384 void memory_region_del_eventfd(MemoryRegion
*mr
,
2391 MemoryRegionIoeventfd mrfd
= {
2392 .addr
.start
= int128_make64(addr
),
2393 .addr
.size
= int128_make64(size
),
2394 .match_data
= match_data
,
2401 adjust_endianness(mr
, &mrfd
.data
, size
);
2403 memory_region_transaction_begin();
2404 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2405 if (memory_region_ioeventfd_equal(&mrfd
, &mr
->ioeventfds
[i
])) {
2409 assert(i
!= mr
->ioeventfd_nb
);
2410 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2411 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2413 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2414 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2415 ioeventfd_update_pending
|= mr
->enabled
;
2416 memory_region_transaction_commit();
2419 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2421 MemoryRegion
*mr
= subregion
->container
;
2422 MemoryRegion
*other
;
2424 memory_region_transaction_begin();
2426 memory_region_ref(subregion
);
2427 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2428 if (subregion
->priority
>= other
->priority
) {
2429 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2433 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2435 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2436 memory_region_transaction_commit();
2439 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2441 MemoryRegion
*subregion
)
2443 assert(!subregion
->container
);
2444 subregion
->container
= mr
;
2445 subregion
->addr
= offset
;
2446 memory_region_update_container_subregions(subregion
);
2449 void memory_region_add_subregion(MemoryRegion
*mr
,
2451 MemoryRegion
*subregion
)
2453 subregion
->priority
= 0;
2454 memory_region_add_subregion_common(mr
, offset
, subregion
);
2457 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2459 MemoryRegion
*subregion
,
2462 subregion
->priority
= priority
;
2463 memory_region_add_subregion_common(mr
, offset
, subregion
);
2466 void memory_region_del_subregion(MemoryRegion
*mr
,
2467 MemoryRegion
*subregion
)
2469 memory_region_transaction_begin();
2470 assert(subregion
->container
== mr
);
2471 subregion
->container
= NULL
;
2472 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2473 memory_region_unref(subregion
);
2474 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2475 memory_region_transaction_commit();
2478 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2480 if (enabled
== mr
->enabled
) {
2483 memory_region_transaction_begin();
2484 mr
->enabled
= enabled
;
2485 memory_region_update_pending
= true;
2486 memory_region_transaction_commit();
2489 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2491 Int128 s
= int128_make64(size
);
2493 if (size
== UINT64_MAX
) {
2496 if (int128_eq(s
, mr
->size
)) {
2499 memory_region_transaction_begin();
2501 memory_region_update_pending
= true;
2502 memory_region_transaction_commit();
2505 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2507 MemoryRegion
*container
= mr
->container
;
2510 memory_region_transaction_begin();
2511 memory_region_ref(mr
);
2512 memory_region_del_subregion(container
, mr
);
2513 mr
->container
= container
;
2514 memory_region_update_container_subregions(mr
);
2515 memory_region_unref(mr
);
2516 memory_region_transaction_commit();
2520 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2522 if (addr
!= mr
->addr
) {
2524 memory_region_readd_subregion(mr
);
2528 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2532 if (offset
== mr
->alias_offset
) {
2536 memory_region_transaction_begin();
2537 mr
->alias_offset
= offset
;
2538 memory_region_update_pending
|= mr
->enabled
;
2539 memory_region_transaction_commit();
2542 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2547 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2549 const AddrRange
*addr
= addr_
;
2550 const FlatRange
*fr
= fr_
;
2552 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2554 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2560 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2562 return bsearch(&addr
, view
->ranges
, view
->nr
,
2563 sizeof(FlatRange
), cmp_flatrange_addr
);
2566 bool memory_region_is_mapped(MemoryRegion
*mr
)
2568 return mr
->container
? true : false;
2571 /* Same as memory_region_find, but it does not add a reference to the
2572 * returned region. It must be called from an RCU critical section.
2574 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2575 hwaddr addr
, uint64_t size
)
2577 MemoryRegionSection ret
= { .mr
= NULL
};
2585 for (root
= mr
; root
->container
; ) {
2586 root
= root
->container
;
2590 as
= memory_region_to_address_space(root
);
2594 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2596 view
= address_space_to_flatview(as
);
2597 fr
= flatview_lookup(view
, range
);
2602 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2608 range
= addrrange_intersection(range
, fr
->addr
);
2609 ret
.offset_within_region
= fr
->offset_in_region
;
2610 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2612 ret
.size
= range
.size
;
2613 ret
.offset_within_address_space
= int128_get64(range
.start
);
2614 ret
.readonly
= fr
->readonly
;
2615 ret
.nonvolatile
= fr
->nonvolatile
;
2619 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2620 hwaddr addr
, uint64_t size
)
2622 MemoryRegionSection ret
;
2624 ret
= memory_region_find_rcu(mr
, addr
, size
);
2626 memory_region_ref(ret
.mr
);
2632 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2637 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2639 return mr
&& mr
!= container
;
2642 void memory_global_dirty_log_sync(void)
2644 memory_region_sync_dirty_bitmap(NULL
);
2647 void memory_global_after_dirty_log_sync(void)
2649 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync
, Forward
);
2652 static VMChangeStateEntry
*vmstate_change
;
2654 void memory_global_dirty_log_start(void)
2656 if (vmstate_change
) {
2657 qemu_del_vm_change_state_handler(vmstate_change
);
2658 vmstate_change
= NULL
;
2661 global_dirty_log
= true;
2663 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2665 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2666 memory_region_transaction_begin();
2667 memory_region_update_pending
= true;
2668 memory_region_transaction_commit();
2671 static void memory_global_dirty_log_do_stop(void)
2673 global_dirty_log
= false;
2675 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2676 memory_region_transaction_begin();
2677 memory_region_update_pending
= true;
2678 memory_region_transaction_commit();
2680 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2683 static void memory_vm_change_state_handler(void *opaque
, int running
,
2687 memory_global_dirty_log_do_stop();
2689 if (vmstate_change
) {
2690 qemu_del_vm_change_state_handler(vmstate_change
);
2691 vmstate_change
= NULL
;
2696 void memory_global_dirty_log_stop(void)
2698 if (!runstate_is_running()) {
2699 if (vmstate_change
) {
2702 vmstate_change
= qemu_add_vm_change_state_handler(
2703 memory_vm_change_state_handler
, NULL
);
2707 memory_global_dirty_log_do_stop();
2710 static void listener_add_address_space(MemoryListener
*listener
,
2716 if (listener
->begin
) {
2717 listener
->begin(listener
);
2719 if (global_dirty_log
) {
2720 if (listener
->log_global_start
) {
2721 listener
->log_global_start(listener
);
2725 view
= address_space_get_flatview(as
);
2726 FOR_EACH_FLAT_RANGE(fr
, view
) {
2727 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2729 if (listener
->region_add
) {
2730 listener
->region_add(listener
, §ion
);
2732 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2733 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2736 if (listener
->commit
) {
2737 listener
->commit(listener
);
2739 flatview_unref(view
);
2742 static void listener_del_address_space(MemoryListener
*listener
,
2748 if (listener
->begin
) {
2749 listener
->begin(listener
);
2751 view
= address_space_get_flatview(as
);
2752 FOR_EACH_FLAT_RANGE(fr
, view
) {
2753 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2755 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2756 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2758 if (listener
->region_del
) {
2759 listener
->region_del(listener
, §ion
);
2762 if (listener
->commit
) {
2763 listener
->commit(listener
);
2765 flatview_unref(view
);
2768 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2770 MemoryListener
*other
= NULL
;
2772 listener
->address_space
= as
;
2773 if (QTAILQ_EMPTY(&memory_listeners
)
2774 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
)->priority
) {
2775 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2777 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2778 if (listener
->priority
< other
->priority
) {
2782 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2785 if (QTAILQ_EMPTY(&as
->listeners
)
2786 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
)->priority
) {
2787 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2789 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2790 if (listener
->priority
< other
->priority
) {
2794 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2797 listener_add_address_space(listener
, as
);
2800 void memory_listener_unregister(MemoryListener
*listener
)
2802 if (!listener
->address_space
) {
2806 listener_del_address_space(listener
, listener
->address_space
);
2807 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2808 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2809 listener
->address_space
= NULL
;
2812 void address_space_remove_listeners(AddressSpace
*as
)
2814 while (!QTAILQ_EMPTY(&as
->listeners
)) {
2815 memory_listener_unregister(QTAILQ_FIRST(&as
->listeners
));
2819 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2821 memory_region_ref(root
);
2823 as
->current_map
= NULL
;
2824 as
->ioeventfd_nb
= 0;
2825 as
->ioeventfds
= NULL
;
2826 QTAILQ_INIT(&as
->listeners
);
2827 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2828 as
->name
= g_strdup(name
? name
: "anonymous");
2829 address_space_update_topology(as
);
2830 address_space_update_ioeventfds(as
);
2833 static void do_address_space_destroy(AddressSpace
*as
)
2835 assert(QTAILQ_EMPTY(&as
->listeners
));
2837 flatview_unref(as
->current_map
);
2839 g_free(as
->ioeventfds
);
2840 memory_region_unref(as
->root
);
2843 void address_space_destroy(AddressSpace
*as
)
2845 MemoryRegion
*root
= as
->root
;
2847 /* Flush out anything from MemoryListeners listening in on this */
2848 memory_region_transaction_begin();
2850 memory_region_transaction_commit();
2851 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2853 /* At this point, as->dispatch and as->current_map are dummy
2854 * entries that the guest should never use. Wait for the old
2855 * values to expire before freeing the data.
2858 call_rcu(as
, do_address_space_destroy
, rcu
);
2861 static const char *memory_region_type(MemoryRegion
*mr
)
2863 if (memory_region_is_ram_device(mr
)) {
2865 } else if (memory_region_is_romd(mr
)) {
2867 } else if (memory_region_is_rom(mr
)) {
2869 } else if (memory_region_is_ram(mr
)) {
2876 typedef struct MemoryRegionList MemoryRegionList
;
2878 struct MemoryRegionList
{
2879 const MemoryRegion
*mr
;
2880 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2883 typedef QTAILQ_HEAD(, MemoryRegionList
) MemoryRegionListHead
;
2885 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2886 int128_sub((size), int128_one())) : 0)
2887 #define MTREE_INDENT " "
2889 static void mtree_expand_owner(const char *label
, Object
*obj
)
2891 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(obj
, TYPE_DEVICE
);
2893 qemu_printf(" %s:{%s", label
, dev
? "dev" : "obj");
2894 if (dev
&& dev
->id
) {
2895 qemu_printf(" id=%s", dev
->id
);
2897 gchar
*canonical_path
= object_get_canonical_path(obj
);
2898 if (canonical_path
) {
2899 qemu_printf(" path=%s", canonical_path
);
2900 g_free(canonical_path
);
2902 qemu_printf(" type=%s", object_get_typename(obj
));
2908 static void mtree_print_mr_owner(const MemoryRegion
*mr
)
2910 Object
*owner
= mr
->owner
;
2911 Object
*parent
= memory_region_owner((MemoryRegion
*)mr
);
2913 if (!owner
&& !parent
) {
2914 qemu_printf(" orphan");
2918 mtree_expand_owner("owner", owner
);
2920 if (parent
&& parent
!= owner
) {
2921 mtree_expand_owner("parent", parent
);
2925 static void mtree_print_mr(const MemoryRegion
*mr
, unsigned int level
,
2927 MemoryRegionListHead
*alias_print_queue
,
2930 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2931 MemoryRegionListHead submr_print_queue
;
2932 const MemoryRegion
*submr
;
2934 hwaddr cur_start
, cur_end
;
2940 for (i
= 0; i
< level
; i
++) {
2941 qemu_printf(MTREE_INDENT
);
2944 cur_start
= base
+ mr
->addr
;
2945 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2948 * Try to detect overflow of memory region. This should never
2949 * happen normally. When it happens, we dump something to warn the
2950 * user who is observing this.
2952 if (cur_start
< base
|| cur_end
< cur_start
) {
2953 qemu_printf("[DETECTED OVERFLOW!] ");
2957 MemoryRegionList
*ml
;
2960 /* check if the alias is already in the queue */
2961 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2962 if (ml
->mr
== mr
->alias
) {
2968 ml
= g_new(MemoryRegionList
, 1);
2970 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2972 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2973 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2974 "-" TARGET_FMT_plx
"%s",
2977 mr
->nonvolatile
? "nv-" : "",
2978 memory_region_type((MemoryRegion
*)mr
),
2979 memory_region_name(mr
),
2980 memory_region_name(mr
->alias
),
2982 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2983 mr
->enabled
? "" : " [disabled]");
2985 mtree_print_mr_owner(mr
);
2988 qemu_printf(TARGET_FMT_plx
"-" TARGET_FMT_plx
2989 " (prio %d, %s%s): %s%s",
2992 mr
->nonvolatile
? "nv-" : "",
2993 memory_region_type((MemoryRegion
*)mr
),
2994 memory_region_name(mr
),
2995 mr
->enabled
? "" : " [disabled]");
2997 mtree_print_mr_owner(mr
);
3002 QTAILQ_INIT(&submr_print_queue
);
3004 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
3005 new_ml
= g_new(MemoryRegionList
, 1);
3007 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3008 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
3009 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
3010 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
3011 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
3017 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
3021 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
3022 mtree_print_mr(ml
->mr
, level
+ 1, cur_start
,
3023 alias_print_queue
, owner
);
3026 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
3031 struct FlatViewInfo
{
3036 const char *ac_name
;
3039 static void mtree_print_flatview(gpointer key
, gpointer value
,
3042 FlatView
*view
= key
;
3043 GArray
*fv_address_spaces
= value
;
3044 struct FlatViewInfo
*fvi
= user_data
;
3045 FlatRange
*range
= &view
->ranges
[0];
3051 qemu_printf("FlatView #%d\n", fvi
->counter
);
3054 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3055 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3056 qemu_printf(" AS \"%s\", root: %s",
3057 as
->name
, memory_region_name(as
->root
));
3058 if (as
->root
->alias
) {
3059 qemu_printf(", alias %s", memory_region_name(as
->root
->alias
));
3064 qemu_printf(" Root memory region: %s\n",
3065 view
->root
? memory_region_name(view
->root
) : "(none)");
3068 qemu_printf(MTREE_INDENT
"No rendered FlatView\n\n");
3074 if (range
->offset_in_region
) {
3075 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3076 " (prio %d, %s%s): %s @" TARGET_FMT_plx
,
3077 int128_get64(range
->addr
.start
),
3078 int128_get64(range
->addr
.start
)
3079 + MR_SIZE(range
->addr
.size
),
3081 range
->nonvolatile
? "nv-" : "",
3082 range
->readonly
? "rom" : memory_region_type(mr
),
3083 memory_region_name(mr
),
3084 range
->offset_in_region
);
3086 qemu_printf(MTREE_INDENT TARGET_FMT_plx
"-" TARGET_FMT_plx
3087 " (prio %d, %s%s): %s",
3088 int128_get64(range
->addr
.start
),
3089 int128_get64(range
->addr
.start
)
3090 + MR_SIZE(range
->addr
.size
),
3092 range
->nonvolatile
? "nv-" : "",
3093 range
->readonly
? "rom" : memory_region_type(mr
),
3094 memory_region_name(mr
));
3097 mtree_print_mr_owner(mr
);
3101 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
3102 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
3103 if (fvi
->ac
->has_memory(current_machine
, as
,
3104 int128_get64(range
->addr
.start
),
3105 MR_SIZE(range
->addr
.size
) + 1)) {
3106 qemu_printf(" %s", fvi
->ac_name
);
3114 #if !defined(CONFIG_USER_ONLY)
3115 if (fvi
->dispatch_tree
&& view
->root
) {
3116 mtree_print_dispatch(view
->dispatch
, view
->root
);
3123 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3126 FlatView
*view
= key
;
3127 GArray
*fv_address_spaces
= value
;
3129 g_array_unref(fv_address_spaces
);
3130 flatview_unref(view
);
3135 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
)
3137 MemoryRegionListHead ml_head
;
3138 MemoryRegionList
*ml
, *ml2
;
3143 struct FlatViewInfo fvi
= {
3145 .dispatch_tree
= dispatch_tree
,
3148 GArray
*fv_address_spaces
;
3149 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3150 AccelClass
*ac
= ACCEL_GET_CLASS(current_machine
->accelerator
);
3152 if (ac
->has_memory
) {
3154 fvi
.ac_name
= current_machine
->accel
? current_machine
->accel
:
3155 object_class_get_name(OBJECT_CLASS(ac
));
3158 /* Gather all FVs in one table */
3159 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3160 view
= address_space_get_flatview(as
);
3162 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3163 if (!fv_address_spaces
) {
3164 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3165 g_hash_table_insert(views
, view
, fv_address_spaces
);
3168 g_array_append_val(fv_address_spaces
, as
);
3172 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3175 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3176 g_hash_table_unref(views
);
3181 QTAILQ_INIT(&ml_head
);
3183 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3184 qemu_printf("address-space: %s\n", as
->name
);
3185 mtree_print_mr(as
->root
, 1, 0, &ml_head
, owner
);
3189 /* print aliased regions */
3190 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3191 qemu_printf("memory-region: %s\n", memory_region_name(ml
->mr
));
3192 mtree_print_mr(ml
->mr
, 1, 0, &ml_head
, owner
);
3196 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3201 void memory_region_init_ram(MemoryRegion
*mr
,
3202 struct Object
*owner
,
3207 DeviceState
*owner_dev
;
3210 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3212 error_propagate(errp
, err
);
3215 /* This will assert if owner is neither NULL nor a DeviceState.
3216 * We only want the owner here for the purposes of defining a
3217 * unique name for migration. TODO: Ideally we should implement
3218 * a naming scheme for Objects which are not DeviceStates, in
3219 * which case we can relax this restriction.
3221 owner_dev
= DEVICE(owner
);
3222 vmstate_register_ram(mr
, owner_dev
);
3225 void memory_region_init_rom(MemoryRegion
*mr
,
3226 struct Object
*owner
,
3231 DeviceState
*owner_dev
;
3234 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3236 error_propagate(errp
, err
);
3239 /* This will assert if owner is neither NULL nor a DeviceState.
3240 * We only want the owner here for the purposes of defining a
3241 * unique name for migration. TODO: Ideally we should implement
3242 * a naming scheme for Objects which are not DeviceStates, in
3243 * which case we can relax this restriction.
3245 owner_dev
= DEVICE(owner
);
3246 vmstate_register_ram(mr
, owner_dev
);
3249 void memory_region_init_rom_device(MemoryRegion
*mr
,
3250 struct Object
*owner
,
3251 const MemoryRegionOps
*ops
,
3257 DeviceState
*owner_dev
;
3260 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3263 error_propagate(errp
, err
);
3266 /* This will assert if owner is neither NULL nor a DeviceState.
3267 * We only want the owner here for the purposes of defining a
3268 * unique name for migration. TODO: Ideally we should implement
3269 * a naming scheme for Objects which are not DeviceStates, in
3270 * which case we can relax this restriction.
3272 owner_dev
= DEVICE(owner
);
3273 vmstate_register_ram(mr
, owner_dev
);
3276 static const TypeInfo memory_region_info
= {
3277 .parent
= TYPE_OBJECT
,
3278 .name
= TYPE_MEMORY_REGION
,
3279 .class_size
= sizeof(MemoryRegionClass
),
3280 .instance_size
= sizeof(MemoryRegion
),
3281 .instance_init
= memory_region_initfn
,
3282 .instance_finalize
= memory_region_finalize
,
3285 static const TypeInfo iommu_memory_region_info
= {
3286 .parent
= TYPE_MEMORY_REGION
,
3287 .name
= TYPE_IOMMU_MEMORY_REGION
,
3288 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3289 .instance_size
= sizeof(IOMMUMemoryRegion
),
3290 .instance_init
= iommu_memory_region_initfn
,
3294 static void memory_register_types(void)
3296 type_register_static(&memory_region_info
);
3297 type_register_static(&iommu_memory_region_info
);
3300 type_init(memory_register_types
)