2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "qemu-common.h"
20 #include "exec/memory.h"
21 #include "exec/address-spaces.h"
22 #include "exec/ioport.h"
23 #include "qapi/visitor.h"
24 #include "qemu/bitops.h"
25 #include "qemu/error-report.h"
26 #include "qom/object.h"
27 #include "trace-root.h"
29 #include "exec/memory-internal.h"
30 #include "exec/ram_addr.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/sysemu.h"
33 #include "hw/misc/mmio_interface.h"
34 #include "hw/qdev-properties.h"
35 #include "migration/vmstate.h"
37 //#define DEBUG_UNASSIGNED
39 static unsigned memory_region_transaction_depth
;
40 static bool memory_region_update_pending
;
41 static bool ioeventfd_update_pending
;
42 static bool global_dirty_log
= false;
44 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
45 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
47 static QTAILQ_HEAD(, AddressSpace
) address_spaces
48 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
50 typedef struct AddrRange AddrRange
;
53 * Note that signed integers are needed for negative offsetting in aliases
54 * (large MemoryRegion::alias_offset).
61 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
63 return (AddrRange
) { start
, size
};
66 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
68 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
71 static Int128
addrrange_end(AddrRange r
)
73 return int128_add(r
.start
, r
.size
);
76 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
78 int128_addto(&range
.start
, delta
);
82 static bool addrrange_contains(AddrRange range
, Int128 addr
)
84 return int128_ge(addr
, range
.start
)
85 && int128_lt(addr
, addrrange_end(range
));
88 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
90 return addrrange_contains(r1
, r2
.start
)
91 || addrrange_contains(r2
, r1
.start
);
94 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
96 Int128 start
= int128_max(r1
.start
, r2
.start
);
97 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
98 return addrrange_make(start
, int128_sub(end
, start
));
101 enum ListenerDirection
{ Forward
, Reverse
};
103 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
105 MemoryListener *_listener; \
107 switch (_direction) { \
109 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
110 if (_listener->_callback) { \
111 _listener->_callback(_listener, ##_args); \
116 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
117 memory_listeners, link) { \
118 if (_listener->_callback) { \
119 _listener->_callback(_listener, ##_args); \
128 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
130 MemoryListener *_listener; \
131 struct memory_listeners_as *list = &(_as)->listeners; \
133 switch (_direction) { \
135 QTAILQ_FOREACH(_listener, list, link_as) { \
136 if (_listener->_callback) { \
137 _listener->_callback(_listener, _section, ##_args); \
142 QTAILQ_FOREACH_REVERSE(_listener, list, memory_listeners_as, \
144 if (_listener->_callback) { \
145 _listener->_callback(_listener, _section, ##_args); \
154 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
155 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
157 MemoryRegionSection mrs = section_from_flat_range(fr, as); \
158 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
161 struct CoalescedMemoryRange
{
163 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
166 struct MemoryRegionIoeventfd
{
173 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
174 MemoryRegionIoeventfd b
)
176 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
178 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
180 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
182 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
184 } else if (a
.match_data
< b
.match_data
) {
186 } else if (a
.match_data
> b
.match_data
) {
188 } else if (a
.match_data
) {
189 if (a
.data
< b
.data
) {
191 } else if (a
.data
> b
.data
) {
197 } else if (a
.e
> b
.e
) {
203 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
204 MemoryRegionIoeventfd b
)
206 return !memory_region_ioeventfd_before(a
, b
)
207 && !memory_region_ioeventfd_before(b
, a
);
210 typedef struct FlatRange FlatRange
;
211 typedef struct FlatView FlatView
;
213 /* Range of memory in the global map. Addresses are absolute. */
216 hwaddr offset_in_region
;
218 uint8_t dirty_log_mask
;
223 /* Flattened global view of current active memory hierarchy. Kept in sorted
231 unsigned nr_allocated
;
234 typedef struct AddressSpaceOps AddressSpaceOps
;
236 #define FOR_EACH_FLAT_RANGE(var, view) \
237 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
239 static inline MemoryRegionSection
240 section_from_flat_range(FlatRange
*fr
, AddressSpace
*as
)
242 return (MemoryRegionSection
) {
245 .offset_within_region
= fr
->offset_in_region
,
246 .size
= fr
->addr
.size
,
247 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
248 .readonly
= fr
->readonly
,
252 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
254 return a
->mr
== b
->mr
255 && addrrange_equal(a
->addr
, b
->addr
)
256 && a
->offset_in_region
== b
->offset_in_region
257 && a
->romd_mode
== b
->romd_mode
258 && a
->readonly
== b
->readonly
;
261 static void flatview_init(FlatView
*view
)
266 view
->nr_allocated
= 0;
269 /* Insert a range into a given position. Caller is responsible for maintaining
272 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
274 if (view
->nr
== view
->nr_allocated
) {
275 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
276 view
->ranges
= g_realloc(view
->ranges
,
277 view
->nr_allocated
* sizeof(*view
->ranges
));
279 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
280 (view
->nr
- pos
) * sizeof(FlatRange
));
281 view
->ranges
[pos
] = *range
;
282 memory_region_ref(range
->mr
);
286 static void flatview_destroy(FlatView
*view
)
290 for (i
= 0; i
< view
->nr
; i
++) {
291 memory_region_unref(view
->ranges
[i
].mr
);
293 g_free(view
->ranges
);
297 static void flatview_ref(FlatView
*view
)
299 atomic_inc(&view
->ref
);
302 static void flatview_unref(FlatView
*view
)
304 if (atomic_fetch_dec(&view
->ref
) == 1) {
305 flatview_destroy(view
);
309 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
311 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
313 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
315 int128_make64(r2
->offset_in_region
))
316 && r1
->dirty_log_mask
== r2
->dirty_log_mask
317 && r1
->romd_mode
== r2
->romd_mode
318 && r1
->readonly
== r2
->readonly
;
321 /* Attempt to simplify a view by merging adjacent ranges */
322 static void flatview_simplify(FlatView
*view
)
327 while (i
< view
->nr
) {
330 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
331 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
335 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
336 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
341 static bool memory_region_big_endian(MemoryRegion
*mr
)
343 #ifdef TARGET_WORDS_BIGENDIAN
344 return mr
->ops
->endianness
!= DEVICE_LITTLE_ENDIAN
;
346 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
350 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
352 #ifdef TARGET_WORDS_BIGENDIAN
353 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
355 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
359 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
361 if (memory_region_wrong_endianness(mr
)) {
366 *data
= bswap16(*data
);
369 *data
= bswap32(*data
);
372 *data
= bswap64(*data
);
380 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
383 hwaddr abs_addr
= offset
;
385 abs_addr
+= mr
->addr
;
386 for (root
= mr
; root
->container
; ) {
387 root
= root
->container
;
388 abs_addr
+= root
->addr
;
394 static int get_cpu_index(void)
397 return current_cpu
->cpu_index
;
402 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
412 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
414 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
415 } else if (mr
== &io_mem_notdirty
) {
416 /* Accesses to code which has previously been translated into a TB show
417 * up in the MMIO path, as accesses to the io_mem_notdirty
419 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
420 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
421 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
422 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
424 *value
|= (tmp
& mask
) << shift
;
428 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
438 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
440 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
441 } else if (mr
== &io_mem_notdirty
) {
442 /* Accesses to code which has previously been translated into a TB show
443 * up in the MMIO path, as accesses to the io_mem_notdirty
445 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
446 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
447 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
448 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
450 *value
|= (tmp
& mask
) << shift
;
454 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
465 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
467 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
468 } else if (mr
== &io_mem_notdirty
) {
469 /* Accesses to code which has previously been translated into a TB show
470 * up in the MMIO path, as accesses to the io_mem_notdirty
472 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
473 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
474 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
475 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
477 *value
|= (tmp
& mask
) << shift
;
481 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
491 tmp
= (*value
>> shift
) & mask
;
493 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
494 } else if (mr
== &io_mem_notdirty
) {
495 /* Accesses to code which has previously been translated into a TB show
496 * up in the MMIO path, as accesses to the io_mem_notdirty
498 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
499 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
500 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
501 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
503 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
507 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
517 tmp
= (*value
>> shift
) & mask
;
519 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
520 } else if (mr
== &io_mem_notdirty
) {
521 /* Accesses to code which has previously been translated into a TB show
522 * up in the MMIO path, as accesses to the io_mem_notdirty
524 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
525 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
526 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
527 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
529 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
533 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
543 tmp
= (*value
>> shift
) & mask
;
545 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
546 } else if (mr
== &io_mem_notdirty
) {
547 /* Accesses to code which has previously been translated into a TB show
548 * up in the MMIO path, as accesses to the io_mem_notdirty
550 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
551 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
552 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
553 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
555 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
558 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
561 unsigned access_size_min
,
562 unsigned access_size_max
,
563 MemTxResult (*access
)(MemoryRegion
*mr
,
573 uint64_t access_mask
;
574 unsigned access_size
;
576 MemTxResult r
= MEMTX_OK
;
578 if (!access_size_min
) {
581 if (!access_size_max
) {
585 /* FIXME: support unaligned access? */
586 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
587 access_mask
= -1ULL >> (64 - access_size
* 8);
588 if (memory_region_big_endian(mr
)) {
589 for (i
= 0; i
< size
; i
+= access_size
) {
590 r
|= access(mr
, addr
+ i
, value
, access_size
,
591 (size
- access_size
- i
) * 8, access_mask
, attrs
);
594 for (i
= 0; i
< size
; i
+= access_size
) {
595 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
602 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
606 while (mr
->container
) {
609 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
610 if (mr
== as
->root
) {
617 /* Render a memory region into the global view. Ranges in @view obscure
620 static void render_memory_region(FlatView
*view
,
626 MemoryRegion
*subregion
;
628 hwaddr offset_in_region
;
638 int128_addto(&base
, int128_make64(mr
->addr
));
639 readonly
|= mr
->readonly
;
641 tmp
= addrrange_make(base
, mr
->size
);
643 if (!addrrange_intersects(tmp
, clip
)) {
647 clip
= addrrange_intersection(tmp
, clip
);
650 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
651 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
652 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
656 /* Render subregions in priority order. */
657 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
658 render_memory_region(view
, subregion
, base
, clip
, readonly
);
661 if (!mr
->terminates
) {
665 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
670 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
671 fr
.romd_mode
= mr
->romd_mode
;
672 fr
.readonly
= readonly
;
674 /* Render the region itself into any gaps left by the current view. */
675 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
676 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
679 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
680 now
= int128_min(remain
,
681 int128_sub(view
->ranges
[i
].addr
.start
, base
));
682 fr
.offset_in_region
= offset_in_region
;
683 fr
.addr
= addrrange_make(base
, now
);
684 flatview_insert(view
, i
, &fr
);
686 int128_addto(&base
, now
);
687 offset_in_region
+= int128_get64(now
);
688 int128_subfrom(&remain
, now
);
690 now
= int128_sub(int128_min(int128_add(base
, remain
),
691 addrrange_end(view
->ranges
[i
].addr
)),
693 int128_addto(&base
, now
);
694 offset_in_region
+= int128_get64(now
);
695 int128_subfrom(&remain
, now
);
697 if (int128_nz(remain
)) {
698 fr
.offset_in_region
= offset_in_region
;
699 fr
.addr
= addrrange_make(base
, remain
);
700 flatview_insert(view
, i
, &fr
);
704 /* Render a memory topology into a list of disjoint absolute ranges. */
705 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
709 view
= g_new(FlatView
, 1);
713 render_memory_region(view
, mr
, int128_zero(),
714 addrrange_make(int128_zero(), int128_2_64()), false);
716 flatview_simplify(view
);
721 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
722 MemoryRegionIoeventfd
*fds_new
,
724 MemoryRegionIoeventfd
*fds_old
,
728 MemoryRegionIoeventfd
*fd
;
729 MemoryRegionSection section
;
731 /* Generate a symmetric difference of the old and new fd sets, adding
732 * and deleting as necessary.
736 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
737 if (iold
< fds_old_nb
738 && (inew
== fds_new_nb
739 || memory_region_ioeventfd_before(fds_old
[iold
],
742 section
= (MemoryRegionSection
) {
744 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
745 .size
= fd
->addr
.size
,
747 MEMORY_LISTENER_CALL(as
, eventfd_del
, Forward
, §ion
,
748 fd
->match_data
, fd
->data
, fd
->e
);
750 } else if (inew
< fds_new_nb
751 && (iold
== fds_old_nb
752 || memory_region_ioeventfd_before(fds_new
[inew
],
755 section
= (MemoryRegionSection
) {
757 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
758 .size
= fd
->addr
.size
,
760 MEMORY_LISTENER_CALL(as
, eventfd_add
, Reverse
, §ion
,
761 fd
->match_data
, fd
->data
, fd
->e
);
770 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
775 view
= atomic_rcu_read(&as
->current_map
);
781 static void address_space_update_ioeventfds(AddressSpace
*as
)
785 unsigned ioeventfd_nb
= 0;
786 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
790 view
= address_space_get_flatview(as
);
791 FOR_EACH_FLAT_RANGE(fr
, view
) {
792 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
793 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
794 int128_sub(fr
->addr
.start
,
795 int128_make64(fr
->offset_in_region
)));
796 if (addrrange_intersects(fr
->addr
, tmp
)) {
798 ioeventfds
= g_realloc(ioeventfds
,
799 ioeventfd_nb
* sizeof(*ioeventfds
));
800 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
801 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
806 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
807 as
->ioeventfds
, as
->ioeventfd_nb
);
809 g_free(as
->ioeventfds
);
810 as
->ioeventfds
= ioeventfds
;
811 as
->ioeventfd_nb
= ioeventfd_nb
;
812 flatview_unref(view
);
815 static void address_space_update_topology_pass(AddressSpace
*as
,
816 const FlatView
*old_view
,
817 const FlatView
*new_view
,
821 FlatRange
*frold
, *frnew
;
823 /* Generate a symmetric difference of the old and new memory maps.
824 * Kill ranges in the old map, and instantiate ranges in the new map.
827 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
828 if (iold
< old_view
->nr
) {
829 frold
= &old_view
->ranges
[iold
];
833 if (inew
< new_view
->nr
) {
834 frnew
= &new_view
->ranges
[inew
];
841 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
842 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
843 && !flatrange_equal(frold
, frnew
)))) {
844 /* In old but not in new, or in both but attributes changed. */
847 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
851 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
852 /* In both and unchanged (except logging may have changed) */
855 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
856 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
857 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
858 frold
->dirty_log_mask
,
859 frnew
->dirty_log_mask
);
861 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
862 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
863 frold
->dirty_log_mask
,
864 frnew
->dirty_log_mask
);
874 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
883 static void address_space_update_topology(AddressSpace
*as
)
885 FlatView
*old_view
= address_space_get_flatview(as
);
886 FlatView
*new_view
= generate_memory_topology(as
->root
);
888 address_space_update_topology_pass(as
, old_view
, new_view
, false);
889 address_space_update_topology_pass(as
, old_view
, new_view
, true);
891 /* Writes are protected by the BQL. */
892 atomic_rcu_set(&as
->current_map
, new_view
);
893 call_rcu(old_view
, flatview_unref
, rcu
);
895 /* Note that all the old MemoryRegions are still alive up to this
896 * point. This relieves most MemoryListeners from the need to
897 * ref/unref the MemoryRegions they get---unless they use them
898 * outside the iothread mutex, in which case precise reference
899 * counting is necessary.
901 flatview_unref(old_view
);
903 address_space_update_ioeventfds(as
);
906 void memory_region_transaction_begin(void)
908 qemu_flush_coalesced_mmio_buffer();
909 ++memory_region_transaction_depth
;
912 void memory_region_transaction_commit(void)
916 assert(memory_region_transaction_depth
);
917 assert(qemu_mutex_iothread_locked());
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
);
927 memory_region_update_pending
= false;
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
);
933 ioeventfd_update_pending
= false;
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 static void memory_region_do_init(MemoryRegion
*mr
,
986 mr
->size
= int128_make64(size
);
987 if (size
== UINT64_MAX
) {
988 mr
->size
= int128_2_64();
990 mr
->name
= g_strdup(name
);
992 mr
->ram_block
= NULL
;
995 char *escaped_name
= memory_region_escape_name(name
);
996 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
999 owner
= container_get(qdev_get_machine(), "/unattached");
1002 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1003 object_unref(OBJECT(mr
));
1005 g_free(escaped_name
);
1009 void memory_region_init(MemoryRegion
*mr
,
1014 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
1015 memory_region_do_init(mr
, owner
, name
, size
);
1018 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1019 void *opaque
, Error
**errp
)
1021 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1022 uint64_t value
= mr
->addr
;
1024 visit_type_uint64(v
, name
, &value
, errp
);
1027 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1028 const char *name
, void *opaque
,
1031 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1032 gchar
*path
= (gchar
*)"";
1034 if (mr
->container
) {
1035 path
= object_get_canonical_path(OBJECT(mr
->container
));
1037 visit_type_str(v
, name
, &path
, errp
);
1038 if (mr
->container
) {
1043 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1046 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1048 return OBJECT(mr
->container
);
1051 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1052 const char *name
, void *opaque
,
1055 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1056 int32_t value
= mr
->priority
;
1058 visit_type_int32(v
, name
, &value
, errp
);
1061 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1062 void *opaque
, Error
**errp
)
1064 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1065 uint64_t value
= memory_region_size(mr
);
1067 visit_type_uint64(v
, name
, &value
, errp
);
1070 static void memory_region_initfn(Object
*obj
)
1072 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1075 mr
->ops
= &unassigned_mem_ops
;
1077 mr
->romd_mode
= true;
1078 mr
->global_locking
= true;
1079 mr
->destructor
= memory_region_destructor_none
;
1080 QTAILQ_INIT(&mr
->subregions
);
1081 QTAILQ_INIT(&mr
->coalesced
);
1083 op
= object_property_add(OBJECT(mr
), "container",
1084 "link<" TYPE_MEMORY_REGION
">",
1085 memory_region_get_container
,
1086 NULL
, /* memory_region_set_container */
1087 NULL
, NULL
, &error_abort
);
1088 op
->resolve
= memory_region_resolve_container
;
1090 object_property_add(OBJECT(mr
), "addr", "uint64",
1091 memory_region_get_addr
,
1092 NULL
, /* memory_region_set_addr */
1093 NULL
, NULL
, &error_abort
);
1094 object_property_add(OBJECT(mr
), "priority", "uint32",
1095 memory_region_get_priority
,
1096 NULL
, /* memory_region_set_priority */
1097 NULL
, NULL
, &error_abort
);
1098 object_property_add(OBJECT(mr
), "size", "uint64",
1099 memory_region_get_size
,
1100 NULL
, /* memory_region_set_size, */
1101 NULL
, NULL
, &error_abort
);
1104 static void iommu_memory_region_initfn(Object
*obj
)
1106 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1108 mr
->is_iommu
= true;
1111 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1114 #ifdef DEBUG_UNASSIGNED
1115 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1117 if (current_cpu
!= NULL
) {
1118 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1123 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1124 uint64_t val
, unsigned size
)
1126 #ifdef DEBUG_UNASSIGNED
1127 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1129 if (current_cpu
!= NULL
) {
1130 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1134 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1135 unsigned size
, bool is_write
)
1140 const MemoryRegionOps unassigned_mem_ops
= {
1141 .valid
.accepts
= unassigned_mem_accepts
,
1142 .endianness
= DEVICE_NATIVE_ENDIAN
,
1145 static uint64_t memory_region_ram_device_read(void *opaque
,
1146 hwaddr addr
, unsigned size
)
1148 MemoryRegion
*mr
= opaque
;
1149 uint64_t data
= (uint64_t)~0;
1153 data
= *(uint8_t *)(mr
->ram_block
->host
+ addr
);
1156 data
= *(uint16_t *)(mr
->ram_block
->host
+ addr
);
1159 data
= *(uint32_t *)(mr
->ram_block
->host
+ addr
);
1162 data
= *(uint64_t *)(mr
->ram_block
->host
+ addr
);
1166 trace_memory_region_ram_device_read(get_cpu_index(), mr
, addr
, data
, size
);
1171 static void memory_region_ram_device_write(void *opaque
, hwaddr addr
,
1172 uint64_t data
, unsigned size
)
1174 MemoryRegion
*mr
= opaque
;
1176 trace_memory_region_ram_device_write(get_cpu_index(), mr
, addr
, data
, size
);
1180 *(uint8_t *)(mr
->ram_block
->host
+ addr
) = (uint8_t)data
;
1183 *(uint16_t *)(mr
->ram_block
->host
+ addr
) = (uint16_t)data
;
1186 *(uint32_t *)(mr
->ram_block
->host
+ addr
) = (uint32_t)data
;
1189 *(uint64_t *)(mr
->ram_block
->host
+ addr
) = data
;
1194 static const MemoryRegionOps ram_device_mem_ops
= {
1195 .read
= memory_region_ram_device_read
,
1196 .write
= memory_region_ram_device_write
,
1197 .endianness
= DEVICE_HOST_ENDIAN
,
1199 .min_access_size
= 1,
1200 .max_access_size
= 8,
1204 .min_access_size
= 1,
1205 .max_access_size
= 8,
1210 bool memory_region_access_valid(MemoryRegion
*mr
,
1215 int access_size_min
, access_size_max
;
1218 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1222 if (!mr
->ops
->valid
.accepts
) {
1226 access_size_min
= mr
->ops
->valid
.min_access_size
;
1227 if (!mr
->ops
->valid
.min_access_size
) {
1228 access_size_min
= 1;
1231 access_size_max
= mr
->ops
->valid
.max_access_size
;
1232 if (!mr
->ops
->valid
.max_access_size
) {
1233 access_size_max
= 4;
1236 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1237 for (i
= 0; i
< size
; i
+= access_size
) {
1238 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1247 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1255 if (mr
->ops
->read
) {
1256 return access_with_adjusted_size(addr
, pval
, size
,
1257 mr
->ops
->impl
.min_access_size
,
1258 mr
->ops
->impl
.max_access_size
,
1259 memory_region_read_accessor
,
1261 } else if (mr
->ops
->read_with_attrs
) {
1262 return access_with_adjusted_size(addr
, pval
, size
,
1263 mr
->ops
->impl
.min_access_size
,
1264 mr
->ops
->impl
.max_access_size
,
1265 memory_region_read_with_attrs_accessor
,
1268 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1269 memory_region_oldmmio_read_accessor
,
1274 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1282 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1283 *pval
= unassigned_mem_read(mr
, addr
, size
);
1284 return MEMTX_DECODE_ERROR
;
1287 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1288 adjust_endianness(mr
, pval
, size
);
1292 /* Return true if an eventfd was signalled */
1293 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1299 MemoryRegionIoeventfd ioeventfd
= {
1300 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1305 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1306 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1307 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1309 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1310 event_notifier_set(ioeventfd
.e
);
1318 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1324 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1325 unassigned_mem_write(mr
, addr
, data
, size
);
1326 return MEMTX_DECODE_ERROR
;
1329 adjust_endianness(mr
, &data
, size
);
1331 if ((!kvm_eventfds_enabled()) &&
1332 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1336 if (mr
->ops
->write
) {
1337 return access_with_adjusted_size(addr
, &data
, size
,
1338 mr
->ops
->impl
.min_access_size
,
1339 mr
->ops
->impl
.max_access_size
,
1340 memory_region_write_accessor
, mr
,
1342 } else if (mr
->ops
->write_with_attrs
) {
1344 access_with_adjusted_size(addr
, &data
, size
,
1345 mr
->ops
->impl
.min_access_size
,
1346 mr
->ops
->impl
.max_access_size
,
1347 memory_region_write_with_attrs_accessor
,
1350 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1351 memory_region_oldmmio_write_accessor
,
1356 void memory_region_init_io(MemoryRegion
*mr
,
1358 const MemoryRegionOps
*ops
,
1363 memory_region_init(mr
, owner
, name
, size
);
1364 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1365 mr
->opaque
= opaque
;
1366 mr
->terminates
= true;
1369 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1375 memory_region_init(mr
, owner
, name
, size
);
1377 mr
->terminates
= true;
1378 mr
->destructor
= memory_region_destructor_ram
;
1379 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1380 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1383 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1388 void (*resized
)(const char*,
1393 memory_region_init(mr
, owner
, name
, size
);
1395 mr
->terminates
= true;
1396 mr
->destructor
= memory_region_destructor_ram
;
1397 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1399 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1403 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1404 struct Object
*owner
,
1411 memory_region_init(mr
, owner
, name
, size
);
1413 mr
->terminates
= true;
1414 mr
->destructor
= memory_region_destructor_ram
;
1415 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1416 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1419 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1420 struct Object
*owner
,
1427 memory_region_init(mr
, owner
, name
, size
);
1429 mr
->terminates
= true;
1430 mr
->destructor
= memory_region_destructor_ram
;
1431 mr
->ram_block
= qemu_ram_alloc_from_fd(size
, mr
, share
, fd
, errp
);
1432 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1436 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1442 memory_region_init(mr
, owner
, name
, size
);
1444 mr
->terminates
= true;
1445 mr
->destructor
= memory_region_destructor_ram
;
1446 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1448 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1449 assert(ptr
!= NULL
);
1450 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1453 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1459 memory_region_init_ram_ptr(mr
, owner
, name
, size
, ptr
);
1460 mr
->ram_device
= true;
1461 mr
->ops
= &ram_device_mem_ops
;
1465 void memory_region_init_alias(MemoryRegion
*mr
,
1472 memory_region_init(mr
, owner
, name
, size
);
1474 mr
->alias_offset
= offset
;
1477 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1478 struct Object
*owner
,
1483 memory_region_init(mr
, owner
, name
, size
);
1485 mr
->readonly
= true;
1486 mr
->terminates
= true;
1487 mr
->destructor
= memory_region_destructor_ram
;
1488 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1489 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1492 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1494 const MemoryRegionOps
*ops
,
1501 memory_region_init(mr
, owner
, name
, size
);
1503 mr
->opaque
= opaque
;
1504 mr
->terminates
= true;
1505 mr
->rom_device
= true;
1506 mr
->destructor
= memory_region_destructor_ram
;
1507 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1510 void memory_region_init_iommu(void *_iommu_mr
,
1511 size_t instance_size
,
1512 const char *mrtypename
,
1517 struct IOMMUMemoryRegion
*iommu_mr
;
1518 struct MemoryRegion
*mr
;
1520 object_initialize(_iommu_mr
, instance_size
, mrtypename
);
1521 mr
= MEMORY_REGION(_iommu_mr
);
1522 memory_region_do_init(mr
, owner
, name
, size
);
1523 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1524 mr
->terminates
= true; /* then re-forwards */
1525 QLIST_INIT(&iommu_mr
->iommu_notify
);
1526 iommu_mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1529 static void memory_region_finalize(Object
*obj
)
1531 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1533 assert(!mr
->container
);
1535 /* We know the region is not visible in any address space (it
1536 * does not have a container and cannot be a root either because
1537 * it has no references, so we can blindly clear mr->enabled.
1538 * memory_region_set_enabled instead could trigger a transaction
1539 * and cause an infinite loop.
1541 mr
->enabled
= false;
1542 memory_region_transaction_begin();
1543 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1544 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1545 memory_region_del_subregion(mr
, subregion
);
1547 memory_region_transaction_commit();
1550 memory_region_clear_coalescing(mr
);
1551 g_free((char *)mr
->name
);
1552 g_free(mr
->ioeventfds
);
1555 Object
*memory_region_owner(MemoryRegion
*mr
)
1557 Object
*obj
= OBJECT(mr
);
1561 void memory_region_ref(MemoryRegion
*mr
)
1563 /* MMIO callbacks most likely will access data that belongs
1564 * to the owner, hence the need to ref/unref the owner whenever
1565 * the memory region is in use.
1567 * The memory region is a child of its owner. As long as the
1568 * owner doesn't call unparent itself on the memory region,
1569 * ref-ing the owner will also keep the memory region alive.
1570 * Memory regions without an owner are supposed to never go away;
1571 * we do not ref/unref them because it slows down DMA sensibly.
1573 if (mr
&& mr
->owner
) {
1574 object_ref(mr
->owner
);
1578 void memory_region_unref(MemoryRegion
*mr
)
1580 if (mr
&& mr
->owner
) {
1581 object_unref(mr
->owner
);
1585 uint64_t memory_region_size(MemoryRegion
*mr
)
1587 if (int128_eq(mr
->size
, int128_2_64())) {
1590 return int128_get64(mr
->size
);
1593 const char *memory_region_name(const MemoryRegion
*mr
)
1596 ((MemoryRegion
*)mr
)->name
=
1597 object_get_canonical_path_component(OBJECT(mr
));
1602 bool memory_region_is_ram_device(MemoryRegion
*mr
)
1604 return mr
->ram_device
;
1607 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1609 uint8_t mask
= mr
->dirty_log_mask
;
1610 if (global_dirty_log
&& mr
->ram_block
) {
1611 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1616 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1618 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1621 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion
*iommu_mr
)
1623 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1624 IOMMUNotifier
*iommu_notifier
;
1625 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1627 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1628 flags
|= iommu_notifier
->notifier_flags
;
1631 if (flags
!= iommu_mr
->iommu_notify_flags
&& imrc
->notify_flag_changed
) {
1632 imrc
->notify_flag_changed(iommu_mr
,
1633 iommu_mr
->iommu_notify_flags
,
1637 iommu_mr
->iommu_notify_flags
= flags
;
1640 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1643 IOMMUMemoryRegion
*iommu_mr
;
1646 memory_region_register_iommu_notifier(mr
->alias
, n
);
1650 /* We need to register for at least one bitfield */
1651 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1652 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1653 assert(n
->start
<= n
->end
);
1654 QLIST_INSERT_HEAD(&iommu_mr
->iommu_notify
, n
, node
);
1655 memory_region_update_iommu_notify_flags(iommu_mr
);
1658 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
)
1660 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1662 if (imrc
->get_min_page_size
) {
1663 return imrc
->get_min_page_size(iommu_mr
);
1665 return TARGET_PAGE_SIZE
;
1668 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
)
1670 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
1671 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr
);
1672 hwaddr addr
, granularity
;
1673 IOMMUTLBEntry iotlb
;
1675 /* If the IOMMU has its own replay callback, override */
1677 imrc
->replay(iommu_mr
, n
);
1681 granularity
= memory_region_iommu_get_min_page_size(iommu_mr
);
1683 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1684 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
);
1685 if (iotlb
.perm
!= IOMMU_NONE
) {
1686 n
->notify(n
, &iotlb
);
1689 /* if (2^64 - MR size) < granularity, it's possible to get an
1690 * infinite loop here. This should catch such a wraparound */
1691 if ((addr
+ granularity
) < addr
) {
1697 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
)
1699 IOMMUNotifier
*notifier
;
1701 IOMMU_NOTIFIER_FOREACH(notifier
, iommu_mr
) {
1702 memory_region_iommu_replay(iommu_mr
, notifier
);
1706 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1709 IOMMUMemoryRegion
*iommu_mr
;
1712 memory_region_unregister_iommu_notifier(mr
->alias
, n
);
1715 QLIST_REMOVE(n
, node
);
1716 iommu_mr
= IOMMU_MEMORY_REGION(mr
);
1717 memory_region_update_iommu_notify_flags(iommu_mr
);
1720 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1721 IOMMUTLBEntry
*entry
)
1723 IOMMUNotifierFlag request_flags
;
1726 * Skip the notification if the notification does not overlap
1727 * with registered range.
1729 if (notifier
->start
> entry
->iova
+ entry
->addr_mask
+ 1 ||
1730 notifier
->end
< entry
->iova
) {
1734 if (entry
->perm
& IOMMU_RW
) {
1735 request_flags
= IOMMU_NOTIFIER_MAP
;
1737 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1740 if (notifier
->notifier_flags
& request_flags
) {
1741 notifier
->notify(notifier
, entry
);
1745 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1746 IOMMUTLBEntry entry
)
1748 IOMMUNotifier
*iommu_notifier
;
1750 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr
)));
1752 IOMMU_NOTIFIER_FOREACH(iommu_notifier
, iommu_mr
) {
1753 memory_region_notify_one(iommu_notifier
, &entry
);
1757 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1759 uint8_t mask
= 1 << client
;
1760 uint8_t old_logging
;
1762 assert(client
== DIRTY_MEMORY_VGA
);
1763 old_logging
= mr
->vga_logging_count
;
1764 mr
->vga_logging_count
+= log
? 1 : -1;
1765 if (!!old_logging
== !!mr
->vga_logging_count
) {
1769 memory_region_transaction_begin();
1770 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1771 memory_region_update_pending
|= mr
->enabled
;
1772 memory_region_transaction_commit();
1775 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1776 hwaddr size
, unsigned client
)
1778 assert(mr
->ram_block
);
1779 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr
) + addr
,
1783 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1786 assert(mr
->ram_block
);
1787 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
1789 memory_region_get_dirty_log_mask(mr
));
1792 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1793 hwaddr size
, unsigned client
)
1795 assert(mr
->ram_block
);
1796 return cpu_physical_memory_test_and_clear_dirty(
1797 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1800 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1805 assert(mr
->ram_block
);
1806 return cpu_physical_memory_snapshot_and_clear_dirty(
1807 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1810 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
, DirtyBitmapSnapshot
*snap
,
1811 hwaddr addr
, hwaddr size
)
1813 assert(mr
->ram_block
);
1814 return cpu_physical_memory_snapshot_get_dirty(snap
,
1815 memory_region_get_ram_addr(mr
) + addr
, size
);
1818 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1820 MemoryListener
*listener
;
1825 /* If the same address space has multiple log_sync listeners, we
1826 * visit that address space's FlatView multiple times. But because
1827 * log_sync listeners are rare, it's still cheaper than walking each
1828 * address space once.
1830 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1831 if (!listener
->log_sync
) {
1834 as
= listener
->address_space
;
1835 view
= address_space_get_flatview(as
);
1836 FOR_EACH_FLAT_RANGE(fr
, view
) {
1838 MemoryRegionSection mrs
= section_from_flat_range(fr
, as
);
1839 listener
->log_sync(listener
, &mrs
);
1842 flatview_unref(view
);
1846 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1848 if (mr
->readonly
!= readonly
) {
1849 memory_region_transaction_begin();
1850 mr
->readonly
= readonly
;
1851 memory_region_update_pending
|= mr
->enabled
;
1852 memory_region_transaction_commit();
1856 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1858 if (mr
->romd_mode
!= romd_mode
) {
1859 memory_region_transaction_begin();
1860 mr
->romd_mode
= romd_mode
;
1861 memory_region_update_pending
|= mr
->enabled
;
1862 memory_region_transaction_commit();
1866 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1867 hwaddr size
, unsigned client
)
1869 assert(mr
->ram_block
);
1870 cpu_physical_memory_test_and_clear_dirty(
1871 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1874 int memory_region_get_fd(MemoryRegion
*mr
)
1882 fd
= mr
->ram_block
->fd
;
1888 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1891 uint64_t offset
= 0;
1895 offset
+= mr
->alias_offset
;
1898 assert(mr
->ram_block
);
1899 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
1905 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
1909 block
= qemu_ram_block_from_host(ptr
, false, offset
);
1917 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1919 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
1922 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1924 assert(mr
->ram_block
);
1926 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
1929 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1933 CoalescedMemoryRange
*cmr
;
1935 MemoryRegionSection section
;
1937 view
= address_space_get_flatview(as
);
1938 FOR_EACH_FLAT_RANGE(fr
, view
) {
1940 section
= (MemoryRegionSection
) {
1941 .address_space
= as
,
1942 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1943 .size
= fr
->addr
.size
,
1946 MEMORY_LISTENER_CALL(as
, coalesced_mmio_del
, Reverse
, §ion
,
1947 int128_get64(fr
->addr
.start
),
1948 int128_get64(fr
->addr
.size
));
1949 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1950 tmp
= addrrange_shift(cmr
->addr
,
1951 int128_sub(fr
->addr
.start
,
1952 int128_make64(fr
->offset_in_region
)));
1953 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1956 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1957 MEMORY_LISTENER_CALL(as
, coalesced_mmio_add
, Forward
, §ion
,
1958 int128_get64(tmp
.start
),
1959 int128_get64(tmp
.size
));
1963 flatview_unref(view
);
1966 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1970 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1971 memory_region_update_coalesced_range_as(mr
, as
);
1975 void memory_region_set_coalescing(MemoryRegion
*mr
)
1977 memory_region_clear_coalescing(mr
);
1978 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1981 void memory_region_add_coalescing(MemoryRegion
*mr
,
1985 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1987 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1988 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1989 memory_region_update_coalesced_range(mr
);
1990 memory_region_set_flush_coalesced(mr
);
1993 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1995 CoalescedMemoryRange
*cmr
;
1996 bool updated
= false;
1998 qemu_flush_coalesced_mmio_buffer();
1999 mr
->flush_coalesced_mmio
= false;
2001 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
2002 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
2003 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
2009 memory_region_update_coalesced_range(mr
);
2013 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
2015 mr
->flush_coalesced_mmio
= true;
2018 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
2020 qemu_flush_coalesced_mmio_buffer();
2021 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
2022 mr
->flush_coalesced_mmio
= false;
2026 void memory_region_set_global_locking(MemoryRegion
*mr
)
2028 mr
->global_locking
= true;
2031 void memory_region_clear_global_locking(MemoryRegion
*mr
)
2033 mr
->global_locking
= false;
2036 static bool userspace_eventfd_warning
;
2038 void memory_region_add_eventfd(MemoryRegion
*mr
,
2045 MemoryRegionIoeventfd mrfd
= {
2046 .addr
.start
= int128_make64(addr
),
2047 .addr
.size
= int128_make64(size
),
2048 .match_data
= match_data
,
2054 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2055 userspace_eventfd_warning
))) {
2056 userspace_eventfd_warning
= true;
2057 error_report("Using eventfd without MMIO binding in KVM. "
2058 "Suboptimal performance expected");
2062 adjust_endianness(mr
, &mrfd
.data
, size
);
2064 memory_region_transaction_begin();
2065 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2066 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
2071 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2072 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
2073 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
2074 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
2075 mr
->ioeventfds
[i
] = mrfd
;
2076 ioeventfd_update_pending
|= mr
->enabled
;
2077 memory_region_transaction_commit();
2080 void memory_region_del_eventfd(MemoryRegion
*mr
,
2087 MemoryRegionIoeventfd mrfd
= {
2088 .addr
.start
= int128_make64(addr
),
2089 .addr
.size
= int128_make64(size
),
2090 .match_data
= match_data
,
2097 adjust_endianness(mr
, &mrfd
.data
, size
);
2099 memory_region_transaction_begin();
2100 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
2101 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
2105 assert(i
!= mr
->ioeventfd_nb
);
2106 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
2107 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
2109 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
2110 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
2111 ioeventfd_update_pending
|= mr
->enabled
;
2112 memory_region_transaction_commit();
2115 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
2117 MemoryRegion
*mr
= subregion
->container
;
2118 MemoryRegion
*other
;
2120 memory_region_transaction_begin();
2122 memory_region_ref(subregion
);
2123 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
2124 if (subregion
->priority
>= other
->priority
) {
2125 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
2129 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
2131 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2132 memory_region_transaction_commit();
2135 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
2137 MemoryRegion
*subregion
)
2139 assert(!subregion
->container
);
2140 subregion
->container
= mr
;
2141 subregion
->addr
= offset
;
2142 memory_region_update_container_subregions(subregion
);
2145 void memory_region_add_subregion(MemoryRegion
*mr
,
2147 MemoryRegion
*subregion
)
2149 subregion
->priority
= 0;
2150 memory_region_add_subregion_common(mr
, offset
, subregion
);
2153 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2155 MemoryRegion
*subregion
,
2158 subregion
->priority
= priority
;
2159 memory_region_add_subregion_common(mr
, offset
, subregion
);
2162 void memory_region_del_subregion(MemoryRegion
*mr
,
2163 MemoryRegion
*subregion
)
2165 memory_region_transaction_begin();
2166 assert(subregion
->container
== mr
);
2167 subregion
->container
= NULL
;
2168 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2169 memory_region_unref(subregion
);
2170 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2171 memory_region_transaction_commit();
2174 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2176 if (enabled
== mr
->enabled
) {
2179 memory_region_transaction_begin();
2180 mr
->enabled
= enabled
;
2181 memory_region_update_pending
= true;
2182 memory_region_transaction_commit();
2185 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2187 Int128 s
= int128_make64(size
);
2189 if (size
== UINT64_MAX
) {
2192 if (int128_eq(s
, mr
->size
)) {
2195 memory_region_transaction_begin();
2197 memory_region_update_pending
= true;
2198 memory_region_transaction_commit();
2201 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2203 MemoryRegion
*container
= mr
->container
;
2206 memory_region_transaction_begin();
2207 memory_region_ref(mr
);
2208 memory_region_del_subregion(container
, mr
);
2209 mr
->container
= container
;
2210 memory_region_update_container_subregions(mr
);
2211 memory_region_unref(mr
);
2212 memory_region_transaction_commit();
2216 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2218 if (addr
!= mr
->addr
) {
2220 memory_region_readd_subregion(mr
);
2224 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2228 if (offset
== mr
->alias_offset
) {
2232 memory_region_transaction_begin();
2233 mr
->alias_offset
= offset
;
2234 memory_region_update_pending
|= mr
->enabled
;
2235 memory_region_transaction_commit();
2238 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2243 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2245 const AddrRange
*addr
= addr_
;
2246 const FlatRange
*fr
= fr_
;
2248 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2250 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2256 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2258 return bsearch(&addr
, view
->ranges
, view
->nr
,
2259 sizeof(FlatRange
), cmp_flatrange_addr
);
2262 bool memory_region_is_mapped(MemoryRegion
*mr
)
2264 return mr
->container
? true : false;
2267 /* Same as memory_region_find, but it does not add a reference to the
2268 * returned region. It must be called from an RCU critical section.
2270 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2271 hwaddr addr
, uint64_t size
)
2273 MemoryRegionSection ret
= { .mr
= NULL
};
2281 for (root
= mr
; root
->container
; ) {
2282 root
= root
->container
;
2286 as
= memory_region_to_address_space(root
);
2290 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2292 view
= atomic_rcu_read(&as
->current_map
);
2293 fr
= flatview_lookup(view
, range
);
2298 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2303 ret
.address_space
= as
;
2304 range
= addrrange_intersection(range
, fr
->addr
);
2305 ret
.offset_within_region
= fr
->offset_in_region
;
2306 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2308 ret
.size
= range
.size
;
2309 ret
.offset_within_address_space
= int128_get64(range
.start
);
2310 ret
.readonly
= fr
->readonly
;
2314 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2315 hwaddr addr
, uint64_t size
)
2317 MemoryRegionSection ret
;
2319 ret
= memory_region_find_rcu(mr
, addr
, size
);
2321 memory_region_ref(ret
.mr
);
2327 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2332 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2334 return mr
&& mr
!= container
;
2337 void memory_global_dirty_log_sync(void)
2339 MemoryListener
*listener
;
2344 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2345 if (!listener
->log_sync
) {
2348 as
= listener
->address_space
;
2349 view
= address_space_get_flatview(as
);
2350 FOR_EACH_FLAT_RANGE(fr
, view
) {
2351 if (fr
->dirty_log_mask
) {
2352 MemoryRegionSection mrs
= section_from_flat_range(fr
, as
);
2353 listener
->log_sync(listener
, &mrs
);
2356 flatview_unref(view
);
2360 static VMChangeStateEntry
*vmstate_change
;
2362 void memory_global_dirty_log_start(void)
2364 if (vmstate_change
) {
2365 qemu_del_vm_change_state_handler(vmstate_change
);
2366 vmstate_change
= NULL
;
2369 global_dirty_log
= true;
2371 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2373 /* Refresh DIRTY_LOG_MIGRATION bit. */
2374 memory_region_transaction_begin();
2375 memory_region_update_pending
= true;
2376 memory_region_transaction_commit();
2379 static void memory_global_dirty_log_do_stop(void)
2381 global_dirty_log
= false;
2383 /* Refresh DIRTY_LOG_MIGRATION bit. */
2384 memory_region_transaction_begin();
2385 memory_region_update_pending
= true;
2386 memory_region_transaction_commit();
2388 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2391 static void memory_vm_change_state_handler(void *opaque
, int running
,
2395 memory_global_dirty_log_do_stop();
2397 if (vmstate_change
) {
2398 qemu_del_vm_change_state_handler(vmstate_change
);
2399 vmstate_change
= NULL
;
2404 void memory_global_dirty_log_stop(void)
2406 if (!runstate_is_running()) {
2407 if (vmstate_change
) {
2410 vmstate_change
= qemu_add_vm_change_state_handler(
2411 memory_vm_change_state_handler
, NULL
);
2415 memory_global_dirty_log_do_stop();
2418 static void listener_add_address_space(MemoryListener
*listener
,
2424 if (listener
->begin
) {
2425 listener
->begin(listener
);
2427 if (global_dirty_log
) {
2428 if (listener
->log_global_start
) {
2429 listener
->log_global_start(listener
);
2433 view
= address_space_get_flatview(as
);
2434 FOR_EACH_FLAT_RANGE(fr
, view
) {
2435 MemoryRegionSection section
= {
2437 .address_space
= as
,
2438 .offset_within_region
= fr
->offset_in_region
,
2439 .size
= fr
->addr
.size
,
2440 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2441 .readonly
= fr
->readonly
,
2443 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2444 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2446 if (listener
->region_add
) {
2447 listener
->region_add(listener
, §ion
);
2450 if (listener
->commit
) {
2451 listener
->commit(listener
);
2453 flatview_unref(view
);
2456 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*as
)
2458 MemoryListener
*other
= NULL
;
2460 listener
->address_space
= as
;
2461 if (QTAILQ_EMPTY(&memory_listeners
)
2462 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2463 memory_listeners
)->priority
) {
2464 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2466 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2467 if (listener
->priority
< other
->priority
) {
2471 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2474 if (QTAILQ_EMPTY(&as
->listeners
)
2475 || listener
->priority
>= QTAILQ_LAST(&as
->listeners
,
2476 memory_listeners
)->priority
) {
2477 QTAILQ_INSERT_TAIL(&as
->listeners
, listener
, link_as
);
2479 QTAILQ_FOREACH(other
, &as
->listeners
, link_as
) {
2480 if (listener
->priority
< other
->priority
) {
2484 QTAILQ_INSERT_BEFORE(other
, listener
, link_as
);
2487 listener_add_address_space(listener
, as
);
2490 void memory_listener_unregister(MemoryListener
*listener
)
2492 if (!listener
->address_space
) {
2496 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2497 QTAILQ_REMOVE(&listener
->address_space
->listeners
, listener
, link_as
);
2498 listener
->address_space
= NULL
;
2501 bool memory_region_request_mmio_ptr(MemoryRegion
*mr
, hwaddr addr
)
2505 unsigned offset
= 0;
2506 Object
*new_interface
;
2508 if (!mr
|| !mr
->ops
->request_ptr
) {
2513 * Avoid an update if the request_ptr call
2514 * memory_region_invalidate_mmio_ptr which seems to be likely when we use
2517 memory_region_transaction_begin();
2519 host
= mr
->ops
->request_ptr(mr
->opaque
, addr
- mr
->addr
, &size
, &offset
);
2521 if (!host
|| !size
) {
2522 memory_region_transaction_commit();
2526 new_interface
= object_new("mmio_interface");
2527 qdev_prop_set_uint64(DEVICE(new_interface
), "start", offset
);
2528 qdev_prop_set_uint64(DEVICE(new_interface
), "end", offset
+ size
- 1);
2529 qdev_prop_set_bit(DEVICE(new_interface
), "ro", true);
2530 qdev_prop_set_ptr(DEVICE(new_interface
), "host_ptr", host
);
2531 qdev_prop_set_ptr(DEVICE(new_interface
), "subregion", mr
);
2532 object_property_set_bool(OBJECT(new_interface
), true, "realized", NULL
);
2534 memory_region_transaction_commit();
2538 typedef struct MMIOPtrInvalidate
{
2544 } MMIOPtrInvalidate
;
2546 #define MAX_MMIO_INVALIDATE 10
2547 static MMIOPtrInvalidate mmio_ptr_invalidate_list
[MAX_MMIO_INVALIDATE
];
2549 static void memory_region_do_invalidate_mmio_ptr(CPUState
*cpu
,
2550 run_on_cpu_data data
)
2552 MMIOPtrInvalidate
*invalidate_data
= (MMIOPtrInvalidate
*)data
.host_ptr
;
2553 MemoryRegion
*mr
= invalidate_data
->mr
;
2554 hwaddr offset
= invalidate_data
->offset
;
2555 unsigned size
= invalidate_data
->size
;
2556 MemoryRegionSection section
= memory_region_find(mr
, offset
, size
);
2558 qemu_mutex_lock_iothread();
2560 /* Reset dirty so this doesn't happen later. */
2561 cpu_physical_memory_test_and_clear_dirty(offset
, size
, 1);
2563 if (section
.mr
!= mr
) {
2564 /* memory_region_find add a ref on section.mr */
2565 memory_region_unref(section
.mr
);
2566 if (MMIO_INTERFACE(section
.mr
->owner
)) {
2567 /* We found the interface just drop it. */
2568 object_property_set_bool(section
.mr
->owner
, false, "realized",
2570 object_unref(section
.mr
->owner
);
2571 object_unparent(section
.mr
->owner
);
2575 qemu_mutex_unlock_iothread();
2577 if (invalidate_data
->allocated
) {
2578 g_free(invalidate_data
);
2580 invalidate_data
->busy
= 0;
2584 void memory_region_invalidate_mmio_ptr(MemoryRegion
*mr
, hwaddr offset
,
2588 MMIOPtrInvalidate
*invalidate_data
= NULL
;
2590 for (i
= 0; i
< MAX_MMIO_INVALIDATE
; i
++) {
2591 if (atomic_cmpxchg(&(mmio_ptr_invalidate_list
[i
].busy
), 0, 1) == 0) {
2592 invalidate_data
= &mmio_ptr_invalidate_list
[i
];
2597 if (!invalidate_data
) {
2598 invalidate_data
= g_malloc0(sizeof(MMIOPtrInvalidate
));
2599 invalidate_data
->allocated
= 1;
2602 invalidate_data
->mr
= mr
;
2603 invalidate_data
->offset
= offset
;
2604 invalidate_data
->size
= size
;
2606 async_safe_run_on_cpu(first_cpu
, memory_region_do_invalidate_mmio_ptr
,
2607 RUN_ON_CPU_HOST_PTR(invalidate_data
));
2610 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2612 memory_region_ref(root
);
2613 memory_region_transaction_begin();
2616 as
->malloced
= false;
2617 as
->current_map
= g_new(FlatView
, 1);
2618 flatview_init(as
->current_map
);
2619 as
->ioeventfd_nb
= 0;
2620 as
->ioeventfds
= NULL
;
2621 QTAILQ_INIT(&as
->listeners
);
2622 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2623 as
->name
= g_strdup(name
? name
: "anonymous");
2624 address_space_init_dispatch(as
);
2625 memory_region_update_pending
|= root
->enabled
;
2626 memory_region_transaction_commit();
2629 static void do_address_space_destroy(AddressSpace
*as
)
2631 bool do_free
= as
->malloced
;
2633 address_space_destroy_dispatch(as
);
2634 assert(QTAILQ_EMPTY(&as
->listeners
));
2636 flatview_unref(as
->current_map
);
2638 g_free(as
->ioeventfds
);
2639 memory_region_unref(as
->root
);
2645 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
, const char *name
)
2649 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2650 if (root
== as
->root
&& as
->malloced
) {
2656 as
= g_malloc0(sizeof *as
);
2657 address_space_init(as
, root
, name
);
2658 as
->malloced
= true;
2662 void address_space_destroy(AddressSpace
*as
)
2664 MemoryRegion
*root
= as
->root
;
2667 if (as
->ref_count
) {
2670 /* Flush out anything from MemoryListeners listening in on this */
2671 memory_region_transaction_begin();
2673 memory_region_transaction_commit();
2674 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2675 address_space_unregister(as
);
2677 /* At this point, as->dispatch and as->current_map are dummy
2678 * entries that the guest should never use. Wait for the old
2679 * values to expire before freeing the data.
2682 call_rcu(as
, do_address_space_destroy
, rcu
);
2685 static const char *memory_region_type(MemoryRegion
*mr
)
2687 if (memory_region_is_ram_device(mr
)) {
2689 } else if (memory_region_is_romd(mr
)) {
2691 } else if (memory_region_is_rom(mr
)) {
2693 } else if (memory_region_is_ram(mr
)) {
2700 typedef struct MemoryRegionList MemoryRegionList
;
2702 struct MemoryRegionList
{
2703 const MemoryRegion
*mr
;
2704 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2707 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2709 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2710 int128_sub((size), int128_one())) : 0)
2711 #define MTREE_INDENT " "
2713 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2714 const MemoryRegion
*mr
, unsigned int level
,
2716 MemoryRegionListHead
*alias_print_queue
)
2718 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2719 MemoryRegionListHead submr_print_queue
;
2720 const MemoryRegion
*submr
;
2722 hwaddr cur_start
, cur_end
;
2728 for (i
= 0; i
< level
; i
++) {
2729 mon_printf(f
, MTREE_INDENT
);
2732 cur_start
= base
+ mr
->addr
;
2733 cur_end
= cur_start
+ MR_SIZE(mr
->size
);
2736 * Try to detect overflow of memory region. This should never
2737 * happen normally. When it happens, we dump something to warn the
2738 * user who is observing this.
2740 if (cur_start
< base
|| cur_end
< cur_start
) {
2741 mon_printf(f
, "[DETECTED OVERFLOW!] ");
2745 MemoryRegionList
*ml
;
2748 /* check if the alias is already in the queue */
2749 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2750 if (ml
->mr
== mr
->alias
) {
2756 ml
= g_new(MemoryRegionList
, 1);
2758 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2760 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2761 " (prio %d, %s): alias %s @%s " TARGET_FMT_plx
2762 "-" TARGET_FMT_plx
"%s\n",
2765 memory_region_type((MemoryRegion
*)mr
),
2766 memory_region_name(mr
),
2767 memory_region_name(mr
->alias
),
2769 mr
->alias_offset
+ MR_SIZE(mr
->size
),
2770 mr
->enabled
? "" : " [disabled]");
2773 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %s): %s%s\n",
2776 memory_region_type((MemoryRegion
*)mr
),
2777 memory_region_name(mr
),
2778 mr
->enabled
? "" : " [disabled]");
2781 QTAILQ_INIT(&submr_print_queue
);
2783 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2784 new_ml
= g_new(MemoryRegionList
, 1);
2786 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2787 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2788 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2789 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2790 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2796 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2800 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2801 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, cur_start
,
2805 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2810 static void mtree_print_flatview(fprintf_function p
, void *f
,
2813 FlatView
*view
= address_space_get_flatview(as
);
2814 FlatRange
*range
= &view
->ranges
[0];
2819 p(f
, MTREE_INDENT
"No rendered FlatView for "
2820 "address space '%s'\n", as
->name
);
2821 flatview_unref(view
);
2827 if (range
->offset_in_region
) {
2828 p(f
, MTREE_INDENT TARGET_FMT_plx
"-"
2829 TARGET_FMT_plx
" (prio %d, %s): %s @" TARGET_FMT_plx
"\n",
2830 int128_get64(range
->addr
.start
),
2831 int128_get64(range
->addr
.start
) + MR_SIZE(range
->addr
.size
),
2833 range
->readonly
? "rom" : memory_region_type(mr
),
2834 memory_region_name(mr
),
2835 range
->offset_in_region
);
2837 p(f
, MTREE_INDENT TARGET_FMT_plx
"-"
2838 TARGET_FMT_plx
" (prio %d, %s): %s\n",
2839 int128_get64(range
->addr
.start
),
2840 int128_get64(range
->addr
.start
) + MR_SIZE(range
->addr
.size
),
2842 range
->readonly
? "rom" : memory_region_type(mr
),
2843 memory_region_name(mr
));
2848 flatview_unref(view
);
2851 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
)
2853 MemoryRegionListHead ml_head
;
2854 MemoryRegionList
*ml
, *ml2
;
2858 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2859 mon_printf(f
, "address-space (flat view): %s\n", as
->name
);
2860 mtree_print_flatview(mon_printf
, f
, as
);
2861 mon_printf(f
, "\n");
2866 QTAILQ_INIT(&ml_head
);
2868 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2869 mon_printf(f
, "address-space: %s\n", as
->name
);
2870 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2871 mon_printf(f
, "\n");
2874 /* print aliased regions */
2875 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2876 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2877 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2878 mon_printf(f
, "\n");
2881 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2886 void memory_region_init_ram(MemoryRegion
*mr
,
2887 struct Object
*owner
,
2892 DeviceState
*owner_dev
;
2895 memory_region_init_ram_nomigrate(mr
, owner
, name
, size
, &err
);
2897 error_propagate(errp
, err
);
2900 /* This will assert if owner is neither NULL nor a DeviceState.
2901 * We only want the owner here for the purposes of defining a
2902 * unique name for migration. TODO: Ideally we should implement
2903 * a naming scheme for Objects which are not DeviceStates, in
2904 * which case we can relax this restriction.
2906 owner_dev
= DEVICE(owner
);
2907 vmstate_register_ram(mr
, owner_dev
);
2910 void memory_region_init_rom(MemoryRegion
*mr
,
2911 struct Object
*owner
,
2916 DeviceState
*owner_dev
;
2919 memory_region_init_rom_nomigrate(mr
, owner
, name
, size
, &err
);
2921 error_propagate(errp
, err
);
2924 /* This will assert if owner is neither NULL nor a DeviceState.
2925 * We only want the owner here for the purposes of defining a
2926 * unique name for migration. TODO: Ideally we should implement
2927 * a naming scheme for Objects which are not DeviceStates, in
2928 * which case we can relax this restriction.
2930 owner_dev
= DEVICE(owner
);
2931 vmstate_register_ram(mr
, owner_dev
);
2934 void memory_region_init_rom_device(MemoryRegion
*mr
,
2935 struct Object
*owner
,
2936 const MemoryRegionOps
*ops
,
2942 DeviceState
*owner_dev
;
2945 memory_region_init_rom_device_nomigrate(mr
, owner
, ops
, opaque
,
2948 error_propagate(errp
, err
);
2951 /* This will assert if owner is neither NULL nor a DeviceState.
2952 * We only want the owner here for the purposes of defining a
2953 * unique name for migration. TODO: Ideally we should implement
2954 * a naming scheme for Objects which are not DeviceStates, in
2955 * which case we can relax this restriction.
2957 owner_dev
= DEVICE(owner
);
2958 vmstate_register_ram(mr
, owner_dev
);
2961 static const TypeInfo memory_region_info
= {
2962 .parent
= TYPE_OBJECT
,
2963 .name
= TYPE_MEMORY_REGION
,
2964 .instance_size
= sizeof(MemoryRegion
),
2965 .instance_init
= memory_region_initfn
,
2966 .instance_finalize
= memory_region_finalize
,
2969 static const TypeInfo iommu_memory_region_info
= {
2970 .parent
= TYPE_MEMORY_REGION
,
2971 .name
= TYPE_IOMMU_MEMORY_REGION
,
2972 .class_size
= sizeof(IOMMUMemoryRegionClass
),
2973 .instance_size
= sizeof(IOMMUMemoryRegion
),
2974 .instance_init
= iommu_memory_region_initfn
,
2978 static void memory_register_types(void)
2980 type_register_static(&memory_region_info
);
2981 type_register_static(&iommu_memory_region_info
);
2984 type_init(memory_register_types
)