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 "exec/memory.h"
18 #include "exec/address-spaces.h"
19 #include "exec/ioport.h"
20 #include "qapi/visitor.h"
21 #include "qemu/bitops.h"
22 #include "qemu/error-report.h"
23 #include "qom/object.h"
26 #include "exec/memory-internal.h"
27 #include "exec/ram_addr.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/sysemu.h"
31 //#define DEBUG_UNASSIGNED
33 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
35 static unsigned memory_region_transaction_depth
;
36 static bool memory_region_update_pending
;
37 static bool ioeventfd_update_pending
;
38 static bool global_dirty_log
= false;
40 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
41 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
43 static QTAILQ_HEAD(, AddressSpace
) address_spaces
44 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
46 typedef struct AddrRange AddrRange
;
49 * Note that signed integers are needed for negative offsetting in aliases
50 * (large MemoryRegion::alias_offset).
57 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
59 return (AddrRange
) { start
, size
};
62 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
64 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
67 static Int128
addrrange_end(AddrRange r
)
69 return int128_add(r
.start
, r
.size
);
72 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
74 int128_addto(&range
.start
, delta
);
78 static bool addrrange_contains(AddrRange range
, Int128 addr
)
80 return int128_ge(addr
, range
.start
)
81 && int128_lt(addr
, addrrange_end(range
));
84 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
86 return addrrange_contains(r1
, r2
.start
)
87 || addrrange_contains(r2
, r1
.start
);
90 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
92 Int128 start
= int128_max(r1
.start
, r2
.start
);
93 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
94 return addrrange_make(start
, int128_sub(end
, start
));
97 enum ListenerDirection
{ Forward
, Reverse
};
99 static bool memory_listener_match(MemoryListener
*listener
,
100 MemoryRegionSection
*section
)
102 return !listener
->address_space_filter
103 || listener
->address_space_filter
== section
->address_space
;
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, \
120 memory_listeners, link) { \
121 if (_listener->_callback) { \
122 _listener->_callback(_listener, ##_args); \
131 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
133 MemoryListener *_listener; \
135 switch (_direction) { \
137 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
138 if (_listener->_callback \
139 && memory_listener_match(_listener, _section)) { \
140 _listener->_callback(_listener, _section, ##_args); \
145 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
146 memory_listeners, link) { \
147 if (_listener->_callback \
148 && memory_listener_match(_listener, _section)) { \
149 _listener->_callback(_listener, _section, ##_args); \
158 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
159 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
160 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
162 .address_space = (as), \
163 .offset_within_region = (fr)->offset_in_region, \
164 .size = (fr)->addr.size, \
165 .offset_within_address_space = int128_get64((fr)->addr.start), \
166 .readonly = (fr)->readonly, \
169 struct CoalescedMemoryRange
{
171 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
174 struct MemoryRegionIoeventfd
{
181 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
182 MemoryRegionIoeventfd b
)
184 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
186 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
188 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
190 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
192 } else if (a
.match_data
< b
.match_data
) {
194 } else if (a
.match_data
> b
.match_data
) {
196 } else if (a
.match_data
) {
197 if (a
.data
< b
.data
) {
199 } else if (a
.data
> b
.data
) {
205 } else if (a
.e
> b
.e
) {
211 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
212 MemoryRegionIoeventfd b
)
214 return !memory_region_ioeventfd_before(a
, b
)
215 && !memory_region_ioeventfd_before(b
, a
);
218 typedef struct FlatRange FlatRange
;
219 typedef struct FlatView FlatView
;
221 /* Range of memory in the global map. Addresses are absolute. */
224 hwaddr offset_in_region
;
226 uint8_t dirty_log_mask
;
231 /* Flattened global view of current active memory hierarchy. Kept in sorted
239 unsigned nr_allocated
;
242 typedef struct AddressSpaceOps AddressSpaceOps
;
244 #define FOR_EACH_FLAT_RANGE(var, view) \
245 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
247 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
249 return a
->mr
== b
->mr
250 && addrrange_equal(a
->addr
, b
->addr
)
251 && a
->offset_in_region
== b
->offset_in_region
252 && a
->romd_mode
== b
->romd_mode
253 && a
->readonly
== b
->readonly
;
256 static void flatview_init(FlatView
*view
)
261 view
->nr_allocated
= 0;
264 /* Insert a range into a given position. Caller is responsible for maintaining
267 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
269 if (view
->nr
== view
->nr_allocated
) {
270 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
271 view
->ranges
= g_realloc(view
->ranges
,
272 view
->nr_allocated
* sizeof(*view
->ranges
));
274 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
275 (view
->nr
- pos
) * sizeof(FlatRange
));
276 view
->ranges
[pos
] = *range
;
277 memory_region_ref(range
->mr
);
281 static void flatview_destroy(FlatView
*view
)
285 for (i
= 0; i
< view
->nr
; i
++) {
286 memory_region_unref(view
->ranges
[i
].mr
);
288 g_free(view
->ranges
);
292 static void flatview_ref(FlatView
*view
)
294 atomic_inc(&view
->ref
);
297 static void flatview_unref(FlatView
*view
)
299 if (atomic_fetch_dec(&view
->ref
) == 1) {
300 flatview_destroy(view
);
304 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
306 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
308 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
310 int128_make64(r2
->offset_in_region
))
311 && r1
->dirty_log_mask
== r2
->dirty_log_mask
312 && r1
->romd_mode
== r2
->romd_mode
313 && r1
->readonly
== r2
->readonly
;
316 /* Attempt to simplify a view by merging adjacent ranges */
317 static void flatview_simplify(FlatView
*view
)
322 while (i
< view
->nr
) {
325 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
326 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
330 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
331 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
336 static bool memory_region_big_endian(MemoryRegion
*mr
)
338 #ifdef TARGET_WORDS_BIGENDIAN
339 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
341 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
345 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
347 #ifdef TARGET_WORDS_BIGENDIAN
348 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
350 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
354 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
356 if (memory_region_wrong_endianness(mr
)) {
361 *data
= bswap16(*data
);
364 *data
= bswap32(*data
);
367 *data
= bswap64(*data
);
375 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
378 hwaddr abs_addr
= offset
;
380 abs_addr
+= mr
->addr
;
381 for (root
= mr
; root
->container
; ) {
382 root
= root
->container
;
383 abs_addr
+= root
->addr
;
389 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
399 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
401 trace_memory_region_subpage_read(mr
, addr
, tmp
, size
);
402 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
403 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
404 trace_memory_region_ops_read(mr
, abs_addr
, tmp
, size
);
406 *value
|= (tmp
& mask
) << shift
;
410 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
420 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
422 trace_memory_region_subpage_read(mr
, addr
, tmp
, size
);
423 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
424 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
425 trace_memory_region_ops_read(mr
, abs_addr
, tmp
, size
);
427 *value
|= (tmp
& mask
) << shift
;
431 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
442 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
444 trace_memory_region_subpage_read(mr
, addr
, tmp
, size
);
445 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
446 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
447 trace_memory_region_ops_read(mr
, abs_addr
, tmp
, size
);
449 *value
|= (tmp
& mask
) << shift
;
453 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
463 tmp
= (*value
>> shift
) & mask
;
465 trace_memory_region_subpage_write(mr
, addr
, tmp
, size
);
466 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
467 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
468 trace_memory_region_ops_write(mr
, abs_addr
, tmp
, size
);
470 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
474 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
484 tmp
= (*value
>> shift
) & mask
;
486 trace_memory_region_subpage_write(mr
, addr
, tmp
, size
);
487 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
488 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
489 trace_memory_region_ops_write(mr
, abs_addr
, tmp
, size
);
491 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
495 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
505 tmp
= (*value
>> shift
) & mask
;
507 trace_memory_region_subpage_write(mr
, addr
, tmp
, size
);
508 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
509 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
510 trace_memory_region_ops_write(mr
, abs_addr
, tmp
, size
);
512 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
515 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
518 unsigned access_size_min
,
519 unsigned access_size_max
,
520 MemTxResult (*access
)(MemoryRegion
*mr
,
530 uint64_t access_mask
;
531 unsigned access_size
;
533 MemTxResult r
= MEMTX_OK
;
535 if (!access_size_min
) {
538 if (!access_size_max
) {
542 /* FIXME: support unaligned access? */
543 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
544 access_mask
= -1ULL >> (64 - access_size
* 8);
545 if (memory_region_big_endian(mr
)) {
546 for (i
= 0; i
< size
; i
+= access_size
) {
547 r
|= access(mr
, addr
+ i
, value
, access_size
,
548 (size
- access_size
- i
) * 8, access_mask
, attrs
);
551 for (i
= 0; i
< size
; i
+= access_size
) {
552 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
559 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
563 while (mr
->container
) {
566 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
567 if (mr
== as
->root
) {
574 /* Render a memory region into the global view. Ranges in @view obscure
577 static void render_memory_region(FlatView
*view
,
583 MemoryRegion
*subregion
;
585 hwaddr offset_in_region
;
595 int128_addto(&base
, int128_make64(mr
->addr
));
596 readonly
|= mr
->readonly
;
598 tmp
= addrrange_make(base
, mr
->size
);
600 if (!addrrange_intersects(tmp
, clip
)) {
604 clip
= addrrange_intersection(tmp
, clip
);
607 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
608 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
609 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
613 /* Render subregions in priority order. */
614 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
615 render_memory_region(view
, subregion
, base
, clip
, readonly
);
618 if (!mr
->terminates
) {
622 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
627 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
628 fr
.romd_mode
= mr
->romd_mode
;
629 fr
.readonly
= readonly
;
631 /* Render the region itself into any gaps left by the current view. */
632 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
633 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
636 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
637 now
= int128_min(remain
,
638 int128_sub(view
->ranges
[i
].addr
.start
, base
));
639 fr
.offset_in_region
= offset_in_region
;
640 fr
.addr
= addrrange_make(base
, now
);
641 flatview_insert(view
, i
, &fr
);
643 int128_addto(&base
, now
);
644 offset_in_region
+= int128_get64(now
);
645 int128_subfrom(&remain
, now
);
647 now
= int128_sub(int128_min(int128_add(base
, remain
),
648 addrrange_end(view
->ranges
[i
].addr
)),
650 int128_addto(&base
, now
);
651 offset_in_region
+= int128_get64(now
);
652 int128_subfrom(&remain
, now
);
654 if (int128_nz(remain
)) {
655 fr
.offset_in_region
= offset_in_region
;
656 fr
.addr
= addrrange_make(base
, remain
);
657 flatview_insert(view
, i
, &fr
);
661 /* Render a memory topology into a list of disjoint absolute ranges. */
662 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
666 view
= g_new(FlatView
, 1);
670 render_memory_region(view
, mr
, int128_zero(),
671 addrrange_make(int128_zero(), int128_2_64()), false);
673 flatview_simplify(view
);
678 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
679 MemoryRegionIoeventfd
*fds_new
,
681 MemoryRegionIoeventfd
*fds_old
,
685 MemoryRegionIoeventfd
*fd
;
686 MemoryRegionSection section
;
688 /* Generate a symmetric difference of the old and new fd sets, adding
689 * and deleting as necessary.
693 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
694 if (iold
< fds_old_nb
695 && (inew
== fds_new_nb
696 || memory_region_ioeventfd_before(fds_old
[iold
],
699 section
= (MemoryRegionSection
) {
701 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
702 .size
= fd
->addr
.size
,
704 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
705 fd
->match_data
, fd
->data
, fd
->e
);
707 } else if (inew
< fds_new_nb
708 && (iold
== fds_old_nb
709 || memory_region_ioeventfd_before(fds_new
[inew
],
712 section
= (MemoryRegionSection
) {
714 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
715 .size
= fd
->addr
.size
,
717 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
718 fd
->match_data
, fd
->data
, fd
->e
);
727 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
732 view
= atomic_rcu_read(&as
->current_map
);
738 static void address_space_update_ioeventfds(AddressSpace
*as
)
742 unsigned ioeventfd_nb
= 0;
743 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
747 view
= address_space_get_flatview(as
);
748 FOR_EACH_FLAT_RANGE(fr
, view
) {
749 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
750 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
751 int128_sub(fr
->addr
.start
,
752 int128_make64(fr
->offset_in_region
)));
753 if (addrrange_intersects(fr
->addr
, tmp
)) {
755 ioeventfds
= g_realloc(ioeventfds
,
756 ioeventfd_nb
* sizeof(*ioeventfds
));
757 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
758 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
763 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
764 as
->ioeventfds
, as
->ioeventfd_nb
);
766 g_free(as
->ioeventfds
);
767 as
->ioeventfds
= ioeventfds
;
768 as
->ioeventfd_nb
= ioeventfd_nb
;
769 flatview_unref(view
);
772 static void address_space_update_topology_pass(AddressSpace
*as
,
773 const FlatView
*old_view
,
774 const FlatView
*new_view
,
778 FlatRange
*frold
, *frnew
;
780 /* Generate a symmetric difference of the old and new memory maps.
781 * Kill ranges in the old map, and instantiate ranges in the new map.
784 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
785 if (iold
< old_view
->nr
) {
786 frold
= &old_view
->ranges
[iold
];
790 if (inew
< new_view
->nr
) {
791 frnew
= &new_view
->ranges
[inew
];
798 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
799 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
800 && !flatrange_equal(frold
, frnew
)))) {
801 /* In old but not in new, or in both but attributes changed. */
804 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
808 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
809 /* In both and unchanged (except logging may have changed) */
812 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
813 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
814 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
815 frold
->dirty_log_mask
,
816 frnew
->dirty_log_mask
);
818 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
819 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
820 frold
->dirty_log_mask
,
821 frnew
->dirty_log_mask
);
831 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
840 static void address_space_update_topology(AddressSpace
*as
)
842 FlatView
*old_view
= address_space_get_flatview(as
);
843 FlatView
*new_view
= generate_memory_topology(as
->root
);
845 address_space_update_topology_pass(as
, old_view
, new_view
, false);
846 address_space_update_topology_pass(as
, old_view
, new_view
, true);
848 /* Writes are protected by the BQL. */
849 atomic_rcu_set(&as
->current_map
, new_view
);
850 call_rcu(old_view
, flatview_unref
, rcu
);
852 /* Note that all the old MemoryRegions are still alive up to this
853 * point. This relieves most MemoryListeners from the need to
854 * ref/unref the MemoryRegions they get---unless they use them
855 * outside the iothread mutex, in which case precise reference
856 * counting is necessary.
858 flatview_unref(old_view
);
860 address_space_update_ioeventfds(as
);
863 void memory_region_transaction_begin(void)
865 qemu_flush_coalesced_mmio_buffer();
866 ++memory_region_transaction_depth
;
869 static void memory_region_clear_pending(void)
871 memory_region_update_pending
= false;
872 ioeventfd_update_pending
= false;
875 void memory_region_transaction_commit(void)
879 assert(memory_region_transaction_depth
);
880 --memory_region_transaction_depth
;
881 if (!memory_region_transaction_depth
) {
882 if (memory_region_update_pending
) {
883 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
885 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
886 address_space_update_topology(as
);
889 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
890 } else if (ioeventfd_update_pending
) {
891 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
892 address_space_update_ioeventfds(as
);
895 memory_region_clear_pending();
899 static void memory_region_destructor_none(MemoryRegion
*mr
)
903 static void memory_region_destructor_ram(MemoryRegion
*mr
)
905 qemu_ram_free(mr
->ram_addr
);
908 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
910 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
913 static bool memory_region_need_escape(char c
)
915 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
918 static char *memory_region_escape_name(const char *name
)
925 for (p
= name
; *p
; p
++) {
926 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
928 if (bytes
== p
- name
) {
929 return g_memdup(name
, bytes
+ 1);
932 escaped
= g_malloc(bytes
+ 1);
933 for (p
= name
, q
= escaped
; *p
; p
++) {
935 if (unlikely(memory_region_need_escape(c
))) {
938 *q
++ = "0123456789abcdef"[c
>> 4];
939 c
= "0123456789abcdef"[c
& 15];
947 void memory_region_init(MemoryRegion
*mr
,
952 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
953 mr
->size
= int128_make64(size
);
954 if (size
== UINT64_MAX
) {
955 mr
->size
= int128_2_64();
957 mr
->name
= g_strdup(name
);
959 mr
->ram_block
= NULL
;
962 char *escaped_name
= memory_region_escape_name(name
);
963 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
966 owner
= container_get(qdev_get_machine(), "/unattached");
969 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
970 object_unref(OBJECT(mr
));
972 g_free(escaped_name
);
976 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
977 void *opaque
, Error
**errp
)
979 MemoryRegion
*mr
= MEMORY_REGION(obj
);
980 uint64_t value
= mr
->addr
;
982 visit_type_uint64(v
, name
, &value
, errp
);
985 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
986 const char *name
, void *opaque
,
989 MemoryRegion
*mr
= MEMORY_REGION(obj
);
990 gchar
*path
= (gchar
*)"";
993 path
= object_get_canonical_path(OBJECT(mr
->container
));
995 visit_type_str(v
, name
, &path
, errp
);
1001 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1004 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1006 return OBJECT(mr
->container
);
1009 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1010 const char *name
, void *opaque
,
1013 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1014 int32_t value
= mr
->priority
;
1016 visit_type_int32(v
, name
, &value
, errp
);
1019 static bool memory_region_get_may_overlap(Object
*obj
, Error
**errp
)
1021 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1023 return mr
->may_overlap
;
1026 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1027 void *opaque
, Error
**errp
)
1029 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1030 uint64_t value
= memory_region_size(mr
);
1032 visit_type_uint64(v
, name
, &value
, errp
);
1035 static void memory_region_initfn(Object
*obj
)
1037 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1040 mr
->ops
= &unassigned_mem_ops
;
1041 mr
->ram_addr
= RAM_ADDR_INVALID
;
1043 mr
->romd_mode
= true;
1044 mr
->global_locking
= true;
1045 mr
->destructor
= memory_region_destructor_none
;
1046 QTAILQ_INIT(&mr
->subregions
);
1047 QTAILQ_INIT(&mr
->coalesced
);
1049 op
= object_property_add(OBJECT(mr
), "container",
1050 "link<" TYPE_MEMORY_REGION
">",
1051 memory_region_get_container
,
1052 NULL
, /* memory_region_set_container */
1053 NULL
, NULL
, &error_abort
);
1054 op
->resolve
= memory_region_resolve_container
;
1056 object_property_add(OBJECT(mr
), "addr", "uint64",
1057 memory_region_get_addr
,
1058 NULL
, /* memory_region_set_addr */
1059 NULL
, NULL
, &error_abort
);
1060 object_property_add(OBJECT(mr
), "priority", "uint32",
1061 memory_region_get_priority
,
1062 NULL
, /* memory_region_set_priority */
1063 NULL
, NULL
, &error_abort
);
1064 object_property_add_bool(OBJECT(mr
), "may-overlap",
1065 memory_region_get_may_overlap
,
1066 NULL
, /* memory_region_set_may_overlap */
1068 object_property_add(OBJECT(mr
), "size", "uint64",
1069 memory_region_get_size
,
1070 NULL
, /* memory_region_set_size, */
1071 NULL
, NULL
, &error_abort
);
1074 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1077 #ifdef DEBUG_UNASSIGNED
1078 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1080 if (current_cpu
!= NULL
) {
1081 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1086 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1087 uint64_t val
, unsigned size
)
1089 #ifdef DEBUG_UNASSIGNED
1090 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1092 if (current_cpu
!= NULL
) {
1093 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1097 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1098 unsigned size
, bool is_write
)
1103 const MemoryRegionOps unassigned_mem_ops
= {
1104 .valid
.accepts
= unassigned_mem_accepts
,
1105 .endianness
= DEVICE_NATIVE_ENDIAN
,
1108 bool memory_region_access_valid(MemoryRegion
*mr
,
1113 int access_size_min
, access_size_max
;
1116 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1120 if (!mr
->ops
->valid
.accepts
) {
1124 access_size_min
= mr
->ops
->valid
.min_access_size
;
1125 if (!mr
->ops
->valid
.min_access_size
) {
1126 access_size_min
= 1;
1129 access_size_max
= mr
->ops
->valid
.max_access_size
;
1130 if (!mr
->ops
->valid
.max_access_size
) {
1131 access_size_max
= 4;
1134 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1135 for (i
= 0; i
< size
; i
+= access_size
) {
1136 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1145 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1153 if (mr
->ops
->read
) {
1154 return access_with_adjusted_size(addr
, pval
, size
,
1155 mr
->ops
->impl
.min_access_size
,
1156 mr
->ops
->impl
.max_access_size
,
1157 memory_region_read_accessor
,
1159 } else if (mr
->ops
->read_with_attrs
) {
1160 return access_with_adjusted_size(addr
, pval
, size
,
1161 mr
->ops
->impl
.min_access_size
,
1162 mr
->ops
->impl
.max_access_size
,
1163 memory_region_read_with_attrs_accessor
,
1166 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1167 memory_region_oldmmio_read_accessor
,
1172 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1180 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1181 *pval
= unassigned_mem_read(mr
, addr
, size
);
1182 return MEMTX_DECODE_ERROR
;
1185 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1186 adjust_endianness(mr
, pval
, size
);
1190 /* Return true if an eventfd was signalled */
1191 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1197 MemoryRegionIoeventfd ioeventfd
= {
1198 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1203 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1204 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1205 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1207 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1208 event_notifier_set(ioeventfd
.e
);
1216 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1222 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1223 unassigned_mem_write(mr
, addr
, data
, size
);
1224 return MEMTX_DECODE_ERROR
;
1227 adjust_endianness(mr
, &data
, size
);
1229 if ((!kvm_eventfds_enabled()) &&
1230 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1234 if (mr
->ops
->write
) {
1235 return access_with_adjusted_size(addr
, &data
, size
,
1236 mr
->ops
->impl
.min_access_size
,
1237 mr
->ops
->impl
.max_access_size
,
1238 memory_region_write_accessor
, mr
,
1240 } else if (mr
->ops
->write_with_attrs
) {
1242 access_with_adjusted_size(addr
, &data
, size
,
1243 mr
->ops
->impl
.min_access_size
,
1244 mr
->ops
->impl
.max_access_size
,
1245 memory_region_write_with_attrs_accessor
,
1248 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1249 memory_region_oldmmio_write_accessor
,
1254 void memory_region_init_io(MemoryRegion
*mr
,
1256 const MemoryRegionOps
*ops
,
1261 memory_region_init(mr
, owner
, name
, size
);
1262 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1263 mr
->opaque
= opaque
;
1264 mr
->terminates
= true;
1267 void memory_region_init_ram(MemoryRegion
*mr
,
1273 memory_region_init(mr
, owner
, name
, size
);
1275 mr
->terminates
= true;
1276 mr
->destructor
= memory_region_destructor_ram
;
1277 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1278 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1281 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1286 void (*resized
)(const char*,
1291 memory_region_init(mr
, owner
, name
, size
);
1293 mr
->terminates
= true;
1294 mr
->destructor
= memory_region_destructor_ram
;
1295 mr
->ram_addr
= qemu_ram_alloc_resizeable(size
, max_size
, resized
, mr
, errp
);
1296 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1300 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1301 struct Object
*owner
,
1308 memory_region_init(mr
, owner
, name
, size
);
1310 mr
->terminates
= true;
1311 mr
->destructor
= memory_region_destructor_ram
;
1312 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1313 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1317 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1323 memory_region_init(mr
, owner
, name
, size
);
1325 mr
->terminates
= true;
1326 mr
->destructor
= memory_region_destructor_ram
;
1327 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1329 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1330 assert(ptr
!= NULL
);
1331 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1334 void memory_region_set_skip_dump(MemoryRegion
*mr
)
1336 mr
->skip_dump
= true;
1339 void memory_region_init_alias(MemoryRegion
*mr
,
1346 memory_region_init(mr
, owner
, name
, size
);
1348 mr
->alias_offset
= offset
;
1351 void memory_region_init_rom_device(MemoryRegion
*mr
,
1353 const MemoryRegionOps
*ops
,
1359 memory_region_init(mr
, owner
, name
, size
);
1361 mr
->opaque
= opaque
;
1362 mr
->terminates
= true;
1363 mr
->rom_device
= true;
1364 mr
->destructor
= memory_region_destructor_rom_device
;
1365 mr
->ram_addr
= qemu_ram_alloc(size
, mr
, errp
);
1368 void memory_region_init_iommu(MemoryRegion
*mr
,
1370 const MemoryRegionIOMMUOps
*ops
,
1374 memory_region_init(mr
, owner
, name
, size
);
1375 mr
->iommu_ops
= ops
,
1376 mr
->terminates
= true; /* then re-forwards */
1377 notifier_list_init(&mr
->iommu_notify
);
1380 static void memory_region_finalize(Object
*obj
)
1382 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1384 assert(!mr
->container
);
1386 /* We know the region is not visible in any address space (it
1387 * does not have a container and cannot be a root either because
1388 * it has no references, so we can blindly clear mr->enabled.
1389 * memory_region_set_enabled instead could trigger a transaction
1390 * and cause an infinite loop.
1392 mr
->enabled
= false;
1393 memory_region_transaction_begin();
1394 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1395 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1396 memory_region_del_subregion(mr
, subregion
);
1398 memory_region_transaction_commit();
1401 memory_region_clear_coalescing(mr
);
1402 g_free((char *)mr
->name
);
1403 g_free(mr
->ioeventfds
);
1406 Object
*memory_region_owner(MemoryRegion
*mr
)
1408 Object
*obj
= OBJECT(mr
);
1412 void memory_region_ref(MemoryRegion
*mr
)
1414 /* MMIO callbacks most likely will access data that belongs
1415 * to the owner, hence the need to ref/unref the owner whenever
1416 * the memory region is in use.
1418 * The memory region is a child of its owner. As long as the
1419 * owner doesn't call unparent itself on the memory region,
1420 * ref-ing the owner will also keep the memory region alive.
1421 * Memory regions without an owner are supposed to never go away;
1422 * we do not ref/unref them because it slows down DMA sensibly.
1424 if (mr
&& mr
->owner
) {
1425 object_ref(mr
->owner
);
1429 void memory_region_unref(MemoryRegion
*mr
)
1431 if (mr
&& mr
->owner
) {
1432 object_unref(mr
->owner
);
1436 uint64_t memory_region_size(MemoryRegion
*mr
)
1438 if (int128_eq(mr
->size
, int128_2_64())) {
1441 return int128_get64(mr
->size
);
1444 const char *memory_region_name(const MemoryRegion
*mr
)
1447 ((MemoryRegion
*)mr
)->name
=
1448 object_get_canonical_path_component(OBJECT(mr
));
1453 bool memory_region_is_skip_dump(MemoryRegion
*mr
)
1455 return mr
->skip_dump
;
1458 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1460 uint8_t mask
= mr
->dirty_log_mask
;
1461 if (global_dirty_log
) {
1462 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1467 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1469 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1472 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1474 notifier_list_add(&mr
->iommu_notify
, n
);
1477 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
,
1478 hwaddr granularity
, bool is_write
)
1481 IOMMUTLBEntry iotlb
;
1483 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1484 iotlb
= mr
->iommu_ops
->translate(mr
, addr
, is_write
);
1485 if (iotlb
.perm
!= IOMMU_NONE
) {
1486 n
->notify(n
, &iotlb
);
1489 /* if (2^64 - MR size) < granularity, it's possible to get an
1490 * infinite loop here. This should catch such a wraparound */
1491 if ((addr
+ granularity
) < addr
) {
1497 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1502 void memory_region_notify_iommu(MemoryRegion
*mr
,
1503 IOMMUTLBEntry entry
)
1505 assert(memory_region_is_iommu(mr
));
1506 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1509 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1511 uint8_t mask
= 1 << client
;
1512 uint8_t old_logging
;
1514 assert(client
== DIRTY_MEMORY_VGA
);
1515 old_logging
= mr
->vga_logging_count
;
1516 mr
->vga_logging_count
+= log
? 1 : -1;
1517 if (!!old_logging
== !!mr
->vga_logging_count
) {
1521 memory_region_transaction_begin();
1522 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1523 memory_region_update_pending
|= mr
->enabled
;
1524 memory_region_transaction_commit();
1527 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1528 hwaddr size
, unsigned client
)
1530 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1531 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1534 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1537 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1538 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
,
1539 memory_region_get_dirty_log_mask(mr
));
1542 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1543 hwaddr size
, unsigned client
)
1545 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1546 return cpu_physical_memory_test_and_clear_dirty(mr
->ram_addr
+ addr
,
1551 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1556 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1557 FlatView
*view
= address_space_get_flatview(as
);
1558 FOR_EACH_FLAT_RANGE(fr
, view
) {
1560 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1563 flatview_unref(view
);
1567 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1569 if (mr
->readonly
!= readonly
) {
1570 memory_region_transaction_begin();
1571 mr
->readonly
= readonly
;
1572 memory_region_update_pending
|= mr
->enabled
;
1573 memory_region_transaction_commit();
1577 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1579 if (mr
->romd_mode
!= romd_mode
) {
1580 memory_region_transaction_begin();
1581 mr
->romd_mode
= romd_mode
;
1582 memory_region_update_pending
|= mr
->enabled
;
1583 memory_region_transaction_commit();
1587 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1588 hwaddr size
, unsigned client
)
1590 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1591 cpu_physical_memory_test_and_clear_dirty(mr
->ram_addr
+ addr
, size
,
1595 int memory_region_get_fd(MemoryRegion
*mr
)
1598 return memory_region_get_fd(mr
->alias
);
1601 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1603 return qemu_get_ram_fd(mr
->ram_addr
& TARGET_PAGE_MASK
);
1606 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1609 uint64_t offset
= 0;
1613 offset
+= mr
->alias_offset
;
1616 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1617 ptr
= qemu_get_ram_ptr(mr
->ram_block
, mr
->ram_addr
& TARGET_PAGE_MASK
);
1620 return ptr
+ offset
;
1623 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1625 assert(mr
->ram_addr
!= RAM_ADDR_INVALID
);
1627 qemu_ram_resize(mr
->ram_addr
, newsize
, errp
);
1630 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1634 CoalescedMemoryRange
*cmr
;
1636 MemoryRegionSection section
;
1638 view
= address_space_get_flatview(as
);
1639 FOR_EACH_FLAT_RANGE(fr
, view
) {
1641 section
= (MemoryRegionSection
) {
1642 .address_space
= as
,
1643 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1644 .size
= fr
->addr
.size
,
1647 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1648 int128_get64(fr
->addr
.start
),
1649 int128_get64(fr
->addr
.size
));
1650 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1651 tmp
= addrrange_shift(cmr
->addr
,
1652 int128_sub(fr
->addr
.start
,
1653 int128_make64(fr
->offset_in_region
)));
1654 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1657 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1658 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1659 int128_get64(tmp
.start
),
1660 int128_get64(tmp
.size
));
1664 flatview_unref(view
);
1667 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1671 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1672 memory_region_update_coalesced_range_as(mr
, as
);
1676 void memory_region_set_coalescing(MemoryRegion
*mr
)
1678 memory_region_clear_coalescing(mr
);
1679 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1682 void memory_region_add_coalescing(MemoryRegion
*mr
,
1686 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1688 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1689 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1690 memory_region_update_coalesced_range(mr
);
1691 memory_region_set_flush_coalesced(mr
);
1694 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1696 CoalescedMemoryRange
*cmr
;
1697 bool updated
= false;
1699 qemu_flush_coalesced_mmio_buffer();
1700 mr
->flush_coalesced_mmio
= false;
1702 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1703 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1704 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1710 memory_region_update_coalesced_range(mr
);
1714 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1716 mr
->flush_coalesced_mmio
= true;
1719 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1721 qemu_flush_coalesced_mmio_buffer();
1722 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1723 mr
->flush_coalesced_mmio
= false;
1727 void memory_region_set_global_locking(MemoryRegion
*mr
)
1729 mr
->global_locking
= true;
1732 void memory_region_clear_global_locking(MemoryRegion
*mr
)
1734 mr
->global_locking
= false;
1737 static bool userspace_eventfd_warning
;
1739 void memory_region_add_eventfd(MemoryRegion
*mr
,
1746 MemoryRegionIoeventfd mrfd
= {
1747 .addr
.start
= int128_make64(addr
),
1748 .addr
.size
= int128_make64(size
),
1749 .match_data
= match_data
,
1755 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1756 userspace_eventfd_warning
))) {
1757 userspace_eventfd_warning
= true;
1758 error_report("Using eventfd without MMIO binding in KVM. "
1759 "Suboptimal performance expected");
1763 adjust_endianness(mr
, &mrfd
.data
, size
);
1765 memory_region_transaction_begin();
1766 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1767 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1772 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1773 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1774 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1775 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1776 mr
->ioeventfds
[i
] = mrfd
;
1777 ioeventfd_update_pending
|= mr
->enabled
;
1778 memory_region_transaction_commit();
1781 void memory_region_del_eventfd(MemoryRegion
*mr
,
1788 MemoryRegionIoeventfd mrfd
= {
1789 .addr
.start
= int128_make64(addr
),
1790 .addr
.size
= int128_make64(size
),
1791 .match_data
= match_data
,
1798 adjust_endianness(mr
, &mrfd
.data
, size
);
1800 memory_region_transaction_begin();
1801 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1802 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1806 assert(i
!= mr
->ioeventfd_nb
);
1807 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1808 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1810 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1811 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1812 ioeventfd_update_pending
|= mr
->enabled
;
1813 memory_region_transaction_commit();
1816 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1818 hwaddr offset
= subregion
->addr
;
1819 MemoryRegion
*mr
= subregion
->container
;
1820 MemoryRegion
*other
;
1822 memory_region_transaction_begin();
1824 memory_region_ref(subregion
);
1825 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1826 if (subregion
->may_overlap
|| other
->may_overlap
) {
1829 if (int128_ge(int128_make64(offset
),
1830 int128_add(int128_make64(other
->addr
), other
->size
))
1831 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1832 int128_make64(other
->addr
))) {
1836 printf("warning: subregion collision %llx/%llx (%s) "
1837 "vs %llx/%llx (%s)\n",
1838 (unsigned long long)offset
,
1839 (unsigned long long)int128_get64(subregion
->size
),
1841 (unsigned long long)other
->addr
,
1842 (unsigned long long)int128_get64(other
->size
),
1846 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1847 if (subregion
->priority
>= other
->priority
) {
1848 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1852 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1854 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1855 memory_region_transaction_commit();
1858 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1860 MemoryRegion
*subregion
)
1862 assert(!subregion
->container
);
1863 subregion
->container
= mr
;
1864 subregion
->addr
= offset
;
1865 memory_region_update_container_subregions(subregion
);
1868 void memory_region_add_subregion(MemoryRegion
*mr
,
1870 MemoryRegion
*subregion
)
1872 subregion
->may_overlap
= false;
1873 subregion
->priority
= 0;
1874 memory_region_add_subregion_common(mr
, offset
, subregion
);
1877 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1879 MemoryRegion
*subregion
,
1882 subregion
->may_overlap
= true;
1883 subregion
->priority
= priority
;
1884 memory_region_add_subregion_common(mr
, offset
, subregion
);
1887 void memory_region_del_subregion(MemoryRegion
*mr
,
1888 MemoryRegion
*subregion
)
1890 memory_region_transaction_begin();
1891 assert(subregion
->container
== mr
);
1892 subregion
->container
= NULL
;
1893 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1894 memory_region_unref(subregion
);
1895 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1896 memory_region_transaction_commit();
1899 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1901 if (enabled
== mr
->enabled
) {
1904 memory_region_transaction_begin();
1905 mr
->enabled
= enabled
;
1906 memory_region_update_pending
= true;
1907 memory_region_transaction_commit();
1910 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
1912 Int128 s
= int128_make64(size
);
1914 if (size
== UINT64_MAX
) {
1917 if (int128_eq(s
, mr
->size
)) {
1920 memory_region_transaction_begin();
1922 memory_region_update_pending
= true;
1923 memory_region_transaction_commit();
1926 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1928 MemoryRegion
*container
= mr
->container
;
1931 memory_region_transaction_begin();
1932 memory_region_ref(mr
);
1933 memory_region_del_subregion(container
, mr
);
1934 mr
->container
= container
;
1935 memory_region_update_container_subregions(mr
);
1936 memory_region_unref(mr
);
1937 memory_region_transaction_commit();
1941 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1943 if (addr
!= mr
->addr
) {
1945 memory_region_readd_subregion(mr
);
1949 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1953 if (offset
== mr
->alias_offset
) {
1957 memory_region_transaction_begin();
1958 mr
->alias_offset
= offset
;
1959 memory_region_update_pending
|= mr
->enabled
;
1960 memory_region_transaction_commit();
1963 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
1968 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1970 const AddrRange
*addr
= addr_
;
1971 const FlatRange
*fr
= fr_
;
1973 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1975 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1981 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1983 return bsearch(&addr
, view
->ranges
, view
->nr
,
1984 sizeof(FlatRange
), cmp_flatrange_addr
);
1987 bool memory_region_is_mapped(MemoryRegion
*mr
)
1989 return mr
->container
? true : false;
1992 /* Same as memory_region_find, but it does not add a reference to the
1993 * returned region. It must be called from an RCU critical section.
1995 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
1996 hwaddr addr
, uint64_t size
)
1998 MemoryRegionSection ret
= { .mr
= NULL
};
2006 for (root
= mr
; root
->container
; ) {
2007 root
= root
->container
;
2011 as
= memory_region_to_address_space(root
);
2015 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2017 view
= atomic_rcu_read(&as
->current_map
);
2018 fr
= flatview_lookup(view
, range
);
2023 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2028 ret
.address_space
= as
;
2029 range
= addrrange_intersection(range
, fr
->addr
);
2030 ret
.offset_within_region
= fr
->offset_in_region
;
2031 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2033 ret
.size
= range
.size
;
2034 ret
.offset_within_address_space
= int128_get64(range
.start
);
2035 ret
.readonly
= fr
->readonly
;
2039 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2040 hwaddr addr
, uint64_t size
)
2042 MemoryRegionSection ret
;
2044 ret
= memory_region_find_rcu(mr
, addr
, size
);
2046 memory_region_ref(ret
.mr
);
2052 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2057 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2059 return mr
&& mr
!= container
;
2062 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
2067 view
= address_space_get_flatview(as
);
2068 FOR_EACH_FLAT_RANGE(fr
, view
) {
2069 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
2071 flatview_unref(view
);
2074 void memory_global_dirty_log_start(void)
2076 global_dirty_log
= true;
2078 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2080 /* Refresh DIRTY_LOG_MIGRATION bit. */
2081 memory_region_transaction_begin();
2082 memory_region_update_pending
= true;
2083 memory_region_transaction_commit();
2086 void memory_global_dirty_log_stop(void)
2088 global_dirty_log
= false;
2090 /* Refresh DIRTY_LOG_MIGRATION bit. */
2091 memory_region_transaction_begin();
2092 memory_region_update_pending
= true;
2093 memory_region_transaction_commit();
2095 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2098 static void listener_add_address_space(MemoryListener
*listener
,
2104 if (listener
->address_space_filter
2105 && listener
->address_space_filter
!= as
) {
2109 if (listener
->begin
) {
2110 listener
->begin(listener
);
2112 if (global_dirty_log
) {
2113 if (listener
->log_global_start
) {
2114 listener
->log_global_start(listener
);
2118 view
= address_space_get_flatview(as
);
2119 FOR_EACH_FLAT_RANGE(fr
, view
) {
2120 MemoryRegionSection section
= {
2122 .address_space
= as
,
2123 .offset_within_region
= fr
->offset_in_region
,
2124 .size
= fr
->addr
.size
,
2125 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2126 .readonly
= fr
->readonly
,
2128 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2129 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2131 if (listener
->region_add
) {
2132 listener
->region_add(listener
, §ion
);
2135 if (listener
->commit
) {
2136 listener
->commit(listener
);
2138 flatview_unref(view
);
2141 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
2143 MemoryListener
*other
= NULL
;
2146 listener
->address_space_filter
= filter
;
2147 if (QTAILQ_EMPTY(&memory_listeners
)
2148 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2149 memory_listeners
)->priority
) {
2150 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2152 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2153 if (listener
->priority
< other
->priority
) {
2157 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2160 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2161 listener_add_address_space(listener
, as
);
2165 void memory_listener_unregister(MemoryListener
*listener
)
2167 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2170 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2172 memory_region_ref(root
);
2173 memory_region_transaction_begin();
2176 as
->malloced
= false;
2177 as
->current_map
= g_new(FlatView
, 1);
2178 flatview_init(as
->current_map
);
2179 as
->ioeventfd_nb
= 0;
2180 as
->ioeventfds
= NULL
;
2181 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2182 as
->name
= g_strdup(name
? name
: "anonymous");
2183 address_space_init_dispatch(as
);
2184 memory_region_update_pending
|= root
->enabled
;
2185 memory_region_transaction_commit();
2188 static void do_address_space_destroy(AddressSpace
*as
)
2190 MemoryListener
*listener
;
2191 bool do_free
= as
->malloced
;
2193 address_space_destroy_dispatch(as
);
2195 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2196 assert(listener
->address_space_filter
!= as
);
2199 flatview_unref(as
->current_map
);
2201 g_free(as
->ioeventfds
);
2202 memory_region_unref(as
->root
);
2208 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
, const char *name
)
2212 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2213 if (root
== as
->root
&& as
->malloced
) {
2219 as
= g_malloc0(sizeof *as
);
2220 address_space_init(as
, root
, name
);
2221 as
->malloced
= true;
2225 void address_space_destroy(AddressSpace
*as
)
2227 MemoryRegion
*root
= as
->root
;
2230 if (as
->ref_count
) {
2233 /* Flush out anything from MemoryListeners listening in on this */
2234 memory_region_transaction_begin();
2236 memory_region_transaction_commit();
2237 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2238 address_space_unregister(as
);
2240 /* At this point, as->dispatch and as->current_map are dummy
2241 * entries that the guest should never use. Wait for the old
2242 * values to expire before freeing the data.
2245 call_rcu(as
, do_address_space_destroy
, rcu
);
2248 typedef struct MemoryRegionList MemoryRegionList
;
2250 struct MemoryRegionList
{
2251 const MemoryRegion
*mr
;
2252 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2255 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2257 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2258 const MemoryRegion
*mr
, unsigned int level
,
2260 MemoryRegionListHead
*alias_print_queue
)
2262 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2263 MemoryRegionListHead submr_print_queue
;
2264 const MemoryRegion
*submr
;
2271 for (i
= 0; i
< level
; i
++) {
2276 MemoryRegionList
*ml
;
2279 /* check if the alias is already in the queue */
2280 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2281 if (ml
->mr
== mr
->alias
) {
2287 ml
= g_new(MemoryRegionList
, 1);
2289 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2291 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2292 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2293 "-" TARGET_FMT_plx
"%s\n",
2296 + (int128_nz(mr
->size
) ?
2297 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2298 int128_one())) : 0),
2300 mr
->romd_mode
? 'R' : '-',
2301 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2303 memory_region_name(mr
),
2304 memory_region_name(mr
->alias
),
2307 + (int128_nz(mr
->size
) ?
2308 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2309 int128_one())) : 0),
2310 mr
->enabled
? "" : " [disabled]");
2313 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s%s\n",
2316 + (int128_nz(mr
->size
) ?
2317 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2318 int128_one())) : 0),
2320 mr
->romd_mode
? 'R' : '-',
2321 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2323 memory_region_name(mr
),
2324 mr
->enabled
? "" : " [disabled]");
2327 QTAILQ_INIT(&submr_print_queue
);
2329 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2330 new_ml
= g_new(MemoryRegionList
, 1);
2332 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2333 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2334 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2335 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2336 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2342 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2346 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2347 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2351 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2356 void mtree_info(fprintf_function mon_printf
, void *f
)
2358 MemoryRegionListHead ml_head
;
2359 MemoryRegionList
*ml
, *ml2
;
2362 QTAILQ_INIT(&ml_head
);
2364 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2365 mon_printf(f
, "address-space: %s\n", as
->name
);
2366 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2367 mon_printf(f
, "\n");
2370 /* print aliased regions */
2371 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2372 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2373 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2374 mon_printf(f
, "\n");
2377 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2382 static const TypeInfo memory_region_info
= {
2383 .parent
= TYPE_OBJECT
,
2384 .name
= TYPE_MEMORY_REGION
,
2385 .instance_size
= sizeof(MemoryRegion
),
2386 .instance_init
= memory_region_initfn
,
2387 .instance_finalize
= memory_region_finalize
,
2390 static void memory_register_types(void)
2392 type_register_static(&memory_region_info
);
2395 type_init(memory_register_types
)