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"
18 #include "qemu-common.h"
20 #include "exec/memory.h"
21 #include "exec/address-spaces.h"
22 #include "exec/ioport.h"
23 #include "qapi/visitor.h"
24 #include "qemu/bitops.h"
25 #include "qemu/error-report.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/sysemu.h"
33 #include "hw/misc/mmio_interface.h"
34 #include "hw/qdev-properties.h"
35 #include "migration/vmstate.h"
37 //#define DEBUG_UNASSIGNED
39 static unsigned memory_region_transaction_depth
;
40 static bool memory_region_update_pending
;
41 static bool ioeventfd_update_pending
;
42 static bool global_dirty_log
= false;
44 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
45 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
47 static QTAILQ_HEAD(, AddressSpace
) address_spaces
48 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
50 static GHashTable
*flat_views
;
52 typedef struct AddrRange AddrRange
;
55 * Note that signed integers are needed for negative offsetting in aliases
56 * (large MemoryRegion::alias_offset).
63 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
65 return (AddrRange
) { start
, size
};
68 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
70 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
73 static Int128
addrrange_end(AddrRange r
)
75 return int128_add(r
.start
, r
.size
);
78 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
80 int128_addto(&range
.start
, delta
);
84 static bool addrrange_contains(AddrRange range
, Int128 addr
)
86 return int128_ge(addr
, range
.start
)
87 && int128_lt(addr
, addrrange_end(range
));
90 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
92 return addrrange_contains(r1
, r2
.start
)
93 || addrrange_contains(r2
, r1
.start
);
96 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
98 Int128 start
= int128_max(r1
.start
, r2
.start
);
99 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
100 return addrrange_make(start
, int128_sub(end
, start
));
103 enum ListenerDirection
{ Forward
, Reverse
};
105 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
107 MemoryListener *_listener; \
109 switch (_direction) { \
111 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
112 if (_listener->_callback) { \
113 _listener->_callback(_listener, ##_args); \
118 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
119 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; \
133 struct memory_listeners_as *list = &(_as)->listeners; \
135 switch (_direction) { \
137 QTAILQ_FOREACH(_listener, list, link_as) { \
138 if (_listener->_callback) { \
139 _listener->_callback(_listener, _section, ##_args); \
144 QTAILQ_FOREACH_REVERSE(_listener, list, memory_listeners_as, \
146 if (_listener->_callback) { \
147 _listener->_callback(_listener, _section, ##_args); \
156 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
157 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
159 MemoryRegionSection mrs = section_from_flat_range(fr, \
160 address_space_to_flatview(as)); \
161 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
164 struct CoalescedMemoryRange
{
166 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
169 struct MemoryRegionIoeventfd
{
176 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
177 MemoryRegionIoeventfd b
)
179 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
181 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
183 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
185 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
187 } else if (a
.match_data
< b
.match_data
) {
189 } else if (a
.match_data
> b
.match_data
) {
191 } else if (a
.match_data
) {
192 if (a
.data
< b
.data
) {
194 } else if (a
.data
> b
.data
) {
200 } else if (a
.e
> b
.e
) {
206 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
207 MemoryRegionIoeventfd b
)
209 return !memory_region_ioeventfd_before(a
, b
)
210 && !memory_region_ioeventfd_before(b
, a
);
213 /* Range of memory in the global map. Addresses are absolute. */
216 hwaddr offset_in_region
;
218 uint8_t dirty_log_mask
;
223 typedef struct AddressSpaceOps AddressSpaceOps
;
225 #define FOR_EACH_FLAT_RANGE(var, view) \
226 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
228 static inline MemoryRegionSection
229 section_from_flat_range(FlatRange
*fr
, FlatView
*fv
)
231 return (MemoryRegionSection
) {
234 .offset_within_region
= fr
->offset_in_region
,
235 .size
= fr
->addr
.size
,
236 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
237 .readonly
= fr
->readonly
,
241 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
243 return a
->mr
== b
->mr
244 && addrrange_equal(a
->addr
, b
->addr
)
245 && a
->offset_in_region
== b
->offset_in_region
246 && a
->romd_mode
== b
->romd_mode
247 && a
->readonly
== b
->readonly
;
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 static 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
;
322 /* Attempt to simplify a view by merging adjacent ranges */
323 static void flatview_simplify(FlatView
*view
)
328 while (i
< view
->nr
) {
331 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
332 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
336 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
337 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
342 static bool memory_region_big_endian(MemoryRegion
*mr
)
344 #ifdef TARGET_WORDS_BIGENDIAN
345 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
347 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
351 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
353 #ifdef TARGET_WORDS_BIGENDIAN
354 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
356 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
360 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
362 if (memory_region_wrong_endianness(mr
)) {
367 *data
= bswap16(*data
);
370 *data
= bswap32(*data
);
373 *data
= bswap64(*data
);
381 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
384 hwaddr abs_addr
= offset
;
386 abs_addr
+= mr
->addr
;
387 for (root
= mr
; root
->container
; ) {
388 root
= root
->container
;
389 abs_addr
+= root
->addr
;
395 static int get_cpu_index(void)
398 return current_cpu
->cpu_index
;
403 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
413 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
415 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
416 } else if (mr
== &io_mem_notdirty
) {
417 /* Accesses to code which has previously been translated into a TB show
418 * up in the MMIO path, as accesses to the io_mem_notdirty
420 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
421 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
422 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
423 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
425 *value
|= (tmp
& mask
) << shift
;
429 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
439 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
441 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
442 } else if (mr
== &io_mem_notdirty
) {
443 /* Accesses to code which has previously been translated into a TB show
444 * up in the MMIO path, as accesses to the io_mem_notdirty
446 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
447 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
448 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
449 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
451 *value
|= (tmp
& mask
) << shift
;
455 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
466 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
468 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
469 } else if (mr
== &io_mem_notdirty
) {
470 /* Accesses to code which has previously been translated into a TB show
471 * up in the MMIO path, as accesses to the io_mem_notdirty
473 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
474 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
475 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
476 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
478 *value
|= (tmp
& mask
) << shift
;
482 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
492 tmp
= (*value
>> shift
) & mask
;
494 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
495 } else if (mr
== &io_mem_notdirty
) {
496 /* Accesses to code which has previously been translated into a TB show
497 * up in the MMIO path, as accesses to the io_mem_notdirty
499 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
500 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
501 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
502 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
504 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
508 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
518 tmp
= (*value
>> shift
) & mask
;
520 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
521 } else if (mr
== &io_mem_notdirty
) {
522 /* Accesses to code which has previously been translated into a TB show
523 * up in the MMIO path, as accesses to the io_mem_notdirty
525 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
526 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
527 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
528 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
530 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
534 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
544 tmp
= (*value
>> shift
) & mask
;
546 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
547 } else if (mr
== &io_mem_notdirty
) {
548 /* Accesses to code which has previously been translated into a TB show
549 * up in the MMIO path, as accesses to the io_mem_notdirty
551 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
552 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
553 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
554 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
556 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
559 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
562 unsigned access_size_min
,
563 unsigned access_size_max
,
564 MemTxResult (*access_fn
)
575 uint64_t access_mask
;
576 unsigned access_size
;
578 MemTxResult r
= MEMTX_OK
;
580 if (!access_size_min
) {
583 if (!access_size_max
) {
587 /* FIXME: support unaligned access? */
588 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
589 access_mask
= -1ULL >> (64 - access_size
* 8);
590 if (memory_region_big_endian(mr
)) {
591 for (i
= 0; i
< size
; i
+= access_size
) {
592 r
|= access_fn(mr
, addr
+ i
, value
, access_size
,
593 (size
- access_size
- i
) * 8, access_mask
, attrs
);
596 for (i
= 0; i
< size
; i
+= access_size
) {
597 r
|= access_fn(mr
, addr
+ i
, value
, access_size
, i
* 8,
604 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
608 while (mr
->container
) {
611 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
612 if (mr
== as
->root
) {
619 /* Render a memory region into the global view. Ranges in @view obscure
622 static void render_memory_region(FlatView
*view
,
628 MemoryRegion
*subregion
;
630 hwaddr offset_in_region
;
640 int128_addto(&base
, int128_make64(mr
->addr
));
641 readonly
|= mr
->readonly
;
643 tmp
= addrrange_make(base
, mr
->size
);
645 if (!addrrange_intersects(tmp
, clip
)) {
649 clip
= addrrange_intersection(tmp
, clip
);
652 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
653 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
654 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
658 /* Render subregions in priority order. */
659 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
660 render_memory_region(view
, subregion
, base
, clip
, readonly
);
663 if (!mr
->terminates
) {
667 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
672 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
673 fr
.romd_mode
= mr
->romd_mode
;
674 fr
.readonly
= readonly
;
676 /* Render the region itself into any gaps left by the current view. */
677 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
678 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
681 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
682 now
= int128_min(remain
,
683 int128_sub(view
->ranges
[i
].addr
.start
, base
));
684 fr
.offset_in_region
= offset_in_region
;
685 fr
.addr
= addrrange_make(base
, now
);
686 flatview_insert(view
, i
, &fr
);
688 int128_addto(&base
, now
);
689 offset_in_region
+= int128_get64(now
);
690 int128_subfrom(&remain
, now
);
692 now
= int128_sub(int128_min(int128_add(base
, remain
),
693 addrrange_end(view
->ranges
[i
].addr
)),
695 int128_addto(&base
, now
);
696 offset_in_region
+= int128_get64(now
);
697 int128_subfrom(&remain
, now
);
699 if (int128_nz(remain
)) {
700 fr
.offset_in_region
= offset_in_region
;
701 fr
.addr
= addrrange_make(base
, remain
);
702 flatview_insert(view
, i
, &fr
);
706 static MemoryRegion
*memory_region_get_flatview_root(MemoryRegion
*mr
)
708 while (mr
->enabled
) {
710 if (!mr
->alias_offset
&& int128_ge(mr
->size
, mr
->alias
->size
)) {
711 /* The alias is included in its entirety. Use it as
712 * the "real" root, so that we can share more FlatViews.
717 } else if (!mr
->terminates
) {
718 unsigned int found
= 0;
719 MemoryRegion
*child
, *next
= NULL
;
720 QTAILQ_FOREACH(child
, &mr
->subregions
, subregions_link
) {
721 if (child
->enabled
) {
726 if (!child
->addr
&& int128_ge(mr
->size
, child
->size
)) {
727 /* A child is included in its entirety. If it's the only
728 * enabled one, use it in the hope of finding an alias down the
729 * way. This will also let us share FlatViews.
750 /* Render a memory topology into a list of disjoint absolute ranges. */
751 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
756 view
= flatview_new(mr
);
759 render_memory_region(view
, mr
, int128_zero(),
760 addrrange_make(int128_zero(), int128_2_64()), false);
762 flatview_simplify(view
);
764 view
->dispatch
= address_space_dispatch_new(view
);
765 for (i
= 0; i
< view
->nr
; i
++) {
766 MemoryRegionSection mrs
=
767 section_from_flat_range(&view
->ranges
[i
], view
);
768 flatview_add_to_dispatch(view
, &mrs
);
770 address_space_dispatch_compact(view
->dispatch
);
771 g_hash_table_replace(flat_views
, mr
, view
);
776 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
777 MemoryRegionIoeventfd
*fds_new
,
779 MemoryRegionIoeventfd
*fds_old
,
783 MemoryRegionIoeventfd
*fd
;
784 MemoryRegionSection section
;
786 /* Generate a symmetric difference of the old and new fd sets, adding
787 * and deleting as necessary.
791 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
792 if (iold
< fds_old_nb
793 && (inew
== fds_new_nb
794 || memory_region_ioeventfd_before(fds_old
[iold
],
797 section
= (MemoryRegionSection
) {
798 .fv
= address_space_to_flatview(as
),
799 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
800 .size
= fd
->addr
.size
,
802 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
803 fd
->match_data
, fd
->data
, fd
->e
);
805 } else if (inew
< fds_new_nb
806 && (iold
== fds_old_nb
807 || memory_region_ioeventfd_before(fds_new
[inew
],
810 section
= (MemoryRegionSection
) {
811 .fv
= address_space_to_flatview(as
),
812 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
813 .size
= fd
->addr
.size
,
815 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
816 fd
->match_data
, fd
->data
, fd
->e
);
825 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
831 view
= address_space_to_flatview(as
);
832 /* If somebody has replaced as->current_map concurrently,
833 * flatview_ref returns false.
835 } while (!flatview_ref(view
));
840 static void address_space_update_ioeventfds(AddressSpace
*as
)
844 unsigned ioeventfd_nb
= 0;
845 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
849 view
= address_space_get_flatview(as
);
850 FOR_EACH_FLAT_RANGE(fr
, view
) {
851 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
852 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
853 int128_sub(fr
->addr
.start
,
854 int128_make64(fr
->offset_in_region
)));
855 if (addrrange_intersects(fr
->addr
, tmp
)) {
857 ioeventfds
= g_realloc(ioeventfds
,
858 ioeventfd_nb
* sizeof(*ioeventfds
));
859 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
860 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
865 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
866 as
->ioeventfds
, as
->ioeventfd_nb
);
868 g_free(as
->ioeventfds
);
869 as
->ioeventfds
= ioeventfds
;
870 as
->ioeventfd_nb
= ioeventfd_nb
;
871 flatview_unref(view
);
874 static void address_space_update_topology_pass(AddressSpace
*as
,
875 const FlatView
*old_view
,
876 const FlatView
*new_view
,
880 FlatRange
*frold
, *frnew
;
882 /* Generate a symmetric difference of the old and new memory maps.
883 * Kill ranges in the old map, and instantiate ranges in the new map.
886 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
887 if (iold
< old_view
->nr
) {
888 frold
= &old_view
->ranges
[iold
];
892 if (inew
< new_view
->nr
) {
893 frnew
= &new_view
->ranges
[inew
];
900 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
901 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
902 && !flatrange_equal(frold
, frnew
)))) {
903 /* In old but not in new, or in both but attributes changed. */
906 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
910 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
911 /* In both and unchanged (except logging may have changed) */
914 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
915 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
916 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
917 frold
->dirty_log_mask
,
918 frnew
->dirty_log_mask
);
920 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
921 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
922 frold
->dirty_log_mask
,
923 frnew
->dirty_log_mask
);
933 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
941 static void flatviews_init(void)
943 static FlatView
*empty_view
;
949 flat_views
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
950 (GDestroyNotify
) flatview_unref
);
952 empty_view
= generate_memory_topology(NULL
);
953 /* We keep it alive forever in the global variable. */
954 flatview_ref(empty_view
);
956 g_hash_table_replace(flat_views
, NULL
, empty_view
);
957 flatview_ref(empty_view
);
961 static void flatviews_reset(void)
966 g_hash_table_unref(flat_views
);
971 /* Render unique FVs */
972 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
973 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
975 if (g_hash_table_lookup(flat_views
, physmr
)) {
979 generate_memory_topology(physmr
);
983 static void address_space_set_flatview(AddressSpace
*as
)
985 FlatView
*old_view
= address_space_to_flatview(as
);
986 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
987 FlatView
*new_view
= g_hash_table_lookup(flat_views
, physmr
);
991 if (old_view
== new_view
) {
996 flatview_ref(old_view
);
999 flatview_ref(new_view
);
1001 if (!QTAILQ_EMPTY(&as
->listeners
)) {
1002 FlatView tmpview
= { .nr
= 0 }, *old_view2
= old_view
;
1005 old_view2
= &tmpview
;
1007 address_space_update_topology_pass(as
, old_view2
, new_view
, false);
1008 address_space_update_topology_pass(as
, old_view2
, new_view
, true);
1011 /* Writes are protected by the BQL. */
1012 atomic_rcu_set(&as
->current_map
, new_view
);
1014 flatview_unref(old_view
);
1017 /* Note that all the old MemoryRegions are still alive up to this
1018 * point. This relieves most MemoryListeners from the need to
1019 * ref/unref the MemoryRegions they get---unless they use them
1020 * outside the iothread mutex, in which case precise reference
1021 * counting is necessary.
1024 flatview_unref(old_view
);
1028 static void address_space_update_topology(AddressSpace
*as
)
1030 MemoryRegion
*physmr
= memory_region_get_flatview_root(as
->root
);
1033 if (!g_hash_table_lookup(flat_views
, physmr
)) {
1034 generate_memory_topology(physmr
);
1036 address_space_set_flatview(as
);
1039 void memory_region_transaction_begin(void)
1041 qemu_flush_coalesced_mmio_buffer();
1042 ++memory_region_transaction_depth
;
1045 void memory_region_transaction_commit(void)
1049 assert(memory_region_transaction_depth
);
1050 assert(qemu_mutex_iothread_locked());
1052 --memory_region_transaction_depth
;
1053 if (!memory_region_transaction_depth
) {
1054 if (memory_region_update_pending
) {
1057 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
1059 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1060 address_space_set_flatview(as
);
1061 address_space_update_ioeventfds(as
);
1063 memory_region_update_pending
= false;
1064 ioeventfd_update_pending
= false;
1065 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
1066 } else if (ioeventfd_update_pending
) {
1067 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1068 address_space_update_ioeventfds(as
);
1070 ioeventfd_update_pending
= false;
1075 static void memory_region_destructor_none(MemoryRegion
*mr
)
1079 static void memory_region_destructor_ram(MemoryRegion
*mr
)
1081 qemu_ram_free(mr
->ram_block
);
1084 static bool memory_region_need_escape(char c
)
1086 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
1089 static char *memory_region_escape_name(const char *name
)
1096 for (p
= name
; *p
; p
++) {
1097 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
1099 if (bytes
== p
- name
) {
1100 return g_memdup(name
, bytes
+ 1);
1103 escaped
= g_malloc(bytes
+ 1);
1104 for (p
= name
, q
= escaped
; *p
; p
++) {
1106 if (unlikely(memory_region_need_escape(c
))) {
1109 *q
++ = "0123456789abcdef"[c
>> 4];
1110 c
= "0123456789abcdef"[c
& 15];
1118 static void memory_region_do_init(MemoryRegion
*mr
,
1123 mr
->size
= int128_make64(size
);
1124 if (size
== UINT64_MAX
) {
1125 mr
->size
= int128_2_64();
1127 mr
->name
= g_strdup(name
);
1129 mr
->ram_block
= NULL
;
1132 char *escaped_name
= memory_region_escape_name(name
);
1133 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1136 owner
= container_get(qdev_get_machine(), "/unattached");
1139 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1140 object_unref(OBJECT(mr
));
1142 g_free(escaped_name
);
1146 void memory_region_init(MemoryRegion
*mr
,
1151 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1152 memory_region_do_init(mr
, owner
, name
, size
);
1155 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1156 void *opaque
, Error
**errp
)
1158 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1159 uint64_t value
= mr
->addr
;
1161 visit_type_uint64(v
, name
, &value
, errp
);
1164 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1165 const char *name
, void *opaque
,
1168 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1169 gchar
*path
= (gchar
*)"";
1171 if (mr
->container
) {
1172 path
= object_get_canonical_path(OBJECT(mr
->container
));
1174 visit_type_str(v
, name
, &path
, errp
);
1175 if (mr
->container
) {
1180 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1183 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1185 return OBJECT(mr
->container
);
1188 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1189 const char *name
, void *opaque
,
1192 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1193 int32_t value
= mr
->priority
;
1195 visit_type_int32(v
, name
, &value
, errp
);
1198 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1199 void *opaque
, Error
**errp
)
1201 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1202 uint64_t value
= memory_region_size(mr
);
1204 visit_type_uint64(v
, name
, &value
, errp
);
1207 static void memory_region_initfn(Object
*obj
)
1209 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1212 mr
->ops
= &unassigned_mem_ops
;
1214 mr
->romd_mode
= true;
1215 mr
->global_locking
= true;
1216 mr
->destructor
= memory_region_destructor_none
;
1217 QTAILQ_INIT(&mr
->subregions
);
1218 QTAILQ_INIT(&mr
->coalesced
);
1220 op
= object_property_add(OBJECT(mr
), "container",
1221 "link<" TYPE_MEMORY_REGION
">",
1222 memory_region_get_container
,
1223 NULL
, /* memory_region_set_container */
1224 NULL
, NULL
, &error_abort
);
1225 op
->resolve
= memory_region_resolve_container
;
1227 object_property_add(OBJECT(mr
), "addr", "uint64",
1228 memory_region_get_addr
,
1229 NULL
, /* memory_region_set_addr */
1230 NULL
, NULL
, &error_abort
);
1231 object_property_add(OBJECT(mr
), "priority", "uint32",
1232 memory_region_get_priority
,
1233 NULL
, /* memory_region_set_priority */
1234 NULL
, NULL
, &error_abort
);
1235 object_property_add(OBJECT(mr
), "size", "uint64",
1236 memory_region_get_size
,
1237 NULL
, /* memory_region_set_size, */
1238 NULL
, NULL
, &error_abort
);
1241 static void iommu_memory_region_initfn(Object
*obj
)
1243 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1245 mr
->is_iommu
= true;
1248 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1251 #ifdef DEBUG_UNASSIGNED
1252 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1254 if (current_cpu
!= NULL
) {
1255 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1260 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1261 uint64_t val
, unsigned size
)
1263 #ifdef DEBUG_UNASSIGNED
1264 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1266 if (current_cpu
!= NULL
) {
1267 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1271 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1272 unsigned size
, bool is_write
)
1277 const MemoryRegionOps unassigned_mem_ops
= {
1278 .valid
.accepts
= unassigned_mem_accepts
,
1279 .endianness
= DEVICE_NATIVE_ENDIAN
,
1282 static uint64_t memory_region_ram_device_read(void *opaque
,
1283 hwaddr addr
, unsigned size
)
1285 MemoryRegion
*mr
= opaque
;
1286 uint64_t data
= (uint64_t)~0;
1290 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1293 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1296 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1299 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1303 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1308 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1309 uint64_t data
, unsigned size
)
1311 MemoryRegion
*mr
= opaque
;
1313 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1317 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1320 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1323 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1326 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1331 static const MemoryRegionOps ram_device_mem_ops
= {
1332 .read
= memory_region_ram_device_read
,
1333 .write
= memory_region_ram_device_write
,
1334 .endianness
= DEVICE_HOST_ENDIAN
,
1336 .min_access_size
= 1,
1337 .max_access_size
= 8,
1341 .min_access_size
= 1,
1342 .max_access_size
= 8,
1347 bool memory_region_access_valid(MemoryRegion
*mr
,
1352 int access_size_min
, access_size_max
;
1355 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1359 if (!mr
->ops
->valid
.accepts
) {
1363 access_size_min
= mr
->ops
->valid
.min_access_size
;
1364 if (!mr
->ops
->valid
.min_access_size
) {
1365 access_size_min
= 1;
1368 access_size_max
= mr
->ops
->valid
.max_access_size
;
1369 if (!mr
->ops
->valid
.max_access_size
) {
1370 access_size_max
= 4;
1373 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1374 for (i
= 0; i
< size
; i
+= access_size
) {
1375 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1384 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1392 if (mr
->ops
->read
) {
1393 return access_with_adjusted_size(addr
, pval
, size
,
1394 mr
->ops
->impl
.min_access_size
,
1395 mr
->ops
->impl
.max_access_size
,
1396 memory_region_read_accessor
,
1398 } else if (mr
->ops
->read_with_attrs
) {
1399 return access_with_adjusted_size(addr
, pval
, size
,
1400 mr
->ops
->impl
.min_access_size
,
1401 mr
->ops
->impl
.max_access_size
,
1402 memory_region_read_with_attrs_accessor
,
1405 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1406 memory_region_oldmmio_read_accessor
,
1411 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1419 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1420 *pval
= unassigned_mem_read(mr
, addr
, size
);
1421 return MEMTX_DECODE_ERROR
;
1424 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1425 adjust_endianness(mr
, pval
, size
);
1429 /* Return true if an eventfd was signalled */
1430 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1436 MemoryRegionIoeventfd ioeventfd
= {
1437 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1442 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1443 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1444 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1446 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1447 event_notifier_set(ioeventfd
.e
);
1455 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1461 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1462 unassigned_mem_write(mr
, addr
, data
, size
);
1463 return MEMTX_DECODE_ERROR
;
1466 adjust_endianness(mr
, &data
, size
);
1468 if ((!kvm_eventfds_enabled()) &&
1469 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1473 if (mr
->ops
->write
) {
1474 return access_with_adjusted_size(addr
, &data
, size
,
1475 mr
->ops
->impl
.min_access_size
,
1476 mr
->ops
->impl
.max_access_size
,
1477 memory_region_write_accessor
, mr
,
1479 } else if (mr
->ops
->write_with_attrs
) {
1481 access_with_adjusted_size(addr
, &data
, size
,
1482 mr
->ops
->impl
.min_access_size
,
1483 mr
->ops
->impl
.max_access_size
,
1484 memory_region_write_with_attrs_accessor
,
1487 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1488 memory_region_oldmmio_write_accessor
,
1493 void memory_region_init_io(MemoryRegion
*mr
,
1495 const MemoryRegionOps
*ops
,
1500 memory_region_init(mr
, owner
, name
, size
);
1501 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1502 mr
->opaque
= opaque
;
1503 mr
->terminates
= true;
1506 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1512 memory_region_init_ram_shared_nomigrate(mr
, owner
, name
, size
, false, errp
);
1515 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
1522 memory_region_init(mr
, owner
, name
, size
);
1524 mr
->terminates
= true;
1525 mr
->destructor
= memory_region_destructor_ram
;
1526 mr
->ram_block
= qemu_ram_alloc(size
, share
, mr
, errp
);
1527 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1530 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1535 void (*resized
)(const char*,
1540 memory_region_init(mr
, owner
, name
, size
);
1542 mr
->terminates
= true;
1543 mr
->destructor
= memory_region_destructor_ram
;
1544 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1546 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1550 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1551 struct Object
*owner
,
1559 memory_region_init(mr
, owner
, name
, size
);
1561 mr
->terminates
= true;
1562 mr
->destructor
= memory_region_destructor_ram
;
1564 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1565 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1568 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1569 struct Object
*owner
,
1576 memory_region_init(mr
, owner
, name
, size
);
1578 mr
->terminates
= true;
1579 mr
->destructor
= memory_region_destructor_ram
;
1580 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
, share
, fd
, errp
);
1581 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1585 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1591 memory_region_init(mr
, owner
, name
, size
);
1593 mr
->terminates
= true;
1594 mr
->destructor
= memory_region_destructor_ram
;
1595 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1597 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1598 assert(ptr
!= NULL
);
1599 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1602 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1608 memory_region_init_ram_ptr(mr
, owner
, name
, size
, ptr
);
1609 mr
->ram_device
= true;
1610 mr
->ops
= &ram_device_mem_ops
;
1614 void memory_region_init_alias(MemoryRegion
*mr
,
1621 memory_region_init(mr
, owner
, name
, size
);
1623 mr
->alias_offset
= offset
;
1626 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1627 struct Object
*owner
,
1632 memory_region_init(mr
, owner
, name
, size
);
1634 mr
->readonly
= true;
1635 mr
->terminates
= true;
1636 mr
->destructor
= memory_region_destructor_ram
;
1637 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, errp
);
1638 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1641 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1643 const MemoryRegionOps
*ops
,
1650 memory_region_init(mr
, owner
, name
, size
);
1652 mr
->opaque
= opaque
;
1653 mr
->terminates
= true;
1654 mr
->rom_device
= true;
1655 mr
->destructor
= memory_region_destructor_ram
;
1656 mr
->ram_block
= qemu_ram_alloc(size
, false, mr
, errp
);
1659 void memory_region_init_iommu(void *_iommu_mr
,
1660 size_t instance_size
,
1661 const char *mrtypename
,
1666 struct IOMMUMemoryRegion
*iommu_mr
;
1667 struct MemoryRegion
*mr
;
1669 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1670 mr
= MEMORY_REGION(_iommu_mr
);
1671 memory_region_do_init(mr
, owner
, name
, size
);
1672 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1673 mr
->terminates
= true; /* then re-forwards */
1674 QLIST_INIT(&iommu_mr
->iommu_notify
);
1675 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1678 static void memory_region_finalize(Object
*obj
)
1680 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1682 assert(!mr
->container
);
1684 /* We know the region is not visible in any address space (it
1685 * does not have a container and cannot be a root either because
1686 * it has no references, so we can blindly clear mr->enabled.
1687 * memory_region_set_enabled instead could trigger a transaction
1688 * and cause an infinite loop.
1690 mr
->enabled
= false;
1691 memory_region_transaction_begin();
1692 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1693 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1694 memory_region_del_subregion(mr
, subregion
);
1696 memory_region_transaction_commit();
1699 memory_region_clear_coalescing(mr
);
1700 g_free((char *)mr
->name
);
1701 g_free(mr
->ioeventfds
);
1704 Object
*memory_region_owner(MemoryRegion
*mr
)
1706 Object
*obj
= OBJECT(mr
);
1710 void memory_region_ref(MemoryRegion
*mr
)
1712 /* MMIO callbacks most likely will access data that belongs
1713 * to the owner, hence the need to ref/unref the owner whenever
1714 * the memory region is in use.
1716 * The memory region is a child of its owner. As long as the
1717 * owner doesn't call unparent itself on the memory region,
1718 * ref-ing the owner will also keep the memory region alive.
1719 * Memory regions without an owner are supposed to never go away;
1720 * we do not ref/unref them because it slows down DMA sensibly.
1722 if (mr
&& mr
->owner
) {
1723 object_ref(mr
->owner
);
1727 void memory_region_unref(MemoryRegion
*mr
)
1729 if (mr
&& mr
->owner
) {
1730 object_unref(mr
->owner
);
1734 uint64_t memory_region_size(MemoryRegion
*mr
)
1736 if (int128_eq(mr
->size
, int128_2_64())) {
1739 return int128_get64(mr
->size
);
1742 const char *memory_region_name(const MemoryRegion
*mr
)
1745 ((MemoryRegion
*)mr
)->name
=
1746 object_get_canonical_path_component(OBJECT(mr
));
1751 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1753 return mr
->ram_device
;
1756 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1758 uint8_t mask
= mr
->dirty_log_mask
;
1759 if (global_dirty_log
&& mr
->ram_block
) {
1760 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1765 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1767 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1770 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1772 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1773 IOMMUNotifier
*iommu_notifier
;
1774 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1776 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1777 flags
|= iommu_notifier
->notifier_flags
;
1780 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1781 imrc
->notify_flag_changed(iommu_mr
,
1782 iommu_mr
->iommu_notify_flags
,
1786 iommu_mr
->iommu_notify_flags
= flags
;
1789 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1792 IOMMUMemoryRegion
*iommu_mr
;
1795 memory_region_register_iommu_notifier(mr
->alias
, n
);
1799 /* We need to register for at least one bitfield */
1800 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1801 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1802 assert(n
->start
<= n
->end
);
1803 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1804 memory_region_update_iommu_notify_flags(iommu_mr
);
1807 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1809 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1811 if (imrc
->get_min_page_size
) {
1812 return imrc
->get_min_page_size(iommu_mr
);
1814 return TARGET_PAGE_SIZE
;
1817 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1819 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1820 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1821 hwaddr addr
, granularity
;
1822 IOMMUTLBEntry iotlb
;
1824 /* If the IOMMU has its own replay callback, override */
1826 imrc
->replay(iommu_mr
, n
);
1830 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1832 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1833 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
);
1834 if (iotlb
.perm
!= IOMMU_NONE
) {
1835 n
->notify(n
, &iotlb
);
1838 /* if (2^64 - MR size) < granularity, it's possible to get an
1839 * infinite loop here. This should catch such a wraparound */
1840 if ((addr
+ granularity
) < addr
) {
1846 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
)
1848 IOMMUNotifier
*notifier
;
1850 IOMMU_NOTIFIER_FOREACH(notifier
, iommu_mr
) {
1851 memory_region_iommu_replay(iommu_mr
, notifier
);
1855 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1858 IOMMUMemoryRegion
*iommu_mr
;
1861 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1864 QLIST_REMOVE(n
, node
);
1865 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1866 memory_region_update_iommu_notify_flags(iommu_mr
);
1869 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1870 IOMMUTLBEntry
*entry
)
1872 IOMMUNotifierFlag request_flags
;
1875 * Skip the notification if the notification does not overlap
1876 * with registered range.
1878 if (notifier
->start
> entry
->iova
+ entry
->addr_mask
||
1879 notifier
->end
< entry
->iova
) {
1883 if (entry
->perm
& IOMMU_RW
) {
1884 request_flags
= IOMMU_NOTIFIER_MAP
;
1886 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1889 if (notifier
->notifier_flags
& request_flags
) {
1890 notifier
->notify(notifier
, entry
);
1894 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1895 IOMMUTLBEntry entry
)
1897 IOMMUNotifier
*iommu_notifier
;
1899 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1901 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1902 memory_region_notify_one(iommu_notifier
, &entry
);
1906 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1907 enum IOMMUMemoryRegionAttr attr
,
1910 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1912 if (!imrc
->get_attr
) {
1916 return imrc
->get_attr(iommu_mr
, attr
, data
);
1919 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1921 uint8_t mask
= 1 << client
;
1922 uint8_t old_logging
;
1924 assert(client
== DIRTY_MEMORY_VGA
);
1925 old_logging
= mr
->vga_logging_count
;
1926 mr
->vga_logging_count
+= log
? 1 : -1;
1927 if (!!old_logging
== !!mr
->vga_logging_count
) {
1931 memory_region_transaction_begin();
1932 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1933 memory_region_update_pending
|= mr
->enabled
;
1934 memory_region_transaction_commit();
1937 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1938 hwaddr size
, unsigned client
)
1940 assert(mr
->ram_block
);
1941 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr
) + addr
,
1945 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1948 assert(mr
->ram_block
);
1949 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
1951 memory_region_get_dirty_log_mask(mr
));
1954 static void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1956 MemoryListener
*listener
;
1961 /* If the same address space has multiple log_sync listeners, we
1962 * visit that address space's FlatView multiple times. But because
1963 * log_sync listeners are rare, it's still cheaper than walking each
1964 * address space once.
1966 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1967 if (!listener
->log_sync
) {
1970 as
= listener
->address_space
;
1971 view
= address_space_get_flatview(as
);
1972 FOR_EACH_FLAT_RANGE(fr
, view
) {
1973 if (fr
->dirty_log_mask
&& (!mr
|| fr
->mr
== mr
)) {
1974 MemoryRegionSection mrs
= section_from_flat_range(fr
, view
);
1975 listener
->log_sync(listener
, &mrs
);
1978 flatview_unref(view
);
1982 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1987 assert(mr
->ram_block
);
1988 memory_region_sync_dirty_bitmap(mr
);
1989 return cpu_physical_memory_snapshot_and_clear_dirty(
1990 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1993 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
1994 hwaddr addr
, hwaddr size
)
1996 assert(mr
->ram_block
);
1997 return cpu_physical_memory_snapshot_get_dirty(snap
,
1998 memory_region_get_ram_addr(mr
) + addr
, size
);
2001 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
2003 if (mr
->readonly
!= readonly
) {
2004 memory_region_transaction_begin();
2005 mr
->readonly
= readonly
;
2006 memory_region_update_pending
|= mr
->enabled
;
2007 memory_region_transaction_commit();
2011 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
2013 if (mr
->romd_mode
!= romd_mode
) {
2014 memory_region_transaction_begin();
2015 mr
->romd_mode
= romd_mode
;
2016 memory_region_update_pending
|= mr
->enabled
;
2017 memory_region_transaction_commit();
2021 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2022 hwaddr size
, unsigned client
)
2024 assert(mr
->ram_block
);
2025 cpu_physical_memory_test_and_clear_dirty(
2026 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
2029 int memory_region_get_fd(MemoryRegion
*mr
)
2037 fd
= mr
->ram_block
->fd
;
2043 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
2046 uint64_t offset
= 0;
2050 offset
+= mr
->alias_offset
;
2053 assert(mr
->ram_block
);
2054 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
2060 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
2064 block
= qemu_ram_block_from_host(ptr
, false, offset
);
2072 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
2074 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
2077 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
2079 assert(mr
->ram_block
);
2081 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
2084 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
2088 CoalescedMemoryRange
*cmr
;
2090 MemoryRegionSection section
;
2092 view
= address_space_get_flatview(as
);
2093 FOR_EACH_FLAT_RANGE(fr
, view
) {
2095 section
= (MemoryRegionSection
) {
2097 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2098 .size
= fr
->addr
.size
,
2101 MEMORY_LISTENER_CALL(as
, coalesced_mmio_del
, Reverse
, §ion
,
2102 int128_get64(fr
->addr
.start
),
2103 int128_get64(fr
->addr
.size
));
2104 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
2105 tmp
= addrrange_shift(cmr
->addr
,
2106 int128_sub(fr
->addr
.start
,
2107 int128_make64(fr
->offset_in_region
)));
2108 if (!addrrange_intersects(tmp
, fr
->addr
)) {
2111 tmp
= addrrange_intersection(tmp
, fr
->addr
);
2112 MEMORY_LISTENER_CALL(as
, coalesced_mmio_add
, Forward
, §ion
,
2113 int128_get64(tmp
.start
),
2114 int128_get64(tmp
.size
));
2118 flatview_unref(view
);
2121 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
2125 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2126 memory_region_update_coalesced_range_as(mr
, as
);
2130 void memory_region_set_coalescing(MemoryRegion
*mr
)
2132 memory_region_clear_coalescing(mr
);
2133 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
2136 void memory_region_add_coalescing(MemoryRegion
*mr
,
2140 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
2142 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
2143 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
2144 memory_region_update_coalesced_range(mr
);
2145 memory_region_set_flush_coalesced(mr
);
2148 void memory_region_clear_coalescing(MemoryRegion
*mr
)
2150 CoalescedMemoryRange
*cmr
;
2151 bool updated
= false;
2153 qemu_flush_coalesced_mmio_buffer();
2154 mr
->flush_coalesced_mmio
= false;
2156 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2157 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2158 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2164 memory_region_update_coalesced_range(mr
);
2168 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2170 mr
->flush_coalesced_mmio
= true;
2173 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2175 qemu_flush_coalesced_mmio_buffer();
2176 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2177 mr
->flush_coalesced_mmio
= false;
2181 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2183 mr
->global_locking
= false;
2186 static bool userspace_eventfd_warning
;
2188 void memory_region_add_eventfd(MemoryRegion
*mr
,
2195 MemoryRegionIoeventfd mrfd
= {
2196 .addr
.start
= int128_make64(addr
),
2197 .addr
.size
= int128_make64(size
),
2198 .match_data
= match_data
,
2204 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2205 userspace_eventfd_warning
))) {
2206 userspace_eventfd_warning
= true;
2207 error_report("Using eventfd without MMIO binding in KVM. "
2208 "Suboptimal performance expected");
2212 adjust_endianness(mr
, &mrfd
.data
, size
);
2214 memory_region_transaction_begin();
2215 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2216 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
2221 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2222 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2223 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2224 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2225 mr
->ioeventfds
[i
] = mrfd
;
2226 ioeventfd_update_pending
|= mr
->enabled
;
2227 memory_region_transaction_commit();
2230 void memory_region_del_eventfd(MemoryRegion
*mr
,
2237 MemoryRegionIoeventfd mrfd
= {
2238 .addr
.start
= int128_make64(addr
),
2239 .addr
.size
= int128_make64(size
),
2240 .match_data
= match_data
,
2247 adjust_endianness(mr
, &mrfd
.data
, size
);
2249 memory_region_transaction_begin();
2250 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2251 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
2255 assert(i
!= mr
->ioeventfd_nb
);
2256 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2257 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2259 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2260 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2261 ioeventfd_update_pending
|= mr
->enabled
;
2262 memory_region_transaction_commit();
2265 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2267 MemoryRegion
*mr
= subregion
->container
;
2268 MemoryRegion
*other
;
2270 memory_region_transaction_begin();
2272 memory_region_ref(subregion
);
2273 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2274 if (subregion
->priority
>= other
->priority
) {
2275 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2279 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2281 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2282 memory_region_transaction_commit();
2285 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2287 MemoryRegion
*subregion
)
2289 assert(!subregion
->container
);
2290 subregion
->container
= mr
;
2291 subregion
->addr
= offset
;
2292 memory_region_update_container_subregions(subregion
);
2295 void memory_region_add_subregion(MemoryRegion
*mr
,
2297 MemoryRegion
*subregion
)
2299 subregion
->priority
= 0;
2300 memory_region_add_subregion_common(mr
, offset
, subregion
);
2303 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2305 MemoryRegion
*subregion
,
2308 subregion
->priority
= priority
;
2309 memory_region_add_subregion_common(mr
, offset
, subregion
);
2312 void memory_region_del_subregion(MemoryRegion
*mr
,
2313 MemoryRegion
*subregion
)
2315 memory_region_transaction_begin();
2316 assert(subregion
->container
== mr
);
2317 subregion
->container
= NULL
;
2318 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2319 memory_region_unref(subregion
);
2320 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2321 memory_region_transaction_commit();
2324 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2326 if (enabled
== mr
->enabled
) {
2329 memory_region_transaction_begin();
2330 mr
->enabled
= enabled
;
2331 memory_region_update_pending
= true;
2332 memory_region_transaction_commit();
2335 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2337 Int128 s
= int128_make64(size
);
2339 if (size
== UINT64_MAX
) {
2342 if (int128_eq(s
, mr
->size
)) {
2345 memory_region_transaction_begin();
2347 memory_region_update_pending
= true;
2348 memory_region_transaction_commit();
2351 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2353 MemoryRegion
*container
= mr
->container
;
2356 memory_region_transaction_begin();
2357 memory_region_ref(mr
);
2358 memory_region_del_subregion(container
, mr
);
2359 mr
->container
= container
;
2360 memory_region_update_container_subregions(mr
);
2361 memory_region_unref(mr
);
2362 memory_region_transaction_commit();
2366 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2368 if (addr
!= mr
->addr
) {
2370 memory_region_readd_subregion(mr
);
2374 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2378 if (offset
== mr
->alias_offset
) {
2382 memory_region_transaction_begin();
2383 mr
->alias_offset
= offset
;
2384 memory_region_update_pending
|= mr
->enabled
;
2385 memory_region_transaction_commit();
2388 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2393 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2395 const AddrRange
*addr
= addr_
;
2396 const FlatRange
*fr
= fr_
;
2398 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2400 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2406 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2408 return bsearch(&addr
, view
->ranges
, view
->nr
,
2409 sizeof(FlatRange
), cmp_flatrange_addr
);
2412 bool memory_region_is_mapped(MemoryRegion
*mr
)
2414 return mr
->container
? true : false;
2417 /* Same as memory_region_find, but it does not add a reference to the
2418 * returned region. It must be called from an RCU critical section.
2420 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2421 hwaddr addr
, uint64_t size
)
2423 MemoryRegionSection ret
= { .mr
= NULL
};
2431 for (root
= mr
; root
->container
; ) {
2432 root
= root
->container
;
2436 as
= memory_region_to_address_space(root
);
2440 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2442 view
= address_space_to_flatview(as
);
2443 fr
= flatview_lookup(view
, range
);
2448 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2454 range
= addrrange_intersection(range
, fr
->addr
);
2455 ret
.offset_within_region
= fr
->offset_in_region
;
2456 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2458 ret
.size
= range
.size
;
2459 ret
.offset_within_address_space
= int128_get64(range
.start
);
2460 ret
.readonly
= fr
->readonly
;
2464 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2465 hwaddr addr
, uint64_t size
)
2467 MemoryRegionSection ret
;
2469 ret
= memory_region_find_rcu(mr
, addr
, size
);
2471 memory_region_ref(ret
.mr
);
2477 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2482 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2484 return mr
&& mr
!= container
;
2487 void memory_global_dirty_log_sync(void)
2489 memory_region_sync_dirty_bitmap(NULL
);
2492 static VMChangeStateEntry
*vmstate_change
;
2494 void memory_global_dirty_log_start(void)
2496 if (vmstate_change
) {
2497 qemu_del_vm_change_state_handler(vmstate_change
);
2498 vmstate_change
= NULL
;
2501 global_dirty_log
= true;
2503 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2505 /* Refresh DIRTY_LOG_MIGRATION bit. */
2506 memory_region_transaction_begin();
2507 memory_region_update_pending
= true;
2508 memory_region_transaction_commit();
2511 static void memory_global_dirty_log_do_stop(void)
2513 global_dirty_log
= false;
2515 /* Refresh DIRTY_LOG_MIGRATION bit. */
2516 memory_region_transaction_begin();
2517 memory_region_update_pending
= true;
2518 memory_region_transaction_commit();
2520 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2523 static void memory_vm_change_state_handler(void *opaque
, int running
,
2527 memory_global_dirty_log_do_stop();
2529 if (vmstate_change
) {
2530 qemu_del_vm_change_state_handler(vmstate_change
);
2531 vmstate_change
= NULL
;
2536 void memory_global_dirty_log_stop(void)
2538 if (!runstate_is_running()) {
2539 if (vmstate_change
) {
2542 vmstate_change
= qemu_add_vm_change_state_handler(
2543 memory_vm_change_state_handler
, NULL
);
2547 memory_global_dirty_log_do_stop();
2550 static void listener_add_address_space(MemoryListener
*listener
,
2556 if (listener
->begin
) {
2557 listener
->begin(listener
);
2559 if (global_dirty_log
) {
2560 if (listener
->log_global_start
) {
2561 listener
->log_global_start(listener
);
2565 view
= address_space_get_flatview(as
);
2566 FOR_EACH_FLAT_RANGE(fr
, view
) {
2567 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2569 if (listener
->region_add
) {
2570 listener
->region_add(listener
, §ion
);
2572 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2573 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2576 if (listener
->commit
) {
2577 listener
->commit(listener
);
2579 flatview_unref(view
);
2582 static void listener_del_address_space(MemoryListener
*listener
,
2588 if (listener
->begin
) {
2589 listener
->begin(listener
);
2591 view
= address_space_get_flatview(as
);
2592 FOR_EACH_FLAT_RANGE(fr
, view
) {
2593 MemoryRegionSection section
= section_from_flat_range(fr
, view
);
2595 if (fr
->dirty_log_mask
&& listener
->log_stop
) {
2596 listener
->log_stop(listener
, §ion
, fr
->dirty_log_mask
, 0);
2598 if (listener
->region_del
) {
2599 listener
->region_del(listener
, §ion
);
2602 if (listener
->commit
) {
2603 listener
->commit(listener
);
2605 flatview_unref(view
);
2608 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2610 MemoryListener
*other
= NULL
;
2612 listener
->address_space
= as
;
2613 if (QTAILQ_EMPTY(&memory_listeners
)
2614 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2615 memory_listeners
)->priority
) {
2616 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2618 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2619 if (listener
->priority
< other
->priority
) {
2623 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2626 if (QTAILQ_EMPTY(&as
->listeners
)
2627 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
,
2628 memory_listeners
)->priority
) {
2629 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2631 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2632 if (listener
->priority
< other
->priority
) {
2636 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2639 listener_add_address_space(listener
, as
);
2642 void memory_listener_unregister(MemoryListener
*listener
)
2644 if (!listener
->address_space
) {
2648 listener_del_address_space(listener
, listener
->address_space
);
2649 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2650 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2651 listener
->address_space
= NULL
;
2654 bool memory_region_request_mmio_ptr(MemoryRegion
*mr
, hwaddr addr
)
2658 unsigned offset
= 0;
2659 Object
*new_interface
;
2661 if (!mr
|| !mr
->ops
->request_ptr
) {
2666 * Avoid an update if the request_ptr call
2667 * memory_region_invalidate_mmio_ptr which seems to be likely when we use
2670 memory_region_transaction_begin();
2672 host
= mr
->ops
->request_ptr(mr
->opaque
, addr
- mr
->addr
, &size
, &offset
);
2674 if (!host
|| !size
) {
2675 memory_region_transaction_commit();
2679 new_interface
= object_new("mmio_interface");
2680 qdev_prop_set_uint64(DEVICE(new_interface
), "start", offset
);
2681 qdev_prop_set_uint64(DEVICE(new_interface
), "end", offset
+ size
- 1);
2682 qdev_prop_set_bit(DEVICE(new_interface
), "ro", true);
2683 qdev_prop_set_ptr(DEVICE(new_interface
), "host_ptr", host
);
2684 qdev_prop_set_ptr(DEVICE(new_interface
), "subregion", mr
);
2685 object_property_set_bool(OBJECT(new_interface
), true, "realized", NULL
);
2687 memory_region_transaction_commit();
2691 typedef struct MMIOPtrInvalidate
{
2697 } MMIOPtrInvalidate
;
2699 #define MAX_MMIO_INVALIDATE 10
2700 static MMIOPtrInvalidate mmio_ptr_invalidate_list
[MAX_MMIO_INVALIDATE
];
2702 static void memory_region_do_invalidate_mmio_ptr(CPUState
*cpu
,
2703 run_on_cpu_data data
)
2705 MMIOPtrInvalidate
*invalidate_data
= (MMIOPtrInvalidate
*)data
.host_ptr
;
2706 MemoryRegion
*mr
= invalidate_data
->mr
;
2707 hwaddr offset
= invalidate_data
->offset
;
2708 unsigned size
= invalidate_data
->size
;
2709 MemoryRegionSection section
= memory_region_find(mr
, offset
, size
);
2711 qemu_mutex_lock_iothread();
2713 /* Reset dirty so this doesn't happen later. */
2714 cpu_physical_memory_test_and_clear_dirty(offset
, size
, 1);
2716 if (section
.mr
!= mr
) {
2717 /* memory_region_find add a ref on section.mr */
2718 memory_region_unref(section
.mr
);
2719 if (MMIO_INTERFACE(section
.mr
->owner
)) {
2720 /* We found the interface just drop it. */
2721 object_property_set_bool(section
.mr
->owner
, false, "realized",
2723 object_unref(section
.mr
->owner
);
2724 object_unparent(section
.mr
->owner
);
2728 qemu_mutex_unlock_iothread();
2730 if (invalidate_data
->allocated
) {
2731 g_free(invalidate_data
);
2733 invalidate_data
->busy
= 0;
2737 void memory_region_invalidate_mmio_ptr(MemoryRegion
*mr
, hwaddr offset
,
2741 MMIOPtrInvalidate
*invalidate_data
= NULL
;
2743 for (i
= 0; i
< MAX_MMIO_INVALIDATE
; i
++) {
2744 if (atomic_cmpxchg(&(mmio_ptr_invalidate_list
[i
].busy
), 0, 1) == 0) {
2745 invalidate_data
= &mmio_ptr_invalidate_list
[i
];
2750 if (!invalidate_data
) {
2751 invalidate_data
= g_malloc0(sizeof(MMIOPtrInvalidate
));
2752 invalidate_data
->allocated
= 1;
2755 invalidate_data
->mr
= mr
;
2756 invalidate_data
->offset
= offset
;
2757 invalidate_data
->size
= size
;
2759 async_safe_run_on_cpu(first_cpu
, memory_region_do_invalidate_mmio_ptr
,
2760 RUN_ON_CPU_HOST_PTR(invalidate_data
));
2763 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2765 memory_region_ref(root
);
2767 as
->current_map
= NULL
;
2768 as
->ioeventfd_nb
= 0;
2769 as
->ioeventfds
= NULL
;
2770 QTAILQ_INIT(&as
->listeners
);
2771 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2772 as
->name
= g_strdup(name
? name
: "anonymous");
2773 address_space_update_topology(as
);
2774 address_space_update_ioeventfds(as
);
2777 static void do_address_space_destroy(AddressSpace
*as
)
2779 assert(QTAILQ_EMPTY(&as
->listeners
));
2781 flatview_unref(as
->current_map
);
2783 g_free(as
->ioeventfds
);
2784 memory_region_unref(as
->root
);
2787 void address_space_destroy(AddressSpace
*as
)
2789 MemoryRegion
*root
= as
->root
;
2791 /* Flush out anything from MemoryListeners listening in on this */
2792 memory_region_transaction_begin();
2794 memory_region_transaction_commit();
2795 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2797 /* At this point, as->dispatch and as->current_map are dummy
2798 * entries that the guest should never use. Wait for the old
2799 * values to expire before freeing the data.
2802 call_rcu(as
, do_address_space_destroy
, rcu
);
2805 static const char *memory_region_type(MemoryRegion
*mr
)
2807 if (memory_region_is_ram_device(mr
)) {
2809 } else if (memory_region_is_romd(mr
)) {
2811 } else if (memory_region_is_rom(mr
)) {
2813 } else if (memory_region_is_ram(mr
)) {
2820 typedef struct MemoryRegionList MemoryRegionList
;
2822 struct MemoryRegionList
{
2823 const MemoryRegion
*mr
;
2824 QTAILQ_ENTRY(MemoryRegionList
) mrqueue
;
2827 typedef QTAILQ_HEAD(mrqueue
, MemoryRegionList
) MemoryRegionListHead
;
2829 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2830 int128_sub((size), int128_one())) : 0)
2831 #define MTREE_INDENT " "
2833 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2834 const MemoryRegion
*mr
, unsigned int level
,
2836 MemoryRegionListHead
*alias_print_queue
)
2838 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2839 MemoryRegionListHead submr_print_queue
;
2840 const MemoryRegion
*submr
;
2842 hwaddr cur_start
, cur_end
;
2848 for (i
= 0; i
< level
; i
++) {
2849 mon_printf(f
, MTREE_INDENT
);
2852 cur_start
= base
+ mr
->addr
;
2853 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2856 * Try to detect overflow of memory region. This should never
2857 * happen normally. When it happens, we dump something to warn the
2858 * user who is observing this.
2860 if (cur_start
< base
|| cur_end
< cur_start
) {
2861 mon_printf(f
, "[DETECTED OVERFLOW!] ");
2865 MemoryRegionList
*ml
;
2868 /* check if the alias is already in the queue */
2869 QTAILQ_FOREACH(ml
, alias_print_queue
, mrqueue
) {
2870 if (ml
->mr
== mr
->alias
) {
2876 ml
= g_new(MemoryRegionList
, 1);
2878 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, mrqueue
);
2880 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2881 " (prio %d, %s): alias %s @%s " TARGET_FMT_plx
2882 "-" TARGET_FMT_plx
"%s\n",
2885 memory_region_type((MemoryRegion
*)mr
),
2886 memory_region_name(mr
),
2887 memory_region_name(mr
->alias
),
2889 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2890 mr
->enabled
? "" : " [disabled]");
2893 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %s): %s%s\n",
2896 memory_region_type((MemoryRegion
*)mr
),
2897 memory_region_name(mr
),
2898 mr
->enabled
? "" : " [disabled]");
2901 QTAILQ_INIT(&submr_print_queue
);
2903 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2904 new_ml
= g_new(MemoryRegionList
, 1);
2906 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2907 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2908 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2909 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2910 QTAILQ_INSERT_BEFORE(ml
, new_ml
, mrqueue
);
2916 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, mrqueue
);
2920 QTAILQ_FOREACH(ml
, &submr_print_queue
, mrqueue
) {
2921 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, cur_start
,
2925 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, mrqueue
, next_ml
) {
2930 struct FlatViewInfo
{
2931 fprintf_function mon_printf
;
2937 static void mtree_print_flatview(gpointer key
, gpointer value
,
2940 FlatView
*view
= key
;
2941 GArray
*fv_address_spaces
= value
;
2942 struct FlatViewInfo
*fvi
= user_data
;
2943 fprintf_function p
= fvi
->mon_printf
;
2945 FlatRange
*range
= &view
->ranges
[0];
2951 p(f
, "FlatView #%d\n", fvi
->counter
);
2954 for (i
= 0; i
< fv_address_spaces
->len
; ++i
) {
2955 as
= g_array_index(fv_address_spaces
, AddressSpace
*, i
);
2956 p(f
, " AS \"%s\", root: %s", as
->name
, memory_region_name(as
->root
));
2957 if (as
->root
->alias
) {
2958 p(f
, ", alias %s", memory_region_name(as
->root
->alias
));
2963 p(f
, " Root memory region: %s\n",
2964 view
->root
? memory_region_name(view
->root
) : "(none)");
2967 p(f
, MTREE_INDENT
"No rendered FlatView\n\n");
2973 if (range
->offset_in_region
) {
2974 p(f
, MTREE_INDENT TARGET_FMT_plx
"-"
2975 TARGET_FMT_plx
" (prio %d, %s): %s @" TARGET_FMT_plx
"\n",
2976 int128_get64(range
->addr
.start
),
2977 int128_get64(range
->addr
.start
) + MR_SIZE(range
->addr
.size
),
2979 range
->readonly
? "rom" : memory_region_type(mr
),
2980 memory_region_name(mr
),
2981 range
->offset_in_region
);
2983 p(f
, MTREE_INDENT TARGET_FMT_plx
"-"
2984 TARGET_FMT_plx
" (prio %d, %s): %s\n",
2985 int128_get64(range
->addr
.start
),
2986 int128_get64(range
->addr
.start
) + MR_SIZE(range
->addr
.size
),
2988 range
->readonly
? "rom" : memory_region_type(mr
),
2989 memory_region_name(mr
));
2994 #if !defined(CONFIG_USER_ONLY)
2995 if (fvi
->dispatch_tree
&& view
->root
) {
2996 mtree_print_dispatch(p
, f
, view
->dispatch
, view
->root
);
3003 static gboolean
mtree_info_flatview_free(gpointer key
, gpointer value
,
3006 FlatView
*view
= key
;
3007 GArray
*fv_address_spaces
= value
;
3009 g_array_unref(fv_address_spaces
);
3010 flatview_unref(view
);
3015 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
,
3018 MemoryRegionListHead ml_head
;
3019 MemoryRegionList
*ml
, *ml2
;
3024 struct FlatViewInfo fvi
= {
3025 .mon_printf
= mon_printf
,
3028 .dispatch_tree
= dispatch_tree
3030 GArray
*fv_address_spaces
;
3031 GHashTable
*views
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3033 /* Gather all FVs in one table */
3034 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3035 view
= address_space_get_flatview(as
);
3037 fv_address_spaces
= g_hash_table_lookup(views
, view
);
3038 if (!fv_address_spaces
) {
3039 fv_address_spaces
= g_array_new(false, false, sizeof(as
));
3040 g_hash_table_insert(views
, view
, fv_address_spaces
);
3043 g_array_append_val(fv_address_spaces
, as
);
3047 g_hash_table_foreach(views
, mtree_print_flatview
, &fvi
);
3050 g_hash_table_foreach_remove(views
, mtree_info_flatview_free
, 0);
3051 g_hash_table_unref(views
);
3056 QTAILQ_INIT(&ml_head
);
3058 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
3059 mon_printf(f
, "address-space: %s\n", as
->name
);
3060 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
3061 mon_printf(f
, "\n");
3064 /* print aliased regions */
3065 QTAILQ_FOREACH(ml
, &ml_head
, mrqueue
) {
3066 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
3067 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
3068 mon_printf(f
, "\n");
3071 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, mrqueue
, ml2
) {
3076 void memory_region_init_ram(MemoryRegion
*mr
,
3077 struct Object
*owner
,
3082 DeviceState
*owner_dev
;
3085 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
3087 error_propagate(errp
, err
);
3090 /* This will assert if owner is neither NULL nor a DeviceState.
3091 * We only want the owner here for the purposes of defining a
3092 * unique name for migration. TODO: Ideally we should implement
3093 * a naming scheme for Objects which are not DeviceStates, in
3094 * which case we can relax this restriction.
3096 owner_dev
= DEVICE(owner
);
3097 vmstate_register_ram(mr
, owner_dev
);
3100 void memory_region_init_rom(MemoryRegion
*mr
,
3101 struct Object
*owner
,
3106 DeviceState
*owner_dev
;
3109 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
3111 error_propagate(errp
, err
);
3114 /* This will assert if owner is neither NULL nor a DeviceState.
3115 * We only want the owner here for the purposes of defining a
3116 * unique name for migration. TODO: Ideally we should implement
3117 * a naming scheme for Objects which are not DeviceStates, in
3118 * which case we can relax this restriction.
3120 owner_dev
= DEVICE(owner
);
3121 vmstate_register_ram(mr
, owner_dev
);
3124 void memory_region_init_rom_device(MemoryRegion
*mr
,
3125 struct Object
*owner
,
3126 const MemoryRegionOps
*ops
,
3132 DeviceState
*owner_dev
;
3135 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
3138 error_propagate(errp
, err
);
3141 /* This will assert if owner is neither NULL nor a DeviceState.
3142 * We only want the owner here for the purposes of defining a
3143 * unique name for migration. TODO: Ideally we should implement
3144 * a naming scheme for Objects which are not DeviceStates, in
3145 * which case we can relax this restriction.
3147 owner_dev
= DEVICE(owner
);
3148 vmstate_register_ram(mr
, owner_dev
);
3151 static const TypeInfo memory_region_info
= {
3152 .parent
= TYPE_OBJECT
,
3153 .name
= TYPE_MEMORY_REGION
,
3154 .instance_size
= sizeof(MemoryRegion
),
3155 .instance_init
= memory_region_initfn
,
3156 .instance_finalize
= memory_region_finalize
,
3159 static const TypeInfo iommu_memory_region_info
= {
3160 .parent
= TYPE_MEMORY_REGION
,
3161 .name
= TYPE_IOMMU_MEMORY_REGION
,
3162 .class_size
= sizeof(IOMMUMemoryRegionClass
),
3163 .instance_size
= sizeof(IOMMUMemoryRegion
),
3164 .instance_init
= iommu_memory_region_initfn
,
3168 static void memory_register_types(void)
3170 type_register_static(&memory_region_info
);
3171 type_register_static(&iommu_memory_region_info
);
3174 type_init(memory_register_types
)