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"
29 #include "exec/memory-internal.h"
30 #include "exec/ram_addr.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/sysemu.h"
34 //#define DEBUG_UNASSIGNED
36 static unsigned memory_region_transaction_depth
;
37 static bool memory_region_update_pending
;
38 static bool ioeventfd_update_pending
;
39 static bool global_dirty_log
= false;
41 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
42 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
44 static QTAILQ_HEAD(, AddressSpace
) address_spaces
45 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
47 typedef struct AddrRange AddrRange
;
50 * Note that signed integers are needed for negative offsetting in aliases
51 * (large MemoryRegion::alias_offset).
58 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
60 return (AddrRange
) { start
, size
};
63 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
65 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
68 static Int128
addrrange_end(AddrRange r
)
70 return int128_add(r
.start
, r
.size
);
73 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
75 int128_addto(&range
.start
, delta
);
79 static bool addrrange_contains(AddrRange range
, Int128 addr
)
81 return int128_ge(addr
, range
.start
)
82 && int128_lt(addr
, addrrange_end(range
));
85 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
87 return addrrange_contains(r1
, r2
.start
)
88 || addrrange_contains(r2
, r1
.start
);
91 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
93 Int128 start
= int128_max(r1
.start
, r2
.start
);
94 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
95 return addrrange_make(start
, int128_sub(end
, start
));
98 enum ListenerDirection
{ Forward
, Reverse
};
100 static bool memory_listener_match(MemoryListener
*listener
,
101 MemoryRegionSection
*section
)
103 return !listener
->address_space_filter
104 || listener
->address_space_filter
== section
->address_space
;
107 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
109 MemoryListener *_listener; \
111 switch (_direction) { \
113 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
114 if (_listener->_callback) { \
115 _listener->_callback(_listener, ##_args); \
120 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
121 memory_listeners, link) { \
122 if (_listener->_callback) { \
123 _listener->_callback(_listener, ##_args); \
132 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
134 MemoryListener *_listener; \
136 switch (_direction) { \
138 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
139 if (_listener->_callback \
140 && memory_listener_match(_listener, _section)) { \
141 _listener->_callback(_listener, _section, ##_args); \
146 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
147 memory_listeners, link) { \
148 if (_listener->_callback \
149 && memory_listener_match(_listener, _section)) { \
150 _listener->_callback(_listener, _section, ##_args); \
159 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
160 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
161 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
163 .address_space = (as), \
164 .offset_within_region = (fr)->offset_in_region, \
165 .size = (fr)->addr.size, \
166 .offset_within_address_space = int128_get64((fr)->addr.start), \
167 .readonly = (fr)->readonly, \
170 struct CoalescedMemoryRange
{
172 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
175 struct MemoryRegionIoeventfd
{
182 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
183 MemoryRegionIoeventfd b
)
185 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
187 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
189 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
191 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
193 } else if (a
.match_data
< b
.match_data
) {
195 } else if (a
.match_data
> b
.match_data
) {
197 } else if (a
.match_data
) {
198 if (a
.data
< b
.data
) {
200 } else if (a
.data
> b
.data
) {
206 } else if (a
.e
> b
.e
) {
212 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
213 MemoryRegionIoeventfd b
)
215 return !memory_region_ioeventfd_before(a
, b
)
216 && !memory_region_ioeventfd_before(b
, a
);
219 typedef struct FlatRange FlatRange
;
220 typedef struct FlatView FlatView
;
222 /* Range of memory in the global map. Addresses are absolute. */
225 hwaddr offset_in_region
;
227 uint8_t dirty_log_mask
;
232 /* Flattened global view of current active memory hierarchy. Kept in sorted
240 unsigned nr_allocated
;
243 typedef struct AddressSpaceOps AddressSpaceOps
;
245 #define FOR_EACH_FLAT_RANGE(var, view) \
246 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
248 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
250 return a
->mr
== b
->mr
251 && addrrange_equal(a
->addr
, b
->addr
)
252 && a
->offset_in_region
== b
->offset_in_region
253 && a
->romd_mode
== b
->romd_mode
254 && a
->readonly
== b
->readonly
;
257 static void flatview_init(FlatView
*view
)
262 view
->nr_allocated
= 0;
265 /* Insert a range into a given position. Caller is responsible for maintaining
268 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
270 if (view
->nr
== view
->nr_allocated
) {
271 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
272 view
->ranges
= g_realloc(view
->ranges
,
273 view
->nr_allocated
* sizeof(*view
->ranges
));
275 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
276 (view
->nr
- pos
) * sizeof(FlatRange
));
277 view
->ranges
[pos
] = *range
;
278 memory_region_ref(range
->mr
);
282 static void flatview_destroy(FlatView
*view
)
286 for (i
= 0; i
< view
->nr
; i
++) {
287 memory_region_unref(view
->ranges
[i
].mr
);
289 g_free(view
->ranges
);
293 static void flatview_ref(FlatView
*view
)
295 atomic_inc(&view
->ref
);
298 static void flatview_unref(FlatView
*view
)
300 if (atomic_fetch_dec(&view
->ref
) == 1) {
301 flatview_destroy(view
);
305 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
307 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
309 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
311 int128_make64(r2
->offset_in_region
))
312 && r1
->dirty_log_mask
== r2
->dirty_log_mask
313 && r1
->romd_mode
== r2
->romd_mode
314 && r1
->readonly
== r2
->readonly
;
317 /* Attempt to simplify a view by merging adjacent ranges */
318 static void flatview_simplify(FlatView
*view
)
323 while (i
< view
->nr
) {
326 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
327 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
331 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
332 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
337 static bool memory_region_big_endian(MemoryRegion
*mr
)
339 #ifdef TARGET_WORDS_BIGENDIAN
340 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
342 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
346 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
348 #ifdef TARGET_WORDS_BIGENDIAN
349 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
351 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
355 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
357 if (memory_region_wrong_endianness(mr
)) {
362 *data
= bswap16(*data
);
365 *data
= bswap32(*data
);
368 *data
= bswap64(*data
);
376 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
379 hwaddr abs_addr
= offset
;
381 abs_addr
+= mr
->addr
;
382 for (root
= mr
; root
->container
; ) {
383 root
= root
->container
;
384 abs_addr
+= root
->addr
;
390 static int get_cpu_index(void)
393 return current_cpu
->cpu_index
;
398 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
408 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
410 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
411 } else if (mr
== &io_mem_notdirty
) {
412 /* Accesses to code which has previously been translated into a TB show
413 * up in the MMIO path, as accesses to the io_mem_notdirty
415 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
416 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
417 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
418 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
420 *value
|= (tmp
& mask
) << shift
;
424 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
434 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
436 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
437 } else if (mr
== &io_mem_notdirty
) {
438 /* Accesses to code which has previously been translated into a TB show
439 * up in the MMIO path, as accesses to the io_mem_notdirty
441 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
442 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
443 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
444 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
446 *value
|= (tmp
& mask
) << shift
;
450 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
461 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
463 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
464 } else if (mr
== &io_mem_notdirty
) {
465 /* Accesses to code which has previously been translated into a TB show
466 * up in the MMIO path, as accesses to the io_mem_notdirty
468 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
469 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
470 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
471 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
473 *value
|= (tmp
& mask
) << shift
;
477 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
487 tmp
= (*value
>> shift
) & mask
;
489 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
490 } else if (mr
== &io_mem_notdirty
) {
491 /* Accesses to code which has previously been translated into a TB show
492 * up in the MMIO path, as accesses to the io_mem_notdirty
494 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
495 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
496 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
497 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
499 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
503 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
513 tmp
= (*value
>> shift
) & mask
;
515 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
516 } else if (mr
== &io_mem_notdirty
) {
517 /* Accesses to code which has previously been translated into a TB show
518 * up in the MMIO path, as accesses to the io_mem_notdirty
520 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
521 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
522 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
523 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
525 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
529 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
539 tmp
= (*value
>> shift
) & mask
;
541 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
542 } else if (mr
== &io_mem_notdirty
) {
543 /* Accesses to code which has previously been translated into a TB show
544 * up in the MMIO path, as accesses to the io_mem_notdirty
546 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
547 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
548 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
549 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
551 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
554 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
557 unsigned access_size_min
,
558 unsigned access_size_max
,
559 MemTxResult (*access
)(MemoryRegion
*mr
,
569 uint64_t access_mask
;
570 unsigned access_size
;
572 MemTxResult r
= MEMTX_OK
;
574 if (!access_size_min
) {
577 if (!access_size_max
) {
581 /* FIXME: support unaligned access? */
582 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
583 access_mask
= -1ULL >> (64 - access_size
* 8);
584 if (memory_region_big_endian(mr
)) {
585 for (i
= 0; i
< size
; i
+= access_size
) {
586 r
|= access(mr
, addr
+ i
, value
, access_size
,
587 (size
- access_size
- i
) * 8, access_mask
, attrs
);
590 for (i
= 0; i
< size
; i
+= access_size
) {
591 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
598 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
602 while (mr
->container
) {
605 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
606 if (mr
== as
->root
) {
613 /* Render a memory region into the global view. Ranges in @view obscure
616 static void render_memory_region(FlatView
*view
,
622 MemoryRegion
*subregion
;
624 hwaddr offset_in_region
;
634 int128_addto(&base
, int128_make64(mr
->addr
));
635 readonly
|= mr
->readonly
;
637 tmp
= addrrange_make(base
, mr
->size
);
639 if (!addrrange_intersects(tmp
, clip
)) {
643 clip
= addrrange_intersection(tmp
, clip
);
646 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
647 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
648 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
652 /* Render subregions in priority order. */
653 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
654 render_memory_region(view
, subregion
, base
, clip
, readonly
);
657 if (!mr
->terminates
) {
661 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
666 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
667 fr
.romd_mode
= mr
->romd_mode
;
668 fr
.readonly
= readonly
;
670 /* Render the region itself into any gaps left by the current view. */
671 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
672 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
675 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
676 now
= int128_min(remain
,
677 int128_sub(view
->ranges
[i
].addr
.start
, base
));
678 fr
.offset_in_region
= offset_in_region
;
679 fr
.addr
= addrrange_make(base
, now
);
680 flatview_insert(view
, i
, &fr
);
682 int128_addto(&base
, now
);
683 offset_in_region
+= int128_get64(now
);
684 int128_subfrom(&remain
, now
);
686 now
= int128_sub(int128_min(int128_add(base
, remain
),
687 addrrange_end(view
->ranges
[i
].addr
)),
689 int128_addto(&base
, now
);
690 offset_in_region
+= int128_get64(now
);
691 int128_subfrom(&remain
, now
);
693 if (int128_nz(remain
)) {
694 fr
.offset_in_region
= offset_in_region
;
695 fr
.addr
= addrrange_make(base
, remain
);
696 flatview_insert(view
, i
, &fr
);
700 /* Render a memory topology into a list of disjoint absolute ranges. */
701 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
705 view
= g_new(FlatView
, 1);
709 render_memory_region(view
, mr
, int128_zero(),
710 addrrange_make(int128_zero(), int128_2_64()), false);
712 flatview_simplify(view
);
717 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
718 MemoryRegionIoeventfd
*fds_new
,
720 MemoryRegionIoeventfd
*fds_old
,
724 MemoryRegionIoeventfd
*fd
;
725 MemoryRegionSection section
;
727 /* Generate a symmetric difference of the old and new fd sets, adding
728 * and deleting as necessary.
732 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
733 if (iold
< fds_old_nb
734 && (inew
== fds_new_nb
735 || memory_region_ioeventfd_before(fds_old
[iold
],
738 section
= (MemoryRegionSection
) {
740 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
741 .size
= fd
->addr
.size
,
743 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
744 fd
->match_data
, fd
->data
, fd
->e
);
746 } else if (inew
< fds_new_nb
747 && (iold
== fds_old_nb
748 || memory_region_ioeventfd_before(fds_new
[inew
],
751 section
= (MemoryRegionSection
) {
753 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
754 .size
= fd
->addr
.size
,
756 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
757 fd
->match_data
, fd
->data
, fd
->e
);
766 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
771 view
= atomic_rcu_read(&as
->current_map
);
777 static void address_space_update_ioeventfds(AddressSpace
*as
)
781 unsigned ioeventfd_nb
= 0;
782 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
786 view
= address_space_get_flatview(as
);
787 FOR_EACH_FLAT_RANGE(fr
, view
) {
788 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
789 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
790 int128_sub(fr
->addr
.start
,
791 int128_make64(fr
->offset_in_region
)));
792 if (addrrange_intersects(fr
->addr
, tmp
)) {
794 ioeventfds
= g_realloc(ioeventfds
,
795 ioeventfd_nb
* sizeof(*ioeventfds
));
796 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
797 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
802 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
803 as
->ioeventfds
, as
->ioeventfd_nb
);
805 g_free(as
->ioeventfds
);
806 as
->ioeventfds
= ioeventfds
;
807 as
->ioeventfd_nb
= ioeventfd_nb
;
808 flatview_unref(view
);
811 static void address_space_update_topology_pass(AddressSpace
*as
,
812 const FlatView
*old_view
,
813 const FlatView
*new_view
,
817 FlatRange
*frold
, *frnew
;
819 /* Generate a symmetric difference of the old and new memory maps.
820 * Kill ranges in the old map, and instantiate ranges in the new map.
823 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
824 if (iold
< old_view
->nr
) {
825 frold
= &old_view
->ranges
[iold
];
829 if (inew
< new_view
->nr
) {
830 frnew
= &new_view
->ranges
[inew
];
837 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
838 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
839 && !flatrange_equal(frold
, frnew
)))) {
840 /* In old but not in new, or in both but attributes changed. */
843 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
847 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
848 /* In both and unchanged (except logging may have changed) */
851 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
852 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
853 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
854 frold
->dirty_log_mask
,
855 frnew
->dirty_log_mask
);
857 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
858 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
859 frold
->dirty_log_mask
,
860 frnew
->dirty_log_mask
);
870 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
879 static void address_space_update_topology(AddressSpace
*as
)
881 FlatView
*old_view
= address_space_get_flatview(as
);
882 FlatView
*new_view
= generate_memory_topology(as
->root
);
884 address_space_update_topology_pass(as
, old_view
, new_view
, false);
885 address_space_update_topology_pass(as
, old_view
, new_view
, true);
887 /* Writes are protected by the BQL. */
888 atomic_rcu_set(&as
->current_map
, new_view
);
889 call_rcu(old_view
, flatview_unref
, rcu
);
891 /* Note that all the old MemoryRegions are still alive up to this
892 * point. This relieves most MemoryListeners from the need to
893 * ref/unref the MemoryRegions they get---unless they use them
894 * outside the iothread mutex, in which case precise reference
895 * counting is necessary.
897 flatview_unref(old_view
);
899 address_space_update_ioeventfds(as
);
902 void memory_region_transaction_begin(void)
904 qemu_flush_coalesced_mmio_buffer();
905 ++memory_region_transaction_depth
;
908 static void memory_region_clear_pending(void)
910 memory_region_update_pending
= false;
911 ioeventfd_update_pending
= false;
914 void memory_region_transaction_commit(void)
918 assert(memory_region_transaction_depth
);
919 --memory_region_transaction_depth
;
920 if (!memory_region_transaction_depth
) {
921 if (memory_region_update_pending
) {
922 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
924 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
925 address_space_update_topology(as
);
928 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
929 } else if (ioeventfd_update_pending
) {
930 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
931 address_space_update_ioeventfds(as
);
934 memory_region_clear_pending();
938 static void memory_region_destructor_none(MemoryRegion
*mr
)
942 static void memory_region_destructor_ram(MemoryRegion
*mr
)
944 qemu_ram_free(mr
->ram_block
);
947 static bool memory_region_need_escape(char c
)
949 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
952 static char *memory_region_escape_name(const char *name
)
959 for (p
= name
; *p
; p
++) {
960 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
962 if (bytes
== p
- name
) {
963 return g_memdup(name
, bytes
+ 1);
966 escaped
= g_malloc(bytes
+ 1);
967 for (p
= name
, q
= escaped
; *p
; p
++) {
969 if (unlikely(memory_region_need_escape(c
))) {
972 *q
++ = "0123456789abcdef"[c
>> 4];
973 c
= "0123456789abcdef"[c
& 15];
981 void memory_region_init(MemoryRegion
*mr
,
986 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
987 mr
->size
= int128_make64(size
);
988 if (size
== UINT64_MAX
) {
989 mr
->size
= int128_2_64();
991 mr
->name
= g_strdup(name
);
993 mr
->ram_block
= NULL
;
996 char *escaped_name
= memory_region_escape_name(name
);
997 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1000 owner
= container_get(qdev_get_machine(), "/unattached");
1003 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1004 object_unref(OBJECT(mr
));
1006 g_free(escaped_name
);
1010 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1011 void *opaque
, Error
**errp
)
1013 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1014 uint64_t value
= mr
->addr
;
1016 visit_type_uint64(v
, name
, &value
, errp
);
1019 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1020 const char *name
, void *opaque
,
1023 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1024 gchar
*path
= (gchar
*)"";
1026 if (mr
->container
) {
1027 path
= object_get_canonical_path(OBJECT(mr
->container
));
1029 visit_type_str(v
, name
, &path
, errp
);
1030 if (mr
->container
) {
1035 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1038 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1040 return OBJECT(mr
->container
);
1043 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1044 const char *name
, void *opaque
,
1047 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1048 int32_t value
= mr
->priority
;
1050 visit_type_int32(v
, name
, &value
, errp
);
1053 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1054 void *opaque
, Error
**errp
)
1056 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1057 uint64_t value
= memory_region_size(mr
);
1059 visit_type_uint64(v
, name
, &value
, errp
);
1062 static void memory_region_initfn(Object
*obj
)
1064 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1067 mr
->ops
= &unassigned_mem_ops
;
1069 mr
->romd_mode
= true;
1070 mr
->global_locking
= true;
1071 mr
->destructor
= memory_region_destructor_none
;
1072 QTAILQ_INIT(&mr
->subregions
);
1073 QTAILQ_INIT(&mr
->coalesced
);
1075 op
= object_property_add(OBJECT(mr
), "container",
1076 "link<" TYPE_MEMORY_REGION
">",
1077 memory_region_get_container
,
1078 NULL
, /* memory_region_set_container */
1079 NULL
, NULL
, &error_abort
);
1080 op
->resolve
= memory_region_resolve_container
;
1082 object_property_add(OBJECT(mr
), "addr", "uint64",
1083 memory_region_get_addr
,
1084 NULL
, /* memory_region_set_addr */
1085 NULL
, NULL
, &error_abort
);
1086 object_property_add(OBJECT(mr
), "priority", "uint32",
1087 memory_region_get_priority
,
1088 NULL
, /* memory_region_set_priority */
1089 NULL
, NULL
, &error_abort
);
1090 object_property_add(OBJECT(mr
), "size", "uint64",
1091 memory_region_get_size
,
1092 NULL
, /* memory_region_set_size, */
1093 NULL
, NULL
, &error_abort
);
1096 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1099 #ifdef DEBUG_UNASSIGNED
1100 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1102 if (current_cpu
!= NULL
) {
1103 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1108 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1109 uint64_t val
, unsigned size
)
1111 #ifdef DEBUG_UNASSIGNED
1112 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1114 if (current_cpu
!= NULL
) {
1115 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1119 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1120 unsigned size
, bool is_write
)
1125 const MemoryRegionOps unassigned_mem_ops
= {
1126 .valid
.accepts
= unassigned_mem_accepts
,
1127 .endianness
= DEVICE_NATIVE_ENDIAN
,
1130 bool memory_region_access_valid(MemoryRegion
*mr
,
1135 int access_size_min
, access_size_max
;
1138 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1142 if (!mr
->ops
->valid
.accepts
) {
1146 access_size_min
= mr
->ops
->valid
.min_access_size
;
1147 if (!mr
->ops
->valid
.min_access_size
) {
1148 access_size_min
= 1;
1151 access_size_max
= mr
->ops
->valid
.max_access_size
;
1152 if (!mr
->ops
->valid
.max_access_size
) {
1153 access_size_max
= 4;
1156 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1157 for (i
= 0; i
< size
; i
+= access_size
) {
1158 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1167 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1175 if (mr
->ops
->read
) {
1176 return access_with_adjusted_size(addr
, pval
, size
,
1177 mr
->ops
->impl
.min_access_size
,
1178 mr
->ops
->impl
.max_access_size
,
1179 memory_region_read_accessor
,
1181 } else if (mr
->ops
->read_with_attrs
) {
1182 return access_with_adjusted_size(addr
, pval
, size
,
1183 mr
->ops
->impl
.min_access_size
,
1184 mr
->ops
->impl
.max_access_size
,
1185 memory_region_read_with_attrs_accessor
,
1188 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1189 memory_region_oldmmio_read_accessor
,
1194 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1202 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1203 *pval
= unassigned_mem_read(mr
, addr
, size
);
1204 return MEMTX_DECODE_ERROR
;
1207 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1208 adjust_endianness(mr
, pval
, size
);
1212 /* Return true if an eventfd was signalled */
1213 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1219 MemoryRegionIoeventfd ioeventfd
= {
1220 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1225 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1226 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1227 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1229 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1230 event_notifier_set(ioeventfd
.e
);
1238 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1244 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1245 unassigned_mem_write(mr
, addr
, data
, size
);
1246 return MEMTX_DECODE_ERROR
;
1249 adjust_endianness(mr
, &data
, size
);
1251 if ((!kvm_eventfds_enabled()) &&
1252 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1256 if (mr
->ops
->write
) {
1257 return access_with_adjusted_size(addr
, &data
, size
,
1258 mr
->ops
->impl
.min_access_size
,
1259 mr
->ops
->impl
.max_access_size
,
1260 memory_region_write_accessor
, mr
,
1262 } else if (mr
->ops
->write_with_attrs
) {
1264 access_with_adjusted_size(addr
, &data
, size
,
1265 mr
->ops
->impl
.min_access_size
,
1266 mr
->ops
->impl
.max_access_size
,
1267 memory_region_write_with_attrs_accessor
,
1270 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1271 memory_region_oldmmio_write_accessor
,
1276 void memory_region_init_io(MemoryRegion
*mr
,
1278 const MemoryRegionOps
*ops
,
1283 memory_region_init(mr
, owner
, name
, size
);
1284 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1285 mr
->opaque
= opaque
;
1286 mr
->terminates
= true;
1289 void memory_region_init_ram(MemoryRegion
*mr
,
1295 memory_region_init(mr
, owner
, name
, size
);
1297 mr
->terminates
= true;
1298 mr
->destructor
= memory_region_destructor_ram
;
1299 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1300 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1303 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1308 void (*resized
)(const char*,
1313 memory_region_init(mr
, owner
, name
, size
);
1315 mr
->terminates
= true;
1316 mr
->destructor
= memory_region_destructor_ram
;
1317 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1319 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1323 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1324 struct Object
*owner
,
1331 memory_region_init(mr
, owner
, name
, size
);
1333 mr
->terminates
= true;
1334 mr
->destructor
= memory_region_destructor_ram
;
1335 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1336 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1340 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1346 memory_region_init(mr
, owner
, name
, size
);
1348 mr
->terminates
= true;
1349 mr
->destructor
= memory_region_destructor_ram
;
1350 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1352 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1353 assert(ptr
!= NULL
);
1354 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1357 void memory_region_set_skip_dump(MemoryRegion
*mr
)
1359 mr
->skip_dump
= true;
1362 void memory_region_init_alias(MemoryRegion
*mr
,
1369 memory_region_init(mr
, owner
, name
, size
);
1371 mr
->alias_offset
= offset
;
1374 void memory_region_init_rom(MemoryRegion
*mr
,
1375 struct Object
*owner
,
1380 memory_region_init(mr
, owner
, name
, size
);
1382 mr
->readonly
= true;
1383 mr
->terminates
= true;
1384 mr
->destructor
= memory_region_destructor_ram
;
1385 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1386 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1389 void memory_region_init_rom_device(MemoryRegion
*mr
,
1391 const MemoryRegionOps
*ops
,
1398 memory_region_init(mr
, owner
, name
, size
);
1400 mr
->opaque
= opaque
;
1401 mr
->terminates
= true;
1402 mr
->rom_device
= true;
1403 mr
->destructor
= memory_region_destructor_ram
;
1404 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1407 void memory_region_init_iommu(MemoryRegion
*mr
,
1409 const MemoryRegionIOMMUOps
*ops
,
1413 memory_region_init(mr
, owner
, name
, size
);
1414 mr
->iommu_ops
= ops
,
1415 mr
->terminates
= true; /* then re-forwards */
1416 notifier_list_init(&mr
->iommu_notify
);
1419 static void memory_region_finalize(Object
*obj
)
1421 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1423 assert(!mr
->container
);
1425 /* We know the region is not visible in any address space (it
1426 * does not have a container and cannot be a root either because
1427 * it has no references, so we can blindly clear mr->enabled.
1428 * memory_region_set_enabled instead could trigger a transaction
1429 * and cause an infinite loop.
1431 mr
->enabled
= false;
1432 memory_region_transaction_begin();
1433 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1434 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1435 memory_region_del_subregion(mr
, subregion
);
1437 memory_region_transaction_commit();
1440 memory_region_clear_coalescing(mr
);
1441 g_free((char *)mr
->name
);
1442 g_free(mr
->ioeventfds
);
1445 Object
*memory_region_owner(MemoryRegion
*mr
)
1447 Object
*obj
= OBJECT(mr
);
1451 void memory_region_ref(MemoryRegion
*mr
)
1453 /* MMIO callbacks most likely will access data that belongs
1454 * to the owner, hence the need to ref/unref the owner whenever
1455 * the memory region is in use.
1457 * The memory region is a child of its owner. As long as the
1458 * owner doesn't call unparent itself on the memory region,
1459 * ref-ing the owner will also keep the memory region alive.
1460 * Memory regions without an owner are supposed to never go away;
1461 * we do not ref/unref them because it slows down DMA sensibly.
1463 if (mr
&& mr
->owner
) {
1464 object_ref(mr
->owner
);
1468 void memory_region_unref(MemoryRegion
*mr
)
1470 if (mr
&& mr
->owner
) {
1471 object_unref(mr
->owner
);
1475 uint64_t memory_region_size(MemoryRegion
*mr
)
1477 if (int128_eq(mr
->size
, int128_2_64())) {
1480 return int128_get64(mr
->size
);
1483 const char *memory_region_name(const MemoryRegion
*mr
)
1486 ((MemoryRegion
*)mr
)->name
=
1487 object_get_canonical_path_component(OBJECT(mr
));
1492 bool memory_region_is_skip_dump(MemoryRegion
*mr
)
1494 return mr
->skip_dump
;
1497 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1499 uint8_t mask
= mr
->dirty_log_mask
;
1500 if (global_dirty_log
) {
1501 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1506 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1508 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1511 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1513 if (mr
->iommu_ops
->notify_started
&&
1514 QLIST_EMPTY(&mr
->iommu_notify
.notifiers
)) {
1515 mr
->iommu_ops
->notify_started(mr
);
1517 notifier_list_add(&mr
->iommu_notify
, n
);
1520 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
)
1522 assert(memory_region_is_iommu(mr
));
1523 if (mr
->iommu_ops
&& mr
->iommu_ops
->get_min_page_size
) {
1524 return mr
->iommu_ops
->get_min_page_size(mr
);
1526 return TARGET_PAGE_SIZE
;
1529 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
, bool is_write
)
1531 hwaddr addr
, granularity
;
1532 IOMMUTLBEntry iotlb
;
1534 granularity
= memory_region_iommu_get_min_page_size(mr
);
1536 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1537 iotlb
= mr
->iommu_ops
->translate(mr
, addr
, is_write
);
1538 if (iotlb
.perm
!= IOMMU_NONE
) {
1539 n
->notify(n
, &iotlb
);
1542 /* if (2^64 - MR size) < granularity, it's possible to get an
1543 * infinite loop here. This should catch such a wraparound */
1544 if ((addr
+ granularity
) < addr
) {
1550 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1553 if (mr
->iommu_ops
->notify_stopped
&&
1554 QLIST_EMPTY(&mr
->iommu_notify
.notifiers
)) {
1555 mr
->iommu_ops
->notify_stopped(mr
);
1559 void memory_region_notify_iommu(MemoryRegion
*mr
,
1560 IOMMUTLBEntry entry
)
1562 assert(memory_region_is_iommu(mr
));
1563 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1566 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1568 uint8_t mask
= 1 << client
;
1569 uint8_t old_logging
;
1571 assert(client
== DIRTY_MEMORY_VGA
);
1572 old_logging
= mr
->vga_logging_count
;
1573 mr
->vga_logging_count
+= log
? 1 : -1;
1574 if (!!old_logging
== !!mr
->vga_logging_count
) {
1578 memory_region_transaction_begin();
1579 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1580 memory_region_update_pending
|= mr
->enabled
;
1581 memory_region_transaction_commit();
1584 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1585 hwaddr size
, unsigned client
)
1587 assert(mr
->ram_block
);
1588 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr
) + addr
,
1592 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1595 assert(mr
->ram_block
);
1596 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
1598 memory_region_get_dirty_log_mask(mr
));
1601 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1602 hwaddr size
, unsigned client
)
1604 assert(mr
->ram_block
);
1605 return cpu_physical_memory_test_and_clear_dirty(
1606 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1610 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1615 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1616 FlatView
*view
= address_space_get_flatview(as
);
1617 FOR_EACH_FLAT_RANGE(fr
, view
) {
1619 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1622 flatview_unref(view
);
1626 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1628 if (mr
->readonly
!= readonly
) {
1629 memory_region_transaction_begin();
1630 mr
->readonly
= readonly
;
1631 memory_region_update_pending
|= mr
->enabled
;
1632 memory_region_transaction_commit();
1636 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1638 if (mr
->romd_mode
!= romd_mode
) {
1639 memory_region_transaction_begin();
1640 mr
->romd_mode
= romd_mode
;
1641 memory_region_update_pending
|= mr
->enabled
;
1642 memory_region_transaction_commit();
1646 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1647 hwaddr size
, unsigned client
)
1649 assert(mr
->ram_block
);
1650 cpu_physical_memory_test_and_clear_dirty(
1651 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1654 int memory_region_get_fd(MemoryRegion
*mr
)
1662 fd
= mr
->ram_block
->fd
;
1668 void memory_region_set_fd(MemoryRegion
*mr
, int fd
)
1674 mr
->ram_block
->fd
= fd
;
1678 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1681 uint64_t offset
= 0;
1685 offset
+= mr
->alias_offset
;
1688 assert(mr
->ram_block
);
1689 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
1695 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
1699 block
= qemu_ram_block_from_host(ptr
, false, offset
);
1707 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1709 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
1712 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1714 assert(mr
->ram_block
);
1716 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
1719 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1723 CoalescedMemoryRange
*cmr
;
1725 MemoryRegionSection section
;
1727 view
= address_space_get_flatview(as
);
1728 FOR_EACH_FLAT_RANGE(fr
, view
) {
1730 section
= (MemoryRegionSection
) {
1731 .address_space
= as
,
1732 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1733 .size
= fr
->addr
.size
,
1736 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1737 int128_get64(fr
->addr
.start
),
1738 int128_get64(fr
->addr
.size
));
1739 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1740 tmp
= addrrange_shift(cmr
->addr
,
1741 int128_sub(fr
->addr
.start
,
1742 int128_make64(fr
->offset_in_region
)));
1743 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1746 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1747 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1748 int128_get64(tmp
.start
),
1749 int128_get64(tmp
.size
));
1753 flatview_unref(view
);
1756 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1760 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1761 memory_region_update_coalesced_range_as(mr
, as
);
1765 void memory_region_set_coalescing(MemoryRegion
*mr
)
1767 memory_region_clear_coalescing(mr
);
1768 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1771 void memory_region_add_coalescing(MemoryRegion
*mr
,
1775 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1777 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1778 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1779 memory_region_update_coalesced_range(mr
);
1780 memory_region_set_flush_coalesced(mr
);
1783 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1785 CoalescedMemoryRange
*cmr
;
1786 bool updated
= false;
1788 qemu_flush_coalesced_mmio_buffer();
1789 mr
->flush_coalesced_mmio
= false;
1791 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1792 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1793 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1799 memory_region_update_coalesced_range(mr
);
1803 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1805 mr
->flush_coalesced_mmio
= true;
1808 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1810 qemu_flush_coalesced_mmio_buffer();
1811 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1812 mr
->flush_coalesced_mmio
= false;
1816 void memory_region_set_global_locking(MemoryRegion
*mr
)
1818 mr
->global_locking
= true;
1821 void memory_region_clear_global_locking(MemoryRegion
*mr
)
1823 mr
->global_locking
= false;
1826 static bool userspace_eventfd_warning
;
1828 void memory_region_add_eventfd(MemoryRegion
*mr
,
1835 MemoryRegionIoeventfd mrfd
= {
1836 .addr
.start
= int128_make64(addr
),
1837 .addr
.size
= int128_make64(size
),
1838 .match_data
= match_data
,
1844 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1845 userspace_eventfd_warning
))) {
1846 userspace_eventfd_warning
= true;
1847 error_report("Using eventfd without MMIO binding in KVM. "
1848 "Suboptimal performance expected");
1852 adjust_endianness(mr
, &mrfd
.data
, size
);
1854 memory_region_transaction_begin();
1855 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1856 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1861 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1862 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1863 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1864 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1865 mr
->ioeventfds
[i
] = mrfd
;
1866 ioeventfd_update_pending
|= mr
->enabled
;
1867 memory_region_transaction_commit();
1870 void memory_region_del_eventfd(MemoryRegion
*mr
,
1877 MemoryRegionIoeventfd mrfd
= {
1878 .addr
.start
= int128_make64(addr
),
1879 .addr
.size
= int128_make64(size
),
1880 .match_data
= match_data
,
1887 adjust_endianness(mr
, &mrfd
.data
, size
);
1889 memory_region_transaction_begin();
1890 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1891 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1895 assert(i
!= mr
->ioeventfd_nb
);
1896 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1897 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1899 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1900 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1901 ioeventfd_update_pending
|= mr
->enabled
;
1902 memory_region_transaction_commit();
1905 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1907 MemoryRegion
*mr
= subregion
->container
;
1908 MemoryRegion
*other
;
1910 memory_region_transaction_begin();
1912 memory_region_ref(subregion
);
1913 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1914 if (subregion
->priority
>= other
->priority
) {
1915 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1919 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1921 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1922 memory_region_transaction_commit();
1925 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1927 MemoryRegion
*subregion
)
1929 assert(!subregion
->container
);
1930 subregion
->container
= mr
;
1931 subregion
->addr
= offset
;
1932 memory_region_update_container_subregions(subregion
);
1935 void memory_region_add_subregion(MemoryRegion
*mr
,
1937 MemoryRegion
*subregion
)
1939 subregion
->priority
= 0;
1940 memory_region_add_subregion_common(mr
, offset
, subregion
);
1943 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1945 MemoryRegion
*subregion
,
1948 subregion
->priority
= priority
;
1949 memory_region_add_subregion_common(mr
, offset
, subregion
);
1952 void memory_region_del_subregion(MemoryRegion
*mr
,
1953 MemoryRegion
*subregion
)
1955 memory_region_transaction_begin();
1956 assert(subregion
->container
== mr
);
1957 subregion
->container
= NULL
;
1958 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1959 memory_region_unref(subregion
);
1960 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1961 memory_region_transaction_commit();
1964 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1966 if (enabled
== mr
->enabled
) {
1969 memory_region_transaction_begin();
1970 mr
->enabled
= enabled
;
1971 memory_region_update_pending
= true;
1972 memory_region_transaction_commit();
1975 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
1977 Int128 s
= int128_make64(size
);
1979 if (size
== UINT64_MAX
) {
1982 if (int128_eq(s
, mr
->size
)) {
1985 memory_region_transaction_begin();
1987 memory_region_update_pending
= true;
1988 memory_region_transaction_commit();
1991 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1993 MemoryRegion
*container
= mr
->container
;
1996 memory_region_transaction_begin();
1997 memory_region_ref(mr
);
1998 memory_region_del_subregion(container
, mr
);
1999 mr
->container
= container
;
2000 memory_region_update_container_subregions(mr
);
2001 memory_region_unref(mr
);
2002 memory_region_transaction_commit();
2006 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2008 if (addr
!= mr
->addr
) {
2010 memory_region_readd_subregion(mr
);
2014 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2018 if (offset
== mr
->alias_offset
) {
2022 memory_region_transaction_begin();
2023 mr
->alias_offset
= offset
;
2024 memory_region_update_pending
|= mr
->enabled
;
2025 memory_region_transaction_commit();
2028 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2033 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2035 const AddrRange
*addr
= addr_
;
2036 const FlatRange
*fr
= fr_
;
2038 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2040 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2046 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2048 return bsearch(&addr
, view
->ranges
, view
->nr
,
2049 sizeof(FlatRange
), cmp_flatrange_addr
);
2052 bool memory_region_is_mapped(MemoryRegion
*mr
)
2054 return mr
->container
? true : false;
2057 /* Same as memory_region_find, but it does not add a reference to the
2058 * returned region. It must be called from an RCU critical section.
2060 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2061 hwaddr addr
, uint64_t size
)
2063 MemoryRegionSection ret
= { .mr
= NULL
};
2071 for (root
= mr
; root
->container
; ) {
2072 root
= root
->container
;
2076 as
= memory_region_to_address_space(root
);
2080 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2082 view
= atomic_rcu_read(&as
->current_map
);
2083 fr
= flatview_lookup(view
, range
);
2088 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2093 ret
.address_space
= as
;
2094 range
= addrrange_intersection(range
, fr
->addr
);
2095 ret
.offset_within_region
= fr
->offset_in_region
;
2096 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2098 ret
.size
= range
.size
;
2099 ret
.offset_within_address_space
= int128_get64(range
.start
);
2100 ret
.readonly
= fr
->readonly
;
2104 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2105 hwaddr addr
, uint64_t size
)
2107 MemoryRegionSection ret
;
2109 ret
= memory_region_find_rcu(mr
, addr
, size
);
2111 memory_region_ref(ret
.mr
);
2117 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2122 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2124 return mr
&& mr
!= container
;
2127 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
2132 view
= address_space_get_flatview(as
);
2133 FOR_EACH_FLAT_RANGE(fr
, view
) {
2134 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
2136 flatview_unref(view
);
2139 void memory_global_dirty_log_start(void)
2141 global_dirty_log
= true;
2143 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2145 /* Refresh DIRTY_LOG_MIGRATION bit. */
2146 memory_region_transaction_begin();
2147 memory_region_update_pending
= true;
2148 memory_region_transaction_commit();
2151 void memory_global_dirty_log_stop(void)
2153 global_dirty_log
= false;
2155 /* Refresh DIRTY_LOG_MIGRATION bit. */
2156 memory_region_transaction_begin();
2157 memory_region_update_pending
= true;
2158 memory_region_transaction_commit();
2160 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2163 static void listener_add_address_space(MemoryListener
*listener
,
2169 if (listener
->address_space_filter
2170 && listener
->address_space_filter
!= as
) {
2174 if (listener
->begin
) {
2175 listener
->begin(listener
);
2177 if (global_dirty_log
) {
2178 if (listener
->log_global_start
) {
2179 listener
->log_global_start(listener
);
2183 view
= address_space_get_flatview(as
);
2184 FOR_EACH_FLAT_RANGE(fr
, view
) {
2185 MemoryRegionSection section
= {
2187 .address_space
= as
,
2188 .offset_within_region
= fr
->offset_in_region
,
2189 .size
= fr
->addr
.size
,
2190 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2191 .readonly
= fr
->readonly
,
2193 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2194 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2196 if (listener
->region_add
) {
2197 listener
->region_add(listener
, §ion
);
2200 if (listener
->commit
) {
2201 listener
->commit(listener
);
2203 flatview_unref(view
);
2206 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
2208 MemoryListener
*other
= NULL
;
2211 listener
->address_space_filter
= filter
;
2212 if (QTAILQ_EMPTY(&memory_listeners
)
2213 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2214 memory_listeners
)->priority
) {
2215 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2217 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2218 if (listener
->priority
< other
->priority
) {
2222 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2225 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2226 listener_add_address_space(listener
, as
);
2230 void memory_listener_unregister(MemoryListener
*listener
)
2232 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2235 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2237 memory_region_ref(root
);
2238 memory_region_transaction_begin();
2241 as
->malloced
= false;
2242 as
->current_map
= g_new(FlatView
, 1);
2243 flatview_init(as
->current_map
);
2244 as
->ioeventfd_nb
= 0;
2245 as
->ioeventfds
= NULL
;
2246 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2247 as
->name
= g_strdup(name
? name
: "anonymous");
2248 address_space_init_dispatch(as
);
2249 memory_region_update_pending
|= root
->enabled
;
2250 memory_region_transaction_commit();
2253 static void do_address_space_destroy(AddressSpace
*as
)
2255 MemoryListener
*listener
;
2256 bool do_free
= as
->malloced
;
2258 address_space_destroy_dispatch(as
);
2260 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2261 assert(listener
->address_space_filter
!= as
);
2264 flatview_unref(as
->current_map
);
2266 g_free(as
->ioeventfds
);
2267 memory_region_unref(as
->root
);
2273 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
, const char *name
)
2277 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2278 if (root
== as
->root
&& as
->malloced
) {
2284 as
= g_malloc0(sizeof *as
);
2285 address_space_init(as
, root
, name
);
2286 as
->malloced
= true;
2290 void address_space_destroy(AddressSpace
*as
)
2292 MemoryRegion
*root
= as
->root
;
2295 if (as
->ref_count
) {
2298 /* Flush out anything from MemoryListeners listening in on this */
2299 memory_region_transaction_begin();
2301 memory_region_transaction_commit();
2302 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2303 address_space_unregister(as
);
2305 /* At this point, as->dispatch and as->current_map are dummy
2306 * entries that the guest should never use. Wait for the old
2307 * values to expire before freeing the data.
2310 call_rcu(as
, do_address_space_destroy
, rcu
);
2313 typedef struct MemoryRegionList MemoryRegionList
;
2315 struct MemoryRegionList
{
2316 const MemoryRegion
*mr
;
2317 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2320 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2322 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2323 const MemoryRegion
*mr
, unsigned int level
,
2325 MemoryRegionListHead
*alias_print_queue
)
2327 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2328 MemoryRegionListHead submr_print_queue
;
2329 const MemoryRegion
*submr
;
2336 for (i
= 0; i
< level
; i
++) {
2341 MemoryRegionList
*ml
;
2344 /* check if the alias is already in the queue */
2345 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2346 if (ml
->mr
== mr
->alias
) {
2352 ml
= g_new(MemoryRegionList
, 1);
2354 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2356 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2357 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2358 "-" TARGET_FMT_plx
"%s\n",
2361 + (int128_nz(mr
->size
) ?
2362 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2363 int128_one())) : 0),
2365 mr
->romd_mode
? 'R' : '-',
2366 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2368 memory_region_name(mr
),
2369 memory_region_name(mr
->alias
),
2372 + (int128_nz(mr
->size
) ?
2373 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2374 int128_one())) : 0),
2375 mr
->enabled
? "" : " [disabled]");
2378 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s%s\n",
2381 + (int128_nz(mr
->size
) ?
2382 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2383 int128_one())) : 0),
2385 mr
->romd_mode
? 'R' : '-',
2386 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2388 memory_region_name(mr
),
2389 mr
->enabled
? "" : " [disabled]");
2392 QTAILQ_INIT(&submr_print_queue
);
2394 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2395 new_ml
= g_new(MemoryRegionList
, 1);
2397 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2398 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2399 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2400 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2401 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2407 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2411 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2412 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2416 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2421 void mtree_info(fprintf_function mon_printf
, void *f
)
2423 MemoryRegionListHead ml_head
;
2424 MemoryRegionList
*ml
, *ml2
;
2427 QTAILQ_INIT(&ml_head
);
2429 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2430 mon_printf(f
, "address-space: %s\n", as
->name
);
2431 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2432 mon_printf(f
, "\n");
2435 /* print aliased regions */
2436 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2437 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2438 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2439 mon_printf(f
, "\n");
2442 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2447 static const TypeInfo memory_region_info
= {
2448 .parent
= TYPE_OBJECT
,
2449 .name
= TYPE_MEMORY_REGION
,
2450 .instance_size
= sizeof(MemoryRegion
),
2451 .instance_init
= memory_region_initfn
,
2452 .instance_finalize
= memory_region_finalize
,
2455 static void memory_register_types(void)
2457 type_register_static(&memory_region_info
);
2460 type_init(memory_register_types
)