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"
36 //#define DEBUG_UNASSIGNED
38 static unsigned memory_region_transaction_depth
;
39 static bool memory_region_update_pending
;
40 static bool ioeventfd_update_pending
;
41 static bool global_dirty_log
= false;
43 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
44 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
46 static QTAILQ_HEAD(, AddressSpace
) address_spaces
47 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
49 typedef struct AddrRange AddrRange
;
52 * Note that signed integers are needed for negative offsetting in aliases
53 * (large MemoryRegion::alias_offset).
60 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
62 return (AddrRange
) { start
, size
};
65 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
67 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
70 static Int128
addrrange_end(AddrRange r
)
72 return int128_add(r
.start
, r
.size
);
75 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
77 int128_addto(&range
.start
, delta
);
81 static bool addrrange_contains(AddrRange range
, Int128 addr
)
83 return int128_ge(addr
, range
.start
)
84 && int128_lt(addr
, addrrange_end(range
));
87 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
89 return addrrange_contains(r1
, r2
.start
)
90 || addrrange_contains(r2
, r1
.start
);
93 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
95 Int128 start
= int128_max(r1
.start
, r2
.start
);
96 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
97 return addrrange_make(start
, int128_sub(end
, start
));
100 enum ListenerDirection
{ Forward
, Reverse
};
102 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
104 MemoryListener *_listener; \
106 switch (_direction) { \
108 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
109 if (_listener->_callback) { \
110 _listener->_callback(_listener, ##_args); \
115 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
116 memory_listeners, link) { \
117 if (_listener->_callback) { \
118 _listener->_callback(_listener, ##_args); \
127 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
129 MemoryListener *_listener; \
130 struct memory_listeners_as *list = &(_as)->listeners; \
132 switch (_direction) { \
134 QTAILQ_FOREACH(_listener, list, link_as) { \
135 if (_listener->_callback) { \
136 _listener->_callback(_listener, _section, ##_args); \
141 QTAILQ_FOREACH_REVERSE(_listener, list, memory_listeners_as, \
143 if (_listener->_callback) { \
144 _listener->_callback(_listener, _section, ##_args); \
153 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
154 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
156 MemoryRegionSection mrs = section_from_flat_range(fr, as); \
157 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
160 struct CoalescedMemoryRange
{
162 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
165 struct MemoryRegionIoeventfd
{
172 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
173 MemoryRegionIoeventfd b
)
175 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
177 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
179 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
181 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
183 } else if (a
.match_data
< b
.match_data
) {
185 } else if (a
.match_data
> b
.match_data
) {
187 } else if (a
.match_data
) {
188 if (a
.data
< b
.data
) {
190 } else if (a
.data
> b
.data
) {
196 } else if (a
.e
> b
.e
) {
202 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
203 MemoryRegionIoeventfd b
)
205 return !memory_region_ioeventfd_before(a
, b
)
206 && !memory_region_ioeventfd_before(b
, a
);
209 typedef struct FlatRange FlatRange
;
210 typedef struct FlatView FlatView
;
212 /* Range of memory in the global map. Addresses are absolute. */
215 hwaddr offset_in_region
;
217 uint8_t dirty_log_mask
;
222 /* Flattened global view of current active memory hierarchy. Kept in sorted
230 unsigned nr_allocated
;
233 typedef struct AddressSpaceOps AddressSpaceOps
;
235 #define FOR_EACH_FLAT_RANGE(var, view) \
236 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
238 static inline MemoryRegionSection
239 section_from_flat_range(FlatRange
*fr
, AddressSpace
*as
)
241 return (MemoryRegionSection
) {
244 .offset_within_region
= fr
->offset_in_region
,
245 .size
= fr
->addr
.size
,
246 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
247 .readonly
= fr
->readonly
,
251 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
253 return a
->mr
== b
->mr
254 && addrrange_equal(a
->addr
, b
->addr
)
255 && a
->offset_in_region
== b
->offset_in_region
256 && a
->romd_mode
== b
->romd_mode
257 && a
->readonly
== b
->readonly
;
260 static void flatview_init(FlatView
*view
)
265 view
->nr_allocated
= 0;
268 /* Insert a range into a given position. Caller is responsible for maintaining
271 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
273 if (view
->nr
== view
->nr_allocated
) {
274 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
275 view
->ranges
= g_realloc(view
->ranges
,
276 view
->nr_allocated
* sizeof(*view
->ranges
));
278 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
279 (view
->nr
- pos
) * sizeof(FlatRange
));
280 view
->ranges
[pos
] = *range
;
281 memory_region_ref(range
->mr
);
285 static void flatview_destroy(FlatView
*view
)
289 for (i
= 0; i
< view
->nr
; i
++) {
290 memory_region_unref(view
->ranges
[i
].mr
);
292 g_free(view
->ranges
);
296 static void flatview_ref(FlatView
*view
)
298 atomic_inc(&view
->ref
);
301 static void flatview_unref(FlatView
*view
)
303 if (atomic_fetch_dec(&view
->ref
) == 1) {
304 flatview_destroy(view
);
308 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
310 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
312 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
314 int128_make64(r2
->offset_in_region
))
315 && r1
->dirty_log_mask
== r2
->dirty_log_mask
316 && r1
->romd_mode
== r2
->romd_mode
317 && r1
->readonly
== r2
->readonly
;
320 /* Attempt to simplify a view by merging adjacent ranges */
321 static void flatview_simplify(FlatView
*view
)
326 while (i
< view
->nr
) {
329 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
330 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
334 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
335 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
340 static bool memory_region_big_endian(MemoryRegion
*mr
)
342 #ifdef TARGET_WORDS_BIGENDIAN
343 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
345 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
349 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
351 #ifdef TARGET_WORDS_BIGENDIAN
352 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
354 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
358 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
360 if (memory_region_wrong_endianness(mr
)) {
365 *data
= bswap16(*data
);
368 *data
= bswap32(*data
);
371 *data
= bswap64(*data
);
379 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
382 hwaddr abs_addr
= offset
;
384 abs_addr
+= mr
->addr
;
385 for (root
= mr
; root
->container
; ) {
386 root
= root
->container
;
387 abs_addr
+= root
->addr
;
393 static int get_cpu_index(void)
396 return current_cpu
->cpu_index
;
401 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
411 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
413 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
414 } else if (mr
== &io_mem_notdirty
) {
415 /* Accesses to code which has previously been translated into a TB show
416 * up in the MMIO path, as accesses to the io_mem_notdirty
418 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
419 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
420 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
421 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
423 *value
|= (tmp
& mask
) << shift
;
427 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
437 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
439 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
440 } else if (mr
== &io_mem_notdirty
) {
441 /* Accesses to code which has previously been translated into a TB show
442 * up in the MMIO path, as accesses to the io_mem_notdirty
444 trace_memory_region_tb_read(get_cpu_index(), 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(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
449 *value
|= (tmp
& mask
) << shift
;
453 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
464 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
466 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
467 } else if (mr
== &io_mem_notdirty
) {
468 /* Accesses to code which has previously been translated into a TB show
469 * up in the MMIO path, as accesses to the io_mem_notdirty
471 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
472 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
473 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
474 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
476 *value
|= (tmp
& mask
) << shift
;
480 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
490 tmp
= (*value
>> shift
) & mask
;
492 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
493 } else if (mr
== &io_mem_notdirty
) {
494 /* Accesses to code which has previously been translated into a TB show
495 * up in the MMIO path, as accesses to the io_mem_notdirty
497 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
498 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
499 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
500 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
502 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
506 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
516 tmp
= (*value
>> shift
) & mask
;
518 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
519 } else if (mr
== &io_mem_notdirty
) {
520 /* Accesses to code which has previously been translated into a TB show
521 * up in the MMIO path, as accesses to the io_mem_notdirty
523 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
524 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
525 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
526 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
528 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
532 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
542 tmp
= (*value
>> shift
) & mask
;
544 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
545 } else if (mr
== &io_mem_notdirty
) {
546 /* Accesses to code which has previously been translated into a TB show
547 * up in the MMIO path, as accesses to the io_mem_notdirty
549 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
550 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
551 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
552 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
554 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
557 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
560 unsigned access_size_min
,
561 unsigned access_size_max
,
562 MemTxResult (*access
)(MemoryRegion
*mr
,
572 uint64_t access_mask
;
573 unsigned access_size
;
575 MemTxResult r
= MEMTX_OK
;
577 if (!access_size_min
) {
580 if (!access_size_max
) {
584 /* FIXME: support unaligned access? */
585 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
586 access_mask
= -1ULL >> (64 - access_size
* 8);
587 if (memory_region_big_endian(mr
)) {
588 for (i
= 0; i
< size
; i
+= access_size
) {
589 r
|= access(mr
, addr
+ i
, value
, access_size
,
590 (size
- access_size
- i
) * 8, access_mask
, attrs
);
593 for (i
= 0; i
< size
; i
+= access_size
) {
594 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
601 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
605 while (mr
->container
) {
608 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
609 if (mr
== as
->root
) {
616 /* Render a memory region into the global view. Ranges in @view obscure
619 static void render_memory_region(FlatView
*view
,
625 MemoryRegion
*subregion
;
627 hwaddr offset_in_region
;
637 int128_addto(&base
, int128_make64(mr
->addr
));
638 readonly
|= mr
->readonly
;
640 tmp
= addrrange_make(base
, mr
->size
);
642 if (!addrrange_intersects(tmp
, clip
)) {
646 clip
= addrrange_intersection(tmp
, clip
);
649 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
650 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
651 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
655 /* Render subregions in priority order. */
656 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
657 render_memory_region(view
, subregion
, base
, clip
, readonly
);
660 if (!mr
->terminates
) {
664 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
669 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
670 fr
.romd_mode
= mr
->romd_mode
;
671 fr
.readonly
= readonly
;
673 /* Render the region itself into any gaps left by the current view. */
674 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
675 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
678 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
679 now
= int128_min(remain
,
680 int128_sub(view
->ranges
[i
].addr
.start
, base
));
681 fr
.offset_in_region
= offset_in_region
;
682 fr
.addr
= addrrange_make(base
, now
);
683 flatview_insert(view
, i
, &fr
);
685 int128_addto(&base
, now
);
686 offset_in_region
+= int128_get64(now
);
687 int128_subfrom(&remain
, now
);
689 now
= int128_sub(int128_min(int128_add(base
, remain
),
690 addrrange_end(view
->ranges
[i
].addr
)),
692 int128_addto(&base
, now
);
693 offset_in_region
+= int128_get64(now
);
694 int128_subfrom(&remain
, now
);
696 if (int128_nz(remain
)) {
697 fr
.offset_in_region
= offset_in_region
;
698 fr
.addr
= addrrange_make(base
, remain
);
699 flatview_insert(view
, i
, &fr
);
703 /* Render a memory topology into a list of disjoint absolute ranges. */
704 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
708 view
= g_new(FlatView
, 1);
712 render_memory_region(view
, mr
, int128_zero(),
713 addrrange_make(int128_zero(), int128_2_64()), false);
715 flatview_simplify(view
);
720 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
721 MemoryRegionIoeventfd
*fds_new
,
723 MemoryRegionIoeventfd
*fds_old
,
727 MemoryRegionIoeventfd
*fd
;
728 MemoryRegionSection section
;
730 /* Generate a symmetric difference of the old and new fd sets, adding
731 * and deleting as necessary.
735 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
736 if (iold
< fds_old_nb
737 && (inew
== fds_new_nb
738 || memory_region_ioeventfd_before(fds_old
[iold
],
741 section
= (MemoryRegionSection
) {
743 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
744 .size
= fd
->addr
.size
,
746 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
747 fd
->match_data
, fd
->data
, fd
->e
);
749 } else if (inew
< fds_new_nb
750 && (iold
== fds_old_nb
751 || memory_region_ioeventfd_before(fds_new
[inew
],
754 section
= (MemoryRegionSection
) {
756 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
757 .size
= fd
->addr
.size
,
759 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
760 fd
->match_data
, fd
->data
, fd
->e
);
769 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
774 view
= atomic_rcu_read(&as
->current_map
);
780 static void address_space_update_ioeventfds(AddressSpace
*as
)
784 unsigned ioeventfd_nb
= 0;
785 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
789 view
= address_space_get_flatview(as
);
790 FOR_EACH_FLAT_RANGE(fr
, view
) {
791 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
792 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
793 int128_sub(fr
->addr
.start
,
794 int128_make64(fr
->offset_in_region
)));
795 if (addrrange_intersects(fr
->addr
, tmp
)) {
797 ioeventfds
= g_realloc(ioeventfds
,
798 ioeventfd_nb
* sizeof(*ioeventfds
));
799 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
800 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
805 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
806 as
->ioeventfds
, as
->ioeventfd_nb
);
808 g_free(as
->ioeventfds
);
809 as
->ioeventfds
= ioeventfds
;
810 as
->ioeventfd_nb
= ioeventfd_nb
;
811 flatview_unref(view
);
814 static void address_space_update_topology_pass(AddressSpace
*as
,
815 const FlatView
*old_view
,
816 const FlatView
*new_view
,
820 FlatRange
*frold
, *frnew
;
822 /* Generate a symmetric difference of the old and new memory maps.
823 * Kill ranges in the old map, and instantiate ranges in the new map.
826 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
827 if (iold
< old_view
->nr
) {
828 frold
= &old_view
->ranges
[iold
];
832 if (inew
< new_view
->nr
) {
833 frnew
= &new_view
->ranges
[inew
];
840 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
841 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
842 && !flatrange_equal(frold
, frnew
)))) {
843 /* In old but not in new, or in both but attributes changed. */
846 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
850 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
851 /* In both and unchanged (except logging may have changed) */
854 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
855 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
856 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
857 frold
->dirty_log_mask
,
858 frnew
->dirty_log_mask
);
860 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
861 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
862 frold
->dirty_log_mask
,
863 frnew
->dirty_log_mask
);
873 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
882 static void address_space_update_topology(AddressSpace
*as
)
884 FlatView
*old_view
= address_space_get_flatview(as
);
885 FlatView
*new_view
= generate_memory_topology(as
->root
);
887 address_space_update_topology_pass(as
, old_view
, new_view
, false);
888 address_space_update_topology_pass(as
, old_view
, new_view
, true);
890 /* Writes are protected by the BQL. */
891 atomic_rcu_set(&as
->current_map
, new_view
);
892 call_rcu(old_view
, flatview_unref
, rcu
);
894 /* Note that all the old MemoryRegions are still alive up to this
895 * point. This relieves most MemoryListeners from the need to
896 * ref/unref the MemoryRegions they get---unless they use them
897 * outside the iothread mutex, in which case precise reference
898 * counting is necessary.
900 flatview_unref(old_view
);
902 address_space_update_ioeventfds(as
);
905 void memory_region_transaction_begin(void)
907 qemu_flush_coalesced_mmio_buffer();
908 ++memory_region_transaction_depth
;
911 void memory_region_transaction_commit(void)
915 assert(memory_region_transaction_depth
);
916 assert(qemu_mutex_iothread_locked());
918 --memory_region_transaction_depth
;
919 if (!memory_region_transaction_depth
) {
920 if (memory_region_update_pending
) {
921 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
923 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
924 address_space_update_topology(as
);
926 memory_region_update_pending
= false;
927 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
928 } else if (ioeventfd_update_pending
) {
929 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
930 address_space_update_ioeventfds(as
);
932 ioeventfd_update_pending
= false;
937 static void memory_region_destructor_none(MemoryRegion
*mr
)
941 static void memory_region_destructor_ram(MemoryRegion
*mr
)
943 qemu_ram_free(mr
->ram_block
);
946 static bool memory_region_need_escape(char c
)
948 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
951 static char *memory_region_escape_name(const char *name
)
958 for (p
= name
; *p
; p
++) {
959 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
961 if (bytes
== p
- name
) {
962 return g_memdup(name
, bytes
+ 1);
965 escaped
= g_malloc(bytes
+ 1);
966 for (p
= name
, q
= escaped
; *p
; p
++) {
968 if (unlikely(memory_region_need_escape(c
))) {
971 *q
++ = "0123456789abcdef"[c
>> 4];
972 c
= "0123456789abcdef"[c
& 15];
980 static void memory_region_do_init(MemoryRegion
*mr
,
985 mr
->size
= int128_make64(size
);
986 if (size
== UINT64_MAX
) {
987 mr
->size
= int128_2_64();
989 mr
->name
= g_strdup(name
);
991 mr
->ram_block
= NULL
;
994 char *escaped_name
= memory_region_escape_name(name
);
995 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
998 owner
= container_get(qdev_get_machine(), "/unattached");
1001 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1002 object_unref(OBJECT(mr
));
1004 g_free(escaped_name
);
1008 void memory_region_init(MemoryRegion
*mr
,
1013 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1014 memory_region_do_init(mr
, owner
, name
, size
);
1017 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1018 void *opaque
, Error
**errp
)
1020 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1021 uint64_t value
= mr
->addr
;
1023 visit_type_uint64(v
, name
, &value
, errp
);
1026 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1027 const char *name
, void *opaque
,
1030 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1031 gchar
*path
= (gchar
*)"";
1033 if (mr
->container
) {
1034 path
= object_get_canonical_path(OBJECT(mr
->container
));
1036 visit_type_str(v
, name
, &path
, errp
);
1037 if (mr
->container
) {
1042 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1045 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1047 return OBJECT(mr
->container
);
1050 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1051 const char *name
, void *opaque
,
1054 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1055 int32_t value
= mr
->priority
;
1057 visit_type_int32(v
, name
, &value
, errp
);
1060 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1061 void *opaque
, Error
**errp
)
1063 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1064 uint64_t value
= memory_region_size(mr
);
1066 visit_type_uint64(v
, name
, &value
, errp
);
1069 static void memory_region_initfn(Object
*obj
)
1071 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1074 mr
->ops
= &unassigned_mem_ops
;
1076 mr
->romd_mode
= true;
1077 mr
->global_locking
= true;
1078 mr
->destructor
= memory_region_destructor_none
;
1079 QTAILQ_INIT(&mr
->subregions
);
1080 QTAILQ_INIT(&mr
->coalesced
);
1082 op
= object_property_add(OBJECT(mr
), "container",
1083 "link<" TYPE_MEMORY_REGION
">",
1084 memory_region_get_container
,
1085 NULL
, /* memory_region_set_container */
1086 NULL
, NULL
, &error_abort
);
1087 op
->resolve
= memory_region_resolve_container
;
1089 object_property_add(OBJECT(mr
), "addr", "uint64",
1090 memory_region_get_addr
,
1091 NULL
, /* memory_region_set_addr */
1092 NULL
, NULL
, &error_abort
);
1093 object_property_add(OBJECT(mr
), "priority", "uint32",
1094 memory_region_get_priority
,
1095 NULL
, /* memory_region_set_priority */
1096 NULL
, NULL
, &error_abort
);
1097 object_property_add(OBJECT(mr
), "size", "uint64",
1098 memory_region_get_size
,
1099 NULL
, /* memory_region_set_size, */
1100 NULL
, NULL
, &error_abort
);
1103 static void iommu_memory_region_initfn(Object
*obj
)
1105 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1107 mr
->is_iommu
= true;
1110 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1113 #ifdef DEBUG_UNASSIGNED
1114 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1116 if (current_cpu
!= NULL
) {
1117 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1122 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1123 uint64_t val
, unsigned size
)
1125 #ifdef DEBUG_UNASSIGNED
1126 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1128 if (current_cpu
!= NULL
) {
1129 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1133 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1134 unsigned size
, bool is_write
)
1139 const MemoryRegionOps unassigned_mem_ops
= {
1140 .valid
.accepts
= unassigned_mem_accepts
,
1141 .endianness
= DEVICE_NATIVE_ENDIAN
,
1144 static uint64_t memory_region_ram_device_read(void *opaque
,
1145 hwaddr addr
, unsigned size
)
1147 MemoryRegion
*mr
= opaque
;
1148 uint64_t data
= (uint64_t)~0;
1152 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1155 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1158 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1161 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1165 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1170 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1171 uint64_t data
, unsigned size
)
1173 MemoryRegion
*mr
= opaque
;
1175 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1179 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1182 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1185 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1188 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1193 static const MemoryRegionOps ram_device_mem_ops
= {
1194 .read
= memory_region_ram_device_read
,
1195 .write
= memory_region_ram_device_write
,
1196 .endianness
= DEVICE_HOST_ENDIAN
,
1198 .min_access_size
= 1,
1199 .max_access_size
= 8,
1203 .min_access_size
= 1,
1204 .max_access_size
= 8,
1209 bool memory_region_access_valid(MemoryRegion
*mr
,
1214 int access_size_min
, access_size_max
;
1217 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1221 if (!mr
->ops
->valid
.accepts
) {
1225 access_size_min
= mr
->ops
->valid
.min_access_size
;
1226 if (!mr
->ops
->valid
.min_access_size
) {
1227 access_size_min
= 1;
1230 access_size_max
= mr
->ops
->valid
.max_access_size
;
1231 if (!mr
->ops
->valid
.max_access_size
) {
1232 access_size_max
= 4;
1235 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1236 for (i
= 0; i
< size
; i
+= access_size
) {
1237 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1246 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1254 if (mr
->ops
->read
) {
1255 return access_with_adjusted_size(addr
, pval
, size
,
1256 mr
->ops
->impl
.min_access_size
,
1257 mr
->ops
->impl
.max_access_size
,
1258 memory_region_read_accessor
,
1260 } else if (mr
->ops
->read_with_attrs
) {
1261 return access_with_adjusted_size(addr
, pval
, size
,
1262 mr
->ops
->impl
.min_access_size
,
1263 mr
->ops
->impl
.max_access_size
,
1264 memory_region_read_with_attrs_accessor
,
1267 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1268 memory_region_oldmmio_read_accessor
,
1273 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1281 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1282 *pval
= unassigned_mem_read(mr
, addr
, size
);
1283 return MEMTX_DECODE_ERROR
;
1286 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1287 adjust_endianness(mr
, pval
, size
);
1291 /* Return true if an eventfd was signalled */
1292 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1298 MemoryRegionIoeventfd ioeventfd
= {
1299 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1304 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1305 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1306 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1308 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1309 event_notifier_set(ioeventfd
.e
);
1317 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1323 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1324 unassigned_mem_write(mr
, addr
, data
, size
);
1325 return MEMTX_DECODE_ERROR
;
1328 adjust_endianness(mr
, &data
, size
);
1330 if ((!kvm_eventfds_enabled()) &&
1331 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1335 if (mr
->ops
->write
) {
1336 return access_with_adjusted_size(addr
, &data
, size
,
1337 mr
->ops
->impl
.min_access_size
,
1338 mr
->ops
->impl
.max_access_size
,
1339 memory_region_write_accessor
, mr
,
1341 } else if (mr
->ops
->write_with_attrs
) {
1343 access_with_adjusted_size(addr
, &data
, size
,
1344 mr
->ops
->impl
.min_access_size
,
1345 mr
->ops
->impl
.max_access_size
,
1346 memory_region_write_with_attrs_accessor
,
1349 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1350 memory_region_oldmmio_write_accessor
,
1355 void memory_region_init_io(MemoryRegion
*mr
,
1357 const MemoryRegionOps
*ops
,
1362 memory_region_init(mr
, owner
, name
, size
);
1363 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1364 mr
->opaque
= opaque
;
1365 mr
->terminates
= true;
1368 void memory_region_init_ram(MemoryRegion
*mr
,
1374 memory_region_init(mr
, owner
, name
, size
);
1376 mr
->terminates
= true;
1377 mr
->destructor
= memory_region_destructor_ram
;
1378 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1379 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1382 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1387 void (*resized
)(const char*,
1392 memory_region_init(mr
, owner
, name
, size
);
1394 mr
->terminates
= true;
1395 mr
->destructor
= memory_region_destructor_ram
;
1396 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1398 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1402 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1403 struct Object
*owner
,
1410 memory_region_init(mr
, owner
, name
, size
);
1412 mr
->terminates
= true;
1413 mr
->destructor
= memory_region_destructor_ram
;
1414 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1415 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1418 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1419 struct Object
*owner
,
1426 memory_region_init(mr
, owner
, name
, size
);
1428 mr
->terminates
= true;
1429 mr
->destructor
= memory_region_destructor_ram
;
1430 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
, share
, fd
, errp
);
1431 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1435 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1441 memory_region_init(mr
, owner
, name
, size
);
1443 mr
->terminates
= true;
1444 mr
->destructor
= memory_region_destructor_ram
;
1445 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1447 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1448 assert(ptr
!= NULL
);
1449 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1452 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1458 memory_region_init_ram_ptr(mr
, owner
, name
, size
, ptr
);
1459 mr
->ram_device
= true;
1460 mr
->ops
= &ram_device_mem_ops
;
1464 void memory_region_init_alias(MemoryRegion
*mr
,
1471 memory_region_init(mr
, owner
, name
, size
);
1473 mr
->alias_offset
= offset
;
1476 void memory_region_init_rom(MemoryRegion
*mr
,
1477 struct Object
*owner
,
1482 memory_region_init(mr
, owner
, name
, size
);
1484 mr
->readonly
= true;
1485 mr
->terminates
= true;
1486 mr
->destructor
= memory_region_destructor_ram
;
1487 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1488 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1491 void memory_region_init_rom_device(MemoryRegion
*mr
,
1493 const MemoryRegionOps
*ops
,
1500 memory_region_init(mr
, owner
, name
, size
);
1502 mr
->opaque
= opaque
;
1503 mr
->terminates
= true;
1504 mr
->rom_device
= true;
1505 mr
->destructor
= memory_region_destructor_ram
;
1506 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1509 void memory_region_init_iommu(void *_iommu_mr
,
1510 size_t instance_size
,
1511 const char *mrtypename
,
1516 struct IOMMUMemoryRegion
*iommu_mr
;
1517 struct MemoryRegion
*mr
;
1519 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1520 mr
= MEMORY_REGION(_iommu_mr
);
1521 memory_region_do_init(mr
, owner
, name
, size
);
1522 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1523 mr
->terminates
= true; /* then re-forwards */
1524 QLIST_INIT(&iommu_mr
->iommu_notify
);
1525 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1528 static void memory_region_finalize(Object
*obj
)
1530 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1532 assert(!mr
->container
);
1534 /* We know the region is not visible in any address space (it
1535 * does not have a container and cannot be a root either because
1536 * it has no references, so we can blindly clear mr->enabled.
1537 * memory_region_set_enabled instead could trigger a transaction
1538 * and cause an infinite loop.
1540 mr
->enabled
= false;
1541 memory_region_transaction_begin();
1542 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1543 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1544 memory_region_del_subregion(mr
, subregion
);
1546 memory_region_transaction_commit();
1549 memory_region_clear_coalescing(mr
);
1550 g_free((char *)mr
->name
);
1551 g_free(mr
->ioeventfds
);
1554 Object
*memory_region_owner(MemoryRegion
*mr
)
1556 Object
*obj
= OBJECT(mr
);
1560 void memory_region_ref(MemoryRegion
*mr
)
1562 /* MMIO callbacks most likely will access data that belongs
1563 * to the owner, hence the need to ref/unref the owner whenever
1564 * the memory region is in use.
1566 * The memory region is a child of its owner. As long as the
1567 * owner doesn't call unparent itself on the memory region,
1568 * ref-ing the owner will also keep the memory region alive.
1569 * Memory regions without an owner are supposed to never go away;
1570 * we do not ref/unref them because it slows down DMA sensibly.
1572 if (mr
&& mr
->owner
) {
1573 object_ref(mr
->owner
);
1577 void memory_region_unref(MemoryRegion
*mr
)
1579 if (mr
&& mr
->owner
) {
1580 object_unref(mr
->owner
);
1584 uint64_t memory_region_size(MemoryRegion
*mr
)
1586 if (int128_eq(mr
->size
, int128_2_64())) {
1589 return int128_get64(mr
->size
);
1592 const char *memory_region_name(const MemoryRegion
*mr
)
1595 ((MemoryRegion
*)mr
)->name
=
1596 object_get_canonical_path_component(OBJECT(mr
));
1601 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1603 return mr
->ram_device
;
1606 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1608 uint8_t mask
= mr
->dirty_log_mask
;
1609 if (global_dirty_log
&& mr
->ram_block
) {
1610 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1615 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1617 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1620 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1622 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1623 IOMMUNotifier
*iommu_notifier
;
1624 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1626 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1627 flags
|= iommu_notifier
->notifier_flags
;
1630 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1631 imrc
->notify_flag_changed(iommu_mr
,
1632 iommu_mr
->iommu_notify_flags
,
1636 iommu_mr
->iommu_notify_flags
= flags
;
1639 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1642 IOMMUMemoryRegion
*iommu_mr
;
1645 memory_region_register_iommu_notifier(mr
->alias
, n
);
1649 /* We need to register for at least one bitfield */
1650 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1651 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1652 assert(n
->start
<= n
->end
);
1653 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1654 memory_region_update_iommu_notify_flags(iommu_mr
);
1657 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1659 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1661 if (imrc
->get_min_page_size
) {
1662 return imrc
->get_min_page_size(iommu_mr
);
1664 return TARGET_PAGE_SIZE
;
1667 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1669 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1670 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1671 hwaddr addr
, granularity
;
1672 IOMMUTLBEntry iotlb
;
1674 /* If the IOMMU has its own replay callback, override */
1676 imrc
->replay(iommu_mr
, n
);
1680 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1682 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1683 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
);
1684 if (iotlb
.perm
!= IOMMU_NONE
) {
1685 n
->notify(n
, &iotlb
);
1688 /* if (2^64 - MR size) < granularity, it's possible to get an
1689 * infinite loop here. This should catch such a wraparound */
1690 if ((addr
+ granularity
) < addr
) {
1696 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
)
1698 IOMMUNotifier
*notifier
;
1700 IOMMU_NOTIFIER_FOREACH(notifier
, iommu_mr
) {
1701 memory_region_iommu_replay(iommu_mr
, notifier
);
1705 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1708 IOMMUMemoryRegion
*iommu_mr
;
1711 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1714 QLIST_REMOVE(n
, node
);
1715 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1716 memory_region_update_iommu_notify_flags(iommu_mr
);
1719 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1720 IOMMUTLBEntry
*entry
)
1722 IOMMUNotifierFlag request_flags
;
1725 * Skip the notification if the notification does not overlap
1726 * with registered range.
1728 if (notifier
->start
> entry
->iova
+ entry
->addr_mask
+ 1 ||
1729 notifier
->end
< entry
->iova
) {
1733 if (entry
->perm
& IOMMU_RW
) {
1734 request_flags
= IOMMU_NOTIFIER_MAP
;
1736 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1739 if (notifier
->notifier_flags
& request_flags
) {
1740 notifier
->notify(notifier
, entry
);
1744 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1745 IOMMUTLBEntry entry
)
1747 IOMMUNotifier
*iommu_notifier
;
1749 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1751 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1752 memory_region_notify_one(iommu_notifier
, &entry
);
1756 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1758 uint8_t mask
= 1 << client
;
1759 uint8_t old_logging
;
1761 assert(client
== DIRTY_MEMORY_VGA
);
1762 old_logging
= mr
->vga_logging_count
;
1763 mr
->vga_logging_count
+= log
? 1 : -1;
1764 if (!!old_logging
== !!mr
->vga_logging_count
) {
1768 memory_region_transaction_begin();
1769 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1770 memory_region_update_pending
|= mr
->enabled
;
1771 memory_region_transaction_commit();
1774 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1775 hwaddr size
, unsigned client
)
1777 assert(mr
->ram_block
);
1778 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr
) + addr
,
1782 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1785 assert(mr
->ram_block
);
1786 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
1788 memory_region_get_dirty_log_mask(mr
));
1791 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1792 hwaddr size
, unsigned client
)
1794 assert(mr
->ram_block
);
1795 return cpu_physical_memory_test_and_clear_dirty(
1796 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1799 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1804 assert(mr
->ram_block
);
1805 return cpu_physical_memory_snapshot_and_clear_dirty(
1806 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1809 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
1810 hwaddr addr
, hwaddr size
)
1812 assert(mr
->ram_block
);
1813 return cpu_physical_memory_snapshot_get_dirty(snap
,
1814 memory_region_get_ram_addr(mr
) + addr
, size
);
1817 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1819 MemoryListener
*listener
;
1824 /* If the same address space has multiple log_sync listeners, we
1825 * visit that address space's FlatView multiple times. But because
1826 * log_sync listeners are rare, it's still cheaper than walking each
1827 * address space once.
1829 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1830 if (!listener
->log_sync
) {
1833 as
= listener
->address_space
;
1834 view
= address_space_get_flatview(as
);
1835 FOR_EACH_FLAT_RANGE(fr
, view
) {
1837 MemoryRegionSection mrs
= section_from_flat_range(fr
, as
);
1838 listener
->log_sync(listener
, &mrs
);
1841 flatview_unref(view
);
1845 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1847 if (mr
->readonly
!= readonly
) {
1848 memory_region_transaction_begin();
1849 mr
->readonly
= readonly
;
1850 memory_region_update_pending
|= mr
->enabled
;
1851 memory_region_transaction_commit();
1855 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1857 if (mr
->romd_mode
!= romd_mode
) {
1858 memory_region_transaction_begin();
1859 mr
->romd_mode
= romd_mode
;
1860 memory_region_update_pending
|= mr
->enabled
;
1861 memory_region_transaction_commit();
1865 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1866 hwaddr size
, unsigned client
)
1868 assert(mr
->ram_block
);
1869 cpu_physical_memory_test_and_clear_dirty(
1870 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1873 int memory_region_get_fd(MemoryRegion
*mr
)
1881 fd
= mr
->ram_block
->fd
;
1887 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1890 uint64_t offset
= 0;
1894 offset
+= mr
->alias_offset
;
1897 assert(mr
->ram_block
);
1898 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
1904 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
1908 block
= qemu_ram_block_from_host(ptr
, false, offset
);
1916 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1918 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
1921 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1923 assert(mr
->ram_block
);
1925 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
1928 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1932 CoalescedMemoryRange
*cmr
;
1934 MemoryRegionSection section
;
1936 view
= address_space_get_flatview(as
);
1937 FOR_EACH_FLAT_RANGE(fr
, view
) {
1939 section
= (MemoryRegionSection
) {
1940 .address_space
= as
,
1941 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1942 .size
= fr
->addr
.size
,
1945 MEMORY_LISTENER_CALL(as
, coalesced_mmio_del
, Reverse
, §ion
,
1946 int128_get64(fr
->addr
.start
),
1947 int128_get64(fr
->addr
.size
));
1948 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1949 tmp
= addrrange_shift(cmr
->addr
,
1950 int128_sub(fr
->addr
.start
,
1951 int128_make64(fr
->offset_in_region
)));
1952 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1955 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1956 MEMORY_LISTENER_CALL(as
, coalesced_mmio_add
, Forward
, §ion
,
1957 int128_get64(tmp
.start
),
1958 int128_get64(tmp
.size
));
1962 flatview_unref(view
);
1965 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1969 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1970 memory_region_update_coalesced_range_as(mr
, as
);
1974 void memory_region_set_coalescing(MemoryRegion
*mr
)
1976 memory_region_clear_coalescing(mr
);
1977 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1980 void memory_region_add_coalescing(MemoryRegion
*mr
,
1984 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1986 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1987 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1988 memory_region_update_coalesced_range(mr
);
1989 memory_region_set_flush_coalesced(mr
);
1992 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1994 CoalescedMemoryRange
*cmr
;
1995 bool updated
= false;
1997 qemu_flush_coalesced_mmio_buffer();
1998 mr
->flush_coalesced_mmio
= false;
2000 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2001 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2002 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2008 memory_region_update_coalesced_range(mr
);
2012 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2014 mr
->flush_coalesced_mmio
= true;
2017 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2019 qemu_flush_coalesced_mmio_buffer();
2020 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2021 mr
->flush_coalesced_mmio
= false;
2025 void memory_region_set_global_locking(MemoryRegion
*mr
)
2027 mr
->global_locking
= true;
2030 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2032 mr
->global_locking
= false;
2035 static bool userspace_eventfd_warning
;
2037 void memory_region_add_eventfd(MemoryRegion
*mr
,
2044 MemoryRegionIoeventfd mrfd
= {
2045 .addr
.start
= int128_make64(addr
),
2046 .addr
.size
= int128_make64(size
),
2047 .match_data
= match_data
,
2053 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2054 userspace_eventfd_warning
))) {
2055 userspace_eventfd_warning
= true;
2056 error_report("Using eventfd without MMIO binding in KVM. "
2057 "Suboptimal performance expected");
2061 adjust_endianness(mr
, &mrfd
.data
, size
);
2063 memory_region_transaction_begin();
2064 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2065 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
2070 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2071 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2072 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2073 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2074 mr
->ioeventfds
[i
] = mrfd
;
2075 ioeventfd_update_pending
|= mr
->enabled
;
2076 memory_region_transaction_commit();
2079 void memory_region_del_eventfd(MemoryRegion
*mr
,
2086 MemoryRegionIoeventfd mrfd
= {
2087 .addr
.start
= int128_make64(addr
),
2088 .addr
.size
= int128_make64(size
),
2089 .match_data
= match_data
,
2096 adjust_endianness(mr
, &mrfd
.data
, size
);
2098 memory_region_transaction_begin();
2099 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2100 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
2104 assert(i
!= mr
->ioeventfd_nb
);
2105 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2106 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2108 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2109 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2110 ioeventfd_update_pending
|= mr
->enabled
;
2111 memory_region_transaction_commit();
2114 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2116 MemoryRegion
*mr
= subregion
->container
;
2117 MemoryRegion
*other
;
2119 memory_region_transaction_begin();
2121 memory_region_ref(subregion
);
2122 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2123 if (subregion
->priority
>= other
->priority
) {
2124 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2128 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2130 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2131 memory_region_transaction_commit();
2134 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2136 MemoryRegion
*subregion
)
2138 assert(!subregion
->container
);
2139 subregion
->container
= mr
;
2140 subregion
->addr
= offset
;
2141 memory_region_update_container_subregions(subregion
);
2144 void memory_region_add_subregion(MemoryRegion
*mr
,
2146 MemoryRegion
*subregion
)
2148 subregion
->priority
= 0;
2149 memory_region_add_subregion_common(mr
, offset
, subregion
);
2152 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2154 MemoryRegion
*subregion
,
2157 subregion
->priority
= priority
;
2158 memory_region_add_subregion_common(mr
, offset
, subregion
);
2161 void memory_region_del_subregion(MemoryRegion
*mr
,
2162 MemoryRegion
*subregion
)
2164 memory_region_transaction_begin();
2165 assert(subregion
->container
== mr
);
2166 subregion
->container
= NULL
;
2167 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2168 memory_region_unref(subregion
);
2169 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2170 memory_region_transaction_commit();
2173 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2175 if (enabled
== mr
->enabled
) {
2178 memory_region_transaction_begin();
2179 mr
->enabled
= enabled
;
2180 memory_region_update_pending
= true;
2181 memory_region_transaction_commit();
2184 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2186 Int128 s
= int128_make64(size
);
2188 if (size
== UINT64_MAX
) {
2191 if (int128_eq(s
, mr
->size
)) {
2194 memory_region_transaction_begin();
2196 memory_region_update_pending
= true;
2197 memory_region_transaction_commit();
2200 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2202 MemoryRegion
*container
= mr
->container
;
2205 memory_region_transaction_begin();
2206 memory_region_ref(mr
);
2207 memory_region_del_subregion(container
, mr
);
2208 mr
->container
= container
;
2209 memory_region_update_container_subregions(mr
);
2210 memory_region_unref(mr
);
2211 memory_region_transaction_commit();
2215 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2217 if (addr
!= mr
->addr
) {
2219 memory_region_readd_subregion(mr
);
2223 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2227 if (offset
== mr
->alias_offset
) {
2231 memory_region_transaction_begin();
2232 mr
->alias_offset
= offset
;
2233 memory_region_update_pending
|= mr
->enabled
;
2234 memory_region_transaction_commit();
2237 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2242 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2244 const AddrRange
*addr
= addr_
;
2245 const FlatRange
*fr
= fr_
;
2247 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2249 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2255 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2257 return bsearch(&addr
, view
->ranges
, view
->nr
,
2258 sizeof(FlatRange
), cmp_flatrange_addr
);
2261 bool memory_region_is_mapped(MemoryRegion
*mr
)
2263 return mr
->container
? true : false;
2266 /* Same as memory_region_find, but it does not add a reference to the
2267 * returned region. It must be called from an RCU critical section.
2269 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2270 hwaddr addr
, uint64_t size
)
2272 MemoryRegionSection ret
= { .mr
= NULL
};
2280 for (root
= mr
; root
->container
; ) {
2281 root
= root
->container
;
2285 as
= memory_region_to_address_space(root
);
2289 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2291 view
= atomic_rcu_read(&as
->current_map
);
2292 fr
= flatview_lookup(view
, range
);
2297 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2302 ret
.address_space
= as
;
2303 range
= addrrange_intersection(range
, fr
->addr
);
2304 ret
.offset_within_region
= fr
->offset_in_region
;
2305 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2307 ret
.size
= range
.size
;
2308 ret
.offset_within_address_space
= int128_get64(range
.start
);
2309 ret
.readonly
= fr
->readonly
;
2313 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2314 hwaddr addr
, uint64_t size
)
2316 MemoryRegionSection ret
;
2318 ret
= memory_region_find_rcu(mr
, addr
, size
);
2320 memory_region_ref(ret
.mr
);
2326 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2331 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2333 return mr
&& mr
!= container
;
2336 void memory_global_dirty_log_sync(void)
2338 MemoryListener
*listener
;
2343 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2344 if (!listener
->log_sync
) {
2347 as
= listener
->address_space
;
2348 view
= address_space_get_flatview(as
);
2349 FOR_EACH_FLAT_RANGE(fr
, view
) {
2350 if (fr
->dirty_log_mask
) {
2351 MemoryRegionSection mrs
= section_from_flat_range(fr
, as
);
2352 listener
->log_sync(listener
, &mrs
);
2355 flatview_unref(view
);
2359 void memory_global_dirty_log_start(void)
2361 global_dirty_log
= true;
2363 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2365 /* Refresh DIRTY_LOG_MIGRATION bit. */
2366 memory_region_transaction_begin();
2367 memory_region_update_pending
= true;
2368 memory_region_transaction_commit();
2371 void memory_global_dirty_log_stop(void)
2373 global_dirty_log
= false;
2375 /* Refresh DIRTY_LOG_MIGRATION bit. */
2376 memory_region_transaction_begin();
2377 memory_region_update_pending
= true;
2378 memory_region_transaction_commit();
2380 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2383 static void listener_add_address_space(MemoryListener
*listener
,
2389 if (listener
->begin
) {
2390 listener
->begin(listener
);
2392 if (global_dirty_log
) {
2393 if (listener
->log_global_start
) {
2394 listener
->log_global_start(listener
);
2398 view
= address_space_get_flatview(as
);
2399 FOR_EACH_FLAT_RANGE(fr
, view
) {
2400 MemoryRegionSection section
= {
2402 .address_space
= as
,
2403 .offset_within_region
= fr
->offset_in_region
,
2404 .size
= fr
->addr
.size
,
2405 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2406 .readonly
= fr
->readonly
,
2408 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2409 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2411 if (listener
->region_add
) {
2412 listener
->region_add(listener
, §ion
);
2415 if (listener
->commit
) {
2416 listener
->commit(listener
);
2418 flatview_unref(view
);
2421 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2423 MemoryListener
*other
= NULL
;
2425 listener
->address_space
= as
;
2426 if (QTAILQ_EMPTY(&memory_listeners
)
2427 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2428 memory_listeners
)->priority
) {
2429 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2431 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2432 if (listener
->priority
< other
->priority
) {
2436 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2439 if (QTAILQ_EMPTY(&as
->listeners
)
2440 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
,
2441 memory_listeners
)->priority
) {
2442 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2444 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2445 if (listener
->priority
< other
->priority
) {
2449 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2452 listener_add_address_space(listener
, as
);
2455 void memory_listener_unregister(MemoryListener
*listener
)
2457 if (!listener
->address_space
) {
2461 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2462 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2463 listener
->address_space
= NULL
;
2466 bool memory_region_request_mmio_ptr(MemoryRegion
*mr
, hwaddr addr
)
2470 unsigned offset
= 0;
2471 Object
*new_interface
;
2473 if (!mr
|| !mr
->ops
->request_ptr
) {
2478 * Avoid an update if the request_ptr call
2479 * memory_region_invalidate_mmio_ptr which seems to be likely when we use
2482 memory_region_transaction_begin();
2484 host
= mr
->ops
->request_ptr(mr
->opaque
, addr
- mr
->addr
, &size
, &offset
);
2486 if (!host
|| !size
) {
2487 memory_region_transaction_commit();
2491 new_interface
= object_new("mmio_interface");
2492 qdev_prop_set_uint64(DEVICE(new_interface
), "start", offset
);
2493 qdev_prop_set_uint64(DEVICE(new_interface
), "end", offset
+ size
- 1);
2494 qdev_prop_set_bit(DEVICE(new_interface
), "ro", true);
2495 qdev_prop_set_ptr(DEVICE(new_interface
), "host_ptr", host
);
2496 qdev_prop_set_ptr(DEVICE(new_interface
), "subregion", mr
);
2497 object_property_set_bool(OBJECT(new_interface
), true, "realized", NULL
);
2499 memory_region_transaction_commit();
2503 typedef struct MMIOPtrInvalidate
{
2509 } MMIOPtrInvalidate
;
2511 #define MAX_MMIO_INVALIDATE 10
2512 static MMIOPtrInvalidate mmio_ptr_invalidate_list
[MAX_MMIO_INVALIDATE
];
2514 static void memory_region_do_invalidate_mmio_ptr(CPUState
*cpu
,
2515 run_on_cpu_data data
)
2517 MMIOPtrInvalidate
*invalidate_data
= (MMIOPtrInvalidate
*)data
.host_ptr
;
2518 MemoryRegion
*mr
= invalidate_data
->mr
;
2519 hwaddr offset
= invalidate_data
->offset
;
2520 unsigned size
= invalidate_data
->size
;
2521 MemoryRegionSection section
= memory_region_find(mr
, offset
, size
);
2523 qemu_mutex_lock_iothread();
2525 /* Reset dirty so this doesn't happen later. */
2526 cpu_physical_memory_test_and_clear_dirty(offset
, size
, 1);
2528 if (section
.mr
!= mr
) {
2529 /* memory_region_find add a ref on section.mr */
2530 memory_region_unref(section
.mr
);
2531 if (MMIO_INTERFACE(section
.mr
->owner
)) {
2532 /* We found the interface just drop it. */
2533 object_property_set_bool(section
.mr
->owner
, false, "realized",
2535 object_unref(section
.mr
->owner
);
2536 object_unparent(section
.mr
->owner
);
2540 qemu_mutex_unlock_iothread();
2542 if (invalidate_data
->allocated
) {
2543 g_free(invalidate_data
);
2545 invalidate_data
->busy
= 0;
2549 void memory_region_invalidate_mmio_ptr(MemoryRegion
*mr
, hwaddr offset
,
2553 MMIOPtrInvalidate
*invalidate_data
= NULL
;
2555 for (i
= 0; i
< MAX_MMIO_INVALIDATE
; i
++) {
2556 if (atomic_cmpxchg(&(mmio_ptr_invalidate_list
[i
].busy
), 0, 1) == 0) {
2557 invalidate_data
= &mmio_ptr_invalidate_list
[i
];
2562 if (!invalidate_data
) {
2563 invalidate_data
= g_malloc0(sizeof(MMIOPtrInvalidate
));
2564 invalidate_data
->allocated
= 1;
2567 invalidate_data
->mr
= mr
;
2568 invalidate_data
->offset
= offset
;
2569 invalidate_data
->size
= size
;
2571 async_safe_run_on_cpu(first_cpu
, memory_region_do_invalidate_mmio_ptr
,
2572 RUN_ON_CPU_HOST_PTR(invalidate_data
));
2575 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2577 memory_region_ref(root
);
2578 memory_region_transaction_begin();
2581 as
->malloced
= false;
2582 as
->current_map
= g_new(FlatView
, 1);
2583 flatview_init(as
->current_map
);
2584 as
->ioeventfd_nb
= 0;
2585 as
->ioeventfds
= NULL
;
2586 QTAILQ_INIT(&as
->listeners
);
2587 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2588 as
->name
= g_strdup(name
? name
: "anonymous");
2589 address_space_init_dispatch(as
);
2590 memory_region_update_pending
|= root
->enabled
;
2591 memory_region_transaction_commit();
2594 static void do_address_space_destroy(AddressSpace
*as
)
2596 bool do_free
= as
->malloced
;
2598 address_space_destroy_dispatch(as
);
2599 assert(QTAILQ_EMPTY(&as
->listeners
));
2601 flatview_unref(as
->current_map
);
2603 g_free(as
->ioeventfds
);
2604 memory_region_unref(as
->root
);
2610 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
, const char *name
)
2614 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2615 if (root
== as
->root
&& as
->malloced
) {
2621 as
= g_malloc0(sizeof *as
);
2622 address_space_init(as
, root
, name
);
2623 as
->malloced
= true;
2627 void address_space_destroy(AddressSpace
*as
)
2629 MemoryRegion
*root
= as
->root
;
2632 if (as
->ref_count
) {
2635 /* Flush out anything from MemoryListeners listening in on this */
2636 memory_region_transaction_begin();
2638 memory_region_transaction_commit();
2639 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2640 address_space_unregister(as
);
2642 /* At this point, as->dispatch and as->current_map are dummy
2643 * entries that the guest should never use. Wait for the old
2644 * values to expire before freeing the data.
2647 call_rcu(as
, do_address_space_destroy
, rcu
);
2650 static const char *memory_region_type(MemoryRegion
*mr
)
2652 if (memory_region_is_ram_device(mr
)) {
2654 } else if (memory_region_is_romd(mr
)) {
2656 } else if (memory_region_is_rom(mr
)) {
2658 } else if (memory_region_is_ram(mr
)) {
2665 typedef struct MemoryRegionList MemoryRegionList
;
2667 struct MemoryRegionList
{
2668 const MemoryRegion
*mr
;
2669 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2672 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2674 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2675 int128_sub((size), int128_one())) : 0)
2676 #define MTREE_INDENT " "
2678 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2679 const MemoryRegion
*mr
, unsigned int level
,
2681 MemoryRegionListHead
*alias_print_queue
)
2683 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2684 MemoryRegionListHead submr_print_queue
;
2685 const MemoryRegion
*submr
;
2687 hwaddr cur_start
, cur_end
;
2693 for (i
= 0; i
< level
; i
++) {
2694 mon_printf(f
, MTREE_INDENT
);
2697 cur_start
= base
+ mr
->addr
;
2698 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2701 * Try to detect overflow of memory region. This should never
2702 * happen normally. When it happens, we dump something to warn the
2703 * user who is observing this.
2705 if (cur_start
< base
|| cur_end
< cur_start
) {
2706 mon_printf(f
, "[DETECTED OVERFLOW!] ");
2710 MemoryRegionList
*ml
;
2713 /* check if the alias is already in the queue */
2714 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2715 if (ml
->mr
== mr
->alias
) {
2721 ml
= g_new(MemoryRegionList
, 1);
2723 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2725 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2726 " (prio %d, %s): alias %s @%s " TARGET_FMT_plx
2727 "-" TARGET_FMT_plx
"%s\n",
2730 memory_region_type((MemoryRegion
*)mr
),
2731 memory_region_name(mr
),
2732 memory_region_name(mr
->alias
),
2734 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2735 mr
->enabled
? "" : " [disabled]");
2738 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %s): %s%s\n",
2741 memory_region_type((MemoryRegion
*)mr
),
2742 memory_region_name(mr
),
2743 mr
->enabled
? "" : " [disabled]");
2746 QTAILQ_INIT(&submr_print_queue
);
2748 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2749 new_ml
= g_new(MemoryRegionList
, 1);
2751 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2752 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2753 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2754 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2755 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2761 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2765 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2766 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, cur_start
,
2770 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2775 static void mtree_print_flatview(fprintf_function p
, void *f
,
2778 FlatView
*view
= address_space_get_flatview(as
);
2779 FlatRange
*range
= &view
->ranges
[0];
2784 p(f
, MTREE_INDENT
"No rendered FlatView for "
2785 "address space '%s'\n", as
->name
);
2786 flatview_unref(view
);
2792 if (range
->offset_in_region
) {
2793 p(f
, MTREE_INDENT TARGET_FMT_plx
"-"
2794 TARGET_FMT_plx
" (prio %d, %s): %s @" TARGET_FMT_plx
"\n",
2795 int128_get64(range
->addr
.start
),
2796 int128_get64(range
->addr
.start
) + MR_SIZE(range
->addr
.size
),
2798 range
->readonly
? "rom" : memory_region_type(mr
),
2799 memory_region_name(mr
),
2800 range
->offset_in_region
);
2802 p(f
, MTREE_INDENT TARGET_FMT_plx
"-"
2803 TARGET_FMT_plx
" (prio %d, %s): %s\n",
2804 int128_get64(range
->addr
.start
),
2805 int128_get64(range
->addr
.start
) + MR_SIZE(range
->addr
.size
),
2807 range
->readonly
? "rom" : memory_region_type(mr
),
2808 memory_region_name(mr
));
2813 flatview_unref(view
);
2816 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
)
2818 MemoryRegionListHead ml_head
;
2819 MemoryRegionList
*ml
, *ml2
;
2823 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2824 mon_printf(f
, "address-space (flat view): %s\n", as
->name
);
2825 mtree_print_flatview(mon_printf
, f
, as
);
2826 mon_printf(f
, "\n");
2831 QTAILQ_INIT(&ml_head
);
2833 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2834 mon_printf(f
, "address-space: %s\n", as
->name
);
2835 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2836 mon_printf(f
, "\n");
2839 /* print aliased regions */
2840 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2841 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2842 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2843 mon_printf(f
, "\n");
2846 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2851 static const TypeInfo memory_region_info
= {
2852 .parent
= TYPE_OBJECT
,
2853 .name
= TYPE_MEMORY_REGION
,
2854 .instance_size
= sizeof(MemoryRegion
),
2855 .instance_init
= memory_region_initfn
,
2856 .instance_finalize
= memory_region_finalize
,
2859 static const TypeInfo iommu_memory_region_info
= {
2860 .parent
= TYPE_MEMORY_REGION
,
2861 .name
= TYPE_IOMMU_MEMORY_REGION
,
2862 .class_size
= sizeof(IOMMUMemoryRegionClass
),
2863 .instance_size
= sizeof(IOMMUMemoryRegion
),
2864 .instance_init
= iommu_memory_region_initfn
,
2868 static void memory_register_types(void)
2870 type_register_static(&memory_region_info
);
2871 type_register_static(&iommu_memory_region_info
);
2874 type_init(memory_register_types
)